network_list_view_controller_unittest.cc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/network/network_list_view_controller_impl.h"
  5. #include <cstddef>
  6. #include <memory>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/system/model/system_tray_model.h"
  12. #include "ash/system/network/fake_network_detailed_network_view.h"
  13. #include "ash/system/network/fake_network_list_mobile_header_view.h"
  14. #include "ash/system/network/fake_network_list_wifi_header_view.h"
  15. #include "ash/system/network/network_utils.h"
  16. #include "ash/system/network/tray_network_state_model.h"
  17. #include "ash/system/tray/tray_info_label.h"
  18. #include "ash/system/tray/tri_view.h"
  19. #include "ash/test/ash_test_base.h"
  20. #include "ash/test/ash_test_helper.h"
  21. #include "base/run_loop.h"
  22. #include "base/test/bind.h"
  23. #include "base/test/metrics/histogram_tester.h"
  24. #include "base/test/scoped_feature_list.h"
  25. #include "chromeos/ash/components/network/mock_managed_network_configuration_handler.h"
  26. #include "chromeos/ash/components/network/network_state.h"
  27. #include "chromeos/ash/components/network/network_state_handler.h"
  28. #include "chromeos/ash/components/network/network_type_pattern.h"
  29. #include "chromeos/services/bluetooth_config/fake_adapter_state_controller.h"
  30. #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
  31. #include "chromeos/services/bluetooth_config/scoped_bluetooth_config_test_helper.h"
  32. #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
  33. #include "chromeos/services/network_config/public/cpp/cros_network_config_util.h"
  34. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  35. #include "components/session_manager/session_manager_types.h"
  36. #include "testing/gmock/include/gmock/gmock.h"
  37. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  38. #include "ui/base/l10n/l10n_util.h"
  39. #include "ui/views/controls/button/toggle_button.h"
  40. namespace ash {
  41. namespace {
  42. using testing::_;
  43. using testing::Return;
  44. using chromeos::bluetooth_config::ScopedBluetoothConfigTestHelper;
  45. using chromeos::bluetooth_config::mojom::BluetoothSystemState;
  46. using chromeos::network_config::CrosNetworkConfigTestHelper;
  47. using chromeos::network_config::NetworkTypeMatchesType;
  48. using chromeos::network_config::mojom::ActivationStateType;
  49. using chromeos::network_config::mojom::ConnectionStateType;
  50. using chromeos::network_config::mojom::NetworkStatePropertiesPtr;
  51. using chromeos::network_config::mojom::NetworkType;
  52. using chromeos::network_config::mojom::OncSource;
  53. using chromeos::network_config::mojom::SecurityType;
  54. const std::string kCellularName = "cellular";
  55. const std::string kCellularName2 = "cellular_2";
  56. const char kCellularDeviceName[] = "cellular_device";
  57. const char kCellularDevicePath[] = "/device/cellular_device";
  58. const char kCellularTestIccid[] = "1234567890";
  59. const char kTetherName[] = "tether";
  60. const char kTetherGuid[] = "tetherNetworkGuid";
  61. const char kTetherCarrier[] = "TetherNetworkCarrier";
  62. const char kWifiServiceGuid[] = "wifiServiceGuid";
  63. const std::string kEthernet = "ethernet";
  64. const std::string kEthernet2 = "ethernet_2";
  65. const char kVpnName[] = "vpn";
  66. const char kVpnDevicePath[] = "device/vpn";
  67. const char kWifiName[] = "wifi";
  68. const char kWifiName2[] = "wifi_2";
  69. const char kWifiDevicePath[] = "device/wifi";
  70. const char kTestEuiccBasePath[] = "/org/chromium/Hermes/Euicc/";
  71. const char kTestBaseEid[] = "12345678901234567890123456789012";
  72. const int kSignalStrength = 50;
  73. constexpr char kUser1Email[] = "user1@quicksettings.com";
  74. constexpr char kNetworkListNetworkItemView[] = "NetworkListNetworkItemView";
  75. // Delay used to simulate running process when setting device technology state.
  76. constexpr base::TimeDelta kInteractiveDelay = base::Milliseconds(3000);
  77. std::string CreateConfigurationJsonString(const std::string& guid,
  78. const std::string& type,
  79. const std::string& state) {
  80. std::stringstream ss;
  81. ss << "{"
  82. << " \"GUID\": \"" << guid << "\","
  83. << " \"Type\": \"" << type << "\","
  84. << " \"State\": \"" << state << "\""
  85. << "}";
  86. return ss.str();
  87. }
  88. std::string CreateTestEuiccPath(int euicc_num) {
  89. return base::StringPrintf("%s%d", kTestEuiccBasePath, euicc_num);
  90. }
  91. std::string CreateTestEid(int euicc_num) {
  92. return base::StringPrintf("%s%d", kTestBaseEid, euicc_num);
  93. }
  94. class TestNetworkStateHandlerObserver : public NetworkStateHandlerObserver {
  95. public:
  96. TestNetworkStateHandlerObserver() = default;
  97. TestNetworkStateHandlerObserver(const TestNetworkStateHandlerObserver&) =
  98. delete;
  99. TestNetworkStateHandlerObserver& operator=(
  100. const TestNetworkStateHandlerObserver&) = delete;
  101. // NetworkStateHandlerObserver:
  102. void ScanRequested(const NetworkTypePattern& type) override {
  103. scan_request_count_++;
  104. if (type.MatchesPattern(NetworkTypePattern::WiFi())) {
  105. wifi_scan_request_count_++;
  106. }
  107. if (type.MatchesPattern(NetworkTypePattern::Tether())) {
  108. tether_scan_request_count_++;
  109. }
  110. }
  111. // Returns the number of ScanRequested() call.
  112. size_t scan_request_count() { return scan_request_count_; }
  113. size_t wifi_scan_request_count() { return wifi_scan_request_count_; }
  114. size_t tether_scan_request_count() { return tether_scan_request_count_; }
  115. private:
  116. size_t scan_request_count_ = 0;
  117. size_t wifi_scan_request_count_ = 0;
  118. size_t tether_scan_request_count_ = 0;
  119. };
  120. } // namespace
  121. class NetworkListViewControllerTest : public AshTestBase {
  122. public:
  123. NetworkListViewControllerTest()
  124. : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  125. NetworkListViewControllerTest(const NetworkListViewControllerTest&) = delete;
  126. NetworkListViewControllerTest& operator=(
  127. const NetworkListViewControllerTest&) = delete;
  128. ~NetworkListViewControllerTest() override = default;
  129. void SetUp() override {
  130. // Initialize CrosNetworkConfigTestHelper here, so we can use
  131. // MockManagedNetworkConfigurationHandler.
  132. cros_network_config_test_helper_ =
  133. std::make_unique<network_config::CrosNetworkConfigTestHelper>(
  134. /*initialize=*/false);
  135. mock_managed_network_configuration_manager_ = base::WrapUnique(
  136. new testing::NiceMock<MockManagedNetworkConfigurationHandler>);
  137. SetGlobalPolicyConfig(/*allow_only_policy=*/false);
  138. ON_CALL(*mock_managed_network_configuration_manager_,
  139. GetGlobalConfigFromPolicy(_))
  140. .WillByDefault(Return(&global_config_));
  141. cros_network_config_test_helper_->Initialize(
  142. mock_managed_network_configuration_manager_.get());
  143. base::RunLoop().RunUntilIdle();
  144. AshTestBase::SetUp();
  145. feature_list_.InitAndEnableFeature(features::kQuickSettingsNetworkRevamp);
  146. fake_network_detailed_network_view_ =
  147. std::make_unique<FakeNetworkDetailedNetworkView>(
  148. /*delegate=*/nullptr);
  149. network_list_view_controller_impl_ =
  150. std::make_unique<NetworkListViewControllerImpl>(
  151. fake_network_detailed_network_view_.get());
  152. network_state_handler_observer_ =
  153. std::make_unique<TestNetworkStateHandlerObserver>();
  154. network_state_handler()->AddObserver(network_state_handler_observer_.get());
  155. }
  156. void SetGlobalPolicyConfig(bool allow_only_policy) {
  157. base::Value::Dict global_config_dict;
  158. global_config_dict.Set(
  159. ::onc::global_network_config::kAllowOnlyPolicyCellularNetworks,
  160. allow_only_policy);
  161. global_config_ = base::Value(std::move(global_config_dict));
  162. // This function can be called before AshTestBase::SetUp(), Shell is not
  163. // initialized, make sure to only call FlushGlobalPolicyForTesting
  164. // after initialization.
  165. if (Shell::HasInstance()) {
  166. Shell::Get()
  167. ->system_tray_model()
  168. ->network_state_model()
  169. ->FlushGlobalPolicyForTesting();
  170. base::RunLoop().RunUntilIdle();
  171. }
  172. }
  173. void TearDown() override {
  174. network_state_handler()->RemoveObserver(
  175. network_state_handler_observer_.get());
  176. network_state_handler_observer_.reset();
  177. network_list_view_controller_impl_.reset();
  178. fake_network_detailed_network_view_.reset();
  179. cros_network_config_test_helper_.reset();
  180. AshTestBase::TearDown();
  181. }
  182. views::ToggleButton* GetMobileToggleButton() {
  183. return static_cast<views::ToggleButton*>(GetMobileSubHeader()->GetViewByID(
  184. static_cast<int>(NetworkListNetworkHeaderView::kToggleButtonId)));
  185. }
  186. views::ToggleButton* GetWifiToggleButton() {
  187. return static_cast<views::ToggleButton*>(GetWifiSubHeader()->GetViewByID(
  188. static_cast<int>(NetworkListNetworkHeaderView::kToggleButtonId)));
  189. }
  190. FakeNetworkListMobileHeaderView* GetMobileSubHeader() {
  191. return FindViewById<FakeNetworkListMobileHeaderView*>(
  192. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  193. kMobileSectionHeader);
  194. }
  195. views::Separator* GetMobileSeparator() {
  196. return FindViewById<views::Separator*>(
  197. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  198. kMobileSeperator);
  199. }
  200. FakeNetworkListWifiHeaderView* GetWifiSubHeader() {
  201. return FindViewById<FakeNetworkListWifiHeaderView*>(
  202. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  203. kWifiSectionHeader);
  204. }
  205. views::Separator* GetWifiSeparator() {
  206. return FindViewById<views::Separator*>(
  207. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  208. kWifiSeperator);
  209. }
  210. TrayInfoLabel* GetMobileStatusMessage() {
  211. return FindViewById<TrayInfoLabel*>(
  212. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  213. kMobileStatusMessage);
  214. }
  215. TrayInfoLabel* GetWifiStatusMessage() {
  216. return FindViewById<TrayInfoLabel*>(
  217. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  218. kWifiStatusMessage);
  219. }
  220. TriView* GetConnectionWarning() {
  221. return FindViewById<TriView*>(
  222. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  223. kConnectionWarning);
  224. }
  225. views::Label* GetConnectionLabelView() {
  226. return FindViewById<views::Label*>(
  227. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId::
  228. kConnectionWarningLabel);
  229. }
  230. views::View* GetViewInNetworkList(std::string id) {
  231. return network_list_view_controller_impl_->network_id_to_view_map_[id];
  232. }
  233. void UpdateNetworkList(
  234. const std::vector<NetworkStatePropertiesPtr>& networks) {
  235. network_list_view_controller_impl_->OnGetNetworkStateList(
  236. mojo::Clone(networks));
  237. }
  238. // Checks that network list items are in the right order. Wifi section
  239. // is always shown.
  240. void CheckNetworkListOrdering(int ethernet_network_count,
  241. int mobile_network_count,
  242. int wifi_network_count) {
  243. EXPECT_NE(nullptr, GetWifiSubHeader());
  244. size_t index = 0;
  245. // Expect that the view at |index| is a network item, and that it is an
  246. // ethernet network.
  247. for (int i = 0; i < ethernet_network_count; i++) {
  248. CheckNetworkListItem(NetworkType::kEthernet, index++,
  249. /*guid=*/absl::nullopt);
  250. }
  251. // Mobile data section. If |mobile_network_count| is equal to -1
  252. // Mobile device is not available.
  253. if (mobile_network_count != -1) {
  254. EXPECT_NE(nullptr, GetMobileSubHeader());
  255. if (index > 0) {
  256. // Expect that the mobile network separator exists.
  257. EXPECT_NE(nullptr, GetMobileSeparator());
  258. EXPECT_EQ(network_list()->children().at(index++), GetMobileSeparator());
  259. EXPECT_EQ(network_list()->children().at(index++), GetMobileSubHeader());
  260. } else {
  261. EXPECT_EQ(nullptr, GetMobileSeparator());
  262. EXPECT_EQ(network_list()->children().at(index++), GetMobileSubHeader());
  263. }
  264. for (int i = 0; i < mobile_network_count; i++) {
  265. CheckNetworkListItem(NetworkType::kMobile, index,
  266. /*guid=*/absl::nullopt);
  267. EXPECT_STREQ(network_list()->children().at(index++)->GetClassName(),
  268. kNetworkListNetworkItemView);
  269. }
  270. if (!mobile_network_count) {
  271. // No mobile networks message is shown.
  272. EXPECT_NE(nullptr, GetMobileStatusMessage());
  273. index++;
  274. }
  275. }
  276. // Wifi section.
  277. if (index > 0) {
  278. // Expect that the wifi network separator exists.
  279. EXPECT_NE(nullptr, GetWifiSeparator());
  280. EXPECT_EQ(network_list()->children().at(index++), GetWifiSeparator());
  281. EXPECT_EQ(network_list()->children().at(index++), GetWifiSubHeader());
  282. } else {
  283. EXPECT_EQ(nullptr, GetWifiSeparator());
  284. EXPECT_EQ(network_list()->children().at(index++), GetWifiSubHeader());
  285. }
  286. for (int i = 0; i < wifi_network_count; i++) {
  287. CheckNetworkListItem(NetworkType::kWiFi, index, /*guid=*/absl::nullopt);
  288. EXPECT_STREQ(network_list()->children().at(index++)->GetClassName(),
  289. kNetworkListNetworkItemView);
  290. }
  291. if (!wifi_network_count) {
  292. // When no WiFi networks are available, status message is shown.
  293. EXPECT_NE(nullptr, GetWifiStatusMessage());
  294. index++;
  295. } else {
  296. // Status message is not shown when WiFi networks are available.
  297. EXPECT_EQ(nullptr, GetWifiStatusMessage());
  298. }
  299. }
  300. void CheckNetworkListItem(NetworkType type,
  301. size_t index,
  302. const absl::optional<std::string>& guid) {
  303. ASSERT_GT(network_list()->children().size(), index);
  304. EXPECT_STREQ(network_list()->children().at(index)->GetClassName(),
  305. kNetworkListNetworkItemView);
  306. const NetworkStatePropertiesPtr& network =
  307. static_cast<NetworkListNetworkItemView*>(
  308. network_list()->children().at(index))
  309. ->network_properties();
  310. EXPECT_TRUE(NetworkTypeMatchesType(network->type, type));
  311. if (guid.has_value()) {
  312. EXPECT_EQ(network->guid, guid);
  313. }
  314. }
  315. void SetupCellular() {
  316. network_state_helper()->manager_test()->AddTechnology(shill::kTypeCellular,
  317. /*enabled=*/true);
  318. network_state_helper()->device_test()->AddDevice(
  319. kCellularDevicePath, shill::kTypeCellular, kCellularDeviceName);
  320. base::Value::List sim_slot_infos;
  321. base::Value::Dict slot_info_item;
  322. slot_info_item.Set(shill::kSIMSlotInfoICCID, kCellularTestIccid);
  323. slot_info_item.Set(shill::kSIMSlotInfoPrimary, true);
  324. slot_info_item.Set(shill::kSIMSlotInfoEID, kTestBaseEid);
  325. sim_slot_infos.Append(std::move(slot_info_item));
  326. network_state_helper()->device_test()->SetDeviceProperty(
  327. kCellularDevicePath, shill::kSIMSlotInfoProperty,
  328. base::Value(std::move(sim_slot_infos)), /*notify_changed=*/true);
  329. // Wait for network state and device change events to be handled.
  330. base::RunLoop().RunUntilIdle();
  331. }
  332. void AddEuicc() {
  333. network_state_helper()->hermes_manager_test()->AddEuicc(
  334. dbus::ObjectPath(CreateTestEuiccPath(/*euicc_num=*/1)),
  335. CreateTestEid(/*euicc_num=*/1), /*is_active=*/true,
  336. /*physical_slot=*/0);
  337. // Wait for network state change events to be handled.
  338. base::RunLoop().RunUntilIdle();
  339. }
  340. void SetCellularSimLockStatus(const std::string& lock_type, bool sim_locked) {
  341. base::Value sim_lock_status(base::Value::Type::DICTIONARY);
  342. sim_lock_status.SetKey(shill::kSIMLockEnabledProperty,
  343. base::Value(sim_locked));
  344. sim_lock_status.SetKey(shill::kSIMLockTypeProperty, base::Value(lock_type));
  345. sim_lock_status.SetKey(shill::kSIMLockRetriesLeftProperty, base::Value(3));
  346. network_state_helper()->device_test()->SetDeviceProperty(
  347. kCellularDevicePath, shill::kSIMLockStatusProperty,
  348. std::move(sim_lock_status),
  349. /*notify_changed=*/true);
  350. base::RunLoop().RunUntilIdle();
  351. }
  352. // Adds a Tether network state, adds a Wifi network to be used as the Wifi
  353. // hotspot, and associates the two networks.
  354. void AddTetherNetworkState() {
  355. network_state_handler()->SetTetherTechnologyState(
  356. NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED);
  357. network_state_handler()->AddTetherNetworkState(
  358. kTetherGuid, kTetherName, kTetherCarrier, /*battery_percentage=*/100,
  359. kSignalStrength, /*has_connected_to_host=*/false);
  360. network_state_helper()->ConfigureService(CreateConfigurationJsonString(
  361. kWifiServiceGuid, shill::kTypeWifi, shill::kStateReady));
  362. network_state_handler()->AssociateTetherNetworkStateWithWifiNetwork(
  363. kTetherGuid, kWifiServiceGuid);
  364. }
  365. void AddVpnDevice() {
  366. network_state_helper()->manager_test()->AddTechnology(shill::kTypeVPN,
  367. /*enabled=*/true);
  368. network_state_helper()->device_test()->AddDevice(kVpnDevicePath,
  369. shill::kTypeVPN, kVpnName);
  370. // Wait for network state and device change events to be handled.
  371. base::RunLoop().RunUntilIdle();
  372. }
  373. void AddWifiDevice() {
  374. network_state_helper()->manager_test()->AddTechnology(shill::kTypeWifi,
  375. /*enabled=*/true);
  376. network_state_helper()->device_test()->AddDevice(
  377. kWifiDevicePath, shill::kTypeWifi, kWifiName);
  378. network_state_helper()->device_test()->SetDeviceProperty(
  379. kWifiDevicePath, shill::kScanningProperty, base::Value(true),
  380. /*notify_changed=*/true);
  381. // Wait for network state and device change events to be handled.
  382. base::RunLoop().RunUntilIdle();
  383. }
  384. bool getScanningBarVisibility() {
  385. return fake_network_detailed_network_view_->last_scan_bar_visibility();
  386. }
  387. size_t GetScanCount() {
  388. return network_state_handler_observer_->scan_request_count();
  389. }
  390. size_t GetWifiScanCount() {
  391. return network_state_handler_observer_->wifi_scan_request_count();
  392. }
  393. size_t GetTetherScanCount() {
  394. return network_state_handler_observer_->tether_scan_request_count();
  395. }
  396. std::unique_ptr<CellularInhibitor::InhibitLock> InhibitCellularScanning() {
  397. base::RunLoop inhibit_loop;
  398. CellularInhibitor::InhibitReason inhibit_reason =
  399. CellularInhibitor::InhibitReason::kInstallingProfile;
  400. std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock;
  401. cros_network_config_test_helper_->cellular_inhibitor()
  402. ->InhibitCellularScanning(
  403. inhibit_reason,
  404. base::BindLambdaForTesting(
  405. [&](std::unique_ptr<CellularInhibitor::InhibitLock> result) {
  406. inhibit_lock = std::move(result);
  407. inhibit_loop.Quit();
  408. }));
  409. inhibit_loop.Run();
  410. return inhibit_lock;
  411. }
  412. NetworkStatePropertiesPtr CreateStandaloneNetworkProperties(
  413. const std::string& id,
  414. NetworkType type,
  415. ConnectionStateType connection_state) {
  416. return cros_network_config_test_helper_->CreateStandaloneNetworkProperties(
  417. id, type, connection_state, kSignalStrength);
  418. }
  419. void SetBluetoothAdapterState(BluetoothSystemState system_state) {
  420. bluetooth_config_test_helper()
  421. ->fake_adapter_state_controller()
  422. ->SetSystemState(system_state);
  423. base::RunLoop().RunUntilIdle();
  424. }
  425. void LoginAsSecondaryUser() {
  426. GetSessionControllerClient()->AddUserSession(kUser1Email);
  427. SimulateUserLogin(kUser1Email);
  428. GetSessionControllerClient()->SetSessionState(
  429. session_manager::SessionState::LOGIN_SECONDARY);
  430. base::RunLoop().RunUntilIdle();
  431. }
  432. bool HasScanTimerStarted() {
  433. return network_list_view_controller_impl_->network_scan_repeating_timer_
  434. .IsRunning();
  435. }
  436. NetworkStateHandler* network_state_handler() {
  437. return network_state_helper()->network_state_handler();
  438. }
  439. NetworkStateTestHelper* network_state_helper() {
  440. return &cros_network_config_test_helper_->network_state_helper();
  441. }
  442. views::View* network_list() {
  443. return static_cast<NetworkDetailedNetworkView*>(
  444. fake_network_detailed_network_view_.get())
  445. ->network_list();
  446. }
  447. protected:
  448. const std::vector<NetworkStatePropertiesPtr> empty_list_;
  449. base::HistogramTester histogram_tester;
  450. private:
  451. template <class T>
  452. T FindViewById(
  453. NetworkListViewControllerImpl::NetworkListViewControllerViewChildId id) {
  454. return static_cast<T>(network_list()->GetViewByID(static_cast<int>(id)));
  455. }
  456. ScopedBluetoothConfigTestHelper* bluetooth_config_test_helper() {
  457. return ash_test_helper()->bluetooth_config_test_helper();
  458. }
  459. base::test::ScopedFeatureList feature_list_;
  460. std::unique_ptr<FakeNetworkDetailedNetworkView>
  461. fake_network_detailed_network_view_;
  462. std::unique_ptr<NetworkListViewControllerImpl>
  463. network_list_view_controller_impl_;
  464. std::unique_ptr<network_config::CrosNetworkConfigTestHelper>
  465. cros_network_config_test_helper_;
  466. std::unique_ptr<MockManagedNetworkConfigurationHandler>
  467. mock_managed_network_configuration_manager_;
  468. base::Value global_config_;
  469. std::unique_ptr<TestNetworkStateHandlerObserver>
  470. network_state_handler_observer_;
  471. };
  472. TEST_F(NetworkListViewControllerTest, MobileDataSectionIsShown) {
  473. EXPECT_EQ(nullptr, GetMobileSubHeader());
  474. EXPECT_EQ(nullptr, GetMobileSeparator());
  475. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  476. DetailedViewSection::kMobileSection, 0);
  477. AddEuicc();
  478. SetupCellular();
  479. EXPECT_NE(nullptr, GetMobileSubHeader());
  480. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  481. DetailedViewSection::kMobileSection, 1);
  482. // Mobile separator is still null because mobile data is at index 0.
  483. EXPECT_EQ(nullptr, GetMobileSeparator());
  484. // Clear device list and check if Mobile subheader is shown with just
  485. // tether device.
  486. network_state_helper()->ClearDevices();
  487. EXPECT_EQ(nullptr, GetMobileSubHeader());
  488. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  489. DetailedViewSection::kMobileSection, 1);
  490. // Add tether networks
  491. AddTetherNetworkState();
  492. EXPECT_NE(nullptr, GetMobileSubHeader());
  493. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  494. DetailedViewSection::kMobileSection, 2);
  495. // Tether device is prohibited.
  496. network_state_handler()->SetTetherTechnologyState(
  497. NetworkStateHandler::TechnologyState::TECHNOLOGY_PROHIBITED);
  498. base::RunLoop().RunUntilIdle();
  499. EXPECT_EQ(nullptr, GetMobileSubHeader());
  500. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  501. DetailedViewSection::kMobileSection, 2);
  502. // Tether device is uninitialized but is primary user.
  503. network_state_handler()->SetTetherTechnologyState(
  504. NetworkStateHandler::TechnologyState::TECHNOLOGY_UNINITIALIZED);
  505. base::RunLoop().RunUntilIdle();
  506. EXPECT_NE(nullptr, GetMobileSubHeader());
  507. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  508. DetailedViewSection::kMobileSection, 3);
  509. // Simulate login as secondary user.
  510. LoginAsSecondaryUser();
  511. UpdateNetworkList(empty_list_);
  512. EXPECT_EQ(nullptr, GetMobileSubHeader());
  513. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  514. DetailedViewSection::kMobileSection, 3);
  515. // Add tether networks
  516. AddTetherNetworkState();
  517. EXPECT_NE(nullptr, GetMobileSubHeader());
  518. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  519. DetailedViewSection::kMobileSection, 4);
  520. }
  521. TEST_F(NetworkListViewControllerTest, WifiSectionHeader) {
  522. EXPECT_EQ(nullptr, GetWifiSubHeader());
  523. EXPECT_EQ(nullptr, GetWifiSeparator());
  524. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  525. DetailedViewSection::kWifiSection, 0);
  526. // Add an enabled wifi device.
  527. AddWifiDevice();
  528. EXPECT_NE(nullptr, GetWifiSubHeader());
  529. EXPECT_EQ(nullptr, GetWifiSeparator());
  530. EXPECT_TRUE(GetWifiToggleButton()->GetVisible());
  531. EXPECT_TRUE(GetWifiSubHeader()->is_toggle_enabled());
  532. EXPECT_TRUE(GetWifiSubHeader()->is_toggle_on());
  533. EXPECT_TRUE(GetWifiSubHeader()->is_join_wifi_enabled());
  534. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  535. DetailedViewSection::kWifiSection, 1);
  536. // Disable wifi device.
  537. network_state_handler()->SetTechnologyEnabled(
  538. NetworkTypePattern::WiFi(), /*enabled=*/false, base::DoNothing());
  539. base::RunLoop().RunUntilIdle();
  540. EXPECT_NE(nullptr, GetWifiSubHeader());
  541. EXPECT_TRUE(GetWifiToggleButton()->GetVisible());
  542. EXPECT_TRUE(GetWifiSubHeader()->is_toggle_enabled());
  543. EXPECT_FALSE(GetWifiSubHeader()->is_toggle_on());
  544. EXPECT_FALSE(GetWifiSubHeader()->is_join_wifi_enabled());
  545. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  546. DetailedViewSection::kWifiSection, 1);
  547. }
  548. TEST_F(NetworkListViewControllerTest, MobileSectionHeaderAddEsimButtonStates) {
  549. EXPECT_EQ(nullptr, GetMobileSubHeader());
  550. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  551. SetupCellular();
  552. EXPECT_NE(nullptr, GetMobileSubHeader());
  553. EXPECT_TRUE(GetMobileSubHeader()->is_add_esim_enabled());
  554. // Since no Euicc was added, this means device is not eSIM capable, do not
  555. // show add eSIM button.
  556. EXPECT_FALSE(GetMobileSubHeader()->is_add_esim_visible());
  557. AddEuicc();
  558. UpdateNetworkList(empty_list_);
  559. EXPECT_TRUE(GetMobileSubHeader()->is_add_esim_visible());
  560. EXPECT_EQ(nullptr, GetMobileSeparator());
  561. EXPECT_NE(nullptr, GetMobileStatusMessage());
  562. // Add eSIM button is not enabled when inhibited.
  563. std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
  564. InhibitCellularScanning();
  565. EXPECT_TRUE(inhibit_lock);
  566. base::RunLoop().RunUntilIdle();
  567. EXPECT_FALSE(GetMobileSubHeader()->is_add_esim_enabled());
  568. EXPECT_TRUE(GetMobileSubHeader()->is_add_esim_visible());
  569. // Uninhibit the device.
  570. inhibit_lock.reset();
  571. base::RunLoop().RunUntilIdle();
  572. EXPECT_TRUE(GetMobileSubHeader()->is_add_esim_enabled());
  573. EXPECT_TRUE(GetMobileSubHeader()->is_add_esim_visible());
  574. // When no Mobile networks are available and eSIM policy is set to allow only
  575. // cellular devices which means adding a new eSIM is disallowed by enterprise
  576. // policy, add eSIM button is not displayed.
  577. SetGlobalPolicyConfig(/*allow_only_policy=*/true);
  578. UpdateNetworkList(empty_list_);
  579. EXPECT_FALSE(GetMobileSubHeader()->is_add_esim_visible());
  580. }
  581. TEST_F(NetworkListViewControllerTest, HasCorrectMobileNetworkList) {
  582. EXPECT_EQ(0u, network_list()->children().size());
  583. EXPECT_EQ(nullptr, GetMobileSubHeader());
  584. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  585. AddEuicc();
  586. SetupCellular();
  587. AddWifiDevice();
  588. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  589. /*mobile_network_count=*/0,
  590. /*wifi_network_count=*/0);
  591. std::vector<NetworkStatePropertiesPtr> networks;
  592. NetworkStatePropertiesPtr cellular_network =
  593. CreateStandaloneNetworkProperties(kCellularName, NetworkType::kCellular,
  594. ConnectionStateType::kConnected);
  595. networks.push_back(std::move(cellular_network));
  596. UpdateNetworkList(networks);
  597. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  598. /*mobile_network_count=*/1,
  599. /*wifi_network_count=*/0);
  600. CheckNetworkListItem(NetworkType::kCellular, /*index=*/1u,
  601. /*guid=*/kCellularName);
  602. cellular_network = CreateStandaloneNetworkProperties(
  603. kCellularName2, NetworkType::kCellular, ConnectionStateType::kConnected);
  604. networks.push_back(std::move(cellular_network));
  605. UpdateNetworkList(networks);
  606. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  607. /*mobile_network_count=*/2,
  608. /*wifi_network_count=*/0);
  609. CheckNetworkListItem(NetworkType::kCellular, /*index=*/2u,
  610. /*guid=*/kCellularName2);
  611. // Update a network and make sure it is still in network list.
  612. networks.front()->connection_state = ConnectionStateType::kNotConnected;
  613. UpdateNetworkList(networks);
  614. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  615. /*mobile_network_count=*/2,
  616. /*wifi_network_count=*/0);
  617. CheckNetworkListItem(NetworkType::kCellular, /*index=*/1u,
  618. /*guid=*/kCellularName);
  619. CheckNetworkListItem(NetworkType::kCellular, /*index=*/2u,
  620. /*guid=*/kCellularName2);
  621. // Remove all networks and add Tether networks. Only one network should be in
  622. // list.
  623. networks.clear();
  624. NetworkStatePropertiesPtr tether_network = CreateStandaloneNetworkProperties(
  625. kTetherName, NetworkType::kTether, ConnectionStateType::kConnected);
  626. networks.push_back(std::move(tether_network));
  627. UpdateNetworkList(networks);
  628. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  629. /*mobile_network_count=*/1,
  630. /*wifi_network_count=*/0);
  631. CheckNetworkListItem(NetworkType::kTether, /*index=*/1u,
  632. /*guid=*/kTetherName);
  633. }
  634. TEST_F(NetworkListViewControllerTest, HasCorrectEthernetNetworkList) {
  635. std::vector<NetworkStatePropertiesPtr> networks;
  636. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  637. DetailedViewSection::kEthernetSection, 0);
  638. NetworkStatePropertiesPtr ethernet_network =
  639. CreateStandaloneNetworkProperties(kEthernet, NetworkType::kEthernet,
  640. ConnectionStateType::kNotConnected);
  641. networks.push_back(std::move(ethernet_network));
  642. UpdateNetworkList(networks);
  643. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  644. DetailedViewSection::kEthernetSection, 1);
  645. CheckNetworkListOrdering(/*ethernet_network_count=*/1,
  646. /*mobile_network_count=*/-1,
  647. /*wifi_network_count=*/0);
  648. CheckNetworkListItem(NetworkType::kEthernet, /*index=*/0u,
  649. /*guid=*/kEthernet);
  650. // Add mobile network.
  651. AddEuicc();
  652. SetupCellular();
  653. NetworkStatePropertiesPtr cellular_network =
  654. CreateStandaloneNetworkProperties(kCellularName, NetworkType::kCellular,
  655. ConnectionStateType::kConnected);
  656. networks.push_back(std::move(cellular_network));
  657. UpdateNetworkList(networks);
  658. CheckNetworkListOrdering(/*ethernet_network_count=*/1,
  659. /*mobile_network_count=*/1,
  660. /*wifi_network_count=*/0);
  661. // Metrics is recorded here because when AddEuicc() and SetupCellular() are
  662. // called, model()->cros_network_config()->GetNetworkStateList returns an
  663. // empty list of networks, this resets the present network list map.
  664. // The next call to UpdateNetworkList(networks), the views are re-added and
  665. // a metric is recorded.
  666. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  667. DetailedViewSection::kEthernetSection, 2);
  668. // Mobile list item will be at index 3 after ethernet, separator and header.
  669. CheckNetworkListItem(NetworkType::kCellular, /*index=*/3u,
  670. /*guid=*/kCellularName);
  671. ethernet_network = CreateStandaloneNetworkProperties(
  672. kEthernet2, NetworkType::kEthernet, ConnectionStateType::kNotConnected);
  673. networks.push_back(std::move(ethernet_network));
  674. UpdateNetworkList(networks);
  675. // Metrics is only recorded the first time ethernet section is shown. Here a
  676. // new ethernet network was added but the section was already being shown, so
  677. // no new metric would be recorded.
  678. histogram_tester.ExpectBucketCount("ChromeOS.SystemTray.Network.SectionShown",
  679. DetailedViewSection::kEthernetSection, 2);
  680. CheckNetworkListOrdering(/*ethernet_network_count=*/2,
  681. /*mobile_network_count=*/1,
  682. /*wifi_network_count=*/0);
  683. CheckNetworkListItem(NetworkType::kEthernet, /*index=*/0u,
  684. /*guid=*/kEthernet);
  685. CheckNetworkListItem(NetworkType::kEthernet, /*index=*/1u,
  686. /*guid=*/kEthernet2);
  687. // Mobile list item will be at index 4 after ethernet, separator and header.
  688. CheckNetworkListItem(NetworkType::kCellular, /*index=*/4u,
  689. /*guid=*/kCellularName);
  690. }
  691. TEST_F(NetworkListViewControllerTest, HasCorrectWifiNetworkList) {
  692. std::vector<NetworkStatePropertiesPtr> networks;
  693. // Add an enabled wifi device.
  694. AddWifiDevice();
  695. // Add Wifi network.
  696. NetworkStatePropertiesPtr wifi_network = CreateStandaloneNetworkProperties(
  697. kWifiName, NetworkType::kWiFi, ConnectionStateType::kNotConnected);
  698. networks.push_back(std::move(wifi_network));
  699. UpdateNetworkList(networks);
  700. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  701. /*mobile_network_count=*/-1,
  702. /*wifi_network_count=*/1);
  703. // Wifi list item will be at index 1 after Wifi header.
  704. CheckNetworkListItem(NetworkType::kWiFi, /*index=*/1u, /*guid=*/kWifiName);
  705. // Add mobile network.
  706. AddEuicc();
  707. SetupCellular();
  708. NetworkStatePropertiesPtr cellular_network =
  709. CreateStandaloneNetworkProperties(kCellularName, NetworkType::kCellular,
  710. ConnectionStateType::kConnected);
  711. networks.push_back(std::move(cellular_network));
  712. UpdateNetworkList(networks);
  713. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  714. /*mobile_network_count=*/1,
  715. /*wifi_network_count=*/1);
  716. // Wifi list item be at index 4 after Mobile header, Mobile network
  717. // item, Wifi separator and header.
  718. CheckNetworkListItem(NetworkType::kWiFi, /*index=*/4u, /*guid=*/kWifiName);
  719. // Add a second Wifi network.
  720. wifi_network = CreateStandaloneNetworkProperties(
  721. kWifiName2, NetworkType::kWiFi, ConnectionStateType::kNotConnected);
  722. networks.push_back(std::move(wifi_network));
  723. UpdateNetworkList(networks);
  724. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  725. /*mobile_network_count=*/1,
  726. /*wifi_network_count=*/2);
  727. CheckNetworkListItem(NetworkType::kWiFi, /*index=*/4u, /*guid=*/kWifiName);
  728. CheckNetworkListItem(NetworkType::kWiFi, /*index=*/5u, /*guid=*/kWifiName2);
  729. }
  730. TEST_F(NetworkListViewControllerTest,
  731. CellularStatusMessageAndToggleButtonState) {
  732. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  733. AddEuicc();
  734. SetupCellular();
  735. // Update cellular device state to be Uninitialized.
  736. network_state_helper()->manager_test()->SetTechnologyInitializing(
  737. shill::kTypeCellular, /*initializing=*/true);
  738. base::RunLoop().RunUntilIdle();
  739. EXPECT_NE(nullptr, GetMobileStatusMessage());
  740. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  741. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  742. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_on());
  743. EXPECT_EQ(
  744. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR),
  745. GetMobileStatusMessage()->label()->GetText());
  746. network_state_helper()->manager_test()->SetTechnologyInitializing(
  747. shill::kTypeCellular, /*initializing=*/false);
  748. base::RunLoop().RunUntilIdle();
  749. SetupCellular();
  750. EXPECT_NE(nullptr, GetMobileStatusMessage());
  751. EXPECT_NE(nullptr, GetMobileSubHeader());
  752. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NO_MOBILE_NETWORKS),
  753. GetMobileStatusMessage()->label()->GetText());
  754. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  755. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  756. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  757. // No message is shown when there are available networks.
  758. std::vector<NetworkStatePropertiesPtr> networks;
  759. networks.push_back(CreateStandaloneNetworkProperties(
  760. kCellularName, NetworkType::kCellular, ConnectionStateType::kConnected));
  761. UpdateNetworkList(networks);
  762. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  763. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  764. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  765. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  766. // Message shown again when list is empty.
  767. UpdateNetworkList(empty_list_);
  768. EXPECT_NE(nullptr, GetMobileStatusMessage());
  769. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NO_MOBILE_NETWORKS),
  770. GetMobileStatusMessage()->label()->GetText());
  771. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  772. // No message is shown when inhibited.
  773. std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
  774. InhibitCellularScanning();
  775. EXPECT_TRUE(inhibit_lock);
  776. base::RunLoop().RunUntilIdle();
  777. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  778. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  779. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  780. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  781. // Uninhibit the device.
  782. inhibit_lock.reset();
  783. base::RunLoop().RunUntilIdle();
  784. // Message is shown when uninhibited.
  785. EXPECT_NE(nullptr, GetMobileStatusMessage());
  786. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NO_MOBILE_NETWORKS),
  787. GetMobileStatusMessage()->label()->GetText());
  788. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  789. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  790. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  791. // When device is in disabling message is shown.
  792. network_state_helper()->manager_test()->SetInteractiveDelay(
  793. kInteractiveDelay);
  794. network_state_handler()->SetTechnologyEnabled(
  795. NetworkTypePattern::Cellular(), /*enabled=*/false, base::DoNothing());
  796. base::RunLoop().RunUntilIdle();
  797. EXPECT_NE(nullptr, GetMobileStatusMessage());
  798. EXPECT_EQ(
  799. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_MOBILE_DISABLING),
  800. GetMobileStatusMessage()->label()->GetText());
  801. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  802. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_on());
  803. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  804. task_environment()->FastForwardBy(kInteractiveDelay);
  805. // Message is shown when device is disabled.
  806. EXPECT_NE(nullptr, GetMobileStatusMessage());
  807. EXPECT_EQ(
  808. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_MOBILE_DISABLED),
  809. GetMobileStatusMessage()->label()->GetText());
  810. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  811. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_on());
  812. EXPECT_TRUE(GetMobileToggleButton()->GetVisible());
  813. // The toggle is not enabled, the cellular device SIM is locked, and user
  814. // cannot open the settings page.
  815. GetSessionControllerClient()->SetSessionState(
  816. session_manager::SessionState::LOGIN_SECONDARY);
  817. SetCellularSimLockStatus(shill::kSIMLockPin, /*sim_locked=*/true);
  818. base::RunLoop().RunUntilIdle();
  819. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  820. }
  821. TEST_F(NetworkListViewControllerTest, HasCorrectTetherStatusMessage) {
  822. // Mobile section is not shown if Tether network is unavailable.
  823. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  824. // Tether is enabled but no devices are added.
  825. network_state_handler()->SetTetherTechnologyState(
  826. NetworkStateHandler::TechnologyState::TECHNOLOGY_ENABLED);
  827. base::RunLoop().RunUntilIdle();
  828. EXPECT_NE(nullptr, GetMobileStatusMessage());
  829. EXPECT_NE(nullptr, GetMobileSubHeader());
  830. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  831. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  832. EXPECT_EQ(
  833. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NO_MOBILE_DEVICES_FOUND),
  834. GetMobileStatusMessage()->label()->GetText());
  835. // Tether network is uninitialized and Bluetooth state enabling.
  836. network_state_handler()->SetTetherTechnologyState(
  837. NetworkStateHandler::TechnologyState::TECHNOLOGY_UNINITIALIZED);
  838. base::RunLoop().RunUntilIdle();
  839. SetBluetoothAdapterState(BluetoothSystemState::kEnabling);
  840. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  841. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_on());
  842. EXPECT_NE(nullptr, GetMobileStatusMessage());
  843. EXPECT_EQ(
  844. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR),
  845. GetMobileStatusMessage()->label()->GetText());
  846. // Set Bluetooth device to disabling.
  847. SetBluetoothAdapterState(BluetoothSystemState::kDisabling);
  848. EXPECT_TRUE(GetMobileSubHeader()->is_toggle_enabled());
  849. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_on());
  850. EXPECT_NE(nullptr, GetMobileStatusMessage());
  851. EXPECT_EQ(l10n_util::GetStringUTF16(
  852. IDS_ASH_STATUS_TRAY_ENABLING_MOBILE_ENABLES_BLUETOOTH),
  853. GetMobileStatusMessage()->label()->GetText());
  854. // Simulate login as secondary user and disable Bluetooth device.
  855. LoginAsSecondaryUser();
  856. SetBluetoothAdapterState(BluetoothSystemState::kDisabled);
  857. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_enabled());
  858. EXPECT_FALSE(GetMobileSubHeader()->is_toggle_on());
  859. EXPECT_NE(nullptr, GetMobileStatusMessage());
  860. EXPECT_EQ(l10n_util::GetStringUTF16(
  861. IDS_ASH_STATUS_TRAY_ENABLING_MOBILE_ENABLES_BLUETOOTH),
  862. GetMobileStatusMessage()->label()->GetText());
  863. // No message shown when Tether devices are added.
  864. AddTetherNetworkState();
  865. EXPECT_EQ(nullptr, GetMobileStatusMessage());
  866. }
  867. TEST_F(NetworkListViewControllerTest, HasCorrectWifiStatusMessage) {
  868. EXPECT_EQ(nullptr, GetWifiStatusMessage());
  869. // Add an enabled wifi device.
  870. AddWifiDevice();
  871. // Wifi is enabled but not networks are added.
  872. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED),
  873. GetWifiStatusMessage()->label()->GetText());
  874. // Disable wifi device.
  875. network_state_handler()->SetTechnologyEnabled(
  876. NetworkTypePattern::WiFi(), /*enabled=*/false, base::DoNothing());
  877. base::RunLoop().RunUntilIdle();
  878. EXPECT_EQ(
  879. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED),
  880. GetWifiStatusMessage()->label()->GetText());
  881. // Enable and add wifi network.
  882. network_state_handler()->SetTechnologyEnabled(
  883. NetworkTypePattern::WiFi(), /*enabled=*/true, base::DoNothing());
  884. base::RunLoop().RunUntilIdle();
  885. std::vector<NetworkStatePropertiesPtr> networks;
  886. networks.push_back(CreateStandaloneNetworkProperties(
  887. kWifiName, NetworkType::kWiFi, ConnectionStateType::kNotConnected));
  888. UpdateNetworkList(networks);
  889. CheckNetworkListOrdering(/*ethernet_network_count=*/0,
  890. /*mobile_network_count=*/-1,
  891. /*wifi_network_count=*/1);
  892. }
  893. TEST_F(NetworkListViewControllerTest, HasConnectionWarning) {
  894. EXPECT_EQ(nullptr, GetConnectionWarning());
  895. AddVpnDevice();
  896. std::vector<NetworkStatePropertiesPtr> networks;
  897. networks.push_back(CreateStandaloneNetworkProperties(
  898. kVpnName, NetworkType::kVPN, ConnectionStateType::kConnected));
  899. UpdateNetworkList(networks);
  900. EXPECT_NE(nullptr, GetConnectionWarning());
  901. EXPECT_NE(nullptr, GetConnectionLabelView());
  902. EXPECT_EQ(
  903. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_MONITORED_WARNING),
  904. GetConnectionLabelView()->GetText());
  905. EXPECT_EQ(network_list()->children().at(0), GetConnectionWarning());
  906. // Clear all devices and make sure warning is no longer being shown.
  907. network_state_helper()->ClearDevices();
  908. EXPECT_EQ(nullptr, GetConnectionWarning());
  909. }
  910. TEST_F(NetworkListViewControllerTest, NetworkScanning) {
  911. network_state_helper()->ClearDevices();
  912. network_state_helper()->manager_test()->SetInteractiveDelay(
  913. kInteractiveDelay);
  914. // ClearDevices() calls RunUntilIdle which performs some initial scans.
  915. size_t initial_wifi_count = 1u;
  916. size_t initial_tether_count = 1u;
  917. size_t initial_scan_count = 2u;
  918. // Scanning bar is not visible if WiFi is not enabled.
  919. EXPECT_FALSE(HasScanTimerStarted());
  920. EXPECT_FALSE(getScanningBarVisibility());
  921. EXPECT_EQ(initial_scan_count + 0u, GetScanCount());
  922. EXPECT_EQ(initial_wifi_count + 0u, GetWifiScanCount());
  923. EXPECT_EQ(initial_tether_count + 0u, GetTetherScanCount());
  924. // Add an enabled WiFi device.
  925. AddWifiDevice();
  926. EXPECT_TRUE(HasScanTimerStarted());
  927. EXPECT_TRUE(getScanningBarVisibility());
  928. EXPECT_EQ(initial_scan_count + 2u, GetScanCount());
  929. EXPECT_EQ(initial_wifi_count + 1u, GetWifiScanCount());
  930. EXPECT_EQ(initial_tether_count + 1u, GetTetherScanCount());
  931. // Simulate scanning finishing.
  932. task_environment()->FastForwardBy(kInteractiveDelay);
  933. EXPECT_FALSE(getScanningBarVisibility());
  934. EXPECT_TRUE(HasScanTimerStarted());
  935. EXPECT_EQ(initial_scan_count + 2u, GetScanCount());
  936. EXPECT_EQ(initial_wifi_count + 1u, GetWifiScanCount());
  937. EXPECT_EQ(initial_tether_count + 1u, GetTetherScanCount());
  938. // Make sure scan timer is still running.
  939. task_environment()->FastForwardBy(kInteractiveDelay);
  940. EXPECT_TRUE(HasScanTimerStarted());
  941. EXPECT_FALSE(getScanningBarVisibility());
  942. EXPECT_EQ(initial_scan_count + 2u, GetScanCount());
  943. EXPECT_EQ(initial_wifi_count + 1u, GetWifiScanCount());
  944. EXPECT_EQ(initial_tether_count + 1u, GetTetherScanCount());
  945. task_environment()->FastForwardBy(kInteractiveDelay);
  946. EXPECT_TRUE(HasScanTimerStarted());
  947. EXPECT_FALSE(getScanningBarVisibility());
  948. EXPECT_EQ(initial_scan_count + 2u, GetScanCount());
  949. EXPECT_EQ(initial_wifi_count + 1u, GetWifiScanCount());
  950. EXPECT_EQ(initial_tether_count + 1u, GetTetherScanCount());
  951. // Disabling WiFi device ends scan timer.
  952. network_state_handler()->SetTechnologyEnabled(
  953. NetworkTypePattern::WiFi(), /*enabled=*/false, base::DoNothing());
  954. base::RunLoop().RunUntilIdle();
  955. EXPECT_FALSE(getScanningBarVisibility());
  956. EXPECT_FALSE(HasScanTimerStarted());
  957. EXPECT_EQ(initial_scan_count + 2u, GetScanCount());
  958. EXPECT_EQ(initial_wifi_count + 1u, GetWifiScanCount());
  959. EXPECT_EQ(initial_tether_count + 1u, GetTetherScanCount());
  960. }
  961. } // namespace ash