dom/tests/mochitest/webcomponents/test_document_register_stack.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=783129
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for document.registerElement lifecycle callback</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a>
michael@0 13 <div id="container">
michael@0 14 </div>
michael@0 15 <script>
michael@0 16
michael@0 17 var container = document.getElementById("container");
michael@0 18
michael@0 19 // Test changing attributes in the created callback on an element
michael@0 20 // created after registration.
michael@0 21 function testChangeAttributeInCreatedCallback() {
michael@0 22 var createdCallbackCalled = false;
michael@0 23 var attributeChangedCallbackCalled = false;
michael@0 24
michael@0 25 var p = Object.create(HTMLElement.prototype);
michael@0 26 p.createdCallback = function() {
michael@0 27 is(createdCallbackCalled, false, "Created callback should be called before attached callback.");
michael@0 28 createdCallbackCalled = true;
michael@0 29 is(attributeChangedCallbackCalled, false, "Attribute changed callback should not have been called prior to setting the attribute.");
michael@0 30 this.setAttribute("foo", "bar");
michael@0 31 is(attributeChangedCallbackCalled, false, "While element is being created, element should not be added to the current element callback queue.");
michael@0 32 };
michael@0 33
michael@0 34 p.attributeChangedCallback = function(name, oldValue, newValue) {
michael@0 35 is(createdCallbackCalled, true, "attributeChanged callback should be called after the created callback because it was enqueued during created callback.");
michael@0 36 is(attributeChangedCallbackCalled, false, "attributeChanged callback should only be called once in this tests.");
michael@0 37 is(newValue, "bar", "The new value should be 'bar'");
michael@0 38 attributeChangedCallbackCalled = true;
michael@0 39 runNextTest();
michael@0 40 };
michael@0 41
michael@0 42 document.registerElement("x-one", { prototype: p });
michael@0 43 document.createElement("x-one");
michael@0 44 }
michael@0 45
michael@0 46 function testChangeAttributeInEnteredViewCallback() {
michael@0 47 var p = Object.create(HTMLElement.prototype);
michael@0 48 var attributeChangedCallbackCalled = false;
michael@0 49 var attachedCallbackCalled = false;
michael@0 50
michael@0 51 p.attachedCallback = function() {
michael@0 52 is(attachedCallbackCalled, false, "attached callback should be called only once in this test.");
michael@0 53 attachedCallbackCalled = true;
michael@0 54 is(attributeChangedCallbackCalled, false, "Attribute changed callback should not be called before changing attribute.");
michael@0 55 this.setAttribute("foo", "bar");
michael@0 56 is(attributeChangedCallbackCalled, true, "Transition from user-agent implementation to script should result in attribute changed callback being called.");
michael@0 57 runNextTest();
michael@0 58 };
michael@0 59
michael@0 60 p.attributeChangedCallback = function() {
michael@0 61 is(attachedCallbackCalled, true, "attached callback should have been called prior to attribute changed callback.");
michael@0 62 is(attributeChangedCallbackCalled, false, "attributeChanged callback should only be called once in this tests.");
michael@0 63 attributeChangedCallbackCalled = true;
michael@0 64 };
michael@0 65
michael@0 66 document.registerElement("x-two", { prototype: p });
michael@0 67 var elem = document.createElement("x-two");
michael@0 68
michael@0 69 var container = document.getElementById("container");
michael@0 70 container.appendChild(elem);
michael@0 71 }
michael@0 72
michael@0 73 function testLeaveViewInEnteredViewCallback() {
michael@0 74 var p = Object.create(HTMLElement.prototype);
michael@0 75 var attachedCallbackCalled = false;
michael@0 76 var detachedCallbackCalled = false;
michael@0 77 var container = document.getElementById("container");
michael@0 78
michael@0 79 p.attachedCallback = function() {
michael@0 80 is(this.parentNode, container, "Parent node should the container in which the node was appended.");
michael@0 81 is(attachedCallbackCalled, false, "attached callback should be called only once in this test.");
michael@0 82 attachedCallbackCalled = true;
michael@0 83 is(detachedCallbackCalled, false, "detached callback should not be called prior to removing element from document.");
michael@0 84 container.removeChild(this);
michael@0 85 is(detachedCallbackCalled, true, "Transition from user-agent implementation to script should run left view callback.");
michael@0 86 runNextTest();
michael@0 87 };
michael@0 88
michael@0 89 p.detachedCallback = function() {
michael@0 90 is(detachedCallbackCalled, false, "The detached callback should only be called once in this test.");
michael@0 91 is(attachedCallbackCalled, true, "The attached callback should be called prior to detached callback.");
michael@0 92 detachedCallbackCalled = true;
michael@0 93 };
michael@0 94
michael@0 95 document.registerElement("x-three", { prototype: p });
michael@0 96 var elem = document.createElement("x-three");
michael@0 97
michael@0 98 container.appendChild(elem);
michael@0 99 }
michael@0 100
michael@0 101 function testStackedAttributeChangedCallback() {
michael@0 102 var p = Object.create(HTMLElement.prototype);
michael@0 103 var attributeChangedCallbackCount = 0;
michael@0 104
michael@0 105 var attributeSequence = ["foo", "bar", "baz"];
michael@0 106
michael@0 107 p.attributeChangedCallback = function(attrName, oldValue, newValue) {
michael@0 108 if (newValue == "baz") {
michael@0 109 return;
michael@0 110 }
michael@0 111
michael@0 112 var nextAttribute = attributeSequence.shift();
michael@0 113 ok(true, nextAttribute);
michael@0 114 // Setting this attribute will call this function again, when
michael@0 115 // control returns to the script, the last attribute in the sequence should
michael@0 116 // be set on the element.
michael@0 117 this.setAttribute("foo", nextAttribute);
michael@0 118 is(this.getAttribute("foo"), "baz", "The last value in the sequence should be the value of the attribute.");
michael@0 119
michael@0 120 attributeChangedCallbackCount++;
michael@0 121 if (attributeChangedCallbackCount == 3) {
michael@0 122 runNextTest();
michael@0 123 }
michael@0 124 };
michael@0 125
michael@0 126 document.registerElement("x-four", { prototype: p });
michael@0 127 var elem = document.createElement("x-four");
michael@0 128 elem.setAttribute("foo", "changeme");
michael@0 129 }
michael@0 130
michael@0 131 var testFunctions = [
michael@0 132 testChangeAttributeInCreatedCallback,
michael@0 133 testChangeAttributeInEnteredViewCallback,
michael@0 134 testLeaveViewInEnteredViewCallback,
michael@0 135 testStackedAttributeChangedCallback,
michael@0 136 SimpleTest.finish
michael@0 137 ];
michael@0 138
michael@0 139 function runNextTest() {
michael@0 140 if (testFunctions.length > 0) {
michael@0 141 var nextTestFunction = testFunctions.shift();
michael@0 142 nextTestFunction();
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 SimpleTest.waitForExplicitFinish();
michael@0 147
michael@0 148 runNextTest();
michael@0 149
michael@0 150 </script>
michael@0 151 </body>
michael@0 152 </html>

mercurial