1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/webcomponents/test_document_register_stack.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=783129 1.8 +--> 1.9 +<head> 1.10 + <title>Test for document.registerElement lifecycle callback</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a> 1.16 +<div id="container"> 1.17 +</div> 1.18 +<script> 1.19 + 1.20 +var container = document.getElementById("container"); 1.21 + 1.22 +// Test changing attributes in the created callback on an element 1.23 +// created after registration. 1.24 +function testChangeAttributeInCreatedCallback() { 1.25 + var createdCallbackCalled = false; 1.26 + var attributeChangedCallbackCalled = false; 1.27 + 1.28 + var p = Object.create(HTMLElement.prototype); 1.29 + p.createdCallback = function() { 1.30 + is(createdCallbackCalled, false, "Created callback should be called before attached callback."); 1.31 + createdCallbackCalled = true; 1.32 + is(attributeChangedCallbackCalled, false, "Attribute changed callback should not have been called prior to setting the attribute."); 1.33 + this.setAttribute("foo", "bar"); 1.34 + is(attributeChangedCallbackCalled, false, "While element is being created, element should not be added to the current element callback queue."); 1.35 + }; 1.36 + 1.37 + p.attributeChangedCallback = function(name, oldValue, newValue) { 1.38 + is(createdCallbackCalled, true, "attributeChanged callback should be called after the created callback because it was enqueued during created callback."); 1.39 + is(attributeChangedCallbackCalled, false, "attributeChanged callback should only be called once in this tests."); 1.40 + is(newValue, "bar", "The new value should be 'bar'"); 1.41 + attributeChangedCallbackCalled = true; 1.42 + runNextTest(); 1.43 + }; 1.44 + 1.45 + document.registerElement("x-one", { prototype: p }); 1.46 + document.createElement("x-one"); 1.47 +} 1.48 + 1.49 +function testChangeAttributeInEnteredViewCallback() { 1.50 + var p = Object.create(HTMLElement.prototype); 1.51 + var attributeChangedCallbackCalled = false; 1.52 + var attachedCallbackCalled = false; 1.53 + 1.54 + p.attachedCallback = function() { 1.55 + is(attachedCallbackCalled, false, "attached callback should be called only once in this test."); 1.56 + attachedCallbackCalled = true; 1.57 + is(attributeChangedCallbackCalled, false, "Attribute changed callback should not be called before changing attribute."); 1.58 + this.setAttribute("foo", "bar"); 1.59 + is(attributeChangedCallbackCalled, true, "Transition from user-agent implementation to script should result in attribute changed callback being called."); 1.60 + runNextTest(); 1.61 + }; 1.62 + 1.63 + p.attributeChangedCallback = function() { 1.64 + is(attachedCallbackCalled, true, "attached callback should have been called prior to attribute changed callback."); 1.65 + is(attributeChangedCallbackCalled, false, "attributeChanged callback should only be called once in this tests."); 1.66 + attributeChangedCallbackCalled = true; 1.67 + }; 1.68 + 1.69 + document.registerElement("x-two", { prototype: p }); 1.70 + var elem = document.createElement("x-two"); 1.71 + 1.72 + var container = document.getElementById("container"); 1.73 + container.appendChild(elem); 1.74 +} 1.75 + 1.76 +function testLeaveViewInEnteredViewCallback() { 1.77 + var p = Object.create(HTMLElement.prototype); 1.78 + var attachedCallbackCalled = false; 1.79 + var detachedCallbackCalled = false; 1.80 + var container = document.getElementById("container"); 1.81 + 1.82 + p.attachedCallback = function() { 1.83 + is(this.parentNode, container, "Parent node should the container in which the node was appended."); 1.84 + is(attachedCallbackCalled, false, "attached callback should be called only once in this test."); 1.85 + attachedCallbackCalled = true; 1.86 + is(detachedCallbackCalled, false, "detached callback should not be called prior to removing element from document."); 1.87 + container.removeChild(this); 1.88 + is(detachedCallbackCalled, true, "Transition from user-agent implementation to script should run left view callback."); 1.89 + runNextTest(); 1.90 + }; 1.91 + 1.92 + p.detachedCallback = function() { 1.93 + is(detachedCallbackCalled, false, "The detached callback should only be called once in this test."); 1.94 + is(attachedCallbackCalled, true, "The attached callback should be called prior to detached callback."); 1.95 + detachedCallbackCalled = true; 1.96 + }; 1.97 + 1.98 + document.registerElement("x-three", { prototype: p }); 1.99 + var elem = document.createElement("x-three"); 1.100 + 1.101 + container.appendChild(elem); 1.102 +} 1.103 + 1.104 +function testStackedAttributeChangedCallback() { 1.105 + var p = Object.create(HTMLElement.prototype); 1.106 + var attributeChangedCallbackCount = 0; 1.107 + 1.108 + var attributeSequence = ["foo", "bar", "baz"]; 1.109 + 1.110 + p.attributeChangedCallback = function(attrName, oldValue, newValue) { 1.111 + if (newValue == "baz") { 1.112 + return; 1.113 + } 1.114 + 1.115 + var nextAttribute = attributeSequence.shift(); 1.116 + ok(true, nextAttribute); 1.117 + // Setting this attribute will call this function again, when 1.118 + // control returns to the script, the last attribute in the sequence should 1.119 + // be set on the element. 1.120 + this.setAttribute("foo", nextAttribute); 1.121 + is(this.getAttribute("foo"), "baz", "The last value in the sequence should be the value of the attribute."); 1.122 + 1.123 + attributeChangedCallbackCount++; 1.124 + if (attributeChangedCallbackCount == 3) { 1.125 + runNextTest(); 1.126 + } 1.127 + }; 1.128 + 1.129 + document.registerElement("x-four", { prototype: p }); 1.130 + var elem = document.createElement("x-four"); 1.131 + elem.setAttribute("foo", "changeme"); 1.132 +} 1.133 + 1.134 +var testFunctions = [ 1.135 + testChangeAttributeInCreatedCallback, 1.136 + testChangeAttributeInEnteredViewCallback, 1.137 + testLeaveViewInEnteredViewCallback, 1.138 + testStackedAttributeChangedCallback, 1.139 + SimpleTest.finish 1.140 +]; 1.141 + 1.142 +function runNextTest() { 1.143 + if (testFunctions.length > 0) { 1.144 + var nextTestFunction = testFunctions.shift(); 1.145 + nextTestFunction(); 1.146 + } 1.147 +} 1.148 + 1.149 +SimpleTest.waitForExplicitFinish(); 1.150 + 1.151 +runNextTest(); 1.152 + 1.153 +</script> 1.154 +</body> 1.155 +</html>