summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/test_domWindowUtils.html
blob: 377900c8088b3c022da5c85912f2c6cd2b11f768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE HTML>
<html>
<head>
  <title>Test nsIDOMWindowUtils</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
  <style>
    html, body, div {
      padding: 0;
      margin: 0;
    }
    
    div.test {
      position: absolute;
      height: 10px;
      width: 10px;
    }
  </style>
</head>

<body id="body">

<div class="test" id="onscreen" style="top: 100px; background-color: red;"></div>
<div class="test" id="offscreen" style="top: 100000px; background-color: green;"></div>

<script type="application/javascript;version=1.8">

SimpleTest.waitForExplicitFinish();

var domWindowUtils = SpecialPowers.getDOMWindowUtils(window);

var gTests = [
/*
  nsIDOMElement elementFromPoint(in long aX,
                                 in long aY,
                                 in boolean aIgnoreRootScrollFrame,
                                 in boolean aFlushLayout);
*/
function testElementFromPoint() {
  let onscreen = document.getElementById("onscreen");
  let offscreen = document.getElementById("offscreen");
  let htmldoc = document.documentElement;
  ok(onscreen, "on screen element exists");
  ok(offscreen, "off screen element exists");
  ok(htmldoc, "htmldoc element exists");

  let testData = [
    // default behavior is to return null for items outside the viewport
    [[0, 100], null, onscreen],
    [[9, 109], null, onscreen],
    [[0, 100000], null, null],
    [[9, 100009], null, null],

    // ignore scroll frame
    [[0, 100, true, false], null, onscreen],
    [[9, 109, true, false], null, onscreen],
    [[0, 100000, true, false], null, offscreen],
    [[9, 100009, true, false], null, offscreen],

    // layout flush tests
    // test moving element 10px to the left and down, and flushing layout
    [[10, 110, false, true], [[10, 110], onscreen], onscreen],
    // test moving element back, not flushing layout
    // (will get the html document instead)
    [[0, 100, false, false], [[0, 100], onscreen], htmldoc],
    // test element at same position, flushing layout
    [[0, 100, false, true], [[0, 100], onscreen], onscreen],

    // same tests repeated for offscreen element
    [[10, 100010, true, true], [[10, 100010], offscreen], offscreen],
    [[0, 100000, true, false], [[0, 100000], offscreen], htmldoc],
    [[0, 100000, true, true], [[0, 100000], offscreen], offscreen],
  ];

  for (let i = 0; i < testData.length; ++i) {
    let [x, y, ignoreScroll, flushLayout] = testData[i][0];
    let moveData = testData[i][1];
    let expected = testData[i][2];
    
    if (moveData) {
      let moveEl = moveData[1];
      let [moveX, moveY] = moveData[0];

      moveEl.style.left = moveX + "px";
      moveEl.style.top = moveY + "px";
    }
    let found = SpecialPowers.unwrap(domWindowUtils.elementFromPoint(
                                     x, y, ignoreScroll, flushLayout));
    is(found, expected, "at index " + i + " for data " + testData[i][0].toSource());
  }

  SimpleTest.executeSoon(function() {
    next();
  });
},

/**
 * Test .isHandlingUserInput attribute.
 */
function testHandlingUserInput() {
  ok('isHandlingUserInput' in domWindowUtils,
     "isHandlingUserInput should be present");

  is(domWindowUtils.isHandlingUserInput, false,
     "isHandlingUserInput should return false if nothing is happening");

  function testEvents() {
    var data = [
      {
        eventName: "click",
        result: true,
      },
      {
        eventName: "mousemove",
        result: false,
      },
      {
        eventName: "mouseup",
        result: true,
      },
      {
        eventName: "mousedown",
        result: true,
      },
      {
        eventName: "keydown",
        result: true,
      },
      {
        eventName: "keyup",
        result: true,
      },
    ];

    for (let i=0; i<data.length; ++i) {
      document.addEventListener(data[i].eventName, function() {
        document.removeEventListener(data[i].eventName, arguments.callee);

        is(domWindowUtils.isHandlingUserInput, data[i].result,
           "isHandlingUserInput should be " + data[i].result);

        SimpleTest.executeSoon(function() {
          try { testEventsRunner.next(); } catch (e) { }
        });
      });

      SimpleTest.executeSoon(function() {
        if (data[i].eventName == "click") {
          synthesizeMouseAtCenter(document.body, {});
        } else if (data[i].eventName.indexOf("key") == 0) {
          synthesizeKey("VK_A", { type: data[i].eventName });
        } else {
          synthesizeMouseAtCenter(document.body, { type: data[i].eventName });
        }
      });

      yield undefined;
    }

    SimpleTest.executeSoon(function() {
      next();
    });
  }

  var testEventsRunner = testEvents();
  testEventsRunner.next();
},
];

function runner() {
  for (let i=0; i<gTests.length; ++i) {
    gTests[i]();
    yield undefined;
  }

  SimpleTest.finish();
};

var gTestRunner = runner();

function next() {
  try { gTestRunner.next(); } catch (e) { if (e != StopIteration) { throw e; } }
}

// Run the test from onload, since the onscreen and offscreen divs should be in
// the right places by then.
addLoadEvent(function() {
  gTestRunner.next();
});

</script>

<p id="display"></p>

</body>
</html>