Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1382)

Unified Diff: content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java

Issue 2361633002: android: Introduce DisplayAndroid (Closed)
Patch Set: test comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java b/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java
index 5afd8cab27931cebb3f333eee7d22e223e746ec2..d82b6b8f5c43544882934c9fd7f29ee1b2aa4bae 100644
--- a/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java
+++ b/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationListenerTest.java
@@ -4,217 +4,247 @@
package org.chromium.content.browser;
-import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.test.suitebuilder.annotation.MediumTest;
+import android.view.Surface;
import org.chromium.base.ThreadUtils;
-import org.chromium.base.test.util.DisabledTest;
-import org.chromium.base.test.util.Feature;
-import org.chromium.base.test.util.RetryOnFailure;
-import org.chromium.base.test.util.UrlUtils;
-import org.chromium.content.browser.test.util.CriteriaHelper;
-import org.chromium.content.browser.test.util.MockOrientationObserver;
-import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria;
-import org.chromium.content_shell_apk.ContentShellActivity;
+import org.chromium.content.browser.test.util.CallbackHelper;
+import org.chromium.content_public.common.ScreenOrientationValues;
import org.chromium.content_shell_apk.ContentShellTestBase;
-import org.chromium.ui.gfx.DeviceDisplayInfo;
+import org.chromium.ui.display.DisplayAndroid;
+import org.chromium.ui.display.DisplayAndroid.DisplayAndroidObserver;
+
+import java.util.concurrent.Callable;
+
/**
* Tests for ScreenOrientationListener and its implementations.
+ *
+ * rotation: Surface.ROTATION_*
+ * orientation: ActivityInfo.SCREEN_ORIENTATION_*
+ * orientation value: ScreenOrientationValues.*
*/
public class ScreenOrientationListenerTest extends ContentShellTestBase {
+ private static class OrientationChangeCallbackHelper
+ extends CallbackHelper implements DisplayAndroidObserver {
+ private int mLastOrientation;
+
+ @Override
+ public void onRotationChanged(int rotation) {
+ mLastOrientation = rotation;
+ notifyCalled();
+ }
- // For some reasons build bots are not able to lock to 180 degrees. This
- // boolean is here to make the false negative go away in that situation.
- private static final boolean ALLOW_0_FOR_180 = true;
+ public int getLastRotation() {
+ return mLastOrientation;
+ }
+ }
- private static final String DEFAULT_URL =
- UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
+ private OrientationChangeCallbackHelper mCallbackHelper;
+ private DisplayAndroid mDisplayAndroid;
+ private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
- private MockOrientationObserver mObserver;
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
- private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
+ launchContentShellWithUrl("about:blank");
+ mCallbackHelper = new OrientationChangeCallbackHelper();
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ mDisplayAndroid = getContentViewCore().getWindowAndroid().getDisplay();
+ mDisplayAndroid.addObserver(mCallbackHelper);
+ DisplayAndroid.startAccurateListening();
+ }
+ });
+
+ // Calculate device natural orientation, as mObserver.mOrientation
+ // is difference between current and natural orientation in degrees.
+ mNaturalOrientation = getNaturalOrientation(mDisplayAndroid);
+
+ // Make sure we start all the tests with the same orientation.
+ lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ mDisplayAndroid.removeObserver(mCallbackHelper);
+ mDisplayAndroid = null;
+ getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
+ DisplayAndroid.stopAccurateListening();
+ }
+ });
+
+ mCallbackHelper = null;
+ super.tearDown();
+ }
- /**
- * Checks does the device orientation match the requested one.
- */
- private boolean checkOrientationForLock(int orientation) {
- int expectedOrientation = orientationTypeToAngle(orientation);
- int currentOrientation = mObserver.mOrientation;
- switch (orientation) {
- case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
- case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
- case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
- case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
- if (expectedOrientation == currentOrientation) {
- return true;
- } else if (ALLOW_0_FOR_180 && expectedOrientation == 180
- && currentOrientation == 0) {
- return true;
- }
- return false;
- default:
- return false;
+ private static int getNaturalOrientation(DisplayAndroid display) {
+ int rotation = display.getRotation();
+ if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
+ if (display.getDisplayHeight() >= display.getDisplayWidth()) {
+ return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ }
+ return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+ } else {
+ if (display.getDisplayHeight() < display.getDisplayWidth()) {
+ return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ }
+ return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}
- /**
- * Returns the expected orientation angle based on the orientation type.
- */
- private int orientationTypeToAngle(int orientation) {
+ private int orientationToRotation(int orientation) {
if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
switch (orientation) {
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
- return 0;
+ return Surface.ROTATION_0;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
- return 90;
+ return Surface.ROTATION_90;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
- return 180;
+ return Surface.ROTATION_180;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
- return -90;
+ return Surface.ROTATION_270;
default:
fail("Should not be there!");
- return 0;
+ return Surface.ROTATION_0;
}
} else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
switch (orientation) {
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
- return -90;
+ return Surface.ROTATION_270;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
- return 0;
+ return Surface.ROTATION_0;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
- return 90;
+ return Surface.ROTATION_90;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
- return 180;
+ return Surface.ROTATION_180;
default:
fail("Should not be there!");
- return 0;
+ return Surface.ROTATION_0;
}
}
}
- /**
- * Retrieves device natural orientation.
- */
- private int getNaturalOrientation(Activity activity) {
- DeviceDisplayInfo displayInfo = DeviceDisplayInfo.create(activity);
- int rotation = displayInfo.getRotationDegrees();
- if (rotation == 0 || rotation == 180) {
- if (displayInfo.getDisplayHeight() >= displayInfo.getDisplayWidth()) {
- return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
- }
- return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
- } else {
- if (displayInfo.getDisplayHeight() < displayInfo.getDisplayWidth()) {
- return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ private int getCurrentRotation() {
+ return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Integer>() {
+ @Override
+ public Integer call() {
+ return mDisplayAndroid.getRotation();
}
- return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
- }
+ });
}
- /**
- * Locks the screen orientation to the predefined orientation type then wait
- * for the orientation change to happen.
- */
- private void lockOrientationAndWait(final int orientation) throws InterruptedException {
- OrientationChangeObserverCriteria criteria =
- new OrientationChangeObserverCriteria(mObserver,
- orientationTypeToAngle(orientation));
+ // Returns the rotation observed.
+ private int lockOrientationAndWait(final int orientation) throws Exception {
+ int expectedRotation = orientationToRotation(orientation);
+ int currentRotation = getCurrentRotation();
+ if (expectedRotation == currentRotation) return expectedRotation;
+
+ int callCount = mCallbackHelper.getCallCount();
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
getActivity().setRequestedOrientation(orientation);
}
});
- getInstrumentation().waitForIdleSync();
-
- try {
- CriteriaHelper.pollInstrumentationThread(criteria);
- } catch (AssertionError e) {
- // This should not be here but the Criteria does not support cases where the orientation
- // is not being changed (i.e. where the Natural orientation matches the one you are
- // locking to). crbug.com/565587
- }
+ mCallbackHelper.waitForCallback(callCount);
+ return mCallbackHelper.getLastRotation();
}
- @Override
- public void setUp() throws Exception {
- super.setUp();
-
- mObserver = new MockOrientationObserver();
+ @MediumTest
+ public void testOrientationChanges() throws Exception {
+ int rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+ assertEquals(orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE), rotation);
- final ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
- waitForActiveShellToBeDoneLoading();
+ rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+ assertEquals(orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT), rotation);
- ThreadUtils.runOnUiThreadBlocking(new Runnable() {
- @Override
- public void run() {
- ScreenOrientationListener.getInstance().addObserver(mObserver, activity);
- ScreenOrientationListener.getInstance().startAccurateListening();
- }
- });
+ rotation = lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
+ assertEquals(
+ orientationToRotation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE), rotation);
- // Calculate device natural orientation, as mObserver.mOrientation
- // is difference between current and natural orientation in degrees.
- mNaturalOrientation = getNaturalOrientation(activity);
+ // Note: REVERSE_PORTRAIT does not work when device orientation is locked by user (eg from
+ // the notification shade). Bots on the commit queue are all locked, so don't bother testing
+ // REVERSE_PORTRAIT.
+ }
- // Make sure we start all the tests with the same orientation.
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+ private int orientationValueToRotation(int orientationValue) {
+ if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
+ switch (orientationValue) {
+ case ScreenOrientationValues.PORTRAIT_PRIMARY:
+ return Surface.ROTATION_0;
+ case ScreenOrientationValues.LANDSCAPE_PRIMARY:
+ return Surface.ROTATION_90;
+ case ScreenOrientationValues.PORTRAIT:
+ return Surface.ROTATION_0;
+ case ScreenOrientationValues.LANDSCAPE:
+ return Surface.ROTATION_90;
+ case ScreenOrientationValues.LANDSCAPE_SECONDARY:
+ return Surface.ROTATION_270;
+ case ScreenOrientationValues.PORTRAIT_SECONDARY:
+ return Surface.ROTATION_180;
+ default:
+ fail("Can't requiest this orientation value " + orientationValue);
+ return 0;
+ }
+ } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+ switch (orientationValue) {
+ case ScreenOrientationValues.PORTRAIT_PRIMARY:
+ return Surface.ROTATION_270;
+ case ScreenOrientationValues.LANDSCAPE_PRIMARY:
+ return Surface.ROTATION_0;
+ case ScreenOrientationValues.PORTRAIT:
+ return Surface.ROTATION_90;
+ case ScreenOrientationValues.LANDSCAPE:
+ return Surface.ROTATION_0;
+ case ScreenOrientationValues.LANDSCAPE_SECONDARY:
+ return Surface.ROTATION_180;
+ case ScreenOrientationValues.PORTRAIT_SECONDARY:
+ return Surface.ROTATION_90;
+ default:
+ fail("Can't requiest this orientation value " + orientationValue);
+ return 0;
+ }
+ }
}
- @Override
- public void tearDown() throws Exception {
+ // Returns the rotation observed.
+ private int lockOrientationValueAndWait(final int orientationValue) throws Exception {
+ int expectedRotation = orientationValueToRotation(orientationValue);
+ int currentRotation = getCurrentRotation();
+ if (expectedRotation == currentRotation) return expectedRotation;
+
+ int callCount = mCallbackHelper.getCallCount();
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
- getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
- ScreenOrientationListener.getInstance().startAccurateListening();
+ ScreenOrientationProvider.lockOrientation((byte) orientationValue);
}
});
-
- mObserver = null;
- super.tearDown();
- }
-
- //@MediumTest
- //@Feature({"ScreenOrientation"})
- //@RetryOnFailure
- // Disabled due to flakiness. See crbug.com/645609.
- @DisabledTest
- public void testVariousOrientationChanges() throws Exception {
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
-
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
-
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
-
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
+ mCallbackHelper.waitForCallback(callCount);
+ return mCallbackHelper.getLastRotation();
}
@MediumTest
- @Feature({"ScreenOrientation"})
- public void testFlipPortrait() throws Exception {
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
+ public void testBasicValues() throws Exception {
+ int rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
+ assertEquals(
+ orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_PRIMARY), rotation);
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
- }
+ rotation = lockOrientationValueAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
+ assertEquals(
+ orientationValueToRotation(ScreenOrientationValues.PORTRAIT_PRIMARY), rotation);
- @DisabledTest(message = "crbug.com/645609")
- @MediumTest
- @Feature({"ScreenOrientation"})
- @RetryOnFailure
- public void testFlipLandscape() throws Exception {
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
-
- lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
- assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
+ rotation = lockOrientationValueAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
+ assertEquals(
+ orientationValueToRotation(ScreenOrientationValues.LANDSCAPE_SECONDARY), rotation);
+
+ // The note in testOrientationChanges about REVERSE_PORTRAIT applies to PORTRAIT_SECONDARY.
}
}

Powered by Google App Engine
This is Rietveld 408576698