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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
ash / system / network / active_network_icon_unittest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/network/active_network_icon.h"
#include <memory>
#include <string>
#include "ash/public/cpp/network_config_service.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/color_util.h"
#include "ash/system/network/network_icon.h"
#include "ash/system/network/tray_network_state_model.h"
#include "ash/test/ash_test_base.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace ash {
namespace {
using ::chromeos::network_config::mojom::ConnectionStateType;
using ::chromeos::network_config::mojom::NetworkType;
const char kShillManagerClientStubCellularDevice[] =
"/device/stub_cellular_device";
const char kCellularNetworkGuid[] = "cellular_guid";
const char16_t kCellularNetworkGuid16[] = u"cellular_guid";
} // namespace
class ActiveNetworkIconTest : public AshTestBase {
public:
ActiveNetworkIconTest() = default;
ActiveNetworkIconTest(const ActiveNetworkIconTest&) = delete;
ActiveNetworkIconTest& operator=(const ActiveNetworkIconTest&) = delete;
~ActiveNetworkIconTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
network_state_model_ = std::make_unique<TrayNetworkStateModel>();
active_network_icon_ =
std::make_unique<ActiveNetworkIcon>(network_state_model_.get());
}
void TearDown() override {
active_network_icon_.reset();
AshTestBase::TearDown();
}
void SetupEthernet() {
if (eth_path_.empty()) {
network_state_helper().device_test()->AddDevice(
"/device/stub_eth_device", shill::kTypeEthernet, "stub_eth_device");
eth_path_ = ConfigureService(
R"({"GUID": "eth_guid", "Type": "ethernet", "State": "online"})");
}
base::RunLoop().RunUntilIdle();
}
void SetupWiFi(const char* state) {
if (wifi_path_.empty()) {
// WiFi device already exists.
wifi_path_ = ConfigureService(
R"({"GUID": "wifi_guid", "Type": "wifi", "State": "idle"})");
}
SetServiceProperty(wifi_path_, shill::kStateProperty, base::Value(state));
SetServiceProperty(wifi_path_, shill::kSignalStrengthProperty,
base::Value(100));
base::RunLoop().RunUntilIdle();
}
void SetupCellular(const char* state) {
if (cellular_path_.empty()) {
network_state_helper().manager_test()->AddTechnology(shill::kTypeCellular,
true /* enabled */);
network_state_helper().AddDevice(kShillManagerClientStubCellularDevice,
shill::kTypeCellular,
"stub_cellular_device");
cellular_path_ = ConfigureService(base::StringPrintf(
R"({"GUID": "%s", "Type": "cellular", "Technology": "LTE",
"State": "idle"})",
kCellularNetworkGuid));
SetServiceProperty(cellular_path_, shill::kDeviceProperty,
base::Value(kShillManagerClientStubCellularDevice));
}
SetServiceProperty(cellular_path_, shill::kStateProperty,
base::Value(state));
SetServiceProperty(cellular_path_, shill::kSignalStrengthProperty,
base::Value(100));
base::RunLoop().RunUntilIdle();
}
void SetCellularUninitialized(bool scanning) {
const bool enabled = scanning;
network_state_helper().manager_test()->RemoveTechnology(
shill::kTypeCellular);
network_state_helper().manager_test()->AddTechnology(shill::kTypeCellular,
enabled);
network_state_helper().device_test()->AddDevice(
kShillManagerClientStubCellularDevice, shill::kTypeCellular,
"stub_cellular_device");
if (scanning) {
network_state_helper().device_test()->SetDeviceProperty(
kShillManagerClientStubCellularDevice, shill::kScanningProperty,
base::Value(true), true /* notify_changed */);
} else {
network_state_helper().manager_test()->SetTechnologyInitializing(
shill::kTypeCellular, true);
}
base::RunLoop().RunUntilIdle();
}
gfx::ImageSkia ImageForNetwork(NetworkType type,
ConnectionStateType connection_state,
int signal_strength = 100) {
std::string id = base::StringPrintf("reference_%d", reference_count_++);
chromeos::network_config::mojom::NetworkStatePropertiesPtr
reference_properties =
network_config_helper_.CreateStandaloneNetworkProperties(
id, type, connection_state, signal_strength);
return network_icon::GetImageForNonVirtualNetwork(
GetColorProvider(), reference_properties.get(), icon_type_,
false /* show_vpn_badge */);
}
bool AreImagesEqual(const gfx::ImageSkia& image,
const gfx::ImageSkia& reference) {
return gfx::test::AreBitmapsEqual(*image.bitmap(), *reference.bitmap());
}
std::string ConfigureService(const std::string& shill_json_string) {
return network_state_helper().ConfigureService(shill_json_string);
}
void SetServiceProperty(const std::string& service_path,
const std::string& key,
const base::Value& value) {
network_state_helper().SetServiceProperty(service_path, key, value);
}
NetworkStateTestHelper& network_state_helper() {
return network_config_helper_.network_state_helper();
}
NetworkStateHandler* network_state_handler() {
return network_state_helper().network_state_handler();
}
ActiveNetworkIcon* active_network_icon() {
return active_network_icon_.get();
}
TrayNetworkStateModel* network_state_model() {
return network_state_model_.get();
}
const std::string& eth_path() const { return eth_path_; }
const std::string& wifi_path() const { return wifi_path_; }
const std::string& cellular_path() const { return cellular_path_; }
network_icon::IconType icon_type() { return icon_type_; }
const ui::ColorProvider* GetColorProvider() {
// TODO(b/279177422): Replace with a stable ColorProvider
return ColorUtil::GetColorProviderSourceForWindow(
Shell::GetPrimaryRootWindow())
->GetColorProvider();
}
private:
network_config::CrosNetworkConfigTestHelper network_config_helper_;
std::unique_ptr<TrayNetworkStateModel> network_state_model_;
std::unique_ptr<ActiveNetworkIcon> active_network_icon_;
std::string eth_path_;
std::string wifi_path_;
std::string cellular_path_;
network_icon::IconType icon_type_ = network_icon::ICON_TYPE_TRAY_REGULAR;
// Counter to provide unique ids for reference networks.
int reference_count_ = 0;
};
TEST_F(ActiveNetworkIconTest, GetConnectionStatusStrings) {
// TODO(902409): Test multi icon and improve coverage.
SetupCellular(shill::kStateOnline);
std::u16string name, desc, tooltip;
active_network_icon()->GetConnectionStatusStrings(
ActiveNetworkIcon::Type::kSingle, &name, &desc, &tooltip);
// Note: The guid is used for the name in ConfigureService.
EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED,
kCellularNetworkGuid16),
name);
std::u16string connected_string = l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED, kCellularNetworkGuid16);
EXPECT_EQ(
l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED_TOOLTIP, connected_string,
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_SIGNAL_STRONG)),
tooltip);
}
TEST_F(ActiveNetworkIconTest, GetSingleImage) {
// Cellular only = Cellular icon
SetupCellular(shill::kStateOnline);
bool animating;
gfx::ImageSkia image = active_network_icon()->GetImage(
GetColorProvider(), ActiveNetworkIcon::Type::kSingle, icon_type(),
&animating);
EXPECT_TRUE(AreImagesEqual(
image,
ImageForNetwork(NetworkType::kCellular, ConnectionStateType::kOnline)));
EXPECT_FALSE(animating);
// Cellular + WiFi connected = WiFi connected icon
SetupWiFi(shill::kStateOnline);
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(
image,
ImageForNetwork(NetworkType::kWiFi, ConnectionStateType::kOnline)));
EXPECT_FALSE(animating);
// Cellular + WiFi connecting = WiFi connecting icon
SetupWiFi(shill::kStateAssociation);
network_state_handler()->SetNetworkConnectRequested(wifi_path(), true);
SetServiceProperty(wifi_path(), shill::kSignalStrengthProperty,
base::Value(50));
base::RunLoop().RunUntilIdle();
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(
image, ImageForNetwork(NetworkType::kWiFi,
ConnectionStateType::kConnecting, 50)));
EXPECT_TRUE(animating);
// Cellular + WiFi connecting + Ethernet = WiFi connecting icon
SetupEthernet();
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(
image, ImageForNetwork(NetworkType::kWiFi,
ConnectionStateType::kConnecting, 50)));
EXPECT_TRUE(animating);
// Cellular + WiFi connected + Ethernet = Ethernet connected icon
SetupWiFi(shill::kStateOnline);
network_state_handler()->SetNetworkConnectRequested(wifi_path(), false);
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(
image,
ImageForNetwork(NetworkType::kEthernet, ConnectionStateType::kOnline)));
EXPECT_FALSE(animating);
}
TEST_F(ActiveNetworkIconTest, CellularUninitialized) {
SetCellularUninitialized(false /* scanning */);
bool animating;
gfx::ImageSkia image = active_network_icon()->GetImage(
GetColorProvider(), ActiveNetworkIcon::Type::kSingle, icon_type(),
&animating);
EXPECT_TRUE(
AreImagesEqual(image, ImageForNetwork(NetworkType::kCellular,
ConnectionStateType::kConnecting)));
EXPECT_TRUE(animating);
}
TEST_F(ActiveNetworkIconTest, CellularScanning) {
SetCellularUninitialized(true /* scanning */);
ASSERT_TRUE(network_state_handler()->GetScanningByType(
NetworkTypePattern::Cellular()));
bool animating;
gfx::ImageSkia image = active_network_icon()->GetImage(
GetColorProvider(), ActiveNetworkIcon::Type::kSingle, icon_type(),
&animating);
EXPECT_TRUE(
AreImagesEqual(image, ImageForNetwork(NetworkType::kCellular,
ConnectionStateType::kConnecting)));
EXPECT_TRUE(animating);
// Set scanning property to false, expect no network connections icon.
network_state_helper().device_test()->SetDeviceProperty(
kShillManagerClientStubCellularDevice, shill::kScanningProperty,
base::Value(false), true /* notify_changed */);
base::RunLoop().RunUntilIdle();
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(image, network_icon::GetImageForWiFiNoConnections(
GetColorProvider(), icon_type())));
EXPECT_FALSE(animating);
}
TEST_F(ActiveNetworkIconTest, CellularDisable) {
SetupCellular(shill::kStateOnline);
bool animating;
gfx::ImageSkia image = active_network_icon()->GetImage(
GetColorProvider(), ActiveNetworkIcon::Type::kSingle, icon_type(),
&animating);
EXPECT_TRUE(AreImagesEqual(
image,
ImageForNetwork(NetworkType::kCellular, ConnectionStateType::kOnline)));
EXPECT_FALSE(animating);
// The cellular device's scanning property may be true while it's being
// disabled, mock this.
network_state_helper().device_test()->SetDeviceProperty(
kShillManagerClientStubCellularDevice, shill::kScanningProperty,
base::Value(true), true /* notify_changed */);
// Disable the device.
network_state_model()->SetNetworkTypeEnabledState(NetworkType::kCellular,
false);
// Disabling the device doesn't actually remove the services in the fakes,
// remove them explicitly.
network_state_helper().ClearServices();
base::RunLoop().RunUntilIdle();
image = active_network_icon()->GetImage(GetColorProvider(),
ActiveNetworkIcon::Type::kSingle,
icon_type(), &animating);
EXPECT_TRUE(AreImagesEqual(image, network_icon::GetImageForWiFiNoConnections(
GetColorProvider(), icon_type())));
EXPECT_FALSE(animating);
}
// TODO(stevenjb): Test GetDualImagePrimary, GetDualImageCellular.
} // namespace ash