File: bluetooth_notification_controller_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (218 lines) | stat: -rw-r--r-- 7,941 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 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/bluetooth/bluetooth_notification_controller.h"

#include <memory>
#include <utility>

#include "ash/public/cpp/test/test_nearby_share_delegate.h"
#include "ash/public/cpp/test/test_system_tray_client.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/toast/toast_manager_impl.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/test/ash_test_base.h"
#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/fake_message_center.h"

using testing::NiceMock;
using testing::Return;

namespace ash {
namespace {

const char kTestAdapterName[] = "Chromebook";
const char16_t kTestAdapterName16[] = u"Chromebook";

class TestMessageCenter : public message_center::FakeMessageCenter {
 public:
  TestMessageCenter() = default;

  TestMessageCenter(const TestMessageCenter&) = delete;
  TestMessageCenter& operator=(const TestMessageCenter&) = delete;

  ~TestMessageCenter() override = default;

  void ClickOnNotification(const std::string& id) override {
    message_center::Notification* notification =
        FindVisibleNotificationById(id);
    DCHECK(notification);
    notification->delegate()->Click(std::nullopt, std::nullopt);
  }
};

}  // namespace

class BluetoothNotificationControllerTest : public AshTestBase {
 public:
  BluetoothNotificationControllerTest() = default;

  BluetoothNotificationControllerTest(
      const BluetoothNotificationControllerTest&) = delete;
  BluetoothNotificationControllerTest& operator=(
      const BluetoothNotificationControllerTest&) = delete;

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

    mock_adapter_ =
        base::MakeRefCounted<NiceMock<device::MockBluetoothAdapter>>();
    ON_CALL(*mock_adapter_, IsPresent()).WillByDefault(Return(true));
    ON_CALL(*mock_adapter_, IsPowered()).WillByDefault(Return(true));
    ON_CALL(*mock_adapter_, GetName()).WillByDefault(Return(kTestAdapterName));
    device::BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter_);

    notification_controller_ =
        std::make_unique<BluetoothNotificationController>(
            &test_message_center_);
    system_tray_client_ = GetSystemTrayClient();

    bluetooth_device_1_ =
        std::make_unique<NiceMock<device::MockBluetoothDevice>>(
            mock_adapter_.get(), 0 /* bluetooth_class */, "name_1", "address_1",
            false /* paired */, false /* connected */);
    bluetooth_device_2_ =
        std::make_unique<NiceMock<device::MockBluetoothDevice>>(
            mock_adapter_.get(), 0 /* bluetooth_class */, "name_2", "address_2",
            false /* paired */, false /* connected */);

    toast_manager_ = Shell::Get()->toast_manager();
  }

  void VerifyDiscoverableToastVisibility(bool visible) {
    if (visible) {
      ToastOverlay* overlay = GetCurrentOverlay();
      ASSERT_NE(nullptr, overlay);
      EXPECT_EQ(
          l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_BLUETOOTH_DISCOVERABLE,
                                     kTestAdapterName16),
          overlay->GetText());
    } else {
      EXPECT_EQ(nullptr, GetCurrentOverlay());
    }
  }

  void VerifyPairingNotificationVisibility(bool visible) {
    EXPECT_EQ(test_message_center_.FindVisibleNotificationById(
                  BluetoothNotificationController::
                      kBluetoothDevicePairingNotificationId) != nullptr,
              visible);
  }

  // Run the notification controller to simulate showing a toast.
  void ShowDiscoverableToast(
      BluetoothNotificationController* notification_controller) {
    notification_controller->NotifyAdapterDiscoverable();
  }

  void ShowPairingNotification(
      BluetoothNotificationController* notification_controller,
      device::MockBluetoothDevice* mock_device) {
    notification_controller->AuthorizePairing(mock_device);
  }

  void SimulateDevicePaired(
      BluetoothNotificationController* notification_controller,
      device::MockBluetoothDevice* mock_device) {
    ON_CALL(*mock_device, IsPaired()).WillByDefault(Return(true));
    notification_controller->DeviceChanged(mock_adapter_.get(), mock_device);
  }

  void SimulateDeviceBonded(
      BluetoothNotificationController* notification_controller,
      device::MockBluetoothDevice* mock_device) {
    ON_CALL(*mock_device, IsBonded()).WillByDefault(Return(true));
    notification_controller->DeviceChanged(mock_adapter_.get(), mock_device);
  }

  ToastOverlay* GetCurrentOverlay() {
    return toast_manager_->GetCurrentOverlayForTesting();
  }

  TestMessageCenter test_message_center_;
  scoped_refptr<device::MockBluetoothAdapter> mock_adapter_;
  std::unique_ptr<BluetoothNotificationController> notification_controller_;
  raw_ptr<TestSystemTrayClient, DanglingUntriaged> system_tray_client_;
  std::unique_ptr<device::MockBluetoothDevice> bluetooth_device_1_;
  std::unique_ptr<device::MockBluetoothDevice> bluetooth_device_2_;
  raw_ptr<ToastManagerImpl, DanglingUntriaged> toast_manager_ = nullptr;
};

TEST_F(BluetoothNotificationControllerTest, DiscoverableToast) {
  VerifyDiscoverableToastVisibility(/*visible=*/false);

  GetSessionControllerClient()->SetSessionState(
      session_manager::SessionState::LOCKED);

  ShowDiscoverableToast(notification_controller_.get());

  VerifyDiscoverableToastVisibility(/*visible=*/false);

  GetSessionControllerClient()->SetSessionState(
      session_manager::SessionState::ACTIVE);

  ShowDiscoverableToast(notification_controller_.get());

  VerifyDiscoverableToastVisibility(/*visible=*/true);
}

TEST_F(BluetoothNotificationControllerTest,
       DiscoverableToast_NearbyShareEnableHighVisibilityRequestActive) {
  VerifyDiscoverableToastVisibility(/*visible=*/false);

  auto* nearby_share_delegate_ = static_cast<TestNearbyShareDelegate*>(
      Shell::Get()->nearby_share_delegate());
  nearby_share_delegate_->set_is_enable_high_visibility_request_active(true);

  ShowDiscoverableToast(notification_controller_.get());

  VerifyDiscoverableToastVisibility(/*visible=*/false);
}

TEST_F(BluetoothNotificationControllerTest,
       DiscoverableToast_NearbyShareHighVisibilityOn) {
  VerifyDiscoverableToastVisibility(/*visible=*/false);

  auto* nearby_share_delegate_ = static_cast<TestNearbyShareDelegate*>(
      Shell::Get()->nearby_share_delegate());
  nearby_share_delegate_->set_is_high_visibility_on(true);

  ShowDiscoverableToast(notification_controller_.get());

  VerifyDiscoverableToastVisibility(/*visible=*/false);
}

TEST_F(BluetoothNotificationControllerTest, PairingNotification) {
  VerifyPairingNotificationVisibility(/*visible=*/false);

  ShowPairingNotification(notification_controller_.get(),
                          bluetooth_device_1_.get());
  VerifyPairingNotificationVisibility(/*visible=*/true);

  // Simulate the device being paired. This should not remove the pairing
  // notification.
  SimulateDevicePaired(notification_controller_.get(),
                       bluetooth_device_1_.get());
  VerifyPairingNotificationVisibility(/*visible=*/true);

  // Simulate the device being bonded. This should remove the pairing
  // notification.
  SimulateDeviceBonded(notification_controller_.get(),
                       bluetooth_device_1_.get());
  VerifyPairingNotificationVisibility(/*visible=*/false);
}

}  // namespace ash