Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ScreenOrientationListenerTest.java
index 907df4c..af0b0c2 100644 (file)
@@ -4,6 +4,7 @@
 
 package org.chromium.content.browser;
 
+import android.app.Activity;
 import android.content.pm.ActivityInfo;
 import android.test.suitebuilder.annotation.MediumTest;
 
@@ -15,6 +16,7 @@ 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_shell_apk.ContentShellTestBase;
+import org.chromium.ui.gfx.DeviceDisplayInfo;
 
 /**
  * Tests for ScreenOrientationListener and its implementations.
@@ -30,22 +32,81 @@ public class ScreenOrientationListenerTest extends ContentShellTestBase {
 
     private MockOrientationObserver mObserver;
 
+    private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
+
     /**
-     * Returns the expected orientation angle based on the orientation type.
+     * Checks does the device orientation match the requested one.
      */
-    private static int orientationTypeToAngle(int orientation) {
+    private boolean checkOrientationForLock(int orientation) {
+        int expectedOrientation = orientationTypeToAngle(orientation);
+        int currentOrientation = mObserver.mOrientation;
         switch (orientation) {
             case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
-                return 0;
             case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
-                return 90;
             case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
-                return 180;
             case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
-                return 270;
+                if (expectedOrientation == currentOrientation)
+                    return true;
+                else if (ALLOW_0_FOR_180 && expectedOrientation == 180
+                        && currentOrientation == 0)
+                    return true;
+                return false;
             default:
-                fail("Should not be there!");
-                return 0;
+                return false;
+        }
+    }
+
+    /**
+     * Returns the expected orientation angle based on the orientation type.
+     */
+    private int orientationTypeToAngle(int orientation) {
+        if (mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
+            switch (orientation) {
+                case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
+                    return 0;
+                case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
+                    return 90;
+                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
+                    return 180;
+                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
+                    return -90;
+                default:
+                    fail("Should not be there!");
+                    return 0;
+            }
+        } else { // mNaturalOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+            switch (orientation) {
+                case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
+                    return -90;
+                case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
+                    return 0;
+                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
+                    return 90;
+                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
+                    return 180;
+                default:
+                    fail("Should not be there!");
+                    return 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;
+            }
+            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
         }
     }
 
@@ -85,6 +146,10 @@ public class ScreenOrientationListenerTest 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(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
     }
@@ -107,37 +172,35 @@ public class ScreenOrientationListenerTest extends ContentShellTestBase {
     @Feature({"ScreenOrientation"})
     public void testVariousOrientationChanges() throws Exception {
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-        assertEquals(90, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
 
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
-        assertTrue(mObserver.mOrientation == 180 ||
-                   (ALLOW_0_FOR_180 && mObserver.mOrientation == 0));
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
 
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
-        assertEquals(-90, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
 
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
-        assertEquals(0, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
     }
 
     @MediumTest
     @Feature({"ScreenOrientation"})
     public void testFlipPortrait() throws Exception {
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
-        assertEquals(0, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
 
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
-        assertTrue(mObserver.mOrientation == 180 ||
-                   (ALLOW_0_FOR_180 && mObserver.mOrientation == 0));
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT));
     }
 
     @MediumTest
     @Feature({"ScreenOrientation"})
     public void testFlipLandscape() throws Exception {
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-        assertEquals(90, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE));
 
         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
-        assertEquals(-90, mObserver.mOrientation);
+        assertTrue(checkOrientationForLock(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE));
     }
 }