Commit 662ea3b8 authored by Becky Zhou's avatar Becky Zhou Committed by Commit Bot

[EoC] Remember EoC was closed per tab/URL

If the contextual suggestions bottom sheet is closed by user click on
the close button, we don't show the sheet again until the page is
reloaded or a new URL is loaded. This state is stored in memory and
will be cleared on restart.

Bug: 838950
Change-Id: I64ddb66cbbca7ce1831baf9a65fa676d4c951770
Reviewed-on: https://chromium-review.googlesource.com/1070750Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Becky Zhou <huayinz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561596}
parent e0bdede2
......@@ -369,6 +369,8 @@ class ContextualSuggestionsMediator
: ContextualSuggestionsEvent.UI_DISMISSED_WITHOUT_OPEN;
reportEvent(openedEvent);
clearSuggestions();
assert mFetchHelper != null && mTabModelSelector.getCurrentTab() != null;
mFetchHelper.onSuggestionsDismissed(mTabModelSelector.getCurrentTab());
});
mModel.setMenuButtonVisibility(false);
if (!mModel.isSlimPeekEnabled()) {
......
......@@ -56,6 +56,7 @@ class FetchHelper {
class TabFetchReadinessState {
private long mFetchTimeBaselineMillis;
private String mUrl;
private boolean mSuggestionsDismissed;
TabFetchReadinessState(String url) {
updateUrl(url);
......@@ -69,6 +70,7 @@ class FetchHelper {
void updateUrl(String url) {
mUrl = URLUtil.isNetworkUrl(url) ? url : null;
mFetchTimeBaselineMillis = 0;
setSuggestionsDismissed(false);
}
/** @return The current URL tracked by this tab state. */
......@@ -76,9 +78,12 @@ class FetchHelper {
return mUrl;
}
/** @return Whether the tab state is tracking a tab with valid page loaded. */
/**
* @return Whether the tab state is tracking a tab with valid page loaded and valid status
* for fetching.
*/
boolean isTrackingPage() {
return mUrl != null;
return mUrl != null && !mSuggestionsDismissed;
}
/**
......@@ -104,6 +109,11 @@ class FetchHelper {
return mFetchTimeBaselineMillis != 0;
}
/** @param dismissed Whether the suggestions have been dismissed by the user. */
void setSuggestionsDismissed(boolean dismissed) {
mSuggestionsDismissed = dismissed;
}
/**
* Checks whether the provided url is the same (ignoring fragments) as the one tracked by
* tab state.
......@@ -395,6 +405,14 @@ class FetchHelper {
: mObservedTabs.get(tab.getId()).getFetchTimeBaselineMillis();
}
/**
* Called when suggestions were dismissed.
* @param tab The tab on which suggestions were dismissed.
*/
void onSuggestionsDismissed(@NonNull Tab tab) {
mObservedTabs.get(tab.getId()).setSuggestionsDismissed(true);
}
private boolean isFromGoogleSearchRequired() {
return mRequireCurrentPageFromSRP || mRequireNavChainFromSRP;
}
......
......@@ -282,6 +282,42 @@ public class ContextualSuggestionsTest {
openSheet();
}
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testTriggerAfterClose() throws InterruptedException, TimeoutException {
int firstTabId = mActivityTestRule.getActivity().getActivityTab().getId();
mActivityTestRule.loadUrlInNewTab(mTestServer.getURL(TEST_PAGE));
int secondTabId = mActivityTestRule.getActivity().getActivityTab().getId();
// Show suggestions on the second tab and simulate clicking close button.
forceShowSuggestions();
openSheet();
simulateClickOnCloseButton();
// Switch to the first tab and verify that the suggestions can be shown.
ChromeTabUtils.switchTabInCurrentTabModel(mActivityTestRule.getActivity(), firstTabId);
forceShowSuggestions();
// Switch to the second tab.
ChromeTabUtils.switchTabInCurrentTabModel(mActivityTestRule.getActivity(), secondTabId);
ThreadUtils.runOnUiThreadBlocking(() -> mBottomSheet.endAnimations());
// Simulate reverse scroll.
FullscreenManagerTestUtils.disableBrowserOverrides();
FullscreenManagerTestUtils.waitForBrowserControlsToBeMoveable(
mActivityTestRule, mActivityTestRule.getActivity().getActivityTab());
// Verify that the model is empty.
assertEquals("Model should be empty.", 0, mModel.getClusterList().getItemCount());
assertEquals("Bottom sheet should be hidden.", BottomSheet.SHEET_STATE_HIDDEN,
mBottomSheet.getSheetState());
// Switch to the first tab and verify that the suggestions can still be shown.
ChromeTabUtils.switchTabInCurrentTabModel(mActivityTestRule.getActivity(), firstTabId);
forceShowSuggestions();
}
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
......
......@@ -361,6 +361,32 @@ public final class FetchHelperTest {
verify(mDelegate, times(1)).requestSuggestions(eq(STARTING_URL));
}
@Test
public void switchTabs_suggestionsDismissed() {
FetchHelper helper = createFetchHelper();
addTab(mTab2);
// Wait for the fetch delay and verify that suggestions are requested for the first tab.
verify(mDelegate, times(1)).reportFetchDelayed(eq(mWebContents));
runUntilFetchPossible();
verify(mDelegate, times(1)).requestSuggestions(eq(STARTING_URL));
// Simulate suggestions dismissed by user on the first tab.
helper.onSuggestionsDismissed(mTab);
// Switch to the second tab, and verify that suggestions are requested without a delay.
selectTab(mTab2);
verify(mDelegate, times(1)).clearState();
verify(mDelegate, times(0)).reportFetchDelayed(eq(mWebContents2));
verify(mDelegate, times(1)).requestSuggestions(eq(DIFFERENT_URL));
// Switch back to the first tab, and verify that fetch is not requested.
selectTab(mTab);
verify(mDelegate, times(2)).clearState();
verify(mDelegate, times(1)).reportFetchDelayed(eq(mWebContents));
verify(mDelegate, times(1)).requestSuggestions(eq(STARTING_URL));
}
private void addTab(Tab tab) {
getTabModelObserver().didAddTab(tab, TabLaunchType.FROM_LINK);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment