Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / message_center_notifications_browsertest.cc
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.
4
5 #include <map>
6 #include <string>
7
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"
24
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);
30   }
31
32   ~TestAddObserver() override { message_center_->RemoveObserver(this); }
33
34   void OnNotificationAdded(const std::string& id) override {
35     std::string log = logs_[id];
36     if (log != "")
37       log += "_";
38     logs_[id] = log + "add-" + id;
39   }
40
41   void OnNotificationUpdated(const std::string& id) override {
42     std::string log = logs_[id];
43     if (log != "")
44       log += "_";
45     logs_[id] = log + "update-" + id;
46   }
47
48   const std::string log(const std::string& id) { return logs_[id]; }
49   void reset_logs() { logs_.clear(); }
50
51  private:
52   std::map<std::string, std::string> logs_;
53   message_center::MessageCenter* message_center_;
54 };
55
56 class MessageCenterNotificationsTest : public InProcessBrowserTest {
57  public:
58   MessageCenterNotificationsTest() {}
59
60   MessageCenterNotificationManager* manager() {
61     return static_cast<MessageCenterNotificationManager*>(
62         g_browser_process->notification_ui_manager());
63   }
64
65   message_center::MessageCenter* message_center() {
66     return g_browser_process->message_center();
67   }
68
69   Profile* profile() { return browser()->profile(); }
70
71   class TestDelegate : public NotificationDelegate {
72    public:
73     explicit TestDelegate(const std::string& id) : id_(id) {}
74
75     void Display() override { log_ += "Display_"; }
76     void Close(bool by_user) override {
77       log_ += "Close_";
78       log_ += ( by_user ? "by_user_" : "programmatically_");
79     }
80     void Click() override { log_ += "Click_"; }
81     void ButtonClick(int button_index) override {
82       log_ += "ButtonClick_";
83       log_ += base::IntToString(button_index) + "_";
84     }
85     std::string id() const override { return id_; }
86
87     const std::string& log() { return log_; }
88
89    private:
90     ~TestDelegate() override {}
91     std::string id_;
92     std::string log_;
93
94     DISALLOW_COPY_AND_ASSIGN(TestDelegate);
95   };
96
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();
103     }
104
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                         base::UTF8ToUTF16("REPLACE-ME"),
111                         new_delegate);
112   }
113
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();
120     }
121
122     message_center::RichNotificationData data;
123
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                         blink::WebTextDirectionDefault,
130                         message_center::NotifierId(
131                             message_center::NotifierId::APPLICATION,
132                             "extension_id"),
133                         base::UTF8ToUTF16("chrome-test://testing/"),
134                         base::UTF8ToUTF16("REPLACE-ME"),
135                         data,
136                         new_delegate);
137   }
138 };
139
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)
143
144 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) {
145   EXPECT_TRUE(manager());
146   EXPECT_TRUE(message_center());
147 }
148
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))
153     return;
154 #endif
155
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());
165 }
166
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))
171     return;
172 #endif
173
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());
181   delegate->Release();
182 }
183
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))
188     return;
189 #endif
190
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());
198   delegate->Release();
199 }
200
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))
206     return;
207 #endif
208
209   TestDelegate* delegate;
210   manager()->Add(CreateTestNotification("n", &delegate), profile());
211   TestDelegate* delegate2;
212   manager()->Add(CreateRichTestNotification("n", &delegate2), profile());
213
214   manager()->CancelById("n", NotificationUIManager::GetProfileID(profile()));
215   EXPECT_EQ("Display_", delegate->log());
216   EXPECT_EQ("Close_programmatically_", delegate2->log());
217
218   delegate->Release();
219   delegate2->Release();
220 }
221
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))
226     return;
227 #endif
228
229   TestAddObserver observer(message_center());
230
231   TestDelegate* delegate;
232   TestDelegate* delegate2;
233
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());
241
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",
245                                id_n.c_str(),
246                                id_n.c_str(),
247                                id_n.c_str()),
248             observer.log(id_n));
249
250   message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
251
252   EXPECT_EQ(base::StringPrintf("add-%s", id_n2.c_str()), observer.log(id_n2));
253
254   delegate->Release();
255   delegate2->Release();
256 }
257
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))
263     return;
264 #endif
265
266   TestAddObserver observer(message_center());
267
268   TestDelegate* delegate;
269
270   // Add a non-progress notification and update it while the message center
271   // is visible.
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());
281
282   // Expect that the notification update is not done.
283   EXPECT_EQ("", observer.log(notification_id));
284
285   message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
286   EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
287             observer.log(notification_id));
288
289   delegate->Release();
290 }
291
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))
298     return;
299 #endif
300
301   TestAddObserver observer(message_center());
302
303   TestDelegate* delegate;
304
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());
316
317   // Expect that the notification update is not done.
318   EXPECT_EQ("", observer.log(notification_id));
319
320   message_center()->SetVisibility(message_center::VISIBILITY_TRANSIENT);
321   EXPECT_EQ(base::StringPrintf("update-%s", notification_id.c_str()),
322             observer.log(notification_id));
323
324   delegate->Release();
325 }
326
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))
332     return;
333 #endif
334
335   TestAddObserver observer(message_center());
336
337   TestDelegate* delegate;
338
339   // Add a progress notification and update it while the message center
340   // is visible.
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());
351
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));
355
356   delegate->Release();
357 }
358
359 #endif  // !defined(OS_MACOSX)