Backed out changeset 3607e50ad27a (bug 1351078) for build bustage. CLOSED TREE
authorDorel Luca <dluca@mozilla.com>
Fri, 22 Feb 2019 18:35:24 +0200
changeset 460604 176ff7c8871e7df2977659d1f7c9107241bb6318
parent 460603 3607e50ad27a07aea93c2e8b81d8ae05d34fc148
child 460605 d9d6968b7c2f682ee72cd0b4df5c8ada53a788f8
push id35596
push userrmaries@mozilla.com
push dateSat, 23 Feb 2019 04:13:22 +0000
treeherdermozilla-central@fdd04819e350 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1351078
milestone67.0a1
backs out3607e50ad27a07aea93c2e8b81d8ae05d34fc148
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Backed out changeset 3607e50ad27a (bug 1351078) for build bustage. CLOSED TREE
browser/base/content/test/static/browser_all_files_referenced.js
toolkit/modules/Battery.jsm
toolkit/modules/moz.build
toolkit/modules/tests/browser/browser_Battery.js
tools/lint/eslint/modules.json
--- a/browser/base/content/test/static/browser_all_files_referenced.js
+++ b/browser/base/content/test/static/browser_all_files_referenced.js
@@ -139,16 +139,18 @@ var whitelist = [
   // Bug 1348533
   {file: "chrome://mozapps/skin/downloads/buttons.png", platforms: ["macosx"]},
   {file: "chrome://mozapps/skin/downloads/downloadButtons.png", platforms: ["linux", "win"]},
   // Bug 1348558
   {file: "chrome://mozapps/skin/update/downloadButtons.png",
    platforms: ["linux"]},
   // Bug 1348559
   {file: "chrome://pippki/content/resetpassword.xul"},
+  // Bug 1351078
+  {file: "resource://gre/modules/Battery.jsm"},
   // Bug 1337345
   {file: "resource://gre/modules/Manifest.jsm"},
   // Bug 1351097
   {file: "resource://gre/modules/accessibility/AccessFu.jsm"},
   // Bug 1356043
   {file: "resource://gre/modules/PerfMeasurement.jsm"},
   // Bug 1356045
   {file: "chrome://global/content/test-ipc.xul"},
new file mode 100644
--- /dev/null
+++ b/toolkit/modules/Battery.jsm
@@ -0,0 +1,67 @@
+// -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// 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";
+
+/** This module wraps around navigator.getBattery (https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getBattery).
+  * and provides a framework for spoofing battery values in test code.
+  * To spoof the battery values, set `Debugging.fake = true` after exporting this with a BackstagePass,
+  * after which you can spoof a property yb setting the relevant property of the BatteryManager object.
+  */
+var EXPORTED_SYMBOLS = ["GetBattery", "Battery"];
+
+// Load Services, for the BatteryManager API
+ChromeUtils.defineModuleGetter(this, "Services",
+  "resource://gre/modules/Services.jsm");
+
+// Values for the fake battery. See the documentation of Navigator.battery for the meaning of each field.
+var gFakeBattery = {
+  charging: false,
+  chargingTime: 0,
+  dischargingTime: Infinity,
+  level: 1,
+};
+
+// BackendPass-exported object for toggling spoofing
+var Debugging = {
+  /**
+   * If `false`, use the DOM Battery implementation.
+   * Set it to `true` if you need to fake battery values
+   * for testing or debugging purposes.
+   */
+  fake: false,
+};
+
+var GetBattery = function() {
+  return new Services.appShell.hiddenDOMWindow.Promise(function(resolve, reject) {
+    // Return fake values if spoofing is enabled, otherwise fetch the real values from the BatteryManager API
+    if (Debugging.fake) {
+      resolve(gFakeBattery);
+      return;
+    }
+    Services.appShell.hiddenDOMWindow.navigator.getBattery().then(resolve, reject);
+  });
+};
+
+var Battery = {};
+
+for (let k of ["charging", "chargingTime", "dischargingTime", "level"]) {
+  let prop = k;
+  Object.defineProperty(this.Battery, prop, {
+    get() {
+      // Return fake value if spoofing is enabled, otherwise fetch the real value from the BatteryManager API
+      if (Debugging.fake) {
+        return gFakeBattery[prop];
+      }
+      return Services.appShell.hiddenDOMWindow.navigator.battery[prop];
+    },
+    set(fakeSetting) {
+      if (!Debugging.fake) {
+        throw new Error("Tried to set fake battery value when battery spoofing was disabled");
+      }
+      gFakeBattery[prop] = fakeSetting;
+    },
+  });
+}
--- a/toolkit/modules/moz.build
+++ b/toolkit/modules/moz.build
@@ -181,16 +181,17 @@ EXTRA_JS_MODULES += [
     'addons/WebRequest.jsm',
     'addons/WebRequestCommon.jsm',
     'addons/WebRequestContent.js',
     'addons/WebRequestUpload.jsm',
     'AppMenuNotifications.jsm',
     'AsyncPrefs.jsm',
     'AutoCompletePopupContent.jsm',
     'AutoScrollController.jsm',
+    'Battery.jsm',
     'BinarySearch.jsm',
     'BrowserUtils.jsm',
     'CanonicalJSON.jsm',
     'CertUtils.jsm',
     'CharsetMenu.jsm',
     'ClientID.jsm',
     'Color.jsm',
     'Console.jsm',
new file mode 100644
--- /dev/null
+++ b/toolkit/modules/tests/browser/browser_Battery.js
@@ -0,0 +1,51 @@
+/* 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";
+var {GetBattery, Debugging} = ChromeUtils.import("resource://gre/modules/Battery.jsm", null);
+ChromeUtils.import("resource://gre/modules/Services.jsm", this);
+
+function test() {
+  waitForExplicitFinish();
+
+  is(Debugging.fake, false, "Battery spoofing is initially false");
+
+  GetBattery().then(function(battery) {
+    for (let k of ["charging", "chargingTime", "dischargingTime", "level"]) {
+      let backup = battery[k];
+      try {
+        battery[k] = "__magic__";
+      } catch (e) {
+        // We are testing that we cannot set battery to new values
+        // when "use strict" is enabled, this throws a TypeError
+        if (e.name != "TypeError")
+          throw e;
+      }
+      is(battery[k], backup, "Setting battery " + k + " preference without spoofing enabled should fail");
+    }
+
+    Debugging.fake = true;
+
+    // reload again to get the fake one
+    GetBattery().then(function(battery) {
+      battery.charging = true;
+      battery.chargingTime = 100;
+      battery.level = 0.5;
+      ok(battery.charging, "Test for charging setter");
+      is(battery.chargingTime, 100, "Test for chargingTime setter");
+      is(battery.level, 0.5, "Test for level setter");
+
+      battery.charging = false;
+      battery.dischargingTime = 50;
+      battery.level = 0.7;
+      ok(!battery.charging, "Test for charging setter");
+      is(battery.dischargingTime, 50, "Test for dischargingTime setter");
+      is(battery.level, 0.7, "Test for level setter");
+
+      // Resetting the value to make the test run successful
+      // for multiple runs in same browser session.
+      Debugging.fake = false;
+      finish();
+    });
+  });
+}
--- a/tools/lint/eslint/modules.json
+++ b/tools/lint/eslint/modules.json
@@ -13,16 +13,17 @@
   "ajv-4.1.1.js": ["Ajv"],
   "AlertsHelper.jsm": [],
   "AppData.jsm": ["makeFakeAppDir"],
   "AppInfo.jsm": ["newAppInfo", "getAppInfo", "updateAppInfo"],
   "ASRouterTargeting.jsm": ["ASRouterTargeting", "QueryCache", "CachedTargetingGetter"],
   "async.js": ["Async"],
   "AsyncSpellCheckTestHelper.jsm": ["onSpellCheck"],
   "base-loader.js": ["Loader", "resolveURI", "Module", "Require", "unload"],
+  "Battery.jsm": ["GetBattery", "Battery"],
   "blocklist-clients.js": ["AddonBlocklistClient", "GfxBlocklistClient", "OneCRLBlocklistClient", "PluginBlocklistClient"],
   "blocklist-updater.js": ["checkVersions", "addTestBlocklistClient"],
   "bogus_element_type.jsm": [],
   "bookmarks.js": ["BookmarksEngine", "PlacesItem", "Bookmark", "BookmarkFolder", "BookmarkQuery", "Livemark", "BookmarkSeparator", "BufferedBookmarksEngine"],
   "bookmarks.jsm": ["PlacesItem", "Bookmark", "Separator", "Livemark", "BookmarkFolder", "DumpBookmarks"],
   "bookmark_repair.js": ["BookmarkRepairRequestor", "BookmarkRepairResponder"],
   "bookmark_validator.js": ["BookmarkValidator", "BookmarkProblemData"],
   "browser-loader.js": ["BrowserLoader"],