4 <div id=
"description"></div>
5 <div id=
"console"></div>
6 <input type=
"file" id=
"fileInput"></input>
8 if (window
.testRunner
) {
9 testRunner
.dumpAsText();
10 testRunner
.waitUntilDone();
13 var console
= document
.getElementById("console");
17 function equal(actual
, expected
)
19 if (typeof actual
!== typeof expected
)
21 if (actual
=== expected
)
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();
28 if (Array
.isArray(actual
) || Array
.isArray(expected
)) {
29 if (!Array
.isArray(actual
) || !Array
.isArray(expected
))
31 if (actual
.length
!= expected
.length
)
33 for (var i
= 0; i
< actual
.length
; i
++) {
34 if ((i
in actual
) ^ (i
in expected
))
36 if (!equal(actual
[i
], expected
[i
]))
41 if (actual
.constructor !== expected
.constructor)
43 var keys
= Object
.keys(actual
);
44 if (!equal(keys
, Object
.keys(expected
)))
46 for (var i
= 0; i
< keys
.length
; i
++) {
47 if (!equal(actual
[keys
[i
]], expected
[keys
[i
]]))
53 function safeToString(o
) {
54 if (o
instanceof Date
)
55 return o
.toISOString();
56 if (typeof o
!== "object" || !o
)
61 return Object
.prototype.toString
.call(o
) + "(default toString threw "+e
+")";
65 function shouldBe(actual
, expected
)
67 var actualValue
= eval(actual
);
68 var expectedValue
= eval(expected
);
69 if (equal(actualValue
, expectedValue
))
70 console
.innerHTML
+= "PASS: " + actual
+ " is " + safeToString(expectedValue
) + " of type " + typeof actualValue
+ "<br>";
72 console
.innerHTML
+= "FAIL: " + actual
+ " is " + actualValue
+ " should be " + expectedValue
+ " of type " + typeof expectedValue
+ "<br>";
75 function onmessage(evt
) {
76 evt
.foo
= "bar"; // add a property.
78 shouldBe("eventData", messages
[0]);
79 evt
.func = function(){}; // add a function property.
81 shouldBe("eventData", messages
[0]);
82 evt
.func
= ""; // despecialise a function property.
84 shouldBe("eventData", messages
[0]);
85 for (var i
= 0; i
< 1000; i
++) // To dictionary transition
88 shouldBe("eventData", messages
[0]);
89 delete evt
.foo
; // To uncachable dictionary
91 shouldBe("eventData", messages
[0]);
93 if (safeToString(evt
.data
) == 'done' && window
.testRunner
)
94 testRunner
.notifyDone();
97 window
.addEventListener('message', onmessage
, false);
99 function tryPostMessage(message
, shouldThrow
, expected
) {
101 var value
= eval(message
);
102 postMessage(value
, "*");
104 console
.innerHTML
+= "FAIL: 'postMessage("+message
+")' should throw but didn't<br>";
105 messages
.push(expected
|| message
);
108 console
.innerHTML
+= "PASS: 'postMessage("+message
+")' threw " + e
+ "<br>";
110 console
.innerHTML
+= "FAIL: 'postMessage("+message
+")' should not throw but threw " + e
+ "<br>";
114 document
.getElementById("description").innerHTML
= "Tests that we clone object hierarchies";
116 tryPostMessage('null');
117 tryPostMessage('undefined');
119 tryPostMessage('true');
120 tryPostMessage('"1"');
121 tryPostMessage('({})');
122 tryPostMessage('({a:1})');
123 tryPostMessage('[]');
124 tryPostMessage('[1,2,3]');
125 tryPostMessage('[,,1]');
126 tryPostMessage('(function(){})', true);
127 tryPostMessage('new Date(1234567890000)');
128 tryPostMessage('"done"');