package org.chromium.content.browser;
+import android.app.Activity;
+import android.content.pm.ActivityInfo;
import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.base.ThreadUtils;
import org.chromium.content_public.common.ScreenOrientationValues;
import org.chromium.content_shell_apk.ContentShellActivity;
import org.chromium.content_shell_apk.ContentShellTestBase;
+import org.chromium.ui.gfx.DeviceDisplayInfo;
/**
* Tests for ScreenOrientationListener and its implementations.
private MockOrientationObserver mObserver;
+ private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
+
private boolean checkOrientationForLock(int orientations) {
- switch (orientations) {
- case ScreenOrientationValues.PORTRAIT_PRIMARY:
- return mObserver.mOrientation == 0;
- case ScreenOrientationValues.PORTRAIT_SECONDARY:
- return mObserver.mOrientation == 180 ||
- (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
- case ScreenOrientationValues.LANDSCAPE_PRIMARY:
- return mObserver.mOrientation == 90;
- case ScreenOrientationValues.LANDSCAPE_SECONDARY:
- return mObserver.mOrientation == -90;
- case ScreenOrientationValues.PORTRAIT:
- return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
- case ScreenOrientationValues.LANDSCAPE:
- return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
- case ScreenOrientationValues.ANY:
- // The orientation should not change but might and the value could be anything.
- return true;
- default:
- return !mObserver.mHasChanged;
+ if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
+ switch (orientations) {
+ case ScreenOrientationValues.PORTRAIT_PRIMARY:
+ return mObserver.mOrientation == 0;
+ case ScreenOrientationValues.PORTRAIT_SECONDARY:
+ return mObserver.mOrientation == 180 ||
+ (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
+ case ScreenOrientationValues.LANDSCAPE_PRIMARY:
+ return mObserver.mOrientation == 90;
+ case ScreenOrientationValues.LANDSCAPE_SECONDARY:
+ return mObserver.mOrientation == -90;
+ case ScreenOrientationValues.PORTRAIT:
+ return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
+ case ScreenOrientationValues.LANDSCAPE:
+ return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
+ case ScreenOrientationValues.ANY:
+ // The orientation should not change but might and the value could be anything.
+ return true;
+ default:
+ return !mObserver.mHasChanged;
+ }
+ } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+ switch (orientations) {
+ case ScreenOrientationValues.PORTRAIT_PRIMARY:
+ return mObserver.mOrientation == -90;
+ case ScreenOrientationValues.PORTRAIT_SECONDARY:
+ return mObserver.mOrientation == 90;
+ case ScreenOrientationValues.LANDSCAPE_PRIMARY:
+ return mObserver.mOrientation == 0;
+ case ScreenOrientationValues.LANDSCAPE_SECONDARY:
+ return mObserver.mOrientation == 180 ||
+ (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
+ case ScreenOrientationValues.PORTRAIT:
+ return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
+ case ScreenOrientationValues.LANDSCAPE:
+ return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
+ case ScreenOrientationValues.ANY:
+ // The orientation should not change but might and the value could be anything.
+ return true;
+ default:
+ return !mObserver.mHasChanged;
+ }
+ }
+ }
+
+ /**
+ * 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;
+ }
+ return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
}
}
});
+ // Calculate device natural orientation, as mObserver.mOrientation
+ // is difference between current and natural orientation in degrees.
+ mNaturalOrientation = getNaturalOrientation(activity);
+
// Make sure we start all the tests with the same orientation.
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);