summaryrefslogtreecommitdiffstats
path: root/browser/tools/mozscreenshots/tests/browser/browser_boundingbox.js
blob: cef3fa7de1f7497c92718bd85a281ebb285de37f (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

/* import-globals-from ../../head.js */

add_task(async function () {
  const scale = window.docShell.QueryInterface(
    Ci.nsIBaseWindow
  ).devicePixelsPerDesktopPixel;
  let { bounds, rects } = TestRunner._findBoundingBox(["#tabbrowser-tabs"]);
  let tabBar = document.querySelector("#tabbrowser-tabs");
  let tabBarRect = tabBar.getBoundingClientRect();

  // Calculate expected values
  let expectedLeft = scale * (tabBar.screenX - TestRunner.croppingPadding);
  let expectedTop = scale * (tabBar.screenY - TestRunner.croppingPadding);
  let expectedRight =
    scale * (tabBarRect.width + TestRunner.croppingPadding * 2) + expectedLeft;
  let expectedBottom =
    scale * (tabBarRect.height + TestRunner.croppingPadding * 2) + expectedTop;

  // Calculate browser region
  let windowLeft = window.screenX * scale;
  let windowTop = window.screenY * scale;
  let windowRight = window.outerWidth * scale + windowLeft;
  let windowBottom = window.outerHeight * scale + windowTop;

  // Adjust values based on browser window
  expectedLeft = Math.max(expectedLeft, windowLeft);
  expectedTop = Math.max(expectedTop, windowTop);
  expectedRight = Math.min(expectedRight, windowRight);
  expectedBottom = Math.min(expectedBottom, windowBottom);
  // Check width calculation on simple example
  is(
    bounds.width,
    expectedRight - expectedLeft,
    "Checking _findBoundingBox width calculation"
  );
  // Check height calculation on simple example
  is(
    bounds.height,
    expectedBottom - expectedTop,
    "Checking _findBoundingBox height caclulation"
  );
  is(
    bounds.left,
    rects[0].left,
    "Checking _findBoundingBox union.left and rect.left is the same for a single selector"
  );
  is(
    bounds.right,
    rects[0].right,
    "Checking _findBoundingBox union.right and rect.right is the same for a single selector"
  );
  is(
    bounds.top,
    rects[0].top,
    "Checking _findBoundingBox union.top and rect.top is the same for a single selector"
  );
  is(
    bounds.bottom,
    rects[0].bottom,
    "Checking _findBoundingBox union.bottom and rect.bottom is the same for a single selector"
  );

  let result = TestRunner._findBoundingBox(["#forward-button", "#TabsToolbar"]);
  bounds = result.bounds;
  rects = result.rects;

  let tabToolbar = document.querySelector("#TabsToolbar");
  let tabToolbarRect = tabToolbar.getBoundingClientRect();
  let fButton = document.querySelector("#forward-button");
  let fButtonRect = fButton.getBoundingClientRect();

  // Calculate expected values
  expectedLeft =
    scale *
    (Math.min(tabToolbar.screenX, fButton.screenX) -
      TestRunner.croppingPadding);
  expectedTop =
    scale *
    (Math.min(tabToolbar.screenY, fButton.screenY) -
      TestRunner.croppingPadding);
  expectedRight =
    scale *
    (Math.max(
      tabToolbarRect.width + tabToolbar.screenX,
      fButtonRect.width + fButton.screenX
    ) +
      TestRunner.croppingPadding);
  expectedBottom =
    scale *
    (Math.max(
      tabToolbarRect.height + tabToolbar.screenY,
      fButtonRect.height + fButton.screenY
    ) +
      TestRunner.croppingPadding);

  // Adjust values based on browser window
  expectedLeft = Math.max(expectedLeft, windowLeft);
  expectedTop = Math.max(expectedTop, windowTop);
  expectedRight = Math.min(expectedRight, windowRight);
  expectedBottom = Math.min(expectedBottom, windowBottom);

  // Check width calculation on union
  is(
    bounds.width,
    expectedRight - expectedLeft,
    "Checking _findBoundingBox union width calculation"
  );
  // Check height calculation on union
  is(
    bounds.height,
    expectedBottom - expectedTop,
    "Checking _findBoundingBox union height calculation"
  );
  // Check single selector's left position
  is(
    rects[0].left,
    Math.max(
      scale * (fButton.screenX - TestRunner.croppingPadding),
      windowLeft
    ),
    "Checking single selector's left position when _findBoundingBox has multiple selectors"
  );
  // Check single selector's right position
  is(
    rects[0].right,
    Math.min(
      scale *
        (fButtonRect.width + fButton.screenX + TestRunner.croppingPadding),
      windowRight
    ),
    "Checking single selector's right position when _findBoundingBox has multiple selectors"
  );
  // Check single selector's top position
  is(
    rects[0].top,
    Math.max(scale * (fButton.screenY - TestRunner.croppingPadding), windowTop),
    "Checking single selector's top position when _findBoundingBox has multiple selectors"
  );
  // Check single selector's bottom position
  is(
    rects[0].bottom,
    Math.min(
      scale *
        (fButtonRect.height + fButton.screenY + TestRunner.croppingPadding),
      windowBottom
    ),
    "Checking single selector's bottom position when _findBoundingBox has multiple selectors"
  );

  // Check that nonexistent selectors throws an exception
  Assert.throws(
    () => {
      TestRunner._findBoundingBox(["#does_not_exist"]);
    },
    /No element for '#does_not_exist' found/,
    "Checking that nonexistent selectors throws an exception"
  );

  // Check that no selectors throws an exception
  Assert.throws(
    () => {
      TestRunner._findBoundingBox([]);
    },
    /No selectors specified/,
    "Checking that no selectors throws an exception"
  );
});