WebCore:
[WebKit-https.git] / LayoutTests / http / tests / workers / resources / worker-importScripts.js
1 try {
2 postMessage("Test started.");
3
4 importScripts();
5 postMessage("PASS: importScripts(), exists, is a function, and doesn't throw when not given any arguments");
6
7 var source1 = "worker-importScripts-source1.js";
8 var source2 = "worker-importScripts-source2.js";
9 var differentOrigin = "http://localhost:8000/workers/resources/worker-importScripts-differentOrigin.js";
10 var differentRedirectOrigin = "/resources/redirect.php?url=http://localhost:8000/workers/resources/worker-importScripts-differentOrigin.js";
11 var syntaxErrorSource = "worker-importScripts-syntaxError.js";
12 var fakeSource = "nonexistant";
13 var loadedSource1 = false;
14 var loadedSource2 = false;
15 var differentOriginLoaded = false;
16
17 function resetLoadFlags() {
18     loadedSource1 = false;
19     loadedSource2 = false;
20     differentOriginLoaded = false;
21 }
22
23 try {
24     importScripts(differentOrigin)
25 } catch(e) {
26     postMessage("FAIL: Threw " + e + " when attempting cross origin load");
27 }
28 if (differentOriginLoaded)
29     postMessage("PASS: executed script from different origin");
30
31 resetLoadFlags();
32
33 try {
34     importScripts(differentRedirectOrigin)
35 } catch(e) {
36     postMessage("FAIL: Threw " + e + " when attempting load from different origin through a redirect");
37 }
38 if (differentOriginLoaded)
39     postMessage("PASS: executed script from different origin through a redirect");
40 else
41     postMessage("FAIL: did not load script from different origin through a redirect");
42
43 resetLoadFlags();
44
45 postMessage("Testing single argument:");
46 importScripts(source1)
47 if (loadedSource1)
48     postMessage("PASS: loaded first source");
49 else
50     postMessage("FAIL: did not load first source");
51
52 resetLoadFlags();
53
54 postMessage("Testing multiple arguments:");
55 importScripts(source1, source2);
56 if (loadedSource1 && loadedSource2)
57     postMessage("PASS: Both sources loaded and executed.");
58 else
59     postMessage("FAIL: not all sources were loaded");
60
61 resetLoadFlags();
62
63 postMessage("Testing multiple arguments (different order):");
64 importScripts(source2, source1);
65 if (loadedSource1 && loadedSource2)
66     postMessage("PASS: Both sources loaded and executed.");
67 else
68     postMessage("FAIL: not all sources were loaded");
69
70 resetLoadFlags();
71 firstShouldThrow = false;
72 secondShouldThrow = false;
73 postMessage("Testing multiple arguments, with different origin for one argument:");
74 try {
75     importScripts(source1, differentOrigin, source2);
76 } catch(e) {
77     postMessage("FAIL: Threw " + e + " when attempt cross origin load");
78 }
79 if (loadedSource1 && loadedSource2 && differentOriginLoaded)
80     postMessage("PASS: all resources executed.");
81 else
82     postMessage("FAIL: Not  of the origin failure");
83
84 resetLoadFlags();
85
86 try {
87     importScripts(source1, fakeSource, source2);
88 } catch(e) {
89     postMessage("PASS: Threw " + e + " when load failed");
90 }
91 if (!loadedSource1 && !loadedSource2)
92     postMessage("FAIL: Nothing was executed when network error occurred.");
93 else
94     postMessage("PASS: some resources were loaded despite the network error");
95
96 resetLoadFlags();
97
98 try {
99     importScripts(source1, syntaxErrorSource, source2);
100 } catch(e) {
101     postMessage("PASS: Threw " + e + " when encountering a syntax error in imported script");
102 }
103 if (!loadedSource1 && !loadedSource2)
104     postMessage("FAIL: Nothing was executed when syntax error was present in any source.");
105 else
106     postMessage("PASS: some resources were loaded despite the presence of a syntax error");
107
108 resetLoadFlags();
109
110 postMessage("Testing multiple arguments, with first resource throwing an exception:");
111 firstShouldThrow = true;
112 try {
113     importScripts(source1, source2);
114 } catch(e) {
115     postMessage("PASS: Propagated '" + e + "' from script");
116 }
117 firstShouldThrow = false;
118 if (loadedSource1 && !loadedSource2)
119     postMessage("PASS: First resource was executed, and second resource was not");
120 else if (loadedSource2)
121     postMessage("FAIL: Second resource was executed");
122 else
123     postMessage("FAIL: first resource did not execute correctly");
124
125 resetLoadFlags();
126
127 postMessage("Testing multiple arguments, with second resource throwing an exception:");
128 secondShouldThrow = true;
129 try {
130     importScripts(source1, source2);
131 } catch(e) {
132     postMessage("PASS: Propagated '" + e + "' from script");
133 }
134 secondShouldThrow = false;
135 if (loadedSource1 && loadedSource2)
136     postMessage("PASS: Both scripts were executed");
137 else
138     postMessage("FAIL: scripts did not execute correctly");
139
140 resetLoadFlags();
141
142 } catch(e) {
143     postMessage("FAIL: Unexpected exception: " + e);
144 } finally {
145     postMessage("DONE");
146 }