summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_modal_dialogs.py
blob: e738625899fa6170432b929c600eebe33b3d22ee (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
from marionette_driver.by import By
from marionette_driver.expected import element_present
from marionette_driver import errors
from marionette_driver.marionette import Alert
from marionette_driver.wait import Wait

from marionette_harness import MarionetteTestCase, parameterized, WindowManagerMixin


class TestModalDialogs(WindowManagerMixin, MarionetteTestCase):
    def setUp(self):
        super(TestModalDialogs, self).setUp()
        self.new_tab = self.open_tab()
        self.marionette.switch_to_window(self.new_tab)

        self.http_auth_pref = (
            "network.auth.non-web-content-triggered-resources-http-auth-allow"
        )

    def tearDown(self):
        # Ensure to close all possible remaining tab modal dialogs
        try:
            while True:
                alert = self.marionette.switch_to_alert()
                alert.dismiss()
        except errors.NoAlertPresentException:
            pass

        self.close_all_tabs()
        self.close_all_windows()

        super(TestModalDialogs, self).tearDown()

    @property
    def alert_present(self):
        try:
            Alert(self.marionette).text
            return True
        except errors.NoAlertPresentException:
            return False

    def wait_for_alert(self, timeout=None):
        Wait(self.marionette, timeout=timeout).until(lambda _: self.alert_present)

    def open_custom_prompt(self, modal_type, delay=0):
        browsing_context_id = self.marionette.execute_script(
            """
            return window.browsingContext.id;
        """,
            sandbox="system",
        )

        with self.marionette.using_context("chrome"):
            self.marionette.execute_script(
                """
                const [ modalType, browsingContextId, delay ] = arguments;

                const modalTypes = {
                  1: Services.prompt.MODAL_TYPE_CONTENT,
                  2: Services.prompt.MODAL_TYPE_TAB,
                  3: Services.prompt.MODAL_TYPE_WINDOW,
                  4: Services.prompt.MODAL_TYPE_INTERNAL_WINDOW,
                }

                window.setTimeout(() => {
                  Services.prompt.alertBC(
                    BrowsingContext.get(browsingContextId),
                    modalTypes[modalType],
                    "title",
                    "text"
                  );
                }, delay);
            """,
                script_args=(modal_type, browsing_context_id, delay * 1000),
            )

    @parameterized("content", 1)
    @parameterized("tab", 2)
    @parameterized("window", 3)
    @parameterized("internal_window", 4)
    def test_detect_modal_type_in_current_tab_for_type(self, type):
        self.open_custom_prompt(type)
        self.wait_for_alert()

        self.assertTrue(self.alert_present)

        # Restart the session to ensure we still find the formerly left-open dialog.
        self.marionette.delete_session()
        self.marionette.start_session()

        alert = self.marionette.switch_to_alert()
        alert.dismiss()

    @parameterized("content", 1)
    @parameterized("tab", 2)
    def test_dont_detect_content_and_tab_modal_type_in_another_tab_for_type(self, type):
        self.open_custom_prompt(type, delay=0.25)

        self.marionette.switch_to_window(self.start_tab)
        with self.assertRaises(errors.TimeoutException):
            self.wait_for_alert(2)

        self.marionette.switch_to_window(self.new_tab)
        alert = self.marionette.switch_to_alert()
        alert.dismiss()

    @parameterized("window", 3)
    @parameterized("internal_window", 4)
    def test_detect_window_modal_type_in_another_tab_for_type(self, type):
        self.open_custom_prompt(type, delay=0.25)

        self.marionette.switch_to_window(self.start_tab)
        self.wait_for_alert()

        alert = self.marionette.switch_to_alert()
        alert.dismiss()

        self.marionette.switch_to_window(self.new_tab)
        self.assertFalse(self.alert_present)

    @parameterized("window", 3)
    @parameterized("internal_window", 4)
    def test_detect_window_modal_type_in_another_window_for_type(self, type):
        self.new_window = self.open_window()

        self.marionette.switch_to_window(self.new_window)

        self.open_custom_prompt(type, delay=0.25)

        self.marionette.switch_to_window(self.new_tab)
        with self.assertRaises(errors.TimeoutException):
            self.wait_for_alert(2)

        self.marionette.switch_to_window(self.new_window)
        alert = self.marionette.switch_to_alert()
        alert.dismiss()

        self.marionette.switch_to_window(self.new_tab)
        self.assertFalse(self.alert_present)

    def test_http_auth_dismiss(self):
        with self.marionette.using_prefs({self.http_auth_pref: True}):
            self.marionette.navigate(self.marionette.absolute_url("http_auth"))
            self.wait_for_alert(timeout=self.marionette.timeout.page_load)

            alert = self.marionette.switch_to_alert()
            alert.dismiss()

            status = Wait(
                self.marionette, timeout=self.marionette.timeout.page_load
            ).until(element_present(By.ID, "status"))
            self.assertEqual(status.text, "restricted")

    def test_http_auth_send_keys(self):
        with self.marionette.using_prefs({self.http_auth_pref: True}):
            self.marionette.navigate(self.marionette.absolute_url("http_auth"))
            self.wait_for_alert(timeout=self.marionette.timeout.page_load)

            alert = self.marionette.switch_to_alert()
            with self.assertRaises(errors.UnsupportedOperationException):
                alert.send_keys("foo")