mock-battery-monitor.js (1744B)
1 import {BatteryMonitor, BatteryMonitorReceiver} from '/gen/services/device/public/mojom/battery_monitor.mojom.m.js'; 2 3 class MockBatteryMonitor { 4 constructor() { 5 this.receiver_ = new BatteryMonitorReceiver(this); 6 this.interceptor_ = 7 new MojoInterfaceInterceptor(BatteryMonitor.$interfaceName); 8 this.interceptor_.oninterfacerequest = e => 9 this.receiver_.$.bindHandle(e.handle); 10 this.reset(); 11 } 12 13 start() { 14 this.interceptor_.start(); 15 } 16 17 stop() { 18 this.interceptor_.stop(); 19 } 20 21 reset() { 22 this.pendingRequests_ = []; 23 this.status_ = null; 24 this.lastKnownStatus_ = null; 25 } 26 27 queryNextStatus() { 28 const result = new Promise(resolve => this.pendingRequests_.push(resolve)); 29 this.runCallbacks_(); 30 return result; 31 } 32 33 setBatteryStatus(charging, chargingTime, dischargingTime, level) { 34 this.status_ = {charging, chargingTime, dischargingTime, level}; 35 this.lastKnownStatus_ = this.status_; 36 this.runCallbacks_(); 37 } 38 39 verifyBatteryStatus(manager) { 40 assert_not_equals(manager, undefined); 41 assert_not_equals(this.lastKnownStatus_, null); 42 assert_equals(manager.charging, this.lastKnownStatus_.charging); 43 assert_equals(manager.chargingTime, this.lastKnownStatus_.chargingTime); 44 assert_equals( 45 manager.dischargingTime, this.lastKnownStatus_.dischargingTime); 46 assert_equals(manager.level, this.lastKnownStatus_.level); 47 } 48 49 runCallbacks_() { 50 if (!this.status_ || !this.pendingRequests_.length) 51 return; 52 53 let result = {status: this.status_}; 54 while (this.pendingRequests_.length) { 55 this.pendingRequests_.pop()(result); 56 } 57 this.status_ = null; 58 } 59 } 60 61 export const mockBatteryMonitor = new MockBatteryMonitor();