2 <script src=
"../../../resources/testharness.js"></script>
3 <script src=
"../../../resources/testharnessreport.js"></script>
4 <script src=
"test-harness-utils.js"></script>
8 var t
= async_test('entered, left callbacks should only be invoked when ' +
9 'entering or leaving a document with a view');
10 withFrame(t
.step_func(function (frame
) {
11 // Set up a definition
13 var docA
= frame
.contentDocument
;
14 var docB
= docA
.implementation
.createHTMLDocument();
18 return function () { invocations
.push(msg
); };
21 var proto
= Object
.create(frame
.contentWindow
.HTMLElement
.prototype);
22 proto
.createdCallback
= log('created');
23 proto
.attributeChangedCallback
= log('attribute changed');
24 proto
.attachedCallback
= log('entered');
25 proto
.detachedCallback
= log('left');
27 // Created, owned by a document without a view
29 var A
= docB
.registerElement('x-a', {prototype: proto
});
31 assert_equals(a
.ownerDocument
, docB
,
32 'new instance should be owned by the document the ' +
33 'definition was registered with');
34 assert_array_equals(invocations
, ['created'],
35 'calling the constructor should invoke the created ' +
38 // Entered document without a view
41 docB
.body
.appendChild(a
);
42 assert_array_equals(invocations
, [],
43 'entered callback should not be invoked when ' +
44 'entering a document without a view');
46 // Attribute changed in document without a view
48 a
.setAttribute('data-foo', 'bar');
49 assert_array_equals(invocations
, ['attribute changed'],
50 'changing an attribute should invoke the callback, ' +
51 'even in a document without a view');
53 // Entered document with a view
56 docA
.body
.appendChild(a
);
57 assert_array_equals(invocations
, ['entered'],
58 'entered callback should be invoked when entering ' +
59 'a document with a view');
61 // Left document with a view
65 assert_array_equals(invocations
, ['left'],
66 'left callback should be invoked when leaving a ' +
67 'document with a view');
69 // Created in a document without a view
72 docB
.body
.innerHTML
= '<x-a></x-a>';
73 assert_array_equals(invocations
, ['created'],
74 'only created callback should be invoked when ' +
75 'parsing a custom element in a document without a ' +
78 // Created in Shadow DOM that is not in a document
80 var div
= docB
.createElement('div');
81 var s
= div
.createShadowRoot();
83 s
.innerHTML
= '<x-a></x-a>';
84 assert_array_equals(invocations
, ['created'],
85 'the entered callback should not be invoked when ' +
86 'entering a Shadow DOM subtree not in the document');
88 // Leaves Shadow DOM that is not in a document
92 assert_array_equals(invocations
, [],
93 'the left callback should not be invoked when ' +
94 'leaving a Shadow DOM subtree not in the document');
96 // Enters a document with a view as a constituent of Shadow DOM
98 s
.innerHTML
= '<x-a></x-a>';
100 docA
.body
.appendChild(div
);
101 assert_array_equals(invocations
, ['entered'],
102 'the entered callback should be invoked when ' +
103 'inserted into a document with a view as part of ' +
106 // Leaves a document with a view as a constituent of Shadow DOM
109 docB
.body
.appendChild(div
);
110 assert_array_equals(invocations
, ['left'],
111 'the left callback should be invoked when removed ' +
112 'from a document with a view as part of Shadow DOM');
114 // Enters a disconnected subtree of DOM
117 div
= docA
.createElement('div');
118 div
.innerHTML
= '<x-a></x-a>';
119 assert_array_equals(invocations
, ['created'],
120 'the entered callback should not be invoked when ' +
121 'inserted into a disconnected subtree');
123 // Leaves a disconnected subtree of DOM
127 assert_array_equals(invocations
, [],
128 'the left callback should not be invoked when ' +
129 'removed from a disconnected subtree');
131 // Enters a document with a view as a constituent of a subtree
133 div
.innerHTML
= '<x-a></x-a>';
135 docA
.body
.appendChild(div
);
136 assert_array_equals(invocations
, ['entered'],
137 'the entered callback should be invoked when ' +
138 'inserted into a document with a view as part of ' +