summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/page/browser_getLayoutMetrics.js
blob: db8b3e8f3cc975a00b54206675bf6ae0a1c1731d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function documentSmallerThanViewport({ client }) {
  const { Page } = client;
  await loadURL(toDataURL("<div>Hello world"));

  const { contentSize, layoutViewport } = await Page.getLayoutMetrics();
  await checkContentSize(contentSize);
  await checkLayoutViewport(layoutViewport);

  is(
    contentSize.x,
    layoutViewport.pageX,
    "X position of content is equal to layout viewport"
  );
  is(
    contentSize.y,
    layoutViewport.pageY,
    "Y position of content is equal to layout viewport"
  );
  ok(
    contentSize.width <= layoutViewport.clientWidth,
    "Width of content is smaller than the layout viewport"
  );
  ok(
    contentSize.height <= layoutViewport.clientHeight,
    "Height of content is smaller than the layout viewport"
  );
});

add_task(async function documentLargerThanViewport({ client }) {
  const { Page } = client;
  await loadURL(toDataURL("<div style='margin: 150vh 0 0 150vw'>Hello world"));

  const { contentSize, layoutViewport } = await Page.getLayoutMetrics();
  await checkContentSize(contentSize);
  await checkLayoutViewport(layoutViewport, { scrollbars: true });

  is(
    contentSize.x,
    layoutViewport.pageX,
    "X position of content is equal to layout viewport"
  );
  is(
    contentSize.y,
    layoutViewport.pageY,
    "Y position of content is equal to layout viewport"
  );
  ok(
    contentSize.width > layoutViewport.clientWidth,
    "Width of content is larger than the layout viewport"
  );
  ok(
    contentSize.height > layoutViewport.clientHeight,
    "Height of content is larger than the layout viewport"
  );
});

add_task(async function documentLargerThanViewportScrolledXY({ client }) {
  const { Page } = client;
  await loadURL(toDataURL("<div style='margin: 150vh 0 0 150vw'>Hello world"));

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.scrollTo(50, 100);
  });

  const { contentSize, layoutViewport } = await Page.getLayoutMetrics();
  await checkContentSize(contentSize);
  await checkLayoutViewport(layoutViewport, { scrollbars: true });

  is(
    layoutViewport.pageX,
    contentSize.x + 50,
    "X position of content is equal to layout viewport"
  );
  is(
    layoutViewport.pageY,
    contentSize.y + 100,
    "Y position of content is equal to layout viewport"
  );
  ok(
    contentSize.width > layoutViewport.clientWidth,
    "Width of content is larger than the layout viewport"
  );
  ok(
    contentSize.height > layoutViewport.clientHeight,
    "Height of content is larger than the layout viewport"
  );
});

async function checkContentSize(rect) {
  const expected = await getContentSize();

  is(rect.x, expected.x, "Expected x position returned");
  is(rect.y, expected.y, "Expected y position returned");
  is(rect.width, expected.width, "Expected width returned");
  is(rect.height, expected.height, "Expected height returned");
}

async function checkLayoutViewport(viewport, options = {}) {
  const { scrollbars = false } = options;

  const expected = await getViewportSize();

  if (scrollbars) {
    const { width, height } = await getScrollbarSize();
    expected.width -= width;
    expected.height -= height;
  }

  is(viewport.pageX, expected.x, "Expected x position returned");
  is(viewport.pageY, expected.y, "Expected y position returned");
  is(viewport.clientWidth, expected.width, "Expected width returned");
  is(viewport.clientHeight, expected.height, "Expected height returned");
}