Bug 1403682 - Add _findBoundingBox function and tests for mozscreenshots cropping; r?jaws
MozReview-Commit-ID: FSfYa28kKGo
--- a/browser/tools/mozscreenshots/browser.ini
+++ b/browser/tools/mozscreenshots/browser.ini
@@ -1,6 +1,7 @@
[DEFAULT]
subsuite = screenshots
support-files =
head.js
[browser_screenshots.js]
+[browser_boundingbox.js]
new file mode 100644
--- /dev/null
+++ b/browser/tools/mozscreenshots/browser_boundingbox.js
@@ -0,0 +1,32 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+"use strict";
+
+add_task(async function capture() {
+ let rect = TestRunner._findBoundingBox(["#tabbrowser-tabs"]);
+ let tabBar = document.querySelector("#tabbrowser-tabs").getBoundingClientRect();
+ // Check width calculation on simple example
+ is(rect.width, tabBar.width + 20, "Checking _findBoundingBox width calculation");
+ // Check height calculation on simple example
+ is(rect.height, tabBar.height + 20, "Checking _findBoundingBox height caclulation");
+
+ rect = TestRunner._findBoundingBox(["#forward-button", "#TabsToolbar"]);
+ let tabToolbar = document.querySelector("#TabsToolbar").getBoundingClientRect();
+ let fButton = document.querySelector("#forward-button").getBoundingClientRect();
+ let width = Math.max(tabToolbar.right, fButton.right) - Math.min(tabToolbar.left, fButton.left);
+ let height = Math.max(tabToolbar.bottom, fButton.bottom) - Math.min(tabToolbar.top, fButton.top);
+ // Check width calculation on union
+ is(rect.width, width + 20, "Checking _findBoundingBox union width calculation");
+ // Check height calculation on union
+ is(rect.height, height + 20, "Checking _findBoundingBox union height calculation");
+
+ rect = TestRunner._findBoundingBox(["#does_not_exist"]);
+ // Check that 'selector not found' returns null
+ is(rect, null, "Checking that selector not found returns null");
+
+ rect = TestRunner._findBoundingBox([]);
+ // Check that no selectors returns null
+ is(rect, null, "Checking that no selectors returns null");
+});
--- a/browser/tools/mozscreenshots/mozscreenshots/extension/TestRunner.jsm
+++ b/browser/tools/mozscreenshots/mozscreenshots/extension/TestRunner.jsm
@@ -11,16 +11,17 @@ const env = Cc["@mozilla.org/process/env
const APPLY_CONFIG_TIMEOUT_MS = 60 * 1000;
const HOME_PAGE = "chrome://mozscreenshots/content/lib/mozscreenshots.html";
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Timer.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/osfile.jsm");
+Cu.import("resource://gre/modules/Geometry.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "BrowserTestUtils",
"resource://testing-common/BrowserTestUtils.jsm");
Cu.import("chrome://mozscreenshots/content/Screenshot.jsm");
// Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
// See LOG_LEVELS in Console.jsm. Common examples: "All", "Info", "Warn", & "Error".
@@ -154,16 +155,53 @@ this.TestRunner = {
gBrowser.unpinTab(gBrowser.selectedTab);
gBrowser.selectedBrowser.loadURI("data:text/html;charset=utf-8,<h1>Done!");
browserWindow.restore();
Services.prefs.clearUserPref("toolkit.cosmeticAnimations.enabled");
},
// helpers
+ /**
+ * Calculate the bounding box based on CSS selector from config for cropping
+ *
+ * @param {String[]} selectors - array of CSS selectors for relevant DOM element
+ * @return {Geometry.jsm Rect} Rect holding relevant x, y, width, height with 10px padding
+ **/
+ _findBoundingBox(selectors, windowType) {
+ // No selectors provided
+ if (!selectors.length) {
+ log.info("_findBoundingBox: selectors argument is empty");
+ return null;
+ }
+ // Set window type, default "navigator:browser"
+ windowType = windowType || "navigator:browser";
+ let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
+ let element = browserWindow.document.querySelector(selectors[0]);
+ // Selector not found
+ if (!element) {
+ log.info("_findBoundingBox: selector not found");
+ return null;
+ }
+ let box = browserWindow.document.getBoxObjectFor(element);
+ let finalRect = new Rect(box.screenX, box.screenY, box.width, box.height);
+ for (let i = 1; i < selectors.length; i++) {
+ let newElement = browserWindow.document.querySelector(selectors[i]);
+ // Selector not found
+ if (!newElement) {
+ log.info("_findBoundingBox: selector not found");
+ return null;
+ }
+ let newBox = browserWindow.document.getBoxObjectFor(newElement);
+ let newRect = new Rect(newBox.screenX, newBox.screenY, newBox.width, newBox.height);
+ finalRect = finalRect.union(newRect);
+ }
+ return finalRect.inflateFixed(10);
+ },
+
async _performCombo(combo) {
let paddedComboIndex = padLeft(this.currentComboIndex + 1, String(this.combos.length).length);
log.info("Combination " + paddedComboIndex + "/" + this.combos.length + ": " +
this._comboName(combo).substring(1));
function changeConfig(config) {
log.debug("calling " + config.name);
let applyPromise = Promise.resolve(config.applyConfig());
--- a/toolkit/modules/Geometry.jsm
+++ b/toolkit/modules/Geometry.jsm
@@ -325,10 +325,22 @@ Rect.prototype = {
let xAdj = (this.width * xscl - this.width) / 2;
let s = (arguments.length > 1) ? yscl : xscl;
let yAdj = (this.height * s - this.height) / 2;
this.left -= xAdj;
this.right += xAdj;
this.top -= yAdj;
this.bottom += yAdj;
return this;
+ },
+
+ /**
+ * Grows or shrinks the rectangle by fixed amount while keeping the center point.
+ * Accepts single fixed amount
+ */
+ inflateFixed: function inflateFixed(fixed) {
+ this.left -= fixed;
+ this.right += fixed;
+ this.top -= fixed;
+ this.bottom += fixed;
+ return this;
}
};