OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.support.test.filters.MediumTest; |
| 8 |
| 9 import org.chromium.base.test.util.CallbackHelper; |
| 10 import org.chromium.chrome.browser.util.MathUtils; |
| 11 import org.chromium.chrome.test.BottomSheetTestCaseBase; |
| 12 |
| 13 import java.util.concurrent.TimeoutException; |
| 14 |
| 15 /** This class tests the functionality of the {@link BottomSheetObserver}. */ |
| 16 public class BottomSheetObserverTest extends BottomSheetTestCaseBase { |
| 17 /** A handle to the sheet's observer. */ |
| 18 private TestBottomSheetObserver mObserver; |
| 19 |
| 20 /** An observer used to record events that occur with respect to the bottom
sheet. */ |
| 21 private static class TestBottomSheetObserver implements BottomSheetObserver
{ |
| 22 /** A {@link CallbackHelper} that can wait for the bottom sheet to be cl
osed. */ |
| 23 private final CallbackHelper mClosedCallbackHelper = new CallbackHelper(
); |
| 24 |
| 25 /** A {@link CallbackHelper} that can wait for the bottom sheet to be op
ened. */ |
| 26 private final CallbackHelper mOpenedCallbackHelper = new CallbackHelper(
); |
| 27 |
| 28 /** A {@link CallbackHelper} that can wait for the onTransitionPeekToHal
f event. */ |
| 29 private final CallbackHelper mPeekToHalfCallbackHelper = new CallbackHel
per(); |
| 30 |
| 31 /** The last value that the onTransitionPeekToHalf event sent. */ |
| 32 private float mLastPeekToHalfValue; |
| 33 |
| 34 @Override |
| 35 public void onTransitionPeekToHalf(float fraction) { |
| 36 mLastPeekToHalfValue = fraction; |
| 37 mPeekToHalfCallbackHelper.notifyCalled(); |
| 38 } |
| 39 |
| 40 @Override |
| 41 public void onSheetOpened() { |
| 42 mOpenedCallbackHelper.notifyCalled(); |
| 43 } |
| 44 |
| 45 @Override |
| 46 public void onSheetClosed() { |
| 47 mClosedCallbackHelper.notifyCalled(); |
| 48 } |
| 49 |
| 50 @Override |
| 51 public void onLoadUrl(String url) {} |
| 52 |
| 53 public CallbackHelper getClosedCallbackHelper() { |
| 54 return mClosedCallbackHelper; |
| 55 } |
| 56 |
| 57 public CallbackHelper getOpenedCallbackHelper() { |
| 58 return mOpenedCallbackHelper; |
| 59 } |
| 60 |
| 61 public CallbackHelper getPeekToHalfCallbackHelper() { |
| 62 return mPeekToHalfCallbackHelper; |
| 63 } |
| 64 |
| 65 public float getLastPeekToHalfValue() { |
| 66 return mLastPeekToHalfValue; |
| 67 } |
| 68 } |
| 69 |
| 70 @Override |
| 71 protected void setUp() throws Exception { |
| 72 super.setUp(); |
| 73 |
| 74 mObserver = new TestBottomSheetObserver(); |
| 75 mBottomSheet.addObserver(mObserver); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Test that the onSheetClosed event is triggered if the sheet is closed wit
hout animation. |
| 80 */ |
| 81 @MediumTest |
| 82 public void testCloseEventCalledNoAnimation() throws InterruptedException, T
imeoutException { |
| 83 setSheetState(BottomSheet.SHEET_STATE_FULL, false); |
| 84 |
| 85 CallbackHelper closedCallbackHelper = mObserver.getClosedCallbackHelper(
); |
| 86 |
| 87 int initialOpenedCount = mObserver.getOpenedCallbackHelper().getCallCoun
t(); |
| 88 |
| 89 int closedCallbackCount = closedCallbackHelper.getCallCount(); |
| 90 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); |
| 91 closedCallbackHelper.waitForCallback(closedCallbackCount, 1); |
| 92 |
| 93 assertEquals(initialOpenedCount, mObserver.getOpenedCallbackHelper().get
CallCount()); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Test that the onSheetClosed event is triggered if the sheet is closed wit
h animation. |
| 98 */ |
| 99 @MediumTest |
| 100 public void testCloseEventCalledWithAnimation() throws InterruptedException,
TimeoutException { |
| 101 setSheetState(BottomSheet.SHEET_STATE_FULL, false); |
| 102 |
| 103 CallbackHelper closedCallbackHelper = mObserver.getClosedCallbackHelper(
); |
| 104 |
| 105 int initialOpenedCount = mObserver.getOpenedCallbackHelper().getCallCoun
t(); |
| 106 |
| 107 int closedCallbackCount = closedCallbackHelper.getCallCount(); |
| 108 setSheetState(BottomSheet.SHEET_STATE_PEEK, true); |
| 109 closedCallbackHelper.waitForCallback(closedCallbackCount, 1); |
| 110 |
| 111 assertEquals(initialOpenedCount, mObserver.getOpenedCallbackHelper().get
CallCount()); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Test that the onSheetOpened event is triggered if the sheet is opened wit
hout animation. |
| 116 */ |
| 117 @MediumTest |
| 118 public void testOpenedEventCalledNoAnimation() throws InterruptedException,
TimeoutException { |
| 119 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); |
| 120 |
| 121 CallbackHelper openedCallbackHelper = mObserver.getOpenedCallbackHelper(
); |
| 122 |
| 123 int initialClosedCount = mObserver.getClosedCallbackHelper().getCallCoun
t(); |
| 124 |
| 125 int openedCallbackCount = openedCallbackHelper.getCallCount(); |
| 126 setSheetState(BottomSheet.SHEET_STATE_FULL, false); |
| 127 openedCallbackHelper.waitForCallback(openedCallbackCount, 1); |
| 128 |
| 129 assertEquals(initialClosedCount, mObserver.getClosedCallbackHelper().get
CallCount()); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Test that the onSheetOpened event is triggered if the sheet is opened wit
h animation. |
| 134 */ |
| 135 @MediumTest |
| 136 public void testOpenedEventCalledWithAnimation() throws InterruptedException
, TimeoutException { |
| 137 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); |
| 138 |
| 139 CallbackHelper openedCallbackHelper = mObserver.getOpenedCallbackHelper(
); |
| 140 |
| 141 int initialClosedCount = mObserver.getClosedCallbackHelper().getCallCoun
t(); |
| 142 |
| 143 int openedCallbackCount = openedCallbackHelper.getCallCount(); |
| 144 setSheetState(BottomSheet.SHEET_STATE_FULL, true); |
| 145 openedCallbackHelper.waitForCallback(openedCallbackCount, 1); |
| 146 |
| 147 assertEquals(initialClosedCount, mObserver.getClosedCallbackHelper().get
CallCount()); |
| 148 } |
| 149 |
| 150 /** |
| 151 * Test the onTransitionPeekToHalf event. |
| 152 */ |
| 153 @MediumTest |
| 154 public void testPeekToHalfTransition() throws InterruptedException, TimeoutE
xception { |
| 155 CallbackHelper callbackHelper = mObserver.getPeekToHalfCallbackHelper(); |
| 156 |
| 157 float peekHeight = mBottomSheet.getPeekRatio() * mBottomSheet.getSheetCo
ntainerHeight(); |
| 158 float halfHeight = mBottomSheet.getHalfRatio() * mBottomSheet.getSheetCo
ntainerHeight(); |
| 159 float fullHeight = mBottomSheet.getFullRatio() * mBottomSheet.getSheetCo
ntainerHeight(); |
| 160 |
| 161 float midPeekHalf = (peekHeight + halfHeight) / 2f; |
| 162 float midHalfFull = (halfHeight + fullHeight) / 2f; |
| 163 |
| 164 // When in the peeking state, the transition value should be 0. |
| 165 int callbackCount = callbackHelper.getCallCount(); |
| 166 setSheetOffsetFromBottom(peekHeight); |
| 167 callbackHelper.waitForCallback(callbackCount, 1); |
| 168 assertEquals(0f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); |
| 169 |
| 170 // When in between peek and half states, the transition value should be
0.5. |
| 171 callbackCount = callbackHelper.getCallCount(); |
| 172 setSheetOffsetFromBottom(midPeekHalf); |
| 173 callbackHelper.waitForCallback(callbackCount, 1); |
| 174 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON
); |
| 175 |
| 176 // After jumping to the full state (skipping the half state), the event
should have |
| 177 // triggered once more with a max value of 1. |
| 178 callbackCount = callbackHelper.getCallCount(); |
| 179 setSheetOffsetFromBottom(fullHeight); |
| 180 callbackHelper.waitForCallback(callbackCount, 1); |
| 181 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); |
| 182 |
| 183 // Moving from full to somewhere between half and full should not trigge
r the event. |
| 184 callbackCount = callbackHelper.getCallCount(); |
| 185 setSheetOffsetFromBottom(midHalfFull); |
| 186 assertEquals(callbackCount, callbackHelper.getCallCount()); |
| 187 |
| 188 // Reset the sheet to be between peek and half states. |
| 189 callbackCount = callbackHelper.getCallCount(); |
| 190 setSheetOffsetFromBottom(midPeekHalf); |
| 191 callbackHelper.waitForCallback(callbackCount, 1); |
| 192 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON
); |
| 193 |
| 194 // At the half state the event should send 1. |
| 195 callbackCount = callbackHelper.getCallCount(); |
| 196 setSheetOffsetFromBottom(halfHeight); |
| 197 callbackHelper.waitForCallback(callbackCount, 1); |
| 198 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); |
| 199 } |
| 200 } |
OLD | NEW |