4 <script src=
"../resources/js-test.js"></script>
6 description("Test multiple windows with page visibility.");
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();
15 testRunner
.setCanOpenWindows();
16 testRunner
.setCloseRemainingWindowsWhenComplete(true);
19 function setAndFireMockBatteryInfo(charging
, chargingTime
, dischargingTime
, level
) {
20 mockBatteryInfo
= { charging
: charging
,
21 chargingTime
: chargingTime
,
22 dischargingTime
: dischargingTime
,
24 testRunner
.didChangeBatteryStatus(charging
, chargingTime
, dischargingTime
, level
);
27 // compare obtained battery values with the mock values
28 function checkBatteryInfo(batteryManager
) {
29 batteryInfo
= batteryManager
;
30 shouldBeDefined("batteryInfo");
31 shouldBeDefined("mockBatteryInfo");
32 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
33 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
34 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
35 shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
38 function batteryStatusFailure() {
39 testFailed('failed to successfully resolve the promise');
40 setTimeout(finishJSTest
, 0);
44 function batteryStatusSuccess(batteryManager
) {
45 battery
= batteryManager
;
46 debug('resolution in window');
47 checkBatteryInfo(battery
);
48 battery
.addEventListener('levelchange', failAndFinish
);
51 secondWindow
= window
.open("");
52 secondWindow
.navigator
.getBattery().then(
54 debug('resolution in secondWindow');
55 checkBatteryInfo(battery2
);
56 setTimeout(fireNewMockLevel
, 0);
57 }, batteryStatusFailure
);
59 window
.testRunner
.setPageVisibility("hidden");
60 debug("first window: page is hidden");
63 function fireNewMockLevel() {
64 setAndFireMockBatteryInfo(false, 10, 20, 0.6);
68 function proceedToVisible() {
69 battery
.removeEventListener('levelchange', failAndFinish
);
70 battery
.addEventListener('levelchange', onLevelChange
);
71 testRunner
.setPageVisibility("visible");
72 debug("first window: page is visible");
75 function onLevelChange() {
76 checkBatteryInfo(battery
);
77 battery
.removeEventListener('levelchange', onLevelChange
);
78 setTimeout(finishJSTest
, 0);
81 function failAndFinish() {
82 testFailed('received event while the page was hidden');
83 setTimeout(finishJSTest
, 0);
86 debug("first window: page is visible");
87 navigator
.getBattery().then(batteryStatusSuccess
, batteryStatusFailure
);
88 setAndFireMockBatteryInfo(false, 10, 20, 0.5);