123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // Copyright 2018 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/system/network/auto_connect_notifier.h"
- #include <memory>
- #include "ash/shell.h"
- #include "ash/strings/grit/ash_strings.h"
- #include "ash/system/system_notification_controller.h"
- #include "ash/system/toast/toast_manager_impl.h"
- #include "ash/test/ash_test_base.h"
- #include "base/bind.h"
- #include "base/memory/ptr_util.h"
- #include "base/run_loop.h"
- #include "base/timer/mock_timer.h"
- #include "chromeos/ash/components/dbus/shill/shill_service_client.h"
- #include "chromeos/ash/components/network/auto_connect_handler.h"
- #include "chromeos/ash/components/network/network_cert_loader.h"
- #include "chromeos/ash/components/network/network_handler.h"
- #include "chromeos/ash/components/network/network_handler_test_helper.h"
- #include "chromeos/ash/components/network/system_token_cert_db_storage.h"
- #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
- #include "dbus/object_path.h"
- #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
- #include "ui/base/l10n/l10n_util.h"
- namespace ash {
- namespace {
- constexpr char kTestServicePath[] = "testServicePath";
- constexpr char kTestServiceGuid[] = "testServiceGuid";
- constexpr char kTestServiceName[] = "testServiceName";
- } // namespace
- class AutoConnectNotifierTest : public AshTestBase {
- public:
- AutoConnectNotifierTest(const AutoConnectNotifierTest&) = delete;
- AutoConnectNotifierTest& operator=(const AutoConnectNotifierTest&) = delete;
- protected:
- AutoConnectNotifierTest() = default;
- ~AutoConnectNotifierTest() override = default;
- void SetUp() override {
- SystemTokenCertDbStorage::Initialize();
- NetworkCertLoader::Initialize();
- NetworkCertLoader::ForceAvailableForNetworkAuthForTesting();
- network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
- CHECK(NetworkHandler::Get()->auto_connect_handler());
- network_config_helper_ = std::make_unique<
- chromeos::network_config::CrosNetworkConfigTestHelper>();
- AshTestBase::SetUp();
- toast_manager_ = Shell::Get()->toast_manager();
- mock_notification_timer_ = new base::MockOneShotTimer();
- Shell::Get()
- ->system_notification_controller()
- ->auto_connect_->set_timer_for_testing(
- base::WrapUnique(mock_notification_timer_));
- ShillServiceClient::Get()->GetTestInterface()->AddService(
- kTestServicePath, kTestServiceGuid, kTestServiceName, shill::kTypeWifi,
- shill::kStateIdle, true /* visible*/);
- // Ensure fake DBus service initialization completes.
- base::RunLoop().RunUntilIdle();
- }
- void TearDown() override {
- AshTestBase::TearDown();
- network_config_helper_.reset();
- network_handler_test_helper_.reset();
- NetworkCertLoader::Shutdown();
- SystemTokenCertDbStorage::Shutdown();
- }
- void NotifyConnectToNetworkRequested() {
- Shell::Get()
- ->system_notification_controller()
- ->auto_connect_->ConnectToNetworkRequested(kTestServicePath);
- base::RunLoop().RunUntilIdle();
- }
- void SuccessfullyJoinWifiNetwork() {
- ShillServiceClient::Get()->Connect(dbus::ObjectPath(kTestServicePath),
- base::BindOnce([]() {}),
- ShillServiceClient::ErrorCallback());
- base::RunLoop().RunUntilIdle();
- }
- ToastOverlay* GetCurrentOverlay() {
- return toast_manager_->GetCurrentOverlayForTesting();
- }
- void VerifyAutoConnectToastVisibility(bool visible) {
- if (visible) {
- ToastOverlay* overlay = GetCurrentOverlay();
- ASSERT_NE(nullptr, overlay);
- EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_NETWORK_AUTOCONNECT),
- overlay->GetText());
- } else {
- EXPECT_EQ(nullptr, GetCurrentOverlay());
- }
- }
- // Ownership passed to Shell owned AutoConnectNotifier instance.
- base::MockOneShotTimer* mock_notification_timer_;
- private:
- std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
- std::unique_ptr<chromeos::network_config::CrosNetworkConfigTestHelper>
- network_config_helper_;
- ToastManagerImpl* toast_manager_ = nullptr;
- };
- TEST_F(AutoConnectNotifierTest, NoExplicitConnectionRequested) {
- NetworkHandler::Get()
- ->auto_connect_handler()
- ->NotifyAutoConnectInitiatedForTest(
- AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
- SuccessfullyJoinWifiNetwork();
- // Toast should not be displayed.
- VerifyAutoConnectToastVisibility(/*visible=*/false);
- }
- TEST_F(AutoConnectNotifierTest, AutoConnectDueToLoginOnly) {
- NotifyConnectToNetworkRequested();
- NetworkHandler::Get()
- ->auto_connect_handler()
- ->NotifyAutoConnectInitiatedForTest(
- AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN);
- SuccessfullyJoinWifiNetwork();
- // Toast should not be displayed.
- VerifyAutoConnectToastVisibility(/*visible=*/false);
- }
- TEST_F(AutoConnectNotifierTest, NoConnectionBeforeTimerExpires) {
- NotifyConnectToNetworkRequested();
- NetworkHandler::Get()
- ->auto_connect_handler()
- ->NotifyAutoConnectInitiatedForTest(
- AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
- // No connection occurs.
- ASSERT_TRUE(mock_notification_timer_->IsRunning());
- mock_notification_timer_->Fire();
- // Connect after the timer fires; since the connection did not occur before
- // the timeout, no notification should be displayed.
- SuccessfullyJoinWifiNetwork();
- // Toast should not be displayed.
- VerifyAutoConnectToastVisibility(/*visible=*/false);
- }
- TEST_F(AutoConnectNotifierTest, ConnectToConnectedNetwork) {
- SuccessfullyJoinWifiNetwork();
- NotifyConnectToNetworkRequested();
- NetworkHandler::Get()
- ->auto_connect_handler()
- ->NotifyAutoConnectInitiatedForTest(
- AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
- SuccessfullyJoinWifiNetwork();
- // Toast should not be displayed.
- VerifyAutoConnectToastVisibility(/*visible=*/false);
- }
- TEST_F(AutoConnectNotifierTest, ToastDisplayed) {
- NotifyConnectToNetworkRequested();
- NetworkHandler::Get()
- ->auto_connect_handler()
- ->NotifyAutoConnectInitiatedForTest(
- AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED);
- SuccessfullyJoinWifiNetwork();
- VerifyAutoConnectToastVisibility(/*visible=*/true);
- }
- } // namespace ash
|