| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <body> | 3 <body> |
| 4 <script src="../resources/js-test.js"></script> | 4 <script src="../resources/js-test.js"></script> |
| 5 <script src="../resources/mojo-helpers.js"></script> |
| 6 <script src="resources/mock-battery-monitor.js"></script> |
| 5 <script> | 7 <script> |
| 6 description("Test with page visibility."); | 8 description("Test with page visibility."); |
| 7 | 9 |
| 8 if (!window.testRunner) | 10 if (!window.testRunner) |
| 9 debug('This test cannot be run without the TestRunner'); | 11 debug('This test cannot be run without the TestRunner'); |
| 10 if (!window.internals) | 12 if (!window.internals) |
| 11 debug('This test cannot be run without the window.internals'); | 13 debug('This test cannot be run without the window.internals'); |
| 12 | 14 |
| 13 // Clean-up any unused battery manager objects from previous tests. | 15 // Clean-up any unused battery manager objects from previous tests. |
| 14 gc(); | 16 gc(); |
| 15 jsTestIsAsync = true; | 17 jsTestIsAsync = true; |
| 16 testRunner.waitUntilDone(); | 18 testRunner.waitUntilDone(); |
| 17 | 19 |
| 18 var mockBatteryInfo; | |
| 19 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve
l) { | |
| 20 mockBatteryInfo = { charging: charging, | |
| 21 chargingTime: chargingTime, | |
| 22 dischargingTime: dischargingTime, | |
| 23 level: level }; | |
| 24 window.internals.updateBatteryStatus(charging, chargingTime, dischargingTime
, level); | |
| 25 } | |
| 26 | |
| 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'); | |
| 36 } | |
| 37 | |
| 38 function batteryStatusFailure() { | |
| 39 testFailed('failed to successfully resolve the promise'); | |
| 40 setTimeout(cleanupAndFinish, 0); | |
| 41 } | |
| 42 | |
| 43 var battery; | 20 var battery; |
| 44 function batteryStatusSuccess(batteryManager) { | 21 function batteryStatusSuccess(batteryManager) { |
| 45 battery = batteryManager; | 22 battery = batteryManager; |
| 46 checkBatteryInfo(battery); | 23 checkBatteryInfo(battery); |
| 47 battery.addEventListener('levelchange', failAndFinish); | 24 battery.addEventListener('levelchange', failAndFinish); |
| 48 testRunner.setPageVisibility("hidden"); | 25 testRunner.setPageVisibility("hidden"); |
| 49 debug("page is hidden"); | 26 debug("page is hidden"); |
| 50 setTimeout(fireNextMockBatteryInfo, 0); | 27 setTimeout(fireNextMockBatteryInfo, 0); |
| 51 } | 28 } |
| 52 | 29 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 68 battery.removeEventListener('levelchange', onLevelChange); | 45 battery.removeEventListener('levelchange', onLevelChange); |
| 69 setTimeout(finishJSTest, 0); | 46 setTimeout(finishJSTest, 0); |
| 70 } | 47 } |
| 71 | 48 |
| 72 function failAndFinish() { | 49 function failAndFinish() { |
| 73 testFailed('received event while the page was hidden'); | 50 testFailed('received event while the page was hidden'); |
| 74 setTimeout(finishJSTest, 0); | 51 setTimeout(finishJSTest, 0); |
| 75 } | 52 } |
| 76 | 53 |
| 77 debug("page is visible"); | 54 debug("page is visible"); |
| 78 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); | 55 ready.then(() => { |
| 79 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | 56 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); |
| 57 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 58 }); |
| 80 </script> | 59 </script> |
| 81 </body> | 60 </body> |
| 82 </html> | 61 </html> |
| OLD | NEW |