OLD | NEW |
(Empty) | |
| 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 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.test.suitebuilder.annotation.MediumTest; |
| 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 |
| 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_shell_apk.ContentShellActivity; |
| 16 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 17 |
| 18 /** |
| 19 * Tests for ScreenOrientationListener and its implementations. |
| 20 */ |
| 21 public class ScreenOrientationProviderTest extends ContentShellTestBase { |
| 22 |
| 23 // For some reasons build bots are not able to lock to 180 degrees. This |
| 24 // boolean is here to make the false negative go away in that situation. |
| 25 private static final boolean ALLOW_0_FOR_180 = true; |
| 26 |
| 27 private static final String DEFAULT_URL = |
| 28 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>"); |
| 29 |
| 30 private MockOrientationObserver mObserver; |
| 31 private final ScreenOrientationProvider mProvider = ScreenOrientationProvide
r.create(); |
| 32 |
| 33 // Has to be kept in sync with: |
| 34 // third_party/WebKit/public/platform/WebScreenOrientation.h |
| 35 private static final byte PORTRAIT_PRIMARY = 1 << 0; |
| 36 private static final byte LANDSCAPE_PRIMARY = 1 << 1; |
| 37 private static final byte PORTRAIT_SECONDARY = 1 << 2; |
| 38 private static final byte LANDSCAPE_SECONDARY = 1 << 3; |
| 39 |
| 40 private boolean checkOrientationForLock(int orientations) { |
| 41 switch (orientations) { |
| 42 case PORTRAIT_PRIMARY: |
| 43 return mObserver.mOrientation == 0; |
| 44 case PORTRAIT_SECONDARY: |
| 45 return mObserver.mOrientation == 180 || |
| 46 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0); |
| 47 case LANDSCAPE_PRIMARY: |
| 48 return mObserver.mOrientation == 90; |
| 49 case LANDSCAPE_SECONDARY: |
| 50 return mObserver.mOrientation == -90; |
| 51 case PORTRAIT_PRIMARY | PORTRAIT_SECONDARY: |
| 52 return mObserver.mOrientation == 0 || mObserver.mOrientation ==
180; |
| 53 case LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY: |
| 54 return mObserver.mOrientation == 90 || mObserver.mOrientation ==
-90; |
| 55 case PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LAN
DSCAPE_SECONDARY: |
| 56 // The orientation should not change but might and the value cou
ld be anything. |
| 57 return true; |
| 58 default: |
| 59 return mObserver.mHasChanged == false; |
| 60 } |
| 61 } |
| 62 |
| 63 /** |
| 64 * Locks the screen orientation to |orientations| using ScreenOrientationPro
vider. |
| 65 */ |
| 66 private void lockOrientation(int orientations) { |
| 67 mProvider.lockOrientation((byte)orientations); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Call |lockOrientation| and wait for an orientation change. |
| 72 */ |
| 73 private boolean lockOrientationAndWait(int orientations) |
| 74 throws InterruptedException { |
| 75 OrientationChangeObserverCriteria criteria = |
| 76 new OrientationChangeObserverCriteria(mObserver); |
| 77 |
| 78 lockOrientation(orientations); |
| 79 |
| 80 return CriteriaHelper.pollForCriteria(criteria); |
| 81 } |
| 82 |
| 83 /** |
| 84 * Unlock the screen orientation using |ScreenOrientationProvider|. |
| 85 */ |
| 86 private void unlockOrientation() { |
| 87 mProvider.unlockOrientation(); |
| 88 } |
| 89 |
| 90 @Override |
| 91 public void setUp() throws Exception { |
| 92 super.setUp(); |
| 93 |
| 94 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL); |
| 95 waitForActiveShellToBeDoneLoading(); |
| 96 |
| 97 mObserver = new MockOrientationObserver(); |
| 98 ScreenOrientationListener.getInstance().addObserver( |
| 99 mObserver, getInstrumentation().getTargetContext()); |
| 100 } |
| 101 |
| 102 @Override |
| 103 public void tearDown() throws Exception { |
| 104 unlockOrientation(); |
| 105 |
| 106 mObserver = null; |
| 107 super.tearDown(); |
| 108 } |
| 109 |
| 110 @SmallTest |
| 111 @Feature({"ScreenOrientation"}) |
| 112 public void testBasicValues() throws Exception { |
| 113 lockOrientationAndWait(PORTRAIT_PRIMARY); |
| 114 assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY)); |
| 115 |
| 116 lockOrientationAndWait(LANDSCAPE_PRIMARY); |
| 117 assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY)); |
| 118 |
| 119 lockOrientationAndWait(PORTRAIT_SECONDARY); |
| 120 assertTrue(checkOrientationForLock(PORTRAIT_SECONDARY)); |
| 121 |
| 122 lockOrientationAndWait(LANDSCAPE_SECONDARY); |
| 123 assertTrue(checkOrientationForLock(LANDSCAPE_SECONDARY)); |
| 124 } |
| 125 |
| 126 @MediumTest |
| 127 @Feature({"ScreenOrientation"}) |
| 128 public void testPortrait() throws Exception { |
| 129 lockOrientationAndWait(PORTRAIT_PRIMARY); |
| 130 assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY)); |
| 131 |
| 132 lockOrientationAndWait(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY); |
| 133 assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY
)); |
| 134 |
| 135 lockOrientationAndWait(PORTRAIT_SECONDARY); |
| 136 assertTrue(checkOrientationForLock(PORTRAIT_SECONDARY)); |
| 137 |
| 138 lockOrientationAndWait(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY); |
| 139 assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY
)); |
| 140 } |
| 141 |
| 142 @MediumTest |
| 143 @Feature({"ScreenOrientation"}) |
| 144 public void testLandscape() throws Exception { |
| 145 lockOrientationAndWait(LANDSCAPE_PRIMARY); |
| 146 assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY)); |
| 147 |
| 148 lockOrientationAndWait(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY); |
| 149 assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDA
RY)); |
| 150 |
| 151 lockOrientationAndWait(LANDSCAPE_SECONDARY); |
| 152 assertTrue(checkOrientationForLock(LANDSCAPE_SECONDARY)); |
| 153 |
| 154 lockOrientationAndWait(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY); |
| 155 assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDA
RY)); |
| 156 } |
| 157 |
| 158 // There is no point in testing the case where we try to lock to |
| 159 // PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SEC
ONDARY |
| 160 // because with the device being likely flat during the test, locking to tha
t |
| 161 // will be a no-op. |
| 162 |
| 163 // We can't test unlock because the device is likely flat during the test |
| 164 // and in that situation unlocking is a no-op. |
| 165 } |
OLD | NEW |