summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_bug295977_autoscroll_overflow.js
blob: 431fdf048f780e25d42a4eae33b793c9989e6ede (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
requestLongerTimeout(2);
add_task(async function () {
  function pushPrefs(prefs) {
    return SpecialPowers.pushPrefEnv({ set: prefs });
  }

  await pushPrefs([
    ["general.autoScroll", true],
    ["test.events.async.enabled", true],
  ]);

  const expectScrollNone = 0;
  const expectScrollVert = 1;
  const expectScrollHori = 2;
  const expectScrollBoth = 3;

  var allTests = [
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><body><style type="text/css">div { display: inline-block; }</style>\
      <div id="a" style="width: 100px; height: 100px; overflow: hidden;"><div style="width: 200px; height: 200px;"></div></div>\
      <div id="b" style="width: 100px; height: 100px; overflow: auto;"><div style="width: 200px; height: 200px;"></div></div>\
      <div id="c" style="width: 100px; height: 100px; overflow-x: auto; overflow-y: hidden;"><div style="width: 200px; height: 200px;"></div></div>\
      <div id="d" style="width: 100px; height: 100px; overflow-y: auto; overflow-x: hidden;"><div style="width: 200px; height: 200px;"></div></div>\
      <select id="e" style="width: 100px; height: 100px;" multiple="multiple"><option>aaaaaaaaaaaaaaaaaaaaaaaa</option><option>a</option><option>a</option>\
      <option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option>\
      <option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option></select>\
      <select id="f" style="width: 100px; height: 100px;"><option>a</option><option>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</option><option>a</option>\
      <option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option>\
      <option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option><option>a</option></select>\
      <div id="g" style="width: 99px; height: 99px; border: 10px solid black; margin: 10px; overflow: auto;"><div style="width: 100px; height: 100px;"></div></div>\
      <div id="h" style="width: 100px; height: 100px; overflow: clip;"><div style="width: 200px; height: 200px;"></div></div>\
      <iframe id="iframe" style="display: none;"></iframe>\
      </body></html>',
    },
    { elem: "a", expected: expectScrollNone },
    { elem: "b", expected: expectScrollBoth },
    { elem: "c", expected: expectScrollHori },
    { elem: "d", expected: expectScrollVert },
    { elem: "e", expected: expectScrollVert },
    { elem: "f", expected: expectScrollNone },
    { elem: "g", expected: expectScrollBoth },
    { elem: "h", expected: expectScrollNone },
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><body id="i" style="overflow-y: scroll"><div style="height: 2000px"></div>\
      <iframe id="iframe" style="display: none;"></iframe>\
      </body></html>',
    },
    { elem: "i", expected: expectScrollVert }, // bug 695121
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><style>html, body { width: 100%; height: 100%; overflow-x: hidden; overflow-y: scroll; }</style>\
      <body id="j"><div style="height: 2000px"></div>\
      <iframe id="iframe" style="display: none;"></iframe>\
      </body></html>',
    },
    { elem: "j", expected: expectScrollVert }, // bug 914251
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8">\
<style>\
body > div {scroll-behavior: smooth;width: 300px;height: 300px;overflow: scroll;}\
body > div > div {width: 1000px;height: 1000px;}\
</style>\
</head><body><div id="t"><div></div></div></body></html>',
    },
    { elem: "t", expected: expectScrollBoth }, // bug 1308775
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><body>\
<div id="k" style="height: 150px;  width: 200px; overflow: scroll; border: 1px solid black;">\
<iframe style="height: 200px; width: 300px;"></iframe>\
</div>\
<div id="l" style="height: 150px;  width: 300px; overflow: scroll; border: 1px dashed black;">\
<iframe style="height: 200px; width: 200px;" src="data:text/html,<div style=\'border: 5px solid blue; height: 200%; width: 200%;\'></div>"></iframe>\
</div>\
<iframe id="m"></iframe>\
<div style="height: 200%; border: 5px dashed black;">filler to make document overflow: scroll;</div>\
</body></html>',
    },
    { elem: "k", expected: expectScrollBoth },
    { elem: "k", expected: expectScrollNone, testwindow: true },
    { elem: "l", expected: expectScrollNone },
    { elem: "m", expected: expectScrollVert, testwindow: true },
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><body>\
<img width="100" height="100" alt="image map" usemap="%23planetmap">\
<map name="planetmap">\
  <area id="n" shape="rect" coords="0,0,100,100" href="javascript:void(null)">\
</map>\
<a href="javascript:void(null)" id="o" style="width: 100px; height: 100px; border: 1px solid black; display: inline-block; vertical-align: top;">link</a>\
<input id="p" style="width: 100px; height: 100px; vertical-align: top;">\
<textarea id="q" style="width: 100px; height: 100px; vertical-align: top;"></textarea>\
<div style="height: 200%; border: 1px solid black;"></div>\
</body></html>',
    },
    { elem: "n", expected: expectScrollNone, testwindow: true },
    { elem: "o", expected: expectScrollNone, testwindow: true },
    {
      elem: "p",
      expected: expectScrollVert,
      testwindow: true,
      middlemousepastepref: false,
    },
    {
      elem: "q",
      expected: expectScrollVert,
      testwindow: true,
      middlemousepastepref: false,
    },
    {
      dataUri:
        'data:text/html,<html><head><meta charset="utf-8"></head><body>\
<input id="r" style="width: 100px; height: 100px; vertical-align: top;">\
<textarea id="s" style="width: 100px; height: 100px; vertical-align: top;"></textarea>\
<div style="height: 200%; border: 1px solid black;"></div>\
</body></html>',
    },
    {
      elem: "r",
      expected: expectScrollNone,
      testwindow: true,
      middlemousepastepref: true,
    },
    {
      elem: "s",
      expected: expectScrollNone,
      testwindow: true,
      middlemousepastepref: true,
    },
    {
      dataUri:
        "data:text/html," +
        encodeURIComponent(`
<!doctype html>
<iframe id=i height=100 width=100 scrolling="no" srcdoc="<div style='height: 200px'>Auto-scrolling should never make me disappear"></iframe>
<div style="height: 100vh"></div>
      `),
    },
    {
      elem: "i",
      // We expect the outer window to scroll vertically, not the iframe's window.
      expected: expectScrollVert,
      testwindow: true,
    },
    {
      dataUri:
        "data:text/html," +
        encodeURIComponent(`
<!doctype html>
<iframe id=i height=100 width=100 srcdoc="<div style='height: 200px'>Auto-scrolling should make me disappear"></iframe>
<div style="height: 100vh"></div>
      `),
    },
    {
      elem: "i",
      // We expect the iframe's window to scroll vertically, so the outer window should not scroll.
      expected: expectScrollNone,
      testwindow: true,
    },
    {
      // Test: scroll is initiated in out of process iframe having no scrollable area
      dataUri:
        "data:text/html," +
        encodeURIComponent(`
<!doctype html>
<head><meta content="text/html;charset=utf-8"></head><body>
<div id="scroller" style="width: 300px; height: 300px; overflow-y: scroll; overflow-x: hidden; border: solid 1px blue;">
  <iframe id="noscroll-outofprocess-iframe"
          src="https://example.com/document-builder.sjs?html=<html><body>Hey!</body></html>"
          style="border: solid 1px green; margin: 2px;"></iframe>
  <div style="width: 100%; height: 200px;"></div>
</div></body>
        `),
    },
    {
      elem: "noscroll-outofprocess-iframe",
      // We expect the div to scroll vertically, not the iframe's window.
      expected: expectScrollVert,
      scrollable: "scroller",
    },
  ];

  for (let test of allTests) {
    if (test.dataUri) {
      let loadedPromise = BrowserTestUtils.browserLoaded(
        gBrowser.selectedBrowser
      );
      BrowserTestUtils.loadURIString(gBrowser, test.dataUri);
      await loadedPromise;
      await ContentTask.spawn(gBrowser.selectedBrowser, {}, async () => {
        // Wait for a paint so that hit-testing works correctly.
        await new Promise(resolve =>
          content.requestAnimationFrame(() =>
            content.requestAnimationFrame(resolve)
          )
        );
      });
      continue;
    }

    let prefsChanged = "middlemousepastepref" in test;
    if (prefsChanged) {
      await pushPrefs([["middlemouse.paste", test.middlemousepastepref]]);
    }

    await BrowserTestUtils.synthesizeMouse(
      "#" + test.elem,
      50,
      80,
      { button: 1 },
      gBrowser.selectedBrowser
    );

    // This ensures bug 605127 is fixed: pagehide in an unrelated document
    // should not cancel the autoscroll.
    await ContentTask.spawn(
      gBrowser.selectedBrowser,
      { waitForAutoScrollStart: test.expected != expectScrollNone },
      async ({ waitForAutoScrollStart }) => {
        var iframe = content.document.getElementById("iframe");

        if (iframe) {
          var e = new iframe.contentWindow.PageTransitionEvent("pagehide", {
            bubbles: true,
            cancelable: true,
            persisted: false,
          });
          iframe.contentDocument.dispatchEvent(e);
          iframe.contentDocument.documentElement.dispatchEvent(e);
        }
        if (waitForAutoScrollStart) {
          await new Promise(resolve =>
            Services.obs.addObserver(resolve, "autoscroll-start")
          );
        }
      }
    );

    is(
      document.activeElement,
      gBrowser.selectedBrowser,
      "Browser still focused after autoscroll started"
    );

    await BrowserTestUtils.synthesizeMouse(
      "#" + test.elem,
      100,
      100,
      { type: "mousemove", clickCount: "0" },
      gBrowser.selectedBrowser
    );

    if (prefsChanged) {
      await SpecialPowers.popPrefEnv();
    }

    // Start checking for the scroll.
    let firstTimestamp = undefined;
    let timeCompensation;
    do {
      let timestamp = await new Promise(resolve =>
        window.requestAnimationFrame(resolve)
      );
      if (firstTimestamp === undefined) {
        firstTimestamp = timestamp;
      }

      // This value is calculated similarly to the value of the same name in
      // ClickEventHandler.autoscrollLoop, except here it's cumulative across
      // all frames after the first one instead of being based only on the
      // current frame.
      timeCompensation = (timestamp - firstTimestamp) / 20;
      info(
        "timestamp=" +
          timestamp +
          " firstTimestamp=" +
          firstTimestamp +
          " timeCompensation=" +
          timeCompensation
      );

      // Try to wait until enough time has passed to allow the scroll to happen.
      // autoscrollLoop incrementally scrolls during each animation frame, but
      // due to how its calculations work, when a frame is very close to the
      // previous frame, no scrolling may actually occur during that frame.
      // After 100ms's worth of frames, timeCompensation will be 1, making it
      // more likely that the accumulated scroll in autoscrollLoop will be >= 1,
      // although it also depends on acceleration, which here in this test
      // should be > 1 due to how it synthesizes mouse events below.
    } while (timeCompensation < 5);

    // Close the autoscroll popup by synthesizing Esc.
    EventUtils.synthesizeKey("KEY_Escape");
    let scrollVert = test.expected & expectScrollVert;
    let scrollHori = test.expected & expectScrollHori;

    await SpecialPowers.spawn(
      gBrowser.selectedBrowser,
      [
        {
          scrollVert,
          scrollHori,
          elemid: test.scrollable || test.elem,
          checkWindow: test.testwindow,
        },
      ],
      async function (args) {
        let msg = "";
        if (args.checkWindow) {
          if (
            !(
              (args.scrollVert && content.scrollY > 0) ||
              (!args.scrollVert && content.scrollY == 0)
            )
          ) {
            msg += "Failed: ";
          }
          msg +=
            "Window for " +
            args.elemid +
            " should" +
            (args.scrollVert ? "" : " not") +
            " have scrolled vertically\n";

          if (
            !(
              (args.scrollHori && content.scrollX > 0) ||
              (!args.scrollHori && content.scrollX == 0)
            )
          ) {
            msg += "Failed: ";
          }
          msg +=
            " Window for " +
            args.elemid +
            " should" +
            (args.scrollHori ? "" : " not") +
            " have scrolled horizontally\n";
        } else {
          let elem = content.document.getElementById(args.elemid);
          if (
            !(
              (args.scrollVert && elem.scrollTop > 0) ||
              (!args.scrollVert && elem.scrollTop == 0)
            )
          ) {
            msg += "Failed: ";
          }
          msg +=
            " " +
            args.elemid +
            " should" +
            (args.scrollVert ? "" : " not") +
            " have scrolled vertically\n";
          if (
            !(
              (args.scrollHori && elem.scrollLeft > 0) ||
              (!args.scrollHori && elem.scrollLeft == 0)
            )
          ) {
            msg += "Failed: ";
          }
          msg +=
            args.elemid +
            " should" +
            (args.scrollHori ? "" : " not") +
            " have scrolled horizontally";
        }

        Assert.ok(!msg.includes("Failed"), msg);
      }
    );

    // Before continuing the test, we need to ensure that the IPC
    // message that stops autoscrolling has had time to arrive.
    await new Promise(resolve => executeSoon(resolve));
  }

  // remove 2 tabs that were opened by middle-click on links
  while (gBrowser.visibleTabs.length > 1) {
    gBrowser.removeTab(gBrowser.visibleTabs[gBrowser.visibleTabs.length - 1]);
  }

  // wait for focus to fix a failure in the next test if the latter runs too soon.
  await SimpleTest.promiseFocus();
});