[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.22
[framework/web/webkit-efl.git] / LayoutTests / fast / events / message-port-clone.html
1 <body>
2 <p>Tests various use cases when cloning MessagePorts.</p>
3 <p>Should be a series of SUCCESS messages, followed with DONE.</p>
4 <pre id=log></pre>
5 <script>
6
7 function gc()
8 {
9     if (window.GCController)
10         return GCController.collect();
11
12     for (var i = 0; i < 10000; i++) { // force garbage collection (FF requires about 9K allocations before a collect).
13         var s = new String("abc");
14     }
15 }
16
17 function log(message)
18 {
19     document.getElementById("log").innerHTML += message + "<br>";
20 }
21
22 if (window.layoutTestController) {
23     layoutTestController.dumpAsText();
24     layoutTestController.waitUntilDone();
25 }
26
27 var channel = new MessageChannel;
28 channel.port1.onmessage = channel.port2.onmessage = function(evt) {
29     fail("FAIL: Should not have received message: " + evt.data);
30 }
31 try {
32     channel.port1.postMessage("msg", [channel.port1]);
33     log("FAIL: Posting port to itself should throw an exception.");
34 } catch (ex) {
35     log("SUCCESS: Posting port to itself: " + ex);
36 }
37
38 try {
39     channel.port1.postMessage("msg", [channel.port2]);
40     log("FAIL: Posting port to entangled pair should throw an exception.");
41 } catch (ex) {
42     log("SUCCESS: Posting entangled port: " + ex);
43 }
44
45 channel = new MessageChannel;
46 var channel2 = new MessageChannel;
47 channel.port1.postMessage("msg", [channel2.port1]);
48
49 // Should not be able to post a cloned port.
50 try {
51     channel.port1.postMessage("msg", [channel2.port1]);
52     log("FAIL: Posting cloned port should throw an exception.");
53 } catch (ex) {
54     log("SUCCESS: Posting cloned port.");
55 }
56
57 // Test posting messages to a port in cloned state.
58
59 var channel = new MessageChannel;
60 var channel2 = new MessageChannel;
61
62 // Post messages before and after clone to make sure ordering is preserved and all messages are received.
63 channel2.port2.postMessage("1");
64 channel.port1.postMessage("msg", [channel2.port1]);
65 channel2.port2.postMessage("2");
66 channel2.port2.postMessage("3");
67 channel.port2.onmessage = function(evt) {
68     var messageIndex = 1;
69     if (evt.ports.length != 1)
70         log("FAIL: Got message without wrong number of ports: " + evt.ports.length);
71     evt.ports[0].onmessage = function(evt) {
72         if (evt.data != messageIndex)
73             log("FAIL: Got message " + evt.data + ", expected " + messageIndex);
74         messageIndex++;
75         if (messageIndex == 4) {
76             log("SUCCESS: Posted messages to cloned port.");
77             testDoublyClonedPort();
78         }
79     }
80 }
81
82 function testDoublyClonedPort()
83 {
84     var channel = new MessageChannel;
85     var channel2 = new MessageChannel;
86     channel.port1.postMessage("msg", [channel2.port1]);
87     channel.port2.postMessage("msg", [channel2.port2]);
88     gc();
89     channel.port1.onmessage = function(evt) {
90         evt.ports[0].postMessage("testme");
91     }
92     channel.port2.onmessage = function(evt) {
93         evt.ports[0].onmessage = function (evt) {
94             if (evt.data == "testme")
95                 log("SUCCESS: Cloned both endpoints of a channel.");
96             else
97                 log("FAIL: Invalid message arrived: " + evt.data);
98             testPostClosePort();
99         }
100     }
101
102 }
103
104 // *Should* be able to post a closed port.
105 function testPostClosePort()
106 {
107     var channel = new MessageChannel;
108     var channel2 = new MessageChannel;
109     channel2.port2.close();
110     channel.port1.postMessage("closed", [channel2.port2]);
111     channel.port2.onmessage = function(evt) {
112         if (!evt.ports || evt.ports.length != 1)
113             log("FAIL: Closed port not sent.");
114         else if (evt.data != "closed")
115             log("FAIL: Unexpected message: " + evt.data);
116         else
117             log("SUCCESS: Got closed port with event message " + evt.data);
118         done();
119     }
120 }
121
122 function done()
123 {
124     log("DONE");
125
126     if (window.layoutTestController)
127         layoutTestController.notifyDone();
128 }
129 </script>
130 </body>