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 {
// TODO(fgorski): flip this to finch controlled setting.
private final static long MINIMUM_FETCH_DELAY_MILLIS = 2 * 1000; // 2 seconds.
private static boolean sDisableDelayForTesting;
private final Delegate mDelegate;
private TabModelSelector mTabModelSelector;
private TabModelSelectorTabModelObserver mTabModelObserver;
......@@ -265,7 +267,7 @@ class FetchHelper {
String url = tabFetchReadinessState.getUrl();
long remainingFetchDelayMillis =
SystemClock.uptimeMillis() - tabFetchReadinessState.getFetchTimeBaselineMillis();
if (remainingFetchDelayMillis < MINIMUM_FETCH_DELAY_MILLIS) {
if (!sDisableDelayForTesting && remainingFetchDelayMillis < MINIMUM_FETCH_DELAY_MILLIS) {
postDelayedFetch(
url, mCurrentTab, MINIMUM_FETCH_DELAY_MILLIS - remainingFetchDelayMillis);
return;
......@@ -333,4 +335,9 @@ class FetchHelper {
if (tab == null) return null;
return mObservedTabs.get(tab.getId());
}
@VisibleForTesting
static void setDisableDelayForTesting(boolean disable) {
sDisableDelayForTesting = disable;
}
}
......@@ -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/FakeContextualSuggestionsSource.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/MinidumpUploadServiceTest.java",
"javatests/src/org/chromium/chrome/browser/crash/PureJavaExceptionReporterTest.java",
......
......@@ -12,7 +12,6 @@ import static org.junit.Assert.assertTrue;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import org.junit.After;
import org.junit.Before;
......@@ -42,7 +41,6 @@ import org.chromium.content.browser.test.util.TestWebContentsObserver;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.ui.test.util.UiRestriction;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
/**
......@@ -76,7 +74,7 @@ public class ContextualSuggestionsTest {
public void setUp() throws Exception {
mContextualSuggestionsDeps.getFactory().suggestionsSource =
new FakeContextualSuggestionsSource();
mContextualSuggestionsDeps.getFactory().fetchHelper = new FakeFetchHelper();
FetchHelper.setDisableDelayForTesting(true);
FakeEnabledStateMonitor stateMonitor = new FakeEnabledStateMonitor();
mContextualSuggestionsDeps.getFactory().enabledStateMonitor = new FakeEnabledStateMonitor();
......@@ -98,21 +96,13 @@ public class ContextualSuggestionsTest {
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
FetchHelper.setDisableDelayForTesting(false);
}
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
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",
mBottomSheet.getCurrentSheetContent()
instanceof ContextualSuggestionsBottomSheetContent);
......@@ -149,7 +139,7 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testCloseFromPeek() {
public void testCloseFromPeek() throws InterruptedException, TimeoutException {
forceShowSuggestions();
simulateClickOnCloseButton();
}
......@@ -157,7 +147,7 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testCloseFromOpen() {
public void testCloseFromOpen() throws InterruptedException, TimeoutException {
forceShowSuggestions();
openSheet();
simulateClickOnCloseButton();
......@@ -166,10 +156,15 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testTriggerMultipleTimes() {
public void testTriggerMultipleTimes() throws InterruptedException, TimeoutException {
// Show one time and close.
forceShowSuggestions();
openSheet();
simulateClickOnCloseButton();
// Show a second time.
ThreadUtils.runOnUiThreadBlocking(
() -> mMediator.requestSuggestions("http://www.testurl.com"));
forceShowSuggestions();
openSheet();
}
......@@ -205,7 +200,7 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testOpenArticleInNewTab() throws InterruptedException, ExecutionException {
public void testOpenArticleInNewTab() throws Exception {
forceShowSuggestions();
openSheet();
......@@ -222,8 +217,7 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testOpenSuggestionInNewTabIncognito()
throws InterruptedException, ExecutionException {
public void testOpenSuggestionInNewTabIncognito() throws Exception {
forceShowSuggestions();
openSheet();
......@@ -241,7 +235,7 @@ public class ContextualSuggestionsTest {
@Test
@MediumTest
@Feature({"ContextualSuggestions"})
public void testEnterTabSwitcher() throws InterruptedException, ExecutionException {
public void testEnterTabSwitcher() throws Exception {
forceShowSuggestions();
ThreadUtils.runOnUiThreadBlocking(() -> {
......@@ -261,9 +255,12 @@ public class ContextualSuggestionsTest {
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(() -> {
mMediator.requestSuggestions("http://www.testurl.com");
mMediator.showContentInSheetForTesting();
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