| 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) | |
| 11 debug('This test cannot be run without the window.internals'); | |
| 12 | 12 |
| 13 // Clean-up any unused battery manager objects from previous tests. | 13 // Clean-up any unused battery manager objects from previous tests. |
| 14 gc(); | 14 gc(); |
| 15 jsTestIsAsync = true; | 15 jsTestIsAsync = true; |
| 16 testRunner.waitUntilDone(); | 16 testRunner.waitUntilDone(); |
| 17 | 17 |
| 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; | 18 var battery; |
| 44 function batteryStatusSuccess(batteryManager) { | 19 function batteryStatusSuccess(batteryManager) { |
| 45 battery = batteryManager; | 20 battery = batteryManager; |
| 46 checkBatteryInfo(battery); | 21 testIfBatteryStatusIsUpToDate(battery); |
| 47 battery.addEventListener('levelchange', failAndFinish); | 22 battery.addEventListener('levelchange', failAndFinish); |
| 48 testRunner.setPageVisibility("hidden"); | 23 testRunner.setPageVisibility("hidden"); |
| 49 debug("page is hidden"); | 24 debug("page is hidden"); |
| 50 setTimeout(fireNextMockBatteryInfo, 0); | 25 setTimeout(fireNextMockBatteryInfo, 0); |
| 51 } | 26 } |
| 52 | 27 |
| 53 function fireNextMockBatteryInfo() { | 28 function fireNextMockBatteryInfo() { |
| 54 setAndFireMockBatteryInfo(false, 10, 20, 0.55); | 29 setAndFireMockBatteryInfo(false, 10, 20, 0.55); |
| 55 testWithVisiblePage(); | 30 testWithVisiblePage(); |
| 56 } | 31 } |
| 57 | 32 |
| 58 function testWithVisiblePage() { | 33 function testWithVisiblePage() { |
| 59 battery.removeEventListener('levelchange', failAndFinish); | 34 battery.removeEventListener('levelchange', failAndFinish); |
| 60 battery.addEventListener('levelchange', onLevelChange); | 35 battery.addEventListener('levelchange', onLevelChange); |
| 61 testRunner.setPageVisibility("visible"); | 36 testRunner.setPageVisibility("visible"); |
| 62 debug("page is visible"); | 37 debug("page is visible"); |
| 63 setAndFireMockBatteryInfo(false, 10, 20, 0.6); | 38 setAndFireMockBatteryInfo(false, 10, 20, 0.6); |
| 64 } | 39 } |
| 65 | 40 |
| 66 function onLevelChange() { | 41 function onLevelChange() { |
| 67 checkBatteryInfo(battery); | 42 testIfBatteryStatusIsUpToDate(battery); |
| 68 battery.removeEventListener('levelchange', onLevelChange); | 43 battery.removeEventListener('levelchange', onLevelChange); |
| 69 setTimeout(finishJSTest, 0); | 44 setTimeout(finishJSTest, 0); |
| 70 } | 45 } |
| 71 | 46 |
| 72 function failAndFinish() { | 47 function failAndFinish() { |
| 73 testFailed('received event while the page was hidden'); | 48 testFailed('received event while the page was hidden'); |
| 74 setTimeout(finishJSTest, 0); | 49 setTimeout(finishJSTest, 0); |
| 75 } | 50 } |
| 76 | 51 |
| 77 debug("page is visible"); | 52 debug("page is visible"); |
| 78 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); | 53 mockBatteryMonitorReady.then(() => { |
| 79 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | 54 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); |
| 55 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 56 }); |
| 80 </script> | 57 </script> |
| 81 </body> | 58 </body> |
| 82 </html> | 59 </html> |
| OLD | NEW |