Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / selectionchange-iframe.html
blob5a8eb37c5033315ce51aadc2fc96301c5011bf8c
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <p>This tests that changing selection in an iframe does not fire selectionchange event in the parent document and vice versa.</p>
5 <div id="container">
6 <iframe id="child" src="about:blank"></iframe>
7 <div id="parent">WebKit rocks</div>
8 </div>
9 <ul id="console">
10 </ul>
11 <script>
13 var parent = document.getElementById('parent');
14 var child = document.getElementById('child');
15 var childDocument = (child.contentWindow || child.contentDocument).document;
16 if (!childDocument.body)
17 childDocument.appendChild(document.createElement('body'));
18 childDocument.body.innerHTML = 'hello world';
20 var selectionOfParent = window.getSelection();
21 var selectionOfChild = child.contentDocument.getSelection();
23 tests = [
25 selector: function() {
26 selectionOfParent.selectAllChildren(document.body);
27 return "all of parent's document body";
29 expected: 'parent',
30 alt_expected: null,
33 selector: function() {
34 selectionOfChild.selectAllChildren(childDocument.body);
35 return "all of parent's document body";
37 expected: 'child',
38 alt_expected: null,
41 selector: function() {
42 var range = document.createRange();
43 range.setStart(parent.firstChild, 7);
44 range.setEnd(parent.firstChild, 12);
45 selectionOfParent.removeAllRanges();
46 selectionOfParent.addRange(range);
47 return '"rocks" of "WebKit rocks" in the parent';
49 expected: 'parent, parent',
50 alt_expected: 'parent',
53 selector: function() {
54 var range = childDocument.createRange();
55 range.setStart(childDocument.body.firstChild, 6);
56 range.setEnd(childDocument.body.firstChild, 11);
57 selectionOfChild.removeAllRanges();
58 selectionOfChild.addRange(range);
59 return '"world" of "hello world" in the child';
61 expected: 'child, child',
62 alt_expected: 'child',
65 var i = 0;
66 var selected = null;
68 if (window.testRunner) {
69 testRunner.dumpAsText();
70 testRunner.waitUntilDone();
73 var log = [];
75 function logger(event) {
76 if (event.target == childDocument)
77 log.push('child');
78 else if (event.target == document)
79 log.push('parent');
80 else
81 log.push('unknown');
84 function verify(selected, expected, alt_expected) {
85 setTimeout(function () {
86 var actual = log.length ? '' : 'none';
87 for (var i = 0; i < log.length; i++) {
88 if (actual.length)
89 actual += ', ';
90 actual += log[i];
92 var result = 'selecting ' + selected + ' fired selection change events from ' + actual;
93 if (actual == expected || actual == alt_expected)
94 result = 'PASS: ' + result;
95 else
96 result = 'FAIL: ' + result + ' but expected ' + expected + (alt_expected ? ' or ' + alt_expected : '');
97 var console = document.getElementById('console');
98 var listItem = document.createElement('li');
99 console.appendChild(listItem);
100 listItem.appendChild(document.createTextNode(result));
101 }, 0);
104 // selects and verify in turn until all tests are ran
105 function timer() {
106 if (!selected) {
107 log = [];
108 selected = tests[i].selector();
109 } else {
110 verify(selected, tests[i].expected, tests[i].alt_expected);
111 selected = null;
112 i++;
113 if (i == tests.length) {
114 if (window.testRunner)
115 document.getElementById('container').style.display = 'none';
116 testRunner.notifyDone();
117 return;
120 setTimeout(timer, 0);
123 document.addEventListener('selectionchange', logger, false);
124 childDocument.onselectionchange = logger;
126 timer();
128 </script>
129 </body>
130 </html>