OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <body> |
| 4 <script src="../resources/js-test.js"></script> |
| 5 <script> |
| 6 description("Test battery status API with multiple windows."); |
| 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 function batteryStatusFailure() { |
| 36 testFailed('failed to successfully resolve the promise'); |
| 37 finishJSTest(); |
| 38 } |
| 39 |
| 40 var firstWindow = window; |
| 41 var secondWindow = window.open(""); |
| 42 |
| 43 function batteryStatusSuccess(battery) { |
| 44 debug('resolution in window'); |
| 45 checkBatteryInfo(battery); |
| 46 |
| 47 secondWindow.navigator.getBattery().then( |
| 48 function(battery) { |
| 49 debug('resolution in secondWindow'); |
| 50 checkBatteryInfo(battery); |
| 51 setTimeout(cleanupAndFinish, 0); |
| 52 }, batteryStatusFailure); |
| 53 } |
| 54 |
| 55 function cleanupAndFinish() { |
| 56 // stops updating battery and clears current battery status. |
| 57 firstWindow.testRunner.setPageVisibility("hidden"); |
| 58 secondWindow.testRunner.setPageVisibility("hidden"); |
| 59 secondWindow.close(); |
| 60 finishJSTest(); |
| 61 } |
| 62 |
| 63 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); |
| 64 setAndFireMockBatteryInfo(false, 10, 20, 0.5); |
| 65 </script> |
| 66 </body> |
| 67 </html> |
OLD | NEW |