Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_notifications_browsertest.cc
blob87c00c4d5293df5f1c281fec7420853129af6d84
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.
5 #include <map>
6 #include <string>
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 {
26 public:
27 explicit TestAddObserver(message_center::MessageCenter* message_center)
28 : message_center_(message_center) {
29 message_center_->AddObserver(this);
32 virtual ~TestAddObserver() { message_center_->RemoveObserver(this); }
34 virtual void OnNotificationAdded(const std::string& id) override {
35 std::string log = logs_[id];
36 if (log != "")
37 log += "_";
38 logs_[id] = log + "add-" + id;
41 virtual void OnNotificationUpdated(const std::string& id) override {
42 std::string log = logs_[id];
43 if (log != "")
44 log += "_";
45 logs_[id] = log + "update-" + id;
48 const std::string log(const std::string& id) { return logs_[id]; }
49 void reset_logs() { logs_.clear(); }
51 private:
52 std::map<std::string, std::string> logs_;
53 message_center::MessageCenter* message_center_;
56 class MessageCenterNotificationsTest : public InProcessBrowserTest {
57 public:
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 {
72 public:
73 explicit TestDelegate(const std::string& id) : id_(id) {}
75 virtual void Display() override { log_ += "Display_"; }
76 virtual void Error() override { log_ += "Error_"; }
77 virtual void Close(bool by_user) override {
78 log_ += "Close_";
79 log_ += ( by_user ? "by_user_" : "programmatically_");
81 virtual void Click() override { log_ += "Click_"; }
82 virtual void ButtonClick(int button_index) override {
83 log_ += "ButtonClick_";
84 log_ += base::IntToString(button_index) + "_";
86 virtual std::string id() const override { return id_; }
88 const std::string& log() { return log_; }
90 private:
91 virtual ~TestDelegate() {}
92 std::string id_;
93 std::string log_;
95 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
98 Notification CreateTestNotification(const std::string& delegate_id,
99 TestDelegate** delegate = NULL) {
100 TestDelegate* new_delegate = new TestDelegate(delegate_id);
101 if (delegate) {
102 *delegate = new_delegate;
103 new_delegate->AddRef();
106 return Notification(GURL("chrome-test://testing/"),
107 base::ASCIIToUTF16("title"),
108 base::ASCIIToUTF16("message"),
109 gfx::Image(),
110 base::UTF8ToUTF16("chrome-test://testing/"),
111 base::UTF8ToUTF16("REPLACE-ME"),
112 new_delegate);
115 Notification CreateRichTestNotification(const std::string& id,
116 TestDelegate** delegate = NULL) {
117 TestDelegate* new_delegate = new TestDelegate(id);
118 if (delegate) {
119 *delegate = new_delegate;
120 new_delegate->AddRef();
123 message_center::RichNotificationData data;
125 return Notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
126 GURL("chrome-test://testing/"),
127 base::ASCIIToUTF16("title"),
128 base::ASCIIToUTF16("message"),
129 gfx::Image(),
130 blink::WebTextDirectionDefault,
131 message_center::NotifierId(
132 message_center::NotifierId::APPLICATION,
133 "extension_id"),
134 base::UTF8ToUTF16("chrome-test://testing/"),
135 base::UTF8ToUTF16("REPLACE-ME"),
136 data,
137 new_delegate);
141 // TODO(rsesek): Implement Message Center on Mac and get these tests passing
142 // for real. http://crbug.com/179904
143 #if !defined(OS_MACOSX)
145 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) {
146 EXPECT_TRUE(manager());
147 EXPECT_TRUE(message_center());
150 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, BasicAddCancel) {
151 #if defined(OS_WIN) && defined(USE_ASH)
152 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
153 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
154 return;
155 #endif
157 // Someone may create system notifications like "you're in multi-profile
158 // mode..." or something which may change the expectation.
159 // TODO(mukai): move this to SetUpOnMainThread() after fixing the side-effect
160 // of canceling animation which prevents some Displayed() event.
161 manager()->CancelAll();
162 manager()->Add(CreateTestNotification("hey"), profile());
163 EXPECT_EQ(1u, message_center()->NotificationCount());
164 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
165 EXPECT_EQ(0u, message_center()->NotificationCount());
168 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, BasicDelegate) {
169 #if defined(OS_WIN) && defined(USE_ASH)
170 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
171 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
172 return;
173 #endif
175 TestDelegate* delegate;
176 manager()->Add(CreateTestNotification("hey", &delegate), profile());
177 // Verify that delegate accumulated correct log of events.
178 EXPECT_EQ("Display_", delegate->log());
179 manager()->CancelById("hey", NotificationUIManager::GetProfileID(profile()));
180 // Verify that delegate accumulated correct log of events.
181 EXPECT_EQ("Display_Close_programmatically_", delegate->log());
182 delegate->Release();
185 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, ButtonClickedDelegate) {
186 #if defined(OS_WIN) && defined(USE_ASH)
187 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
188 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
189 return;
190 #endif
192 TestDelegate* delegate;
193 manager()->Add(CreateTestNotification("n", &delegate), profile());
194 const std::string notification_id =
195 manager()->GetMessageCenterNotificationIdForTest("n", profile());
196 message_center()->ClickOnNotificationButton(notification_id, 1);
197 // Verify that delegate accumulated correct log of events.
198 EXPECT_EQ("Display_ButtonClick_1_", delegate->log());
199 delegate->Release();
202 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
203 UpdateExistingNotification) {
204 #if defined(OS_WIN) && defined(USE_ASH)
205 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
206 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
207 return;
208 #endif
210 TestDelegate* delegate;
211 manager()->Add(CreateTestNotification("n", &delegate), profile());
212 TestDelegate* delegate2;
213 manager()->Add(CreateRichTestNotification("n", &delegate2), profile());
215 manager()->CancelById("n", NotificationUIManager::GetProfileID(profile()));
216 EXPECT_EQ("Display_", delegate->log());
217 EXPECT_EQ("Close_programmatically_", delegate2->log());
219 delegate->Release();
220 delegate2->Release();
223 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, QueueWhenCenterVisible) {
224 #if defined(OS_WIN) && defined(USE_ASH)
225 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
226 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
227 return;
228 #endif
230 TestAddObserver observer(message_center());
232 TestDelegate* delegate;
233 TestDelegate* delegate2;
235 manager()->Add(CreateTestNotification("n", &delegate), profile());
236 const std::string id_n =
237 manager()->GetMessageCenterNotificationIdForTest("n", profile());
238 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
239 manager()->Add(CreateTestNotification("n2", &delegate2), profile());
240 const std::string id_n2 =
241 manager()->GetMessageCenterNotificationIdForTest("n2", profile());
243 // 'update-n' should happen since SetVisibility updates is_read status of n.
244 // TODO(mukai): fix event handling to happen update-n just once.
245 EXPECT_EQ(base::StringPrintf("add-%s_update-%s_update-%s",
246 id_n.c_str(),
247 id_n.c_str(),
248 id_n.c_str()),
249 observer.log(id_n));
251 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
253 EXPECT_EQ(base::StringPrintf("add-%s", id_n2.c_str()), observer.log(id_n2));
255 delegate->Release();
256 delegate2->Release();
259 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
260 UpdateNonProgressNotificationWhenCenterVisible) {
261 #if defined(OS_WIN) && defined(USE_ASH)
262 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
263 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
264 return;
265 #endif
267 TestAddObserver observer(message_center());
269 TestDelegate* delegate;
271 // Add a non-progress notification and update it while the message center
272 // is visible.
273 Notification notification = CreateTestNotification("n", &delegate);
274 manager()->Add(notification, profile());
275 const std::string notification_id =
276 manager()->GetMessageCenterNotificationIdForTest("n", profile());
277 message_center()->ClickOnNotification(notification_id);
278 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
279 observer.reset_logs();
280 notification.set_title(base::ASCIIToUTF16("title2"));
281 manager()->Update(notification, profile());
283 // Expect that the notification update is not done.
284 EXPECT_EQ("", observer.log(notification_id));
286 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
287 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
288 observer.log(notification_id));
290 delegate->Release();
293 IN_PROC_BROWSER_TEST_F(
294 MessageCenterNotificationsTest,
295 UpdateNonProgressToProgressNotificationWhenCenterVisible) {
296 #if defined(OS_WIN) && defined(USE_ASH)
297 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
298 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
299 return;
300 #endif
302 TestAddObserver observer(message_center());
304 TestDelegate* delegate;
306 // Add a non-progress notification and change the type to progress while the
307 // message center is visible.
308 Notification notification = CreateTestNotification("n", &delegate);
309 manager()->Add(notification, profile());
310 const std::string notification_id =
311 manager()->GetMessageCenterNotificationIdForTest("n", profile());
312 message_center()->ClickOnNotification(notification_id);
313 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
314 observer.reset_logs();
315 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
316 manager()->Update(notification, profile());
318 // Expect that the notification update is not done.
319 EXPECT_EQ("", observer.log(notification_id));
321 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
322 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
323 observer.log(notification_id));
325 delegate->Release();
328 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
329 UpdateProgressNotificationWhenCenterVisible) {
330 #if defined(OS_WIN) && defined(USE_ASH)
331 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
332 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
333 return;
334 #endif
336 TestAddObserver observer(message_center());
338 TestDelegate* delegate;
340 // Add a progress notification and update it while the message center
341 // is visible.
342 Notification notification = CreateTestNotification("n", &delegate);
343 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
344 manager()->Add(notification, profile());
345 const std::string notification_id =
346 manager()->GetMessageCenterNotificationIdForTest("n", profile());
347 message_center()->ClickOnNotification(notification_id);
348 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
349 observer.reset_logs();
350 notification.set_progress(50);
351 manager()->Update(notification, profile());
353 // Expect that the progress notification update is performed.
354 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
355 observer.log(notification_id));
357 delegate->Release();
360 #endif // !defined(OS_MACOSX)