4 <script src=
"../resources/js-test.js"></script>
6 description("Test battery status API with multiple promises after resolve.");
8 if (!window
.testRunner
)
9 debug('This test cannot be run without the TestRunner');
11 // Clean-up any unused battery manager objects from previous tests.
14 testRunner
.waitUntilDone();
18 function setAndFireMockBatteryInfo(charging
, chargingTime
, dischargingTime
, level
) {
19 mockBatteryInfo
= { charging
: charging
,
20 chargingTime
: chargingTime
,
21 dischargingTime
: dischargingTime
,
23 testRunner
.didChangeBatteryStatus(charging
, chargingTime
, dischargingTime
, level
);
26 // compare obtained battery values with the mock values
27 function checkBatteryInfo(batteryManager
) {
28 batteryInfo
= batteryManager
;
29 shouldBeDefined("batteryInfo");
30 shouldBeDefined("mockBatteryInfo");
31 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
32 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
33 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
34 shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
37 function batteryStatusFailure() {
38 testFailed('failed to successfully resolve the promise');
39 setTimeout(finishJSTest
, 0);
42 function batteryStatusSuccess(battery
) {
43 debug('resolution number 1');
44 checkBatteryInfo(battery
);
46 promise2
= navigator
.getBattery();
49 debug('resolution number 2');
50 checkBatteryInfo(battery
);
51 setTimeout(finishJSTest
, 0);
52 }, batteryStatusFailure
);
53 shouldBeTrue('promise1 === promise2');
56 promise1
= navigator
.getBattery();
57 promise1
.then(batteryStatusSuccess
, batteryStatusFailure
);
58 setAndFireMockBatteryInfo(false, 10, 20, 0.5);