summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_execute_async_script.py
blob: 49f68f7b94c73f77f63f326b785b32bf7c1e78c1 (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
import os

from marionette_driver.errors import (
    JavascriptException,
    NoAlertPresentException,
    ScriptTimeoutException,
)
from marionette_driver.marionette import Alert
from marionette_driver.wait import Wait

from marionette_harness import MarionetteTestCase


class TestExecuteAsyncContent(MarionetteTestCase):
    def setUp(self):
        super(TestExecuteAsyncContent, self).setUp()
        self.marionette.timeout.script = 1

    def tearDown(self):
        if self.alert_present():
            alert = self.marionette.switch_to_alert()
            alert.dismiss()
            self.wait_for_alert_closed()

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

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

    def test_execute_async_simple(self):
        self.assertEqual(
            1, self.marionette.execute_async_script("arguments[arguments.length-1](1);")
        )

    def test_execute_async_ours(self):
        self.assertEqual(1, self.marionette.execute_async_script("arguments[0](1);"))

    def test_script_timeout_error(self):
        with self.assertRaisesRegexp(ScriptTimeoutException, "Timed out after 100 ms"):
            self.marionette.execute_async_script("var x = 1;", script_timeout=100)

    def test_script_timeout_reset_after_timeout_error(self):
        script_timeout = self.marionette.timeout.script
        with self.assertRaises(ScriptTimeoutException):
            self.marionette.execute_async_script("var x = 1;", script_timeout=100)
        self.assertEqual(self.marionette.timeout.script, script_timeout)

    def test_script_timeout_no_timeout_error(self):
        self.assertTrue(
            self.marionette.execute_async_script(
                """
            var callback = arguments[arguments.length - 1];
            setTimeout(function() { callback(true); }, 500);
            """,
                script_timeout=1000,
            )
        )

    def test_no_timeout(self):
        self.marionette.timeout.script = 10
        self.assertTrue(
            self.marionette.execute_async_script(
                """
            var callback = arguments[arguments.length - 1];
            setTimeout(function() { callback(true); }, 500);
            """
            )
        )

    def test_execute_async_unload(self):
        self.marionette.timeout.script = 5
        unload = """
                window.location.href = "about:blank";
                 """
        self.assertRaises(
            JavascriptException, self.marionette.execute_async_script, unload
        )

    def test_check_window(self):
        self.assertTrue(
            self.marionette.execute_async_script(
                "arguments[0](window != null && window != undefined);"
            )
        )

    def test_same_context(self):
        var1 = "testing"
        self.assertEqual(
            self.marionette.execute_script(
                """
            this.testvar = '{}';
            return this.testvar;
            """.format(
                    var1
                )
            ),
            var1,
        )
        self.assertEqual(
            self.marionette.execute_async_script(
                "arguments[0](this.testvar);", new_sandbox=False
            ),
            var1,
        )

    def test_execute_no_return(self):
        self.assertEqual(self.marionette.execute_async_script("arguments[0]()"), None)

    def test_execute_js_exception(self):
        try:
            self.marionette.execute_async_script(
                """
                let a = 1;
                foo(bar);
                """
            )
            self.fail()
        except JavascriptException as e:
            self.assertIsNotNone(e.stacktrace)
            self.assertIn(
                os.path.relpath(__file__.replace(".pyc", ".py")), e.stacktrace
            )

    def test_execute_async_js_exception(self):
        try:
            self.marionette.execute_async_script(
                """
                let [resolve] = arguments;
                resolve(foo());
            """
            )
            self.fail()
        except JavascriptException as e:
            self.assertIsNotNone(e.stacktrace)
            self.assertIn(
                os.path.relpath(__file__.replace(".pyc", ".py")), e.stacktrace
            )

    def test_script_finished(self):
        self.assertTrue(
            self.marionette.execute_async_script(
                """
            arguments[0](true);
            """
            )
        )

    def test_execute_permission(self):
        self.assertRaises(
            JavascriptException,
            self.marionette.execute_async_script,
            """
let prefs = Components.classes["@mozilla.org/preferences-service;1"]
                              .getService(Components.interfaces.nsIPrefBranch);
arguments[0](4);
""",
        )

    def test_sandbox_reuse(self):
        # Sandboxes between `execute_script()` invocations are shared.
        self.marionette.execute_async_script(
            "this.foobar = [23, 42];" "arguments[0]();"
        )
        self.assertEqual(
            self.marionette.execute_async_script(
                "arguments[0](this.foobar);", new_sandbox=False
            ),
            [23, 42],
        )

    def test_sandbox_refresh_arguments(self):
        self.marionette.execute_async_script(
            "this.foobar = [arguments[0], arguments[1]];"
            "let resolve = "
            "arguments[arguments.length - 1];"
            "resolve();",
            script_args=[23, 42],
        )
        self.assertEqual(
            self.marionette.execute_async_script(
                "arguments[0](this.foobar);", new_sandbox=False
            ),
            [23, 42],
        )

    # Functions defined in higher privilege scopes, such as the privileged
    # JSWindowActor child runs in, cannot be accessed from
    # content.  This tests that it is possible to introspect the objects on
    # `arguments` without getting permission defined errors.  This is made
    # possible because the last argument is always the callback/complete
    # function.
    #
    # See bug 1290966.
    def test_introspection_of_arguments(self):
        self.marionette.execute_async_script(
            "arguments[0].cheese; __webDriverCallback();", script_args=[], sandbox=None
        )

    def test_return_value_on_alert(self):
        res = self.marionette.execute_async_script("alert()")
        self.assertIsNone(res)


class TestExecuteAsyncChrome(TestExecuteAsyncContent):
    def setUp(self):
        super(TestExecuteAsyncChrome, self).setUp()
        self.marionette.set_context("chrome")

    def test_execute_async_unload(self):
        pass

    def test_execute_permission(self):
        self.assertEqual(
            5,
            self.marionette.execute_async_script(
                """
            var c = Components.classes;
            arguments[0](5);
            """
            ),
        )

    def test_execute_async_js_exception(self):
        # Javascript exceptions are not propagated in chrome code
        self.marionette.timeout.script = 0.2
        with self.assertRaises(ScriptTimeoutException):
            self.marionette.execute_async_script(
                """
                var callback = arguments[arguments.length - 1];
                setTimeout(function() { callback(foo()); }, 50);
                """
            )

    def test_return_value_on_alert(self):
        pass