Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / scroll-event-handler-count.html
blobf17219a6112ed2ef0452b7380234702e96a8a92f
1 <div id="scrolltarget">
2 <script src="../../resources/js-test.js"></script>
3 <script>
4 description('This test checks that we correctly update the scroll event handler count as event handlers are added and removed');
5 (function() {
6 // Test addEventListener/removeEventListener on the document.
7 var listener = function() { }
9 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
10 document.addEventListener('scroll', listener, true);
11 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
12 document.addEventListener('scroll', listener, false);
13 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
14 document.removeEventListener('scroll', listener, true);
15 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
17 // Try removing the capturing listener again.
18 document.removeEventListener('scroll', listener, true);
19 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
21 document.removeEventListener('scroll', listener, false);
22 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
23 })();
25 debug('Test setting onscroll on the document.');
26 (function() {
27 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
28 document.onscroll = function() { }
29 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
30 document.onscroll = function() { }
31 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
32 document.onscroll = null;
33 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
34 })();
36 debug('Test that nested Documents\' scroll handlers are properly tracked in their parent Document.');
37 (function() {
38 var iframe = document.createElement('iframe');
39 var scrolltarget = document.getElementById('scrolltarget');
40 scrolltarget.onscroll = function() {};
42 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
44 scrolltarget.appendChild(iframe);
46 nestedDocument = iframe.contentWindow.document;
47 nestedDocument.open('text/html', 'replace');
48 nestedDocument.write('<!DOCTYPE html>\n<script>\ndocument.onscroll=function(){};\n</' + 'script>\n');
49 shouldBe('window.internals.scrollEventHandlerCount(nestedDocument)', '2');
50 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
52 nestedDocument.write('<script>document.onscroll=undefined</' + 'script>\n');
53 shouldBe('window.internals.scrollEventHandlerCount(nestedDocument)', '1');
54 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
56 nestedDocument.close();
58 scrolltarget.removeChild(iframe);
59 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
60 scrolltarget.onscroll = undefined;
61 })();
63 debug('Test that detaching a nested Document with handlers works properly.');
64 (function() {
65 var iframe = document.createElement('iframe');
66 var scrolltarget = document.getElementById('scrolltarget');
68 scrolltarget.appendChild(iframe);
70 nestedDocument = iframe.contentWindow.document;
71 nestedDocument.open('text/html', 'replace');
72 nestedDocument.write('<!DOCTYPE html>\n<script>\ndocument.onscroll=function(){};\n' +
73 'window.onscroll=function(){};</' + 'script>\n');
74 shouldBe('window.internals.scrollEventHandlerCount(nestedDocument)', '2');
75 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
77 nestedDocument.close();
78 scrolltarget.removeChild(iframe);
79 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
80 })();
82 debug('Test moving event listeners from an unattached document to an attached one');
83 (function() {
84 doc = document.implementation.createHTMLDocument('');
85 var div = doc.createElement('div');
86 var childDiv = doc.createElement('div');
88 div.addEventListener('scroll', function() { });
89 childDiv.addEventListener('scroll', function() { });
90 div.appendChild(childDiv);
92 // Since we only track event handlers on documents that attached to a page,
93 // |doc| should not have any registered handlers at this point.
94 shouldBe('window.internals.scrollEventHandlerCount(doc)', '0');
95 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
97 // Move the top level div into the current document. Both event handlers should
98 // get registered.
99 document.body.appendChild(div);
100 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
102 // Removing the div from the document does not affect the event handler count.
103 document.body.removeChild(div);
104 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
106 // Once the divs are destroyed the handlers go away.
107 div = null;
108 childDiv = null;
109 doc = null;
110 gc();
111 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
112 })();
114 debug('Test moving event listeners from an attached document to an unattached one');
115 (function() {
116 var div = document.createElement('div');
117 div.addEventListener('scroll', function() { });
118 document.body.appendChild(div);
120 var iframe = document.createElement('iframe');
121 div.appendChild(iframe);
122 var nestedDocument = iframe.contentWindow.document;
123 nestedDocument.open('text/html', 'replace');
124 nestedDocument.write('<!DOCTYPE html>\n<script>\ndocument.onscroll=function(){};\n' +
125 'window.onscroll=function(){};</' + 'script>\n');
126 nestedDocument.close();
128 shouldBe('window.internals.scrollEventHandlerCount(document)', '3');
130 var unattachedDoc = document.implementation.createHTMLDocument('');
131 unattachedDoc.body.appendChild(div);
132 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
133 })();
135 debug('Test moving a scroll event listener between documents belonging to the same page');
136 (function() {
137 var iframe = document.createElement('iframe');
138 document.body.appendChild(iframe);
139 var nestedDocument = iframe.contentWindow.document;
140 nestedDocument.open('text/html', 'replace');
141 nestedDocument.write('<!DOCTYPE html><div id=foo></div>');
142 nestedDocument.close();
144 var element = frames[0].document.getElementById('foo');
145 var listener = function() { }
146 element.addEventListener('scroll', listener, false);
147 frames[0].window.addEventListener('scroll', listener, false);
148 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
150 document.body.appendChild(element);
151 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
153 element.removeEventListener('scroll', listener, false);
154 frames[0].window.removeEventListener('scroll', listener, false);
155 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
156 })();
158 debug('Test addEventListener/removeEventListener on the window.');
159 (function() {
160 var listener = function() { }
162 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
163 window.addEventListener('scroll', listener, true);
164 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
165 window.addEventListener('scroll', listener, false);
166 shouldBe('window.internals.scrollEventHandlerCount(document)', '2');
167 window.removeEventListener('scroll', listener, true);
168 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
170 // Try removing the capturing listener again.
171 window.removeEventListener('scroll', listener, true);
172 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
174 window.removeEventListener('scroll', listener, false);
175 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
176 })();
178 debug('Test setting onscroll on the window.');
179 (function() {
180 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
181 window.onscroll = function() { }
182 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
183 window.onscroll = function() { }
184 shouldBe('window.internals.scrollEventHandlerCount(document)', '1');
185 window.onscroll = null;
186 shouldBe('window.internals.scrollEventHandlerCount(document)', '0');
187 })();
188 </script>
189 </body>