Skip to content

Commit

Permalink
Bug 1561435 - Format browser/tools/, a=automatic-formatting
Browse files Browse the repository at this point in the history
# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D36048

--HG--
extra : source : fe526ac7e45be073f1074edeee508a22063bac4c
  • Loading branch information
Victor Porof committed Jul 5, 2019
1 parent af7d1d3 commit 6456074
Show file tree
Hide file tree
Showing 24 changed files with 857 additions and 378 deletions.
162 changes: 115 additions & 47 deletions browser/tools/mozscreenshots/browser_boundingbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
"use strict";

add_task(async function() {
const scale = window.docShell
.QueryInterface(Ci.nsIBaseWindow)
.devicePixelsPerDesktopPixel;
let {bounds, rects} = TestRunner._findBoundingBox(["#tabbrowser-tabs"]);
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;
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;
Expand All @@ -30,19 +31,37 @@ add_task(async function() {
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");
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");
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;
Expand All @@ -54,16 +73,28 @@ add_task(async function() {
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 );
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);
Expand All @@ -72,31 +103,68 @@ add_task(async function() {
expectedBottom = Math.min(expectedBottom, windowBottom);

// Check width calculation on union
is(bounds.width, expectedRight - expectedLeft,
"Checking _findBoundingBox union width calculation");
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");
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");
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");
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");
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");
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 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");
Assert.throws(
() => {
TestRunner._findBoundingBox([]);
},
/No selectors specified/,
"Checking that no selectors throws an exception"
);
});
5 changes: 4 additions & 1 deletion browser/tools/mozscreenshots/browser_screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
add_task(async function capture() {
let setsEnv = env.get("MOZSCREENSHOTS_SETS");
if (!setsEnv) {
ok(true, "MOZSCREENSHOTS_SETS wasn't specified so there's nothing to capture");
ok(
true,
"MOZSCREENSHOTS_SETS wasn't specified so there's nothing to capture"
);
return;
}

Expand Down
62 changes: 45 additions & 17 deletions browser/tools/mozscreenshots/browser_screenshots_cropping.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ async function draw(window, src) {

img.onload = function() {
// Create a new offscreen canvas
const canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "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");
Expand All @@ -39,8 +42,16 @@ 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");
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"
);

return window.windowUtils.compareCanvases(expectedCanvas, testCanvas, {});
}
Expand All @@ -55,34 +66,47 @@ add_task(async function crop() {
const window = Services.wm.getMostRecentWindow("navigator:browser");

const tmp = OS.Constants.Path.tmpDir;
is(await cropAndCompare(
is(
await cropAndCompare(
window,
"resource://mozscreenshots/lib/robot.png",
"resource://mozscreenshots/lib/robot_upperleft.png",
OS.Path.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");
),
0,
"The image should be cropped to the upper left quadrant"
);

is(await cropAndCompare(
is(
await cropAndCompare(
window,
"resource://mozscreenshots/lib/robot.png",
"resource://mozscreenshots/lib/robot_center.png",
OS.Path.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");
),
0,
"The image should be cropped to the center of the image"
);

is(await cropAndCompare(
is(
await cropAndCompare(
window,
"resource://mozscreenshots/lib/robot.png",
"resource://mozscreenshots/lib/robot_uncropped.png",
OS.Path.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");
),
0,
"The image should be not be cropped, and the cropping region should be clipped to the size of the image"
);

is(await cropAndCompare(
is(
await cropAndCompare(
window,
"resource://mozscreenshots/lib/robot.png",
"resource://mozscreenshots/lib/robot_diagonal.png",
Expand All @@ -94,17 +118,21 @@ add_task(async function crop() {
new Rect(32, 32, 16, 16),
new Rect(48, 48, 16, 16),
]
), 0, "The image should be contain squares across the diagonal");
),
0,
"The image should be contain squares across the diagonal"
);

is(await cropAndCompare(
is(
await cropAndCompare(
window,
"resource://mozscreenshots/lib/robot.png",
"resource://mozscreenshots/lib/robot_cropped_diagonal.png",
OS.Path.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");
[new Rect(16, 16, 16, 16), new Rect(32, 32, 16, 16)]
),
0,
"The image should be cropped with squares across the diagonal"
);
});
8 changes: 6 additions & 2 deletions browser/tools/mozscreenshots/devtools/browser_devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

"use strict";

const {gDevTools} = ChromeUtils.import("resource://devtools/client/framework/gDevTools.jsm");
const {devtools: {TargetFactory}} = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
const { gDevTools } = ChromeUtils.import(
"resource://devtools/client/framework/gDevTools.jsm"
);
const {
devtools: { TargetFactory },
} = ChromeUtils.import("resource://devtools/shared/Loader.jsm");

add_task(async function capture() {
if (!shouldCapture()) {
Expand Down
29 changes: 21 additions & 8 deletions browser/tools/mozscreenshots/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

"use strict";

const chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIChromeRegistry);
const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
const EXTENSION_DIR = "chrome://mochitests/content/browser/browser/tools/mozscreenshots/mozscreenshots/extension/mozscreenshots/browser/";
const chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
Ci.nsIChromeRegistry
);
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
const EXTENSION_DIR =
"chrome://mochitests/content/browser/browser/tools/mozscreenshots/mozscreenshots/extension/mozscreenshots/browser/";

let TestRunner;

Expand All @@ -22,14 +27,19 @@ async function setup() {

info("installing extension temporarily");
let chromeURL = Services.io.newURI(EXTENSION_DIR);
let dir = chromeRegistry.convertChromeURL(chromeURL).QueryInterface(Ci.nsIFileURL).file;
let dir = chromeRegistry
.convertChromeURL(chromeURL)
.QueryInterface(Ci.nsIFileURL).file;
await AddonManager.installTemporaryAddon(dir);

info("Checking for mozscreenshots extension");
return new Promise(async (resolve) => {
return new Promise(async resolve => {
let aAddon = await AddonManager.getAddonByID("[email protected]");
isnot(aAddon, null, "The mozscreenshots extension should be installed");
TestRunner = ChromeUtils.import("resource://mozscreenshots/TestRunner.jsm", {}).TestRunner;
TestRunner = ChromeUtils.import(
"resource://mozscreenshots/TestRunner.jsm",
{}
).TestRunner;
TestRunner.initTest(this);
resolve();
});
Expand All @@ -42,8 +52,11 @@ async function setup() {
*/
function shouldCapture() {
if (env.get("MOZSCREENSHOTS_SETS")) {
ok(true, "MOZSCREENSHOTS_SETS was specified so only capture what was " +
"requested (in browser_screenshots.js)");
ok(
true,
"MOZSCREENSHOTS_SETS was specified so only capture what was " +
"requested (in browser_screenshots.js)"
);
return false;
}

Expand Down
Loading

0 comments on commit 6456074

Please sign in to comment.