Commit cc42e982 authored by Theresa's avatar Theresa Committed by Commit Bot

[EoC] Add more UI integration tests

Add UI integration tests for:
 - testScrollPageToTrigger
 - testShadowVisibleOnScroll
 - testInProductHelp
 - testMultiInstanceMode

And updated testEnterAndExitTabSwitcher.

BUG=835450

Change-Id: Ife58a7ba6dde69687d652f81fd2657cecdf5cb61
Reviewed-on: https://chromium-review.googlesource.com/1036795Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555232}
parent 36d29057
...@@ -39,7 +39,7 @@ class ContextualSuggestionsDependencyFactory { ...@@ -39,7 +39,7 @@ class ContextualSuggestionsDependencyFactory {
* @return A {@link ContextualSuggestionsSource} for getting contextual suggestions for * @return A {@link ContextualSuggestionsSource} for getting contextual suggestions for
* the current user. * the current user.
*/ */
ContextualSuggestionsSource createContextualSuggestionSource(Profile profile) { ContextualSuggestionsSource createContextualSuggestionsSource(Profile profile) {
return new ContextualSuggestionsSource(profile); return new ContextualSuggestionsSource(profile);
} }
......
...@@ -143,7 +143,7 @@ class ContextualSuggestionsMediator ...@@ -143,7 +143,7 @@ class ContextualSuggestionsMediator
public void onEnabledStateChanged(boolean enabled) { public void onEnabledStateChanged(boolean enabled) {
if (enabled) { if (enabled) {
mSuggestionsSource = ContextualSuggestionsDependencyFactory.getInstance() mSuggestionsSource = ContextualSuggestionsDependencyFactory.getInstance()
.createContextualSuggestionSource(mProfile); .createContextualSuggestionsSource(mProfile);
mFetchHelper = ContextualSuggestionsDependencyFactory.getInstance().createFetchHelper( mFetchHelper = ContextualSuggestionsDependencyFactory.getInstance().createFetchHelper(
this, mTabModelSelector); this, mTabModelSelector);
} else { } else {
...@@ -371,4 +371,9 @@ class ContextualSuggestionsMediator ...@@ -371,4 +371,9 @@ class ContextualSuggestionsMediator
void showContentInSheetForTesting() { void showContentInSheetForTesting() {
showContentInSheet(); showContentInSheet();
} }
@VisibleForTesting
TextBubble getHelpBubbleForTesting() {
return mHelpBubble;
}
} }
...@@ -1590,6 +1590,7 @@ chrome_test_java_sources = [ ...@@ -1590,6 +1590,7 @@ 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/FakeTracker.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",
...@@ -1635,6 +1636,7 @@ chrome_test_java_sources = [ ...@@ -1635,6 +1636,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/firstrun/FirstRunActivityTestObserver.java", "javatests/src/org/chromium/chrome/browser/firstrun/FirstRunActivityTestObserver.java",
"javatests/src/org/chromium/chrome/browser/firstrun/FirstRunIntegrationTest.java", "javatests/src/org/chromium/chrome/browser/firstrun/FirstRunIntegrationTest.java",
"javatests/src/org/chromium/chrome/browser/fullscreen/FullscreenManagerTest.java", "javatests/src/org/chromium/chrome/browser/fullscreen/FullscreenManagerTest.java",
"javatests/src/org/chromium/chrome/browser/fullscreen/FullscreenManagerTestUtils.java",
"javatests/src/org/chromium/chrome/browser/gcore/MockConnectedTask.java", "javatests/src/org/chromium/chrome/browser/gcore/MockConnectedTask.java",
"javatests/src/org/chromium/chrome/browser/gcore/MockConnectedTaskTest.java", "javatests/src/org/chromium/chrome/browser/gcore/MockConnectedTaskTest.java",
"javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java", "javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java",
......
...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.contextual_suggestions; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.contextual_suggestions;
import org.junit.rules.TestWatcher; import org.junit.rules.TestWatcher;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tabmodel.TabModelSelector; import org.chromium.chrome.browser.tabmodel.TabModelSelector;
...@@ -52,10 +53,14 @@ public class ContextualSuggestionsDependenciesRule extends TestWatcher { ...@@ -52,10 +53,14 @@ public class ContextualSuggestionsDependenciesRule extends TestWatcher {
public EnabledStateMonitor enabledStateMonitor; public EnabledStateMonitor enabledStateMonitor;
public FetchHelper fetchHelper; public FetchHelper fetchHelper;
public CallbackHelper createSuggestionsSourceCallback = new CallbackHelper();
@Override @Override
ContextualSuggestionsSource createContextualSuggestionSource(Profile profile) { ContextualSuggestionsSource createContextualSuggestionsSource(Profile profile) {
createSuggestionsSourceCallback.notifyCalled();
if (suggestionsSource != null) return suggestionsSource; if (suggestionsSource != null) return suggestionsSource;
return super.createContextualSuggestionSource(profile); return super.createContextualSuggestionsSource(profile);
} }
@Override @Override
......
// 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 android.text.TextUtils;
import org.junit.Assert;
import org.chromium.base.Callback;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.components.feature_engagement.Tracker;
/**
* A feature engagement {@link Tracker} for testing.
*/
class FakeTracker implements Tracker {
public CallbackHelper mDimissedCallbackHelper = new CallbackHelper();
private String mEnabledFeature;
public FakeTracker(String enabledFeature) {
mEnabledFeature = enabledFeature;
}
@Override
public void notifyEvent(String event) {}
@Override
public boolean shouldTriggerHelpUI(String feature) {
return TextUtils.equals(mEnabledFeature, feature);
}
@Override
public int getTriggerState(String feature) {
return 0;
}
@Override
public void dismissed(String feature) {
Assert.assertEquals("Wrong feature dismissed.", mEnabledFeature, feature);
mDimissedCallbackHelper.notifyCalled();
}
@Override
public boolean isInitialized() {
return true;
}
@Override
public void addOnInitializedCallback(Callback<Boolean> callback) {}
@Override
public boolean wouldTriggerHelpUI(String feature) {
return shouldTriggerHelpUI(feature);
}
@Override
public DisplayLockHandle acquireDisplayLock() {
return null;
}
}
// 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.fullscreen;
import android.os.SystemClock;
import org.junit.Assert;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.fullscreen.ChromeFullscreenManager.FullscreenListener;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.TouchCommon;
import org.chromium.content_public.browser.RenderCoordinates;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Utility methods for testing the {@link ChromeFullscreenManager}.
*/
public class FullscreenManagerTestUtils {
/**
* Scrolls the underlying web page to show or hide the browser controls.
* @param testRule The test rule for the currently running test.
* @param show Whether the browser controls should be shown.
*/
public static void scrollBrowserControls(ChromeTabbedActivityTestRule testRule, boolean show) {
ChromeFullscreenManager fullscreenManager = testRule.getActivity().getFullscreenManager();
int browserControlsHeight = fullscreenManager.getTopControlsHeight();
waitForPageToBeScrollable(testRule.getActivity().getActivityTab());
float dragX = 50f;
// Use a larger scroll range than the height of the browser controls to ensure we overcome
// the delay in a scroll start being sent.
float dragStartY = browserControlsHeight * 3;
float dragEndY = dragStartY - browserControlsHeight * 2;
float expectedPosition = -browserControlsHeight;
if (show) {
expectedPosition = 0f;
float tempDragStartY = dragStartY;
dragStartY = dragEndY;
dragEndY = tempDragStartY;
}
long downTime = SystemClock.uptimeMillis();
TouchCommon.dragStart(testRule.getActivity(), dragX, dragStartY, downTime);
TouchCommon.dragTo(
testRule.getActivity(), dragX, dragX, dragStartY, dragEndY, 100, downTime);
TouchCommon.dragEnd(testRule.getActivity(), dragX, dragEndY, downTime);
waitForBrowserControlsPosition(testRule, expectedPosition);
}
/**
* Waits for the browser controls to reach the specified position.
* @param testRule The test rule for the currently running test.
* @param position The desired top controls offset.
*/
public static void waitForBrowserControlsPosition(
ChromeTabbedActivityTestRule testRule, float position) {
final ChromeFullscreenManager fullscreenManager =
testRule.getActivity().getFullscreenManager();
CriteriaHelper.pollUiThread(Criteria.equals(position, new Callable<Float>() {
@Override
public Float call() {
return fullscreenManager.getTopControlOffset();
}
}));
}
/**
* Waits for the base page to be scrollable.
* @param tab The current activity tab.
*/
public static void waitForPageToBeScrollable(final Tab tab) {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return RenderCoordinates.fromWebContents(tab.getWebContents())
.getContentHeightPixInt()
> tab.getContentView().getHeight();
}
});
}
/**
* Waits for the browser controls to be moveable by user gesture.
*
* This function requires the browser controls to start fully visible. Then it ensures that
* at some point the controls can be moved by user gesture. It will then fully cycle the top
* controls to entirely hidden and back to fully shown.
*
* @param testRule The test rule for the currently running test.
* @param tab The current activity tab.
*/
public static void waitForBrowserControlsToBeMoveable(
ChromeTabbedActivityTestRule testRule, final Tab tab) throws InterruptedException {
waitForBrowserControlsPosition(testRule, 0f);
final CallbackHelper contentMovedCallback = new CallbackHelper();
final ChromeFullscreenManager fullscreenManager =
testRule.getActivity().getFullscreenManager();
final float initialVisibleContentOffset = fullscreenManager.getTopVisibleContentOffset();
fullscreenManager.addListener(new FullscreenListener() {
@Override
public void onControlsOffsetChanged(
float topOffset, float bottomOffset, boolean needsAnimate) {
if (fullscreenManager.getTopVisibleContentOffset() != initialVisibleContentOffset) {
contentMovedCallback.notifyCalled();
fullscreenManager.removeListener(this);
}
}
@Override
public void onToggleOverlayVideoMode(boolean enabled) {}
@Override
public void onContentOffsetChanged(float offset) {}
@Override
public void onBottomControlsHeightChanged(int bottomControlsHeight) {}
});
float dragX = 50f;
float dragStartY = tab.getView().getHeight() - 50f;
for (int i = 0; i < 10; i++) {
float dragEndY = dragStartY - fullscreenManager.getTopControlsHeight();
long downTime = SystemClock.uptimeMillis();
TouchCommon.dragStart(testRule.getActivity(), dragX, dragStartY, downTime);
TouchCommon.dragTo(
testRule.getActivity(), dragX, dragX, dragStartY, dragEndY, 100, downTime);
TouchCommon.dragEnd(testRule.getActivity(), dragX, dragEndY, downTime);
try {
contentMovedCallback.waitForCallback(0, 1, 500, TimeUnit.MILLISECONDS);
scrollBrowserControls(testRule, false);
scrollBrowserControls(testRule, true);
return;
} catch (TimeoutException e) {
// Ignore and retry
}
}
Assert.fail("Visible content never moved as expected.");
}
/**
* Disable any browser visibility overrides for testing.
*/
public static void disableBrowserOverrides() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
BrowserStateBrowserControlsVisibilityDelegate.disableForTesting();
}
});
}
}
...@@ -27,6 +27,7 @@ import org.chromium.chrome.browser.ChromeTabbedActivity; ...@@ -27,6 +27,7 @@ import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver; import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModel; import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType;
import org.chromium.chrome.browser.widget.FadingBackgroundView; import org.chromium.chrome.browser.widget.FadingBackgroundView;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetContent; import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.BottomSheetContent;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.ContentPriority; import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet.ContentPriority;
...@@ -125,6 +126,36 @@ public class BottomSheetControllerTest { ...@@ -125,6 +126,36 @@ public class BottomSheetControllerTest {
mBottomSheet.getCurrentSheetContent()); mBottomSheet.getCurrentSheetContent());
} }
@Test
@MediumTest
@Feature({"BottomSheetController"})
public void testSheetHiddenAfterTabSwitcher() throws InterruptedException, TimeoutException {
// Open a second tab and then reselect the original activity tab.
Tab tab1 = mActivityTestRule.getActivity().getActivityTab();
ChromeTabUtils.newTabFromMenu(
InstrumentationRegistry.getInstrumentation(), mActivityTestRule.getActivity());
Tab tab2 = mActivityTestRule.getActivity().getActivityTab();
requestContentInSheet(mLowPriorityContent, true);
// Enter the tab switcher and select a different tab.
ThreadUtils.runOnUiThreadBlocking(() -> {
mActivityTestRule.getActivity().getLayoutManager().showOverview(false);
mBottomSheet.endAnimations();
assertEquals("The bottom sheet should be hidden.", BottomSheet.SHEET_STATE_HIDDEN,
mBottomSheet.getSheetState());
mActivityTestRule.getActivity().getTabModelSelector().getCurrentModel().setIndex(
0, TabSelectionType.FROM_USER);
mActivityTestRule.getActivity().getLayoutManager().hideOverview(false);
mBottomSheet.endAnimations();
});
assertEquals("The bottom sheet still should be hidden.", BottomSheet.SHEET_STATE_HIDDEN,
mBottomSheet.getSheetState());
assertEquals("The bottom sheet is showing incorrect content.", null,
mBottomSheet.getCurrentSheetContent());
}
@Test @Test
@MediumTest @MediumTest
@Feature({"BottomSheetController"}) @Feature({"BottomSheetController"})
......
...@@ -159,6 +159,29 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe ...@@ -159,6 +159,29 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
return 0; return 0;
} }
/**
* Waits for the activity to fully finish its native initialization.
* @param The {@link ChromeActivity} to wait for.
*/
public static void waitForActivityNativeInitializationComplete(ChromeActivity activity) {
CriteriaHelper.pollUiThread(()
-> ChromeBrowserInitializer.getInstance(activity)
.hasNativeInitializationCompleted(),
"Native initialization never finished",
20 * CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL,
CriteriaHelper.DEFAULT_POLLING_INTERVAL);
CriteriaHelper.pollUiThread(() -> activity.didFinishNativeInitialization(),
"Native initialization (of Activity) never finished");
}
/**
* Waits for the activity to fully finish its native initialization.
*/
public void waitForActivityNativeInitializationComplete() {
waitForActivityNativeInitializationComplete(getActivity());
}
/** /**
* Invokes {@link Instrumentation#startActivitySync(Intent)} and sets the * Invokes {@link Instrumentation#startActivitySync(Intent)} and sets the
* test case's activity to the result. See the documentation for * test case's activity to the result. See the documentation for
...@@ -407,21 +430,6 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe ...@@ -407,21 +430,6 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
InstrumentationRegistry.getInstrumentation().waitForIdleSync(); InstrumentationRegistry.getInstrumentation().waitForIdleSync();
} }
/**
* Waits for the activity to fully finish it's native initialization.
*/
public void waitForActivityNativeInitializationComplete() {
CriteriaHelper.pollUiThread(()
-> ChromeBrowserInitializer.getInstance(getActivity())
.hasNativeInitializationCompleted(),
"Native initialization never finished",
20 * CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL,
CriteriaHelper.DEFAULT_POLLING_INTERVAL);
CriteriaHelper.pollUiThread(() -> getActivity().didFinishNativeInitialization(),
"Native initialization (of Activity) never finished");
}
/** /**
* Prepares a URL intent to start the activity. * Prepares a URL intent to start the activity.
* @param intent the intent to be modified * @param intent the intent to be modified
......
...@@ -638,7 +638,9 @@ public class ChromeTabUtils { ...@@ -638,7 +638,9 @@ public class ChromeTabUtils {
* asserts that a new tab is opened and is incognito if expectIncognito is true. * asserts that a new tab is opened and is incognito if expectIncognito is true.
* For use in testing long-press context menu options that open new tabs. * For use in testing long-press context menu options that open new tabs.
* *
* @param view The View to long press. * @param testRule The {@link ChromeTabbedActivityTestRule} used to retrieve the currently
* running activity.
* @param view The {@link View} to long press.
* @param contextMenuItemId The context menu item to select on the view. * @param contextMenuItemId The context menu item to select on the view.
* @param expectIncognito Whether the opened tab is expected to be incognito. * @param expectIncognito Whether the opened tab is expected to be incognito.
* @param expectedUrl The expected url for the new tab. * @param expectedUrl The expected url for the new tab.
...@@ -676,4 +678,53 @@ public class ChromeTabUtils { ...@@ -676,4 +678,53 @@ public class ChromeTabUtils {
Assert.assertFalse(testRule.getActivity().getTabModelSelector().isIncognitoSelected()); Assert.assertFalse(testRule.getActivity().getTabModelSelector().isIncognitoSelected());
} }
} }
/**
* Long presses the view, selects an item from the context menu, and
* asserts that a new tab is opened and is incognito if expectIncognito is true.
* For use in testing long-press context menu options that open new tabs in a different
* ChromeTabbedActivity instance.
*
* @param foregroundActivity The {@link ChromeTabbedActivity} currently in the foreground.
* @param backgroundActivity The {@link ChromeTabbedActivity} currently in the background. The
* new tab is expected to open in this activity.
* @param view The {@link View} in the {@code foregroundActivity} to long press.
* @param contextMenuItemId The context menu item to select on the view.
* @param expectIncognito Whether the opened tab is expected to be incognito.
* @param expectedUrl The expected url for the new tab.
*/
public static void invokeContextMenuAndOpenInOtherWindow(
ChromeTabbedActivity foregroundActivity, ChromeTabbedActivity backgroundActivity,
View view, int contextMenuItemId, boolean expectIncognito, final String expectedUrl)
throws InterruptedException, ExecutionException {
final CallbackHelper createdCallback = new CallbackHelper();
final TabModel tabModel =
backgroundActivity.getTabModelSelector().getModel(expectIncognito);
tabModel.addObserver(new EmptyTabModelObserver() {
@Override
public void didAddTab(Tab tab, TabLaunchType type) {
if (TextUtils.equals(expectedUrl, tab.getUrl())) {
createdCallback.notifyCalled();
tabModel.removeObserver(this);
}
}
});
TestTouchUtils.performLongClickOnMainSync(
InstrumentationRegistry.getInstrumentation(), view);
Assert.assertTrue(InstrumentationRegistry.getInstrumentation().invokeContextMenuAction(
foregroundActivity, contextMenuItemId, 0));
try {
createdCallback.waitForCallback(0);
} catch (TimeoutException e) {
Assert.fail("Never received tab creation event");
}
if (expectIncognito) {
Assert.assertTrue(backgroundActivity.getTabModelSelector().isIncognitoSelected());
} else {
Assert.assertFalse(backgroundActivity.getTabModelSelector().isIncognitoSelected());
}
}
} }
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