4 <script src=
"../resources/js-test.js"></script>
6 description("Test 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();
17 function setAndFireMockBatteryInfo(charging
, chargingTime
, dischargingTime
, level
) {
18 mockBatteryInfo
= { charging
: charging
,
19 chargingTime
: chargingTime
,
20 dischargingTime
: dischargingTime
,
22 testRunner
.didChangeBatteryStatus(charging
, chargingTime
, dischargingTime
, level
);
25 // compare obtained battery values with the mock values
26 function checkBatteryInfo(batteryManager
) {
27 batteryInfo
= batteryManager
;
28 shouldBeDefined("batteryInfo");
29 shouldBeDefined("mockBatteryInfo");
30 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
31 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
32 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
33 shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
36 function batteryStatusFailure() {
37 testFailed('failed to successfully resolve the promise');
38 setTimeout(cleanupAndFinish
, 0);
42 function batteryStatusSuccess(batteryManager
) {
43 battery
= batteryManager
;
44 checkBatteryInfo(battery
);
45 battery
.addEventListener('levelchange', failAndFinish
);
46 testRunner
.setPageVisibility("hidden");
47 debug("page is hidden");
48 setTimeout(fireNextMockBatteryInfo
, 0);
51 function fireNextMockBatteryInfo() {
52 setAndFireMockBatteryInfo(false, 10, 20, 0.55);
53 testWithVisiblePage();
56 function testWithVisiblePage() {
57 battery
.removeEventListener('levelchange', failAndFinish
);
58 battery
.addEventListener('levelchange', onLevelChange
);
59 testRunner
.setPageVisibility("visible");
60 debug("page is visible");
61 setAndFireMockBatteryInfo(false, 10, 20, 0.6);
64 function onLevelChange() {
65 checkBatteryInfo(battery
);
66 battery
.removeEventListener('levelchange', onLevelChange
);
67 setTimeout(finishJSTest
, 0);
70 function failAndFinish() {
71 testFailed('received event while the page was hidden');
72 setTimeout(finishJSTest
, 0);
75 debug("page is visible");
76 navigator
.getBattery().then(batteryStatusSuccess
, batteryStatusFailure
);
77 setAndFireMockBatteryInfo(false, 10, 20, 0.5);