OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <body> |
| 4 <script src="../resources/js-test.js"></script> |
| 5 <script> |
| 6 description("Test with page visibility."); |
| 7 |
| 8 window.jsTestIsAsync = true; |
| 9 if (!window.testRunner) |
| 10 debug('This test cannot be run without the TestRunner'); |
| 11 |
| 12 var mockBatteryInfo; |
| 13 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve
l) { |
| 14 mockBatteryInfo = { charging: charging, |
| 15 chargingTime: chargingTime, |
| 16 dischargingTime: dischargingTime, |
| 17 level: level }; |
| 18 testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, l
evel); |
| 19 } |
| 20 |
| 21 // compare obtained battery values with the mock values |
| 22 function checkBatteryInfo(batteryManager) { |
| 23 batteryInfo = batteryManager; |
| 24 shouldBeDefined("batteryInfo"); |
| 25 shouldBeDefined("mockBatteryInfo"); |
| 26 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); |
| 27 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); |
| 28 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); |
| 29 shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); |
| 30 } |
| 31 |
| 32 function batteryStatusFailure() { |
| 33 testFailed('failed to successfully resolve the promise'); |
| 34 setTimeout(cleanupAndFinish, 0); |
| 35 } |
| 36 |
| 37 var battery; |
| 38 function batteryStatusSuccess(batteryManager) { |
| 39 battery = batteryManager; |
| 40 checkBatteryInfo(battery); |
| 41 battery.addEventListener('levelchange', failAndFinish); |
| 42 testRunner.setPageVisibility("hidden"); |
| 43 debug("page is hidden"); |
| 44 setAndFireMockBatteryInfo(false, 10, 20, 0.55); |
| 45 setTimeout(testWithVisiblePage, 100); |
| 46 } |
| 47 |
| 48 function testWithVisiblePage() { |
| 49 battery.removeEventListener('levelchange', failAndFinish); |
| 50 battery.addEventListener('levelchange', onLevelChange); |
| 51 testRunner.setPageVisibility("visible"); |
| 52 debug("page is visible"); |
| 53 setAndFireMockBatteryInfo(false, 10, 20, 0.6); |
| 54 } |
| 55 |
| 56 function onLevelChange() { |
| 57 checkBatteryInfo(battery); |
| 58 setTimeout(cleanupAndFinish, 0); |
| 59 } |
| 60 |
| 61 function cleanupAndFinish() { |
| 62 // stops updating battery and clears current battery status. |
| 63 window.testRunner.setPageVisibility("hidden"); |
| 64 finishJSTest(); |
| 65 } |
| 66 |
| 67 function failAndFinish() { |
| 68 testFailed('received event while the page was hidden'); |
| 69 setTimeout(cleanupAndFinish, 0); |
| 70 } |
| 71 |
| 72 debug("page is visible"); |
| 73 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); |
| 74 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 75 </script> |
| 76 </body> |
| 77 </html> |
OLD | NEW |