summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_layoutHelpers.js
blob: 5698c47bce000f288762989a02280e1632d21c96 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that scrollIntoViewIfNeeded works properly.
const {
  scrollIntoViewIfNeeded,
} = require("resource://devtools/client/shared/scroll.js");

const TEST_URI = CHROME_URL_ROOT + "doc_layoutHelpers.html";

add_task(async function () {
  const { host, win } = await createHost("bottom", TEST_URI);
  await runTest(win);
  host.destroy();
});

async function runTest(win) {
  const some = win.document.getElementById("some");

  some.style.top = win.innerHeight + "px";
  some.style.left = win.innerWidth + "px";
  // The tests start with a black 2x2 pixels square below bottom right.
  // Do not resize the window during the tests.

  const xPos = Math.floor(win.innerWidth / 2);
  // Above the viewport.
  win.scroll(xPos, win.innerHeight + 2);
  scrollIntoViewIfNeeded(some);
  is(
    win.scrollY,
    Math.floor(win.innerHeight / 2) + 1,
    "Element completely hidden above should appear centered."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // On the top edge.
  win.scroll(win.innerWidth / 2, win.innerHeight + 1);
  scrollIntoViewIfNeeded(some);
  is(
    win.scrollY,
    win.innerHeight,
    "Element partially visible above should appear above."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // Just below the viewport.
  win.scroll(win.innerWidth / 2, 0);
  scrollIntoViewIfNeeded(some);
  is(
    win.scrollY,
    Math.floor(win.innerHeight / 2) + 1,
    "Element completely hidden below should appear centered."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // On the bottom edge.
  win.scroll(win.innerWidth / 2, 1);
  scrollIntoViewIfNeeded(some);
  is(win.scrollY, 2, "Element partially visible below should appear below.");
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // Above the viewport.
  win.scroll(win.innerWidth / 2, win.innerHeight + 2);
  scrollIntoViewIfNeeded(some, false);
  is(
    win.scrollY,
    win.innerHeight,
    "Element completely hidden above should appear above " +
      "if parameter is false."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // On the top edge.
  win.scroll(win.innerWidth / 2, win.innerHeight + 1);
  scrollIntoViewIfNeeded(some, false);
  is(
    win.scrollY,
    win.innerHeight,
    "Element partially visible above should appear above " +
      "if parameter is false."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // Below the viewport.
  win.scroll(win.innerWidth / 2, 0);
  scrollIntoViewIfNeeded(some, false);
  is(
    win.scrollY,
    2,
    "Element completely hidden below should appear below " +
      "if parameter is false."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // On the bottom edge.
  win.scroll(win.innerWidth / 2, 1);
  scrollIntoViewIfNeeded(some, false);
  is(
    win.scrollY,
    2,
    "Element partially visible below should appear below " +
      "if parameter is false."
  );
  is(win.scrollX, xPos, "scrollX position has not changed.");

  // Check smooth flag (scroll goes below the viewport)
  await pushPref("ui.prefersReducedMotion", 0);

  info("Checking smooth flag");
  is(
    win.matchMedia("(prefers-reduced-motion)").matches,
    false,
    "Reduced motion is disabled"
  );

  const other = win.document.getElementById("other");
  other.style.top = win.innerHeight + "px";
  other.style.left = win.innerWidth + "px";
  win.scroll(0, 0);

  scrollIntoViewIfNeeded(other, false, true);
  Assert.less(
    win.scrollY,
    other.clientHeight,
    "Window has not instantly scrolled to the final position"
  );
  await waitUntil(() => win.scrollY === other.clientHeight);
  ok(true, "Window did finish scrolling");
}