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
ash / webui / diagnostics_ui / resources / fake_network_health_provider.ts [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {FakeObservables} from 'chrome://resources/ash/common/fake_observables.js';
import {assert} from 'chrome://resources/js/assert.js';
import {CellularNetwork, EthernetNetwork, NetworkGuidInfo, WiFiNetwork} from './diagnostics_types.js';
import {NetworkHealthProviderInterface, NetworkListObserverRemote, NetworkStateObserverRemote} from './network_health_provider.mojom-webui.js';
// Method names.
export const ON_NETWORK_LIST_CHANGED_METHOD_NAME =
'NetworkListObserver_onNetworkListChanged';
const ON_NETWORK_STATE_CHANGED_METHOD_NAME =
'NetworkStateObserver_onNetworkStateChanged';
/**
* @fileoverview
* Implements a fake version of the NetworkHealthProvider mojo interface.
*/
/**
* Type for methods needed for the fake NetworkHealthProvider implementation.
*/
export type FakeNetworkHealthProviderInterface =
NetworkHealthProviderInterface&{
setFakeNetworkGuidInfo(networkGuidInfoList: NetworkGuidInfo[]): void,
setFakeNetworkState(
guid: string,
networkStateList: EthernetNetwork[]|WiFiNetwork[]|CellularNetwork[]):
void,
};
export class FakeNetworkHealthProvider implements
FakeNetworkHealthProviderInterface {
private observables: FakeObservables = new FakeObservables();
private observeNetworkListPromise: Promise<void>|null = null;
private observeNetworkStatePromise: Promise<void>|null = null;
constructor() {
this.registerObservables();
}
/**
* Implements NetworkHealthProviderInterface.ObserveNetworkList.
*/
observeNetworkList(remote: NetworkListObserverRemote): void {
this.observeNetworkListPromise =
this.observe(ON_NETWORK_LIST_CHANGED_METHOD_NAME, (networkGuidInfo) => {
remote.onNetworkListChanged(
networkGuidInfo.networkGuids, networkGuidInfo.activeGuid);
});
}
/**
* Implements NetworkHealthProviderInterface.ObserveNetwork.
* The guid argument is used to observe a specific network identified
* by |guid| within a group of observers.
*/
observeNetwork(remote: NetworkStateObserverRemote, guid: string): void {
this.observeNetworkStatePromise = this.observeWithArg(
ON_NETWORK_STATE_CHANGED_METHOD_NAME, guid, (network) => {
remote.onNetworkStateChanged(
/** @type {!Network} */ (network));
});
}
// Sets the values that will be observed from observeNetworkList.
setFakeNetworkGuidInfo(networkGuidInfoList: NetworkGuidInfo[]): void {
this.observables.setObservableData(
ON_NETWORK_LIST_CHANGED_METHOD_NAME, networkGuidInfoList);
}
setFakeNetworkState(
guid: string,
networkStateList: EthernetNetwork[]|WiFiNetwork[]|
CellularNetwork[]): void {
this.observables.setObservableDataForArg(
ON_NETWORK_STATE_CHANGED_METHOD_NAME, guid, networkStateList);
}
// Returns the promise for the most recent network list observation.
getObserveNetworkListPromiseForTesting(): Promise<void> {
assert(this.observeNetworkListPromise);
return this.observeNetworkListPromise;
}
// Returns the promise for the most recent network state observation.
getObserveNetworkStatePromiseForTesting(): Promise<void> {
assert(this.observeNetworkStatePromise);
return this.observeNetworkStatePromise;
}
// Causes the network list observer to fire.
triggerNetworkListObserver(): void {
this.observables.trigger(ON_NETWORK_LIST_CHANGED_METHOD_NAME);
}
// Make the observable fire automatically on provided interval.
startTriggerInterval(methodName: string, intervalMs: number): void {
this.observables.startTriggerOnInterval(methodName, intervalMs);
}
// Stop automatically triggering observables.
stopTriggerIntervals(): void {
this.observables.stopAllTriggerIntervals();
}
registerObservables(): void {
this.observables.register(ON_NETWORK_LIST_CHANGED_METHOD_NAME);
this.observables.registerObservableWithArg(
ON_NETWORK_STATE_CHANGED_METHOD_NAME);
}
// Disables all observers and resets provider to its initial state.
reset(): void {
this.observables.stopAllTriggerIntervals();
this.observables = new FakeObservables();
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();
});
}
// Sets up an observer for a methodName that takes an additional arg.
private observeWithArg(
methodName: string, arg: string,
callback: (T: any) => void): Promise<void> {
return new Promise((resolve) => {
this.observables.observeWithArg(methodName, arg, callback);
this.observables.triggerWithArg(methodName, arg);
resolve();
});
}
}