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
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.os.Build;
8 import android.test.FlakyTest;
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.base.test.util.UrlUtils;
12 import org.chromium.content.browser.test.util.CriteriaHelper;
13 import org.chromium.content.browser.test.util.MockOrientationObserver;
14 import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria;
15 import org.chromium.content.common.ScreenOrientationValues;
16 import org.chromium.content_shell_apk.ContentShellActivity;
17 import org.chromium.content_shell_apk.ContentShellTestBase;
20 * Tests for ScreenOrientationListener and its implementations.
22 public class ScreenOrientationProviderTest extends ContentShellTestBase {
24 // For some reasons build bots are not able to lock to 180 degrees. This
25 // boolean is here to make the false negative go away in that situation.
26 private static final boolean ALLOW_0_FOR_180 = true;
28 private static final String DEFAULT_URL =
29 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
31 private MockOrientationObserver mObserver;
32 private final ScreenOrientationProvider mProvider = ScreenOrientationProvider.create();
34 private boolean checkOrientationForLock(int orientations) {
35 switch (orientations) {
36 case ScreenOrientationValues.PORTRAIT_PRIMARY:
37 return mObserver.mOrientation == 0;
38 case ScreenOrientationValues.PORTRAIT_SECONDARY:
39 return mObserver.mOrientation == 180 ||
40 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
41 case ScreenOrientationValues.LANDSCAPE_PRIMARY:
42 return mObserver.mOrientation == 90;
43 case ScreenOrientationValues.LANDSCAPE_SECONDARY:
44 return mObserver.mOrientation == -90;
45 case ScreenOrientationValues.PORTRAIT:
46 return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
47 case ScreenOrientationValues.LANDSCAPE:
48 return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
49 case ScreenOrientationValues.ANY:
50 // The orientation should not change but might and the value could be anything.
53 return mObserver.mHasChanged == false;
58 * Locks the screen orientation to |orientations| using ScreenOrientationProvider.
60 private void lockOrientation(int orientations) {
61 mProvider.lockOrientation((byte)orientations);
65 * Call |lockOrientation| and wait for an orientation change.
67 private boolean lockOrientationAndWait(int orientations)
68 throws InterruptedException {
69 OrientationChangeObserverCriteria criteria =
70 new OrientationChangeObserverCriteria(mObserver);
72 lockOrientation(orientations);
74 return CriteriaHelper.pollForCriteria(criteria);
78 * Unlock the screen orientation using |ScreenOrientationProvider|.
80 private void unlockOrientation() {
81 mProvider.unlockOrientation();
85 public void setUp() throws Exception {
88 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
89 waitForActiveShellToBeDoneLoading();
91 mObserver = new MockOrientationObserver();
92 ScreenOrientationListener.getInstance().addObserver(
93 mObserver, getInstrumentation().getTargetContext());
95 // Make sure mObserver is updated before we start the tests.
96 OrientationChangeObserverCriteria criteria =
97 new OrientationChangeObserverCriteria(mObserver);
98 CriteriaHelper.pollForCriteria(criteria);
102 public void tearDown() throws Exception {
112 @Feature({"ScreenOrientation"})
113 public void testBasicValues() throws Exception {
114 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
115 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
117 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
118 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
120 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
121 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
123 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
124 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
130 @Feature({"ScreenOrientation"})
131 public void testPortrait() throws Exception {
132 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
133 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
135 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
136 ScreenOrientationValues.PORTRAIT_SECONDARY);
137 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
138 ScreenOrientationValues.PORTRAIT_SECONDARY));
140 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
141 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
143 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
144 ScreenOrientationValues.PORTRAIT_SECONDARY);
145 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
146 ScreenOrientationValues.PORTRAIT_SECONDARY));
152 @Feature({"ScreenOrientation"})
153 public void testLandscape() throws Exception {
154 int initialOrientation = mObserver.mOrientation;
156 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
157 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
158 // If we were in LANDSCAPE_SECONDARY (90 degrees), old SDK will not
159 // be able to catch this change correctly. However, we still want to
160 // wait to not break the rest of the test.
161 boolean result = checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY);
162 if (initialOrientation != -90)
165 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
168 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
169 ScreenOrientationValues.LANDSCAPE_SECONDARY);
170 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
171 ScreenOrientationValues.LANDSCAPE_SECONDARY));
173 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
174 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
175 // Exactly the opposite situation as above.
176 boolean result = checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY);
177 if (initialOrientation == -90)
180 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
183 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
184 ScreenOrientationValues.LANDSCAPE_SECONDARY);
185 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
186 ScreenOrientationValues.LANDSCAPE_SECONDARY));
189 // There is no point in testing the case where we try to lock to
190 // PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY
191 // because with the device being likely flat during the test, locking to that
194 // We can't test unlock because the device is likely flat during the test
195 // and in that situation unlocking is a no-op.