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 multiple promise resolution."); | 8 description("Test multiple promise resolution."); |
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 testRunner.setCanOpenWindows(); | 19 testRunner.setCanOpenWindows(); |
Yuki
2016/03/30 02:39:45
We're not opening any windows, right? Let's remov
Sam McNally
2016/03/30 03:15:32
Done.
| |
18 testRunner.setCloseRemainingWindowsWhenComplete(true); | 20 testRunner.setCloseRemainingWindowsWhenComplete(true); |
19 | 21 |
20 var mockBatteryInfo; | |
21 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve l) { | |
22 mockBatteryInfo = { charging: charging, | |
23 chargingTime: chargingTime, | |
24 dischargingTime: dischargingTime, | |
25 level: level }; | |
26 window.internals.updateBatteryStatus(charging, chargingTime, dischargingTime , level); | |
27 } | |
28 | |
29 // compare obtained battery values with the mock values | |
30 function checkBatteryInfo(batteryManager) { | |
31 batteryInfo = batteryManager; | |
32 shouldBeDefined("batteryInfo"); | |
33 shouldBeDefined("mockBatteryInfo"); | |
34 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); | |
35 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); | |
36 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); | |
37 shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); | |
38 } | |
39 | |
40 function batteryStatusFailure() { | |
41 testFailed('failed to successfully resolve the promise'); | |
42 setTimeout(finishJSTest, 0); | |
43 } | |
44 | |
45 var promise1Count = 0; | 22 var promise1Count = 0; |
46 var promise2Count = 0; | 23 var promise2Count = 0; |
47 | 24 |
48 function finishIfReady() { | 25 function finishIfReady() { |
49 if (promise1Count == 1 && promise2Count == 1) | 26 if (promise1Count == 1 && promise2Count == 1) |
50 setTimeout(finishJSTest, 0); | 27 setTimeout(finishJSTest, 0); |
51 } | 28 } |
52 | 29 |
53 promise1 = navigator.getBattery(); | 30 var promise1; |
54 promise1.then( | 31 var promise2; |
55 function(battery) { | |
56 debug('first resolution'); | |
57 checkBatteryInfo(battery); | |
58 promise1Count++; | |
59 finishIfReady(); | |
60 }, batteryStatusFailure); | |
61 | 32 |
62 promise2 = navigator.getBattery(); | 33 ready.then(() => { |
63 promise2.then( | 34 promise1 = navigator.getBattery(); |
64 function(battery) { | 35 promise1.then( |
65 debug('second resolution'); | 36 function(battery) { |
66 checkBatteryInfo(battery); | 37 debug('first resolution'); |
67 promise2Count++; | 38 checkBatteryInfo(battery); |
68 finishIfReady(); | 39 promise1Count++; |
69 }, batteryStatusFailure); | 40 finishIfReady(); |
41 }, batteryStatusFailure); | |
70 | 42 |
71 shouldBeTrue('promise1 === promise2'); | 43 promise2 = navigator.getBattery(); |
72 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | 44 promise2.then( |
45 function(battery) { | |
46 debug('second resolution'); | |
47 checkBatteryInfo(battery); | |
48 promise2Count++; | |
49 finishIfReady(); | |
50 }, batteryStatusFailure); | |
51 | |
52 shouldBeTrue('promise1 === promise2'); | |
53 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | |
54 }); | |
73 </script> | 55 </script> |
74 </body> | 56 </body> |
75 </html> | 57 </html> |
OLD | NEW |