auto_connect_notifier_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 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/auto_connect_notifier.h"
  5. #include <memory>
  6. #include "ash/shell.h"
  7. #include "ash/strings/grit/ash_strings.h"
  8. #include "ash/system/system_notification_controller.h"
  9. #include "ash/system/toast/toast_manager_impl.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/bind.h"
  12. #include "base/memory/ptr_util.h"
  13. #include "base/run_loop.h"
  14. #include "base/timer/mock_timer.h"
  15. #include "chromeos/ash/components/dbus/shill/shill_service_client.h"
  16. #include "chromeos/ash/components/network/auto_connect_handler.h"
  17. #include "chromeos/ash/components/network/network_cert_loader.h"
  18. #include "chromeos/ash/components/network/network_handler.h"
  19. #include "chromeos/ash/components/network/network_handler_test_helper.h"
  20. #include "chromeos/ash/components/network/system_token_cert_db_storage.h"
  21. #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
  22. #include "dbus/object_path.h"
  23. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  24. #include "ui/base/l10n/l10n_util.h"
  25. namespace ash {
  26. namespace {
  27. constexpr char kTestServicePath[] = "testServicePath";
  28. constexpr char kTestServiceGuid[] = "testServiceGuid";
  29. constexpr char kTestServiceName[] = "testServiceName";
  30. } // namespace
  31. class AutoConnectNotifierTest : public AshTestBase {
  32. public:
  33. AutoConnectNotifierTest(const AutoConnectNotifierTest&) = delete;
  34. AutoConnectNotifierTest& operator=(const AutoConnectNotifierTest&) = delete;
  35. protected:
  36. AutoConnectNotifierTest() = default;
  37. ~AutoConnectNotifierTest() override = default;
  38. void SetUp() override {
  39. SystemTokenCertDbStorage::Initialize();
  40. NetworkCertLoader::Initialize();
  41. NetworkCertLoader::ForceAvailableForNetworkAuthForTesting();
  42. network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
  43. CHECK(NetworkHandler::Get()->auto_connect_handler());
  44. network_config_helper_ = std::make_unique<
  45. chromeos::network_config::CrosNetworkConfigTestHelper>();
  46. AshTestBase::SetUp();
  47. toast_manager_ = Shell::Get()->toast_manager();
  48. mock_notification_timer_ = new base::MockOneShotTimer();
  49. Shell::Get()
  50. ->system_notification_controller()
  51. ->auto_connect_->set_timer_for_testing(
  52. base::WrapUnique(mock_notification_timer_));
  53. ShillServiceClient::Get()->GetTestInterface()->AddService(
  54. kTestServicePath, kTestServiceGuid, kTestServiceName, shill::kTypeWifi,
  55. shill::kStateIdle, true /* visible*/);
  56. // Ensure fake DBus service initialization completes.
  57. base::RunLoop().RunUntilIdle();
  58. }
  59. void TearDown() override {
  60. AshTestBase::TearDown();
  61. network_config_helper_.reset();
  62. network_handler_test_helper_.reset();
  63. NetworkCertLoader::Shutdown();
  64. SystemTokenCertDbStorage::Shutdown();
  65. }
  66. void NotifyConnectToNetworkRequested() {
  67. Shell::Get()
  68. ->system_notification_controller()
  69. ->auto_connect_->ConnectToNetworkRequested(kTestServicePath);
  70. base::RunLoop().RunUntilIdle();
  71. }
  72. void SuccessfullyJoinWifiNetwork() {
  73. ShillServiceClient::Get()->Connect(dbus::ObjectPath(kTestServicePath),
  74. base::BindOnce([]() {}),
  75. ShillServiceClient::ErrorCallback());
  76. base::RunLoop().RunUntilIdle();
  77. }
  78. ToastOverlay* GetCurrentOverlay() {
  79. return toast_manager_->GetCurrentOverlayForTesting();
  80. }
  81. void VerifyAutoConnectToastVisibility(bool visible) {
  82. if (visible) {
  83. ToastOverlay* overlay = GetCurrentOverlay();
  84. ASSERT_NE(nullptr, overlay);
  85. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_NETWORK_AUTOCONNECT),
  86. overlay->GetText());
  87. } else {
  88. EXPECT_EQ(nullptr, GetCurrentOverlay());
  89. }
  90. }
  91. // Ownership passed to Shell owned AutoConnectNotifier instance.
  92. base::MockOneShotTimer* mock_notification_timer_;
  93. private:
  94. std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
  95. std::unique_ptr<chromeos::network_config::CrosNetworkConfigTestHelper>
  96. network_config_helper_;
  97. ToastManagerImpl* toast_manager_ = nullptr;
  98. };
  99. TEST_F(AutoConnectNotifierTest, NoExplicitConnectionRequested) {
  100. NetworkHandler::Get()
  101. ->auto_connect_handler()
  102. ->NotifyAutoConnectInitiatedForTest(
  103. AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
  104. SuccessfullyJoinWifiNetwork();
  105. // Toast should not be displayed.
  106. VerifyAutoConnectToastVisibility(/*visible=*/false);
  107. }
  108. TEST_F(AutoConnectNotifierTest, AutoConnectDueToLoginOnly) {
  109. NotifyConnectToNetworkRequested();
  110. NetworkHandler::Get()
  111. ->auto_connect_handler()
  112. ->NotifyAutoConnectInitiatedForTest(
  113. AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN);
  114. SuccessfullyJoinWifiNetwork();
  115. // Toast should not be displayed.
  116. VerifyAutoConnectToastVisibility(/*visible=*/false);
  117. }
  118. TEST_F(AutoConnectNotifierTest, NoConnectionBeforeTimerExpires) {
  119. NotifyConnectToNetworkRequested();
  120. NetworkHandler::Get()
  121. ->auto_connect_handler()
  122. ->NotifyAutoConnectInitiatedForTest(
  123. AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
  124. // No connection occurs.
  125. ASSERT_TRUE(mock_notification_timer_->IsRunning());
  126. mock_notification_timer_->Fire();
  127. // Connect after the timer fires; since the connection did not occur before
  128. // the timeout, no notification should be displayed.
  129. SuccessfullyJoinWifiNetwork();
  130. // Toast should not be displayed.
  131. VerifyAutoConnectToastVisibility(/*visible=*/false);
  132. }
  133. TEST_F(AutoConnectNotifierTest, ConnectToConnectedNetwork) {
  134. SuccessfullyJoinWifiNetwork();
  135. NotifyConnectToNetworkRequested();
  136. NetworkHandler::Get()
  137. ->auto_connect_handler()
  138. ->NotifyAutoConnectInitiatedForTest(
  139. AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
  140. SuccessfullyJoinWifiNetwork();
  141. // Toast should not be displayed.
  142. VerifyAutoConnectToastVisibility(/*visible=*/false);
  143. }
  144. TEST_F(AutoConnectNotifierTest, ToastDisplayed) {
  145. NotifyConnectToNetworkRequested();
  146. NetworkHandler::Get()
  147. ->auto_connect_handler()
  148. ->NotifyAutoConnectInitiatedForTest(
  149. AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
  150. SuccessfullyJoinWifiNetwork();
  151. VerifyAutoConnectToastVisibility(/*visible=*/true);
  152. }
  153. } // namespace ash