[go: nahoru, domu]

blob: 6b3a7c6bf5bc0b2abf33f268328b2a42f35070fb [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2021 The Chromium Authors
Jimmy Gonge1bd8082021-02-23 21:40:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/system/pcie_peripheral/pcie_peripheral_notification_controller.h"
6
7#include <memory>
Arthur Sonzognia98e4432023-11-28 18:15:018#include <optional>
Jimmy Gonge1bd8082021-02-23 21:40:579#include <vector>
10
Henrique Ferreirof9d1cb22021-07-13 01:32:4711#include "ash/constants/ash_pref_names.h"
Jimmy Gonge1bd8082021-02-23 21:40:5712#include "ash/public/cpp/test/test_new_window_delegate.h"
13#include "ash/public/cpp/test/test_system_tray_client.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0014#include "ash/session/session_controller_impl.h"
Jimmy Gonge1bd8082021-02-23 21:40:5715#include "ash/shell.h"
16#include "ash/test/ash_test_base.h"
Arthur Sonzogni834e018f2023-04-22 10:20:0217#include "base/memory/raw_ptr.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0018#include "components/prefs/pref_service.h"
Jimmy Gonge1bd8082021-02-23 21:40:5719#include "testing/gmock/include/gmock/gmock.h"
20#include "ui/message_center/fake_message_center.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0021#include "ui/message_center/message_center.h"
Jimmy Gonge1bd8082021-02-23 21:40:5722#include "ui/message_center/public/cpp/notification.h"
23#include "ui/message_center/public/cpp/notification_types.h"
24
25using message_center::MessageCenter;
26
27namespace ash {
28
29namespace {
30const char kPciePeripheralLimitedPerformanceNotificationId[] =
31 "cros_pcie_peripheral_limited_performance_notification_id";
32const char kPciePeripheralLimitedPerformanceGuestModeNotificationId[] =
33 "cros_pcie_peripheral_limited_performance_guest_mode_notification_id";
34const char kPciePeripheralGuestModeNotSupportedNotificationId[] =
35 "cros_pcie_peripheral_guest_mode_not_supported_notifcation_id";
Jimmy Gong4041bd132021-03-10 00:00:0336const char kPciePeripheralDeviceBlockedNotificationId[] =
37 "cros_pcie_peripheral_device_blocked_notifcation_id";
Anton Swiftona1cda032021-08-03 22:42:3338const char kPciePeripheralBillboardDeviceNotificationId[] =
39 "cros_pcie_peripheral_billboard_device_notifcation_id";
Jimmy Gonge1bd8082021-02-23 21:40:5740const char kLearnMoreHelpUrl[] =
41 "https://www.support.google.com/chromebook?p=connect_thblt_usb4_accy";
42
43// A mock implementation of |NewWindowDelegate| for use in tests.
44class MockNewWindowDelegate : public testing::NiceMock<TestNewWindowDelegate> {
45 public:
46 // TestNewWindowDelegate:
Georg Neisb386a3422022-08-17 00:33:4747 MOCK_METHOD(void,
48 OpenUrl,
49 (const GURL& url, OpenUrlFrom from, Disposition disposition),
50 (override));
Jimmy Gonge1bd8082021-02-23 21:40:5751};
52
53} // namespace
54
55class PciePeripheralNotificationControllerTest : public AshTestBase {
56 public:
Hidehiko Abe6e3e57f42021-03-23 11:23:5857 PciePeripheralNotificationControllerTest() {
Georg Neised4e58d2022-02-08 17:47:2658 auto instance = std::make_unique<MockNewWindowDelegate>();
59 auto primary = std::make_unique<MockNewWindowDelegate>();
60 new_window_delegate_primary_ = primary.get();
61 delegate_provider_ = std::make_unique<TestNewWindowDelegateProvider>(
62 std::move(instance), std::move(primary));
Hidehiko Abe6e3e57f42021-03-23 11:23:5863 }
Jimmy Gonge1bd8082021-02-23 21:40:5764 PciePeripheralNotificationControllerTest(
65 const PciePeripheralNotificationControllerTest&) = delete;
66 PciePeripheralNotificationControllerTest& operator=(
67 const PciePeripheralNotificationControllerTest&) = delete;
68 ~PciePeripheralNotificationControllerTest() override = default;
69
70 PciePeripheralNotificationController* controller() {
71 return Shell::Get()->pcie_peripheral_notification_controller();
72 }
73
Georg Neised4e58d2022-02-08 17:47:2674 MockNewWindowDelegate& new_window_delegate_primary() {
75 return *new_window_delegate_primary_;
76 }
Jimmy Gonge1bd8082021-02-23 21:40:5777
78 message_center::Notification* GetLimitedPerformanceNotification() {
79 return MessageCenter::Get()->FindVisibleNotificationById(
80 kPciePeripheralLimitedPerformanceNotificationId);
81 }
82
83 message_center::Notification* GetLimitedPerformanceGuestModeNotification() {
84 return MessageCenter::Get()->FindVisibleNotificationById(
85 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
86 }
87
88 message_center::Notification* GetGuestModeNotSupportedNotification() {
89 return MessageCenter::Get()->FindVisibleNotificationById(
90 kPciePeripheralGuestModeNotSupportedNotificationId);
91 }
92
Jimmy Gong4041bd132021-03-10 00:00:0393 message_center::Notification* GetPeripheralBlockedNotification() {
94 return MessageCenter::Get()->FindVisibleNotificationById(
95 kPciePeripheralDeviceBlockedNotificationId);
96 }
97
Anton Swiftona1cda032021-08-03 22:42:3398 message_center::Notification* GetBillboardDeviceNotification() {
99 return MessageCenter::Get()->FindVisibleNotificationById(
100 kPciePeripheralBillboardDeviceNotificationId);
101 }
102
Jimmy Gonge1bd8082021-02-23 21:40:57103 int GetNumOsPrivacySettingsOpened() {
104 return GetSystemTrayClient()->show_os_settings_privacy_and_security_count();
105 }
106
Jimmy Gongc6f4fd02021-02-25 01:55:00107 int GetPrefNotificationCount() {
108 PrefService* prefs =
109 Shell::Get()->session_controller()->GetActivePrefService();
110 return prefs->GetInteger(
111 prefs::kPciePeripheralDisplayNotificationRemaining);
112 }
113
Arthur Sonzognia98e4432023-11-28 18:15:01114 void ClickLimitedNotificationButton(std::optional<int> button_index) {
Jimmy Gonge1bd8082021-02-23 21:40:57115 // No button index means the notification body was clicked.
116 if (!button_index.has_value()) {
117 message_center::Notification* notification =
118 MessageCenter::Get()->FindVisibleNotificationById(
119 kPciePeripheralLimitedPerformanceNotificationId);
Arthur Sonzognia98e4432023-11-28 18:15:01120 notification->delegate()->Click(std::nullopt, std::nullopt);
Jimmy Gonge1bd8082021-02-23 21:40:57121 return;
122 }
123
124 message_center::Notification* notification =
125 MessageCenter::Get()->FindVisibleNotificationById(
126 kPciePeripheralLimitedPerformanceNotificationId);
Arthur Sonzognia98e4432023-11-28 18:15:01127 notification->delegate()->Click(button_index, std::nullopt);
Jimmy Gonge1bd8082021-02-23 21:40:57128 }
129
130 void ClickGuestNotification(bool is_thunderbolt_only) {
131 if (is_thunderbolt_only) {
132 MessageCenter::Get()->ClickOnNotification(
133 kPciePeripheralGuestModeNotSupportedNotificationId);
134 return;
135 }
136
137 MessageCenter::Get()->ClickOnNotification(
138 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
139 }
140
Jimmy Gongc6f4fd02021-02-25 01:55:00141 void RemoveAllNotifications() {
142 MessageCenter::Get()->RemoveAllNotifications(
143 /*by_user=*/false, message_center::MessageCenter::RemoveType::ALL);
144 }
145
Jimmy Gonge1bd8082021-02-23 21:40:57146 private:
Bartek Nowierskideb75842023-12-27 02:32:42147 raw_ptr<MockNewWindowDelegate, DanglingUntriaged>
Arthur Sonzogni34efeb82023-08-17 12:55:11148 new_window_delegate_primary_;
Hidehiko Abe6e3e57f42021-03-23 11:23:58149 std::unique_ptr<TestNewWindowDelegateProvider> delegate_provider_;
Jimmy Gonge1bd8082021-02-23 21:40:57150};
151
Jimmy Gonge1bd8082021-02-23 21:40:57152TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtOnly) {
153 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
154
155 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
Jimmy Gonge1bd8082021-02-23 21:40:57156 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
157
158 message_center::Notification* notification =
159 GetGuestModeNotSupportedNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00160 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57161
162 // This notification has no buttons.
163 EXPECT_EQ(0u, notification->buttons().size());
164
165 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
166 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
167
Anton Swiftona1cda032021-08-03 22:42:33168 // Click on the notification and expect the Learn More page to appear.
Hidehiko Abe4eb522b2022-02-16 11:18:58169 EXPECT_CALL(new_window_delegate_primary(),
170 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47171 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
172 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gonge1bd8082021-02-23 21:40:57173 ClickGuestNotification(/*is_thunderbolt_only=*/true);
174 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
175}
176
177TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtAltMode) {
178 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
179
180 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
181 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
182
183 message_center::Notification* notification =
184 GetLimitedPerformanceGuestModeNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00185 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57186
187 // This notification has no buttons.
188 EXPECT_EQ(0u, notification->buttons().size());
189
190 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
191 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
192
Anton Swiftona1cda032021-08-03 22:42:33193 // Click on the notification and expect the Learn More page to appear.
Hidehiko Abe4eb522b2022-02-16 11:18:58194 EXPECT_CALL(new_window_delegate_primary(),
195 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47196 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
197 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gonge1bd8082021-02-23 21:40:57198 ClickGuestNotification(/*is_thunderbolt_only=*/false);
199 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
200}
201
Jimmy Gongc6f4fd02021-02-25 01:55:00202TEST_F(PciePeripheralNotificationControllerTest,
Jimmy Gong4041bd132021-03-10 00:00:03203 PeripheralBlockedNotification) {
204 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
205
206 controller()->NotifyPeripheralBlockedNotification();
207 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
208
209 message_center::Notification* notification =
210 GetPeripheralBlockedNotification();
211 ASSERT_TRUE(notification);
212
213 // This notification has no buttons.
214 EXPECT_EQ(0u, notification->buttons().size());
215
Anton Swiftona1cda032021-08-03 22:42:33216 // Click on the notification and expect the Learn More page to appear.
Hidehiko Abe4eb522b2022-02-16 11:18:58217 EXPECT_CALL(new_window_delegate_primary(),
218 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47219 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
220 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gong4041bd132021-03-10 00:00:03221 MessageCenter::Get()->ClickOnNotification(
222 kPciePeripheralDeviceBlockedNotificationId);
223 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
224}
225
Anton Swiftona1cda032021-08-03 22:42:33226TEST_F(PciePeripheralNotificationControllerTest, BillboardDeviceNotification) {
227 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
228 EXPECT_EQ(3, GetPrefNotificationCount());
229
230 controller()->NotifyBillboardDevice();
231 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
232
233 message_center::Notification* notification = GetBillboardDeviceNotification();
234 ASSERT_TRUE(notification);
235
236 // This notification has no buttons.
237 EXPECT_EQ(0u, notification->buttons().size());
238
239 controller()->NotifyBillboardDevice();
240 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
241
242 // Click on the notification and expect the Learn More page to appear.
Hidehiko Abe4eb522b2022-02-16 11:18:58243 EXPECT_CALL(new_window_delegate_primary(),
244 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47245 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
246 NewWindowDelegate::Disposition::kNewForegroundTab));
Anton Swiftona1cda032021-08-03 22:42:33247 MessageCenter::Get()->ClickOnNotification(
248 kPciePeripheralBillboardDeviceNotificationId);
249 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
250 EXPECT_EQ(3, GetPrefNotificationCount());
251}
252
Jimmy Gong4041bd132021-03-10 00:00:03253TEST_F(PciePeripheralNotificationControllerTest,
Jimmy Gongc6f4fd02021-02-25 01:55:00254 LimitedPerformanceNotificationLearnMoreClick) {
255 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
256 EXPECT_EQ(3, GetPrefNotificationCount());
257
258 controller()->NotifyLimitedPerformance();
259 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
260
261 message_center::Notification* notification =
262 GetLimitedPerformanceNotification();
263 ASSERT_TRUE(notification);
264
265 // Ensure this notification has the two correct buttons.
266 EXPECT_EQ(2u, notification->buttons().size());
267
Hidehiko Abe4eb522b2022-02-16 11:18:58268 EXPECT_CALL(new_window_delegate_primary(),
269 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47270 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
271 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gongc6f4fd02021-02-25 01:55:00272 // Click the learn more link.
273 ClickLimitedNotificationButton(/*button_index=*/1);
274 EXPECT_EQ(2, GetPrefNotificationCount());
275 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
276
Hidehiko Abe4eb522b2022-02-16 11:18:58277 EXPECT_CALL(new_window_delegate_primary(),
278 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47279 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
280 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gongc6f4fd02021-02-25 01:55:00281 controller()->NotifyLimitedPerformance();
282 ClickLimitedNotificationButton(/*button_index=*/1);
283 EXPECT_EQ(1, GetPrefNotificationCount());
284 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
285
Hidehiko Abe4eb522b2022-02-16 11:18:58286 EXPECT_CALL(new_window_delegate_primary(),
287 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47288 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
289 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gongc6f4fd02021-02-25 01:55:00290 controller()->NotifyLimitedPerformance();
291 ClickLimitedNotificationButton(/*button_index=*/1);
292 EXPECT_EQ(0, GetPrefNotificationCount());
293 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
294
295 controller()->NotifyLimitedPerformance();
296 // Pref is currently at 0, so no new notifications should appear.
297 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
298}
299
300TEST_F(PciePeripheralNotificationControllerTest,
301 LimitedPerformanceNotificationBodyClick) {
302 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
303 EXPECT_EQ(3, GetPrefNotificationCount());
304
305 controller()->NotifyLimitedPerformance();
306 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
307 // New notifications will decrement the counter.
308 EXPECT_EQ(2, GetPrefNotificationCount());
309
310 message_center::Notification* notification =
311 GetLimitedPerformanceNotification();
312 ASSERT_TRUE(notification);
313
314 // Ensure this notification has the two correct buttons.
315 EXPECT_EQ(2u, notification->buttons().size());
316
317 // Click the notification body.
Arthur Sonzognia98e4432023-11-28 18:15:01318 ClickLimitedNotificationButton(std::nullopt);
Jimmy Gongc6f4fd02021-02-25 01:55:00319 EXPECT_EQ(0, GetPrefNotificationCount());
320 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
321 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
322
323 // No new notifications can appear.
324 controller()->NotifyLimitedPerformance();
325 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
326}
327
328TEST_F(PciePeripheralNotificationControllerTest,
329 LimitedPerformanceNotificationSettingsButtonClick) {
330 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
331 EXPECT_EQ(3, GetPrefNotificationCount());
332
333 controller()->NotifyLimitedPerformance();
334 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
335 // New notifications will decrement the counter.
336 EXPECT_EQ(2, GetPrefNotificationCount());
337
338 message_center::Notification* notification =
339 GetLimitedPerformanceNotification();
340 ASSERT_TRUE(notification);
341
342 // Ensure this notification has the two correct buttons.
343 EXPECT_EQ(2u, notification->buttons().size());
344
345 // Click the Settings button.
346 ClickLimitedNotificationButton(/*button_index=*/0);
347 EXPECT_EQ(0, GetPrefNotificationCount());
348 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
349 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
350
351 // No new notifications can appear.
352 controller()->NotifyLimitedPerformance();
353 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
354}
355
356TEST_F(PciePeripheralNotificationControllerTest,
357 ClickGuestNotificationTbtOnly) {
358 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
359 EXPECT_EQ(3, GetPrefNotificationCount());
360
361 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
362 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
363
364 message_center::Notification* notification =
365 GetGuestModeNotSupportedNotification();
366 ASSERT_TRUE(notification);
367
368 // This notification has no buttons.
369 EXPECT_EQ(0u, notification->buttons().size());
370
371 // We will always show guest notifications, expect that the pref did not
372 // decrement.
Hidehiko Abe4eb522b2022-02-16 11:18:58373 EXPECT_CALL(new_window_delegate_primary(),
374 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47375 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
376 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gongc6f4fd02021-02-25 01:55:00377 ClickGuestNotification(/*is_thunderbolt_only=*/true);
378 EXPECT_EQ(3, GetPrefNotificationCount());
379 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
380
381 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
382 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
383}
384
385TEST_F(PciePeripheralNotificationControllerTest,
386 ClickGuestNotificationTbtAltMode) {
387 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
388 EXPECT_EQ(3, GetPrefNotificationCount());
389
390 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
391 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
392
393 message_center::Notification* notification =
394 GetLimitedPerformanceGuestModeNotification();
395 ASSERT_TRUE(notification);
396
397 // This notification has no buttons.
398 EXPECT_EQ(0u, notification->buttons().size());
399
400 // We will always show guest notifications, expect that the pref did not
401 // decrement.
Hidehiko Abe4eb522b2022-02-16 11:18:58402 EXPECT_CALL(new_window_delegate_primary(),
403 OpenUrl(GURL(kLearnMoreHelpUrl),
Georg Neisb386a3422022-08-17 00:33:47404 NewWindowDelegate::OpenUrlFrom::kUserInteraction,
405 NewWindowDelegate::Disposition::kNewForegroundTab));
Jimmy Gongc6f4fd02021-02-25 01:55:00406 ClickGuestNotification(/*is_thunderbolt_only=*/false);
407 EXPECT_EQ(3, GetPrefNotificationCount());
408 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
409
410 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
411 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
412}
413
Jimmy Gonge1bd8082021-02-23 21:40:57414} // namespace ash