Bug 1708422: part 14) Extend documentation of `mozInlineSpellChecker::SpellCheckerTim...
[gecko.git] / dom / xhr / tests / test_XHR_parameters.html
blob4ba1c5562a89c30dccbf468654546cc5799b3636
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="utf-8">
7 <title>Test for XMLHttpRequest with system privileges</title>
8 <script src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body onload="runTests();">
12 <p id="display">
13 </p>
14 <div id="content" style="display: none">
16 </div>
17 <pre id="test">
18 <script class="testbody" type="application/javascript">
20 function runTests() {
21 SimpleTest.waitForExplicitFinish();
23 let validParameters = [
24 undefined,
25 null,
26 {},
27 {mozSystem: ""},
28 {mozSystem: 0},
29 {mozAnon: 1},
30 {mozAnon: []},
31 {get mozAnon() { return true; }},
34 Math.PI,
35 "string",
36 true,
37 false,
40 let invalidParameters = [
41 {get mozSystem() { throw new Error("Bla"); } },
44 let havePrivileges = false;
46 function testValidParameter(value) {
47 let xhr;
48 try {
49 xhr = new XMLHttpRequest(value);
50 } catch (ex) {
51 ok(false, "Got unexpected exception: " + ex);
52 return;
54 ok(xhr instanceof XMLHttpRequest, "passed " + JSON.stringify(value));
56 // If the page doesnt have privileges to create a system XHR,
57 // this flag will always be false no matter what is passed.
58 let expectedAnon = Boolean(value && value.mozAnon);
59 let expectedSystem = false;
60 if (havePrivileges) {
61 expectedSystem = Boolean(value && value.mozSystem);
63 is(xhr.mozAnon, expectedAnon, "testing mozAnon");
64 is(xhr.mozSystem, expectedSystem, "testing mozSystem");
67 function testInvalidParameter(value) {
68 let expectedError;
69 try {
70 new XMLHttpRequest(value);
71 ok(false, "invalid parameter did not cause exception: " +
72 JSON.stringify(value));
73 } catch (ex) {
74 expectedError = ex;
76 ok(expectedError, "invalid parameter raised exception as expected: " +
77 JSON.stringify(expectedError))
80 // Run the tests once without API privileges...
81 validParameters.forEach(testValidParameter);
82 invalidParameters.forEach(testInvalidParameter);
84 // ...and once with privileges.
85 havePrivileges = true;
86 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], function() {
87 validParameters.forEach(testValidParameter);
88 invalidParameters.forEach(testInvalidParameter);
90 SimpleTest.finish();
91 });
94 </script>
95 </pre>
96 </body>
97 </html>