summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_set_window_size.py
blob: e1bd5e68493433597b983f8de585943f1a16872f (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
# 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_harness import MarionetteTestCase


class TestSetWindowSize(MarionetteTestCase):
    def setUp(self):
        super(MarionetteTestCase, self).setUp()
        self.start_size = self.marionette.window_size
        self.max_width = self.marionette.execute_script("return window.screen.availWidth;")
        self.max_height = self.marionette.execute_script("return window.screen.availHeight;")

    def tearDown(self):
        # WebDriver spec says a resize cannot result in window being maximized, an
        # error is returned if that is the case; therefore if the window is maximized
        # at the start of this test, returning to the original size via set_window_size
        # size will result in error; so reset to original size minus 1 pixel width
        if self.start_size['width'] == self.max_width and self.start_size['height'] == self.max_height:
            self.start_size['width']-=1
        self.marionette.set_window_size(self.start_size['width'], self.start_size['height'])
        super(MarionetteTestCase, self).tearDown()

    def test_that_we_can_get_and_set_window_size(self):
        # event handler
        self.marionette.execute_script("""
        window.wrappedJSObject.rcvd_event = false;
        window.onresize = function() {
            window.wrappedJSObject.rcvd_event = true;
        };
        """)

        # valid size
        width = self.max_width - 100
        height = self.max_height - 100
        self.marionette.set_window_size(width, height)
        self.wait_for_condition(lambda m: m.execute_script("return window.wrappedJSObject.rcvd_event;"))
        size = self.marionette.window_size
        self.assertEqual(size['width'], width,
                         "Window width is {0} but should be {1}".format(size['width'], width))
        self.assertEqual(size['height'], height,
                         "Window height is {0} but should be {1}".format(size['height'], height))

    def test_that_we_can_get_new_size_when_set_window_size(self):
        actual = self.marionette.window_size
        width = actual['width'] - 50
        height = actual['height'] - 50
        size = self.marionette.set_window_size(width, height)
        self.assertIsNotNone(size, "Response is None")
        self.assertEqual(size['width'], width,
                         "New width is {0} but should be {1}".format(size['width'], width))
        self.assertEqual(size['height'], height,
                         "New height is {0} but should be {1}".format(size['height'], height))

    def test_possible_to_request_window_larger_than_screen(self):
        self.marionette.set_window_size(4 * self.max_width, 4 * self.max_height)
        size = self.marionette.window_size

        # In X the window size may be greater than the bounds of the screen
        self.assertGreaterEqual(size["width"], self.max_width)
        self.assertGreaterEqual(size["height"], self.max_height)

    def test_that_we_can_maximise_the_window(self):
        # valid size
        width = self.max_width - 100
        height = self.max_height - 100
        self.marionette.set_window_size(width, height)

        # event handler
        self.marionette.execute_script("""
        window.wrappedJSObject.rcvd_event = false;
        window.onresize = function() {
            window.wrappedJSObject.rcvd_event = true;
        };
        """)
        self.marionette.maximize_window()
        self.wait_for_condition(lambda m: m.execute_script("return window.wrappedJSObject.rcvd_event;"))

        size = self.marionette.window_size
        self.assertGreaterEqual(size['width'], self.max_width,
                         "Window width does not use availWidth, current width: {0}, max width: {1}".format(size['width'], self.max_width))
        self.assertGreaterEqual(size['height'], self.max_height,
                         "Window height does not use availHeight. current width: {0}, max width: {1}".format(size['height'], self.max_height))