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.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 /** A {@link CallbackHelper} that can wait for the bottom sheet to be cl osed. */ | |
27 private final CallbackHelper mClosedCallbackHelper = new CallbackHelper( ); | |
28 | |
29 /** A {@link CallbackHelper} that can wait for the bottom sheet to be op ened. */ | |
30 private final CallbackHelper mOpenedCallbackHelper = new CallbackHelper( ); | |
31 | |
32 /** A {@link CallbackHelper} that can wait for the onTransitionPeekToHal f event. */ | |
33 private final CallbackHelper mPeekToHalfCallbackHelper = new CallbackHel per(); | |
34 | |
35 /** The last value that the onTransitionPeekToHalf event sent. */ | |
36 private float mLastPeekToHalfValue; | |
37 | |
38 @Override | |
39 public void onTransitionPeekToHalf(float fraction) { | |
40 mLastPeekToHalfValue = fraction; | |
41 mPeekToHalfCallbackHelper.notifyCalled(); | |
42 } | |
43 | |
44 @Override | |
45 public void onSheetOpened() { | |
46 mOpenedCallbackHelper.notifyCalled(); | |
47 } | |
48 | |
49 @Override | |
50 public void onSheetClosed() { | |
51 mClosedCallbackHelper.notifyCalled(); | |
52 } | |
53 | |
54 @Override | |
55 public void onLoadUrl(String url) {} | |
56 | |
57 public CallbackHelper getClosedCallbackHelper() { | |
58 return mClosedCallbackHelper; | |
59 } | |
60 | |
61 public CallbackHelper getOpenedCallbackHelper() { | |
62 return mOpenedCallbackHelper; | |
63 } | |
64 | |
65 public CallbackHelper getPeekToHalfCallbackHelper() { | |
66 return mPeekToHalfCallbackHelper; | |
67 } | |
68 | |
69 public float getLastPeekToHalfValue() { | |
70 return mLastPeekToHalfValue; | |
71 } | |
72 } | |
73 | |
74 @Override | |
75 protected void setUp() throws Exception { | |
76 super.setUp(); | |
77 | |
78 mBottomSheet = getActivity().getBottomSheet(); | |
79 mObserver = new TestBottomSheetObserver(); | |
80 mBottomSheet.addObserver(mObserver); | |
81 } | |
82 | |
83 /** | |
84 * Set the bottom sheet's state on the UI thread. | |
85 * @param state The state to set the sheet to. | |
86 * @param animate If the sheet should animate to the provided state. | |
87 */ | |
88 private void setSheetState(final int state, final boolean animate) { | |
dgn
2017/02/20 11:37:14
maybe move these utility methods to the base test
mdjones
2017/02/21 17:19:11
Done.
| |
89 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
90 @Override | |
91 public void run() { | |
92 mBottomSheet.setSheetState(state, animate); | |
93 } | |
94 }); | |
95 } | |
96 | |
97 /** | |
98 * Set the bottom sheet's offset from the bottom of the screen on the UI thr ead. | |
99 * @param offset The offset from the bottom that the sheet should be. | |
100 */ | |
101 private void setSheetOffsetFromBottom(final float offset) { | |
102 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
103 @Override | |
104 public void run() { | |
105 mBottomSheet.setSheetOffsetFromBottomForTesting(offset); | |
106 } | |
107 }); | |
108 } | |
109 | |
110 /** | |
111 * Test that the onSheetClosed event is triggered if the sheet is closed wit hout animation. | |
112 */ | |
113 @MediumTest | |
114 public void testCloseEventCalledNoAnimation() throws InterruptedException, T imeoutException { | |
115 setSheetState(BottomSheet.SHEET_STATE_FULL, false); | |
116 | |
117 CallbackHelper closedCallbackHelper = mObserver.getClosedCallbackHelper( ); | |
118 | |
119 int initialOpenedCount = mObserver.getOpenedCallbackHelper().getCallCoun t(); | |
120 | |
121 int closedCallbackCount = closedCallbackHelper.getCallCount(); | |
122 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); | |
123 closedCallbackHelper.waitForCallback(closedCallbackCount, 1); | |
124 | |
125 assertEquals(initialOpenedCount, mObserver.getOpenedCallbackHelper().get CallCount()); | |
126 } | |
127 | |
128 /** | |
129 * Test that the onSheetClosed event is triggered if the sheet is closed wit h animation. | |
130 */ | |
131 @MediumTest | |
132 public void testCloseEventCalledWithAnimation() throws InterruptedException, TimeoutException { | |
133 setSheetState(BottomSheet.SHEET_STATE_FULL, false); | |
134 | |
135 CallbackHelper closedCallbackHelper = mObserver.getClosedCallbackHelper( ); | |
136 | |
137 int initialOpenedCount = mObserver.getOpenedCallbackHelper().getCallCoun t(); | |
138 | |
139 int closedCallbackCount = closedCallbackHelper.getCallCount(); | |
140 setSheetState(BottomSheet.SHEET_STATE_PEEK, true); | |
141 closedCallbackHelper.waitForCallback(closedCallbackCount, 1); | |
142 | |
143 assertEquals(initialOpenedCount, mObserver.getOpenedCallbackHelper().get CallCount()); | |
144 } | |
145 | |
146 /** | |
147 * Test that the onSheetOpened event is triggered if the sheet is opened wit hout animation. | |
148 */ | |
149 @MediumTest | |
150 public void testOpenedEventCalledNoAnimation() throws InterruptedException, TimeoutException { | |
151 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); | |
152 | |
153 CallbackHelper openedCallbackHelper = mObserver.getOpenedCallbackHelper( ); | |
154 | |
155 int initialClosedCount = mObserver.getClosedCallbackHelper().getCallCoun t(); | |
156 | |
157 int openedCallbackCount = openedCallbackHelper.getCallCount(); | |
158 setSheetState(BottomSheet.SHEET_STATE_FULL, false); | |
159 openedCallbackHelper.waitForCallback(openedCallbackCount, 1); | |
160 | |
161 assertEquals(initialClosedCount, mObserver.getClosedCallbackHelper().get CallCount()); | |
162 } | |
163 | |
164 /** | |
165 * Test that the onSheetOpened event is triggered if the sheet is opened wit h animation. | |
166 */ | |
167 @MediumTest | |
168 public void testOpenedEventCalledWithAnimation() throws InterruptedException , TimeoutException { | |
169 setSheetState(BottomSheet.SHEET_STATE_PEEK, false); | |
170 | |
171 CallbackHelper openedCallbackHelper = mObserver.getOpenedCallbackHelper( ); | |
172 | |
173 int initialClosedCount = mObserver.getClosedCallbackHelper().getCallCoun t(); | |
174 | |
175 int openedCallbackCount = openedCallbackHelper.getCallCount(); | |
176 setSheetState(BottomSheet.SHEET_STATE_FULL, true); | |
177 openedCallbackHelper.waitForCallback(openedCallbackCount, 1); | |
178 | |
179 assertEquals(initialClosedCount, mObserver.getClosedCallbackHelper().get CallCount()); | |
180 } | |
181 | |
182 /** | |
183 * Test the onTransitionPeekToHalf event. | |
184 */ | |
185 @MediumTest | |
186 public void testPeekToHalfTransition() throws InterruptedException, TimeoutE xception { | |
187 CallbackHelper callbackHelper = mObserver.getPeekToHalfCallbackHelper(); | |
188 | |
189 float peekHeight = mBottomSheet.getPeekRatio() * mBottomSheet.getSheetCo ntainerHeight(); | |
190 float halfHeight = mBottomSheet.getHalfRatio() * mBottomSheet.getSheetCo ntainerHeight(); | |
191 float fullHeight = mBottomSheet.getFullRatio() * mBottomSheet.getSheetCo ntainerHeight(); | |
192 | |
193 float midPeekHalf = (peekHeight + halfHeight) / 2f; | |
194 float midHalfFull = (halfHeight + fullHeight) / 2f; | |
195 | |
196 // When in the peeking state, the transition value should be 0. | |
197 int callbackCount = callbackHelper.getCallCount(); | |
198 setSheetOffsetFromBottom(peekHeight); | |
199 callbackHelper.waitForCallback(callbackCount, 1); | |
200 assertEquals(0f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); | |
201 | |
202 // When in between peek and half states, the transition value should be 0.5. | |
203 callbackCount = callbackHelper.getCallCount(); | |
204 setSheetOffsetFromBottom(midPeekHalf); | |
205 callbackHelper.waitForCallback(callbackCount, 1); | |
206 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON ); | |
207 | |
208 // After jumping to the full state (skipping the half state), the event should have | |
209 // triggered once more with a max value of 1. | |
210 callbackCount = callbackHelper.getCallCount(); | |
211 setSheetOffsetFromBottom(fullHeight); | |
212 callbackHelper.waitForCallback(callbackCount, 1); | |
213 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); | |
214 | |
215 // Moving from full to somewhere between half and full should not trigge r the event. | |
216 callbackCount = callbackHelper.getCallCount(); | |
217 setSheetOffsetFromBottom(midHalfFull); | |
218 assertEquals(callbackCount, callbackHelper.getCallCount()); | |
219 | |
220 // Reset the sheet to be between peek and half states. | |
221 callbackCount = callbackHelper.getCallCount(); | |
222 setSheetOffsetFromBottom(midPeekHalf); | |
223 callbackHelper.waitForCallback(callbackCount, 1); | |
224 assertEquals(0.5f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON ); | |
225 | |
226 // At the half state the event should send 1. | |
227 callbackCount = callbackHelper.getCallCount(); | |
228 setSheetOffsetFromBottom(halfHeight); | |
229 callbackHelper.waitForCallback(callbackCount, 1); | |
230 assertEquals(1f, mObserver.getLastPeekToHalfValue(), MathUtils.EPSILON); | |
231 } | |
232 } | |
OLD | NEW |