1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
ash / webui / diagnostics_ui / resources / fake_system_data_provider.ts [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {FakeMethodResolver} from 'chrome://resources/ash/common/fake_method_resolver.js';
import {FakeObservables} from 'chrome://resources/ash/common/fake_observables.js';
import {assert} from 'chrome://resources/js/assert.js';
import {BatteryChargeStatus, BatteryChargeStatusObserverRemote, BatteryHealth, BatteryHealthObserverRemote, BatteryInfo, CpuUsage, CpuUsageObserverRemote, MemoryUsage, MemoryUsageObserverRemote, SystemDataProviderInterface, SystemInfo} from './system_data_provider.mojom-webui.js';
/**
* @fileoverview
* Implements a fake version of the SystemDataProvider mojo interface.
*/
/**
* Type for methods needed for the fake SystemDataProvider implementation.
*/
export type FakeSystemDataProviderInterface = SystemDataProviderInterface&{
setFakeBatteryChargeStatus(batteryChargeStatusList: BatteryChargeStatus[]):
void,
setFakeBatteryHealth(batteryHealthList: BatteryHealth[]): void,
setFakeBatteryInfo(batteryInfo: BatteryInfo): void,
setFakeCpuUsage(cpuUsageList: CpuUsage[]): void,
setFakeMemoryUsage(memoryUsageList: MemoryUsage[]): void,
setFakeSystemInfo(systemInfo: SystemInfo): void,
};
export class FakeSystemDataProvider implements FakeSystemDataProviderInterface {
private methods: FakeMethodResolver = new FakeMethodResolver();
private observables: FakeObservables = new FakeObservables();
private observeBatteryChargeStatusPromise: Promise<void>|null = null;
private observeBatteryHealthPromise: Promise<void>|null = null;
private observeCpuUsagePromise: Promise<void>|null = null;
private observeMemoryUsagePromise: Promise<void>|null = null;
constructor() {
this.registerMethods();
this.registerObservables();
}
getSystemInfo(): Promise<{systemInfo: SystemInfo}> {
return this.methods.resolveMethod('getSystemInfo');
}
// Sets the value that will be returned when calling getSystemInfo().
setFakeSystemInfo(systemInfo: SystemInfo): void {
this.methods.setResult('getSystemInfo', {systemInfo});
}
// Implements SystemDataProviderInterface.GetBatteryInfo.
getBatteryInfo(): Promise<{batteryInfo: BatteryInfo}> {
return this.methods.resolveMethod('getBatteryInfo');
}
// Sets the value that will be returned when calling getBatteryInfo().
setFakeBatteryInfo(batteryInfo: BatteryInfo): void {
this.methods.setResult('getBatteryInfo', {batteryInfo});
}
// Implements SystemDataProviderInterface.ObserveBatteryChargeStatus.
observeBatteryChargeStatus(remote: BatteryChargeStatusObserverRemote): void {
this.observeBatteryChargeStatusPromise = this.observe(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated',
(batteryChargeStatus) => {
remote.onBatteryChargeStatusUpdated(
/** @type {!BatteryChargeStatus} */ (batteryChargeStatus));
});
}
// Returns the promise for the most recent battery charge status observation.
getObserveBatteryChargeStatusPromiseForTesting(): Promise<void> {
assert(this.observeBatteryChargeStatusPromise);
return this.observeBatteryChargeStatusPromise;
}
/**
* Sets the values that will observed from observeBatteryChargeStatus.
*/
setFakeBatteryChargeStatus(batteryChargeStatusList: BatteryChargeStatus[]):
void {
this.observables.setObservableData(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated',
batteryChargeStatusList);
}
// Causes the battery charge status observer to fire.
triggerBatteryChargeStatusObserver(): void {
this.observables.trigger(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated');
}
// Implements SystemDataProviderInterface.ObserveBatteryHealth.
observeBatteryHealth(remote: BatteryHealthObserverRemote): void {
this.observeBatteryHealthPromise = this.observe(
'BatteryHealthObserver_onBatteryHealthUpdated', (batteryHealth) => {
remote.onBatteryHealthUpdated(
/** @type {!BatteryHealth} */ (batteryHealth));
});
}
// Returns the promise for the most recent battery health observation.
getObserveBatteryHealthPromiseForTesting(): Promise<void> {
assert(this.observeBatteryHealthPromise);
return this.observeBatteryHealthPromise;
}
// Sets the values that will observed from observeBatteryHealth.
setFakeBatteryHealth(batteryHealthList: BatteryHealth[]): void {
this.observables.setObservableData(
'BatteryHealthObserver_onBatteryHealthUpdated', batteryHealthList);
}
// Causes the battery health observer to fire.
triggerBatteryHealthObserver(): void {
this.observables.trigger('BatteryHealthObserver_onBatteryHealthUpdated');
}
// Implements SystemDataProviderInterface.ObserveCpuUsage.
observeCpuUsage(remote: CpuUsageObserverRemote): void {
this.observeCpuUsagePromise =
this.observe('CpuUsageObserver_onCpuUsageUpdated', (cpuUsage) => {
remote.onCpuUsageUpdated(
/** @type {!CpuUsage} */ (cpuUsage));
});
}
// Returns the promise for the most recent cpu usage observation.
getObserveCpuUsagePromiseForTesting(): Promise<void> {
assert(this.observeCpuUsagePromise);
return this.observeCpuUsagePromise;
}
// Sets the values that will observed from observeCpuUsage.
setFakeCpuUsage(cpuUsageList: CpuUsage[]): void {
this.observables.setObservableData(
'CpuUsageObserver_onCpuUsageUpdated', cpuUsageList);
}
// Causes the CPU usage observer to fire.
triggerCpuUsageObserver(): void {
this.observables.trigger('CpuUsageObserver_onCpuUsageUpdated');
}
// Implements SystemDataProviderInterface.ObserveMemoryUsage.
observeMemoryUsage(remote: MemoryUsageObserverRemote): void {
this.observeMemoryUsagePromise = this.observe(
'MemoryUsageObserver_onMemoryUsageUpdated', (memoryUsage) => {
remote.onMemoryUsageUpdated(
/** @type {!MemoryUsage} */ (memoryUsage));
});
}
// Returns the promise for the most recent memory usage observation.
getObserveMemoryUsagePromiseForTesting(): Promise<void> {
assert(this.observeMemoryUsagePromise);
return this.observeMemoryUsagePromise;
}
// Sets the values that will observed from ObserveMemoryUsage.
setFakeMemoryUsage(memoryUsageList: MemoryUsage[]): void {
this.observables.setObservableData(
'MemoryUsageObserver_onMemoryUsageUpdated', memoryUsageList);
}
// Causes the memory usage observer to fire.
triggerMemoryUsageObserver(): void {
this.observables.trigger('MemoryUsageObserver_onMemoryUsageUpdated');
}
// Make the observables fire automatically on various intervals.
startTriggerIntervals(): void {
this.observables.startTriggerOnInterval(
'CpuUsageObserver_onCpuUsageUpdated', 1000);
this.observables.startTriggerOnInterval(
'MemoryUsageObserver_onMemoryUsageUpdated', 5000);
this.observables.startTriggerOnInterval(
'BatteryHealthObserver_onBatteryHealthUpdated', 30000);
this.observables.startTriggerOnInterval(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated', 30000);
}
// Stop automatically triggering observables.
stopTriggerIntervals(): void {
this.observables.stopAllTriggerIntervals();
}
// Setup method resolvers.
registerMethods(): void {
this.methods.register('getSystemInfo');
this.methods.register('getBatteryInfo');
}
// Setup observables.
registerObservables(): void {
this.observables.register(
'BatteryChargeStatusObserver_onBatteryChargeStatusUpdated');
this.observables.register('BatteryHealthObserver_onBatteryHealthUpdated');
this.observables.register('CpuUsageObserver_onCpuUsageUpdated');
this.observables.register('MemoryUsageObserver_onMemoryUsageUpdated');
}
// Disables all observers and resets provider to its initial state.
reset(): void {
this.observables.stopAllTriggerIntervals();
this.methods = new FakeMethodResolver();
this.observables = new FakeObservables();
this.registerMethods();
this.registerObservables();
}
// Sets up an observer for methodName.
private observe(methodName: string, callback: (T: any) => void):
Promise<void> {
return new Promise((resolve) => {
this.observables.observe(methodName, callback);
this.observables.trigger(methodName);
resolve();
});
}
}