network_list_mobile_header_view_unittest.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_mobile_header_view_impl.h"
  5. #include <memory>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/public/cpp/test/test_system_tray_client.h"
  8. #include "ash/strings/grit/ash_strings.h"
  9. #include "ash/style/icon_button.h"
  10. #include "ash/system/network/fake_network_list_network_header_view_delegate.h"
  11. #include "ash/system/tray/tray_toggle_button.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "base/run_loop.h"
  14. #include "base/test/bind.h"
  15. #include "base/test/scoped_feature_list.h"
  16. #include "chromeos/ash/components/network/network_device_handler.h"
  17. #include "chromeos/ash/components/network/network_state_handler.h"
  18. #include "chromeos/ash/components/network/network_type_pattern.h"
  19. #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
  20. #include "components/onc/onc_constants.h"
  21. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  22. #include "ui/base/l10n/l10n_util.h"
  23. #include "ui/events/base_event_utils.h"
  24. #include "ui/views/controls/button/button.h"
  25. #include "ui/views/controls/button/toggle_button.h"
  26. #include "ui/views/controls/label.h"
  27. #include "ui/views/view_utils.h"
  28. #include "ui/views/widget/widget.h"
  29. namespace ash {
  30. namespace {
  31. using chromeos::network_config::CrosNetworkConfigTestHelper;
  32. const char kStubCellularDevicePath[] = "/device/stub_cellular_device";
  33. const char kStubCellularDeviceName[] = "stub_cellular_device";
  34. } // namespace
  35. class NetworkListMobileHeaderViewTest : public AshTestBase {
  36. public:
  37. NetworkListMobileHeaderViewTest() = default;
  38. ~NetworkListMobileHeaderViewTest() override = default;
  39. // AshTestBase:
  40. void SetUp() override {
  41. AshTestBase::SetUp();
  42. feature_list_.InitAndEnableFeature(features::kQuickSettingsNetworkRevamp);
  43. network_state_helper()->ClearDevices();
  44. network_state_helper()->manager_test()->AddTechnology(shill::kTypeCellular,
  45. /*enabled=*/true);
  46. network_state_helper()->device_test()->AddDevice(
  47. kStubCellularDevicePath, shill::kTypeCellular, kStubCellularDeviceName);
  48. // Wait for network state and device change events to be handled.
  49. base::RunLoop().RunUntilIdle();
  50. }
  51. void TearDown() override {
  52. widget_.reset();
  53. AshTestBase::TearDown();
  54. }
  55. void Init() {
  56. std::unique_ptr<NetworkListMobileHeaderViewImpl>
  57. network_list_mobile_header_view =
  58. std::make_unique<NetworkListMobileHeaderViewImpl>(
  59. &fake_network_list_network_header_delegate_);
  60. widget_ = CreateFramelessTestWidget();
  61. widget_->SetFullscreen(true);
  62. network_list_mobile_header_view_ =
  63. widget_->SetContentsView(std::move(network_list_mobile_header_view));
  64. }
  65. std::unique_ptr<CellularInhibitor::InhibitLock> InhibitCellularScanning(
  66. CellularInhibitor::InhibitReason inhibit_reason) {
  67. base::RunLoop inhibit_loop;
  68. std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock;
  69. network_config_helper_.cellular_inhibitor()->InhibitCellularScanning(
  70. inhibit_reason,
  71. base::BindLambdaForTesting(
  72. [&](std::unique_ptr<CellularInhibitor::InhibitLock> result) {
  73. inhibit_lock = std::move(result);
  74. inhibit_loop.Quit();
  75. }));
  76. inhibit_loop.Run();
  77. return inhibit_lock;
  78. }
  79. void SetAddESimButtonState(bool enabled, bool visible) {
  80. network_list_mobile_header_view_->SetAddESimButtonState(enabled, visible);
  81. }
  82. NetworkStateTestHelper* network_state_helper() {
  83. return &network_config_helper_.network_state_helper();
  84. }
  85. IconButton* GetAddEsimButton() {
  86. return FindViewById<IconButton*>(
  87. NetworkListMobileHeaderViewImpl::kAddESimButtonId);
  88. }
  89. TrayToggleButton* GetToggleButton() {
  90. return FindViewById<TrayToggleButton*>(
  91. NetworkListNetworkHeaderView::kToggleButtonId);
  92. }
  93. views::Label* GetLabelView() {
  94. return FindViewById<views::Label*>(
  95. NetworkListHeaderView::kTitleLabelViewId);
  96. }
  97. FakeNetworkListNetworkHeaderViewDelegate*
  98. fake_network_list_network_header_delegate() {
  99. return &fake_network_list_network_header_delegate_;
  100. }
  101. private:
  102. template <class T>
  103. T FindViewById(int id) {
  104. return static_cast<T>(
  105. network_list_mobile_header_view_->container()->GetViewByID(id));
  106. }
  107. std::unique_ptr<views::Widget> widget_;
  108. CrosNetworkConfigTestHelper network_config_helper_;
  109. base::test::ScopedFeatureList feature_list_;
  110. FakeNetworkListNetworkHeaderViewDelegate
  111. fake_network_list_network_header_delegate_;
  112. NetworkListMobileHeaderViewImpl* network_list_mobile_header_view_;
  113. };
  114. TEST_F(NetworkListMobileHeaderViewTest, HeaderLabel) {
  115. Init();
  116. views::Label* labelView = GetLabelView();
  117. ASSERT_NE(nullptr, labelView);
  118. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_MOBILE),
  119. labelView->GetText());
  120. }
  121. TEST_F(NetworkListMobileHeaderViewTest, AddEsimButtonStates) {
  122. Init();
  123. IconButton* add_esim_button = GetAddEsimButton();
  124. ASSERT_NE(nullptr, add_esim_button);
  125. EXPECT_EQ(0, GetSystemTrayClient()->show_network_create_count());
  126. LeftClickOn(add_esim_button);
  127. EXPECT_EQ(1, GetSystemTrayClient()->show_network_create_count());
  128. EXPECT_EQ(::onc::network_type::kCellular,
  129. GetSystemTrayClient()->last_network_type());
  130. EXPECT_TRUE(add_esim_button->GetVisible());
  131. EXPECT_TRUE(add_esim_button->GetEnabled());
  132. SetAddESimButtonState(/*enabled=*/false, /*visible*/ false);
  133. EXPECT_FALSE(add_esim_button->GetVisible());
  134. EXPECT_FALSE(add_esim_button->GetEnabled());
  135. }
  136. TEST_F(NetworkListMobileHeaderViewTest, CellularInhibitState) {
  137. Init();
  138. IconButton* add_esim_button = GetAddEsimButton();
  139. ASSERT_NE(nullptr, add_esim_button);
  140. // Tooltip is not initially set.
  141. EXPECT_EQ(u"", add_esim_button->GetTooltipText());
  142. // Tooltip is not updated when eSIM button is not visible, this is
  143. // because there would not be a valid tooltip when there isnt a valid
  144. // cellular device.
  145. SetAddESimButtonState(/*enabled=*/true, /*visible*/ false);
  146. EXPECT_EQ(u"", add_esim_button->GetTooltipText());
  147. SetAddESimButtonState(/*enabled=*/true, /*visible*/ true);
  148. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ADD_CELLULAR_LABEL),
  149. add_esim_button->GetTooltipText());
  150. const struct {
  151. CellularInhibitor::InhibitReason reason;
  152. int message_id;
  153. } kTestCases[]{
  154. {CellularInhibitor::InhibitReason::kInstallingProfile,
  155. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_INSTALLING_PROFILE},
  156. {CellularInhibitor::InhibitReason::kRenamingProfile,
  157. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_RENAMING_PROFILE},
  158. {CellularInhibitor::InhibitReason::kRemovingProfile,
  159. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_REMOVING_PROFILE},
  160. {CellularInhibitor::InhibitReason::kConnectingToProfile,
  161. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_CONNECTING_TO_PROFILE},
  162. {CellularInhibitor::InhibitReason::kRefreshingProfileList,
  163. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_REFRESHING_PROFILE_LIST},
  164. {CellularInhibitor::InhibitReason::kResettingEuiccMemory,
  165. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_RESETTING_ESIM},
  166. {CellularInhibitor::InhibitReason::kDisablingProfile,
  167. IDS_ASH_STATUS_TRAY_INHIBITED_CELLULAR_DISABLING_PROFILE},
  168. };
  169. for (auto cases : kTestCases) {
  170. SCOPED_TRACE(::testing::Message()
  171. << "Inhibit Reason: " << cases.message_id);
  172. std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
  173. InhibitCellularScanning(cases.reason);
  174. EXPECT_TRUE(inhibit_lock);
  175. base::RunLoop().RunUntilIdle();
  176. SetAddESimButtonState(/*enabled=*/true, /*visible=*/true);
  177. EXPECT_EQ(l10n_util::GetStringUTF16(cases.message_id),
  178. add_esim_button->GetTooltipText());
  179. inhibit_lock = nullptr;
  180. }
  181. }
  182. TEST_F(NetworkListMobileHeaderViewTest, EnabledButtonNotAdded) {
  183. // Add eSim button should not be added if the screen is locked.
  184. GetSessionControllerClient()->SetSessionState(
  185. session_manager::SessionState::LOCKED);
  186. Init();
  187. IconButton* add_esim_button = GetAddEsimButton();
  188. EXPECT_EQ(nullptr, add_esim_button);
  189. }
  190. TEST_F(NetworkListMobileHeaderViewTest, MobileToggleButtonStates) {
  191. Init();
  192. TrayToggleButton* toggle_button = GetToggleButton();
  193. EXPECT_NE(nullptr, toggle_button);
  194. EXPECT_EQ(0u, fake_network_list_network_header_delegate()
  195. ->mobile_toggle_clicked_count());
  196. LeftClickOn(toggle_button);
  197. EXPECT_EQ(1u, fake_network_list_network_header_delegate()
  198. ->mobile_toggle_clicked_count());
  199. }
  200. } // namespace ash