OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.pm.ActivityInfo; | 7 import android.content.pm.ActivityInfo; |
8 import android.support.test.filters.MediumTest; | 8 import android.support.test.filters.MediumTest; |
9 import android.view.Surface; | 9 import android.view.Surface; |
10 | 10 |
| 11 import org.junit.After; |
| 12 import org.junit.Assert; |
| 13 import org.junit.Before; |
| 14 import org.junit.Rule; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 |
11 import org.chromium.base.ThreadUtils; | 18 import org.chromium.base.ThreadUtils; |
| 19 import org.chromium.base.test.BaseJUnit4ClassRunner; |
12 import org.chromium.base.test.util.CallbackHelper; | 20 import org.chromium.base.test.util.CallbackHelper; |
13 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
14 import org.chromium.content_public.common.ScreenOrientationValues; | 22 import org.chromium.content_public.common.ScreenOrientationValues; |
15 import org.chromium.content_shell_apk.ContentShellTestBase; | 23 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
16 import org.chromium.ui.display.DisplayAndroid; | 24 import org.chromium.ui.display.DisplayAndroid; |
17 import org.chromium.ui.display.DisplayAndroid.DisplayAndroidObserver; | 25 import org.chromium.ui.display.DisplayAndroid.DisplayAndroidObserver; |
18 | 26 |
19 import java.util.concurrent.Callable; | 27 import java.util.concurrent.Callable; |
20 | 28 |
21 | 29 |
22 /** | 30 /** |
23 * Tests for ScreenOrientationListener and its implementations. | 31 * Tests for ScreenOrientationListener and its implementations. |
24 * | 32 * |
25 * rotation: Surface.ROTATION_* | 33 * rotation: Surface.ROTATION_* |
26 * orientation: ActivityInfo.SCREEN_ORIENTATION_* | 34 * orientation: ActivityInfo.SCREEN_ORIENTATION_* |
27 * orientation value: ScreenOrientationValues.* | 35 * orientation value: ScreenOrientationValues.* |
28 */ | 36 */ |
29 public class ScreenOrientationListenerTest extends ContentShellTestBase { | 37 @RunWith(BaseJUnit4ClassRunner.class) |
| 38 public class ScreenOrientationListenerTest { |
| 39 @Rule |
| 40 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 41 |
30 private static class OrientationChangeCallbackHelper | 42 private static class OrientationChangeCallbackHelper |
31 extends CallbackHelper implements DisplayAndroidObserver { | 43 extends CallbackHelper implements DisplayAndroidObserver { |
32 private int mLastOrientation; | 44 private int mLastOrientation; |
33 | 45 |
34 @Override | 46 @Override |
35 public void onRotationChanged(int rotation) { | 47 public void onRotationChanged(int rotation) { |
36 mLastOrientation = rotation; | 48 mLastOrientation = rotation; |
37 notifyCalled(); | 49 notifyCalled(); |
38 } | 50 } |
39 | 51 |
40 @Override | 52 @Override |
41 public void onDIPScaleChanged(float dipScale) {} | 53 public void onDIPScaleChanged(float dipScale) {} |
42 | 54 |
43 public int getLastRotation() { | 55 public int getLastRotation() { |
44 return mLastOrientation; | 56 return mLastOrientation; |
45 } | 57 } |
46 } | 58 } |
47 | 59 |
48 private OrientationChangeCallbackHelper mCallbackHelper; | 60 private OrientationChangeCallbackHelper mCallbackHelper; |
49 private DisplayAndroid mDisplayAndroid; | 61 private DisplayAndroid mDisplayAndroid; |
50 private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIE
D; | 62 private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIE
D; |
51 | 63 |
52 @Override | 64 @Before |
53 public void setUp() throws Exception { | 65 public void setUp() throws Exception { |
54 super.setUp(); | 66 mActivityTestRule.launchContentShellWithUrl("about:blank"); |
55 | |
56 launchContentShellWithUrl("about:blank"); | |
57 mCallbackHelper = new OrientationChangeCallbackHelper(); | 67 mCallbackHelper = new OrientationChangeCallbackHelper(); |
58 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 68 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
59 @Override | 69 @Override |
60 public void run() { | 70 public void run() { |
61 mDisplayAndroid = getContentViewCore().getWindowAndroid().getDis
play(); | 71 mDisplayAndroid = |
| 72 mActivityTestRule.getContentViewCore().getWindowAndroid(
).getDisplay(); |
62 mDisplayAndroid.addObserver(mCallbackHelper); | 73 mDisplayAndroid.addObserver(mCallbackHelper); |
63 DisplayAndroid.startAccurateListening(); | 74 DisplayAndroid.startAccurateListening(); |
64 } | 75 } |
65 }); | 76 }); |
66 | 77 |
67 // Calculate device natural orientation, as mObserver.mOrientation | 78 // Calculate device natural orientation, as mObserver.mOrientation |
68 // is difference between current and natural orientation in degrees. | 79 // is difference between current and natural orientation in degrees. |
69 mNaturalOrientation = getNaturalOrientation(mDisplayAndroid); | 80 mNaturalOrientation = getNaturalOrientation(mDisplayAndroid); |
70 | 81 |
71 // Make sure we start all the tests with the same orientation. | 82 // Make sure we start all the tests with the same orientation. |
72 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | 83 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
73 } | 84 } |
74 | 85 |
75 @Override | 86 @After |
76 public void tearDown() throws Exception { | 87 public void tearDown() throws Exception { |
77 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 88 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
78 @Override | 89 @Override |
79 public void run() { | 90 public void run() { |
80 mDisplayAndroid.removeObserver(mCallbackHelper); | 91 mDisplayAndroid.removeObserver(mCallbackHelper); |
81 mDisplayAndroid = null; | 92 mDisplayAndroid = null; |
82 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENT
ATION_UNSPECIFIED); | 93 mActivityTestRule.getActivity().setRequestedOrientation( |
| 94 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
83 DisplayAndroid.stopAccurateListening(); | 95 DisplayAndroid.stopAccurateListening(); |
84 } | 96 } |
85 }); | 97 }); |
86 | 98 |
87 mCallbackHelper = null; | 99 mCallbackHelper = null; |
88 super.tearDown(); | |
89 } | 100 } |
90 | 101 |
91 private static int getNaturalOrientation(DisplayAndroid display) { | 102 private static int getNaturalOrientation(DisplayAndroid display) { |
92 int rotation = display.getRotation(); | 103 int rotation = display.getRotation(); |
93 if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
{ | 104 if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
{ |
94 if (display.getDisplayHeight() >= display.getDisplayWidth()) { | 105 if (display.getDisplayHeight() >= display.getDisplayWidth()) { |
95 return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; | 106 return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
96 } | 107 } |
97 return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; | 108 return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
98 } else { | 109 } else { |
99 if (display.getDisplayHeight() < display.getDisplayWidth()) { | 110 if (display.getDisplayHeight() < display.getDisplayWidth()) { |
100 return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; | 111 return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
101 } | 112 } |
102 return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; | 113 return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
103 } | 114 } |
104 } | 115 } |
105 | 116 |
106 private int orientationToRotation(int orientation) { | 117 private int orientationToRotation(int orientation) { |
107 if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { | 118 if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { |
108 switch (orientation) { | 119 switch (orientation) { |
109 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: | 120 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: |
110 return Surface.ROTATION_0; | 121 return Surface.ROTATION_0; |
111 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: | 122 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: |
112 return Surface.ROTATION_90; | 123 return Surface.ROTATION_90; |
113 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: | 124 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: |
114 return Surface.ROTATION_180; | 125 return Surface.ROTATION_180; |
115 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: | 126 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: |
116 return Surface.ROTATION_270; | 127 return Surface.ROTATION_270; |
117 default: | 128 default: |
118 fail("Should not be there!"); | 129 Assert.fail("Should not be there!"); |
119 return Surface.ROTATION_0; | 130 return Surface.ROTATION_0; |
120 } | 131 } |
121 } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDS
CAPE | 132 } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDS
CAPE |
122 switch (orientation) { | 133 switch (orientation) { |
123 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: | 134 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: |
124 return Surface.ROTATION_270; | 135 return Surface.ROTATION_270; |
125 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: | 136 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: |
126 return Surface.ROTATION_0; | 137 return Surface.ROTATION_0; |
127 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: | 138 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: |
128 return Surface.ROTATION_90; | 139 return Surface.ROTATION_90; |
129 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: | 140 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: |
130 return Surface.ROTATION_180; | 141 return Surface.ROTATION_180; |
131 default: | 142 default: |
132 fail("Should not be there!"); | 143 Assert.fail("Should not be there!"); |
133 return Surface.ROTATION_0; | 144 return Surface.ROTATION_0; |
134 } | 145 } |
135 } | 146 } |
136 } | 147 } |
137 | 148 |
138 private int getCurrentRotation() { | 149 private int getCurrentRotation() { |
139 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Integer
>() { | 150 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Integer
>() { |
140 @Override | 151 @Override |
141 public Integer call() { | 152 public Integer call() { |
142 return mDisplayAndroid.getRotation(); | 153 return mDisplayAndroid.getRotation(); |
143 } | 154 } |
144 }); | 155 }); |
145 } | 156 } |
146 | 157 |
147 // Returns the rotation observed. | 158 // Returns the rotation observed. |
148 private int lockOrientationAndWait(final int orientation) throws Exception { | 159 private int lockOrientationAndWait(final int orientation) throws Exception { |
149 int expectedRotation = orientationToRotation(orientation); | 160 int expectedRotation = orientationToRotation(orientation); |
150 int currentRotation = getCurrentRotation(); | 161 int currentRotation = getCurrentRotation(); |
151 if (expectedRotation == currentRotation) return expectedRotation; | 162 if (expectedRotation == currentRotation) return expectedRotation; |
152 | 163 |
153 int callCount = mCallbackHelper.getCallCount(); | 164 int callCount = mCallbackHelper.getCallCount(); |
154 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 165 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
155 @Override | 166 @Override |
156 public void run() { | 167 public void run() { |
157 getActivity().setRequestedOrientation(orientation); | 168 mActivityTestRule.getActivity().setRequestedOrientation(orientat
ion); |
158 } | 169 } |
159 }); | 170 }); |
160 mCallbackHelper.waitForCallback(callCount); | 171 mCallbackHelper.waitForCallback(callCount); |
161 return mCallbackHelper.getLastRotation(); | 172 return mCallbackHelper.getLastRotation(); |
162 } | 173 } |
163 | 174 |
| 175 @Test |
164 @MediumTest | 176 @MediumTest |
165 @Feature({"ScreenOrientation"}) | 177 @Feature({"ScreenOrientation"}) |
166 public void testOrientationChanges() throws Exception { | 178 public void testOrientationChanges() throws Exception { |
167 int rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LA
NDSCAPE); | 179 int rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LA
NDSCAPE); |
168 assertEquals(orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_LANDS
CAPE), rotation); | 180 Assert.assertEquals( |
| 181 orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
, rotation); |
169 | 182 |
170 rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRA
IT); | 183 rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRA
IT); |
171 assertEquals(orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_PORTR
AIT), rotation); | 184 Assert.assertEquals( |
| 185 orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT),
rotation); |
172 | 186 |
173 rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERS
E_LANDSCAPE); | 187 rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERS
E_LANDSCAPE); |
174 assertEquals( | 188 Assert.assertEquals( |
175 orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LA
NDSCAPE), rotation); | 189 orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LA
NDSCAPE), rotation); |
176 | 190 |
177 // Note: REVERSE_PORTRAIT does not work when device orientation is locke
d by user (eg from | 191 // Note: REVERSE_PORTRAIT does not work when device orientation is locke
d by user (eg from |
178 // the notification shade). Bots on the commit queue are all locked, so
don't bother testing | 192 // the notification shade). Bots on the commit queue are all locked, so
don't bother testing |
179 // REVERSE_PORTRAIT. | 193 // REVERSE_PORTRAIT. |
180 } | 194 } |
181 | 195 |
182 private int orientationValueToRotation(int orientationValue) { | 196 private int orientationValueToRotation(int orientationValue) { |
183 if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { | 197 if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { |
184 switch (orientationValue) { | 198 switch (orientationValue) { |
185 case ScreenOrientationValues.PORTRAIT_PRIMARY: | 199 case ScreenOrientationValues.PORTRAIT_PRIMARY: |
186 return Surface.ROTATION_0; | 200 return Surface.ROTATION_0; |
187 case ScreenOrientationValues.LANDSCAPE_PRIMARY: | 201 case ScreenOrientationValues.LANDSCAPE_PRIMARY: |
188 return Surface.ROTATION_90; | 202 return Surface.ROTATION_90; |
189 case ScreenOrientationValues.PORTRAIT: | 203 case ScreenOrientationValues.PORTRAIT: |
190 return Surface.ROTATION_0; | 204 return Surface.ROTATION_0; |
191 case ScreenOrientationValues.LANDSCAPE: | 205 case ScreenOrientationValues.LANDSCAPE: |
192 return Surface.ROTATION_90; | 206 return Surface.ROTATION_90; |
193 case ScreenOrientationValues.LANDSCAPE_SECONDARY: | 207 case ScreenOrientationValues.LANDSCAPE_SECONDARY: |
194 return Surface.ROTATION_270; | 208 return Surface.ROTATION_270; |
195 case ScreenOrientationValues.PORTRAIT_SECONDARY: | 209 case ScreenOrientationValues.PORTRAIT_SECONDARY: |
196 return Surface.ROTATION_180; | 210 return Surface.ROTATION_180; |
197 default: | 211 default: |
198 fail("Can't requiest this orientation value " + orientationV
alue); | 212 Assert.fail("Can't requiest this orientation value " + orien
tationValue); |
199 return 0; | 213 return 0; |
200 } | 214 } |
201 } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDS
CAPE | 215 } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDS
CAPE |
202 switch (orientationValue) { | 216 switch (orientationValue) { |
203 case ScreenOrientationValues.PORTRAIT_PRIMARY: | 217 case ScreenOrientationValues.PORTRAIT_PRIMARY: |
204 return Surface.ROTATION_270; | 218 return Surface.ROTATION_270; |
205 case ScreenOrientationValues.LANDSCAPE_PRIMARY: | 219 case ScreenOrientationValues.LANDSCAPE_PRIMARY: |
206 return Surface.ROTATION_0; | 220 return Surface.ROTATION_0; |
207 case ScreenOrientationValues.PORTRAIT: | 221 case ScreenOrientationValues.PORTRAIT: |
208 return Surface.ROTATION_90; | 222 return Surface.ROTATION_90; |
209 case ScreenOrientationValues.LANDSCAPE: | 223 case ScreenOrientationValues.LANDSCAPE: |
210 return Surface.ROTATION_0; | 224 return Surface.ROTATION_0; |
211 case ScreenOrientationValues.LANDSCAPE_SECONDARY: | 225 case ScreenOrientationValues.LANDSCAPE_SECONDARY: |
212 return Surface.ROTATION_180; | 226 return Surface.ROTATION_180; |
213 case ScreenOrientationValues.PORTRAIT_SECONDARY: | 227 case ScreenOrientationValues.PORTRAIT_SECONDARY: |
214 return Surface.ROTATION_90; | 228 return Surface.ROTATION_90; |
215 default: | 229 default: |
216 fail("Can't requiest this orientation value " + orientationV
alue); | 230 Assert.fail("Can't requiest this orientation value " + orien
tationValue); |
217 return 0; | 231 return 0; |
218 } | 232 } |
219 } | 233 } |
220 } | 234 } |
221 | 235 |
222 // Returns the rotation observed. | 236 // Returns the rotation observed. |
223 private int lockOrientationValueAndWait(final int orientationValue) throws E
xception { | 237 private int lockOrientationValueAndWait(final int orientationValue) throws E
xception { |
224 int expectedRotation = orientationValueToRotation(orientationValue); | 238 int expectedRotation = orientationValueToRotation(orientationValue); |
225 int currentRotation = getCurrentRotation(); | 239 int currentRotation = getCurrentRotation(); |
226 if (expectedRotation == currentRotation) return expectedRotation; | 240 if (expectedRotation == currentRotation) return expectedRotation; |
227 | 241 |
228 int callCount = mCallbackHelper.getCallCount(); | 242 int callCount = mCallbackHelper.getCallCount(); |
229 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 243 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
230 @Override | 244 @Override |
231 public void run() { | 245 public void run() { |
232 ScreenOrientationProvider.lockOrientation( | 246 ScreenOrientationProvider.lockOrientation( |
233 getContentViewCore().getWindowAndroid(), (byte) orientat
ionValue); | 247 mActivityTestRule.getContentViewCore().getWindowAndroid(
), |
| 248 (byte) orientationValue); |
234 } | 249 } |
235 }); | 250 }); |
236 mCallbackHelper.waitForCallback(callCount); | 251 mCallbackHelper.waitForCallback(callCount); |
237 return mCallbackHelper.getLastRotation(); | 252 return mCallbackHelper.getLastRotation(); |
238 } | 253 } |
239 | 254 |
| 255 @Test |
240 @MediumTest | 256 @MediumTest |
241 @Feature({"ScreenOrientation"}) | 257 @Feature({"ScreenOrientation"}) |
242 public void testBasicValues() throws Exception { | 258 public void testBasicValues() throws Exception { |
243 int rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDS
CAPE_PRIMARY); | 259 int rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDS
CAPE_PRIMARY); |
244 assertEquals( | 260 Assert.assertEquals( |
245 orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_PRI
MARY), rotation); | 261 orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_PRI
MARY), rotation); |
246 | 262 |
247 rotation = lockOrientationValueAndWait(ScreenOrientationValues.PORTRAIT_
PRIMARY); | 263 rotation = lockOrientationValueAndWait(ScreenOrientationValues.PORTRAIT_
PRIMARY); |
248 assertEquals( | 264 Assert.assertEquals( |
249 orientationValueToRotation(ScreenOrientationValues.PORTRAIT_PRIM
ARY), rotation); | 265 orientationValueToRotation(ScreenOrientationValues.PORTRAIT_PRIM
ARY), rotation); |
250 | 266 |
251 rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDSCAPE
_SECONDARY); | 267 rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDSCAPE
_SECONDARY); |
252 assertEquals( | 268 Assert.assertEquals( |
253 orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_SEC
ONDARY), rotation); | 269 orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_SEC
ONDARY), rotation); |
254 | 270 |
255 // The note in testOrientationChanges about REVERSE_PORTRAIT applies to
PORTRAIT_SECONDARY. | 271 // The note in testOrientationChanges about REVERSE_PORTRAIT applies to
PORTRAIT_SECONDARY. |
256 } | 272 } |
257 } | 273 } |
OLD | NEW |