f700f9e4699bc4503a76c890f239820307572756
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / workers / resources / shared-worker-name.js
1 if (window.testRunner) {
2     testRunner.dumpAsText();
3     testRunner.waitUntilDone();
4 }
5
6 description("Checks the various use cases around the SharedWorker constructor's optional name parameter");
7
8 var currentTest = 0;
9 nextTest();
10
11 // Iterates through the tests until none are left.
12 function nextTest()
13 {
14     currentTest++;
15     var testFunction = window["test" + currentTest];
16     if (testFunction)
17         testFunction();
18     else
19         done();
20 }
21
22 function test1()
23 {
24     // Make sure we can create a shared worker with no name.
25     try {
26         var worker = new SharedWorker('resources/shared-worker-common.js');
27         testPassed("created SharedWorker with no name");
28         worker.port.postMessage("eval self.foo = 1234");
29         worker.port.onmessage = function(event) {
30             shouldBeEqual("setting self.foo", event.data, "self.foo = 1234: 1234");
31             nextTest();
32         };
33     } catch (e) {
34         testFailed("SharedWorker with no name threw an exception: " + e);
35         done();
36     }
37 }
38
39 function test2()
40 {
41     // Creating a worker with no name should match an existing worker with no name
42     var worker = new SharedWorker('resources/shared-worker-common.js');
43     worker.port.postMessage("eval self.foo");
44     worker.port.onmessage = function(event) {
45         shouldBeEqual("creating worker with no name", event.data, "self.foo: 1234");
46         nextTest();
47     }
48 }
49
50 function test3()
51 {
52     // Creating a worker with an empty name should be the same as a worker with no name.
53     var worker = new SharedWorker('resources/shared-worker-common.js', '');
54     worker.port.postMessage("eval self.foo");
55     worker.port.onmessage = function(event) {
56         shouldBeEqual("creating worker with empty name", event.data, "self.foo: 1234");
57         nextTest();
58     };
59 }
60
61 function test4()
62 {
63     // Creating a worker with a different name should not be the same as a worker with no name.
64     var worker = new SharedWorker('resources/shared-worker-common.js', 'name');
65     worker.port.postMessage("eval self.foo");
66     worker.port.onmessage = function(event) {
67         shouldBeEqual("creating worker with different name but same URL", event.data, "self.foo: undefined");
68         nextTest();
69     };
70 }
71
72 function test5()
73 {
74     // Creating a worker for an alternate URL with no name should work.
75     var worker = new SharedWorker('resources/shared-worker-common.js?url=1');
76     worker.port.postMessage("eval self.foo");
77     worker.port.onmessage = function(event) {
78         shouldBeEqual("creating no-name worker with alternate URL", event.data, "self.foo: undefined");
79         nextTest();
80     };
81 }
82
83 function test6()
84 {
85     // Creating a worker for an alternate URL with empty name should work.
86     var worker = new SharedWorker('resources/shared-worker-common.js?url=2', '');
87     worker.port.postMessage("eval self.foo");
88     worker.port.onmessage = function(event) {
89         shouldBeEqual("creating empty name worker with alternate URL", event.data, "self.foo: undefined");
90         nextTest();
91     };
92 }
93
94 function test7()
95 {
96     // Make sure we can create a shared worker with name 'null'.
97     try {
98         var worker = new SharedWorker('resources/shared-worker-common.js', 'null');
99         testPassed("created SharedWorker with name 'null'");
100         worker.port.postMessage("eval self.foo = 5678");
101         worker.port.onmessage = function(event) {
102             shouldBeEqual("setting self.foo", event.data, "self.foo = 5678: 5678");
103             nextTest();
104         };
105     } catch (e) {
106         testFailed("SharedWorker with name 'null' threw an exception: " + e);
107         done();
108     }
109 }
110
111 function test8()
112 {
113     // Creating a worker with a null name should match an existing worker with name 'null'
114     var worker = new SharedWorker('resources/shared-worker-common.js', null);
115     worker.port.postMessage("eval self.foo");
116     worker.port.onmessage = function(event) {
117         shouldBeEqual("creating worker with a null name", event.data, "self.foo: 5678");
118         nextTest();
119     }
120 }
121
122 function test9()
123 {
124     // Make sure we can create a shared worker with name 'undefined'.
125     try {
126         var worker = new SharedWorker('resources/shared-worker-common.js', 'undefined');
127         testPassed("created SharedWorker with name 'undefined'");
128         worker.port.postMessage("eval self.foo = 1111");
129         worker.port.onmessage = function(event) {
130             shouldBeEqual("setting self.foo", event.data, "self.foo = 1111: 1111");
131             nextTest();
132         };
133     } catch (e) {
134         testFailed("SharedWorker with name 'undefined' threw an exception: " + e);
135         done();
136     }
137 }
138
139 function test10()
140 {
141     // Creating a worker with an undefined name should match an existing worker with name 'undefined'
142     var worker = new SharedWorker('resources/shared-worker-common.js', undefined);
143     worker.port.postMessage("eval self.foo");
144     worker.port.onmessage = function(event) {
145         shouldBeEqual("creating worker with an undefined name", event.data, "self.foo: 1111");
146         nextTest();
147     }
148 }
149
150 function test11()
151 {
152     // Creating a worker with a specific name, the name attribute should be set to worker correctly.
153     var worker = new SharedWorker('resources/shared-worker-common.js', "testingNameAttribute");
154     worker.port.postMessage("testingNameAttribute");
155     worker.port.onmessage = function(event) {
156         shouldBeEqual("the name attribute of worker can be set correctly", event.data, "testingNameAttribute");
157         nextTest();
158     }
159 }
160
161 function shouldBeEqual(description, a, b)
162 {
163     if (a == b)
164         testPassed(description);
165     else
166         testFailed(description + " - passed value: " + a + ", expected value: " + b);
167 }