Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / custom / entered-left-document.html
blob362661ad84f82327ef9c88968c81a091ac5573e5
1 <!DOCTYPE html>
2 <script src="../../../resources/testharness.js"></script>
3 <script src="../../../resources/testharnessreport.js"></script>
4 <script src="test-harness-utils.js"></script>
5 <body>
6 <script>
7 (function () {
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();
16 var invocations = [];
17 function log(msg) {
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});
30 var a = new A();
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 ' +
36 'callback');
38 // Entered document without a view
40 invocations = [];
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
55 invocations = [];
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
63 invocations = [];
64 a.remove();
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
71 invocations = [];
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 ' +
76 'view');
78 // Created in Shadow DOM that is not in a document
80 var div = docB.createElement('div');
81 var s = div.createShadowRoot();
82 invocations = [];
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
90 invocations = [];
91 s.innerHTML = '';
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>';
99 invocations = [];
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 ' +
104 'Shadow DOM');
106 // Leaves a document with a view as a constituent of Shadow DOM
108 invocations = [];
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
116 invocations = [];
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
125 invocations = [];
126 div.innerHTML = '';
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>';
134 invocations = [];
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 ' +
139 'a subtree');
141 frame.remove();
142 t.done();
143 }));
144 })();
145 </script>