OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <body> |
| 4 <script src="../resources/js-test.js"></script> |
| 5 <script> |
| 6 description("Test multiple promise resolution."); |
| 7 |
| 8 if (!window.testRunner) |
| 9 debug('This test cannot be run without the TestRunner'); |
| 10 |
| 11 testRunner.setCanOpenWindows(); |
| 12 testRunner.waitUntilDone(); |
| 13 jsTestIsAsync = true; |
| 14 |
| 15 var mockBatteryInfo; |
| 16 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve
l) { |
| 17 mockBatteryInfo = { charging: charging, |
| 18 chargingTime: chargingTime, |
| 19 dischargingTime: dischargingTime, |
| 20 level: level }; |
| 21 testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, l
evel); |
| 22 } |
| 23 |
| 24 // compare obtained battery values with the mock values |
| 25 function checkBatteryInfo(batteryManager) { |
| 26 batteryInfo = batteryManager; |
| 27 shouldBeDefined("batteryInfo"); |
| 28 shouldBeDefined("mockBatteryInfo"); |
| 29 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); |
| 30 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); |
| 31 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); |
| 32 shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); |
| 33 } |
| 34 |
| 35 var promise1Count = 0; |
| 36 var promise2Count = 0; |
| 37 |
| 38 function batteryStatusFailure() { |
| 39 testFailed('failed to successfully resolve the promise'); |
| 40 setTimeout(cleanupAndFinish, 0); |
| 41 } |
| 42 |
| 43 function finishIfReady() { |
| 44 if (promise1Count == 1 && promise2Count == 1) { |
| 45 setTimeout(cleanupAndFinish, 0); |
| 46 } |
| 47 } |
| 48 |
| 49 function cleanupAndFinish() { |
| 50 // stops updating battery and clears current battery status. |
| 51 window.testRunner.setPageVisibility("hidden"); |
| 52 finishJSTest(); |
| 53 } |
| 54 |
| 55 promise1 = navigator.getBattery().then( |
| 56 function(battery) { |
| 57 debug('first resolution'); |
| 58 checkBatteryInfo(battery); |
| 59 promise1Count++; |
| 60 finishIfReady(); |
| 61 }, batteryStatusFailure); |
| 62 |
| 63 promise2 = navigator.getBattery().then( |
| 64 function(battery) { |
| 65 debug('second resolution'); |
| 66 checkBatteryInfo(battery); |
| 67 promise2Count++; |
| 68 finishIfReady(); |
| 69 }, batteryStatusFailure); |
| 70 |
| 71 shouldBeFalse('promise1 === promise2'); |
| 72 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 73 </script> |
| 74 </body> |
| 75 </html> |
OLD | NEW |