/
apn_subpage_test.ts
276 lines (246 loc) · 9.62 KB
/
apn_subpage_test.ts
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://os-settings/lazy_load.js';
import {ApnSubpageElement} from 'chrome://os-settings/lazy_load.js';
import {Router, routes} from 'chrome://os-settings/os_settings.js';
import {ApnList} from 'chrome://resources/ash/common/network/apn_list.js';
import {MojoInterfaceProviderImpl} from 'chrome://resources/ash/common/network/mojo_interface_provider.js';
import {OncMojo} from 'chrome://resources/ash/common/network/onc_mojo.js';
import {DeviceStateProperties, InhibitReason, ManagedProperties} from 'chrome://resources/mojo/chromeos/services/network_config/public/mojom/cros_network_config.mojom-webui.js';
import {DeviceStateType, NetworkType, PortalState} from 'chrome://resources/mojo/chromeos/services/network_config/public/mojom/network_types.mojom-webui.js';
import {assertEquals, assertTrue} from 'chrome://webui-test/chai_assert.js';
import {FakeNetworkConfig} from 'chrome://webui-test/chromeos/fake_network_config_mojom.js';
import {flushTasks} from 'chrome://webui-test/polymer_test_util.js';
import {clearBody} from './utils.js';
suite('ApnSubpageTest', () => {
let apnSubpage: ApnSubpageElement;
let mojoApi_: FakeNetworkConfig;
function getDefaultDeviceStateProps(): DeviceStateProperties {
return {
ipv4Address: undefined,
ipv6Address: undefined,
imei: undefined,
macAddress: undefined,
scanning: false,
simLockStatus: undefined,
simInfos: undefined,
inhibitReason: InhibitReason.kNotInhibited,
simAbsent: false,
deviceState: DeviceStateType.kUninitialized,
type: NetworkType.kCellular,
managedNetworkAvailable: false,
serial: undefined,
isCarrierLocked: false,
};
}
function getApnList(): ApnList|null {
return apnSubpage.shadowRoot!.querySelector('apn-list');
}
setup(async () => {
clearBody();
mojoApi_ = new FakeNetworkConfig();
mojoApi_.setNetworkTypeEnabledState(NetworkType.kCellular, true);
mojoApi_.setManagedPropertiesForTest(OncMojo.getDefaultManagedProperties(
NetworkType.kCellular, 'cellular_guid', 'cellular'));
MojoInterfaceProviderImpl.getInstance().setMojoServiceRemoteForTest(
mojoApi_);
// Start at BASIC route so that if the APN subpage navigates backwards, it
// goes back to BASIC and not the previous tests' last page (crbug/1497312).
Router.getInstance().navigateTo(routes.BASIC);
await flushTasks();
apnSubpage = document.createElement('apn-subpage');
document.body.appendChild(apnSubpage);
const params = new URLSearchParams();
params.append('guid', 'cellular_guid');
Router.getInstance().navigateTo(routes.APN, params);
return flushTasks();
});
teardown(() => {
return flushTasks().then(() => {
apnSubpage.remove();
Router.getInstance().resetRouteForTesting();
});
});
test('Check if APN list exists', async () => {
assertTrue(!!apnSubpage);
assertTrue(!!getApnList());
});
test('Page closed while device is updating', async () => {
const oldClose = apnSubpage.close;
let counter = 0;
apnSubpage.close = () => {
counter++;
oldClose.apply(apnSubpage);
};
mojoApi_.beforeGetDeviceStateList = () => {
apnSubpage.close();
};
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kEnabled,
scanning: true,
});
await flushTasks();
assertEquals(1, counter);
assertEquals(undefined, apnSubpage.get('managedProperties_'));
});
test('Page closed while network is updating', async () => {
const oldClose = apnSubpage.close;
let counter = 0;
apnSubpage.close = () => {
counter++;
oldClose.apply(apnSubpage);
};
mojoApi_.beforeGetManagedProperties = () => {
apnSubpage.close();
};
mojoApi_.setManagedPropertiesForTest(OncMojo.getDefaultManagedProperties(
NetworkType.kCellular, 'cellular_guid', 'cellular'));
await flushTasks();
assertEquals(1, counter);
assertEquals(undefined, apnSubpage.get('managedProperties_'));
});
test(
'Only updating with different device state should call networkDetails',
async () => {
let counter = 0;
const oldGetNetworkDetails = apnSubpage.get('getNetworkDetails_');
apnSubpage.set('getNetworkDetails_', () => {
counter++;
oldGetNetworkDetails.apply(apnSubpage);
});
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kEnabled,
macAddress: 'n1',
});
await flushTasks();
// The new device state has the same type as the old, but it is not a
// full match - we won't call getNetworkDetails.
assertEquals(0, counter);
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kEnabled,
macAddress: 'n1',
});
await flushTasks();
// The new device state has the same type as the old and they fully
// match - we need to call getNetworkDetails. This is because for
// cellular networks, some shill device level properties are
// represented at network level in ONC.
assertEquals(1, counter);
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kDisabled,
macAddress: 'n2',
});
await flushTasks();
// The new device state is not a full match - we would call
// getNetworkDetails, because the deviceState has changed.
assertEquals(2, counter);
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kEnabled,
macAddress: 'n2',
});
await flushTasks();
// The new device state is not a full match - we would call
// getNetworkDetails, because the deviceState has changed.
assertEquals(3, counter);
mojoApi_.setDeviceStateForTest({
...getDefaultDeviceStateProps(),
type: NetworkType.kCellular,
deviceState: DeviceStateType.kEnabled,
macAddress: 'n2',
scanning: true,
});
await flushTasks();
// When scanning and type is cellular we always call getNetworkDetails
assertEquals(4, counter);
});
test('Error state is propagated to <apn-list>', async () => {
let props: ManagedProperties = OncMojo.getDefaultManagedProperties(
NetworkType.kCellular, 'cellular_guid', 'cellular');
mojoApi_.setManagedPropertiesForTest(props);
await flushTasks();
assertEquals(undefined, getApnList()!.errorState);
const error = 'connect-failed';
props = {
...props,
errorState: error,
};
mojoApi_.setManagedPropertiesForTest(props);
await flushTasks();
assertEquals(error, getApnList()!.errorState);
});
test('Close is called more than once.', async () => {
let counter = 0;
const navigateToPreviousRoute =
Router.getInstance().navigateToPreviousRoute;
Router.getInstance().navigateToPreviousRoute = () => {
counter++;
};
apnSubpage.close();
await flushTasks();
apnSubpage.close();
await flushTasks();
assertEquals(1, counter);
Router.getInstance().navigateToPreviousRoute = navigateToPreviousRoute;
});
test('Network removed while on subpage', async () => {
let counter = 0;
const navigateToPreviousRoute =
Router.getInstance().navigateToPreviousRoute;
Router.getInstance().navigateToPreviousRoute = () => {
counter++;
};
// Simulate the network being removed.
mojoApi_.resetForTest();
apnSubpage.onNetworkStateChanged(
OncMojo.getDefaultNetworkState(NetworkType.kCellular, 'cellular'));
await flushTasks();
assertEquals(1, counter);
Router.getInstance().navigateToPreviousRoute = navigateToPreviousRoute;
});
test('Network removed while not on subpage', async () => {
// Navigate to a different page.
const params = new URLSearchParams();
params.append('type', OncMojo.getNetworkTypeString(NetworkType.kCellular));
Router.getInstance().navigateTo(routes.INTERNET_NETWORKS, params);
await flushTasks();
assertEquals(routes.INTERNET_NETWORKS, Router.getInstance().currentRoute);
let counter = 0;
const navigateToPreviousRoute =
Router.getInstance().navigateToPreviousRoute;
Router.getInstance().navigateToPreviousRoute = () => {
counter++;
};
// Simulate the network being removed.
mojoApi_.resetForTest();
apnSubpage.onNetworkStateChanged(
OncMojo.getDefaultNetworkState(NetworkType.kCellular, 'cellular'));
await flushTasks();
assertEquals(0, counter);
Router.getInstance().navigateToPreviousRoute = navigateToPreviousRoute;
});
test('Portal state is propagated to <apn-list>', async () => {
let props: ManagedProperties = OncMojo.getDefaultManagedProperties(
NetworkType.kCellular, 'cellular_guid', 'cellular');
mojoApi_.setManagedPropertiesForTest(props);
await flushTasks();
assertEquals(PortalState.kUnknown, getApnList()!.portalState);
props = {
...props,
portalState: PortalState.kNoInternet,
};
mojoApi_.setManagedPropertiesForTest(props);
await flushTasks();
assertEquals(PortalState.kNoInternet, getApnList()!.portalState);
});
});