upgrade_tests.js (4311B)
1 function test_upgrade(f, msg) { 2 // Run upgrading test on an element created by HTML parser. 3 test_with_new_window(function (testWindow, testMsg) { 4 let elementParser = testWindow.document.querySelector("unresolved-element"); 5 f(testWindow, elementParser, testMsg); 6 }, msg + " (HTML parser)"); 7 8 // Run upgrading test on an element created by document.createElement. 9 test_with_new_window(function (testWindow, testMsg) { 10 let element = testWindow.document.createElement("unresolved-element"); 11 testWindow.document.documentElement.appendChild(element); 12 f(testWindow, element, testMsg); 13 }, msg + " (document.createElement)"); 14 } 15 16 // Test cases 17 18 test_upgrade(function (testWindow, testElement, msg) { 19 class MyCustomElement extends testWindow.HTMLElement {} 20 testWindow.customElements.define("unresolved-element", MyCustomElement); 21 SimpleTest.is( 22 Object.getPrototypeOf(Cu.waiveXrays(testElement)), 23 MyCustomElement.prototype, 24 msg 25 ); 26 }, "Custom element must be upgraded if there is a matching definition"); 27 28 test_upgrade(function (testWindow, testElement, msg) { 29 testElement.remove(); 30 class MyCustomElement extends testWindow.HTMLElement {} 31 testWindow.customElements.define("unresolved-element", MyCustomElement); 32 SimpleTest.is( 33 Object.getPrototypeOf(testElement), 34 testWindow.HTMLElement.prototype, 35 msg 36 ); 37 }, "Custom element must not be upgraded if it has been removed from tree"); 38 39 test_upgrade(function (testWindow, testElement, msg) { 40 let exceptionToThrow = { name: "exception thrown by a custom constructor" }; 41 class ThrowCustomElement extends testWindow.HTMLElement { 42 constructor() { 43 super(); 44 if (exceptionToThrow) { 45 throw exceptionToThrow; 46 } 47 } 48 } 49 50 let uncaughtError; 51 window.onerror = function (message, url, lineNumber, columnNumber, error) { 52 uncaughtError = error; 53 return true; 54 }; 55 testWindow.customElements.define("unresolved-element", ThrowCustomElement); 56 57 SimpleTest.is(uncaughtError.name, exceptionToThrow.name, msg); 58 }, "Upgrading must report an exception thrown by a custom element constructor"); 59 60 test_upgrade(function (testWindow, testElement, msg) { 61 class InstantiatesItselfAfterSuper extends testWindow.HTMLElement { 62 constructor(doNotCreateItself) { 63 super(); 64 if (!doNotCreateItself) { 65 new InstantiatesItselfAfterSuper(true); 66 } 67 } 68 } 69 70 let uncaughtError; 71 window.onerror = function (message, url, lineNumber, columnNumber, error) { 72 uncaughtError = error; 73 return true; 74 }; 75 testWindow.customElements.define( 76 "unresolved-element", 77 InstantiatesItselfAfterSuper 78 ); 79 80 SimpleTest.is(uncaughtError.name, "TypeError", msg); 81 }, "Upgrading must report an TypeError when the top of the " + "construction stack is marked AlreadyConstructed"); 82 83 test_upgrade( 84 function (testWindow, testElement, msg) { 85 class InstantiatesItselfBeforeSuper extends testWindow.HTMLElement { 86 constructor(doNotCreateItself) { 87 if (!doNotCreateItself) { 88 new InstantiatesItselfBeforeSuper(true); 89 } 90 super(); 91 } 92 } 93 94 let uncaughtError; 95 window.onerror = function (message, url, lineNumber, columnNumber, error) { 96 uncaughtError = error; 97 return true; 98 }; 99 testWindow.customElements.define( 100 "unresolved-element", 101 InstantiatesItselfBeforeSuper 102 ); 103 104 SimpleTest.is(uncaughtError.name, "TypeError", msg); 105 }, 106 "Upgrading must report an TypeError when the top of the " + 107 "construction stack is marked AlreadyConstructed due to a custom element " + 108 "constructor constructing itself before super() call" 109 ); 110 111 test_upgrade(function (testWindow, testElement, msg) { 112 class MyOtherElement extends testWindow.HTMLElement { 113 constructor() { 114 super(); 115 if (this == testElement) { 116 return testWindow.document.createElement("other-element"); 117 } 118 } 119 } 120 121 let uncaughtError; 122 window.onerror = function (message, url, lineNumber, columnNumber, error) { 123 uncaughtError = error; 124 return true; 125 }; 126 testWindow.customElements.define("unresolved-element", MyOtherElement); 127 128 SimpleTest.is(uncaughtError.name, "TypeError", msg); 129 }, "Upgrading must report an TypeError when the returned element is " + "not SameValue as the upgraded element");