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

ash / system / hotspot / hotspot_detailed_view_controller_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_detailed_view_controller.h"

#include "ash/style/switch.h"
#include "ash/system/hotspot/hotspot_detailed_view.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "base/run_loop.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 "ui/views/view.h"

namespace ash {

using hotspot_config::mojom::HotspotAllowStatus;
using hotspot_config::mojom::HotspotConfig;
using hotspot_config::mojom::HotspotConfigPtr;
using hotspot_config::mojom::HotspotInfo;
using hotspot_config::mojom::HotspotState;

class HotspotDetailedViewControllerTest : public AshTestBase {
 public:
  HotspotDetailedViewControllerTest() = default;
  ~HotspotDetailedViewControllerTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();

    GetPrimaryUnifiedSystemTray()->ShowBubble();

    UnifiedSystemTrayController* unified_system_tray_controller =
        GetPrimaryUnifiedSystemTray()
            ->bubble()
            ->unified_system_tray_controller();
    unified_system_tray_controller->ShowHotspotDetailedView();
    hotspot_detailed_view_controller_ =
        static_cast<HotspotDetailedViewController*>(
            unified_system_tray_controller->detailed_view_controller());

    // Spin the runloop to have hotspot_detailed_view_controller_ observe the
    // hotspot info change.
    base::RunLoop().RunUntilIdle();
  }

  void UpdateHotspotInfo(HotspotState state,
                         HotspotAllowStatus allow_status,
                         int client_count = 0,
                         HotspotConfigPtr config = {}) {
    auto hotspot_info = HotspotInfo::New();
    hotspot_info->state = state;
    hotspot_info->allow_status = allow_status;
    hotspot_info->client_count = client_count;
    hotspot_info->config = std::move(config);
    ash_test_helper()->cros_hotspot_config_test_helper()->SetFakeHotspotInfo(
        std::move(hotspot_info));
    // Spin the runloop to observe the hotspot info change.
    base::RunLoop().RunUntilIdle();
  }

  void EnableAndDisableHotspotOnce() {
    UpdateHotspotInfo(HotspotState::kEnabled, HotspotAllowStatus::kAllowed);
    UpdateHotspotInfo(HotspotState::kDisabled, HotspotAllowStatus::kAllowed);
  }

  HotspotDetailedView::Delegate* hotspot_detailed_view_delegate() {
    return hotspot_detailed_view_controller_;
  }

  HotspotDetailedView* hotspot_detailed_view() {
    return hotspot_detailed_view_controller_->view_;
  }

  Switch* GetToggleButton() { return hotspot_detailed_view()->toggle_; }

  void AssertSubtextLabel(const std::u16string& expected_text) {
    HoverHighlightView* entry_row = hotspot_detailed_view()->entry_row_;
    if (expected_text.empty()) {
      EXPECT_FALSE(entry_row->sub_text_label());
      return;
    }
    ASSERT_TRUE(entry_row->sub_text_label());
    EXPECT_TRUE(entry_row->sub_text_label()->GetVisible());
    EXPECT_EQ(expected_text, entry_row->sub_text_label()->GetText());
  }

 protected:
  raw_ptr<HotspotDetailedViewController, DanglingUntriaged>
      hotspot_detailed_view_controller_;
};

TEST_F(HotspotDetailedViewControllerTest, ToggleClicked) {
  ASSERT_TRUE(hotspot_detailed_view_controller_);
  UpdateHotspotInfo(HotspotState::kDisabled, HotspotAllowStatus::kAllowed);

  ASSERT_TRUE(hotspot_detailed_view());
  Switch* toggle = GetToggleButton();
  ASSERT_TRUE(toggle);
  EXPECT_FALSE(toggle->GetIsOn());

  hotspot_detailed_view_delegate()->OnToggleClicked(/*new_state=*/true);
  // Spin the runloop to have cros_hotspot_config finish enable hotspot and
  // notify the hotspot_info change.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(toggle->GetIsOn());
  hotspot_detailed_view_delegate()->OnToggleClicked(/*new_state=*/false);

  // Spin the runloop to have cros_hotspot_config finish disable hotspot and
  // notify the hotspot_info change.
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(toggle->GetIsOn());
}

TEST_F(HotspotDetailedViewControllerTest, NotifiesWhenHotspotInfoChanges) {
  ASSERT_TRUE(hotspot_detailed_view_controller_);
  UpdateHotspotInfo(HotspotState::kDisabled, HotspotAllowStatus::kAllowed);

  ASSERT_TRUE(hotspot_detailed_view());
  Switch* toggle = GetToggleButton();
  ASSERT_TRUE(toggle);
  EXPECT_FALSE(toggle->GetIsOn());

  UpdateHotspotInfo(HotspotState::kEnabled, HotspotAllowStatus::kAllowed);
  EXPECT_TRUE(toggle->GetIsOn());
  AssertSubtextLabel(u"No devices connected");

  UpdateHotspotInfo(HotspotState::kEnabled, HotspotAllowStatus::kAllowed,
                    /*client_count=*/1);
  EXPECT_TRUE(toggle->GetIsOn());
  AssertSubtextLabel(u"1 device connected");

  // Updating the hotspot configuration should not update hotspot detailed
  // view.
  auto config = HotspotConfig::New();
  config->ssid = "hotspot_ssid";
  config->passphrase = "hotspot_passphrase";
  UpdateHotspotInfo(HotspotState::kEnabled, HotspotAllowStatus::kAllowed,
                    /*client_count=*/1, std::move(config));
  EXPECT_TRUE(toggle->GetIsOn());
  AssertSubtextLabel(u"1 device connected");
}

}  // namespace ash