2014-07-01 07:50:00 +04:00
|
|
|
// -*- 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";
|
|
|
|
|
2015-08-21 13:29:25 +03:00
|
|
|
/** This module wraps around navigator.getBattery (https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getBattery).
|
2014-07-01 07:50:00 +04:00
|
|
|
* 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,
|
2015-08-21 13:29:25 +03:00
|
|
|
* after which you can spoof a property yb setting the relevant property of the BatteryManager object.
|
2014-07-01 07:50:00 +04:00
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["GetBattery", "Battery"];
|
2014-07-01 07:50:00 +04:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
|
2014-07-01 07:50:00 +04:00
|
|
|
|
|
|
|
// Load Services, for the BatteryManager API
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "Services",
|
2014-07-01 07:50:00 +04:00
|
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
// Values for the fake battery. See the documentation of Navigator.battery for the meaning of each field.
|
2015-09-15 21:19:45 +03:00
|
|
|
var gFakeBattery = {
|
2014-07-01 07:50:00 +04:00
|
|
|
charging: false,
|
|
|
|
chargingTime: 0,
|
|
|
|
dischargingTime: Infinity,
|
|
|
|
level: 1,
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2014-07-01 07:50:00 +04:00
|
|
|
|
|
|
|
// BackendPass-exported object for toggling spoofing
|
2018-02-23 22:50:01 +03:00
|
|
|
var Debugging = {
|
2014-07-01 07:50:00 +04:00
|
|
|
/**
|
|
|
|
* 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
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2014-07-01 07:50:00 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var GetBattery = function() {
|
2016-11-12 02:22:34 +03:00
|
|
|
return new Services.appShell.hiddenDOMWindow.Promise(function(resolve, reject) {
|
2015-08-21 13:29:25 +03:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var Battery = {};
|
2014-07-01 07:50:00 +04:00
|
|
|
|
|
|
|
for (let k of ["charging", "chargingTime", "dischargingTime", "level"]) {
|
|
|
|
let prop = k;
|
|
|
|
Object.defineProperty(this.Battery, prop, {
|
2016-12-30 02:34:54 +03:00
|
|
|
get() {
|
2014-07-01 07:50:00 +04:00
|
|
|
// 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];
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
set(fakeSetting) {
|
2014-07-01 07:50:00 +04:00
|
|
|
if (!Debugging.fake) {
|
2015-08-21 13:29:25 +03:00
|
|
|
throw new Error("Tried to set fake battery value when battery spoofing was disabled");
|
2014-07-01 07:50:00 +04:00
|
|
|
}
|
|
|
|
gFakeBattery[prop] = fakeSetting;
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
});
|
2015-08-21 13:29:25 +03:00
|
|
|
}
|