OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.compositor.bottombar; | 5 package org.chromium.chrome.browser.compositor.bottombar; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
| 9 import android.support.test.annotation.UiThreadTest; |
8 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
9 import android.test.InstrumentationTestCase; | 11 import android.support.test.rule.UiThreadTestRule; |
| 12 |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Rule; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
10 | 18 |
11 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
12 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanel.PanelState; | 20 import org.chromium.chrome.browser.compositor.bottombar.OverlayPanel.PanelState; |
| 21 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
13 | 22 |
14 /** | 23 /** |
15 * Tests logic in the OverlayPanelBase. | 24 * Tests logic in the OverlayPanelBase. |
16 */ | 25 */ |
17 public class OverlayPanelBaseTest extends InstrumentationTestCase { | 26 @RunWith(ChromeJUnit4ClassRunner.class) |
18 | 27 public class OverlayPanelBaseTest { |
19 private static final float UPWARD_VELOCITY = -1.0f; | 28 private static final float UPWARD_VELOCITY = -1.0f; |
20 private static final float DOWNWARD_VELOCITY = 1.0f; | 29 private static final float DOWNWARD_VELOCITY = 1.0f; |
21 | 30 |
22 private static final float MOCK_PEEKED_HEIGHT = 200.0f; | 31 private static final float MOCK_PEEKED_HEIGHT = 200.0f; |
23 private static final float MOCK_EXPANDED_HEIGHT = 400.0f; | 32 private static final float MOCK_EXPANDED_HEIGHT = 400.0f; |
24 private static final float MOCK_MAXIMIZED_HEIGHT = 600.0f; | 33 private static final float MOCK_MAXIMIZED_HEIGHT = 600.0f; |
25 | 34 |
| 35 @Rule |
| 36 public UiThreadTestRule mRule = new UiThreadTestRule(); |
| 37 |
26 MockOverlayPanel mNoExpandPanel; | 38 MockOverlayPanel mNoExpandPanel; |
27 MockOverlayPanel mExpandPanel; | 39 MockOverlayPanel mExpandPanel; |
28 | 40 |
29 /** | 41 /** |
30 * Mock OverlayPanel. | 42 * Mock OverlayPanel. |
31 */ | 43 */ |
32 private static class MockOverlayPanel extends OverlayPanel { | 44 private static class MockOverlayPanel extends OverlayPanel { |
33 public MockOverlayPanel(Context context, OverlayPanelManager manager) { | 45 public MockOverlayPanel(Context context, OverlayPanelManager manager) { |
34 super(context, null, null, manager); | 46 super(context, null, null, manager); |
35 } | 47 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 public float getThresholdToNextState() { | 85 public float getThresholdToNextState() { |
74 return 0.3f; | 86 return 0.3f; |
75 } | 87 } |
76 | 88 |
77 @Override | 89 @Override |
78 protected boolean isSupportedState(PanelState state) { | 90 protected boolean isSupportedState(PanelState state) { |
79 return state != PanelState.EXPANDED; | 91 return state != PanelState.EXPANDED; |
80 } | 92 } |
81 } | 93 } |
82 | 94 |
83 @Override | 95 @Before |
84 protected void setUp() throws Exception { | 96 public void setUp() throws Exception { |
85 super.setUp(); | |
86 OverlayPanelManager panelManager = new OverlayPanelManager(); | 97 OverlayPanelManager panelManager = new OverlayPanelManager(); |
87 mExpandPanel = new MockOverlayPanel(getInstrumentation().getTargetContex
t(), panelManager); | 98 mExpandPanel = new MockOverlayPanel( |
88 mNoExpandPanel = new NoExpandMockOverlayPanel(getInstrumentation().getTa
rgetContext(), | 99 InstrumentationRegistry.getInstrumentation().getTargetContext(),
panelManager); |
89 panelManager); | 100 mNoExpandPanel = new NoExpandMockOverlayPanel( |
| 101 InstrumentationRegistry.getInstrumentation().getTargetContext(),
panelManager); |
90 } | 102 } |
91 | 103 |
92 // Start OverlayPanelBase test suite. | 104 // Start OverlayPanelBase test suite. |
93 | 105 |
94 /** | 106 /** |
95 * Tests that a panel with the EXPANDED state disabled and a lower movement
threshold will move | 107 * Tests that a panel with the EXPANDED state disabled and a lower movement
threshold will move |
96 * to the correct state based on current position and swipe velocity. | 108 * to the correct state based on current position and swipe velocity. |
97 */ | 109 */ |
| 110 @Test |
98 @SmallTest | 111 @SmallTest |
99 @Feature({"OverlayPanelBase"}) | 112 @Feature({"OverlayPanelBase"}) |
| 113 @UiThreadTest |
100 public void testNonExpandingPanelMovesToCorrectState() { | 114 public void testNonExpandingPanelMovesToCorrectState() { |
101 final float threshold = mNoExpandPanel.getThresholdToNextState(); | 115 final float threshold = mNoExpandPanel.getThresholdToNextState(); |
102 final float height = MOCK_MAXIMIZED_HEIGHT - MOCK_PEEKED_HEIGHT; | 116 final float height = MOCK_MAXIMIZED_HEIGHT - MOCK_PEEKED_HEIGHT; |
103 final float peekToMaxBound = threshold * height + MOCK_PEEKED_HEIGHT; | 117 final float peekToMaxBound = threshold * height + MOCK_PEEKED_HEIGHT; |
104 final float maxToPeekBound = (1.0f - threshold) * height + MOCK_PEEKED_H
EIGHT; | 118 final float maxToPeekBound = (1.0f - threshold) * height + MOCK_PEEKED_H
EIGHT; |
105 | 119 |
106 // Between PEEKING and MAXIMIZED past the threshold in the up direction. | 120 // Between PEEKING and MAXIMIZED past the threshold in the up direction. |
107 PanelState nextState = mNoExpandPanel.findNearestPanelStateFromHeight( | 121 PanelState nextState = mNoExpandPanel.findNearestPanelStateFromHeight( |
108 peekToMaxBound + 1, UPWARD_VELOCITY); | 122 peekToMaxBound + 1, UPWARD_VELOCITY); |
109 assertTrue(nextState == PanelState.MAXIMIZED); | 123 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
110 | 124 |
111 // Between PEEKING and MAXIMIZED before the threshold in the up directio
n. | 125 // Between PEEKING and MAXIMIZED before the threshold in the up directio
n. |
112 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( | 126 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( |
113 peekToMaxBound - 1, UPWARD_VELOCITY); | 127 peekToMaxBound - 1, UPWARD_VELOCITY); |
114 assertTrue(nextState == PanelState.PEEKED); | 128 Assert.assertTrue(nextState == PanelState.PEEKED); |
115 | 129 |
116 // Between PEEKING and MAXIMIZED before the threshold in the down direct
ion. | 130 // Between PEEKING and MAXIMIZED before the threshold in the down direct
ion. |
117 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( | 131 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( |
118 maxToPeekBound + 1, DOWNWARD_VELOCITY); | 132 maxToPeekBound + 1, DOWNWARD_VELOCITY); |
119 assertTrue(nextState == PanelState.MAXIMIZED); | 133 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
120 | 134 |
121 // Between PEEKING and MAXIMIZED past the threshold in the down directio
n. | 135 // Between PEEKING and MAXIMIZED past the threshold in the down directio
n. |
122 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( | 136 nextState = mNoExpandPanel.findNearestPanelStateFromHeight( |
123 maxToPeekBound - 1, DOWNWARD_VELOCITY); | 137 maxToPeekBound - 1, DOWNWARD_VELOCITY); |
124 assertTrue(nextState == PanelState.PEEKED); | 138 Assert.assertTrue(nextState == PanelState.PEEKED); |
125 } | 139 } |
126 | 140 |
127 /** | 141 /** |
128 * Tests that a panel will move to the correct state based on current positi
on and swipe | 142 * Tests that a panel will move to the correct state based on current positi
on and swipe |
129 * velocity. | 143 * velocity. |
130 */ | 144 */ |
| 145 @Test |
131 @SmallTest | 146 @SmallTest |
132 @Feature({"OverlayPanelBase"}) | 147 @Feature({"OverlayPanelBase"}) |
| 148 @UiThreadTest |
133 public void testExpandingPanelMovesToCorrectState() { | 149 public void testExpandingPanelMovesToCorrectState() { |
134 final float threshold = mExpandPanel.getThresholdToNextState(); | 150 final float threshold = mExpandPanel.getThresholdToNextState(); |
135 final float peekToExpHeight = MOCK_EXPANDED_HEIGHT - MOCK_PEEKED_HEIGHT; | 151 final float peekToExpHeight = MOCK_EXPANDED_HEIGHT - MOCK_PEEKED_HEIGHT; |
136 final float expToMaxHeight = MOCK_MAXIMIZED_HEIGHT - MOCK_EXPANDED_HEIGH
T; | 152 final float expToMaxHeight = MOCK_MAXIMIZED_HEIGHT - MOCK_EXPANDED_HEIGH
T; |
137 | 153 |
138 // The boundry for moving to the next state will be different depending
on the direction | 154 // The boundry for moving to the next state will be different depending
on the direction |
139 // of the swipe and the threshold. In the default case, the threshold is
0.5, meaning the | 155 // of the swipe and the threshold. In the default case, the threshold is
0.5, meaning the |
140 // the panel must be half way to the next state in order to animate to i
t. In other cases | 156 // the panel must be half way to the next state in order to animate to i
t. In other cases |
141 // where the threshold is 0.3, for example, the boundry will be closer t
o the top when | 157 // where the threshold is 0.3, for example, the boundry will be closer t
o the top when |
142 // swiping down and closer to the bottom when swiping up. Ultimately thi
s means it will | 158 // swiping down and closer to the bottom when swiping up. Ultimately thi
s means it will |
143 // take less effort to swipe to a different state. | 159 // take less effort to swipe to a different state. |
144 // NOTE(mdjones): Consider making these constants to exclude computation
from these tests. | 160 // NOTE(mdjones): Consider making these constants to exclude computation
from these tests. |
145 final float peekToExpBound = threshold * peekToExpHeight + MOCK_PEEKED_H
EIGHT; | 161 final float peekToExpBound = threshold * peekToExpHeight + MOCK_PEEKED_H
EIGHT; |
146 final float expToPeekBound = (1.0f - threshold) * peekToExpHeight + MOCK
_PEEKED_HEIGHT; | 162 final float expToPeekBound = (1.0f - threshold) * peekToExpHeight + MOCK
_PEEKED_HEIGHT; |
147 final float expToMaxBound = threshold * expToMaxHeight + MOCK_EXPANDED_H
EIGHT; | 163 final float expToMaxBound = threshold * expToMaxHeight + MOCK_EXPANDED_H
EIGHT; |
148 final float maxToExpBound = (1.0f - threshold) * expToMaxHeight + MOCK_E
XPANDED_HEIGHT; | 164 final float maxToExpBound = (1.0f - threshold) * expToMaxHeight + MOCK_E
XPANDED_HEIGHT; |
149 | 165 |
150 // Between PEEKING and EXPANDED past the threshold in the up direction. | 166 // Between PEEKING and EXPANDED past the threshold in the up direction. |
151 PanelState nextState = mExpandPanel.findNearestPanelStateFromHeight( | 167 PanelState nextState = mExpandPanel.findNearestPanelStateFromHeight( |
152 peekToExpBound + 1, UPWARD_VELOCITY); | 168 peekToExpBound + 1, UPWARD_VELOCITY); |
153 assertTrue(nextState == PanelState.EXPANDED); | 169 Assert.assertTrue(nextState == PanelState.EXPANDED); |
154 | 170 |
155 // Between PEEKING and EXPANDED before the threshold in the up direction
. | 171 // Between PEEKING and EXPANDED before the threshold in the up direction
. |
156 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 172 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
157 peekToExpBound - 1, UPWARD_VELOCITY); | 173 peekToExpBound - 1, UPWARD_VELOCITY); |
158 assertTrue(nextState == PanelState.PEEKED); | 174 Assert.assertTrue(nextState == PanelState.PEEKED); |
159 | 175 |
160 // Between PEEKING and EXPANDED before the threshold in the down directi
on. | 176 // Between PEEKING and EXPANDED before the threshold in the down directi
on. |
161 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 177 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
162 expToPeekBound + 1, DOWNWARD_VELOCITY); | 178 expToPeekBound + 1, DOWNWARD_VELOCITY); |
163 assertTrue(nextState == PanelState.EXPANDED); | 179 Assert.assertTrue(nextState == PanelState.EXPANDED); |
164 | 180 |
165 // Between PEEKING and EXPANDED past the threshold in the down direction
. | 181 // Between PEEKING and EXPANDED past the threshold in the down direction
. |
166 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 182 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
167 expToPeekBound - 1, DOWNWARD_VELOCITY); | 183 expToPeekBound - 1, DOWNWARD_VELOCITY); |
168 assertTrue(nextState == PanelState.PEEKED); | 184 Assert.assertTrue(nextState == PanelState.PEEKED); |
169 | 185 |
170 // Between EXPANDED and MAXIMIZED past the threshold in the up direction
. | 186 // Between EXPANDED and MAXIMIZED past the threshold in the up direction
. |
171 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 187 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
172 expToMaxBound + 1, UPWARD_VELOCITY); | 188 expToMaxBound + 1, UPWARD_VELOCITY); |
173 assertTrue(nextState == PanelState.MAXIMIZED); | 189 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
174 | 190 |
175 // Between EXPANDED and MAXIMIZED before the threshold in the up directi
on. | 191 // Between EXPANDED and MAXIMIZED before the threshold in the up directi
on. |
176 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 192 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
177 expToMaxBound - 1, UPWARD_VELOCITY); | 193 expToMaxBound - 1, UPWARD_VELOCITY); |
178 assertTrue(nextState == PanelState.EXPANDED); | 194 Assert.assertTrue(nextState == PanelState.EXPANDED); |
179 | 195 |
180 // Between EXPANDED and MAXIMIZED past the threshold in the down directi
on. | 196 // Between EXPANDED and MAXIMIZED past the threshold in the down directi
on. |
181 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 197 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
182 maxToExpBound - 1, DOWNWARD_VELOCITY); | 198 maxToExpBound - 1, DOWNWARD_VELOCITY); |
183 assertTrue(nextState == PanelState.EXPANDED); | 199 Assert.assertTrue(nextState == PanelState.EXPANDED); |
184 | 200 |
185 // Between EXPANDED and MAXIMIZED before the threshold in the down direc
tion. | 201 // Between EXPANDED and MAXIMIZED before the threshold in the down direc
tion. |
186 nextState = mExpandPanel.findNearestPanelStateFromHeight( | 202 nextState = mExpandPanel.findNearestPanelStateFromHeight( |
187 maxToExpBound + 1, DOWNWARD_VELOCITY); | 203 maxToExpBound + 1, DOWNWARD_VELOCITY); |
188 assertTrue(nextState == PanelState.MAXIMIZED); | 204 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
189 } | 205 } |
190 | 206 |
191 /** | 207 /** |
192 * Tests that a panel will be closed if the desired height is negative. | 208 * Tests that a panel will be closed if the desired height is negative. |
193 */ | 209 */ |
| 210 @Test |
194 @SmallTest | 211 @SmallTest |
195 @Feature({"OverlayPanelBase"}) | 212 @Feature({"OverlayPanelBase"}) |
| 213 @UiThreadTest |
196 public void testNegativeHeightClosesPanel() { | 214 public void testNegativeHeightClosesPanel() { |
197 final float belowPeek = MOCK_PEEKED_HEIGHT - 1000; | 215 final float belowPeek = MOCK_PEEKED_HEIGHT - 1000; |
198 | 216 |
199 PanelState nextState = | 217 PanelState nextState = |
200 mExpandPanel.findNearestPanelStateFromHeight(belowPeek, DOWNWARD
_VELOCITY); | 218 mExpandPanel.findNearestPanelStateFromHeight(belowPeek, DOWNWARD
_VELOCITY); |
201 assertTrue(nextState == PanelState.CLOSED); | 219 Assert.assertTrue(nextState == PanelState.CLOSED); |
202 | 220 |
203 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(belowPeek, DO
WNWARD_VELOCITY); | 221 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(belowPeek, DO
WNWARD_VELOCITY); |
204 assertTrue(nextState == PanelState.CLOSED); | 222 Assert.assertTrue(nextState == PanelState.CLOSED); |
205 | 223 |
206 // Make sure nothing bad happens if velocity is upward (this should neve
r happen). | 224 // Make sure nothing bad happens if velocity is upward (this should neve
r happen). |
207 nextState = mExpandPanel.findNearestPanelStateFromHeight(belowPeek, UPWA
RD_VELOCITY); | 225 nextState = mExpandPanel.findNearestPanelStateFromHeight(belowPeek, UPWA
RD_VELOCITY); |
208 assertTrue(nextState == PanelState.CLOSED); | 226 Assert.assertTrue(nextState == PanelState.CLOSED); |
209 | 227 |
210 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(belowPeek, UP
WARD_VELOCITY); | 228 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(belowPeek, UP
WARD_VELOCITY); |
211 assertTrue(nextState == PanelState.CLOSED); | 229 Assert.assertTrue(nextState == PanelState.CLOSED); |
212 } | 230 } |
213 | 231 |
214 /** | 232 /** |
215 * Tests that a panel is only maximized when desired height is far above the
max. | 233 * Tests that a panel is only maximized when desired height is far above the
max. |
216 */ | 234 */ |
| 235 @Test |
217 @SmallTest | 236 @SmallTest |
218 @Feature({"OverlayPanelBase"}) | 237 @Feature({"OverlayPanelBase"}) |
| 238 @UiThreadTest |
219 public void testLargeDesiredHeightIsMaximized() { | 239 public void testLargeDesiredHeightIsMaximized() { |
220 final float aboveMax = MOCK_MAXIMIZED_HEIGHT + 1000; | 240 final float aboveMax = MOCK_MAXIMIZED_HEIGHT + 1000; |
221 | 241 |
222 PanelState nextState = | 242 PanelState nextState = |
223 mExpandPanel.findNearestPanelStateFromHeight(aboveMax, UPWARD_VE
LOCITY); | 243 mExpandPanel.findNearestPanelStateFromHeight(aboveMax, UPWARD_VE
LOCITY); |
224 assertTrue(nextState == PanelState.MAXIMIZED); | 244 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
225 | 245 |
226 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(aboveMax, UPW
ARD_VELOCITY); | 246 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(aboveMax, UPW
ARD_VELOCITY); |
227 assertTrue(nextState == PanelState.MAXIMIZED); | 247 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
228 | 248 |
229 // Make sure nothing bad happens if velocity is downward (this should ne
ver happen). | 249 // Make sure nothing bad happens if velocity is downward (this should ne
ver happen). |
230 nextState = mExpandPanel.findNearestPanelStateFromHeight(aboveMax, DOWNW
ARD_VELOCITY); | 250 nextState = mExpandPanel.findNearestPanelStateFromHeight(aboveMax, DOWNW
ARD_VELOCITY); |
231 assertTrue(nextState == PanelState.MAXIMIZED); | 251 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
232 | 252 |
233 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(aboveMax, DOW
NWARD_VELOCITY); | 253 nextState = mNoExpandPanel.findNearestPanelStateFromHeight(aboveMax, DOW
NWARD_VELOCITY); |
234 assertTrue(nextState == PanelState.MAXIMIZED); | 254 Assert.assertTrue(nextState == PanelState.MAXIMIZED); |
235 } | 255 } |
236 } | 256 } |
OLD | NEW |