From 8ef615a999bda67161b59c4688d99eecd2d72151 Mon Sep 17 00:00:00 2001 From: Henrik Skupin Date: Wed, 14 Nov 2012 08:46:36 +0100 Subject: [PATCH] Bug 811185 - Implement Emulator class for screen orientation in Marionette. r=jgriffin --- .../marionette/client/marionette/emulator.py | 3 + .../client/marionette/emulator_screen.py | 78 +++++++++++++++++++ .../marionette/tests/unit/test_emulator.py | 4 + .../tests/unit/test_emulator_screen.py | 34 ++++++++ 4 files changed, 119 insertions(+) create mode 100644 testing/marionette/client/marionette/emulator_screen.py create mode 100644 testing/marionette/client/marionette/tests/unit/test_emulator_screen.py diff --git a/testing/marionette/client/marionette/emulator.py b/testing/marionette/client/marionette/emulator.py index d3ad70c4cf2..e2f2527c9d2 100644 --- a/testing/marionette/client/marionette/emulator.py +++ b/testing/marionette/client/marionette/emulator.py @@ -19,6 +19,7 @@ import time from emulator_battery import EmulatorBattery from emulator_geo import EmulatorGeo +from emulator_screen import EmulatorScreen class LogcatProc(ProcessHandlerMixin): @@ -59,6 +60,7 @@ class Emulator(object): self.res = res self.battery = EmulatorBattery(self) self.geo = EmulatorGeo(self) + self.screen = EmulatorScreen(self) self.homedir = homedir self.sdcard = sdcard self.noWindow = noWindow @@ -359,6 +361,7 @@ waitFor( # bug 802877 time.sleep(10) self.geo.set_default_location() + self.screen.initialize() if self.logcat_dir: self.save_logcat() diff --git a/testing/marionette/client/marionette/emulator_screen.py b/testing/marionette/client/marionette/emulator_screen.py new file mode 100644 index 00000000000..c93b8eaa3d0 --- /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) diff --git a/testing/marionette/client/marionette/tests/unit/test_emulator.py b/testing/marionette/client/marionette/tests/unit/test_emulator.py index a5ba9a3d5a8..ed0dc6642a2 100644 --- a/testing/marionette/client/marionette/tests/unit/test_emulator.py +++ b/testing/marionette/client/marionette/tests/unit/test_emulator.py @@ -1,3 +1,7 @@ +# 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 diff --git a/testing/marionette/client/marionette/tests/unit/test_emulator_screen.py b/testing/marionette/client/marionette/tests/unit/test_emulator_screen.py new file mode 100644 index 00000000000..b4f1f6c6c0c --- /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')