| OLD | NEW |
| (Empty) |
| 1 "use strict"; | |
| 2 | |
| 3 let mockBatteryMonitor = loadMojoModules( | |
| 4 'mockBatteryMonitor', | |
| 5 ['device/battery/battery_monitor.mojom', | |
| 6 'device/battery/battery_status.mojom', | |
| 7 'mojo/public/js/router', | |
| 8 ]).then(mojo => { | |
| 9 let [batteryMonitor, batteryStatus, router] = mojo.modules; | |
| 10 | |
| 11 class MockBatteryMonitor extends batteryMonitor.BatteryMonitor.stubClass { | |
| 12 constructor(serviceRegistry) { | |
| 13 super(); | |
| 14 serviceRegistry.addServiceOverrideForTesting( | |
| 15 batteryMonitor.BatteryMonitor.name, | |
| 16 handle => this.connect_(handle)); | |
| 17 | |
| 18 this.serviceRegistry_ = serviceRegistry; | |
| 19 this.pendingRequests_ = []; | |
| 20 this.status_ = null; | |
| 21 } | |
| 22 | |
| 23 connect_(handle) { | |
| 24 this.router_ = new router.Router(handle); | |
| 25 this.router_.setIncomingReceiver(this); | |
| 26 } | |
| 27 | |
| 28 queryNextStatus() { | |
| 29 let result = new Promise(resolve => this.pendingRequests_.push(resolve)); | |
| 30 this.runCallbacks_(); | |
| 31 return result; | |
| 32 } | |
| 33 | |
| 34 updateBatteryStatus(charging, chargingTime, dischargingTime, level) { | |
| 35 this.status_ = new batteryStatus.BatteryStatus(); | |
| 36 this.status_.charging = charging; | |
| 37 this.status_.charging_time = chargingTime; | |
| 38 this.status_.discharging_time = dischargingTime; | |
| 39 this.status_.level = level; | |
| 40 this.runCallbacks_(); | |
| 41 } | |
| 42 | |
| 43 runCallbacks_() { | |
| 44 if (!this.status_ || !this.pendingRequests_.length) | |
| 45 return; | |
| 46 | |
| 47 while (this.pendingRequests_.length) { | |
| 48 this.pendingRequests_.pop()({status: this.status_}); | |
| 49 } | |
| 50 this.status_ = null; | |
| 51 } | |
| 52 } | |
| 53 return new MockBatteryMonitor(mojo.serviceRegistry); | |
| 54 }); | |
| 55 | |
| 56 let batteryInfo; | |
| 57 let lastSetMockBatteryInfo; | |
| 58 | |
| 59 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, | |
| 60 level) { | |
| 61 lastSetMockBatteryInfo = { charging: charging, | |
| 62 chargingTime: chargingTime, | |
| 63 dischargingTime: dischargingTime, | |
| 64 level: level }; | |
| 65 mockBatteryMonitor.then(mock => mock.updateBatteryStatus( | |
| 66 charging, chargingTime, dischargingTime, level)); | |
| 67 } | |
| 68 | |
| 69 // compare obtained battery values with the mock values | |
| 70 function testIfBatteryStatusIsUpToDate(batteryManager) { | |
| 71 batteryInfo = batteryManager; | |
| 72 shouldBeDefined("batteryInfo"); | |
| 73 shouldBeDefined("lastSetMockBatteryInfo"); | |
| 74 shouldBe('batteryInfo.charging', 'lastSetMockBatteryInfo.charging'); | |
| 75 shouldBe('batteryInfo.chargingTime', 'lastSetMockBatteryInfo.chargingTime'); | |
| 76 shouldBe('batteryInfo.dischargingTime', | |
| 77 'lastSetMockBatteryInfo.dischargingTime'); | |
| 78 shouldBe('batteryInfo.level', 'lastSetMockBatteryInfo.level'); | |
| 79 } | |
| 80 | |
| 81 function batteryStatusFailure() { | |
| 82 testFailed('failed to successfully resolve the promise'); | |
| 83 setTimeout(finishJSTest, 0); | |
| 84 } | |
| 85 | |
| 86 var mockBatteryMonitorReady = mockBatteryMonitor.then(); | |
| OLD | NEW |