blob: 2e926fd58480266c746b3bda46ae801b1be80352 [file] [log] [blame]
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script>
description("Test battery status API with multiple promises after resolve.");
if (!window.testRunner)
debug('This test cannot be run without the TestRunner');
// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();
var promise1;
var mockBatteryInfo;
function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, level) {
mockBatteryInfo = { charging: charging,
chargingTime: chargingTime,
dischargingTime: dischargingTime,
level: level };
testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, level);
}
// compare obtained battery values with the mock values
function checkBatteryInfo(batteryManager) {
batteryInfo = batteryManager;
shouldBeDefined("batteryInfo");
shouldBeDefined("mockBatteryInfo");
shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
}
function batteryStatusFailure() {
testFailed('failed to successfully resolve the promise');
setTimeout(finishJSTest, 0);
}
function batteryStatusSuccess(battery) {
debug('resolution number 1');
checkBatteryInfo(battery);
promise2 = navigator.getBattery();
promise2.then(
function(battery) {
debug('resolution number 2');
checkBatteryInfo(battery);
setTimeout(finishJSTest, 0);
}, batteryStatusFailure);
shouldBeTrue('promise1 === promise2');
}
promise1 = navigator.getBattery();
promise1.then(batteryStatusSuccess, batteryStatusFailure);
setAndFireMockBatteryInfo(false, 10, 20, 0.5);
</script>
</body>
</html>