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