Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js |
| diff --git a/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js b/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..365ad9848e8b65f8ffb04f8b7361564c98041e04 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js |
| @@ -0,0 +1,86 @@ |
| +"use strict"; |
| + |
| +let mockBatteryMonitor = loadMojoModules( |
| + 'mockBatteryMonitor', |
| + ['device/battery/battery_monitor.mojom', |
| + 'device/battery/battery_status.mojom', |
| + 'mojo/public/js/router', |
| + ]).then(mojo => { |
| + let [batteryMonitor, batteryStatus, router] = mojo.modules; |
| + |
| + class MockBatteryMonitor extends batteryMonitor.BatteryMonitor.stubClass { |
| + constructor(serviceRegistry) { |
| + super(); |
| + serviceRegistry.addServiceOverrideForTesting( |
| + batteryMonitor.BatteryMonitor.name, |
| + handle => this.connect_(handle)); |
| + |
| + this.serviceRegistry_ = serviceRegistry; |
| + this.pendingRequests_ = []; |
| + this.status_ = null; |
| + } |
| + |
| + connect_(handle) { |
| + this.router_ = new router.Router(handle); |
| + this.router_.setIncomingReceiver(this); |
| + } |
| + |
| + queryNextStatus() { |
| + let result = new Promise(resolve => this.pendingRequests_.push(resolve)); |
| + this.runCallbacks_(); |
| + return result; |
| + } |
| + |
| + updateBatteryStatus(charging, chargingTime, dischargingTime, level) { |
| + this.status_ = new batteryStatus.BatteryStatus(); |
| + this.status_.charging = charging; |
| + this.status_.charging_time = chargingTime; |
| + this.status_.discharging_time = dischargingTime; |
| + this.status_.level = level; |
| + this.runCallbacks_(); |
| + } |
| + |
| + runCallbacks_() { |
| + if (!this.status_ || !this.pendingRequests_.length) |
| + return; |
| + |
| + while (this.pendingRequests_.length) { |
| + this.pendingRequests_.pop()({status: this.status_}); |
| + } |
| + this.status_ = null; |
| + } |
| + |
|
Yuki
2016/03/30 02:39:46
nit: no need this empty line?
Sam McNally
2016/03/30 03:15:32
Done.
|
| + } |
| + return new MockBatteryMonitor(mojo.serviceRegistry); |
| +}); |
| + |
| +let batteryInfo; |
|
Yuki
2016/03/30 02:39:46
I think we don't need this declaration here.
Let'
|
| +let mockBatteryInfo; |
|
Yuki
2016/03/30 02:39:46
Maybe better to rename this variable to |lastSetMo
Sam McNally
2016/03/30 03:15:33
Done.
|
| + |
| +function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, |
| + level) { |
| + mockBatteryInfo = { charging: charging, |
| + chargingTime: chargingTime, |
| + dischargingTime: dischargingTime, |
| + level: level }; |
| + mockBatteryMonitor.then(mock => mock.updateBatteryStatus( |
| + charging, chargingTime, dischargingTime, level)); |
| +} |
| + |
| +// compare obtained battery values with the mock values |
| +function checkBatteryInfo(batteryManager) { |
|
Yuki
2016/03/30 02:39:46
Maybe better to rename this function to |testIfBat
Sam McNally
2016/03/30 03:15:33
Done.
|
| + batteryInfo = batteryManager; |
| + shouldBeDefined("batteryInfo"); |
| + shouldBeDefined("mockBatteryInfo"); |
| + shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); |
| + shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); |
| + shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); |
| + shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); |
| +} |
| + |
| +function batteryStatusFailure() { |
| + testFailed('failed to successfully resolve the promise'); |
| + setTimeout(finishJSTest, 0); |
| +} |
| + |
| +let ready = mockBatteryMonitor.then(); |
|
Yuki
2016/03/30 02:39:46
|ready| is too much general as a name of a global
Sam McNally
2016/03/30 03:15:32
Done.
|