1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/general/test_domWindowUtils.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,197 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test nsIDOMWindowUtils</title> 1.8 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> 1.10 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"> 1.11 + <style> 1.12 + html, body, div { 1.13 + padding: 0; 1.14 + margin: 0; 1.15 + } 1.16 + 1.17 + div.test { 1.18 + position: absolute; 1.19 + height: 10px; 1.20 + width: 10px; 1.21 + } 1.22 + </style> 1.23 +</head> 1.24 + 1.25 +<body id="body"> 1.26 + 1.27 +<div class="test" id="onscreen" style="top: 100px; background-color: red;"></div> 1.28 +<div class="test" id="offscreen" style="top: 100000px; background-color: green;"></div> 1.29 + 1.30 +<script type="application/javascript;version=1.8"> 1.31 + 1.32 +SimpleTest.waitForExplicitFinish(); 1.33 + 1.34 +var domWindowUtils = SpecialPowers.getDOMWindowUtils(window); 1.35 + 1.36 +var gTests = [ 1.37 +/* 1.38 + nsIDOMElement elementFromPoint(in long aX, 1.39 + in long aY, 1.40 + in boolean aIgnoreRootScrollFrame, 1.41 + in boolean aFlushLayout); 1.42 +*/ 1.43 +function testElementFromPoint() { 1.44 + let onscreen = document.getElementById("onscreen"); 1.45 + let offscreen = document.getElementById("offscreen"); 1.46 + let htmldoc = document.documentElement; 1.47 + ok(onscreen, "on screen element exists"); 1.48 + ok(offscreen, "off screen element exists"); 1.49 + ok(htmldoc, "htmldoc element exists"); 1.50 + 1.51 + let testData = [ 1.52 + // default behavior is to return null for items outside the viewport 1.53 + [[0, 100], null, onscreen], 1.54 + [[9, 109], null, onscreen], 1.55 + [[0, 100000], null, null], 1.56 + [[9, 100009], null, null], 1.57 + 1.58 + // ignore scroll frame 1.59 + [[0, 100, true, false], null, onscreen], 1.60 + [[9, 109, true, false], null, onscreen], 1.61 + [[0, 100000, true, false], null, offscreen], 1.62 + [[9, 100009, true, false], null, offscreen], 1.63 + 1.64 + // layout flush tests 1.65 + // test moving element 10px to the left and down, and flushing layout 1.66 + [[10, 110, false, true], [[10, 110], onscreen], onscreen], 1.67 + // test moving element back, not flushing layout 1.68 + // (will get the html document instead) 1.69 + [[0, 100, false, false], [[0, 100], onscreen], htmldoc], 1.70 + // test element at same position, flushing layout 1.71 + [[0, 100, false, true], [[0, 100], onscreen], onscreen], 1.72 + 1.73 + // same tests repeated for offscreen element 1.74 + [[10, 100010, true, true], [[10, 100010], offscreen], offscreen], 1.75 + [[0, 100000, true, false], [[0, 100000], offscreen], htmldoc], 1.76 + [[0, 100000, true, true], [[0, 100000], offscreen], offscreen], 1.77 + ]; 1.78 + 1.79 + for (let i = 0; i < testData.length; ++i) { 1.80 + let [x, y, ignoreScroll, flushLayout] = testData[i][0]; 1.81 + let moveData = testData[i][1]; 1.82 + let expected = testData[i][2]; 1.83 + 1.84 + if (moveData) { 1.85 + let moveEl = moveData[1]; 1.86 + let [moveX, moveY] = moveData[0]; 1.87 + 1.88 + moveEl.style.left = moveX + "px"; 1.89 + moveEl.style.top = moveY + "px"; 1.90 + } 1.91 + let found = SpecialPowers.unwrap(domWindowUtils.elementFromPoint( 1.92 + x, y, ignoreScroll, flushLayout)); 1.93 + is(found, expected, "at index " + i + " for data " + testData[i][0].toSource()); 1.94 + } 1.95 + 1.96 + SimpleTest.executeSoon(function() { 1.97 + next(); 1.98 + }); 1.99 +}, 1.100 + 1.101 +/** 1.102 + * Test .isHandlingUserInput attribute. 1.103 + */ 1.104 +function testHandlingUserInput() { 1.105 + ok('isHandlingUserInput' in domWindowUtils, 1.106 + "isHandlingUserInput should be present"); 1.107 + 1.108 + is(domWindowUtils.isHandlingUserInput, false, 1.109 + "isHandlingUserInput should return false if nothing is happening"); 1.110 + 1.111 + function testEvents() { 1.112 + var data = [ 1.113 + { 1.114 + eventName: "click", 1.115 + result: true, 1.116 + }, 1.117 + { 1.118 + eventName: "mousemove", 1.119 + result: false, 1.120 + }, 1.121 + { 1.122 + eventName: "mouseup", 1.123 + result: true, 1.124 + }, 1.125 + { 1.126 + eventName: "mousedown", 1.127 + result: true, 1.128 + }, 1.129 + { 1.130 + eventName: "keydown", 1.131 + result: true, 1.132 + }, 1.133 + { 1.134 + eventName: "keyup", 1.135 + result: true, 1.136 + }, 1.137 + ]; 1.138 + 1.139 + for (let i=0; i<data.length; ++i) { 1.140 + document.addEventListener(data[i].eventName, function() { 1.141 + document.removeEventListener(data[i].eventName, arguments.callee); 1.142 + 1.143 + is(domWindowUtils.isHandlingUserInput, data[i].result, 1.144 + "isHandlingUserInput should be " + data[i].result); 1.145 + 1.146 + SimpleTest.executeSoon(function() { 1.147 + try { testEventsRunner.next(); } catch (e) { } 1.148 + }); 1.149 + }); 1.150 + 1.151 + SimpleTest.executeSoon(function() { 1.152 + if (data[i].eventName == "click") { 1.153 + synthesizeMouseAtCenter(document.body, {}); 1.154 + } else if (data[i].eventName.indexOf("key") == 0) { 1.155 + synthesizeKey("VK_A", { type: data[i].eventName }); 1.156 + } else { 1.157 + synthesizeMouseAtCenter(document.body, { type: data[i].eventName }); 1.158 + } 1.159 + }); 1.160 + 1.161 + yield undefined; 1.162 + } 1.163 + 1.164 + SimpleTest.executeSoon(function() { 1.165 + next(); 1.166 + }); 1.167 + } 1.168 + 1.169 + var testEventsRunner = testEvents(); 1.170 + testEventsRunner.next(); 1.171 +}, 1.172 +]; 1.173 + 1.174 +function runner() { 1.175 + for (let i=0; i<gTests.length; ++i) { 1.176 + gTests[i](); 1.177 + yield undefined; 1.178 + } 1.179 + 1.180 + SimpleTest.finish(); 1.181 +}; 1.182 + 1.183 +var gTestRunner = runner(); 1.184 + 1.185 +function next() { 1.186 + try { gTestRunner.next(); } catch (e) { if (e != StopIteration) { throw e; } } 1.187 +} 1.188 + 1.189 +// Run the test from onload, since the onscreen and offscreen divs should be in 1.190 +// the right places by then. 1.191 +addLoadEvent(function() { 1.192 + gTestRunner.next(); 1.193 +}); 1.194 + 1.195 +</script> 1.196 + 1.197 +<p id="display"></p> 1.198 + 1.199 +</body> 1.200 +</html>