summaryrefslogtreecommitdiffstats
path: root/browser/tools/mozscreenshots/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /browser/tools/mozscreenshots/tests
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/tools/mozscreenshots/tests')
-rw-r--r--browser/tools/mozscreenshots/tests/browser/browser.ini8
-rw-r--r--browser/tools/mozscreenshots/tests/browser/browser_boundingbox.js173
-rw-r--r--browser/tools/mozscreenshots/tests/browser/browser_screenshots.js21
-rw-r--r--browser/tools/mozscreenshots/tests/browser/browser_screenshots_cropping.js153
-rw-r--r--browser/tools/mozscreenshots/tests/xpcshell/test_testConfigurations.js64
-rw-r--r--browser/tools/mozscreenshots/tests/xpcshell/xpcshell.ini5
6 files changed, 424 insertions, 0 deletions
diff --git a/browser/tools/mozscreenshots/tests/browser/browser.ini b/browser/tools/mozscreenshots/tests/browser/browser.ini
new file mode 100644
index 0000000000..c7c7805bca
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/browser/browser.ini
@@ -0,0 +1,8 @@
+[DEFAULT]
+subsuite = screenshots
+support-files =
+ ../../head.js
+
+[browser_boundingbox.js]
+[browser_screenshots.js]
+[browser_screenshots_cropping.js]
diff --git a/browser/tools/mozscreenshots/tests/browser/browser_boundingbox.js b/browser/tools/mozscreenshots/tests/browser/browser_boundingbox.js
new file mode 100644
index 0000000000..cef3fa7de1
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/browser/browser_boundingbox.js
@@ -0,0 +1,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"
+ );
+});
diff --git a/browser/tools/mozscreenshots/tests/browser/browser_screenshots.js b/browser/tools/mozscreenshots/tests/browser/browser_screenshots.js
new file mode 100644
index 0000000000..88f7c6a704
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/browser/browser_screenshots.js
@@ -0,0 +1,21 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/* import-globals-from ../../head.js */
+
+add_task(async function capture() {
+ let setsEnv = Services.env.get("MOZSCREENSHOTS_SETS");
+ if (!setsEnv) {
+ ok(
+ true,
+ "MOZSCREENSHOTS_SETS wasn't specified so there's nothing to capture"
+ );
+ return;
+ }
+
+ let sets = TestRunner.splitEnv(setsEnv.trim());
+ await TestRunner.start(sets);
+});
diff --git a/browser/tools/mozscreenshots/tests/browser/browser_screenshots_cropping.js b/browser/tools/mozscreenshots/tests/browser/browser_screenshots_cropping.js
new file mode 100644
index 0000000000..80fcde5d68
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/browser/browser_screenshots_cropping.js
@@ -0,0 +1,153 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/* import-globals-from ../../head.js */
+
+const { Rect } = ChromeUtils.importESModule(
+ "resource://gre/modules/Geometry.sys.mjs"
+);
+
+async function draw(window, src) {
+ const { document, Image } = window;
+
+ const promise = new Promise((resolve, reject) => {
+ const img = new Image();
+
+ img.onload = function () {
+ // Create a new offscreen canvas
+ const canvas = document.createElementNS(
+ "http://www.w3.org/1999/xhtml",
+ "canvas"
+ );
+ canvas.width = img.naturalWidth;
+ canvas.height = img.naturalHeight;
+ const ctx = canvas.getContext("2d");
+
+ ctx.drawImage(img, 0, 0);
+
+ resolve(canvas);
+ };
+
+ img.onerror = function () {
+ reject(`error loading image ${src}`);
+ };
+
+ // Load the src image for drawing
+ img.src = src;
+ });
+
+ return promise;
+}
+
+async function compareImages(window, expected, test) {
+ const testCanvas = await draw(window, test);
+ const expectedCanvas = await draw(window, expected);
+
+ is(
+ testCanvas.width,
+ expectedCanvas.width,
+ "The test and expected images must be the same size"
+ );
+ is(
+ testCanvas.height,
+ expectedCanvas.height,
+ "The test and expected images must be the same size"
+ );
+
+ var maxDifference = {};
+ var differences = window.windowUtils.compareCanvases(
+ expectedCanvas,
+ testCanvas,
+ maxDifference
+ );
+
+ // Fuzz for minor differences that can be caused by the encoder.
+ if (maxDifference.value > 1) {
+ return differences;
+ }
+ return 0;
+}
+
+async function cropAndCompare(window, src, expected, test, region, subregions) {
+ await TestRunner._cropImage(window, src, region, subregions, test);
+
+ return compareImages(window, expected, PathUtils.toFileURI(test));
+}
+
+add_task(async function crop() {
+ const window = Services.wm.getMostRecentWindow("navigator:browser");
+
+ const tmp = PathUtils.tempDir;
+ is(
+ await cropAndCompare(
+ window,
+ "resource://mozscreenshots/lib/robot.png",
+ "resource://mozscreenshots/lib/robot_upperleft.png",
+ PathUtils.join(tmp, "test_cropped_upperleft.png"),
+ new Rect(0, 0, 32, 32),
+ [new Rect(0, 0, 32, 32)]
+ ),
+ 0,
+ "The image should be cropped to the upper left quadrant"
+ );
+
+ is(
+ await cropAndCompare(
+ window,
+ "resource://mozscreenshots/lib/robot.png",
+ "resource://mozscreenshots/lib/robot_center.png",
+ PathUtils.join(tmp, "test_cropped_center.png"),
+ new Rect(16, 16, 32, 32),
+ [new Rect(16, 16, 32, 32)]
+ ),
+ 0,
+ "The image should be cropped to the center of the image"
+ );
+
+ is(
+ await cropAndCompare(
+ window,
+ "resource://mozscreenshots/lib/robot.png",
+ "resource://mozscreenshots/lib/robot_uncropped.png",
+ PathUtils.join(tmp, "test_uncropped.png"),
+ new Rect(-8, -9, 80, 80),
+ [new Rect(-8, -9, 80, 80)]
+ ),
+ 0,
+ "The image should be not be cropped, and the cropping region should be clipped to the size of the image"
+ );
+
+ is(
+ await cropAndCompare(
+ window,
+ "resource://mozscreenshots/lib/robot.png",
+ "resource://mozscreenshots/lib/robot_diagonal.png",
+ PathUtils.join(tmp, "test_diagonal.png"),
+ new Rect(0, 0, 64, 64),
+ [
+ new Rect(0, 0, 16, 16),
+ new Rect(16, 16, 16, 16),
+ new Rect(32, 32, 16, 16),
+ new Rect(48, 48, 16, 16),
+ ]
+ ),
+ 0,
+ "The image should be contain squares across the diagonal"
+ );
+
+ is(
+ await cropAndCompare(
+ window,
+ "resource://mozscreenshots/lib/robot.png",
+ "resource://mozscreenshots/lib/robot_cropped_diagonal.png",
+ PathUtils.join(tmp, "test_cropped_diagonal.png"),
+ new Rect(16, 16, 48, 48),
+ [new Rect(16, 16, 16, 16), new Rect(32, 32, 16, 16)]
+ ),
+ 0,
+ "The image should be cropped with squares across the diagonal"
+ );
+});
diff --git a/browser/tools/mozscreenshots/tests/xpcshell/test_testConfigurations.js b/browser/tools/mozscreenshots/tests/xpcshell/test_testConfigurations.js
new file mode 100644
index 0000000000..8461e38939
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/xpcshell/test_testConfigurations.js
@@ -0,0 +1,64 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+"use strict";
+
+const { TestRunner } = ChromeUtils.importESModule(
+ "resource://test/TestRunner.sys.mjs"
+);
+
+add_task(async function capture() {
+ equal(TestRunner.findComma("Toolbars,Devs"), 8);
+ equal(TestRunner.findComma("Toolbars"), -1);
+ equal(TestRunner.findComma("Toolbars[onlyNavBar,allToolbars],DevTools"), 32);
+ equal(
+ TestRunner.findComma(
+ "Toolbars[onlyNavBar,allToolbars],DevTools[bottomToolbox,sideToolbox]"
+ ),
+ 32
+ );
+ equal(
+ TestRunner.findComma(
+ "Toolbars[[onlyNavBar],[]], Tabs[ [fiveTabbed], [[[fourPinned]]] ]"
+ ),
+ 25
+ );
+ equal(TestRunner.findComma("[[[[[[[[[[[[[[[[[[[[]]"), -1);
+ equal(TestRunner.findComma("Preferences[[[[[,]]]]]"), -1);
+
+ deepEqual(TestRunner.splitEnv("Toolbars"), ["Toolbars"]);
+ deepEqual(TestRunner.splitEnv("Buttons,Tabs"), ["Buttons", "Tabs"]);
+ deepEqual(TestRunner.splitEnv("Buttons, Tabs"), ["Buttons", "Tabs"]);
+ deepEqual(TestRunner.splitEnv(" Buttons , Tabs "), [
+ "Buttons",
+ "Tabs",
+ ]);
+ deepEqual(TestRunner.splitEnv("Toolbars[onlyNavBar,allToolbars],DevTools"), [
+ "Toolbars[onlyNavBar,allToolbars]",
+ "DevTools",
+ ]);
+ deepEqual(
+ TestRunner.splitEnv(
+ "Toolbars[onlyNavBar,allToolbars],DevTools[bottomToolbox]"
+ ),
+ ["Toolbars[onlyNavBar,allToolbars]", "DevTools[bottomToolbox]"]
+ );
+ deepEqual(
+ TestRunner.splitEnv(
+ "Toolbars[onlyNavBar,allToolbars],DevTools[bottomToolbox],Tabs"
+ ),
+ ["Toolbars[onlyNavBar,allToolbars]", "DevTools[bottomToolbox]", "Tabs"]
+ );
+
+ let filteredData = TestRunner.filterRestrictions("Toolbars[onlyNavBar]");
+ equal(filteredData.trimmedSetName, "Toolbars");
+ ok(filteredData.restrictions.has("onlyNavBar"));
+
+ filteredData = TestRunner.filterRestrictions(
+ "DevTools[bottomToolbox,sideToolbox]"
+ );
+ equal(filteredData.trimmedSetName, "DevTools");
+ ok(filteredData.restrictions.has("bottomToolbox"));
+ ok(filteredData.restrictions.has("sideToolbox"));
+});
diff --git a/browser/tools/mozscreenshots/tests/xpcshell/xpcshell.ini b/browser/tools/mozscreenshots/tests/xpcshell/xpcshell.ini
new file mode 100644
index 0000000000..c318ae4971
--- /dev/null
+++ b/browser/tools/mozscreenshots/tests/xpcshell/xpcshell.ini
@@ -0,0 +1,5 @@
+[DEFAULT]
+skip-if = toolkit == 'android' # bug 1730213
+support-files = ../../mozscreenshots/extension/TestRunner.sys.mjs
+
+[test_testConfigurations.js]