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