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

ash / system / hotspot / hotspot_notifier_unittest.cc [blame]

// Copyright 2023 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/hotspot/hotspot_notifier.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "base/test/bind.h"
#include "chromeos/ash/components/network/hotspot_enabled_state_notifier.h"
#include "chromeos/ash/components/network/hotspot_state_handler.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/services/hotspot_config/cros_hotspot_config.h"
#include "chromeos/ash/services/hotspot_config/public/cpp/cros_hotspot_config_test_helper.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"

namespace ash {

namespace {

const char kCellularServicePath[] = "/service/cellular0";
const char kCellularServiceGuid[] = "cellular_guid0";
const char kCellularServiceName[] = "cellular_name0";
const char kHotspotConfigSSID[] = "hotspot_SSID";
const char kHotspotConfigPassphrase[] = "hotspot_passphrase";

hotspot_config::mojom::HotspotConfigPtr GenerateTestConfig() {
  auto mojom_config = hotspot_config::mojom::HotspotConfig::New();
  mojom_config->auto_disable = false;
  mojom_config->band = hotspot_config::mojom::WiFiBand::kAutoChoose;
  mojom_config->security = hotspot_config::mojom::WiFiSecurityMode::kWpa2;
  mojom_config->ssid = kHotspotConfigSSID;
  mojom_config->passphrase = kHotspotConfigPassphrase;
  return mojom_config;
}

}  // namespace

class HotspotNotifierTest : public NoSessionAshTestBase {
 public:
  HotspotNotifierTest() = default;
  HotspotNotifierTest(const HotspotNotifierTest&) = delete;
  HotspotNotifierTest& operator=(const HotspotNotifierTest&) = delete;
  ~HotspotNotifierTest() override = default;

  void SetUp() override {
    network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
    network_handler_test_helper_->AddDefaultProfiles();
    network_handler_test_helper_->ResetDevicesAndServices();

    cros_network_config_test_helper_ =
        std::make_unique<network_config::CrosNetworkConfigTestHelper>();

    NoSessionAshTestBase::SetUp();
    LogIn();
  }

  void LogIn() { SimulateUserLogin("user1@test.com"); }

  void TearDown() override {
    NoSessionAshTestBase::TearDown();
    cros_network_config_test_helper_.reset();
    network_handler_test_helper_.reset();
  }

  NetworkHandlerTestHelper* helper() {
    return network_handler_test_helper_.get();
  }

  void EnableHotspot() {
    ash_test_helper()->cros_hotspot_config_test_helper()->EnableHotspot();
    base::RunLoop().RunUntilIdle();
  }

  void SetHotspotConfig(hotspot_config::mojom::HotspotConfigPtr mojom_config) {
    ash_test_helper()->cros_hotspot_config_test_helper()->SetHotspotConfig(
        std::move(mojom_config));
    base::RunLoop().RunUntilIdle();
  }

  void NotifyHotspotTurnedOff(
      hotspot_config::mojom::DisableReason disable_reason) {
    NetworkHandler* network_handler = NetworkHandler::Get();
    network_handler->hotspot_enabled_state_notifier()->OnHotspotTurnedOff(
        disable_reason);
  }

  void AddActiveCellularService() {
    network_handler_test_helper_->service_test()->AddService(
        kCellularServicePath, kCellularServiceGuid, kCellularServiceName,
        shill::kTypeCellular, shill::kStateOnline, /*visible=*/true);
    base::RunLoop().RunUntilIdle();
  }

  void SetHotspotStateInShill(const std::string& state) {
    base::Value::Dict status_dict;
    status_dict.Set(shill::kTetheringStatusStateProperty, state);
    network_handler_test_helper_->manager_test()->SetManagerProperty(
        shill::kTetheringStatusProperty, base::Value(std::move(status_dict)));
    base::RunLoop().RunUntilIdle();
  }

  void UpdateHotspotInfo(hotspot_config::mojom::HotspotState state,
                         hotspot_config::mojom::HotspotAllowStatus allow_status,
                         uint32_t client_count = 0) {
    auto hotspot_info = hotspot_config::mojom::HotspotInfo::New();
    hotspot_info->state = state;
    hotspot_info->allow_status = allow_status;
    hotspot_info->client_count = client_count;
    ash_test_helper()->cros_hotspot_config_test_helper()->SetFakeHotspotInfo(
        std::move(hotspot_info));
    base::RunLoop().RunUntilIdle();
  }

  std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
  std::unique_ptr<network_config::CrosNetworkConfigTestHelper>
      cros_network_config_test_helper_;
  std::unique_ptr<HotspotNotifier> hotspot_notifier_;
};

TEST_F(HotspotNotifierTest, AdminRestricted) {
  EnableHotspot();
  NotifyHotspotTurnedOff(
      hotspot_config::mojom::DisableReason::kProhibitedByPolicy);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kAdminRestrictedNotificationId));

  UpdateHotspotInfo(hotspot_config::mojom::HotspotState::kEnabling,
                    hotspot_config::mojom::HotspotAllowStatus::kAllowed);
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kInternalErrorNotificationId));
}

TEST_F(HotspotNotifierTest, WiFiTurnedOn) {
  EnableHotspot();
  NotifyHotspotTurnedOff(hotspot_config::mojom::DisableReason::kWifiEnabled);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kWiFiTurnedOnNotificationId));

  UpdateHotspotInfo(hotspot_config::mojom::HotspotState::kEnabling,
                    hotspot_config::mojom::HotspotAllowStatus::kAllowed);
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kWiFiTurnedOnNotificationId));
}

TEST_F(HotspotNotifierTest, AutoDisabled) {
  EnableHotspot();
  NotifyHotspotTurnedOff(hotspot_config::mojom::DisableReason::kAutoDisabled);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kAutoDisabledNotificationId));

  UpdateHotspotInfo(hotspot_config::mojom::HotspotState::kEnabling,
                    hotspot_config::mojom::HotspotAllowStatus::kAllowed);
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kAutoDisabledNotificationId));

  message_center::MessageCenter::Get()->ClickOnNotificationButton(
      HotspotNotifier::kAutoDisabledNotificationId, 0);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kAutoDisabledNotificationId));
}

TEST_F(HotspotNotifierTest, InternalError) {
  EnableHotspot();
  NotifyHotspotTurnedOff(hotspot_config::mojom::DisableReason::kInternalError);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kInternalErrorNotificationId));

  UpdateHotspotInfo(hotspot_config::mojom::HotspotState::kEnabling,
                    hotspot_config::mojom::HotspotAllowStatus::kAllowed);
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kInternalErrorNotificationId));
}

TEST_F(HotspotNotifierTest, DialogDismissWhenNotAllowed) {
  UpdateHotspotInfo(hotspot_config::mojom::HotspotState::kDisabled,
                    hotspot_config::mojom::HotspotAllowStatus::kAllowed);
  NotifyHotspotTurnedOff(hotspot_config::mojom::DisableReason::kInternalError);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kInternalErrorNotificationId));

  UpdateHotspotInfo(
      hotspot_config::mojom::HotspotState::kDisabled,
      hotspot_config::mojom::HotspotAllowStatus::kDisallowedNoMobileData);
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kInternalErrorNotificationId));
}

TEST_F(HotspotNotifierTest, HotspotTurnedOn) {
  SetHotspotConfig(GenerateTestConfig());
  EnableHotspot();
  SetHotspotStateInShill(shill::kTetheringStateActive);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(message_center::MessageCenter::Get()->FindVisibleNotificationById(
      HotspotNotifier::kHotspotTurnedOnNotificationId));

  message_center::MessageCenter::Get()->ClickOnNotificationButton(
      HotspotNotifier::kHotspotTurnedOnNotificationId, 0);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          HotspotNotifier::kHotspotTurnedOnNotificationId));
}

}  // namespace ash