Commit 59a595e1 authored by Theresa's avatar Theresa Committed by Commit Bot

[EoC] Use real FetchHelper for ContextualSuggestionsTest suite

Use the real FetchHelper, with the request delay removed, for tests.
This is a better end-to-end integration test as fewer things are
stubbed out.

BUG=827348

Change-Id: I34e588d032b0a48871b71a531328f87f705d3716
Reviewed-on: https://chromium-review.googlesource.com/1024055Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#552748}
parent bca97a81
...@@ -112,6 +112,8 @@ class FetchHelper { ...@@ -112,6 +112,8 @@ class FetchHelper {
// TODO(fgorski): flip this to finch controlled setting. // TODO(fgorski): flip this to finch controlled setting.
private final static long MINIMUM_FETCH_DELAY_MILLIS = 2 * 1000; // 2 seconds. private final static long MINIMUM_FETCH_DELAY_MILLIS = 2 * 1000; // 2 seconds.
private static boolean sDisableDelayForTesting;
private final Delegate mDelegate; private final Delegate mDelegate;
private TabModelSelector mTabModelSelector; private TabModelSelector mTabModelSelector;
private TabModelSelectorTabModelObserver mTabModelObserver; private TabModelSelectorTabModelObserver mTabModelObserver;
...@@ -265,7 +267,7 @@ class FetchHelper { ...@@ -265,7 +267,7 @@ class FetchHelper {
String url = tabFetchReadinessState.getUrl(); String url = tabFetchReadinessState.getUrl();
long remainingFetchDelayMillis = long remainingFetchDelayMillis =
SystemClock.uptimeMillis() - tabFetchReadinessState.getFetchTimeBaselineMillis(); SystemClock.uptimeMillis() - tabFetchReadinessState.getFetchTimeBaselineMillis();
if (remainingFetchDelayMillis < MINIMUM_FETCH_DELAY_MILLIS) { if (!sDisableDelayForTesting && remainingFetchDelayMillis < MINIMUM_FETCH_DELAY_MILLIS) {
postDelayedFetch( postDelayedFetch(
url, mCurrentTab, MINIMUM_FETCH_DELAY_MILLIS - remainingFetchDelayMillis); url, mCurrentTab, MINIMUM_FETCH_DELAY_MILLIS - remainingFetchDelayMillis);
return; return;
...@@ -333,4 +335,9 @@ class FetchHelper { ...@@ -333,4 +335,9 @@ class FetchHelper {
if (tab == null) return null; if (tab == null) return null;
return mObservedTabs.get(tab.getId()); return mObservedTabs.get(tab.getId());
} }
@VisibleForTesting
static void setDisableDelayForTesting(boolean disable) {
sDisableDelayForTesting = disable;
}
} }
...@@ -1593,7 +1593,6 @@ chrome_test_java_sources = [ ...@@ -1593,7 +1593,6 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/contextual_suggestions/ContextualSuggestionsTest.java", "javatests/src/org/chromium/chrome/browser/contextual_suggestions/ContextualSuggestionsTest.java",
"javatests/src/org/chromium/chrome/browser/contextual_suggestions/FakeContextualSuggestionsSource.java", "javatests/src/org/chromium/chrome/browser/contextual_suggestions/FakeContextualSuggestionsSource.java",
"javatests/src/org/chromium/chrome/browser/contextual_suggestions/FakeEnabledStateMonitor.java", "javatests/src/org/chromium/chrome/browser/contextual_suggestions/FakeEnabledStateMonitor.java",
"javatests/src/org/chromium/chrome/browser/contextual_suggestions/FakeFetchHelper.java",
"javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java", "javatests/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableTest.java",
"javatests/src/org/chromium/chrome/browser/crash/MinidumpUploadServiceTest.java", "javatests/src/org/chromium/chrome/browser/crash/MinidumpUploadServiceTest.java",
"javatests/src/org/chromium/chrome/browser/crash/PureJavaExceptionReporterTest.java", "javatests/src/org/chromium/chrome/browser/crash/PureJavaExceptionReporterTest.java",
......
...@@ -12,7 +12,6 @@ import static org.junit.Assert.assertTrue; ...@@ -12,7 +12,6 @@ import static org.junit.Assert.assertTrue;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -42,7 +41,6 @@ import org.chromium.content.browser.test.util.TestWebContentsObserver; ...@@ -42,7 +41,6 @@ import org.chromium.content.browser.test.util.TestWebContentsObserver;
import org.chromium.net.test.EmbeddedTestServer; import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.ui.test.util.UiRestriction; import org.chromium.ui.test.util.UiRestriction;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
/** /**
...@@ -76,7 +74,7 @@ public class ContextualSuggestionsTest { ...@@ -76,7 +74,7 @@ public class ContextualSuggestionsTest {
public void setUp() throws Exception { public void setUp() throws Exception {
mContextualSuggestionsDeps.getFactory().suggestionsSource = mContextualSuggestionsDeps.getFactory().suggestionsSource =
new FakeContextualSuggestionsSource(); new FakeContextualSuggestionsSource();
mContextualSuggestionsDeps.getFactory().fetchHelper = new FakeFetchHelper(); FetchHelper.setDisableDelayForTesting(true);
FakeEnabledStateMonitor stateMonitor = new FakeEnabledStateMonitor(); FakeEnabledStateMonitor stateMonitor = new FakeEnabledStateMonitor();
mContextualSuggestionsDeps.getFactory().enabledStateMonitor = new FakeEnabledStateMonitor(); mContextualSuggestionsDeps.getFactory().enabledStateMonitor = new FakeEnabledStateMonitor();
...@@ -98,21 +96,13 @@ public class ContextualSuggestionsTest { ...@@ -98,21 +96,13 @@ public class ContextualSuggestionsTest {
@After @After
public void tearDown() { public void tearDown() {
mTestServer.stopAndDestroyServer(); mTestServer.stopAndDestroyServer();
FetchHelper.setDisableDelayForTesting(false);
} }
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testOpenContextualSuggestionsBottomSheet() { public void testOpenContextualSuggestionsBottomSheet() {
assertEquals("Sheet should be hidden.", BottomSheet.SHEET_STATE_HIDDEN,
mBottomSheet.getSheetState());
assertTrue("Title text should be empty, but was " + mModel.getTitle(),
TextUtils.isEmpty(mModel.getTitle()));
assertEquals("Cluster list should be empty.", 0, mModel.getClusterList().getItemCount());
ThreadUtils.runOnUiThreadBlocking(
() -> mMediator.requestSuggestions("http://www.testurl.com"));
assertTrue("Bottom sheet should contain suggestions content", assertTrue("Bottom sheet should contain suggestions content",
mBottomSheet.getCurrentSheetContent() mBottomSheet.getCurrentSheetContent()
instanceof ContextualSuggestionsBottomSheetContent); instanceof ContextualSuggestionsBottomSheetContent);
...@@ -149,7 +139,7 @@ public class ContextualSuggestionsTest { ...@@ -149,7 +139,7 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testCloseFromPeek() { public void testCloseFromPeek() throws InterruptedException, TimeoutException {
forceShowSuggestions(); forceShowSuggestions();
simulateClickOnCloseButton(); simulateClickOnCloseButton();
} }
...@@ -157,7 +147,7 @@ public class ContextualSuggestionsTest { ...@@ -157,7 +147,7 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testCloseFromOpen() { public void testCloseFromOpen() throws InterruptedException, TimeoutException {
forceShowSuggestions(); forceShowSuggestions();
openSheet(); openSheet();
simulateClickOnCloseButton(); simulateClickOnCloseButton();
...@@ -166,10 +156,15 @@ public class ContextualSuggestionsTest { ...@@ -166,10 +156,15 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testTriggerMultipleTimes() { public void testTriggerMultipleTimes() throws InterruptedException, TimeoutException {
// Show one time and close.
forceShowSuggestions(); forceShowSuggestions();
openSheet(); openSheet();
simulateClickOnCloseButton(); simulateClickOnCloseButton();
// Show a second time.
ThreadUtils.runOnUiThreadBlocking(
() -> mMediator.requestSuggestions("http://www.testurl.com"));
forceShowSuggestions(); forceShowSuggestions();
openSheet(); openSheet();
} }
...@@ -205,7 +200,7 @@ public class ContextualSuggestionsTest { ...@@ -205,7 +200,7 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testOpenArticleInNewTab() throws InterruptedException, ExecutionException { public void testOpenArticleInNewTab() throws Exception {
forceShowSuggestions(); forceShowSuggestions();
openSheet(); openSheet();
...@@ -222,8 +217,7 @@ public class ContextualSuggestionsTest { ...@@ -222,8 +217,7 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testOpenSuggestionInNewTabIncognito() public void testOpenSuggestionInNewTabIncognito() throws Exception {
throws InterruptedException, ExecutionException {
forceShowSuggestions(); forceShowSuggestions();
openSheet(); openSheet();
...@@ -241,7 +235,7 @@ public class ContextualSuggestionsTest { ...@@ -241,7 +235,7 @@ public class ContextualSuggestionsTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"ContextualSuggestions"}) @Feature({"ContextualSuggestions"})
public void testEnterTabSwitcher() throws InterruptedException, ExecutionException { public void testEnterTabSwitcher() throws Exception {
forceShowSuggestions(); forceShowSuggestions();
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
...@@ -261,9 +255,12 @@ public class ContextualSuggestionsTest { ...@@ -261,9 +255,12 @@ public class ContextualSuggestionsTest {
mBottomSheet.getSheetState()); mBottomSheet.getSheetState());
} }
private void forceShowSuggestions() { private void forceShowSuggestions() throws InterruptedException, TimeoutException {
assertEquals("Model has incorrect number of items.",
(int) FakeContextualSuggestionsSource.TOTAL_ITEM_COUNT,
mModel.getClusterList().getItemCount());
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
mMediator.requestSuggestions("http://www.testurl.com");
mMediator.showContentInSheetForTesting(); mMediator.showContentInSheetForTesting();
mBottomSheet.endAnimations(); mBottomSheet.endAnimations();
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.contextual_suggestions;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
/**
* A fake {@link FetchHelper} for use in testing.
*/
public class FakeFetchHelper extends FetchHelper {
FakeFetchHelper() {
super(null, null);
}
@Override
protected void init(TabModelSelector tabModelSelector) {
// Intentionally do nothing.
}
@Override
void destroy() {
// Intentionally do nothing.
}
}
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