[go: nahoru, domu]

blob: d23ae36125ef59757f9cb1266f44678a3d81e378 [file] [log] [blame]
Jimmy Gonge1bd8082021-02-23 21:40:571// Copyright 2021 The Chromium Authors. All rights reserved.
2// 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>
8#include <vector>
9
Jimmy Gongc6f4fd02021-02-25 01:55:0010#include "ash/public/cpp/ash_pref_names.h"
Jimmy Gonge1bd8082021-02-23 21:40:5711#include "ash/public/cpp/test/test_new_window_delegate.h"
12#include "ash/public/cpp/test/test_system_tray_client.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0013#include "ash/session/session_controller_impl.h"
Jimmy Gonge1bd8082021-02-23 21:40:5714#include "ash/shell.h"
15#include "ash/test/ash_test_base.h"
16#include "base/optional.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0017#include "components/prefs/pref_service.h"
Jimmy Gonge1bd8082021-02-23 21:40:5718#include "testing/gmock/include/gmock/gmock.h"
19#include "ui/message_center/fake_message_center.h"
Jimmy Gongc6f4fd02021-02-25 01:55:0020#include "ui/message_center/message_center.h"
Jimmy Gonge1bd8082021-02-23 21:40:5721#include "ui/message_center/public/cpp/notification.h"
22#include "ui/message_center/public/cpp/notification_types.h"
23
24using message_center::MessageCenter;
25
26namespace ash {
27
28namespace {
29const char kPciePeripheralLimitedPerformanceNotificationId[] =
30 "cros_pcie_peripheral_limited_performance_notification_id";
31const char kPciePeripheralLimitedPerformanceGuestModeNotificationId[] =
32 "cros_pcie_peripheral_limited_performance_guest_mode_notification_id";
33const char kPciePeripheralGuestModeNotSupportedNotificationId[] =
34 "cros_pcie_peripheral_guest_mode_not_supported_notifcation_id";
Jimmy Gong4041bd132021-03-10 00:00:0335const char kPciePeripheralDeviceBlockedNotificationId[] =
36 "cros_pcie_peripheral_device_blocked_notifcation_id";
Jimmy Gonge1bd8082021-02-23 21:40:5737const char kLearnMoreHelpUrl[] =
38 "https://www.support.google.com/chromebook?p=connect_thblt_usb4_accy";
39
40// A mock implementation of |NewWindowDelegate| for use in tests.
41class MockNewWindowDelegate : public testing::NiceMock<TestNewWindowDelegate> {
42 public:
43 // TestNewWindowDelegate:
44 MOCK_METHOD(void,
45 NewTabWithUrl,
46 (const GURL& url, bool from_user_interaction),
47 (override));
48};
49
50} // namespace
51
52class PciePeripheralNotificationControllerTest : public AshTestBase {
53 public:
Hidehiko Abe6e3e57f42021-03-23 11:23:5854 PciePeripheralNotificationControllerTest() {
55 auto delegate = std::make_unique<MockNewWindowDelegate>();
56 new_window_delegate_ = delegate.get();
57 delegate_provider_ =
58 std::make_unique<TestNewWindowDelegateProvider>(std::move(delegate));
59 }
Jimmy Gonge1bd8082021-02-23 21:40:5760 PciePeripheralNotificationControllerTest(
61 const PciePeripheralNotificationControllerTest&) = delete;
62 PciePeripheralNotificationControllerTest& operator=(
63 const PciePeripheralNotificationControllerTest&) = delete;
64 ~PciePeripheralNotificationControllerTest() override = default;
65
66 PciePeripheralNotificationController* controller() {
67 return Shell::Get()->pcie_peripheral_notification_controller();
68 }
69
Hidehiko Abe6e3e57f42021-03-23 11:23:5870 MockNewWindowDelegate& new_window_delegate() { return *new_window_delegate_; }
Jimmy Gonge1bd8082021-02-23 21:40:5771
72 message_center::Notification* GetLimitedPerformanceNotification() {
73 return MessageCenter::Get()->FindVisibleNotificationById(
74 kPciePeripheralLimitedPerformanceNotificationId);
75 }
76
77 message_center::Notification* GetLimitedPerformanceGuestModeNotification() {
78 return MessageCenter::Get()->FindVisibleNotificationById(
79 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
80 }
81
82 message_center::Notification* GetGuestModeNotSupportedNotification() {
83 return MessageCenter::Get()->FindVisibleNotificationById(
84 kPciePeripheralGuestModeNotSupportedNotificationId);
85 }
86
Jimmy Gong4041bd132021-03-10 00:00:0387 message_center::Notification* GetPeripheralBlockedNotification() {
88 return MessageCenter::Get()->FindVisibleNotificationById(
89 kPciePeripheralDeviceBlockedNotificationId);
90 }
91
Jimmy Gonge1bd8082021-02-23 21:40:5792 int GetNumOsPrivacySettingsOpened() {
93 return GetSystemTrayClient()->show_os_settings_privacy_and_security_count();
94 }
95
Jimmy Gongc6f4fd02021-02-25 01:55:0096 int GetPrefNotificationCount() {
97 PrefService* prefs =
98 Shell::Get()->session_controller()->GetActivePrefService();
99 return prefs->GetInteger(
100 prefs::kPciePeripheralDisplayNotificationRemaining);
101 }
102
Jimmy Gonge1bd8082021-02-23 21:40:57103 void ClickLimitedNotificationButton(base::Optional<int> button_index) {
104 // No button index means the notification body was clicked.
105 if (!button_index.has_value()) {
106 message_center::Notification* notification =
107 MessageCenter::Get()->FindVisibleNotificationById(
108 kPciePeripheralLimitedPerformanceNotificationId);
109 notification->delegate()->Click(base::nullopt, base::nullopt);
110 return;
111 }
112
113 message_center::Notification* notification =
114 MessageCenter::Get()->FindVisibleNotificationById(
115 kPciePeripheralLimitedPerformanceNotificationId);
116 notification->delegate()->Click(button_index, base::nullopt);
117 }
118
119 void ClickGuestNotification(bool is_thunderbolt_only) {
120 if (is_thunderbolt_only) {
121 MessageCenter::Get()->ClickOnNotification(
122 kPciePeripheralGuestModeNotSupportedNotificationId);
123 return;
124 }
125
126 MessageCenter::Get()->ClickOnNotification(
127 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
128 }
129
Jimmy Gongc6f4fd02021-02-25 01:55:00130 void RemoveAllNotifications() {
131 MessageCenter::Get()->RemoveAllNotifications(
132 /*by_user=*/false, message_center::MessageCenter::RemoveType::ALL);
133 }
134
Jimmy Gonge1bd8082021-02-23 21:40:57135 private:
Hidehiko Abe6e3e57f42021-03-23 11:23:58136 MockNewWindowDelegate* new_window_delegate_;
137 std::unique_ptr<TestNewWindowDelegateProvider> delegate_provider_;
Jimmy Gonge1bd8082021-02-23 21:40:57138};
139
Jimmy Gonge1bd8082021-02-23 21:40:57140TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtOnly) {
141 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
142
143 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
Jimmy Gonge1bd8082021-02-23 21:40:57144 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
145
146 message_center::Notification* notification =
147 GetGuestModeNotSupportedNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00148 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57149
150 // This notification has no buttons.
151 EXPECT_EQ(0u, notification->buttons().size());
152
153 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
154 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
155
156 // Click on the notification and expect the Learn More page to page to appear.
157 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
158 .WillOnce([](const GURL& url, bool from_user_interaction) {
159 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
160 EXPECT_TRUE(from_user_interaction);
161 });
162 ClickGuestNotification(/*is_thunderbolt_only=*/true);
163 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
164}
165
166TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtAltMode) {
167 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
168
169 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
170 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
171
172 message_center::Notification* notification =
173 GetLimitedPerformanceGuestModeNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00174 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57175
176 // This notification has no buttons.
177 EXPECT_EQ(0u, notification->buttons().size());
178
179 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
180 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
181
182 // Click on the notification and expect the Learn More page to page to appear.
183 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
184 .WillOnce([](const GURL& url, bool from_user_interaction) {
185 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
186 EXPECT_TRUE(from_user_interaction);
187 });
188 ClickGuestNotification(/*is_thunderbolt_only=*/false);
189 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
190}
191
Jimmy Gongc6f4fd02021-02-25 01:55:00192TEST_F(PciePeripheralNotificationControllerTest,
Jimmy Gong4041bd132021-03-10 00:00:03193 PeripheralBlockedNotification) {
194 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
195
196 controller()->NotifyPeripheralBlockedNotification();
197 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
198
199 message_center::Notification* notification =
200 GetPeripheralBlockedNotification();
201 ASSERT_TRUE(notification);
202
203 // This notification has no buttons.
204 EXPECT_EQ(0u, notification->buttons().size());
205
206 // Click on the notification and expect the Learn More page to page to appear.
207 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
208 .WillOnce([](const GURL& url, bool from_user_interaction) {
209 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
210 EXPECT_TRUE(from_user_interaction);
211 });
212 MessageCenter::Get()->ClickOnNotification(
213 kPciePeripheralDeviceBlockedNotificationId);
214 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
215}
216
217TEST_F(PciePeripheralNotificationControllerTest,
Jimmy Gongc6f4fd02021-02-25 01:55:00218 LimitedPerformanceNotificationLearnMoreClick) {
219 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
220 EXPECT_EQ(3, GetPrefNotificationCount());
221
222 controller()->NotifyLimitedPerformance();
223 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
224
225 message_center::Notification* notification =
226 GetLimitedPerformanceNotification();
227 ASSERT_TRUE(notification);
228
229 // Ensure this notification has the two correct buttons.
230 EXPECT_EQ(2u, notification->buttons().size());
231
232 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
233 .WillOnce([](const GURL& url, bool from_user_interaction) {
234 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
235 EXPECT_TRUE(from_user_interaction);
236 });
237 // Click the learn more link.
238 ClickLimitedNotificationButton(/*button_index=*/1);
239 EXPECT_EQ(2, GetPrefNotificationCount());
240 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
241
242 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
243 .WillOnce([](const GURL& url, bool from_user_interaction) {
244 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
245 EXPECT_TRUE(from_user_interaction);
246 });
247 controller()->NotifyLimitedPerformance();
248 ClickLimitedNotificationButton(/*button_index=*/1);
249 EXPECT_EQ(1, GetPrefNotificationCount());
250 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
251
252 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
253 .WillOnce([](const GURL& url, bool from_user_interaction) {
254 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
255 EXPECT_TRUE(from_user_interaction);
256 });
257 controller()->NotifyLimitedPerformance();
258 ClickLimitedNotificationButton(/*button_index=*/1);
259 EXPECT_EQ(0, GetPrefNotificationCount());
260 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
261
262 controller()->NotifyLimitedPerformance();
263 // Pref is currently at 0, so no new notifications should appear.
264 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
265}
266
267TEST_F(PciePeripheralNotificationControllerTest,
268 LimitedPerformanceNotificationBodyClick) {
269 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
270 EXPECT_EQ(3, GetPrefNotificationCount());
271
272 controller()->NotifyLimitedPerformance();
273 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
274 // New notifications will decrement the counter.
275 EXPECT_EQ(2, GetPrefNotificationCount());
276
277 message_center::Notification* notification =
278 GetLimitedPerformanceNotification();
279 ASSERT_TRUE(notification);
280
281 // Ensure this notification has the two correct buttons.
282 EXPECT_EQ(2u, notification->buttons().size());
283
284 // Click the notification body.
285 ClickLimitedNotificationButton(base::nullopt);
286 EXPECT_EQ(0, GetPrefNotificationCount());
287 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
288 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
289
290 // No new notifications can appear.
291 controller()->NotifyLimitedPerformance();
292 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
293}
294
295TEST_F(PciePeripheralNotificationControllerTest,
296 LimitedPerformanceNotificationSettingsButtonClick) {
297 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
298 EXPECT_EQ(3, GetPrefNotificationCount());
299
300 controller()->NotifyLimitedPerformance();
301 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
302 // New notifications will decrement the counter.
303 EXPECT_EQ(2, GetPrefNotificationCount());
304
305 message_center::Notification* notification =
306 GetLimitedPerformanceNotification();
307 ASSERT_TRUE(notification);
308
309 // Ensure this notification has the two correct buttons.
310 EXPECT_EQ(2u, notification->buttons().size());
311
312 // Click the Settings button.
313 ClickLimitedNotificationButton(/*button_index=*/0);
314 EXPECT_EQ(0, GetPrefNotificationCount());
315 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
316 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
317
318 // No new notifications can appear.
319 controller()->NotifyLimitedPerformance();
320 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
321}
322
323TEST_F(PciePeripheralNotificationControllerTest,
324 ClickGuestNotificationTbtOnly) {
325 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
326 EXPECT_EQ(3, GetPrefNotificationCount());
327
328 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
329 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
330
331 message_center::Notification* notification =
332 GetGuestModeNotSupportedNotification();
333 ASSERT_TRUE(notification);
334
335 // This notification has no buttons.
336 EXPECT_EQ(0u, notification->buttons().size());
337
338 // We will always show guest notifications, expect that the pref did not
339 // decrement.
340 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
341 .WillOnce([](const GURL& url, bool from_user_interaction) {
342 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
343 EXPECT_TRUE(from_user_interaction);
344 });
345 ClickGuestNotification(/*is_thunderbolt_only=*/true);
346 EXPECT_EQ(3, GetPrefNotificationCount());
347 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
348
349 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
350 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
351}
352
353TEST_F(PciePeripheralNotificationControllerTest,
354 ClickGuestNotificationTbtAltMode) {
355 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
356 EXPECT_EQ(3, GetPrefNotificationCount());
357
358 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
359 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
360
361 message_center::Notification* notification =
362 GetLimitedPerformanceGuestModeNotification();
363 ASSERT_TRUE(notification);
364
365 // This notification has no buttons.
366 EXPECT_EQ(0u, notification->buttons().size());
367
368 // We will always show guest notifications, expect that the pref did not
369 // decrement.
370 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
371 .WillOnce([](const GURL& url, bool from_user_interaction) {
372 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
373 EXPECT_TRUE(from_user_interaction);
374 });
375 ClickGuestNotification(/*is_thunderbolt_only=*/false);
376 EXPECT_EQ(3, GetPrefNotificationCount());
377 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
378
379 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
380 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
381}
382
Jimmy Gonge1bd8082021-02-23 21:40:57383} // namespace ash