ScreenOrientationProviderTest.java revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5package org.chromium.content.browser;
6
7import android.test.suitebuilder.annotation.MediumTest;
8import android.test.suitebuilder.annotation.SmallTest;
9
10import org.chromium.base.test.util.Feature;
11import org.chromium.base.test.util.UrlUtils;
12import org.chromium.content.browser.test.util.CriteriaHelper;
13import org.chromium.content.browser.test.util.MockOrientationObserver;
14import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria;
15import org.chromium.content.common.ScreenOrientationValues;
16import org.chromium.content_shell_apk.ContentShellActivity;
17import org.chromium.content_shell_apk.ContentShellTestBase;
18
19/**
20 * Tests for ScreenOrientationListener and its implementations.
21 */
22public class ScreenOrientationProviderTest extends ContentShellTestBase {
23
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;
27
28    private static final String DEFAULT_URL =
29            UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
30
31    private MockOrientationObserver mObserver;
32    private final ScreenOrientationProvider mProvider = ScreenOrientationProvider.create();
33
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_PRIMARY |
46                    ScreenOrientationValues.PORTRAIT_SECONDARY:
47                return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
48            case ScreenOrientationValues.LANDSCAPE_PRIMARY |
49                    ScreenOrientationValues.LANDSCAPE_SECONDARY:
50                return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
51            case ScreenOrientationValues.PORTRAIT_PRIMARY |
52                    ScreenOrientationValues.PORTRAIT_SECONDARY |
53                    ScreenOrientationValues.LANDSCAPE_PRIMARY |
54                    ScreenOrientationValues.LANDSCAPE_SECONDARY:
55                // The orientation should not change but might and the value could be anything.
56                return true;
57            default:
58                return mObserver.mHasChanged == false;
59        }
60    }
61
62    /**
63     * Locks the screen orientation to |orientations| using ScreenOrientationProvider.
64     */
65    private void lockOrientation(int orientations) {
66        mProvider.lockOrientation((byte)orientations);
67    }
68
69    /**
70     * Call |lockOrientation| and wait for an orientation change.
71     */
72    private boolean lockOrientationAndWait(int orientations)
73            throws InterruptedException {
74        OrientationChangeObserverCriteria criteria =
75                new OrientationChangeObserverCriteria(mObserver);
76
77        lockOrientation(orientations);
78
79        return CriteriaHelper.pollForCriteria(criteria);
80    }
81
82    /**
83     * Unlock the screen orientation using |ScreenOrientationProvider|.
84     */
85    private void unlockOrientation() {
86        mProvider.unlockOrientation();
87    }
88
89    @Override
90    public void setUp() throws Exception {
91        super.setUp();
92
93        ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
94        waitForActiveShellToBeDoneLoading();
95
96        mObserver = new MockOrientationObserver();
97        ScreenOrientationListener.getInstance().addObserver(
98                mObserver, getInstrumentation().getTargetContext());
99    }
100
101    @Override
102    public void tearDown() throws Exception {
103        unlockOrientation();
104
105        mObserver = null;
106        super.tearDown();
107    }
108
109    @SmallTest
110    @Feature({"ScreenOrientation"})
111    public void testBasicValues() throws Exception {
112        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
113        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
114
115        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
116        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
117
118        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
119        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
120
121        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
122        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
123    }
124
125    @MediumTest
126    @Feature({"ScreenOrientation"})
127    public void testPortrait() throws Exception {
128        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
129        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
130
131        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
132                ScreenOrientationValues.PORTRAIT_SECONDARY);
133        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
134                ScreenOrientationValues.PORTRAIT_SECONDARY));
135
136        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
137        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
138
139        lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
140                ScreenOrientationValues.PORTRAIT_SECONDARY);
141        assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
142                ScreenOrientationValues.PORTRAIT_SECONDARY));
143    }
144
145    @MediumTest
146    @Feature({"ScreenOrientation"})
147    public void testLandscape() throws Exception {
148        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
149        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
150
151        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
152                ScreenOrientationValues.LANDSCAPE_SECONDARY);
153        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
154                ScreenOrientationValues.LANDSCAPE_SECONDARY));
155
156        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
157        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
158
159        lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
160                ScreenOrientationValues.LANDSCAPE_SECONDARY);
161        assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
162                ScreenOrientationValues.LANDSCAPE_SECONDARY));
163    }
164
165    // There is no point in testing the case where we try to lock to
166    // PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY
167    // because with the device being likely flat during the test, locking to that
168    // will be a no-op.
169
170    // We can't test unlock because the device is likely flat during the test
171    // and in that situation unlocking is a no-op.
172}
173