Commit cb5de6ea authored by Matt Jones's avatar Matt Jones Committed by Commit Bot

Remove BottomSheetTestRule

Post-chrome home, the BottomSheetTestRule has little functionality and
gets in the way of modularization. All existing tests have been easily
updated to work without it.

Bug: 100227
Change-Id: I6ae907599f0d651ae9d540b483b32146d678ed18
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2210668Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771082}
parent ca7f1eac
......@@ -413,7 +413,13 @@ public class BottomSheetControllerTest {
@MediumTest
public void testScrimTapClosesSheet() throws TimeoutException, ExecutionException {
requestContentInSheet(mHighPriorityContent, true);
BottomSheetTestRule.Observer observer = new BottomSheetTestRule.Observer();
CallbackHelper closedCallbackHelper = new CallbackHelper();
BottomSheetObserver observer = new EmptyBottomSheetObserver() {
@Override
public void onSheetClosed(@BottomSheetController.StateChangeReason int reason) {
closedCallbackHelper.notifyCalled();
}
};
mSheetController.addObserver(observer);
expandSheet();
......@@ -421,7 +427,7 @@ public class BottomSheetControllerTest {
TestThreadUtils.runOnUiThreadBlocking(
() -> ((View) mScrimCoordinator.getViewForTesting()).callOnClick());
observer.mClosedCallbackHelper.waitForCallback(0);
closedCallbackHelper.waitForCallback(0);
}
@Test
......
......@@ -8,6 +8,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.chromium.chrome.browser.flags.ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE;
import android.support.test.filters.MediumTest;
import android.view.View;
import android.view.ViewGroup;
......@@ -21,8 +23,11 @@ import org.junit.runner.RunWith;
import org.chromium.base.MathUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.StateChangeReason;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent;
import org.chromium.ui.test.util.UiRestriction;
......@@ -31,23 +36,88 @@ import java.util.concurrent.TimeoutException;
/** This class tests the functionality of the {@link BottomSheetObserver}. */
@RunWith(ChromeJUnit4ClassRunner.class)
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE) // Bottom sheet is only used on phones.
@CommandLineFlags.Add({DISABLE_FIRST_RUN_EXPERIENCE})
public class BottomSheetObserverTest {
/** An observer used to record events that occur with respect to the bottom sheet. */
public static class TestSheetObserver extends EmptyBottomSheetObserver {
/** A {@link CallbackHelper} that can wait for the bottom sheet to be closed. */
public final CallbackHelper mClosedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the bottom sheet to be opened. */
public final CallbackHelper mOpenedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the onOffsetChanged event. */
public final CallbackHelper mOffsetChangedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the onSheetContentChanged event. */
public final CallbackHelper mContentChangedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the sheet to be in its full state. */
public final CallbackHelper mFullCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the sheet to be hidden. */
public final CallbackHelper mHiddenCallbackHelper = new CallbackHelper();
/** The last value that the onOffsetChanged event sent. */
private float mLastOffsetChangedValue;
@Override
public void onSheetOffsetChanged(float heightFraction, float offsetPx) {
mLastOffsetChangedValue = heightFraction;
mOffsetChangedCallbackHelper.notifyCalled();
}
@Override
public void onSheetOpened(@StateChangeReason int reason) {
mOpenedCallbackHelper.notifyCalled();
}
@Override
public void onSheetClosed(@StateChangeReason int reason) {
mClosedCallbackHelper.notifyCalled();
}
@Override
public void onSheetContentChanged(BottomSheetContent newContent) {
mContentChangedCallbackHelper.notifyCalled();
}
@Override
public void onSheetStateChanged(int newState) {
if (newState == BottomSheetController.SheetState.HIDDEN) {
mHiddenCallbackHelper.notifyCalled();
} else if (newState == BottomSheetController.SheetState.FULL) {
mFullCallbackHelper.notifyCalled();
}
}
/** @return The last value passed in to {@link #onSheetOffsetChanged(float)}. */
public float getLastOffsetChangedValue() {
return mLastOffsetChangedValue;
}
}
@Rule
public BottomSheetTestRule mBottomSheetTestRule = new BottomSheetTestRule();
private BottomSheetTestRule.Observer mObserver;
public ChromeTabbedActivityTestRule mTestRule = new ChromeTabbedActivityTestRule();
private TestSheetObserver mObserver;
private TestBottomSheetContent mSheetContent;
private BottomSheetController mBottomSheetController;
private BottomSheet mSheetView;
@Before
public void setUp() throws Exception {
BottomSheet.setSmallScreenForTesting(false);
mBottomSheetTestRule.startMainActivityOnBlankPage();
mTestRule.startMainActivityOnBlankPage();
mBottomSheetController =
mTestRule.getActivity().getRootUiCoordinatorForTesting().getBottomSheetController();
ThreadUtils.runOnUiThreadBlocking(() -> {
mSheetContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(),
BottomSheetContent.ContentPriority.HIGH, false);
mBottomSheetTestRule.getBottomSheetController().requestShowContent(
mSheetContent, false);
mSheetContent = new TestBottomSheetContent(
mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false);
mBottomSheetController.requestShowContent(mSheetContent, false);
});
mObserver = mBottomSheetTestRule.getObserver();
mObserver = new TestSheetObserver();
mBottomSheetController.addObserver(mObserver);
mSheetView = (BottomSheet) mBottomSheetController.getBottomSheetViewForTesting();
}
/** Test that the onSheetClosed event is triggered if the sheet is closed without animation. */
......@@ -94,7 +164,8 @@ public class BottomSheetObserverTest {
CallbackHelper hiddenHelper = mObserver.mHiddenCallbackHelper;
int initialHideEvents = hiddenHelper.getCallCount();
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, false);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(BottomSheetController.SheetState.FULL, false));
mSheetContent.setPeekHeight(peekStateEnabled ? BottomSheetContent.HeightMode.DEFAULT
: BottomSheetContent.HeightMode.DISABLED);
......@@ -107,7 +178,8 @@ public class BottomSheetObserverTest {
int targetState = peekStateEnabled ? BottomSheetController.SheetState.PEEK
: BottomSheetController.SheetState.HIDDEN;
mBottomSheetTestRule.setSheetState(targetState, animationEnabled);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(targetState, animationEnabled));
closedCallbackHelper.waitForCallback(closedCallbackCount, 1);
......@@ -171,19 +243,20 @@ public class BottomSheetObserverTest {
CallbackHelper closedCallbackHelper = mObserver.mClosedCallbackHelper;
int initialClosedCount = closedCallbackHelper.getCallCount();
mBottomSheetTestRule.setSheetState(
mBottomSheetTestRule.getBottomSheet().getOpeningState(), false);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(mSheetView.getOpeningState(), false));
assertNotEquals("Sheet should not be hidden.",
mBottomSheetTestRule.getBottomSheet().getSheetState(),
assertNotEquals("Sheet should not be hidden.", mSheetView.getSheetState(),
BottomSheetController.SheetState.HIDDEN);
if (!peekStateEnabled) {
assertNotEquals("Sheet should be above the peeking state when peek is disabled.",
mBottomSheetTestRule.getBottomSheet().getSheetState(),
BottomSheetController.SheetState.PEEK);
mSheetView.getSheetState(), BottomSheetController.SheetState.PEEK);
}
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, animationEnabled);
ThreadUtils.runOnUiThreadBlocking(
()
-> mSheetView.setSheetState(
BottomSheetController.SheetState.FULL, animationEnabled));
openedCallbackHelper.waitForCallback(openedCallbackCount, 1);
fullCallbackHelper.waitForCallback(initialFullCount, 1);
......@@ -200,31 +273,34 @@ public class BottomSheetObserverTest {
@Test
@MediumTest
public void testOffsetChangedEvent() throws TimeoutException {
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, false);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(BottomSheetController.SheetState.FULL, false));
CallbackHelper callbackHelper = mObserver.mOffsetChangedCallbackHelper;
BottomSheet bottomSheet = mBottomSheetTestRule.getBottomSheet();
float hiddenHeight = bottomSheet.getHiddenRatio() * bottomSheet.getSheetContainerHeight();
float fullHeight = bottomSheet.getFullRatio() * bottomSheet.getSheetContainerHeight();
float hiddenHeight = mSheetView.getHiddenRatio() * mSheetView.getSheetContainerHeight();
float fullHeight = mSheetView.getFullRatio() * mSheetView.getSheetContainerHeight();
// The sheet's half state is not necessarily 50% of the way to the top.
float midPeekFull = (hiddenHeight + fullHeight) / 2f;
// When in the hidden state, the transition value should be 0.
int callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(hiddenHeight);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(hiddenHeight, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(0f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
// When in the full state, the transition value should be 1.
callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(fullHeight);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(fullHeight, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(1f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
// Halfway between peek and full should send 0.5.
callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(midPeekFull);
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(midPeekFull, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(0.5f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
}
......@@ -233,16 +309,15 @@ public class BottomSheetObserverTest {
@MediumTest
public void testWrapContentBehavior() throws TimeoutException {
// We make sure the height of the wrapped content is smaller than sheetContainerHeight.
BottomSheet bottomSheet = mBottomSheetTestRule.getBottomSheet();
int wrappedContentHeight = (int) bottomSheet.getSheetContainerHeight() / 2;
int wrappedContentHeight = (int) mSheetView.getSheetContainerHeight() / 2;
assertTrue(wrappedContentHeight > 0);
// Show content that should be wrapped.
CallbackHelper callbackHelper = mObserver.mContentChangedCallbackHelper;
int callCount = callbackHelper.getCallCount();
ThreadUtils.runOnUiThreadBlocking(() -> {
bottomSheet.showContent(new TestBottomSheetContent(mBottomSheetTestRule.getActivity(),
BottomSheetContent.ContentPriority.HIGH, false) {
mSheetView.showContent(new TestBottomSheetContent(
mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false) {
private final ViewGroup mContentView;
{
......@@ -251,8 +326,8 @@ public class BottomSheetObserverTest {
// height on its own as View::onMeasure will by default set its height/width to
// be the minimum height/width of its background (if any) or expand as much as
// it can.
mContentView = new FrameLayout(mBottomSheetTestRule.getActivity());
View child = new View(mBottomSheetTestRule.getActivity());
mContentView = new FrameLayout(mTestRule.getActivity());
View child = new View(mTestRule.getActivity());
child.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, wrappedContentHeight));
mContentView.addView(child);
......@@ -272,11 +347,12 @@ public class BottomSheetObserverTest {
callbackHelper.waitForCallback(callCount);
// HALF state is forbidden when wrapping the content.
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.HALF, false);
assertEquals(BottomSheetController.SheetState.FULL, bottomSheet.getSheetState());
ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(BottomSheetController.SheetState.HALF, false));
assertEquals(BottomSheetController.SheetState.FULL, mSheetView.getSheetState());
// Check the offset.
assertEquals(wrappedContentHeight + bottomSheet.getToolbarShadowHeight(),
bottomSheet.getCurrentOffsetPx(), MathUtils.EPSILON);
assertEquals(wrappedContentHeight + mSheetView.getToolbarShadowHeight(),
mSheetView.getCurrentOffsetPx(), MathUtils.EPSILON);
}
}
......@@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.chromium.chrome.browser.flags.ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE;
import static org.chromium.content_public.browser.test.util.CriteriaHelper.pollUiThread;
import static org.chromium.content_public.browser.test.util.TestThreadUtils.runOnUiThreadBlocking;
......@@ -20,12 +21,14 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.toolbar.ToolbarManager;
import org.chromium.chrome.browser.ui.TabObscuringHandler;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.SheetState;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.StateChangeReason;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent.HeightMode;
import org.chromium.ui.test.util.UiRestriction;
......@@ -36,30 +39,32 @@ import java.util.concurrent.TimeoutException;
/** This class tests the functionality of the {@link BottomSheet}. */
@RunWith(ChromeJUnit4ClassRunner.class)
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE)
@CommandLineFlags.Add({DISABLE_FIRST_RUN_EXPERIENCE})
public class BottomSheetTest {
private static final float VELOCITY_WHEN_MOVING_UP = 1.0f;
private static final float VELOCITY_WHEN_MOVING_DOWN = -1.0f;
@Rule
public BottomSheetTestRule mBottomSheetTestRule = new BottomSheetTestRule();
private BottomSheetTestRule.Observer mObserver;
public ChromeTabbedActivityTestRule mTestRule = new ChromeTabbedActivityTestRule();
private TestBottomSheetContent mLowPriorityContent;
private TestBottomSheetContent mHighPriorityContent;
private BottomSheetController mSheetController;
@Before
public void setUp() throws Exception {
BottomSheet.setSmallScreenForTesting(false);
mBottomSheetTestRule.startMainActivityOnBlankPage();
mTestRule.startMainActivityOnBlankPage();
mSheetController =
mTestRule.getActivity().getRootUiCoordinatorForTesting().getBottomSheetController();
runOnUiThreadBlocking(() -> {
mLowPriorityContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(),
BottomSheetContent.ContentPriority.LOW, false);
mHighPriorityContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(),
BottomSheetContent.ContentPriority.HIGH, false);
mLowPriorityContent = new TestBottomSheetContent(
mTestRule.getActivity(), BottomSheetContent.ContentPriority.LOW, false);
mHighPriorityContent = new TestBottomSheetContent(
mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false);
});
mHighPriorityContent.setPeekHeight(HeightMode.DISABLED);
mHighPriorityContent.setHalfHeightRatio(0.5f);
mHighPriorityContent.setSkipHalfStateScrollingDown(false);
mObserver = mBottomSheetTestRule.getObserver();
}
@Test
......@@ -71,7 +76,7 @@ public class BottomSheetTest {
showContent(mHighPriorityContent, SheetState.PEEK);
assertEquals("Sheet should be peeking at the custom height.", customToolbarHeight,
mBottomSheetTestRule.getBottomSheetController().getCurrentOffset());
mSheetController.getCurrentOffset());
}
@Test
......@@ -130,9 +135,9 @@ public class BottomSheetTest {
@Test
@MediumTest
public void testTabObscuringState() throws ExecutionException, TimeoutException {
public void testTabObscuringState() throws TimeoutException {
CallbackHelper obscuringStateChangedHelper = new CallbackHelper();
TabObscuringHandler handler = mBottomSheetTestRule.getActivity().getTabObscuringHandler();
TabObscuringHandler handler = mTestRule.getActivity().getTabObscuringHandler();
handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled());
mHighPriorityContent.setHasCustomScrimLifecycle(false);
......@@ -154,7 +159,7 @@ public class BottomSheetTest {
@MediumTest
public void testTabObscuringState_customScrim() throws ExecutionException {
CallbackHelper obscuringStateChangedHelper = new CallbackHelper();
TabObscuringHandler handler = mBottomSheetTestRule.getActivity().getTabObscuringHandler();
TabObscuringHandler handler = mTestRule.getActivity().getTabObscuringHandler();
handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled());
mHighPriorityContent.setHasCustomScrimLifecycle(true);
......@@ -172,20 +177,19 @@ public class BottomSheetTest {
@Test
@MediumTest
public void testOmniboxFocusSuppressesSheet() {
ToolbarManager toolbarManager = mBottomSheetTestRule.getActivity()
.getRootUiCoordinatorForTesting()
.getToolbarManager();
ToolbarManager toolbarManager =
mTestRule.getActivity().getRootUiCoordinatorForTesting().getToolbarManager();
showContent(mHighPriorityContent, SheetState.HALF);
runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(true, 0));
assertEquals("The bottom sheet should be hidden.", SheetState.HIDDEN,
mBottomSheetTestRule.getBottomSheetController().getSheetState());
mSheetController.getSheetState());
runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(false, 0));
assertNotEquals("The bottom sheet should not be hidden.", SheetState.HIDDEN,
mBottomSheetTestRule.getBottomSheetController().getSheetState());
mSheetController.getSheetState());
}
@Test
......@@ -193,22 +197,21 @@ public class BottomSheetTest {
public void testSuppressionState_unsuppress() {
showContent(mHighPriorityContent, SheetState.HALF);
BottomSheetController controller = mBottomSheetTestRule.getBottomSheetController();
runOnUiThreadBlocking(() -> {
controller.suppressSheet(StateChangeReason.NONE);
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations();
mSheetController.suppressSheet(StateChangeReason.NONE);
((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
});
assertEquals("The sheet should be in the hidden state.", SheetState.HIDDEN,
controller.getSheetState());
mSheetController.getSheetState());
runOnUiThreadBlocking(() -> {
controller.unsuppressSheet();
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations();
mSheetController.unsuppressSheet();
((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
});
assertEquals("The sheet should be restored to the half state.", SheetState.HALF,
controller.getSheetState());
mSheetController.getSheetState());
}
@Test
......@@ -216,51 +219,44 @@ public class BottomSheetTest {
public void testSuppressionState_unsuppressDifferentContent() {
showContent(mHighPriorityContent, SheetState.HALF);
BottomSheetController controller = mBottomSheetTestRule.getBottomSheetController();
runOnUiThreadBlocking(() -> {
controller.suppressSheet(StateChangeReason.NONE);
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations();
mSheetController.suppressSheet(StateChangeReason.NONE);
((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
});
assertEquals("The sheet should be in the hidden state.", SheetState.HIDDEN,
controller.getSheetState());
mSheetController.getSheetState());
runOnUiThreadBlocking(() -> controller.hideContent(mHighPriorityContent, false));
runOnUiThreadBlocking(() -> mSheetController.hideContent(mHighPriorityContent, false));
showContent(mLowPriorityContent, SheetState.PEEK);
runOnUiThreadBlocking(() -> {
controller.unsuppressSheet();
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations();
mSheetController.unsuppressSheet();
((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
});
assertEquals("The sheet should be restored to the peek state.", SheetState.PEEK,
controller.getSheetState());
mSheetController.getSheetState());
}
private void hideSheet() {
runOnUiThreadBlocking(
() -> mBottomSheetTestRule.getBottomSheetController().setSheetStateForTesting(
SheetState.HIDDEN, false));
() -> mSheetController.setSheetStateForTesting(SheetState.HIDDEN, false));
}
private float getMaxSheetHeightInPx() {
return mBottomSheetTestRule.getBottomSheet().getSheetContainerHeight();
return mSheetController.getContainerHeight();
}
private @SheetState int simulateScrollTo(float targetHeightInPx, float yUpwardsVelocity) {
return mBottomSheetTestRule.getBottomSheetController().forceScrollingForTesting(
targetHeightInPx, yUpwardsVelocity);
return mSheetController.forceScrollingForTesting(targetHeightInPx, yUpwardsVelocity);
}
/** @param content The content to show in the bottom sheet. */
private void showContent(BottomSheetContent content, @SheetState int targetState) {
runOnUiThreadBlocking(() -> {
mBottomSheetTestRule.getBottomSheetController().requestShowContent(content, false);
});
mBottomSheetTestRule.getBottomSheetController().setSheetStateForTesting(targetState, false);
pollUiThread(()
-> mBottomSheetTestRule.getBottomSheetController().getSheetState()
== targetState);
runOnUiThreadBlocking(() -> { mSheetController.requestShowContent(content, false); });
mSheetController.setSheetStateForTesting(targetState, false);
pollUiThread(() -> mSheetController.getSheetState() == targetState);
}
}
......@@ -175,7 +175,6 @@ android_library("chrome_java_test_support") {
"javatests/src/org/chromium/chrome/browser/omnibox/suggestions/AutocompleteCoordinatorTestUtils.java",
"javatests/src/org/chromium/chrome/browser/tab/MockTab.java",
"javatests/src/org/chromium/chrome/browser/tab/TabTestUtils.java",
"javatests/src/org/chromium/chrome/browser/widget/bottomsheet/BottomSheetTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeActivityTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeBrowserTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeJUnit4ClassRunner.java",
......
// Copyright 2017 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.widget.bottomsheet;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import static org.chromium.chrome.browser.flags.ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.StateChangeReason;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Junit4 rule for tests testing the bottom sheet. This rule creates a new, separate bottom sheet
* to test with.
*/
@CommandLineFlags.Add({DISABLE_FIRST_RUN_EXPERIENCE})
public class BottomSheetTestRule extends ChromeTabbedActivityTestRule {
/** An observer used to record events that occur with respect to the bottom sheet. */
public static class Observer extends EmptyBottomSheetObserver {
/** A {@link CallbackHelper} that can wait for the bottom sheet to be closed. */
public final CallbackHelper mClosedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the bottom sheet to be opened. */
public final CallbackHelper mOpenedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the onOffsetChanged event. */
public final CallbackHelper mOffsetChangedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the onSheetContentChanged event. */
public final CallbackHelper mContentChangedCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the sheet to be in its full state. */
public final CallbackHelper mFullCallbackHelper = new CallbackHelper();
/** A {@link CallbackHelper} that can wait for the sheet to be hidden. */
public final CallbackHelper mHiddenCallbackHelper = new CallbackHelper();
/** The last value that the onOffsetChanged event sent. */
private float mLastOffsetChangedValue;
@Override
public void onSheetOffsetChanged(float heightFraction, float offsetPx) {
mLastOffsetChangedValue = heightFraction;
mOffsetChangedCallbackHelper.notifyCalled();
}
@Override
public void onSheetOpened(@StateChangeReason int reason) {
mOpenedCallbackHelper.notifyCalled();
}
@Override
public void onSheetClosed(@StateChangeReason int reason) {
mClosedCallbackHelper.notifyCalled();
}
@Override
public void onSheetContentChanged(BottomSheetContent newContent) {
mContentChangedCallbackHelper.notifyCalled();
}
@Override
public void onSheetStateChanged(int newState) {
if (newState == BottomSheetController.SheetState.HIDDEN) {
mHiddenCallbackHelper.notifyCalled();
} else if (newState == BottomSheetController.SheetState.FULL) {
mFullCallbackHelper.notifyCalled();
}
}
/** @return The last value passed in to {@link #onSheetOffsetChanged(float)}. */
public float getLastOffsetChangedValue() {
return mLastOffsetChangedValue;
}
}
/** A handle to the controller for the sheet. */
private BottomSheetController mSheetController;
/** A handle to the sheet's observer. */
private Observer mObserver;
protected void afterStartingActivity() {
TestThreadUtils.runOnUiThreadBlocking(
() -> { mSheetController = getActivity().getBottomSheetController(); });
mObserver = new Observer();
mSheetController.addObserver(mObserver);
}
// TODO (aberent): The Chrome test rules currently bypass ActivityTestRule.launchActivity, hence
// don't call beforeActivityLaunched and afterActivityLaunched as defined in the
// ActivityTestRule interface. To work round this override the methods that start activities.
// See https://crbug.com/726444.
@Override
public void startMainActivityOnBlankPage() {
super.startMainActivityOnBlankPage();
afterStartingActivity();
}
public Observer getObserver() {
return mObserver;
}
public BottomSheetController getBottomSheetController() {
return mSheetController;
}
public BottomSheet getBottomSheet() {
return (BottomSheet) mSheetController.getBottomSheetViewForTesting();
}
/**
* Set the bottom sheet's state on the UI thread.
*
* @param state The state to set the sheet to.
* @param animate If the sheet should animate to the provided state.
*/
public void setSheetState(int state, boolean animate) {
TestThreadUtils.runOnUiThreadBlocking(
() -> getBottomSheetController().setSheetStateForTesting(state, animate));
}
/**
* Set the bottom sheet's offset from the bottom of the screen on the UI thread.
*
* @param offset The offset from the bottom that the sheet should be.
*/
public void setSheetOffsetFromBottom(float offset) {
TestThreadUtils.runOnUiThreadBlocking(
() -> mSheetController.setSheetOffsetFromBottomForTesting(offset));
}
public BottomSheetContent getBottomSheetContent() {
return mSheetController.getCurrentSheetContent();
}
/**
* Wait for an update to start and finish.
*/
public static void waitForWindowUpdates() {
final long maxWindowUpdateTimeMs = scaleTimeout(1000);
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.waitForWindowUpdate(null, maxWindowUpdateTimeMs);
device.waitForIdle(maxWindowUpdateTimeMs);
}
}
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