| 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 multiple promise resolution."); | 6 description("Test multiple promise resolution."); |
| 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 testRunner.setCanOpenWindows(); |
| 18 testRunner.setCloseRemainingWindowsWhenComplete(true); |
| 19 |
| 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 } |
| 17 | 44 |
| 18 var promise1Count = 0; | 45 var promise1Count = 0; |
| 19 var promise2Count = 0; | 46 var promise2Count = 0; |
| 20 | 47 |
| 21 function finishIfReady() { | 48 function finishIfReady() { |
| 22 if (promise1Count == 1 && promise2Count == 1) | 49 if (promise1Count == 1 && promise2Count == 1) |
| 23 setTimeout(finishJSTest, 0); | 50 setTimeout(finishJSTest, 0); |
| 24 } | 51 } |
| 25 | 52 |
| 26 var promise1; | 53 promise1 = navigator.getBattery(); |
| 27 var promise2; | 54 promise1.then( |
| 55 function(battery) { |
| 56 debug('first resolution'); |
| 57 checkBatteryInfo(battery); |
| 58 promise1Count++; |
| 59 finishIfReady(); |
| 60 }, batteryStatusFailure); |
| 28 | 61 |
| 29 mockBatteryMonitorReady.then(() => { | 62 promise2 = navigator.getBattery(); |
| 30 promise1 = navigator.getBattery(); | 63 promise2.then( |
| 31 promise1.then( | 64 function(battery) { |
| 32 function(battery) { | 65 debug('second resolution'); |
| 33 debug('first resolution'); | 66 checkBatteryInfo(battery); |
| 34 testIfBatteryStatusIsUpToDate(battery); | 67 promise2Count++; |
| 35 promise1Count++; | 68 finishIfReady(); |
| 36 finishIfReady(); | 69 }, batteryStatusFailure); |
| 37 }, batteryStatusFailure); | |
| 38 | 70 |
| 39 promise2 = navigator.getBattery(); | 71 shouldBeTrue('promise1 === promise2'); |
| 40 promise2.then( | 72 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 41 function(battery) { | |
| 42 debug('second resolution'); | |
| 43 testIfBatteryStatusIsUpToDate(battery); | |
| 44 promise2Count++; | |
| 45 finishIfReady(); | |
| 46 }, batteryStatusFailure); | |
| 47 | |
| 48 shouldBeTrue('promise1 === promise2'); | |
| 49 setAndFireMockBatteryInfo(false, 10, 20, 0.5); | |
| 50 }); | |
| 51 </script> | 73 </script> |
| 52 </body> | 74 </body> |
| 53 </html> | 75 </html> |
| OLD | NEW |