Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ScreenOrientationProviderTest.java
index c68f301..69355ce 100644 (file)
@@ -4,6 +4,8 @@
 
 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;
@@ -15,6 +17,7 @@ import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria;
 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.
@@ -30,26 +33,70 @@ public class ScreenOrientationProviderTest extends ContentShellTestBase {
 
     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;
         }
     }
 
@@ -90,6 +137,10 @@ public class ScreenOrientationProviderTest extends ContentShellTestBase {
             }
         });
 
+        // 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);