Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / w3c / web-platform-tests / custom-elements / custom-element-lifecycle / types-of-callbacks / attached-callback-move-element-test.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Attached callback of a custom element should be called if element is moved</title>
5 <meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
6 <meta name="assert" content="attached callback ... must be enqueued whenever custom element is inserted into a document and this document has a browsing context.">
7 <link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
8 <script src="../../../../../resources/testharness.js"></script>
9 <script src="../../../../../resources/testharnessreport.js"></script>
10 <script src="../../testcommon.js"></script>
11 <link rel="stylesheet" href="../../../../../resources/testharness.css">
12 </head>
13 <body>
14 <div id="log"></div>
15 <script>
16 test(function() {
17     var doc = newHTMLDocument();
18     var proto = newHTMLElementPrototype();
19     doc.registerElement('x-a', {prototype: proto});
20     var customElement = doc.createElement('x-a');
21
22     doc.body.appendChild(customElement);
23     assert_equals(proto.attachedCallbackCalledCounter, 0, 'Callback attached ' +
24         'should not be called in documents that do not have a browsing context');
25
26     var divElement = doc.createElement('div');
27     doc.body.appendChild(divElement);
28     divElement.appendChild(customElement);
29     assert_equals(proto.attachedCallbackCalledCounter, 0, 'Callback attached ' +
30         'should not be called in documents that do not have a browsing context');
31 }, 'Test attached callback if moving custom element inside document ' +
32     'without browsing context');
33
34
35 testInIFrame('../../resources/blank.html', function(docWithBrowsingContext) {
36     var docNoBrowsingContext = newHTMLDocument();
37     var proto1 = newHTMLElementPrototype();
38     docNoBrowsingContext.registerElement('x-b', {prototype: proto1});
39
40     var customElement = docNoBrowsingContext.createElement('x-b');
41     docNoBrowsingContext.body.appendChild(customElement);
42     assert_equals(proto1.attachedCallbackCalledCounter, 0,
43         'Callback attached should not be called ' +
44         'in documents that do not have a browsing context');
45
46     var proto2 = newHTMLElementPrototype();
47     docWithBrowsingContext.registerElement('x-b', {prototype: proto2});
48     docWithBrowsingContext.body.appendChild(customElement);
49     assert_equals(proto1.attachedCallbackCalledCounter, 1,
50         'Callback attached should be called in documents with browsing context');
51     assert_equals(proto2.attachedCallbackCalledCounter, 0,
52         'Callback attached, defined in receiving document, should not be called');
53 }, 'Test attached callback if moving custom element from ' +
54     'document without browsing context to document with browsing context');
55
56
57 testInIFrame('../../resources/blank.html', function(docWithBrowsingContext) {
58     var proto1 = newHTMLElementPrototype();
59     docWithBrowsingContext.registerElement('x-c', {prototype: proto1});
60
61     var customElement = docWithBrowsingContext.createElement('x-c');
62     docWithBrowsingContext.body.appendChild(customElement);
63     assert_equals(proto1.attachedCallbackCalledCounter, 1,
64         'Callback attached should be called in documents with browsing context');
65
66     var docNoBrowsingContext = newHTMLDocument();
67     var proto2 = newHTMLElementPrototype();
68     docNoBrowsingContext.registerElement('x-c', {prototype: proto2});
69     docNoBrowsingContext.body.appendChild(customElement);
70     assert_equals(proto1.attachedCallbackCalledCounter, 1, 'Callback attached should not be called ' +
71         'in documents that do not have a browsing context');
72     assert_equals(proto2.attachedCallbackCalledCounter, 0,
73         'Callback attached, defined in receiving document, should not be called');
74 }, 'Test attached callback if moving custom element from ' +
75     'document with browsing context to document without browsing context');
76
77
78 testInIFrame('../../resources/blank.html', function(doc) {
79     var proto = newHTMLElementPrototype();
80     doc.registerElement('x-d', {prototype: proto});
81
82     var customElement = doc.createElement('x-d');
83     doc.body.appendChild(customElement);
84     assert_equals(proto.attachedCallbackCalledCounter, 1,
85         'Callback attached should be called in documents with browsing context');
86
87     var divElement = doc.createElement('div');
88     doc.body.appendChild(divElement);
89     divElement.appendChild(customElement);
90     assert_equals(proto.attachedCallbackCalledCounter, 2,
91         'Callback attached should be called in documents with browsing context');
92 }, 'Test attached callback if moving custom element inside document ' +
93     'with browsing context');
94
95
96 testInIFrame('../../resources/blank.html', function(doc) {
97     var proto = newHTMLElementPrototype();
98     doc.registerElement('x-e', {prototype: proto});
99
100     var customElement = doc.createElement('x-e');
101     doc.body.appendChild(customElement);
102     assert_equals(proto.attachedCallbackCalledCounter, 1,
103         'Callback attached should be called in documents with browsing context');
104
105     var divElement = doc.createElement('div');
106     divElement.appendChild(customElement);
107     assert_equals(proto.attachedCallbackCalledCounter, 1,
108         'Callback attached should not be called if element is not appended to the document');
109
110     doc.body.appendChild(divElement);
111     assert_equals(proto.attachedCallbackCalledCounter, 2,
112         'Callback attached should be called in documents with browsing context');
113 }, 'Test attached callback if indirectly moving custom element inside document ' +
114     'with browsing context');
115
116
117 var moveTest =  async_test('Test attached callback if moving custom element from ' +
118     'document with browsing context to document with browsing context');
119
120 moveTest.step(function() {
121     var iframe1 = newIFrame('../../resources/blank.html');
122     iframe1.onload = moveTest.step_func(function() {
123         var doc1 = iframe1.contentDocument;
124
125         // register custom element type
126         var proto1 = newHTMLElementPrototype();
127         doc1.registerElement('x-f', {prototype: proto1});
128
129         // create custom element
130         var customElement = doc1.createElement('x-f');
131         doc1.body.appendChild(customElement);
132         assert_equals(proto1.attachedCallbackCalledCounter, 1,
133             'Callback attached should be called in documents with browsing context');
134
135         // create second iframe
136         var iframe2 = newIFrame('../../resources/x-element.html');
137         iframe2.onload = moveTest.step_func(function() {
138             var doc2 = iframe2.contentDocument;
139             
140             // register custom element type
141             var proto2 = newHTMLElementPrototype();
142             doc2.registerElement('x-f', {prototype: proto2});
143
144             // move element
145             doc2.body.appendChild(customElement);
146             assert_equals(proto1.attachedCallbackCalledCounter, 2,
147                 'Callback attached should be called in documents with browsing context');
148             assert_equals(proto2.attachedCallbackCalledCounter, 0,
149                 'Callback attached, defined in receiving document, should not be called');
150
151             // test clean up
152             iframe1.remove();
153             iframe2.remove();
154             moveTest.done();
155         });
156     });
157 });
158 </script>
159 </body>
160 </html>