test_window_indexing.html (4932B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=823228 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 823228</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=823228">Mozilla Bug 823228</a> 14 <p id="display"></p> 15 <div id="content" style="display: none"> 16 <iframe name="x" id="x"></iframe> 17 <iframe name="y" id="y"></iframe> 18 </div> 19 <pre id="test"> 20 </pre> 21 <script type="application/javascript"> 22 23 /** Test for Bug 823228 */ 24 is(window, window.frames, "These better be equal"); 25 ok("0" in window, "We have a subframe"); 26 ok("1" in window, "We have two subframes"); 27 ok(!("2" in window), "But we don't have three subframes"); 28 window[2] = "myString"; 29 ok(!("2" in window), "Should not be able to set indexed expando"); 30 Object.getPrototypeOf(window)[3] = "Hey there"; 31 ok("3" in window, "Should be walking up the proto chain"); 32 33 is(window[0].name, "x", "First frame is x"); 34 is(window[1].name, "y", "Second frame is y"); 35 is(window[2], undefined, "We should still not have our expando"); 36 is(window[3], "Hey there", "We should still have our prop on the proto chain"); 37 38 var x = $("x"); 39 var y = $("y"); 40 41 is(x.contentWindow, window[0], "First frame should have correct window"); 42 is(y.contentWindow, window[1], "Second frame should have correct window"); 43 44 // set() hook test 45 throws(TypeError, function () { 46 "use strict"; 47 window[1] = "FAIL strict"; 48 }); 49 50 // set() hook test 51 window[1] = "FAIL"; 52 is(window[1].name, "y", "Second frame is still y"); 53 y.remove(); 54 ok(!("1" in window), "We no longer have two subframes"); 55 is(window[1], undefined, "We should not have a value here"); 56 57 // defineProperty() hook test 58 function throws(errorCtor, f) { 59 try { 60 f(); 61 } catch (exc) { 62 if (!(exc instanceof errorCtor)) 63 throw exc; 64 return; 65 } 66 throw new Error("expected " + errCtor.name + ", no exception thrown: " + f); 67 } 68 69 x.parentNode.appendChild(y); 70 throws(TypeError, function () { 71 Object.defineProperty(window, "1", { value: "FAIL2", configurable: true, 72 writable: true }); 73 }); 74 y.remove(); 75 ok(!("1" in window), "We no longer have two subframes, again"); 76 is(window[1], undefined, "We should not have a value here either"); 77 78 // More set() hook test 79 throws(TypeError, function () { 80 "use strict"; 81 window[1] = "FAIL3 strict"; 82 }); 83 window[1] = "FAIL3"; 84 ok(!("1" in window), "We shouldn't allow indexed expandos"); 85 is(window[1], undefined, "We should not have a value for an indexed expando"); 86 var desc = Object.getOwnPropertyDescriptor(window, "1"); 87 is(desc, undefined, "We really really shouldn't have indexed expandos"); 88 89 x.parentNode.appendChild(y); 90 is(window[1], y.contentWindow, "Second frame should now be visible"); 91 desc = Object.getOwnPropertyDescriptor(window, "1"); 92 ok(desc.configurable, "Subframe should be configurable"); 93 ok(desc.enumerable, "Subframe should be configurable"); 94 ok(!desc.writable, "Subframe should not be writable"); 95 is(desc.value, y.contentWindow, "Subframe should have correct value"); 96 97 y.remove(); 98 is(window[1], undefined, "And now we should be back to no [1] property"); 99 100 // And more defineProperty() 101 throws(TypeError, function () { 102 Object.defineProperty(window, "1", { value: "FAIL2", configurable: true, 103 writable: true }); 104 }); 105 ok(!("1" in window), "Defining indexed properties really just shouldn't work"); 106 is(window[1], undefined, "Defining past end of list should not work"); 107 108 // Enumeration tests 109 x.parentNode.appendChild(y); 110 111 var names = Object.getOwnPropertyNames(window); 112 is(names[0], "0", "Must start with 0"); 113 is(names[1], "1", "Must continue with 1"); 114 is(names.indexOf("2"), -1, "And 2, an attempted expando, should not be in there"); 115 is(names.indexOf("3"), -1, "But no 3; that's on the proto"); 116 117 names = []; 118 for (var name in window) { 119 names.push(name); 120 } 121 is(names[0], "0", "Enumeration must start with 0"); 122 is(names[1], "1", "Enumeration must continue with 1"); 123 is(names.indexOf("2"), -1, "Enumeration: but no expando 2"); 124 isnot(names.indexOf("3"), -1, "Enumeration: and then 3, defined on the proto"); 125 is(names.indexOf("4"), -1, "But no 4 around"); 126 127 // Delete tests 128 is(delete window[1], false, "Deleting supported index should return false"); 129 is(window[1], y.contentWindow, "Shouldn't be able to delete a supported index"); 130 y.remove(); 131 is(window[1], undefined, 132 "And now we should have no property here"); 133 is(delete window[1], true, "Deleting unsupported index should return true"); 134 is(window[1], undefined, 135 "And we shouldn't have things magically appear due to delete"); 136 </script> 137 </body> 138 </html>