dom/tests/mochitest/general/test_domWindowUtils.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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     }
    14     div.test {
    15       position: absolute;
    16       height: 10px;
    17       width: 10px;
    18     }
    19   </style>
    20 </head>
    22 <body id="body">
    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>
    27 <script type="application/javascript;version=1.8">
    29 SimpleTest.waitForExplicitFinish();
    31 var domWindowUtils = SpecialPowers.getDOMWindowUtils(window);
    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");
    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],
    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],
    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],
    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   ];
    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];
    81     if (moveData) {
    82       let moveEl = moveData[1];
    83       let [moveX, moveY] = moveData[0];
    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   }
    93   SimpleTest.executeSoon(function() {
    94     next();
    95   });
    96 },
    98 /**
    99  * Test .isHandlingUserInput attribute.
   100  */
   101 function testHandlingUserInput() {
   102   ok('isHandlingUserInput' in domWindowUtils,
   103      "isHandlingUserInput should be present");
   105   is(domWindowUtils.isHandlingUserInput, false,
   106      "isHandlingUserInput should return false if nothing is happening");
   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     ];
   136     for (let i=0; i<data.length; ++i) {
   137       document.addEventListener(data[i].eventName, function() {
   138         document.removeEventListener(data[i].eventName, arguments.callee);
   140         is(domWindowUtils.isHandlingUserInput, data[i].result,
   141            "isHandlingUserInput should be " + data[i].result);
   143         SimpleTest.executeSoon(function() {
   144           try { testEventsRunner.next(); } catch (e) { }
   145         });
   146       });
   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       });
   158       yield undefined;
   159     }
   161     SimpleTest.executeSoon(function() {
   162       next();
   163     });
   164   }
   166   var testEventsRunner = testEvents();
   167   testEventsRunner.next();
   168 },
   169 ];
   171 function runner() {
   172   for (let i=0; i<gTests.length; ++i) {
   173     gTests[i]();
   174     yield undefined;
   175   }
   177   SimpleTest.finish();
   178 };
   180 var gTestRunner = runner();
   182 function next() {
   183   try { gTestRunner.next(); } catch (e) { if (e != StopIteration) { throw e; } }
   184 }
   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 });
   192 </script>
   194 <p id="display"></p>
   196 </body>
   197 </html>

mercurial