summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_window_rect.py
blob: 284989cf5bc8d0b47b637151011ba7d35719ce66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# 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_driver.errors import InvalidArgumentException
from marionette_harness import MarionetteTestCase


class TestWindowRect(MarionetteTestCase):
    def setUp(self):
        super(TestWindowRect, self).setUp()

        self.original_rect = self.marionette.window_rect

        self.max = self.marionette.execute_script(
            """
            return {
              width: window.screen.availWidth,
              height: window.screen.availHeight,
            }""",
            sandbox=None,
        )

        # WebDriver spec says a resize cannot result in window being
        # maximised, an error is returned if that is the case; therefore if
        # the window is maximised at the start of this test, returning to
        # the original size via set_window_rect size will result in error;
        # so reset to original size minus 1 pixel width
        start_size = {
            "height": self.original_rect["height"],
            "width": self.original_rect["width"],
        }
        if (
            start_size["width"] == self.max["width"]
            and start_size["height"] == self.max["height"]
        ):
            start_size["width"] -= 10
            start_size["height"] -= 10
        self.marionette.set_window_rect(
            height=start_size["height"], width=start_size["width"]
        )

    def tearDown(self):
        x, y = self.original_rect["x"], self.original_rect["y"]
        height, width = self.original_rect["height"], self.original_rect["width"]

        self.marionette.set_window_rect(x=x, y=y, height=height, width=width)

        is_fullscreen = self.marionette.execute_script(
            "return document.fullscreenElement;", sandbox=None
        )
        if is_fullscreen:
            self.marionette.fullscreen()

        super(TestWindowRect, self).tearDown()

    def test_get_types(self):
        rect = self.marionette.window_rect
        self.assertIn("x", rect)
        self.assertIn("y", rect)
        self.assertIn("height", rect)
        self.assertIn("width", rect)
        self.assertIsInstance(rect["x"], int)
        self.assertIsInstance(rect["y"], int)
        self.assertIsInstance(rect["height"], int)
        self.assertIsInstance(rect["width"], int)

    def test_set_types(self):
        invalid_rects = (
            ["a", "b", "h", "w"],
            [1.2, 3.4, 4.5, 5.6],
            [True, False, True, False],
            [[], [], [], []],
            [{}, {}, {}, {}],
        )
        for x, y, h, w in invalid_rects:
            print("testing invalid type position ({},{})".format(x, y))
            with self.assertRaises(InvalidArgumentException):
                self.marionette.set_window_rect(x=x, y=y, height=h, width=w)

    def test_setting_window_rect_with_nulls_errors(self):
        with self.assertRaises(InvalidArgumentException):
            self.marionette.set_window_rect(height=None, width=None, x=None, y=None)

    def test_set_position(self):
        old_position = self.marionette.window_rect
        wanted_position = {"x": old_position["x"] + 10, "y": old_position["y"] + 10}

        new_position = self.marionette.set_window_rect(
            x=wanted_position["x"], y=wanted_position["y"]
        )
        expected_position = self.marionette.window_rect

        self.assertEqual(new_position["x"], wanted_position["x"])
        self.assertEqual(new_position["y"], wanted_position["y"])
        self.assertEqual(new_position["x"], expected_position["x"])
        self.assertEqual(new_position["y"], expected_position["y"])

    def test_set_size(self):
        old_size = self.marionette.window_rect
        wanted_size = {
            "height": old_size["height"] - 50,
            "width": old_size["width"] - 50,
        }

        new_size = self.marionette.set_window_rect(
            height=wanted_size["height"], width=wanted_size["width"]
        )
        expected_size = self.marionette.window_rect

        self.assertEqual(
            new_size["width"],
            wanted_size["width"],
            "New width is {0} but should be {1}".format(
                new_size["width"], wanted_size["width"]
            ),
        )
        self.assertEqual(
            new_size["height"],
            wanted_size["height"],
            "New height is {0} but should be {1}".format(
                new_size["height"], wanted_size["height"]
            ),
        )
        self.assertEqual(
            new_size["width"],
            expected_size["width"],
            "New width is {0} but should be {1}".format(
                new_size["width"], expected_size["width"]
            ),
        )
        self.assertEqual(
            new_size["height"],
            expected_size["height"],
            "New height is {0} but should be {1}".format(
                new_size["height"], expected_size["height"]
            ),
        )

    def test_set_position_and_size(self):
        old_rect = self.marionette.window_rect
        wanted_rect = {
            "x": old_rect["x"] + 10,
            "y": old_rect["y"] + 10,
            "width": old_rect["width"] - 50,
            "height": old_rect["height"] - 50,
        }

        new_rect = self.marionette.set_window_rect(
            x=wanted_rect["x"],
            y=wanted_rect["y"],
            width=wanted_rect["width"],
            height=wanted_rect["height"],
        )
        expected_rect = self.marionette.window_rect

        self.assertEqual(new_rect["x"], wanted_rect["x"])
        self.assertEqual(new_rect["y"], wanted_rect["y"])
        self.assertEqual(
            new_rect["width"],
            wanted_rect["width"],
            "New width is {0} but should be {1}".format(
                new_rect["width"], wanted_rect["width"]
            ),
        )
        self.assertEqual(
            new_rect["height"],
            wanted_rect["height"],
            "New height is {0} but should be {1}".format(
                new_rect["height"], wanted_rect["height"]
            ),
        )
        self.assertEqual(new_rect["x"], expected_rect["x"])
        self.assertEqual(new_rect["y"], expected_rect["y"])
        self.assertEqual(
            new_rect["width"],
            expected_rect["width"],
            "New width is {0} but should be {1}".format(
                new_rect["width"], expected_rect["width"]
            ),
        )
        self.assertEqual(
            new_rect["height"],
            expected_rect["height"],
            "New height is {0} but should be {1}".format(
                new_rect["height"], expected_rect["height"]
            ),
        )

    def test_move_to_current_position(self):
        old_position = self.marionette.window_rect
        new_position = self.marionette.set_window_rect(
            x=old_position["x"], y=old_position["y"]
        )

        self.assertEqual(new_position["x"], old_position["x"])
        self.assertEqual(new_position["y"], old_position["y"])

    def test_move_to_current_size(self):
        old_size = self.marionette.window_rect
        new_size = self.marionette.set_window_rect(
            height=old_size["height"], width=old_size["width"]
        )

        self.assertEqual(new_size["height"], old_size["height"])
        self.assertEqual(new_size["width"], old_size["width"])

    def test_move_to_current_position_and_size(self):
        old_position_and_size = self.marionette.window_rect
        new_position_and_size = self.marionette.set_window_rect(
            x=old_position_and_size["x"],
            y=old_position_and_size["y"],
            height=old_position_and_size["height"],
            width=old_position_and_size["width"],
        )

        self.assertEqual(new_position_and_size["x"], old_position_and_size["x"])
        self.assertEqual(new_position_and_size["y"], old_position_and_size["y"])
        self.assertEqual(new_position_and_size["width"], old_position_and_size["width"])
        self.assertEqual(
            new_position_and_size["height"], old_position_and_size["height"]
        )

    def test_move_to_negative_coordinates(self):
        old_position = self.marionette.window_rect
        print("Current position: {}".format(old_position["x"], old_position["y"]))
        new_position = self.marionette.set_window_rect(x=-8, y=-8)
        print(
            "Position after requesting move to negative coordinates: {}, {}".format(
                new_position["x"], new_position["y"]
            )
        )

        # Different systems will report more or less than (-8,-8)
        # depending on the characteristics of the window manager, since
        # the screenX/screenY position measures the chrome boundaries,
        # including any WM decorations.
        #
        # This makes this hard to reliably test across different
        # environments.  Generally we are happy when calling
        # marionette.set_window_position with negative coordinates does
        # not throw.
        #
        # Because we have to cater to an unknown set of environments,
        # the following assertions are the most common denominator that
        # make this test pass, irregardless of system characteristics.

        os = self.marionette.session_capabilities["platformName"]

        # Regardless of platform, headless always supports being positioned
        # off-screen.
        if self.marionette.session_capabilities["moz:headless"]:
            self.assertEqual(-8, new_position["x"])
            self.assertEqual(-8, new_position["y"])

        # Certain WMs prohibit windows from being moved off-screen,
        # but we don't have this information.  It should be safe to
        # assume a window can be moved to (0,0) or less.
        elif os == "linux":
            # certain WMs prohibit windows from being moved off-screen
            self.assertLessEqual(new_position["x"], 0)
            self.assertLessEqual(new_position["y"], 0)

        # On macOS, windows can only be moved off the screen on the
        # horizontal axis.  The system menu bar also blocks windows from
        # being moved to (0,0).
        elif os == "mac":
            self.assertEqual(-8, new_position["x"])
            self.assertEqual(23, new_position["y"])

        # It turns out that Windows is the only platform on which the
        # window can be reliably positioned off-screen.
        elif os == "windows":
            self.assertEqual(-8, new_position["x"])
            self.assertEqual(-8, new_position["y"])

    def test_resize_larger_than_screen(self):
        new_size = self.marionette.set_window_rect(
            width=self.max["width"] * 2, height=self.max["height"] * 2
        )
        actual_size = self.marionette.window_rect

        # in X the window size may be greater than the bounds of the screen
        self.assertGreaterEqual(new_size["width"], self.max["width"])
        self.assertGreaterEqual(new_size["height"], self.max["height"])
        self.assertEqual(actual_size["width"], new_size["width"])
        self.assertEqual(actual_size["height"], new_size["height"])

    def test_resize_to_available_screen_size(self):
        expected_size = self.marionette.set_window_rect(
            width=self.max["width"], height=self.max["height"]
        )
        result_size = self.marionette.window_rect

        self.assertGreaterEqual(expected_size["width"], self.max["width"])
        self.assertGreaterEqual(expected_size["height"], self.max["height"])
        self.assertEqual(result_size["width"], expected_size["width"])
        self.assertEqual(result_size["height"], expected_size["height"])

    def test_resize_while_fullscreen(self):
        self.marionette.fullscreen()
        expected_size = self.marionette.set_window_rect(
            width=self.max["width"] - 100, height=self.max["height"] - 100
        )
        result_size = self.marionette.window_rect

        self.assertTrue(
            self.marionette.execute_script(
                "return window.fullscreenElement == null", sandbox=None
            )
        )
        self.assertEqual(self.max["width"] - 100, expected_size["width"])
        self.assertEqual(self.max["height"] - 100, expected_size["height"])
        self.assertEqual(result_size["width"], expected_size["width"])
        self.assertEqual(result_size["height"], expected_size["height"])