|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test nsIDOMWindowUtils</title> |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"> |
|
8 <style> |
|
9 html, body, div { |
|
10 padding: 0; |
|
11 margin: 0; |
|
12 } |
|
13 |
|
14 div.test { |
|
15 position: absolute; |
|
16 height: 10px; |
|
17 width: 10px; |
|
18 } |
|
19 </style> |
|
20 </head> |
|
21 |
|
22 <body id="body"> |
|
23 |
|
24 <div class="test" id="onscreen" style="top: 100px; background-color: red;"></div> |
|
25 <div class="test" id="offscreen" style="top: 100000px; background-color: green;"></div> |
|
26 |
|
27 <script type="application/javascript;version=1.8"> |
|
28 |
|
29 SimpleTest.waitForExplicitFinish(); |
|
30 |
|
31 var domWindowUtils = SpecialPowers.getDOMWindowUtils(window); |
|
32 |
|
33 var gTests = [ |
|
34 /* |
|
35 nsIDOMElement elementFromPoint(in long aX, |
|
36 in long aY, |
|
37 in boolean aIgnoreRootScrollFrame, |
|
38 in boolean aFlushLayout); |
|
39 */ |
|
40 function testElementFromPoint() { |
|
41 let onscreen = document.getElementById("onscreen"); |
|
42 let offscreen = document.getElementById("offscreen"); |
|
43 let htmldoc = document.documentElement; |
|
44 ok(onscreen, "on screen element exists"); |
|
45 ok(offscreen, "off screen element exists"); |
|
46 ok(htmldoc, "htmldoc element exists"); |
|
47 |
|
48 let testData = [ |
|
49 // default behavior is to return null for items outside the viewport |
|
50 [[0, 100], null, onscreen], |
|
51 [[9, 109], null, onscreen], |
|
52 [[0, 100000], null, null], |
|
53 [[9, 100009], null, null], |
|
54 |
|
55 // ignore scroll frame |
|
56 [[0, 100, true, false], null, onscreen], |
|
57 [[9, 109, true, false], null, onscreen], |
|
58 [[0, 100000, true, false], null, offscreen], |
|
59 [[9, 100009, true, false], null, offscreen], |
|
60 |
|
61 // layout flush tests |
|
62 // test moving element 10px to the left and down, and flushing layout |
|
63 [[10, 110, false, true], [[10, 110], onscreen], onscreen], |
|
64 // test moving element back, not flushing layout |
|
65 // (will get the html document instead) |
|
66 [[0, 100, false, false], [[0, 100], onscreen], htmldoc], |
|
67 // test element at same position, flushing layout |
|
68 [[0, 100, false, true], [[0, 100], onscreen], onscreen], |
|
69 |
|
70 // same tests repeated for offscreen element |
|
71 [[10, 100010, true, true], [[10, 100010], offscreen], offscreen], |
|
72 [[0, 100000, true, false], [[0, 100000], offscreen], htmldoc], |
|
73 [[0, 100000, true, true], [[0, 100000], offscreen], offscreen], |
|
74 ]; |
|
75 |
|
76 for (let i = 0; i < testData.length; ++i) { |
|
77 let [x, y, ignoreScroll, flushLayout] = testData[i][0]; |
|
78 let moveData = testData[i][1]; |
|
79 let expected = testData[i][2]; |
|
80 |
|
81 if (moveData) { |
|
82 let moveEl = moveData[1]; |
|
83 let [moveX, moveY] = moveData[0]; |
|
84 |
|
85 moveEl.style.left = moveX + "px"; |
|
86 moveEl.style.top = moveY + "px"; |
|
87 } |
|
88 let found = SpecialPowers.unwrap(domWindowUtils.elementFromPoint( |
|
89 x, y, ignoreScroll, flushLayout)); |
|
90 is(found, expected, "at index " + i + " for data " + testData[i][0].toSource()); |
|
91 } |
|
92 |
|
93 SimpleTest.executeSoon(function() { |
|
94 next(); |
|
95 }); |
|
96 }, |
|
97 |
|
98 /** |
|
99 * Test .isHandlingUserInput attribute. |
|
100 */ |
|
101 function testHandlingUserInput() { |
|
102 ok('isHandlingUserInput' in domWindowUtils, |
|
103 "isHandlingUserInput should be present"); |
|
104 |
|
105 is(domWindowUtils.isHandlingUserInput, false, |
|
106 "isHandlingUserInput should return false if nothing is happening"); |
|
107 |
|
108 function testEvents() { |
|
109 var data = [ |
|
110 { |
|
111 eventName: "click", |
|
112 result: true, |
|
113 }, |
|
114 { |
|
115 eventName: "mousemove", |
|
116 result: false, |
|
117 }, |
|
118 { |
|
119 eventName: "mouseup", |
|
120 result: true, |
|
121 }, |
|
122 { |
|
123 eventName: "mousedown", |
|
124 result: true, |
|
125 }, |
|
126 { |
|
127 eventName: "keydown", |
|
128 result: true, |
|
129 }, |
|
130 { |
|
131 eventName: "keyup", |
|
132 result: true, |
|
133 }, |
|
134 ]; |
|
135 |
|
136 for (let i=0; i<data.length; ++i) { |
|
137 document.addEventListener(data[i].eventName, function() { |
|
138 document.removeEventListener(data[i].eventName, arguments.callee); |
|
139 |
|
140 is(domWindowUtils.isHandlingUserInput, data[i].result, |
|
141 "isHandlingUserInput should be " + data[i].result); |
|
142 |
|
143 SimpleTest.executeSoon(function() { |
|
144 try { testEventsRunner.next(); } catch (e) { } |
|
145 }); |
|
146 }); |
|
147 |
|
148 SimpleTest.executeSoon(function() { |
|
149 if (data[i].eventName == "click") { |
|
150 synthesizeMouseAtCenter(document.body, {}); |
|
151 } else if (data[i].eventName.indexOf("key") == 0) { |
|
152 synthesizeKey("VK_A", { type: data[i].eventName }); |
|
153 } else { |
|
154 synthesizeMouseAtCenter(document.body, { type: data[i].eventName }); |
|
155 } |
|
156 }); |
|
157 |
|
158 yield undefined; |
|
159 } |
|
160 |
|
161 SimpleTest.executeSoon(function() { |
|
162 next(); |
|
163 }); |
|
164 } |
|
165 |
|
166 var testEventsRunner = testEvents(); |
|
167 testEventsRunner.next(); |
|
168 }, |
|
169 ]; |
|
170 |
|
171 function runner() { |
|
172 for (let i=0; i<gTests.length; ++i) { |
|
173 gTests[i](); |
|
174 yield undefined; |
|
175 } |
|
176 |
|
177 SimpleTest.finish(); |
|
178 }; |
|
179 |
|
180 var gTestRunner = runner(); |
|
181 |
|
182 function next() { |
|
183 try { gTestRunner.next(); } catch (e) { if (e != StopIteration) { throw e; } } |
|
184 } |
|
185 |
|
186 // Run the test from onload, since the onscreen and offscreen divs should be in |
|
187 // the right places by then. |
|
188 addLoadEvent(function() { |
|
189 gTestRunner.next(); |
|
190 }); |
|
191 |
|
192 </script> |
|
193 |
|
194 <p id="display"></p> |
|
195 |
|
196 </body> |
|
197 </html> |