tizen beta release
[profile/ivi/webkit-efl.git] / LayoutTests / fast / dom / Window / script-tests / postmessage-clone.js
1 document.getElementById("description").innerHTML = "Tests that we clone object hierarchies";
2
3 tryPostMessage('null');
4 tryPostMessage('undefined');
5 tryPostMessage('1');
6 tryPostMessage('true');
7 tryPostMessage('"1"');
8 tryPostMessage('({})');
9 tryPostMessage('({a:1})');
10 tryPostMessage('({a:"a"})');
11 tryPostMessage('({b:"a", a:"b"})');
12 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"})');
13 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"})');
14 tryPostMessage('({a:""})');
15 tryPostMessage('({a:0})');
16 tryPostMessage('({a:1})');
17 tryPostMessage('[]');
18 tryPostMessage('["a", "a", "b", "a", "b"]');
19 tryPostMessage('["a", "a", "b", {a:"b", b:"a"}]');
20 tryPostMessage('[1,2,3]');
21 tryPostMessage('[,,1]');
22 tryPostMessage('(function(){})', true, null, DOMException.DATA_CLONE_ERR);
23 tryPostMessage('var x = 0; try { eval("badref"); } catch(e) { x = e; } x', true, null, DOMException.DATA_CLONE_ERR);
24 tryPostMessage('new Date(1234567890000)');
25 tryPostMessage('new ConstructorWithPrototype("foo")', false, '({field:"foo"})');
26 tryPostMessage('new Boolean(true)');
27 tryPostMessage('new Boolean(false)');
28 tryPostMessage('new String("gnirts")');
29 tryPostMessage('new Number(42.0)');
30 cyclicObject={};
31 cyclicObject.self = cyclicObject;
32 tryPostMessage('cyclicObject', false, "cyclicObject");
33 cyclicArray=[];
34 cyclicArray[0] = cyclicArray;
35 tryPostMessage('cyclicArray', false, "cyclicArray");
36 objectGraph = {};
37 object = {};
38 objectGraph.graph1 = object;
39 objectGraph.graph2 = object;
40 tryPostMessage('objectGraph', false, "objectGraph");
41 arrayGraph = [object, object];
42 tryPostMessage('arrayGraph', false, "arrayGraph");
43 tryPostMessage('window', true);
44 tryPostMessage('({get a() { throw "x" }})', true);
45
46 if (window.eventSender) {
47     var fileInput = document.getElementById("fileInput");
48     var fileRect = fileInput.getClientRects()[0];
49     var targetX = fileRect.left + fileRect.width / 2;
50     var targetY = fileRect.top + fileRect.height / 2;
51     eventSender.beginDragWithFiles(['got-file-upload.html', 'got-file-upload-2.html']);
52     eventSender.mouseMoveTo(targetX, targetY);
53     eventSender.mouseUp();
54 }
55 var imageData = document.createElement("canvas").getContext("2d").createImageData(10,10);
56 for (var i = 0; i < imageData.data.length * 4; i++)
57     imageData.data[i] = i % 256;
58 var mutatedImageData = document.createElement("canvas").getContext("2d").createImageData(10,10);
59 for (var i = 0; i < imageData.data.length * 4; i++)
60     mutatedImageData.data[i] = i % 256;
61 tryPostMessage('imageData', false, imageData);
62 tryPostMessage('imageData.data', true, null, DOMException.DATA_CLONE_ERR)
63 tryPostMessage('mutatedImageData', false, imageData);
64 tryPostMessage('mutatedImageData.data', true, null, DOMException.DATA_CLONE_ERR)
65 for (var i = 0; i < imageData.data.length * 4; i++)
66     mutatedImageData.data[i] = 0;
67
68 function thunk(s) {
69     return "(function() {" + s + "})()";
70 }
71 tryPostMessage(thunk('return 42;'), false, '42');
72 tryPostMessage(thunk('return 42;'), false, thunk('return 40 + 2;'));
73 tryPostMessage(thunk('return 42;'), false, "evalThunk",
74                function(v) { doPassFail(v == 42, "evalThunk OK"); })
75
76 // Only enumerable properties should be serialized.
77 tryPostMessage(thunk('var o = {x:"hello"}; Object.defineProperty(o, "y", {value:"goodbye"}); return o;'), false, '({x:"hello"})');
78
79 // It's unclear what we should do if an accessor modifies an object out from under us
80 // while we're serializing it; the standard does mandate certain aspects about evaluation
81 // order, though, including that properties must be processed in their enumeration order.
82 tryPostMessage(thunk(
83         'var a = [0, 1, 2]; ' +
84         'var b = { get x() { a[0] = 40; a[2] = 42; a.push(43); return 41; }}; ' +
85         'a[1] = b; ' +
86         'return a;'
87     ), false, "evalThunk", function(v) {
88         doPassFail(v.length === 3, "length correct"); // undefined
89         doPassFail(v[0] === 0, "evaluation order OK"); // mandatory
90         doPassFail(v[1].x === 41, "evaluation order OK/accessor reached"); // mandatory
91         doPassFail(v[2] === 42, "evaluation order OK"); // mandatory
92     });
93
94 tryPostMessage(thunk(
95         'var a = [0, 1, 2]; ' +
96         'var b = { get x() { a.pop(); return 41; } }; ' +
97         'a[1] = b; ' +
98         'return a;'
99     ), false, "evalThunk", function(v) {
100         doPassFail(v.length === 3 || v.length === 2, "length correct"); // undefined
101         doPassFail(v[0] === 0, "index 0 OK"); // mandatory
102         doPassFail(v[1].x === 41, "accessor reached"); // mandatory
103         doPassFail(v[2] === undefined, "index 2 undefined"); // undefined
104     });
105
106 tryPostMessage(thunk(
107         'var a = [0, 1, 2]; ' +
108         'var b = { get x() { a.pop(); return 41; } }; ' +
109         'a[2] = b; ' +
110         'return a;'
111     ), false, "evalThunk", function(v) {
112         doPassFail(v.length === 3, "length correct"); // undefined
113         doPassFail(v[0] === 0, "index 0 OK"); // mandatory
114         doPassFail(v[1] === 1, "index 1 OK"); // mandatory
115         doPassFail(v[2].x === 41, "index 2 OK"); // undefined
116     });
117
118 // Now with objects! This is a little more tricky because the standard does not
119 // define an enumeration order.
120 tryPostMessage(thunk(
121         'var a = {p0: 0, p1: 1}; ' +
122         'Object.defineProperty(a, "p2", {get:function() {delete a.p3; return 42; }, enumerable: true, configurable: true}); ' +
123         'Object.defineProperty(a, "p3", {get:function() {delete a.p2; return 43; }, enumerable: true, configurable: true}); ' +
124         'Object.defineProperty(a, "p4", {get:function() { a.p5 = 45; return 44; }, enumerable: true, configurable: true}); ' +
125         'return a;'
126     ), false, "evalThunk", function(v) {
127         doPassFail(v.p0 === 0 && v.p1 === 1, "basic properties OK"); // mandatory
128         doPassFail(v.p2 === undefined && v.p3 !== undefined ||
129                    v.p2 !== undefined && v.p3 === undefined, "one accessor was run"); // mandatory
130         doPassFail(v.p2 !== undefined || Object.getOwnPropertyDescriptor(v, "p2") === undefined, "property was removed"); // undefined
131         doPassFail(v.p3 !== undefined || Object.getOwnPropertyDescriptor(v, "p3") === undefined, "property was removed"); // undefined
132         doPassFail(v.p4 === 44, "accessor was run"); // mandatory
133         doPassFail(Object.getOwnPropertyDescriptor(v, "p5") === undefined, "dynamic property not sent"); // undefined
134     });
135
136 // Objects returned from accessors should still be coalesced.
137 tryPostMessage(thunk(
138         'var obja = {get p() { return 42; }}; ' +
139         'var msg = {get a() { return obja; }, b: obja}; ' +
140         'return msg;'
141     ), false, "evalThunk", function(v) {
142         // Interestingly, the standard admits two answers here!
143         doPassFail(v.a === v.b, "reference equality preserved");
144         doPassFail((v.b.p === 42 && v.a.p === 42) ||
145             (v.b.p === null && v.a.p === null), "accessors used");
146     });
147 tryPostMessage(thunk(
148         'var obja = {get p() { return 42; }}; ' +
149         'var msg = {a: obja, get b() { return obja; }}; ' +
150         'return msg;'
151     ), false, "evalThunk", function(v) {
152         // Interestingly, the standard admits two answers here!
153         doPassFail(v.a === v.b, "reference equality preserved (opposite order)");
154         doPassFail((v.b.p === 42 && v.a.p === 42) ||
155                    (v.b.p === null && v.a.p === null), "accessors used (opposite order)");
156     });
157 // We should nullify the results from accessors more than one level deep,
158 // but leave other fields untouched.
159 tryPostMessage(thunk(
160         'var obja = {get p() { return 42; }, q: 43}; ' +
161         'return {get a() { return obja; }};'
162     ), false, "evalThunk", function(v) {
163         doPassFail(v.a.p === null, "accessor value was nullified");
164         doPassFail(v.a.q === 43, "non-accessor value was not nullified");
165     });
166 tryPostMessage(thunk(
167         'var objb = {get r() { return 44; }, t: 45}; ' +
168         'var obja = {get p() { return 42; }, q: 43, s: objb}; ' +
169         'return {get a() { return obja; }};'
170     ), false, "evalThunk", function(v) {
171         doPassFail(v.a.p === null, "accessor value was nullified");
172         doPassFail(v.a.q === 43, "non-accessor value was not nullified");
173         doPassFail(v.s !== null, "non-accessor value was not nullified");
174         doPassFail(v.a.s.r === null, "accessor value was nullified");
175         doPassFail(v.a.s.t === 45, "non-accessor value was not nullified");
176     });
177 tryPostMessage(thunk(
178         'var objb = {get r() { return 44; }, t: 45}; ' +
179         'var obja = {get p() { return 42; }, q: 43, s: [objb]}; ' +
180         'return {get c() { return 47; }, get a() { return obja; }, get b() { return 46; } };'
181     ), false, "evalThunk", function(v) {
182         doPassFail(v.b === 46, "accessor value was not nullified");
183         doPassFail(v.c === 47, "accessor value was not nullified");
184         doPassFail(v.a.p === null, "accessor value was nullified");
185         doPassFail(v.a.q === 43, "non-accessor value was not nullified");
186         doPassFail(v.a.s !== null, "non-accessor value was not nullified");
187         doPassFail(v.a.s !== undefined, "non-accessor value is defined");
188         doPassFail(v.a.s[0] !== null, "non-accessor value was not nullified");
189         doPassFail(v.a.s[0] !== undefined, "non-accessor value is defined");
190         doPassFail(v.a.s[0].r === null, "accessor value was nullified");
191         doPassFail(v.a.s[0].t === 45, "non-accessor value was not nullified");
192     });
193
194 // We need to pass out the exception raised from internal accessors.
195 tryPostMessage(thunk(
196         'return {get a() { throw "accessor-exn"; }};'
197     ), true, null, 'accessor-exn');
198 // We should still return the exception raised from nulled-out accessors.
199 tryPostMessage(thunk(
200         'var obja = {get p() { throw "accessor-exn"; }}; ' +
201         'return {get a() { return obja; }};'
202     ), true, null, 'accessor-exn');
203 // We should run the first nullified accessor, but no more.
204 tryPostMessage(thunk(
205         'window.bcalled = undefined; ' +
206         'window.acalled = undefined; ' +
207         'window.pcalled = undefined; ' +
208         'var objb = {get b() { window.bcalled = true; return 42; }}; ' +
209         'var obja = {get a() { window.acalled = true; return objb; }}; ' +
210         'return { get p() { window.pcalled = true; return obja; }};'
211     ), false, "evalThunk", function(v) {
212         doPassFail(v.p.a === null, "accessor value was nullified");
213         doPassFail(window.pcalled === true, "window.pcalled === true");
214         doPassFail(window.acalled === true, "window.acalled === true");
215         doPassFail(window.bcalled === undefined, "window.bcalled === undefined");
216     });
217
218 // Reference equality between Boolean objects must be maintained.
219 tryPostMessage(thunk(
220         'var t1 = new Boolean(true); ' +
221         'var t2 = new Boolean(true); ' +
222         'var f1 = new Boolean(false); ' +
223         'var f2 = new Boolean(false); ' +
224         'return [t1, t1, t2, f1, f1, f2];'
225     ), false, "evalThunk", function(v) {
226         doPassFail(equal(v[0], new Boolean(true)), "Boolean values correct (0)");
227         doPassFail(equal(v[3], new Boolean(false)), "Boolean values correct (3)");
228         doPassFail(equal(v[1], v[2]), "Boolean values correct (1,2)");
229         doPassFail(equal(v[4], v[5]), "Boolean values correct (4,5)");
230         doPassFail(v[0] === v[1], "References to Booleans correct (0,1)");
231         doPassFail(v[3] === v[4], "References to Booleans correct (3,4)");
232         doPassFail(v[0] !== v[2], "References to Booleans correct (0,2)");
233         doPassFail(v[3] !== v[5], "References to Booleans correct (3,5)");
234     });
235
236 // Reference equality between Number objects must be maintained.
237 tryPostMessage(thunk(
238         'var n1 = new Number(42.0); ' +
239         'var n2 = new Number(42.0); ' +
240         'return [n1, n1, n2];'
241     ), false, "evalThunk", function(v) {
242         doPassFail(equal(v[0], new Number(42.0)), "Number values correct (0)");
243         doPassFail(equal(v[0], v[2]), "Number values correct (0,2)");
244         doPassFail(v[0] === v[1], "References to numbers correct (0,1)");
245         doPassFail(v[0] !== v[2], "References to numbers correct (0,2)");
246     });
247
248 // Reference equality between String objects must be maintained.
249 tryPostMessage(thunk(
250         'var s1 = new String("gnirts"); ' +
251         'var s2 = new String("gnirts"); ' +
252         'return [s1, s1, s2];'
253     ), false, "evalThunk", function(v) {
254         doPassFail(equal(v[0], new String("gnirts")), "String values correct (0)");
255         doPassFail(equal(v[0], v[2]), "String values correct (0,2)");
256         doPassFail(v[0] === v[1], "References to strings correct (0,1)");
257         doPassFail(v[0] !== v[2], "References to strings correct (0,2)");
258     });
259
260 // Properties added to String, Boolean and Number objects should not be serialized.
261 tryPostMessage(thunk(
262         'var s = new String("gnirts"); ' +
263         'var n = new Number(42.0); ' +
264         'var b = new Boolean(true); ' +
265         's.foo = 1; n.foo = 2; b.foo = 3; ' +
266         'return [s, n, b];'
267     ), false, "evalThunk", function(v) {
268         doPassFail(v[0].foo == undefined, "String object properties not serialized");
269         doPassFail(v[1].foo == undefined, "Number object properties not serialized");
270         doPassFail(v[2].foo == undefined, "Boolean object properties not serialized");
271     });
272
273 // Reference equality between dates must be maintained.
274 tryPostMessage(thunk(
275         'var d1 = new Date(1,2,3); ' +
276         'var d2 = new Date(1,2,3); ' +
277         'return [d1,d1,d2];'
278     ), false, "evalThunk", function(v) {
279         doPassFail(equal(v[0], new Date(1,2,3)), "Date values correct (0)");
280         doPassFail(equal(v[0], v[2]), "Date values correct (1)");
281         doPassFail(v[0] === v[1], "References to dates correct (0)");
282         doPassFail(v[0] !== v[2], "References to dates correct (1)");
283     });
284
285 // Reference equality between regexps must be preserved.
286 tryPostMessage(thunk(
287         'var rx1 = new RegExp("foo"); ' +
288         'var rx2 = new RegExp("foo"); ' +
289         'var rx3 = new RegExp("foo", "gim"); ' +
290         'rx3.exec("foofoofoo"); ' +
291         'doPassFail(rx3.lastIndex === 3, "lastIndex initially correct: was " + rx3.lastIndex); ' +
292         'return [rx1,rx1,rx2,rx3];'
293     ), false, "evalThunk", function(v) {
294         doPassFail(v[0].source === "foo", "Regexp value correct (0)");
295         doPassFail(v[0] === v[1], "References to regexps correct (0)");
296         doPassFail(v[0] !== v[2], "References to regexps correct (1)");
297         doPassFail(v[0].global === false, "global set (0)");
298         doPassFail(v[3].global === true, "global set (1)");
299         doPassFail(v[0].ignoreCase === false, "ignoreCase set (0)");
300         doPassFail(v[3].ignoreCase === true, "ignoreCase set (1)");
301         doPassFail(v[0].multiline === false, "multiline set (0)");
302         doPassFail(v[3].multiline === true, "multiline set (1)");
303         doPassFail(v[0].lastIndex === 0, "lastIndex correct (0)");
304         doPassFail(v[3].lastIndex === 0, "lastIndex correct (1)");
305     });
306
307 if (window.eventSender) {
308     tryPostMessage(thunk(
309         'window.fileList = fileInput.files; ' +
310         'window.file0 = fileList[0]; ' +
311         'window.file1 = fileList[1]; ' +
312         'return window.fileList.length'), false, 2);
313     doPassFail(window.fileList[0] === window.file0, "sanity on file reference equality")
314     // The standard mandates that we do _not_ maintain reference equality for files in a transferred FileList.
315     tryPostMessage(thunk('return [window.file0, window.file0];'
316         ), false, "evalThunk", function(v) { doPassFail(v[0] === v[1], "file references transfer")});
317     tryPostMessage(thunk('return [window.fileList, window.file0];'
318         ), false, "evalThunk", function(v) { doPassFail(v[0][0] !== v[1], "FileList should not respect reference equality")});
319     tryPostMessage(thunk('return [window.file0, window.fileList];'
320         ), false, "evalThunk", function(v) { doPassFail(v[1][0] !== v[0], "FileList should not respect reference equality")});
321     tryPostMessage(thunk('return [window.fileList, window.fileList];'
322         ), false, "evalThunk", function(v) { doPassFail(v[0] === v[1], "FileList respects self-reference equality")});
323     tryPostMessage(thunk('return [window.fileList, window.file0, window.file1]'
324         ), false, "evalThunk", function(v) {
325             doPassFail(v[0].length === window.fileList.length, "FileList length sent correctly");
326             doPassFail(v[0][0] !== v[1], "FileList should not respect reference equality (0)");
327             doPassFail(v[0][1] !== v[2], "FileList should not respect reference equality (1)");
328             doPassFail(v[0][0].name == window.file0.name, "FileList preserves order and data (name0)");
329             doPassFail(v[0][1].name == window.file1.name, "FileList preserves order and data (name1)");
330             doPassFail(equal(v[0][0].lastModifiedDate, window.file0.lastModifiedDate), "FileList preserves order and data (date0)");
331             doPassFail(equal(v[0][1].lastModifiedDate, window.file1.lastModifiedDate), "FileList preserves order and data (date1)");
332         });
333 }
334 tryPostMessage('"done"');