summaryrefslogtreecommitdiffstats
path: root/layout/generic/test/file_bug514732_helper.html
blob: 0ef110609372861418ff2230293e3dca5c20107e (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=514732
-->
<head>
  <title>Test for Bug 514732</title>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug?id=514732">Mozilla Bug 514732</a>

<div id="imgs" style="whitespace: nowrap; overflow: visible; height: 10px;"></div>

<iframe id="testframe" src="./file_bug514732_1.html"></iframe>

<br/><br/>
<div id="log">log:<br/></div>
<br/><br/>

<script class="testbody" style="width: 300; height: 150;" type="text/javascript;version=1.8">
// Import test API
var is = window.opener.is;
var ok = window.opener.ok;
var todo = window.opener.todo;
var finish = window.opener.SimpleTest.finish;

function log(msg) {
  document.getElementById("log").innerHTML += msg;
}

function logln(msg) {
  if (arguments.length == 0) msg = "";
  document.getElementById("log").innerHTML += msg + "<br/>";
}

function logev(ev) {
  logln([ev.x, ev.y, ev.width, ev.height].join(' '));
}

/* Absolutely position a 500x500 div at x, y on doc */
function phoom(doc, x, y) {
  var d = doc.createElement('div');
  d.id = "phoomer";
  d.style.position = "absolute";
  d.style.left = x + 'px';
  d.style.top = y + 'px';
  d.style.backgroundColor = "#00FFF0";
  d.style.width = 500 + 'px';
  d.style.height = 500 + 'px';
  doc.getElementById('imgs').appendChild(d);
}

function unphoom(doc) {
  var phoomer = doc.getElementById('phoomer');
  if (phoomer)
    doc.getElementById('imgs').removeChild(phoomer);
}

function getDocBodyDimensions(doc) {
  return [doc.documentElement.scrollWidth, doc.documentElement.scrollHeight];
}

function makeResult() {
  return {
    success: false,
    event: null
  };
}

function makeListener(result, eventGen) {
  return function(ev) {
    result.success = true;
    result.event = ev;
    setTimeout(function() { eventGen.next(); }, 0);
  };
}

function waitInterrupt(result, gen) {
  result.event = null;
  result.success = false;
  setTimeout(function() { if (!result.success) gen.next(); }, 3500);
}

function testPhoom(isCapturing, x, y, expectEvent) {

  var eventGen = (function() {
    var innerdoc = document.getElementById('testframe').contentDocument;

    for (var doc of [document, innerdoc]) {
      var inner = (doc == innerdoc);
      var w, h, result, listener;

      /* --- EXPANSION --- */

      result = makeResult();
      listener = makeListener(result, eventGen);

      doc.addEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (!expectEvent) waitInterrupt(result, eventGen);
      phoom(doc, x, y);
      yield;

      doc.removeEventListener("MozScrolledAreaChanged", listener, isCapturing);

      /* If we're expecting an event, better check that we got one.  If we are not expecting one,
         one can still arrive, but we don't complain if it did not.  In either case, any event
         that arrives will have its data checked below. */
      if (expectEvent) {
        ok(result.success, "Received expected " + (inner ? "inner" : "") + " expansion event");
      }

      if (result.success) {
        is(result.event.x, 0, "Expansion event x is 0");
        is(result.event.y, 0, "Expansion event y is 0");
        if (inner) {
          /* In iframe-land, the values of |myiframe.contentDocument.documentElement.scrollWidth| and
             |.scrollHeight| appear to be not as expected (they stay very small).  This is fine, since
             we know exactly where we're positioning our fixed-dimensions div, and we know our iframe's
             dimensions, so we can do our own math.  NB: The inner iframe is styled at 300x500. */
          is(Math.round(result.event.width),
             Math.max(x + 500, 300),
             "Inner expansion event width matches body width");
          is(Math.round(result.event.height),
             Math.max(y + 500, 150),
             "Inner expansion event height matches body height");
        } else {
          [w, h] = getDocBodyDimensions(doc);
          is(Math.round(result.event.width),  w, "Expansion event width matches body width");
          is(Math.round(result.event.height), h, "Expansion event height matches body height");
        }
      }

      /* --- CONTRACTION --- */

      result = makeResult();
      listener = makeListener(result, eventGen);

      doc.addEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (!expectEvent) waitInterrupt(result, eventGen);
      unphoom(doc);
      yield;

      doc.removeEventListener("MozScrolledAreaChanged", listener, isCapturing);

      if (expectEvent) {
        ok(result.success, "Received expected " + (inner ? "inner" : "") + " contraction event");
      }

      if (result.success) {
        is(result.event.x, 0, "Contraction event x is 0");
        is(result.event.y, 0, "Contraction event y is 0");
        if (inner) {
          is(Math.round(result.event.width),  300, "Inner contraction event width matches body width");
          is(Math.round(result.event.height), 150, "Inner contraction event height matches body height");
        } else {
          [w, h] = getDocBodyDimensions(doc);
          is(Math.round(result.event.width),  w, "Contraction event width matches body width");
          is(Math.round(result.event.height), h, "Contraction event height matches body height");
        }
      }
    }

    setTimeout(nextTest, 0);

    yield;
  })();

  setTimeout(function() { eventGen.next(); }, 0);
}

function testPreliminaries() {
  /* Do any setup here */
  nextTest();
}

function testEventCapturingHorz() {
  testPhoom(true, 10000, 0, true); // calls nextTest() when finished
}

function testEventCapturingVert() {
  testPhoom(true, 0, 10000, true); // calls nextTest() when finished
}

function testEventCapturingBoth() {
  testPhoom(true, 10000, 10000, true); // calls nextTest() when finished
}

function testEventCapturingNegv() {
  testPhoom(true, -10000, -10000, false); // calls nextTest() when finished
}

function testEventBubblingHorz() {
  testPhoom(false, 10000, 0, true); // calls nextTest() when finished
}

function testEventBubblingVert() {
  testPhoom(false, 0, 10000, true); // calls nextTest() when finished
}

function testEventBubblingBoth() {
  testPhoom(false, 10000, 10000, true); // calls nextTest() when finished
}

function testEventBubblingNegv() {
  testPhoom(false, -10000, -10000, false); // calls nextTest() when finished
}

function testCustomCreationHelper(creatingDoc, listeningDoc, x, y, w, h, expectEvent) {
  var result = makeResult();

  var listener = function(evt) {
    result.success = true;
    result.event = evt;
  };

  var ev = creatingDoc.createEvent("ScrollAreaEvent");
  ev.initScrollAreaEvent("MozScrolledAreaChanged",
                         true, true, window, 0,
                         x, y, w, h);

  listeningDoc.addEventListener("MozScrolledAreaChanged", listener, true);

  result.success = false;
  creatingDoc.dispatchEvent(ev);

  listeningDoc.removeEventListener("MozScrolledAreaChanged", listener, true);

  is(result.success, expectEvent, "Received custom event iff expected");
  if (result.success) {
    is(result.event.x,      x, "Custom event data matches for x");
    is(result.event.y,      y, "Custom event data matches for y");
    is(result.event.width,  w, "Custom event data matches for width");
    is(result.event.height, h, "Custom event data matches for height");
  }
}

function testCustomCreation() {
  var innerdoc = document.getElementById('testframe').contentDocument;
  testCustomCreationHelper(document, document, 2, 7, 1, 8, true);  // e = 2.718 ...
  testCustomCreationHelper(innerdoc, innerdoc, 2, 8, 1, 8, true);  //  ... 2818 ...
  testCustomCreationHelper(innerdoc, document, 2, 8, 4, 5, false); //  ... 2845 ...
  nextTest();
}

var TESTS = [testPreliminaries,
             testEventCapturingHorz,
             testEventCapturingVert,
             testEventCapturingBoth,
             testEventCapturingNegv,
             testEventBubblingHorz,
             testEventBubblingVert,
             testEventBubblingBoth,
             testEventBubblingNegv,
             testCustomCreation,
              __end];

var PC = 0;

function nextTest() {
  TESTS[PC++]();
}

function __end() {
  log("done!");
  finish();
  window.close();
}

/*
 * We need both the parent document and all iframe documents to load
 * before starting anything
 */
var loaded = 0;

window
.addEventListener('load', function(ev) {
  logln(++loaded);
  if (loaded >= 2) {
    logln("go...");
    nextTest();
  }
}, false);

document.getElementById("testframe").contentWindow
.addEventListener('load', function(ev) {
  logln(++loaded);
  if (loaded >= 2) {
    logln("go...");
    nextTest();
  }
}, false);

// window.opener says: SimpleTest.waitForExplicitFinish();

</script>

</body>
</html>