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 battery status API with multiple promises after resolve."); | 8 description("Test battery status API with multiple promises after resolve."); |
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 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 } | |
43 | 19 |
44 function batteryStatusSuccess(battery) { | 20 function batteryStatusSuccess(battery) { |
45 debug('resolution number 1'); | 21 debug('resolution number 1'); |
46 checkBatteryInfo(battery); | 22 testIfBatteryStatusIsUpToDate(battery); |
47 | 23 |
48 promise2 = navigator.getBattery(); | 24 promise2 = navigator.getBattery(); |
49 promise2.then( | 25 promise2.then( |
50 function(battery) { | 26 function(battery) { |
51 debug('resolution number 2'); | 27 debug('resolution number 2'); |
52 checkBatteryInfo(battery); | 28 testIfBatteryStatusIsUpToDate(battery); |
53 setTimeout(finishJSTest, 0); | 29 setTimeout(finishJSTest, 0); |
54 }, batteryStatusFailure); | 30 }, batteryStatusFailure); |
55 shouldBeTrue('promise1 === promise2'); | 31 shouldBeTrue('promise1 === promise2'); |
56 } | 32 } |
57 | 33 |
58 promise1 = navigator.getBattery(); | 34 mockBatteryMonitorReady.then(() => { |
59 promise1.then(batteryStatusSuccess, batteryStatusFailure); | 35 promise1 = navigator.getBattery(); |
60 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | 36 promise1.then(batteryStatusSuccess, batteryStatusFailure); |
| 37 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 38 }); |
61 </script> | 39 </script> |
62 </body> | 40 </body> |
63 </html> | 41 </html> |
OLD | NEW |