Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/widget/BottomSheetObserverTest.java

Issue 2698613006: [Home] Add lifecycle events and some tests (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.ThreadUtils;
10 import org.chromium.base.test.util.CallbackHelper;
11 import org.chromium.chrome.browser.util.MathUtils;
12 import org.chromium.chrome.test.BottomSheetTestCaseBase;
13
14 import java.util.concurrent.TimeoutException;
15
16 /** This class tests the functionality of the {@link BottomSheetObserver}. */
17 public class BottomSheetObserverTest extends BottomSheetTestCaseBase {
18 /** A handle to the sheet's observer. */
19 private TestBottomSheetObserver mObserver;
20
21 /** A handle to the bottom sheet. */
22 private BottomSheet mBottomSheet;
23
24 /** An observer used to record events that occur with respect to the bottom sheet. */
25 private static class TestBottomSheetObserver implements BottomSheetObserver {
26 /** The number of times the sheet has been opened. */
27 private int mOpenedCount;
28
29 /** The number of times the sheet has been closed. */
30 private int mClosedCount;
31
32 /** The last value that the onTransitionPeekToHalf event sent. */
33 private float mLastPeekToHalfValue;
34
35 /** A {@link CallbackHelper} that can wait for the bottom sheet to be cl osed. */
36 private CallbackHelper mClosedCallbackHelper = new CallbackHelper();
gone 2017/02/17 20:02:32 finals, move upwards
mdjones 2017/02/17 21:33:19 Done.
37
38 /** A {@link CallbackHelper} that can wait for the bottom sheet to be op ened. */
39 private CallbackHelper mOpenedCallbackHelper = new CallbackHelper();
40
41 /** A {@link CallbackHelper} that can wait for the onTransitionPeekToHal f event. */
42 private CallbackHelper mPeekToHalfCallbackHelper = new CallbackHelper();
43
44 @Override
45 public void onTransitionPeekToHalf(float fraction) {
46 mLastPeekToHalfValue = fraction;
47 mPeekToHalfCallbackHelper.notifyCalled();
48 }
49
50 @Override
51 public void onSheetOpened() {
52 mOpenedCount++;
Theresa 2017/02/17 18:54:48 mOpenedCallbackHelper and mClosedCallbackHelper ke
mdjones 2017/02/17 21:33:19 Removed in favor of the one CallbackHelper has.
53 mOpenedCallbackHelper.notifyCalled();
54 }
55
56 @Override
57 public void onSheetClosed() {
58 mClosedCount++;
59 mClosedCallbackHelper.notifyCalled();
60 }
61
62 @Override
63 public void onLoadUrl(String url) {}
64
65 public CallbackHelper getClosedCallbackHelper() {
66 return mClosedCallbackHelper;
67 }
68
69 public CallbackHelper getOpenedCallbackHelper() {
70 return mOpenedCallbackHelper;
71 }
72
73 public CallbackHelper getPeekToHalfCallbackHelper() {
74 return mPeekToHalfCallbackHelper;
75 }
76
77 public int getOpenedCount() {
78 return mOpenedCount;
79 }
80
81 public int getClosedCount() {
82 return mClosedCount;
83 }
84
85 public float getLastPeekToHalfValue() {
86 return mLastPeekToHalfValue;
87 }
88 }
89
90 @Override
91 protected void setUp() throws Exception {
92 super.setUp();
93
94 mBottomSheet = getActivity().getBottomSheet();
95 mObserver = new TestBottomSheetObserver();
96 mBottomSheet.addObserver(mObserver);
97 }
98
99 /**
100 * Set the bottom sheet's state on the UI thread.
101 * @param state The state to set the sheet to.
102 * @param animate If the sheet should animate to the provided state.
103 */
104 private void setSheetState(final int state, final boolean animate) {
105 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
106 @Override
107 public void run() {
108 mBottomSheet.setSheetState(state, animate);
109 }
110 });
111 }
112
113 /**
114 * Set the bottom sheet's offset from the bottom of the screen on the UI thr ead.
115 * @param offset The offset from the bottom that the sheet should be.
116 */
117 private void setSheetOffsetFromBottom(final float offset) {
118 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
119 @Override
120 public void run() {
121 mBottomSheet.setSheetOffsetFromBottom(offset);
122 }
123 });
124 }
125
126 /**
127 * Test that the onSheetClosed event is triggered if the sheet is closed wit hout animation.
128 */
129 @MediumTest
130 public void testCloseEventCalledNoAnimation() throws InterruptedException, T imeoutException {
131 setSheetState(BottomSheet.SHEET_STATE_FULL, false);
132
133 CallbackHelper callbackHelper = mObserver.getClosedCallbackHelper();
134
135 int initialOpenedCount = mObserver.getOpenedCount();
136 int initialClosedCount = mObserver.getClosedCount();
137
138 int callbackCount = callbackHelper.getCallCount();
139 setSheetState(BottomSheet.SHEET_STATE_PEEK, false);
140 callbackHelper.waitForCallback(callbackCount, 1);
141
142 // Make sure the close event was only called once.
143 assertEquals(initialClosedCount + 1, mObserver.getClosedCount());
144
145 // Make sure the open event was not called.
146 assertEquals(initialOpenedCount, mObserver.getOpenedCount());
147 }
148
149 /**
150 * Test that the onSheetClosed event is triggered if the sheet is closed wit h animation.
151 */
152 @MediumTest
153 public void testCloseEventCalledWithAnimation() throws InterruptedException, TimeoutException {
154 setSheetState(BottomSheet.SHEET_STATE_FULL, false);
155
156 CallbackHelper callbackHelper = mObserver.getClosedCallbackHelper();
157
158 int initialOpenedCount = mObserver.getOpenedCount();
159 int initialClosedCount = mObserver.getClosedCount();
160
161 int callbackCount = callbackHelper.getCallCount();
162 setSheetState(BottomSheet.SHEET_STATE_PEEK, true);
163 callbackHelper.waitForCallback(callbackCount, 1);
164
165 // Make sure the close event was only called once.
gone 2017/02/17 20:02:32 Don't know if the comments here buy you much. You
mdjones 2017/02/17 21:33:19 Removed.
166 assertEquals(initialClosedCount + 1, mObserver.getClosedCount());
167
168 // Make sure the open event was not called.
169 assertEquals(initialOpenedCount, mObserver.getOpenedCount());
170 }
171
172 /**
173 * Test that the onSheetOpened event is triggered if the sheet is opened wit hout animation.
174 */
175 @MediumTest
176 public void testOpenedEventCalledNoAnimation() throws InterruptedException, TimeoutException {
177 setSheetState(BottomSheet.SHEET_STATE_PEEK, false);
178
179 CallbackHelper callbackHelper = mObserver.getOpenedCallbackHelper();
180
181 int initialOpenedCount = mObserver.getOpenedCount();
182 int initialClosedCount = mObserver.getClosedCount();
183
184 int callbackCount = callbackHelper.getCallCount();
185 setSheetState(BottomSheet.SHEET_STATE_FULL, false);
186 callbackHelper.waitForCallback(callbackCount, 1);
187
188 // Make sure the open event was only called once.
189 assertEquals(initialOpenedCount + 1, mObserver.getOpenedCount());
190
191 // Make sure the close event was not called.
192 assertEquals(initialClosedCount, mObserver.getClosedCount());
193 }
194
195 /**
196 * Test that the onSheetOpened event is triggered if the sheet is opened wit h animation.
197 */
198 @MediumTest
199 public void testOpenedEventCalledWithAnimation() throws InterruptedException , TimeoutException {
200 setSheetState(BottomSheet.SHEET_STATE_PEEK, false);
201
202 CallbackHelper callbackHelper = mObserver.getOpenedCallbackHelper();
203
204 int initialOpenedCount = mObserver.getOpenedCount();
205 int initialClosedCount = mObserver.getClosedCount();
206
207 int callbackCount = callbackHelper.getCallCount();
208 setSheetState(BottomSheet.SHEET_STATE_FULL, true);
209 callbackHelper.waitForCallback(callbackCount, 1);
210
211 // Make sure the open event was only called once.
212 assertEquals(initialOpenedCount + 1, mObserver.getOpenedCount());
213
214 // Make sure the close event was not called.
215 assertEquals(initialClosedCount, mObserver.getClosedCount());
216 }
217
218 /**
219 * Test the onTransitionPeekToHalf event.
220 */
221 @MediumTest
222 public void testPeekToHalfTransition() throws InterruptedException, TimeoutE xception {
223 CallbackHelper callbackHelper = mObserver.getPeekToHalfCallbackHelper();
224
225 float peekHeight = mBottomSheet.getPeekRatio() * mBottomSheet.getSheetCo ntainerHeight();
226 float halfHeight = mBottomSheet.getHalfRatio() * mBottomSheet.getSheetCo ntainerHeight();
227 float fullHeight = mBottomSheet.getFullRatio() * mBottomSheet.getSheetCo ntainerHeight();
228
229 float midPeekHalf = (peekHeight + halfHeight) / 2f;
230 float midHalfFull = (halfHeight + fullHeight) / 2f;
231
232 // When in the peeking state, the transition value should be 0.
233 int callbackCount = callbackHelper.getCallCount();
234 setSheetOffsetFromBottom(peekHeight);
235 callbackHelper.waitForCallback(callbackCount, 1);
236 assertEquals(0f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON);
237
238 // When in between peek and half states, the transition value should be 0.5.
239 callbackCount = callbackHelper.getCallCount();
240 setSheetOffsetFromBottom(midPeekHalf);
241 callbackHelper.waitForCallback(callbackCount, 1);
242 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON );
243
244 // After jumping to the full state (skipping the half state), the event should have
245 // triggered once more with a max value of 1.
246 callbackCount = callbackHelper.getCallCount();
247 setSheetOffsetFromBottom(fullHeight);
248 callbackHelper.waitForCallback(callbackCount, 1);
249 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON);
250
251 // Moving from full to somewhere between half and full should not trigge r the event.
252 callbackCount = callbackHelper.getCallCount();
253 setSheetOffsetFromBottom(midHalfFull);
254 assertEquals(callbackCount, callbackHelper.getCallCount());
255
256 // Reset the sheet to be between peek and half states.
257 callbackCount = callbackHelper.getCallCount();
258 setSheetOffsetFromBottom(midPeekHalf);
259 callbackHelper.waitForCallback(callbackCount, 1);
260 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON );
261
262 // At the half state the event should send 1.
263 callbackCount = callbackHelper.getCallCount();
264 setSheetOffsetFromBottom(halfHeight);
265 callbackHelper.waitForCallback(callbackCount, 1);
266 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON);
267 }
268 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698