Bug 811185 - Implement Emulator class for screen orientation in Marionette. r=jgriffin
☠☠ backed out by 83d175cb799d ☠ ☠
authorHenrik Skupin <hskupin@mozilla.com>
Wed, 14 Nov 2012 08:46:36 +0100
changeset 113207 992f2748681f9e8bf202fcb47848965d379bc488
parent 113206 5a2f571e1f6d65c0200c049a59fc07c338714d7f
child 113208 951697d19ced85b11e22e26ca5aa5ee0c0bd37c5
push id23859
push useremorley@mozilla.com
push dateWed, 14 Nov 2012 14:36:31 +0000
treeherdermozilla-central@87928cd21b40 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgriffin
bugs811185
milestone19.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 811185 - Implement Emulator class for screen orientation in Marionette. r=jgriffin
testing/marionette/client/marionette/emulator.py
testing/marionette/client/marionette/emulator_screen.py
testing/marionette/client/marionette/tests/unit/test_emulator.py
testing/marionette/client/marionette/tests/unit/test_emulator_screen.py
--- a/testing/marionette/client/marionette/emulator.py
+++ b/testing/marionette/client/marionette/emulator.py
@@ -14,16 +14,17 @@ import shutil
 import socket
 import subprocess
 from telnetlib import Telnet
 import tempfile
 import time
 
 from emulator_battery import EmulatorBattery
 from emulator_geo import EmulatorGeo
+from emulator_screen import EmulatorScreen
 
 
 class LogcatProc(ProcessHandlerMixin):
     """Process handler for logcat which saves all output to a logfile.
     """
 
     def __init__(self, logfile, cmd, **kwargs):
         self.logfile = logfile
@@ -54,16 +55,17 @@ class Emulator(object):
         self._adb_started = False
         self.logcat_dir = logcat_dir
         self.logcat_proc = None
         self.arch = arch
         self.binary = emulatorBinary
         self.res = res
         self.battery = EmulatorBattery(self)
         self.geo = EmulatorGeo(self)
+        self.screen = EmulatorScreen(self)
         self.homedir = homedir
         self.sdcard = sdcard
         self.noWindow = noWindow
         if self.homedir is not None:
             self.homedir = os.path.expanduser(homedir)
         self.dataImg = userdata
         self.copy_userdata = self.dataImg is None
 
@@ -354,16 +356,17 @@ waitFor(
         self._emulator_launched = True
 
         self.dm = devicemanagerADB.DeviceManagerADB(adbPath=self.adb,
                                                     deviceSerial='emulator-%d' % self.port)
 
         # bug 802877
         time.sleep(10)
         self.geo.set_default_location()
+        self.screen.initialize()
 
         if self.logcat_dir:
             self.save_logcat()
 
         # setup DNS fix for networking
         self._run_adb(['shell', 'setprop', 'net.dns1', '10.0.2.3'])
 
     def _save_logcat_proc(self, filename, cmd):
new file mode 100644
--- /dev/null
+++ b/testing/marionette/client/marionette/emulator_screen.py
@@ -0,0 +1,78 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+class EmulatorScreen(object):
+    """Class for screen related emulator commands."""
+
+    SO_PORTRAIT_PRIMARY = 'portrait-primary'
+    SO_PORTRAIT_SECONDARY = 'portrait-secondary'
+    SO_LANDSCAPE_PRIMARY = 'landscape-primary'
+    SO_LANDSCAPE_SECONDARY = 'landscape-secondary'
+
+    def __init__(self, emulator):
+        self.emulator = emulator
+
+    def initialize(self):
+        self.orientation = self.SO_PORTRAIT_PRIMARY
+
+    def _get_raw_orientation(self):
+        """Get the raw value of the current device orientation."""
+        response = self.emulator._run_telnet('sensor get orientation')
+
+        return response[0].split('=')[1].strip()
+
+    def _set_raw_orientation(self, data):
+        """Set the raw value of the specified device orientation."""
+        self.emulator._run_telnet('sensor set orientation %s' % data)
+
+    def get_orientation(self):
+        """Get the current device orientation.
+
+        Returns;
+            orientation -- Orientation of the device. One of:
+                            SO_PORTRAIT_PRIMARY - system buttons at the bottom
+                            SO_PORTRIAT_SECONDARY - system buttons at the top
+                            SO_LANDSCAPE_PRIMARY - system buttons at the right
+                            SO_LANDSCAPE_SECONDARY - system buttons at the left
+
+        """
+        data = self._get_raw_orientation()
+
+        if data == '0:-90:0':
+            orientation = self.SO_PORTRAIT_PRIMARY
+        elif data == '0:90:0':
+            orientation = self.SO_PORTRAIT_SECONDARY
+        elif data == '0:0:90':
+            orientation = self.SO_LANDSCAPE_PRIMARY
+        elif data == '0:0:-90':
+            orientation = self.SO_LANDSCAPE_SECONDARY
+        else:
+            raise ValueError('Unknown orientation sensor value: %s.' % data)
+
+        return orientation
+
+    def set_orientation(self, orientation):
+        """Set the specified device orientation.
+
+        Args
+            orientation -- Orientation of the device. One of:
+                            SO_PORTRAIT_PRIMARY - system buttons at the bottom
+                            SO_PORTRIAT_SECONDARY - system buttons at the top
+                            SO_LANDSCAPE_PRIMARY - system buttons at the right
+                            SO_LANDSCAPE_SECONDARY - system buttons at the left
+        """
+        if orientation == SO_PORTRAIT_PRIMARY:
+            data = '0:-90:0'
+        elif orientation == SO_PORTRAIT_SECONDARY:
+            data = '0:90:0'
+        elif orientation == SO_LANDSCAPE_PRIMARY:
+            data = '0:0:90'
+        elif orientation == SO_LANDSCAPE_SECONDARY:
+            data = '0:0:-90'
+        else:
+            raise ValueError('Invalid orientation: %s' % orientation)
+
+        self._set_raw_orientation(data)
+
+    orientation = property(get_orientation, set_orientation)
--- a/testing/marionette/client/marionette/tests/unit/test_emulator.py
+++ b/testing/marionette/client/marionette/tests/unit/test_emulator.py
@@ -1,8 +1,12 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
 from marionette_test import MarionetteTestCase
 from errors import JavascriptException, MarionetteException
 
 class TestEmulatorContent(MarionetteTestCase):
 
     def test_emulator_cmd(self):
         self.marionette.set_script_timeout(10000)
         expected = ["<build>",
new file mode 100644
--- /dev/null
+++ b/testing/marionette/client/marionette/tests/unit/test_emulator_screen.py
@@ -0,0 +1,34 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from marionette_test import MarionetteTestCase
+
+
+class TestEmulatorScreen(MarionetteTestCase):
+
+    def setUp(self):
+        MarionetteTestCase.setUp(self)
+
+        self.screen = self.marionette.emulator.screen
+        self.screen.initialize()
+
+    def test_emulator_orientation(self):
+        self.assertEqual(self.screen.orientation, self.screen.SO_PORTRAIT_PRIMARY,
+                         'Orientation has been correctly initialized.')
+
+        self.sensor.orientation = self.sensor.SO_PORTRAIT_SECONDARY
+        self.assertEqual(self.sensor.orientation, self.sensor.SO_PORTRAIT_SECONDARY,
+                         'Orientation has been set to portrait-secondary')
+
+        self.sensor.orientation = self.sensor.SO_LANDSCAPE_PRIMARY
+        self.assertEqual(self.sensor.orientation, self.sensor.SO_LANDSCAPE_PRIMARY,
+                         'Orientation has been set to landscape-primary')
+
+        self.sensor.orientation = self.sensor.SO_LANDSCAPE_SECONDARY
+        self.assertEqual(self.sensor.orientation, self.sensor.SO_LANDSCAPE_SECONDARY,
+                         'Orientation has been set to landscape-secondary')
+
+        self.sensor.orientation = self.sensor.SO_PORTRAIT_PRIMARY
+        self.assertEqual(self.sensor.orientation, self.sensor.SO_PORTRAIT_PRIMARY,
+                         'Orientation has been set to portrait-primary')