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 { ...@@ -413,7 +413,13 @@ public class BottomSheetControllerTest {
@MediumTest @MediumTest
public void testScrimTapClosesSheet() throws TimeoutException, ExecutionException { public void testScrimTapClosesSheet() throws TimeoutException, ExecutionException {
requestContentInSheet(mHighPriorityContent, true); 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); mSheetController.addObserver(observer);
expandSheet(); expandSheet();
...@@ -421,7 +427,7 @@ public class BottomSheetControllerTest { ...@@ -421,7 +427,7 @@ public class BottomSheetControllerTest {
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
() -> ((View) mScrimCoordinator.getViewForTesting()).callOnClick()); () -> ((View) mScrimCoordinator.getViewForTesting()).callOnClick());
observer.mClosedCallbackHelper.waitForCallback(0); closedCallbackHelper.waitForCallback(0);
} }
@Test @Test
......
...@@ -8,6 +8,8 @@ import static org.junit.Assert.assertEquals; ...@@ -8,6 +8,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; 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.support.test.filters.MediumTest;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -21,8 +23,11 @@ import org.junit.runner.RunWith; ...@@ -21,8 +23,11 @@ import org.junit.runner.RunWith;
import org.chromium.base.MathUtils; import org.chromium.base.MathUtils;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction; 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.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent; import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent;
import org.chromium.ui.test.util.UiRestriction; import org.chromium.ui.test.util.UiRestriction;
...@@ -31,23 +36,88 @@ import java.util.concurrent.TimeoutException; ...@@ -31,23 +36,88 @@ import java.util.concurrent.TimeoutException;
/** This class tests the functionality of the {@link BottomSheetObserver}. */ /** This class tests the functionality of the {@link BottomSheetObserver}. */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE) // Bottom sheet is only used on phones. @Restriction(UiRestriction.RESTRICTION_TYPE_PHONE) // Bottom sheet is only used on phones.
@CommandLineFlags.Add({DISABLE_FIRST_RUN_EXPERIENCE})
public class BottomSheetObserverTest { 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 @Rule
public BottomSheetTestRule mBottomSheetTestRule = new BottomSheetTestRule(); public ChromeTabbedActivityTestRule mTestRule = new ChromeTabbedActivityTestRule();
private BottomSheetTestRule.Observer mObserver; private TestSheetObserver mObserver;
private TestBottomSheetContent mSheetContent; private TestBottomSheetContent mSheetContent;
private BottomSheetController mBottomSheetController;
private BottomSheet mSheetView;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
BottomSheet.setSmallScreenForTesting(false); BottomSheet.setSmallScreenForTesting(false);
mBottomSheetTestRule.startMainActivityOnBlankPage(); mTestRule.startMainActivityOnBlankPage();
mBottomSheetController =
mTestRule.getActivity().getRootUiCoordinatorForTesting().getBottomSheetController();
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
mSheetContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(), mSheetContent = new TestBottomSheetContent(
BottomSheetContent.ContentPriority.HIGH, false); mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false);
mBottomSheetTestRule.getBottomSheetController().requestShowContent( mBottomSheetController.requestShowContent(mSheetContent, false);
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. */ /** Test that the onSheetClosed event is triggered if the sheet is closed without animation. */
...@@ -94,7 +164,8 @@ public class BottomSheetObserverTest { ...@@ -94,7 +164,8 @@ public class BottomSheetObserverTest {
CallbackHelper hiddenHelper = mObserver.mHiddenCallbackHelper; CallbackHelper hiddenHelper = mObserver.mHiddenCallbackHelper;
int initialHideEvents = hiddenHelper.getCallCount(); int initialHideEvents = hiddenHelper.getCallCount();
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, false); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(BottomSheetController.SheetState.FULL, false));
mSheetContent.setPeekHeight(peekStateEnabled ? BottomSheetContent.HeightMode.DEFAULT mSheetContent.setPeekHeight(peekStateEnabled ? BottomSheetContent.HeightMode.DEFAULT
: BottomSheetContent.HeightMode.DISABLED); : BottomSheetContent.HeightMode.DISABLED);
...@@ -107,7 +178,8 @@ public class BottomSheetObserverTest { ...@@ -107,7 +178,8 @@ public class BottomSheetObserverTest {
int targetState = peekStateEnabled ? BottomSheetController.SheetState.PEEK int targetState = peekStateEnabled ? BottomSheetController.SheetState.PEEK
: BottomSheetController.SheetState.HIDDEN; : BottomSheetController.SheetState.HIDDEN;
mBottomSheetTestRule.setSheetState(targetState, animationEnabled); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(targetState, animationEnabled));
closedCallbackHelper.waitForCallback(closedCallbackCount, 1); closedCallbackHelper.waitForCallback(closedCallbackCount, 1);
...@@ -171,19 +243,20 @@ public class BottomSheetObserverTest { ...@@ -171,19 +243,20 @@ public class BottomSheetObserverTest {
CallbackHelper closedCallbackHelper = mObserver.mClosedCallbackHelper; CallbackHelper closedCallbackHelper = mObserver.mClosedCallbackHelper;
int initialClosedCount = closedCallbackHelper.getCallCount(); int initialClosedCount = closedCallbackHelper.getCallCount();
mBottomSheetTestRule.setSheetState( ThreadUtils.runOnUiThreadBlocking(
mBottomSheetTestRule.getBottomSheet().getOpeningState(), false); () -> mSheetView.setSheetState(mSheetView.getOpeningState(), false));
assertNotEquals("Sheet should not be hidden.", assertNotEquals("Sheet should not be hidden.", mSheetView.getSheetState(),
mBottomSheetTestRule.getBottomSheet().getSheetState(),
BottomSheetController.SheetState.HIDDEN); BottomSheetController.SheetState.HIDDEN);
if (!peekStateEnabled) { if (!peekStateEnabled) {
assertNotEquals("Sheet should be above the peeking state when peek is disabled.", assertNotEquals("Sheet should be above the peeking state when peek is disabled.",
mBottomSheetTestRule.getBottomSheet().getSheetState(), mSheetView.getSheetState(), BottomSheetController.SheetState.PEEK);
BottomSheetController.SheetState.PEEK);
} }
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, animationEnabled); ThreadUtils.runOnUiThreadBlocking(
()
-> mSheetView.setSheetState(
BottomSheetController.SheetState.FULL, animationEnabled));
openedCallbackHelper.waitForCallback(openedCallbackCount, 1); openedCallbackHelper.waitForCallback(openedCallbackCount, 1);
fullCallbackHelper.waitForCallback(initialFullCount, 1); fullCallbackHelper.waitForCallback(initialFullCount, 1);
...@@ -200,31 +273,34 @@ public class BottomSheetObserverTest { ...@@ -200,31 +273,34 @@ public class BottomSheetObserverTest {
@Test @Test
@MediumTest @MediumTest
public void testOffsetChangedEvent() throws TimeoutException { public void testOffsetChangedEvent() throws TimeoutException {
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.FULL, false); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetState(BottomSheetController.SheetState.FULL, false));
CallbackHelper callbackHelper = mObserver.mOffsetChangedCallbackHelper; CallbackHelper callbackHelper = mObserver.mOffsetChangedCallbackHelper;
BottomSheet bottomSheet = mBottomSheetTestRule.getBottomSheet(); float hiddenHeight = mSheetView.getHiddenRatio() * mSheetView.getSheetContainerHeight();
float hiddenHeight = bottomSheet.getHiddenRatio() * bottomSheet.getSheetContainerHeight(); float fullHeight = mSheetView.getFullRatio() * mSheetView.getSheetContainerHeight();
float fullHeight = bottomSheet.getFullRatio() * bottomSheet.getSheetContainerHeight();
// The sheet's half state is not necessarily 50% of the way to the top. // The sheet's half state is not necessarily 50% of the way to the top.
float midPeekFull = (hiddenHeight + fullHeight) / 2f; float midPeekFull = (hiddenHeight + fullHeight) / 2f;
// When in the hidden state, the transition value should be 0. // When in the hidden state, the transition value should be 0.
int callbackCount = callbackHelper.getCallCount(); int callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(hiddenHeight); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(hiddenHeight, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1); callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(0f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON); assertEquals(0f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
// When in the full state, the transition value should be 1. // When in the full state, the transition value should be 1.
callbackCount = callbackHelper.getCallCount(); callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(fullHeight); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(fullHeight, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1); callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(1f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON); assertEquals(1f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
// Halfway between peek and full should send 0.5. // Halfway between peek and full should send 0.5.
callbackCount = callbackHelper.getCallCount(); callbackCount = callbackHelper.getCallCount();
mBottomSheetTestRule.setSheetOffsetFromBottom(midPeekFull); ThreadUtils.runOnUiThreadBlocking(
() -> mSheetView.setSheetOffsetFromBottom(midPeekFull, StateChangeReason.NONE));
callbackHelper.waitForCallback(callbackCount, 1); callbackHelper.waitForCallback(callbackCount, 1);
assertEquals(0.5f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON); assertEquals(0.5f, mObserver.getLastOffsetChangedValue(), MathUtils.EPSILON);
} }
...@@ -233,16 +309,15 @@ public class BottomSheetObserverTest { ...@@ -233,16 +309,15 @@ public class BottomSheetObserverTest {
@MediumTest @MediumTest
public void testWrapContentBehavior() throws TimeoutException { public void testWrapContentBehavior() throws TimeoutException {
// We make sure the height of the wrapped content is smaller than sheetContainerHeight. // We make sure the height of the wrapped content is smaller than sheetContainerHeight.
BottomSheet bottomSheet = mBottomSheetTestRule.getBottomSheet(); int wrappedContentHeight = (int) mSheetView.getSheetContainerHeight() / 2;
int wrappedContentHeight = (int) bottomSheet.getSheetContainerHeight() / 2;
assertTrue(wrappedContentHeight > 0); assertTrue(wrappedContentHeight > 0);
// Show content that should be wrapped. // Show content that should be wrapped.
CallbackHelper callbackHelper = mObserver.mContentChangedCallbackHelper; CallbackHelper callbackHelper = mObserver.mContentChangedCallbackHelper;
int callCount = callbackHelper.getCallCount(); int callCount = callbackHelper.getCallCount();
ThreadUtils.runOnUiThreadBlocking(() -> { ThreadUtils.runOnUiThreadBlocking(() -> {
bottomSheet.showContent(new TestBottomSheetContent(mBottomSheetTestRule.getActivity(), mSheetView.showContent(new TestBottomSheetContent(
BottomSheetContent.ContentPriority.HIGH, false) { mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false) {
private final ViewGroup mContentView; private final ViewGroup mContentView;
{ {
...@@ -251,8 +326,8 @@ public class BottomSheetObserverTest { ...@@ -251,8 +326,8 @@ public class BottomSheetObserverTest {
// height on its own as View::onMeasure will by default set its height/width to // 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 // be the minimum height/width of its background (if any) or expand as much as
// it can. // it can.
mContentView = new FrameLayout(mBottomSheetTestRule.getActivity()); mContentView = new FrameLayout(mTestRule.getActivity());
View child = new View(mBottomSheetTestRule.getActivity()); View child = new View(mTestRule.getActivity());
child.setLayoutParams(new ViewGroup.LayoutParams( child.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, wrappedContentHeight)); ViewGroup.LayoutParams.MATCH_PARENT, wrappedContentHeight));
mContentView.addView(child); mContentView.addView(child);
...@@ -272,11 +347,12 @@ public class BottomSheetObserverTest { ...@@ -272,11 +347,12 @@ public class BottomSheetObserverTest {
callbackHelper.waitForCallback(callCount); callbackHelper.waitForCallback(callCount);
// HALF state is forbidden when wrapping the content. // HALF state is forbidden when wrapping the content.
mBottomSheetTestRule.setSheetState(BottomSheetController.SheetState.HALF, false); ThreadUtils.runOnUiThreadBlocking(
assertEquals(BottomSheetController.SheetState.FULL, bottomSheet.getSheetState()); () -> mSheetView.setSheetState(BottomSheetController.SheetState.HALF, false));
assertEquals(BottomSheetController.SheetState.FULL, mSheetView.getSheetState());
// Check the offset. // Check the offset.
assertEquals(wrappedContentHeight + bottomSheet.getToolbarShadowHeight(), assertEquals(wrappedContentHeight + mSheetView.getToolbarShadowHeight(),
bottomSheet.getCurrentOffsetPx(), MathUtils.EPSILON); mSheetView.getCurrentOffsetPx(), MathUtils.EPSILON);
} }
} }
...@@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse; ...@@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; 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.CriteriaHelper.pollUiThread;
import static org.chromium.content_public.browser.test.util.TestThreadUtils.runOnUiThreadBlocking; import static org.chromium.content_public.browser.test.util.TestThreadUtils.runOnUiThreadBlocking;
...@@ -20,12 +21,14 @@ import org.junit.Test; ...@@ -20,12 +21,14 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction; import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.toolbar.ToolbarManager; import org.chromium.chrome.browser.toolbar.ToolbarManager;
import org.chromium.chrome.browser.ui.TabObscuringHandler; import org.chromium.chrome.browser.ui.TabObscuringHandler;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.SheetState; import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.SheetState;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.StateChangeReason; import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController.StateChangeReason;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; 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;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent.HeightMode; import org.chromium.components.browser_ui.bottomsheet.BottomSheetContent.HeightMode;
import org.chromium.ui.test.util.UiRestriction; import org.chromium.ui.test.util.UiRestriction;
...@@ -36,30 +39,32 @@ import java.util.concurrent.TimeoutException; ...@@ -36,30 +39,32 @@ import java.util.concurrent.TimeoutException;
/** This class tests the functionality of the {@link BottomSheet}. */ /** This class tests the functionality of the {@link BottomSheet}. */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE) @Restriction(UiRestriction.RESTRICTION_TYPE_PHONE)
@CommandLineFlags.Add({DISABLE_FIRST_RUN_EXPERIENCE})
public class BottomSheetTest { public class BottomSheetTest {
private static final float VELOCITY_WHEN_MOVING_UP = 1.0f; private static final float VELOCITY_WHEN_MOVING_UP = 1.0f;
private static final float VELOCITY_WHEN_MOVING_DOWN = -1.0f; private static final float VELOCITY_WHEN_MOVING_DOWN = -1.0f;
@Rule @Rule
public BottomSheetTestRule mBottomSheetTestRule = new BottomSheetTestRule(); public ChromeTabbedActivityTestRule mTestRule = new ChromeTabbedActivityTestRule();
private BottomSheetTestRule.Observer mObserver;
private TestBottomSheetContent mLowPriorityContent; private TestBottomSheetContent mLowPriorityContent;
private TestBottomSheetContent mHighPriorityContent; private TestBottomSheetContent mHighPriorityContent;
private BottomSheetController mSheetController;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
BottomSheet.setSmallScreenForTesting(false); BottomSheet.setSmallScreenForTesting(false);
mBottomSheetTestRule.startMainActivityOnBlankPage(); mTestRule.startMainActivityOnBlankPage();
mSheetController =
mTestRule.getActivity().getRootUiCoordinatorForTesting().getBottomSheetController();
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> {
mLowPriorityContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(), mLowPriorityContent = new TestBottomSheetContent(
BottomSheetContent.ContentPriority.LOW, false); mTestRule.getActivity(), BottomSheetContent.ContentPriority.LOW, false);
mHighPriorityContent = new TestBottomSheetContent(mBottomSheetTestRule.getActivity(), mHighPriorityContent = new TestBottomSheetContent(
BottomSheetContent.ContentPriority.HIGH, false); mTestRule.getActivity(), BottomSheetContent.ContentPriority.HIGH, false);
}); });
mHighPriorityContent.setPeekHeight(HeightMode.DISABLED); mHighPriorityContent.setPeekHeight(HeightMode.DISABLED);
mHighPriorityContent.setHalfHeightRatio(0.5f); mHighPriorityContent.setHalfHeightRatio(0.5f);
mHighPriorityContent.setSkipHalfStateScrollingDown(false); mHighPriorityContent.setSkipHalfStateScrollingDown(false);
mObserver = mBottomSheetTestRule.getObserver();
} }
@Test @Test
...@@ -71,7 +76,7 @@ public class BottomSheetTest { ...@@ -71,7 +76,7 @@ public class BottomSheetTest {
showContent(mHighPriorityContent, SheetState.PEEK); showContent(mHighPriorityContent, SheetState.PEEK);
assertEquals("Sheet should be peeking at the custom height.", customToolbarHeight, assertEquals("Sheet should be peeking at the custom height.", customToolbarHeight,
mBottomSheetTestRule.getBottomSheetController().getCurrentOffset()); mSheetController.getCurrentOffset());
} }
@Test @Test
...@@ -130,9 +135,9 @@ public class BottomSheetTest { ...@@ -130,9 +135,9 @@ public class BottomSheetTest {
@Test @Test
@MediumTest @MediumTest
public void testTabObscuringState() throws ExecutionException, TimeoutException { public void testTabObscuringState() throws TimeoutException {
CallbackHelper obscuringStateChangedHelper = new CallbackHelper(); CallbackHelper obscuringStateChangedHelper = new CallbackHelper();
TabObscuringHandler handler = mBottomSheetTestRule.getActivity().getTabObscuringHandler(); TabObscuringHandler handler = mTestRule.getActivity().getTabObscuringHandler();
handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled()); handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled());
mHighPriorityContent.setHasCustomScrimLifecycle(false); mHighPriorityContent.setHasCustomScrimLifecycle(false);
...@@ -154,7 +159,7 @@ public class BottomSheetTest { ...@@ -154,7 +159,7 @@ public class BottomSheetTest {
@MediumTest @MediumTest
public void testTabObscuringState_customScrim() throws ExecutionException { public void testTabObscuringState_customScrim() throws ExecutionException {
CallbackHelper obscuringStateChangedHelper = new CallbackHelper(); CallbackHelper obscuringStateChangedHelper = new CallbackHelper();
TabObscuringHandler handler = mBottomSheetTestRule.getActivity().getTabObscuringHandler(); TabObscuringHandler handler = mTestRule.getActivity().getTabObscuringHandler();
handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled()); handler.addObserver((isObscured) -> obscuringStateChangedHelper.notifyCalled());
mHighPriorityContent.setHasCustomScrimLifecycle(true); mHighPriorityContent.setHasCustomScrimLifecycle(true);
...@@ -172,20 +177,19 @@ public class BottomSheetTest { ...@@ -172,20 +177,19 @@ public class BottomSheetTest {
@Test @Test
@MediumTest @MediumTest
public void testOmniboxFocusSuppressesSheet() { public void testOmniboxFocusSuppressesSheet() {
ToolbarManager toolbarManager = mBottomSheetTestRule.getActivity() ToolbarManager toolbarManager =
.getRootUiCoordinatorForTesting() mTestRule.getActivity().getRootUiCoordinatorForTesting().getToolbarManager();
.getToolbarManager();
showContent(mHighPriorityContent, SheetState.HALF); showContent(mHighPriorityContent, SheetState.HALF);
runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(true, 0)); runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(true, 0));
assertEquals("The bottom sheet should be hidden.", SheetState.HIDDEN, assertEquals("The bottom sheet should be hidden.", SheetState.HIDDEN,
mBottomSheetTestRule.getBottomSheetController().getSheetState()); mSheetController.getSheetState());
runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(false, 0)); runOnUiThreadBlocking(() -> toolbarManager.setUrlBarFocus(false, 0));
assertNotEquals("The bottom sheet should not be hidden.", SheetState.HIDDEN, assertNotEquals("The bottom sheet should not be hidden.", SheetState.HIDDEN,
mBottomSheetTestRule.getBottomSheetController().getSheetState()); mSheetController.getSheetState());
} }
@Test @Test
...@@ -193,22 +197,21 @@ public class BottomSheetTest { ...@@ -193,22 +197,21 @@ public class BottomSheetTest {
public void testSuppressionState_unsuppress() { public void testSuppressionState_unsuppress() {
showContent(mHighPriorityContent, SheetState.HALF); showContent(mHighPriorityContent, SheetState.HALF);
BottomSheetController controller = mBottomSheetTestRule.getBottomSheetController();
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> {
controller.suppressSheet(StateChangeReason.NONE); mSheetController.suppressSheet(StateChangeReason.NONE);
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations(); ((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
}); });
assertEquals("The sheet should be in the hidden state.", SheetState.HIDDEN, assertEquals("The sheet should be in the hidden state.", SheetState.HIDDEN,
controller.getSheetState()); mSheetController.getSheetState());
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> {
controller.unsuppressSheet(); mSheetController.unsuppressSheet();
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations(); ((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
}); });
assertEquals("The sheet should be restored to the half state.", SheetState.HALF, assertEquals("The sheet should be restored to the half state.", SheetState.HALF,
controller.getSheetState()); mSheetController.getSheetState());
} }
@Test @Test
...@@ -216,51 +219,44 @@ public class BottomSheetTest { ...@@ -216,51 +219,44 @@ public class BottomSheetTest {
public void testSuppressionState_unsuppressDifferentContent() { public void testSuppressionState_unsuppressDifferentContent() {
showContent(mHighPriorityContent, SheetState.HALF); showContent(mHighPriorityContent, SheetState.HALF);
BottomSheetController controller = mBottomSheetTestRule.getBottomSheetController();
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> {
controller.suppressSheet(StateChangeReason.NONE); mSheetController.suppressSheet(StateChangeReason.NONE);
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations(); ((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
}); });
assertEquals("The sheet should be in the hidden state.", SheetState.HIDDEN, 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); showContent(mLowPriorityContent, SheetState.PEEK);
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> {
controller.unsuppressSheet(); mSheetController.unsuppressSheet();
((BottomSheet) controller.getBottomSheetViewForTesting()).endAnimations(); ((BottomSheet) mSheetController.getBottomSheetViewForTesting()).endAnimations();
}); });
assertEquals("The sheet should be restored to the peek state.", SheetState.PEEK, assertEquals("The sheet should be restored to the peek state.", SheetState.PEEK,
controller.getSheetState()); mSheetController.getSheetState());
} }
private void hideSheet() { private void hideSheet() {
runOnUiThreadBlocking( runOnUiThreadBlocking(
() -> mBottomSheetTestRule.getBottomSheetController().setSheetStateForTesting( () -> mSheetController.setSheetStateForTesting(SheetState.HIDDEN, false));
SheetState.HIDDEN, false));
} }
private float getMaxSheetHeightInPx() { private float getMaxSheetHeightInPx() {
return mBottomSheetTestRule.getBottomSheet().getSheetContainerHeight(); return mSheetController.getContainerHeight();
} }
private @SheetState int simulateScrollTo(float targetHeightInPx, float yUpwardsVelocity) { private @SheetState int simulateScrollTo(float targetHeightInPx, float yUpwardsVelocity) {
return mBottomSheetTestRule.getBottomSheetController().forceScrollingForTesting( return mSheetController.forceScrollingForTesting(targetHeightInPx, yUpwardsVelocity);
targetHeightInPx, yUpwardsVelocity);
} }
/** @param content The content to show in the bottom sheet. */ /** @param content The content to show in the bottom sheet. */
private void showContent(BottomSheetContent content, @SheetState int targetState) { private void showContent(BottomSheetContent content, @SheetState int targetState) {
runOnUiThreadBlocking(() -> { runOnUiThreadBlocking(() -> { mSheetController.requestShowContent(content, false); });
mBottomSheetTestRule.getBottomSheetController().requestShowContent(content, false); mSheetController.setSheetStateForTesting(targetState, false);
}); pollUiThread(() -> mSheetController.getSheetState() == targetState);
mBottomSheetTestRule.getBottomSheetController().setSheetStateForTesting(targetState, false);
pollUiThread(()
-> mBottomSheetTestRule.getBottomSheetController().getSheetState()
== targetState);
} }
} }
...@@ -175,7 +175,6 @@ android_library("chrome_java_test_support") { ...@@ -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/omnibox/suggestions/AutocompleteCoordinatorTestUtils.java",
"javatests/src/org/chromium/chrome/browser/tab/MockTab.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/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/ChromeActivityTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeBrowserTestRule.java", "javatests/src/org/chromium/chrome/test/ChromeBrowserTestRule.java",
"javatests/src/org/chromium/chrome/test/ChromeJUnit4ClassRunner.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