1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from marionette_driver import errors
6from mozrunner.devices.emulator_screen import EmulatorScreen
7
8from marionette_harness import MarionetteTestCase, skip_if_desktop, skip_if_mobile
9
10
11default_orientation = "portrait-primary"
12unknown_orientation = "Unknown screen orientation: {}"
13
14
15class TestScreenOrientation(MarionetteTestCase):
16    def setUp(self):
17        MarionetteTestCase.setUp(self)
18        self.is_mobile = self.marionette.session_capabilities.get("rotatable", False)
19
20    def tearDown(self):
21        if self.is_mobile:
22            self.marionette.set_orientation(default_orientation)
23            self.assertEqual(self.marionette.orientation, default_orientation, "invalid state")
24        MarionetteTestCase.tearDown(self)
25
26    @skip_if_desktop("Not supported in Firefox")
27    def test_set_orientation_to_portrait_primary(self):
28        self.marionette.set_orientation("portrait-primary")
29        new_orientation = self.marionette.orientation
30        self.assertEqual(new_orientation, "portrait-primary")
31
32    @skip_if_desktop("Not supported in Firefox")
33    def test_set_orientation_to_landscape_primary(self):
34        self.marionette.set_orientation("landscape-primary")
35        new_orientation = self.marionette.orientation
36        self.assertEqual(new_orientation, "landscape-primary")
37
38    @skip_if_desktop("Not supported in Firefox")
39    def test_set_orientation_to_portrait_secondary(self):
40        self.marionette.set_orientation("portrait-secondary")
41        new_orientation = self.marionette.orientation
42        self.assertEqual(new_orientation, "portrait-secondary")
43
44    @skip_if_desktop("Not supported in Firefox")
45    def test_set_orientation_to_landscape_secondary(self):
46        self.marionette.set_orientation("landscape-secondary")
47        new_orientation = self.marionette.orientation
48        self.assertEqual(new_orientation, "landscape-secondary")
49
50    @skip_if_desktop("Not supported in Firefox")
51    def test_set_orientation_to_shorthand_portrait(self):
52        # Set orientation to something other than portrait-primary first, since the default is
53        # portrait-primary.
54        self.marionette.set_orientation("landscape-primary")
55        self.assertEqual(self.marionette.orientation, "landscape-primary", "invalid state")
56
57        self.marionette.set_orientation("portrait")
58        new_orientation = self.marionette.orientation
59        self.assertEqual(new_orientation, "portrait-primary")
60
61    @skip_if_desktop("Not supported in Firefox")
62    def test_set_orientation_to_shorthand_landscape(self):
63        self.marionette.set_orientation("landscape")
64        new_orientation = self.marionette.orientation
65        self.assertEqual(new_orientation, "landscape-primary")
66
67    @skip_if_desktop("Not supported in Firefox")
68    def test_set_orientation_with_mixed_casing(self):
69        self.marionette.set_orientation("lAnDsCaPe")
70        new_orientation = self.marionette.orientation
71        self.assertEqual(new_orientation, "landscape-primary")
72
73    @skip_if_desktop("Not supported in Firefox")
74    def test_set_invalid_orientation(self):
75        with self.assertRaisesRegexp(errors.MarionetteException, unknown_orientation.format("cheese")):
76            self.marionette.set_orientation("cheese")
77
78    @skip_if_desktop("Not supported in Firefox")
79    def test_set_null_orientation(self):
80        with self.assertRaisesRegexp(errors.MarionetteException, unknown_orientation.format("null")):
81            self.marionette.set_orientation(None)
82
83    @skip_if_mobile("Specific test for Firefox")
84    def test_unsupported_operation_on_desktop(self):
85        with self.assertRaises(errors.UnsupportedOperationException):
86            self.marionette.set_orientation("landscape-primary")
87