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 basic API definitions."); | 8 description("Test basic API definitions."); |
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 var battery; | 18 var battery; |
28 function batteryStatusSuccess(batteryManager) { | 19 function batteryStatusSuccess(batteryManager) { |
29 debug('batteryStatusSuccess invoked'); | 20 debug('batteryStatusSuccess invoked'); |
30 battery = batteryManager; | 21 battery = batteryManager; |
31 | 22 |
32 shouldBeDefined("battery"); | 23 shouldBeDefined("battery"); |
33 shouldBeNonNull("battery"); | 24 shouldBeNonNull("battery"); |
34 shouldBeDefined("battery.charging"); | 25 shouldBeDefined("battery.charging"); |
35 shouldBeDefined("battery.chargingTime"); | 26 shouldBeDefined("battery.chargingTime"); |
36 shouldBeDefined("battery.dischargingTime"); | 27 shouldBeDefined("battery.dischargingTime"); |
37 shouldBeDefined("battery.level"); | 28 shouldBeDefined("battery.level"); |
38 | 29 |
39 shouldBe('battery.charging', 'mockBatteryInfo.charging'); | 30 testIfBatteryStatusIsUpToDate(batteryManager); |
40 shouldBe('battery.chargingTime', 'mockBatteryInfo.chargingTime'); | |
41 shouldBe('battery.dischargingTime', 'mockBatteryInfo.dischargingTime'); | |
42 shouldBe('battery.level', 'mockBatteryInfo.level'); | |
43 | 31 |
44 shouldBeTrue("typeof battery.onchargingchange == 'object'"); | 32 shouldBeTrue("typeof battery.onchargingchange == 'object'"); |
45 shouldBeTrue("typeof battery.onchargingtimechange == 'object'"); | 33 shouldBeTrue("typeof battery.onchargingtimechange == 'object'"); |
46 shouldBeTrue("typeof battery.ondischargingtimechange == 'object'"); | 34 shouldBeTrue("typeof battery.ondischargingtimechange == 'object'"); |
47 shouldBeTrue("typeof battery.onlevelchange == 'object'"); | 35 shouldBeTrue("typeof battery.onlevelchange == 'object'"); |
48 | 36 |
49 shouldBeTrue("'onchargingchange' in battery"); | 37 shouldBeTrue("'onchargingchange' in battery"); |
50 shouldBeTrue("'onchargingtimechange' in battery"); | 38 shouldBeTrue("'onchargingtimechange' in battery"); |
51 shouldBeTrue("'ondischargingtimechange' in battery"); | 39 shouldBeTrue("'ondischargingtimechange' in battery"); |
52 shouldBeTrue("'onlevelchange' in battery"); | 40 shouldBeTrue("'onlevelchange' in battery"); |
53 | 41 |
54 setTimeout(finishJSTest, 0); | 42 setTimeout(finishJSTest, 0); |
55 } | 43 } |
56 | 44 |
57 function batteryStatusFailure() { | 45 var promise; |
58 testFailed('failed to successfully resolve the promise'); | 46 mockBatteryMonitorReady.then(() => { |
59 setTimeout(finishJSTest, 0); | 47 promise = navigator.getBattery(); |
60 } | 48 shouldBeDefined("promise"); |
61 | 49 shouldBeDefined("promise.then"); |
62 promise = navigator.getBattery(); | 50 promise.then(batteryStatusSuccess, batteryStatusFailure); |
63 shouldBeDefined("promise"); | 51 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
64 shouldBeDefined("promise.then"); | 52 }); |
65 promise.then(batteryStatusSuccess, batteryStatusFailure); | |
66 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | |
67 </script> | 53 </script> |
68 </body> | 54 </body> |
69 </html> | 55 </html> |
OLD | NEW |