1 // Copyright (c) 2013 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.
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/notifications/message_center_notification_manager.h"
16 #include "chrome/browser/notifications/notification.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "ui/message_center/message_center.h"
23 #include "ui/message_center/message_center_types.h"
25 class TestAddObserver
: public message_center::MessageCenterObserver
{
27 explicit TestAddObserver(message_center::MessageCenter
* message_center
)
28 : message_center_(message_center
) {
29 message_center_
->AddObserver(this);
32 ~TestAddObserver() override
{ message_center_
->RemoveObserver(this); }
34 void OnNotificationAdded(const std::string
& id
) override
{
35 std::string log
= logs_
[id
];
38 logs_
[id
] = log
+ "add-" + id
;
41 void OnNotificationUpdated(const std::string
& id
) override
{
42 std::string log
= logs_
[id
];
45 logs_
[id
] = log
+ "update-" + id
;
48 const std::string
log(const std::string
& id
) { return logs_
[id
]; }
49 void reset_logs() { logs_
.clear(); }
52 std::map
<std::string
, std::string
> logs_
;
53 message_center::MessageCenter
* message_center_
;
56 class MessageCenterNotificationsTest
: public InProcessBrowserTest
{
58 MessageCenterNotificationsTest() {}
60 MessageCenterNotificationManager
* manager() {
61 return static_cast<MessageCenterNotificationManager
*>(
62 g_browser_process
->notification_ui_manager());
65 message_center::MessageCenter
* message_center() {
66 return g_browser_process
->message_center();
69 Profile
* profile() { return browser()->profile(); }
71 class TestDelegate
: public NotificationDelegate
{
73 explicit TestDelegate(const std::string
& id
) : id_(id
) {}
75 void Display() override
{ log_
+= "Display_"; }
76 void Close(bool by_user
) override
{
78 log_
+= ( by_user
? "by_user_" : "programmatically_");
80 void Click() override
{ log_
+= "Click_"; }
81 void ButtonClick(int button_index
) override
{
82 log_
+= "ButtonClick_";
83 log_
+= base::IntToString(button_index
) + "_";
85 std::string
id() const override
{ return id_
; }
87 const std::string
& log() { return log_
; }
90 ~TestDelegate() override
{}
94 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
97 Notification
CreateTestNotification(const std::string
& delegate_id
,
98 TestDelegate
** delegate
= NULL
) {
99 TestDelegate
* new_delegate
= new TestDelegate(delegate_id
);
101 *delegate
= new_delegate
;
102 new_delegate
->AddRef();
105 return Notification(GURL("chrome-test://testing/"),
106 base::ASCIIToUTF16("title"),
107 base::ASCIIToUTF16("message"),
109 base::UTF8ToUTF16("chrome-test://testing/"),
110 base::UTF8ToUTF16("REPLACE-ME"),
114 Notification
CreateRichTestNotification(const std::string
& id
,
115 TestDelegate
** delegate
= NULL
) {
116 TestDelegate
* new_delegate
= new TestDelegate(id
);
118 *delegate
= new_delegate
;
119 new_delegate
->AddRef();
122 message_center::RichNotificationData data
;
124 return Notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT
,
125 GURL("chrome-test://testing/"),
126 base::ASCIIToUTF16("title"),
127 base::ASCIIToUTF16("message"),
129 blink::WebTextDirectionDefault
,
130 message_center::NotifierId(
131 message_center::NotifierId::APPLICATION
,
133 base::UTF8ToUTF16("chrome-test://testing/"),
134 base::UTF8ToUTF16("REPLACE-ME"),
140 // TODO(rsesek): Implement Message Center on Mac and get these tests passing
141 // for real. http://crbug.com/179904
142 #if !defined(OS_MACOSX)
144 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, RetrieveBaseParts
) {
145 EXPECT_TRUE(manager());
146 EXPECT_TRUE(message_center());
149 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, BasicAddCancel
) {
150 #if defined(OS_WIN) && defined(USE_ASH)
151 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
152 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
156 // Someone may create system notifications like "you're in multi-profile
157 // mode..." or something which may change the expectation.
158 // TODO(mukai): move this to SetUpOnMainThread() after fixing the side-effect
159 // of canceling animation which prevents some Displayed() event.
160 manager()->CancelAll();
161 manager()->Add(CreateTestNotification("hey"), profile());
162 EXPECT_EQ(1u, message_center()->NotificationCount());
163 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
164 EXPECT_EQ(0u, message_center()->NotificationCount());
167 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, BasicDelegate
) {
168 #if defined(OS_WIN) && defined(USE_ASH)
169 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
170 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
174 TestDelegate
* delegate
;
175 manager()->Add(CreateTestNotification("hey", &delegate
), profile());
176 // Verify that delegate accumulated correct log of events.
177 EXPECT_EQ("Display_", delegate
->log());
178 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
179 // Verify that delegate accumulated correct log of events.
180 EXPECT_EQ("Display_Close_programmatically_", delegate
->log());
184 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, ButtonClickedDelegate
) {
185 #if defined(OS_WIN) && defined(USE_ASH)
186 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
187 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
191 TestDelegate
* delegate
;
192 manager()->Add(CreateTestNotification("n", &delegate
), profile());
193 const std::string notification_id
=
194 manager()->GetMessageCenterNotificationIdForTest("n", profile());
195 message_center()->ClickOnNotificationButton(notification_id
, 1);
196 // Verify that delegate accumulated correct log of events.
197 EXPECT_EQ("Display_ButtonClick_1_", delegate
->log());
201 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
202 UpdateExistingNotification
) {
203 #if defined(OS_WIN) && defined(USE_ASH)
204 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
205 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
209 TestDelegate
* delegate
;
210 manager()->Add(CreateTestNotification("n", &delegate
), profile());
211 TestDelegate
* delegate2
;
212 manager()->Add(CreateRichTestNotification("n", &delegate2
), profile());
214 manager()->CancelById("n", NotificationUIManager::GetProfileID(profile()));
215 EXPECT_EQ("Display_", delegate
->log());
216 EXPECT_EQ("Close_programmatically_", delegate2
->log());
219 delegate2
->Release();
222 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
, QueueWhenCenterVisible
) {
223 #if defined(OS_WIN) && defined(USE_ASH)
224 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
225 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
229 TestAddObserver
observer(message_center());
231 TestDelegate
* delegate
;
232 TestDelegate
* delegate2
;
234 manager()->Add(CreateTestNotification("n", &delegate
), profile());
235 const std::string id_n
=
236 manager()->GetMessageCenterNotificationIdForTest("n", profile());
237 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
238 manager()->Add(CreateTestNotification("n2", &delegate2
), profile());
239 const std::string id_n2
=
240 manager()->GetMessageCenterNotificationIdForTest("n2", profile());
242 // 'update-n' should happen since SetVisibility updates is_read status of n.
243 // TODO(mukai): fix event handling to happen update-n just once.
244 EXPECT_EQ(base::StringPrintf("add-%s_update-%s_update-%s",
250 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
252 EXPECT_EQ(base::StringPrintf("add-%s", id_n2
.c_str()), observer
.log(id_n2
));
255 delegate2
->Release();
258 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
259 UpdateNonProgressNotificationWhenCenterVisible
) {
260 #if defined(OS_WIN) && defined(USE_ASH)
261 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
262 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
266 TestAddObserver
observer(message_center());
268 TestDelegate
* delegate
;
270 // Add a non-progress notification and update it while the message center
272 Notification notification
= CreateTestNotification("n", &delegate
);
273 manager()->Add(notification
, profile());
274 const std::string notification_id
=
275 manager()->GetMessageCenterNotificationIdForTest("n", profile());
276 message_center()->ClickOnNotification(notification_id
);
277 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
278 observer
.reset_logs();
279 notification
.set_title(base::ASCIIToUTF16("title2"));
280 manager()->Update(notification
, profile());
282 // Expect that the notification update is not done.
283 EXPECT_EQ("", observer
.log(notification_id
));
285 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
286 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
287 observer
.log(notification_id
));
292 IN_PROC_BROWSER_TEST_F(
293 MessageCenterNotificationsTest
,
294 UpdateNonProgressToProgressNotificationWhenCenterVisible
) {
295 #if defined(OS_WIN) && defined(USE_ASH)
296 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
297 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
301 TestAddObserver
observer(message_center());
303 TestDelegate
* delegate
;
305 // Add a non-progress notification and change the type to progress while the
306 // message center is visible.
307 Notification notification
= CreateTestNotification("n", &delegate
);
308 manager()->Add(notification
, profile());
309 const std::string notification_id
=
310 manager()->GetMessageCenterNotificationIdForTest("n", profile());
311 message_center()->ClickOnNotification(notification_id
);
312 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
313 observer
.reset_logs();
314 notification
.set_type(message_center::NOTIFICATION_TYPE_PROGRESS
);
315 manager()->Update(notification
, profile());
317 // Expect that the notification update is not done.
318 EXPECT_EQ("", observer
.log(notification_id
));
320 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
321 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
322 observer
.log(notification_id
));
327 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest
,
328 UpdateProgressNotificationWhenCenterVisible
) {
329 #if defined(OS_WIN) && defined(USE_ASH)
330 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
331 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
335 TestAddObserver
observer(message_center());
337 TestDelegate
* delegate
;
339 // Add a progress notification and update it while the message center
341 Notification notification
= CreateTestNotification("n", &delegate
);
342 notification
.set_type(message_center::NOTIFICATION_TYPE_PROGRESS
);
343 manager()->Add(notification
, profile());
344 const std::string notification_id
=
345 manager()->GetMessageCenterNotificationIdForTest("n", profile());
346 message_center()->ClickOnNotification(notification_id
);
347 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
348 observer
.reset_logs();
349 notification
.set_progress(50);
350 manager()->Update(notification
, profile());
352 // Expect that the progress notification update is performed.
353 EXPECT_EQ(base::StringPrintf("update-%s", notification_id
.c_str()),
354 observer
.log(notification_id
));
359 #endif // !defined(OS_MACOSX)