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 battery status API with multiple promises after resolve."); | 6 description("Test battery status API with multiple promises after resolve."); |
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 promise1; | 18 var promise1; |
| 19 var mockBatteryInfo; |
| 20 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve
l) { |
| 21 mockBatteryInfo = { charging: charging, |
| 22 chargingTime: chargingTime, |
| 23 dischargingTime: dischargingTime, |
| 24 level: level }; |
| 25 window.internals.updateBatteryStatus(charging, chargingTime, dischargingTime
, level); |
| 26 } |
| 27 |
| 28 // compare obtained battery values with the mock values |
| 29 function checkBatteryInfo(batteryManager) { |
| 30 batteryInfo = batteryManager; |
| 31 shouldBeDefined("batteryInfo"); |
| 32 shouldBeDefined("mockBatteryInfo"); |
| 33 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); |
| 34 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); |
| 35 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); |
| 36 shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); |
| 37 } |
| 38 |
| 39 function batteryStatusFailure() { |
| 40 testFailed('failed to successfully resolve the promise'); |
| 41 setTimeout(finishJSTest, 0); |
| 42 } |
19 | 43 |
20 function batteryStatusSuccess(battery) { | 44 function batteryStatusSuccess(battery) { |
21 debug('resolution number 1'); | 45 debug('resolution number 1'); |
22 testIfBatteryStatusIsUpToDate(battery); | 46 checkBatteryInfo(battery); |
23 | 47 |
24 promise2 = navigator.getBattery(); | 48 promise2 = navigator.getBattery(); |
25 promise2.then( | 49 promise2.then( |
26 function(battery) { | 50 function(battery) { |
27 debug('resolution number 2'); | 51 debug('resolution number 2'); |
28 testIfBatteryStatusIsUpToDate(battery); | 52 checkBatteryInfo(battery); |
29 setTimeout(finishJSTest, 0); | 53 setTimeout(finishJSTest, 0); |
30 }, batteryStatusFailure); | 54 }, batteryStatusFailure); |
31 shouldBeTrue('promise1 === promise2'); | 55 shouldBeTrue('promise1 === promise2'); |
32 } | 56 } |
33 | 57 |
34 mockBatteryMonitorReady.then(() => { | 58 promise1 = navigator.getBattery(); |
35 promise1 = navigator.getBattery(); | 59 promise1.then(batteryStatusSuccess, batteryStatusFailure); |
36 promise1.then(batteryStatusSuccess, batteryStatusFailure); | 60 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
37 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | |
38 }); | |
39 </script> | 61 </script> |
40 </body> | 62 </body> |
41 </html> | 63 </html> |
OLD | NEW |