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