[go: nahoru, domu]

blob: 1b291f44edaaa2cf166f6b069d56141435645b4c [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";
35const char kLearnMoreHelpUrl[] =
36 "https://www.support.google.com/chromebook?p=connect_thblt_usb4_accy";
37
38// A mock implementation of |NewWindowDelegate| for use in tests.
39class MockNewWindowDelegate : public testing::NiceMock<TestNewWindowDelegate> {
40 public:
41 // TestNewWindowDelegate:
42 MOCK_METHOD(void,
43 NewTabWithUrl,
44 (const GURL& url, bool from_user_interaction),
45 (override));
46};
47
48} // namespace
49
50class PciePeripheralNotificationControllerTest : public AshTestBase {
51 public:
52 PciePeripheralNotificationControllerTest() = default;
53 PciePeripheralNotificationControllerTest(
54 const PciePeripheralNotificationControllerTest&) = delete;
55 PciePeripheralNotificationControllerTest& operator=(
56 const PciePeripheralNotificationControllerTest&) = delete;
57 ~PciePeripheralNotificationControllerTest() override = default;
58
59 PciePeripheralNotificationController* controller() {
60 return Shell::Get()->pcie_peripheral_notification_controller();
61 }
62
63 MockNewWindowDelegate& new_window_delegate() { return new_window_delegate_; }
64
65 message_center::Notification* GetLimitedPerformanceNotification() {
66 return MessageCenter::Get()->FindVisibleNotificationById(
67 kPciePeripheralLimitedPerformanceNotificationId);
68 }
69
70 message_center::Notification* GetLimitedPerformanceGuestModeNotification() {
71 return MessageCenter::Get()->FindVisibleNotificationById(
72 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
73 }
74
75 message_center::Notification* GetGuestModeNotSupportedNotification() {
76 return MessageCenter::Get()->FindVisibleNotificationById(
77 kPciePeripheralGuestModeNotSupportedNotificationId);
78 }
79
80 int GetNumOsPrivacySettingsOpened() {
81 return GetSystemTrayClient()->show_os_settings_privacy_and_security_count();
82 }
83
Jimmy Gongc6f4fd02021-02-25 01:55:0084 int GetPrefNotificationCount() {
85 PrefService* prefs =
86 Shell::Get()->session_controller()->GetActivePrefService();
87 return prefs->GetInteger(
88 prefs::kPciePeripheralDisplayNotificationRemaining);
89 }
90
Jimmy Gonge1bd8082021-02-23 21:40:5791 void ClickLimitedNotificationButton(base::Optional<int> button_index) {
92 // No button index means the notification body was clicked.
93 if (!button_index.has_value()) {
94 message_center::Notification* notification =
95 MessageCenter::Get()->FindVisibleNotificationById(
96 kPciePeripheralLimitedPerformanceNotificationId);
97 notification->delegate()->Click(base::nullopt, base::nullopt);
98 return;
99 }
100
101 message_center::Notification* notification =
102 MessageCenter::Get()->FindVisibleNotificationById(
103 kPciePeripheralLimitedPerformanceNotificationId);
104 notification->delegate()->Click(button_index, base::nullopt);
105 }
106
107 void ClickGuestNotification(bool is_thunderbolt_only) {
108 if (is_thunderbolt_only) {
109 MessageCenter::Get()->ClickOnNotification(
110 kPciePeripheralGuestModeNotSupportedNotificationId);
111 return;
112 }
113
114 MessageCenter::Get()->ClickOnNotification(
115 kPciePeripheralLimitedPerformanceGuestModeNotificationId);
116 }
117
Jimmy Gongc6f4fd02021-02-25 01:55:00118 void RemoveAllNotifications() {
119 MessageCenter::Get()->RemoveAllNotifications(
120 /*by_user=*/false, message_center::MessageCenter::RemoveType::ALL);
121 }
122
Jimmy Gonge1bd8082021-02-23 21:40:57123 private:
124 MockNewWindowDelegate new_window_delegate_;
125};
126
Jimmy Gonge1bd8082021-02-23 21:40:57127TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtOnly) {
128 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
129
130 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
Jimmy Gonge1bd8082021-02-23 21:40:57131 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
132
133 message_center::Notification* notification =
134 GetGuestModeNotSupportedNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00135 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57136
137 // This notification has no buttons.
138 EXPECT_EQ(0u, notification->buttons().size());
139
140 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
141 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
142
143 // Click on the notification and expect the Learn More page to page to appear.
144 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
145 .WillOnce([](const GURL& url, bool from_user_interaction) {
146 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
147 EXPECT_TRUE(from_user_interaction);
148 });
149 ClickGuestNotification(/*is_thunderbolt_only=*/true);
150 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
151}
152
153TEST_F(PciePeripheralNotificationControllerTest, GuestNotificationTbtAltMode) {
154 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
155
156 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
157 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
158
159 message_center::Notification* notification =
160 GetLimitedPerformanceGuestModeNotification();
Jimmy Gongc6f4fd02021-02-25 01:55:00161 ASSERT_TRUE(notification);
Jimmy Gonge1bd8082021-02-23 21:40:57162
163 // This notification has no buttons.
164 EXPECT_EQ(0u, notification->buttons().size());
165
166 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
167 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
168
169 // Click on the notification and expect the Learn More page to page to appear.
170 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
171 .WillOnce([](const GURL& url, bool from_user_interaction) {
172 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
173 EXPECT_TRUE(from_user_interaction);
174 });
175 ClickGuestNotification(/*is_thunderbolt_only=*/false);
176 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
177}
178
Jimmy Gongc6f4fd02021-02-25 01:55:00179TEST_F(PciePeripheralNotificationControllerTest,
180 LimitedPerformanceNotificationLearnMoreClick) {
181 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
182 EXPECT_EQ(3, GetPrefNotificationCount());
183
184 controller()->NotifyLimitedPerformance();
185 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
186
187 message_center::Notification* notification =
188 GetLimitedPerformanceNotification();
189 ASSERT_TRUE(notification);
190
191 // Ensure this notification has the two correct buttons.
192 EXPECT_EQ(2u, notification->buttons().size());
193
194 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
195 .WillOnce([](const GURL& url, bool from_user_interaction) {
196 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
197 EXPECT_TRUE(from_user_interaction);
198 });
199 // Click the learn more link.
200 ClickLimitedNotificationButton(/*button_index=*/1);
201 EXPECT_EQ(2, GetPrefNotificationCount());
202 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
203
204 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
205 .WillOnce([](const GURL& url, bool from_user_interaction) {
206 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
207 EXPECT_TRUE(from_user_interaction);
208 });
209 controller()->NotifyLimitedPerformance();
210 ClickLimitedNotificationButton(/*button_index=*/1);
211 EXPECT_EQ(1, GetPrefNotificationCount());
212 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
213
214 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
215 .WillOnce([](const GURL& url, bool from_user_interaction) {
216 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
217 EXPECT_TRUE(from_user_interaction);
218 });
219 controller()->NotifyLimitedPerformance();
220 ClickLimitedNotificationButton(/*button_index=*/1);
221 EXPECT_EQ(0, GetPrefNotificationCount());
222 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
223
224 controller()->NotifyLimitedPerformance();
225 // Pref is currently at 0, so no new notifications should appear.
226 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
227}
228
229TEST_F(PciePeripheralNotificationControllerTest,
230 LimitedPerformanceNotificationBodyClick) {
231 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
232 EXPECT_EQ(3, GetPrefNotificationCount());
233
234 controller()->NotifyLimitedPerformance();
235 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
236 // New notifications will decrement the counter.
237 EXPECT_EQ(2, GetPrefNotificationCount());
238
239 message_center::Notification* notification =
240 GetLimitedPerformanceNotification();
241 ASSERT_TRUE(notification);
242
243 // Ensure this notification has the two correct buttons.
244 EXPECT_EQ(2u, notification->buttons().size());
245
246 // Click the notification body.
247 ClickLimitedNotificationButton(base::nullopt);
248 EXPECT_EQ(0, GetPrefNotificationCount());
249 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
250 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
251
252 // No new notifications can appear.
253 controller()->NotifyLimitedPerformance();
254 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
255}
256
257TEST_F(PciePeripheralNotificationControllerTest,
258 LimitedPerformanceNotificationSettingsButtonClick) {
259 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
260 EXPECT_EQ(3, GetPrefNotificationCount());
261
262 controller()->NotifyLimitedPerformance();
263 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
264 // New notifications will decrement the counter.
265 EXPECT_EQ(2, GetPrefNotificationCount());
266
267 message_center::Notification* notification =
268 GetLimitedPerformanceNotification();
269 ASSERT_TRUE(notification);
270
271 // Ensure this notification has the two correct buttons.
272 EXPECT_EQ(2u, notification->buttons().size());
273
274 // Click the Settings button.
275 ClickLimitedNotificationButton(/*button_index=*/0);
276 EXPECT_EQ(0, GetPrefNotificationCount());
277 EXPECT_EQ(1, GetNumOsPrivacySettingsOpened());
278 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
279
280 // No new notifications can appear.
281 controller()->NotifyLimitedPerformance();
282 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
283}
284
285TEST_F(PciePeripheralNotificationControllerTest,
286 ClickGuestNotificationTbtOnly) {
287 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
288 EXPECT_EQ(3, GetPrefNotificationCount());
289
290 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
291 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
292
293 message_center::Notification* notification =
294 GetGuestModeNotSupportedNotification();
295 ASSERT_TRUE(notification);
296
297 // This notification has no buttons.
298 EXPECT_EQ(0u, notification->buttons().size());
299
300 // We will always show guest notifications, expect that the pref did not
301 // decrement.
302 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
303 .WillOnce([](const GURL& url, bool from_user_interaction) {
304 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
305 EXPECT_TRUE(from_user_interaction);
306 });
307 ClickGuestNotification(/*is_thunderbolt_only=*/true);
308 EXPECT_EQ(3, GetPrefNotificationCount());
309 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
310
311 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/true);
312 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
313}
314
315TEST_F(PciePeripheralNotificationControllerTest,
316 ClickGuestNotificationTbtAltMode) {
317 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
318 EXPECT_EQ(3, GetPrefNotificationCount());
319
320 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
321 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
322
323 message_center::Notification* notification =
324 GetLimitedPerformanceGuestModeNotification();
325 ASSERT_TRUE(notification);
326
327 // This notification has no buttons.
328 EXPECT_EQ(0u, notification->buttons().size());
329
330 // We will always show guest notifications, expect that the pref did not
331 // decrement.
332 EXPECT_CALL(new_window_delegate(), NewTabWithUrl)
333 .WillOnce([](const GURL& url, bool from_user_interaction) {
334 EXPECT_EQ(GURL(kLearnMoreHelpUrl), url);
335 EXPECT_TRUE(from_user_interaction);
336 });
337 ClickGuestNotification(/*is_thunderbolt_only=*/false);
338 EXPECT_EQ(3, GetPrefNotificationCount());
339 EXPECT_EQ(0u, MessageCenter::Get()->NotificationCount());
340
341 controller()->NotifyGuestModeNotification(/*is_thunderbolt_only=*/false);
342 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
343}
344
Jimmy Gonge1bd8082021-02-23 21:40:57345} // namespace ash