Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_notifications_browsertest.cc
blobf77879434c9a72fa39a11725ef8a92eb97c0e547
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 ~TestAddObserver() override { message_center_->RemoveObserver(this); }
34 void OnNotificationAdded(const std::string& id) override {
35 std::string log = logs_[id];
36 if (!log.empty())
37 log += "_";
38 logs_[id] = log + "add-" + id;
41 void OnNotificationUpdated(const std::string& id) override {
42 std::string log = logs_[id];
43 if (!log.empty())
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 void Display() override { log_ += "Display_"; }
76 void Close(bool by_user) override {
77 log_ += "Close_";
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_; }
89 private:
90 ~TestDelegate() override {}
91 std::string id_;
92 std::string log_;
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);
100 if (delegate) {
101 *delegate = new_delegate;
102 new_delegate->AddRef();
105 return Notification(GURL("chrome-test://testing/"),
106 base::ASCIIToUTF16("title"),
107 base::ASCIIToUTF16("message"),
108 gfx::Image(),
109 base::UTF8ToUTF16("chrome-test://testing/"),
110 "REPLACE-ME",
111 new_delegate);
114 Notification CreateRichTestNotification(const std::string& id,
115 TestDelegate** delegate = NULL) {
116 TestDelegate* new_delegate = new TestDelegate(id);
117 if (delegate) {
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"),
128 gfx::Image(),
129 message_center::NotifierId(
130 message_center::NotifierId::APPLICATION,
131 "extension_id"),
132 base::UTF8ToUTF16("chrome-test://testing/"),
133 "REPLACE-ME",
134 data,
135 new_delegate);
139 // TODO(rsesek): Implement Message Center on Mac and get these tests passing
140 // for real. http://crbug.com/179904
141 #if !defined(OS_MACOSX)
143 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) {
144 EXPECT_TRUE(manager());
145 EXPECT_TRUE(message_center());
148 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, BasicAddCancel) {
149 #if defined(OS_WIN) && defined(USE_ASH)
150 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
151 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
152 switches::kAshBrowserTests))
153 return;
154 #endif
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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
171 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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
189 switches::kAshBrowserTests))
190 return;
191 #endif
193 TestDelegate* delegate;
194 manager()->Add(CreateTestNotification("n", &delegate), profile());
195 const std::string notification_id =
196 manager()->GetMessageCenterNotificationIdForTest("n", profile());
197 message_center()->ClickOnNotificationButton(notification_id, 1);
198 // Verify that delegate accumulated correct log of events.
199 EXPECT_EQ("Display_ButtonClick_1_", delegate->log());
200 delegate->Release();
203 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
204 UpdateExistingNotification) {
205 #if defined(OS_WIN) && defined(USE_ASH)
206 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
207 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
208 switches::kAshBrowserTests))
209 return;
210 #endif
212 TestDelegate* delegate;
213 manager()->Add(CreateTestNotification("n", &delegate), profile());
214 TestDelegate* delegate2;
215 manager()->Add(CreateRichTestNotification("n", &delegate2), profile());
217 manager()->CancelById("n", NotificationUIManager::GetProfileID(profile()));
218 EXPECT_EQ("Display_", delegate->log());
219 EXPECT_EQ("Close_programmatically_", delegate2->log());
221 delegate->Release();
222 delegate2->Release();
225 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, QueueWhenCenterVisible) {
226 #if defined(OS_WIN) && defined(USE_ASH)
227 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
228 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
229 switches::kAshBrowserTests))
230 return;
231 #endif
233 TestAddObserver observer(message_center());
235 TestDelegate* delegate;
236 TestDelegate* delegate2;
238 manager()->Add(CreateTestNotification("n", &delegate), profile());
239 const std::string id_n =
240 manager()->GetMessageCenterNotificationIdForTest("n", profile());
241 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
242 manager()->Add(CreateTestNotification("n2", &delegate2), profile());
243 const std::string id_n2 =
244 manager()->GetMessageCenterNotificationIdForTest("n2", profile());
246 // 'update-n' should happen since SetVisibility updates is_read status of n.
247 // TODO(mukai): fix event handling to happen update-n just once.
248 EXPECT_EQ(base::StringPrintf("add-%s_update-%s_update-%s",
249 id_n.c_str(),
250 id_n.c_str(),
251 id_n.c_str()),
252 observer.log(id_n));
254 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
256 EXPECT_EQ(base::StringPrintf("add-%s", id_n2.c_str()), observer.log(id_n2));
258 delegate->Release();
259 delegate2->Release();
262 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
263 UpdateNonProgressNotificationWhenCenterVisible) {
264 #if defined(OS_WIN) && defined(USE_ASH)
265 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
266 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
267 switches::kAshBrowserTests))
268 return;
269 #endif
271 TestAddObserver observer(message_center());
273 TestDelegate* delegate;
275 // Add a non-progress notification and update it while the message center
276 // is visible.
277 Notification notification = CreateTestNotification("n", &delegate);
278 manager()->Add(notification, profile());
279 const std::string notification_id =
280 manager()->GetMessageCenterNotificationIdForTest("n", profile());
281 message_center()->ClickOnNotification(notification_id);
282 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
283 observer.reset_logs();
284 notification.set_title(base::ASCIIToUTF16("title2"));
285 manager()->Update(notification, profile());
287 // Expect that the notification update is not done.
288 EXPECT_EQ("", observer.log(notification_id));
290 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
291 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
292 observer.log(notification_id));
294 delegate->Release();
297 IN_PROC_BROWSER_TEST_F(
298 MessageCenterNotificationsTest,
299 UpdateNonProgressToProgressNotificationWhenCenterVisible) {
300 #if defined(OS_WIN) && defined(USE_ASH)
301 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
302 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kAshBrowserTests))
304 return;
305 #endif
307 TestAddObserver observer(message_center());
309 TestDelegate* delegate;
311 // Add a non-progress notification and change the type to progress while the
312 // message center is visible.
313 Notification notification = CreateTestNotification("n", &delegate);
314 manager()->Add(notification, profile());
315 const std::string notification_id =
316 manager()->GetMessageCenterNotificationIdForTest("n", profile());
317 message_center()->ClickOnNotification(notification_id);
318 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
319 observer.reset_logs();
320 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
321 manager()->Update(notification, profile());
323 // Expect that the notification update is not done.
324 EXPECT_EQ("", observer.log(notification_id));
326 message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
327 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
328 observer.log(notification_id));
330 delegate->Release();
333 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
334 UpdateProgressNotificationWhenCenterVisible) {
335 #if defined(OS_WIN) && defined(USE_ASH)
336 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
337 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
338 switches::kAshBrowserTests))
339 return;
340 #endif
342 TestAddObserver observer(message_center());
344 TestDelegate* delegate;
346 // Add a progress notification and update it while the message center
347 // is visible.
348 Notification notification = CreateTestNotification("n", &delegate);
349 notification.set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
350 manager()->Add(notification, profile());
351 const std::string notification_id =
352 manager()->GetMessageCenterNotificationIdForTest("n", profile());
353 message_center()->ClickOnNotification(notification_id);
354 message_center()->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
355 observer.reset_logs();
356 notification.set_progress(50);
357 manager()->Update(notification, profile());
359 // Expect that the progress notification update is performed.
360 EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
361 observer.log(notification_id));
363 delegate->Release();
366 #endif // !defined(OS_MACOSX)