2011-02-16 Vitaly Repeshko <vitalyr@chromium.org>
[WebKit-https.git] / LayoutTests / fast / dom / Window / window-postmessage-clone.html
1 <html>
2 <head></head>
3 <body>
4 <div id="description"></div>
5 <input type="file" id="fileInput"></input>
6 <div id="console"></div>
7 <script>
8 if (window.layoutTestController) {
9     layoutTestController.dumpAsText();
10     layoutTestController.waitUntilDone();
11 }
12
13 var console = document.getElementById("console");
14
15 var messages = [];
16
17 function equal(actual, expected)
18 {
19     if (actual === expected)
20         return true;
21     if (typeof actual !== typeof expected)
22         return false;
23     if ((actual instanceof Date) || (expected instanceof Date)) {
24         if ((actual instanceof Date) && (expected instanceof Date))
25             return (expected instanceof Date) && actual.getTime() == expected.getTime();
26         return false;
27     }
28     if (Array.isArray(actual) || Array.isArray(expected)) {
29         if (!Array.isArray(actual) || !Array.isArray(expected))
30             return false;
31         if (actual.length != expected.length)
32             return false;
33         for (var i = 0; i < actual.length; i++) {
34             if ((i in actual) ^ (i in expected))
35                 return false;
36             if (!equal(actual[i], expected[i]))
37                 return false;
38         }
39         return true;
40     }
41     if (actual.constructor !== expected.constructor)
42         return false;
43     try {
44         var keys = Object.keys(actual);
45     } catch(e) {
46         return false;
47     }
48     try {
49     if (!equal(keys, Object.keys(expected)))
50         return false;
51     } catch(e) {
52         return false;
53     }
54     for (var i = 0; i < keys.length; i++) {
55         if (!equal(actual[keys[i]], expected[keys[i]]))
56             return false;
57     }
58     return true;
59 }
60
61 function safeToString(o) {
62     if (o instanceof Date)
63         return o.toISOString();
64     if (typeof o !== "object" || !o)
65         return o;
66     try {
67         return o.toString();
68     } catch (e) {
69         return Object.prototype.toString.call(o) + "(default toString threw "+e+")";
70     }
71 }
72
73 function shouldBe(actual, expected)
74 {
75     var actualValue = eval(actual);
76     var expectedValue = eval(expected);
77     if (equal(actualValue, expectedValue))
78         console.innerHTML += "PASS: " + actual + " is " + safeToString(expectedValue) + " of type " + typeof actualValue + "<br>";
79     else
80         console.innerHTML += "FAIL: " + actual + " is " + actualValue + " should be " + expectedValue + " of type " + typeof expectedValue + "<br>";
81 }
82
83 function shouldBeIdentical(actual, expected)
84 {
85     var actualValue = eval(actual);
86     var expectedValue = eval(expected);
87     if (actualValue === expectedValue)
88         console.innerHTML += "PASS: " + actual + " is === to " + expected + "<br>";
89     else
90         console.innerHTML += "FAIL: " + actual + " is not === to " + expected + "<br>";
91 }
92
93 function onmessage(evt) {
94     eventData = evt.data;
95     if (evt.data !== evt.data)
96         console.innerHTML += "MessageEvent.data does not produce the same value on multiple queries.<br>";
97     var message = messages.shift();
98     switch (message) {
99     case "cyclicObject":
100         shouldBeIdentical("eventData", "eventData.self");
101         break;
102     case "cyclicArray":
103         shouldBeIdentical("eventData", "eventData[0]");
104         break;
105     case "objectGraph":
106         shouldBeIdentical("eventData.graph1", "eventData.graph2");
107         break;
108     case "arrayGraph":
109         shouldBeIdentical("eventData[0]", "eventData[1]");
110         break;
111     default:
112         shouldBe("eventData", message);
113     }
114
115     if (safeToString(evt.data) == 'done' && window.layoutTestController)
116         layoutTestController.notifyDone();
117 }
118
119 window.addEventListener('message', onmessage, false);
120
121 function ConstructorWithPrototype(s) {
122     this.field = s;
123 }
124
125 ConstructorWithPrototype.prototype = {
126     protoProperty: 2010
127 };
128
129 function tryPostMessage(message, shouldThrow, expected) {
130     try {
131         var value = eval(message);
132         postMessage(value, "*");
133         if (shouldThrow)
134             console.innerHTML += "FAIL: 'postMessage("+message+")' should throw but didn't<br>";
135         messages.push(expected || message);
136     } catch(e) {
137         if (shouldThrow)
138             console.innerHTML += "PASS: 'postMessage("+message+")' threw " + e + "<br>";
139         else
140             console.innerHTML += "FAIL: 'postMessage("+message+")' should not throw but threw " + e + "<br>";
141     }
142 }
143
144 document.getElementById("description").innerHTML = "Tests that we clone object hierarchies";
145
146 tryPostMessage('null');
147 tryPostMessage('undefined');
148 tryPostMessage('1');
149 tryPostMessage('true');
150 tryPostMessage('"1"');
151 tryPostMessage('({})');
152 tryPostMessage('({a:1})');
153 tryPostMessage('({a:"a"})');
154 tryPostMessage('({b:"a", a:"b"})');
155 tryPostMessage('({p0:"string0", p1:"string1", p2:"string2", p3:"string3", p4:"string4", p5:"string5", p6:"string6", p7:"string7", p8:"string8", p9:"string9", p10:"string10", p11:"string11", p12:"string12", p13:"string13", p14:"string14", p15:"string15", p16:"string16", p17:"string17", p18:"string18", p19:"string19"})');
156 tryPostMessage('({p0:"string1", p1:"string1", p2:"string2", p3:"string3", p4:"string4", p5:"string5", p6:"string6", p7:"string7", p8:"string8", p9:"string9", p10:"string10", p11:"string11", p12:"string12", p13:"string13", p14:"string14", p15:"string15", p16:"string16", p17:"string17", p18:"string18", p19:"string19"})');
157 tryPostMessage('({a:""})');
158 tryPostMessage('({a:0})');
159 tryPostMessage('({a:1})');
160 tryPostMessage('[]');
161 tryPostMessage('["a", "a", "b", "a", "b"]');
162 tryPostMessage('["a", "a", "b", {a:"b", b:"a"}]');
163 tryPostMessage('[1,2,3]');
164 tryPostMessage('[,,1]');
165 tryPostMessage('(function(){})', false, 'null');
166 tryPostMessage('new Date(1234567890000)');
167 tryPostMessage('new ConstructorWithPrototype("foo")', false, '({field:"foo"})');
168 cyclicObject={};
169 cyclicObject.self = cyclicObject;
170 tryPostMessage('cyclicObject', false, "cyclicObject");
171 cyclicArray=[];
172 cyclicArray[0] = cyclicArray;
173 tryPostMessage('cyclicArray', false, "cyclicArray");
174 objectGraph = {};
175 object = {};
176 objectGraph.graph1 = object;
177 objectGraph.graph2 = object;
178 tryPostMessage('objectGraph', false, "objectGraph");
179 arrayGraph = [object, object];
180 tryPostMessage('arrayGraph', false, "arrayGraph");
181 deepArray=[];
182 for (var i = 0; i < 10000; i++)
183     deepArray=[deepArray];
184 tryPostMessage('deepArray');
185 reallyDeepArray=[];
186 for (var i = 0; i < 100000; i++)
187     reallyDeepArray=[reallyDeepArray];
188 tryPostMessage('reallyDeepArray', true);
189 tryPostMessage('window', true);
190 tryPostMessage('({get a() { throw "x" }})', true);
191
192 if (window.eventSender) {
193     var fileInput = document.getElementById("fileInput");
194     var fileRect = fileInput.getClientRects()[0];
195     var targetX = fileRect.left + fileRect.width / 2;
196     var targetY = fileRect.top + fileRect.height / 2;
197     eventSender.beginDragWithFiles(['get-file-upload.html']);
198     eventSender.mouseMoveTo(targetX, targetY);
199     eventSender.mouseUp();
200     tryPostMessage('fileInput.files[0]', false, fileInput.files[0]);
201     tryPostMessage('fileInput.files', false, fileInput.files);
202 }
203 var imageData = document.createElement("canvas").getContext("2d").createImageData(10,10);
204 for (var i = 0; i < imageData.data.length * 4; i++)
205     imageData.data[i] = i % 256;
206 var mutatedImageData = document.createElement("canvas").getContext("2d").createImageData(10,10);
207 for (var i = 0; i < imageData.data.length * 4; i++)
208     mutatedImageData.data[i] = i % 256;
209 tryPostMessage('imageData', false, imageData);
210 tryPostMessage('mutatedImageData', false, imageData);
211 for (var i = 0; i < imageData.data.length * 4; i++)
212     mutatedImageData.data[i] = 0;
213
214 tryPostMessage('"done"');
215 </script>
216 </body>
217 </html>