ScreenOrientationListenerTest.java revision effb81e5f8246d0db0270817048dc992db66e9fb
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.content.pm.ActivityInfo; 8import android.os.Build; 9import android.test.suitebuilder.annotation.MediumTest; 10import android.test.suitebuilder.annotation.SmallTest; 11 12import org.chromium.base.test.util.Feature; 13import org.chromium.base.test.util.UrlUtils; 14import org.chromium.content.browser.test.util.CriteriaHelper; 15import org.chromium.content.browser.test.util.MockOrientationObserver; 16import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria; 17import org.chromium.content_shell_apk.ContentShellActivity; 18import org.chromium.content_shell_apk.ContentShellTestBase; 19 20/** 21 * Tests for ScreenOrientationListener and its implementations. 22 */ 23public class ScreenOrientationListenerTest extends ContentShellTestBase { 24 25 // For some reasons build bots are not able to lock to 180 degrees. This 26 // boolean is here to make the false negative go away in that situation. 27 private static final boolean ALLOW_0_FOR_180 = true; 28 29 private static final String DEFAULT_URL = 30 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>"); 31 32 private MockOrientationObserver mObserver; 33 34 /** 35 * Returns the expected orientation angle based on the orientation type. 36 */ 37 private static int orientationTypeToAngle(int orientation) { 38 switch (orientation) { 39 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: 40 return 0; 41 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: 42 return 90; 43 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: 44 return 180; 45 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: 46 return 270; 47 default: 48 fail("Should not be there!"); 49 return 0; 50 } 51 } 52 53 /** 54 * Locks the screen orientation to the predefined orientation type. 55 */ 56 private void lockOrientation(int orientation) { 57 getActivity().setRequestedOrientation(orientation); 58 } 59 60 /** 61 * Locks the screen orientation to the predefined orientation type then wait 62 * for the orientation change to happen. 63 */ 64 private boolean lockOrientationAndWait(int orientation) 65 throws InterruptedException { 66 OrientationChangeObserverCriteria criteria = new OrientationChangeObserverCriteria( 67 mObserver, orientationTypeToAngle(orientation)); 68 69 lockOrientation(orientation); 70 71 return CriteriaHelper.pollForCriteria(criteria); 72 } 73 74 /** 75 * Unlock the screen orientation. Equivalent to locking to unspecified. 76 */ 77 private void unlockOrientation() { 78 getActivity().setRequestedOrientation( 79 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 80 } 81 82 @Override 83 public void setUp() throws Exception { 84 super.setUp(); 85 86 mObserver = new MockOrientationObserver(); 87 } 88 89 @Override 90 public void tearDown() throws Exception { 91 unlockOrientation(); 92 93 mObserver = null; 94 super.tearDown(); 95 } 96 97 private void setUpForConfigurationListener() throws InterruptedException { 98 ScreenOrientationListener.getInstance().injectConfigurationListenerBackendForTest(); 99 100 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL); 101 waitForActiveShellToBeDoneLoading(); 102 103 ScreenOrientationListener.getInstance().addObserver( 104 mObserver, getInstrumentation().getTargetContext()); 105 } 106 107 private boolean setUpForDisplayListener() throws InterruptedException { 108 // This can't work for pre JB-MR1 SDKs. 109 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) 110 return false; 111 112 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL); 113 waitForActiveShellToBeDoneLoading(); 114 115 ScreenOrientationListener.getInstance().addObserver( 116 mObserver, getInstrumentation().getTargetContext()); 117 return true; 118 } 119 120 // At least one of these tests flakes 50% on all runs of 121 // contentshell_instrumentation_tests. 122 // crbug.com/356483 123 /* 124 @SmallTest 125 @Feature({"ScreenOrientation"}) 126 public void testConfigurationListenerDefault() throws Exception { 127 setUpForConfigurationListener(); 128 129 assertFalse(mObserver.mHasChanged); 130 assertEquals(-1, mObserver.mOrientation); 131 } 132 133 @SmallTest 134 @Feature({"ScreenOrientation"}) 135 public void testConfigurationListenerAsyncSetup() throws Exception { 136 setUpForConfigurationListener(); 137 138 // We should get a onScreenOrientationChange call asynchronously. 139 CriteriaHelper.pollForCriteria(new OrientationChangeObserverCriteria( 140 mObserver)); 141 142 assertTrue(mObserver.mHasChanged); 143 assertTrue(mObserver.mOrientation != -1); 144 } 145 146 @MediumTest 147 @Feature({"ScreenOrientation"}) 148 public void testConfigurationListenerChanges() throws Exception { 149 setUpForConfigurationListener(); 150 151 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 152 assertEquals(90, mObserver.mOrientation); 153 154 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 155 assertTrue(mObserver.mOrientation == 180 || 156 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0)); 157 158 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 159 assertEquals(-90, mObserver.mOrientation); 160 161 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 162 assertEquals(0, mObserver.mOrientation); 163 } 164 165 @MediumTest 166 @Feature({"ScreenOrientation"}) 167 public void testConfigurationListenerFlipPortrait() throws Exception { 168 setUpForConfigurationListener(); 169 170 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 171 assertEquals(0, mObserver.mOrientation); 172 173 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 174 assertEquals(0, mObserver.mOrientation); 175 } 176 177 @MediumTest 178 @Feature({"ScreenOrientation"}) 179 public void testConfigurationListenerFlipLandscape() throws Exception { 180 setUpForConfigurationListener(); 181 182 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 183 assertEquals(90, mObserver.mOrientation); 184 185 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 186 assertEquals(90, mObserver.mOrientation); 187 } 188 189 @SmallTest 190 @Feature({"ScreenOrientation"}) 191 public void testDisplayListenerDefault() throws Exception { 192 if (!setUpForDisplayListener()) 193 return; 194 195 assertEquals(-1, mObserver.mOrientation); 196 } 197 198 @SmallTest 199 @Feature({"ScreenOrientation"}) 200 public void testDisplayListenerAsyncSetup() throws Exception { 201 if (!setUpForDisplayListener()) 202 return; 203 204 // We should get a onScreenOrientationChange call asynchronously. 205 CriteriaHelper.pollForCriteria(new OrientationChangeObserverCriteria( 206 mObserver)); 207 208 assertTrue(mObserver.mOrientation != -1); 209 } 210 211 @MediumTest 212 @Feature({"ScreenOrientation"}) 213 public void testDisplayListenerChanges() throws Exception { 214 if (!setUpForDisplayListener()) 215 return; 216 217 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 218 assertEquals(90, mObserver.mOrientation); 219 220 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 221 assertTrue(mObserver.mOrientation == 180 || 222 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0)); 223 224 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 225 assertEquals(-90, mObserver.mOrientation); 226 227 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 228 assertEquals(0, mObserver.mOrientation); 229 } 230 231 @SmallTest 232 @Feature({"ScreenOrientation"}) 233 public void testDisplayListenerFlipPortrait() throws Exception { 234 if (!setUpForDisplayListener()) 235 return; 236 237 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 238 assertEquals(0, mObserver.mOrientation); 239 240 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 241 assertTrue(mObserver.mOrientation == 180 || 242 (ALLOW_0_FOR_180 && mObserver.mOrientation == 0)); 243 } 244 245 @SmallTest 246 @Feature({"ScreenOrientation"}) 247 public void testDisplayListenerFlipLandscape() throws Exception { 248 if (!setUpForDisplayListener()) 249 return; 250 251 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 252 assertEquals(90, mObserver.mOrientation); 253 254 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 255 assertEquals(-90, mObserver.mOrientation); 256 } 257 */ 258} 259