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 | |
Yuki
2016/03/30 02:39:46
nit: no need this empty line?
Sam McNally
2016/03/30 03:15:32
Done.
| |
53 } | |
54 return new MockBatteryMonitor(mojo.serviceRegistry); | |
55 }); | |
56 | |
57 let batteryInfo; | |
Yuki
2016/03/30 02:39:46
I think we don't need this declaration here.
Let'
| |
58 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.
| |
59 | |
60 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, | |
61 level) { | |
62 mockBatteryInfo = { charging: charging, | |
63 chargingTime: chargingTime, | |
64 dischargingTime: dischargingTime, | |
65 level: level }; | |
66 mockBatteryMonitor.then(mock => mock.updateBatteryStatus( | |
67 charging, chargingTime, dischargingTime, level)); | |
68 } | |
69 | |
70 // compare obtained battery values with the mock values | |
71 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.
| |
72 batteryInfo = batteryManager; | |
73 shouldBeDefined("batteryInfo"); | |
74 shouldBeDefined("mockBatteryInfo"); | |
75 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging'); | |
76 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime'); | |
77 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime'); | |
78 shouldBe('batteryInfo.level', 'mockBatteryInfo.level'); | |
79 } | |
80 | |
81 function batteryStatusFailure() { | |
82 testFailed('failed to successfully resolve the promise'); | |
83 setTimeout(finishJSTest, 0); | |
84 } | |
85 | |
86 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.
| |
OLD | NEW |