| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.pm.ActivityInfo; | 7 import android.content.pm.ActivityInfo; |
| 8 import android.content.res.Configuration; | |
| 9 import android.graphics.Canvas; | |
| 10 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
| 11 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 12 import android.view.KeyEvent; | 10 import android.view.Surface; |
| 13 import android.view.MotionEvent; | |
| 14 import android.view.View; | |
| 15 | 11 |
| 16 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
| 17 import org.chromium.base.test.util.UrlUtils; | 13 import org.chromium.base.test.util.UrlUtils; |
| 18 import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate; | |
| 19 import org.chromium.content.browser.test.util.Criteria; | 14 import org.chromium.content.browser.test.util.Criteria; |
| 20 import org.chromium.content.browser.test.util.CriteriaHelper; | 15 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 21 import org.chromium.content.browser.test.util.JavaScriptUtils; | 16 import org.chromium.content.browser.test.util.JavaScriptUtils; |
| 22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 17 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 23 import org.chromium.content_shell_apk.ContentShellActivity; | 18 import org.chromium.content_shell_apk.ContentShellActivity; |
| 24 import org.chromium.content_shell_apk.ContentShellTestBase; | 19 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 25 | 20 |
| 26 import java.util.concurrent.TimeoutException; | 21 import java.util.concurrent.TimeoutException; |
| 27 | 22 |
| 28 /** | 23 /** |
| 29 * Integration tests for Screen Orientation API (and legacy API). | 24 * Integration tests for Screen Orientation API (and legacy API). |
| 30 */ | 25 */ |
| 31 public class ScreenOrientationTest extends ContentShellTestBase { | 26 public class ScreenOrientationTest extends ContentShellTestBase { |
| 32 | 27 |
| 33 private static final String DEFAULT_URL = | 28 private static final String DEFAULT_URL = |
| 34 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>"); | 29 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>"); |
| 35 | 30 |
| 36 /** | 31 /** |
| 37 * Class that is used to get notified when the activity configuration | 32 * Whether we should allow 0 degrees when expecting 180 degrees. |
| 38 * changes. | 33 * Try server bots, for some yet unknown reasons, rotate to 0 degrees angle |
| 34 * when they should rotate to 180 degrees. See http://crbug.com/344461 |
| 39 */ | 35 */ |
| 40 private static class ConfigurationChangeAccessDelegate | 36 private static final boolean ALLOW_0_FOR_180 = true; |
| 41 implements InternalAccessDelegate { | |
| 42 | |
| 43 private interface ConfigurationChangedListener { | |
| 44 public void onConfigurationChanged(); | |
| 45 } | |
| 46 | |
| 47 private ConfigurationChangedListener mListener = null; | |
| 48 | |
| 49 private void setListener(ConfigurationChangedListener listener) { | |
| 50 mListener = listener; | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public boolean drawChild(Canvas canvas, View child, long drawingTime) { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 @Override | |
| 59 public boolean super_onKeyUp(int keyCode, KeyEvent event) { | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 @Override | |
| 64 public boolean super_dispatchKeyEventPreIme(KeyEvent event) { | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 @Override | |
| 69 public boolean super_dispatchKeyEvent(KeyEvent event) { | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 @Override | |
| 74 public boolean super_onGenericMotionEvent(MotionEvent event) { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 @Override | |
| 79 public void super_onConfigurationChanged(Configuration newConfig) { | |
| 80 if (mListener != null) | |
| 81 mListener.onConfigurationChanged(); | |
| 82 } | |
| 83 | |
| 84 @Override | |
| 85 public void onScrollChanged(int lPix, int tPix, int oldlPix, int oldtPix
) { | |
| 86 } | |
| 87 | |
| 88 @Override | |
| 89 public boolean awakenScrollBars() { | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 @Override | |
| 94 public boolean super_awakenScrollBars(int startDelay, boolean invalidate
) { | |
| 95 return false; | |
| 96 } | |
| 97 } | |
| 98 | 37 |
| 99 /** | 38 /** |
| 100 * Criteria used to know when an orientation change happens. | 39 * Criteria used to know when an orientation change happens. |
| 101 */ | 40 */ |
| 102 private static class OrientationChangeListenerCriteria implements Criteria { | 41 private class OrientationChangeListenerCriteria implements Criteria { |
| 103 | 42 |
| 104 private boolean mSatisfied = false; | 43 private final int mTargetOrientation; |
| 105 | 44 |
| 106 private ConfigurationChangeAccessDelegate mAccessDelegate; | 45 private OrientationChangeListenerCriteria(int targetOrientation) { |
| 107 | 46 mTargetOrientation = targetOrientation; |
| 108 private OrientationChangeListenerCriteria( | |
| 109 ConfigurationChangeAccessDelegate accessDelegate) { | |
| 110 mAccessDelegate = accessDelegate; | |
| 111 mAccessDelegate.setListener( | |
| 112 new ConfigurationChangeAccessDelegate.ConfigurationChangedListen
er() { | |
| 113 @Override | |
| 114 public void onConfigurationChanged() { | |
| 115 mSatisfied = true; | |
| 116 } | |
| 117 }); | |
| 118 } | 47 } |
| 119 | 48 |
| 120 @Override | 49 @Override |
| 121 public boolean isSatisfied() { | 50 public boolean isSatisfied() { |
| 122 if (mSatisfied) { | 51 return getOrientationAngle() == mTargetOrientation; |
| 123 mAccessDelegate.setListener(null); | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 return false; | |
| 128 } | 52 } |
| 129 } | 53 } |
| 130 | 54 |
| 131 private ContentView mContentView; | 55 private ContentView mContentView; |
| 132 | 56 |
| 133 private ConfigurationChangeAccessDelegate mConfChangeAccessDelegate; | 57 /** |
| 58 * Returns the current device orientation angle. |
| 59 */ |
| 60 private int getOrientationAngle() { |
| 61 switch (getActivity().getWindowManager().getDefaultDisplay() |
| 62 .getRotation()) { |
| 63 case Surface.ROTATION_90: |
| 64 return 90; |
| 65 case Surface.ROTATION_180: |
| 66 return 180; |
| 67 case Surface.ROTATION_270: |
| 68 return 270; |
| 69 case Surface.ROTATION_0: |
| 70 return 0; |
| 71 default: |
| 72 fail("Should not be there!"); |
| 73 return 0; |
| 74 } |
| 75 } |
| 76 |
| 77 /** |
| 78 * Returns the expected orientation angle for a specific orientation type as |
| 79 * defined in ActivityInfor.screenOrientation. |
| 80 */ |
| 81 private static int orientationTypeToAngle(int orientation) { |
| 82 switch (orientation) { |
| 83 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: |
| 84 return 0; |
| 85 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: |
| 86 return 90; |
| 87 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: |
| 88 return 180; |
| 89 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: |
| 90 return 270; |
| 91 default: |
| 92 fail("Should not be there!"); |
| 93 return 0; |
| 94 } |
| 95 } |
| 134 | 96 |
| 135 /** | 97 /** |
| 136 * Locks the screen orientation to the predefined orientation type. | 98 * Locks the screen orientation to the predefined orientation type. |
| 137 */ | 99 */ |
| 138 private void lockOrientation(int orientation) { | 100 private void lockOrientation(int orientation) { |
| 139 getActivity().setRequestedOrientation(orientation); | 101 getActivity().setRequestedOrientation(orientation); |
| 140 } | 102 } |
| 141 | 103 |
| 142 /** | 104 /** |
| 143 * Unlock the screen orientation. Equivalent to locking to unspecified. | 105 * Unlock the screen orientation. Equivalent to locking to unspecified. |
| 144 */ | 106 */ |
| 145 private void unlockOrientation() { | 107 private void unlockOrientation() { |
| 146 getActivity().setRequestedOrientation( | 108 getActivity().setRequestedOrientation( |
| 147 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); | 109 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 148 } | 110 } |
| 149 | 111 |
| 150 /** | 112 /** |
| 151 * Locks the screen orientation to the predefined orientation type then wait | 113 * Locks the screen orientation to the predefined orientation type then wait |
| 152 * for the orientation change to happen. | 114 * for the orientation change to happen. |
| 153 */ | 115 */ |
| 154 private boolean lockOrientationAndWait(int orientation) | 116 private boolean lockOrientationAndWait(int orientation) |
| 155 throws InterruptedException { | 117 throws InterruptedException { |
| 156 OrientationChangeListenerCriteria listenerCriteria = | 118 OrientationChangeListenerCriteria listenerCriteria = |
| 157 new OrientationChangeListenerCriteria(mConfChangeAccessDelegate); | 119 new OrientationChangeListenerCriteria(orientationTypeToAngle(ori
entation)); |
| 158 | 120 |
| 159 lockOrientation(orientation); | 121 lockOrientation(orientation); |
| 160 | 122 |
| 161 return CriteriaHelper.pollForCriteria(listenerCriteria); | 123 return CriteriaHelper.pollForCriteria(listenerCriteria); |
| 162 } | 124 } |
| 163 | 125 |
| 164 /** | 126 /** |
| 165 * Returns the screen orientation as seen by |window.orientation|. | 127 * Returns the screen orientation as seen by |window.orientation|. |
| 166 */ | 128 */ |
| 167 private int getWindowOrientation() | 129 private int getWindowOrientation() |
| 168 throws InterruptedException, TimeoutException { | 130 throws InterruptedException, TimeoutException { |
| 169 return Integer.parseInt( | 131 return Integer.parseInt( |
| 170 JavaScriptUtils.executeJavaScriptAndWaitForResult( | 132 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 171 mContentView, | 133 mContentView, |
| 172 new TestCallbackHelperContainer(mContentView), | 134 new TestCallbackHelperContainer(mContentView), |
| 173 "window.orientation")); | 135 "window.orientation")); |
| 174 } | 136 } |
| 175 | 137 |
| 176 @Override | 138 @Override |
| 177 public void setUp() throws Exception { | 139 public void setUp() throws Exception { |
| 178 super.setUp(); | 140 super.setUp(); |
| 179 | 141 |
| 180 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL); | 142 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL); |
| 181 waitForActiveShellToBeDoneLoading(); | 143 waitForActiveShellToBeDoneLoading(); |
| 182 mContentView = activity.getActiveContentView(); | 144 mContentView = activity.getActiveContentView(); |
| 183 | |
| 184 mConfChangeAccessDelegate = new ConfigurationChangeAccessDelegate(); | |
| 185 getContentViewCore(). | |
| 186 setContainerViewInternals(mConfChangeAccessDelegate); | |
| 187 } | 145 } |
| 188 | 146 |
| 189 @Override | 147 @Override |
| 190 public void tearDown() throws Exception { | 148 public void tearDown() throws Exception { |
| 191 unlockOrientation(); | 149 unlockOrientation(); |
| 192 super.tearDown(); | 150 super.tearDown(); |
| 193 } | 151 } |
| 194 | 152 |
| 195 @SmallTest | 153 @SmallTest |
| 196 @Feature({"ScreenOrientation", "Main"}) | 154 @Feature({"ScreenOrientation", "Main"}) |
| 197 public void testDefault() throws Throwable { | 155 public void testDefault() throws Throwable { |
| 198 assertEquals(0, getWindowOrientation()); | 156 assertEquals(0, getWindowOrientation()); |
| 199 } | 157 } |
| 200 | 158 |
| 201 @MediumTest | 159 @MediumTest |
| 202 @Feature({"ScreenOrientation", "Main"}) | 160 @Feature({"ScreenOrientation", "Main"}) |
| 203 public void testChanges() throws Throwable { | 161 public void testChanges() throws Throwable { |
| 162 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 163 assertEquals(0, getWindowOrientation()); |
| 164 |
| 204 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | 165 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 205 assertEquals(90, getWindowOrientation()); | 166 assertEquals(90, getWindowOrientation()); |
| 206 | 167 |
| 168 lockOrientationAndWait( |
| 169 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); |
| 170 int orientation = getWindowOrientation(); |
| 171 assertTrue(180 == orientation || (0 == orientation && ALLOW_0_FOR_180)); |
| 172 |
| 173 lockOrientationAndWait( |
| 174 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); |
| 175 assertEquals(-90, getWindowOrientation()); |
| 176 |
| 177 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 178 assertEquals(0, getWindowOrientation()); |
| 179 } |
| 180 |
| 181 @MediumTest |
| 182 @Feature({"ScreenOrientation", "Main"}) |
| 183 public void testFlipChangePortrait() throws Throwable { |
| 207 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | 184 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 208 assertEquals(0, getWindowOrientation()); | 185 assertEquals(0, getWindowOrientation()); |
| 209 | 186 |
| 210 lockOrientationAndWait( | 187 lockOrientationAndWait( |
| 188 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); |
| 189 int orientation = getWindowOrientation(); |
| 190 assertTrue(180 == orientation || (0 == orientation && ALLOW_0_FOR_180)); |
| 191 } |
| 192 |
| 193 @MediumTest |
| 194 @Feature({"ScreenOrientation", "Main"}) |
| 195 public void testFlipChangeLandscape() throws Throwable { |
| 196 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 197 assertEquals(90, getWindowOrientation()); |
| 198 |
| 199 lockOrientationAndWait( |
| 211 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); | 200 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); |
| 212 assertEquals(-90, getWindowOrientation()); | 201 assertEquals(-90, getWindowOrientation()); |
| 213 | |
| 214 lockOrientationAndWait( | |
| 215 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); | |
| 216 int orientation = getWindowOrientation(); | |
| 217 assertTrue(orientation == 0 || orientation == 180); | |
| 218 } | 202 } |
| 219 } | 203 } |
| OLD | NEW |