test_domWindowUtils.html (5021B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test nsIDOMWindowUtils</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script 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"> 28 29 SimpleTest.waitForExplicitFinish(); 30 31 var domWindowUtils = SpecialPowers.getDOMWindowUtils(window); 32 33 var gTests = [ 34 /* 35 Element elementFromPoint(in long aX, 36 in long aY, 37 in boolean aIgnoreRootScrollFrame, 38 in boolean aFlushLayout); 39 */ 40 async 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], null], 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 " + JSON.stringify(testData[i][0])); 91 } 92 }, 93 94 /** 95 * Test .isHandlingUserInput attribute. 96 */ 97 async function testHandlingUserInput() { 98 ok('isHandlingUserInput' in domWindowUtils, 99 "isHandlingUserInput should be present"); 100 101 is(domWindowUtils.isHandlingUserInput, false, 102 "isHandlingUserInput should return false if nothing is happening"); 103 104 var data = [ 105 { 106 eventName: "click", 107 result: true, 108 }, 109 { 110 eventName: "auxclick", 111 button: 1, 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 (const {eventName, result, button} of data) { 137 let eventPromise = new Promise(resolve => { 138 document.addEventListener(eventName, function() { 139 is(domWindowUtils.isHandlingUserInput, result, 140 `isHandlingUserInput should be ${result} for ${eventName}`); 141 142 SimpleTest.executeSoon(resolve); 143 }, {once: true}); 144 }); 145 146 SimpleTest.executeSoon(function() { 147 if (eventName == "click") { 148 synthesizeMouseAtCenter(document.body, {}); 149 } else if (eventName == "auxclick" && button) { 150 synthesizeMouseAtCenter(document.body, { button }); 151 } else if (eventName.startsWith("key")) { 152 synthesizeKey("VK_A", { type: eventName }); 153 } else { 154 synthesizeMouseAtCenter(document.body, { type: eventName }); 155 } 156 }); 157 158 await eventPromise; 159 } 160 }, 161 ]; 162 163 async function runner() { 164 for (let i=0; i<gTests.length; ++i) { 165 if (i > 0) { 166 await new Promise(r => SimpleTest.executeSoon(r)); 167 } 168 await gTests[i](); 169 } 170 171 SimpleTest.finish(); 172 }; 173 174 // Run the test from onload, since the onscreen and offscreen divs should be in 175 // the right places by then. 176 addLoadEvent(runner); 177 178 </script> 179 180 <p id="display"></p> 181 182 </body> 183 </html>