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.test.suitebuilder.annotation.MediumTest;
9 import org.chromium.base.ThreadUtils;
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_public.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;
33 private boolean checkOrientationForLock(int orientations) {
34 switch (orientations) {
35 case ScreenOrientationValues.PORTRAIT_PRIMARY:
36 return mObserver.mOrientation == 0;
37 case ScreenOrientationValues.PORTRAIT_SECONDARY:
38 return mObserver.mOrientation == 180 ||
39 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
40 case ScreenOrientationValues.LANDSCAPE_PRIMARY:
41 return mObserver.mOrientation == 90;
42 case ScreenOrientationValues.LANDSCAPE_SECONDARY:
43 return mObserver.mOrientation == -90;
44 case ScreenOrientationValues.PORTRAIT:
45 return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
46 case ScreenOrientationValues.LANDSCAPE:
47 return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
48 case ScreenOrientationValues.ANY:
49 // The orientation should not change but might and the value could be anything.
52 return !mObserver.mHasChanged;
57 * Call |lockOrientation| and wait for an orientation change.
59 private boolean lockOrientationAndWait(final int orientations) throws InterruptedException {
60 OrientationChangeObserverCriteria criteria =
61 new OrientationChangeObserverCriteria(mObserver);
63 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
66 ScreenOrientationProvider.lockOrientation((byte) orientations);
69 getInstrumentation().waitForIdleSync();
71 return CriteriaHelper.pollForCriteria(criteria);
75 public void setUp() throws Exception {
78 mObserver = new MockOrientationObserver();
79 OrientationChangeObserverCriteria criteria =
80 new OrientationChangeObserverCriteria(mObserver);
82 final ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
83 waitForActiveShellToBeDoneLoading();
85 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
88 ScreenOrientationListener.getInstance().addObserver(mObserver, activity);
89 ScreenOrientationProvider.startAccurateListening();
93 // Make sure we start all the tests with the same orientation.
94 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
96 // Make sure mObserver is updated before we start the tests.
97 CriteriaHelper.pollForCriteria(criteria);
101 public void tearDown() throws Exception {
102 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
105 ScreenOrientationProvider.unlockOrientation();
106 ScreenOrientationProvider.startAccurateListening();
115 @Feature({"ScreenOrientation"})
116 public void testBasicValues() throws Exception {
117 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
118 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
120 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
121 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
123 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
124 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
126 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
127 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
131 @Feature({"ScreenOrientation"})
132 public void testPortrait() throws Exception {
133 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
134 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
136 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
137 ScreenOrientationValues.PORTRAIT_SECONDARY);
138 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
139 ScreenOrientationValues.PORTRAIT_SECONDARY));
141 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
142 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
144 lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
145 ScreenOrientationValues.PORTRAIT_SECONDARY);
146 assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
147 ScreenOrientationValues.PORTRAIT_SECONDARY));
151 @Feature({"ScreenOrientation"})
152 public void testLandscape() throws Exception {
153 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
154 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
156 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
157 ScreenOrientationValues.LANDSCAPE_SECONDARY);
158 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
159 ScreenOrientationValues.LANDSCAPE_SECONDARY));
161 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
162 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
164 lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
165 ScreenOrientationValues.LANDSCAPE_SECONDARY);
166 assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
167 ScreenOrientationValues.LANDSCAPE_SECONDARY));
170 // There is no point in testing the case where we try to lock to
171 // PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY
172 // because with the device being likely flat during the test, locking to that
175 // We can't test unlock because the device is likely flat during the test
176 // and in that situation unlocking is a no-op.