Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / message_center / web_notification_tray_browsertest.cc
1 // Copyright 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 "chrome/browser/ui/views/message_center/web_notification_tray.h"
6
7 #include <set>
8
9 #include "ash/root_window_controller.h"
10 #include "ash/system/status_area_widget.h"
11 #include "ash/system/tray/system_tray_item.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_delegate.h"
18 #include "chrome/browser/notifications/notification_ui_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "content/public/test/test_utils.h"
22 #include "ui/message_center/message_center_style.h"
23 #include "ui/message_center/message_center_tray.h"
24 #include "ui/message_center/notification_list.h"
25 #include "ui/message_center/notification_types.h"
26 #include "ui/message_center/views/message_center_bubble.h"
27 #include "ui/message_center/views/message_popup_collection.h"
28 #include "ui/views/controls/label.h"
29 #include "ui/views/layout/fill_layout.h"
30 #include "ui/views/view.h"
31 #include "ui/views/widget/widget.h"
32
33 namespace message_center {
34
35 namespace {
36
37 class WebNotificationTrayTest : public InProcessBrowserTest {
38  public:
39   WebNotificationTrayTest() {}
40   ~WebNotificationTrayTest() override {}
41
42   void TearDownOnMainThread() override {
43     message_center::MessageCenter::Get()->RemoveAllNotifications(false);
44   }
45
46  protected:
47   class TestNotificationDelegate : public ::NotificationDelegate {
48    public:
49     explicit TestNotificationDelegate(std::string id) : id_(id) {}
50     std::string id() const override { return id_; }
51
52    private:
53     ~TestNotificationDelegate() override {}
54
55     std::string id_;
56   };
57
58   void AddNotification(const std::string& delegate_id,
59                        const std::string& replace_id) {
60     ::Notification notification(
61         GURL("chrome-extension://abbccedd"),
62         base::ASCIIToUTF16("Test Web Notification"),
63         base::ASCIIToUTF16("Notification message body."),
64         gfx::Image(),
65         base::string16(),
66         base::ASCIIToUTF16(replace_id),
67         new TestNotificationDelegate(delegate_id));
68
69     g_browser_process->notification_ui_manager()->Add(notification,
70                                                       browser()->profile());
71   }
72
73   std::string FindNotificationIdByDelegateId(const std::string& delegate_id) {
74     const ::Notification* notification =
75         g_browser_process->notification_ui_manager()->FindById(
76             delegate_id,
77             NotificationUIManager::GetProfileID(browser()->profile()));
78     EXPECT_TRUE(notification);
79     return notification->id();
80   }
81
82   void UpdateNotification(const std::string& replace_id,
83                           const std::string& new_id) {
84     ::Notification notification(GURL("chrome-extension://abbccedd"),
85                                 base::ASCIIToUTF16("Updated Web Notification"),
86                                 base::ASCIIToUTF16("Updated message body."),
87                                 gfx::Image(),
88                                 base::string16(),
89                                 base::ASCIIToUTF16(replace_id),
90                                 new TestNotificationDelegate(new_id));
91
92     g_browser_process->notification_ui_manager()->Add(notification,
93                                                       browser()->profile());
94   }
95
96   void RemoveNotification(const std::string& id) {
97     g_browser_process->notification_ui_manager()->CancelById(
98         id, NotificationUIManager::GetProfileID(browser()->profile()));
99   }
100
101   bool HasNotification(message_center::MessageCenter* message_center,
102                        const std::string& id) {
103     return message_center->FindVisibleNotificationById(id) != NULL;
104   }
105
106  private:
107   DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest);
108 };
109
110 }  // namespace
111
112
113 // TODO(dewittj): More exhaustive testing.
114 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotifications) {
115   message_center::MessageCenter* message_center =
116       message_center::MessageCenter::Get();
117
118   // Add a notification.
119   AddNotification("test_id1", "replace_id1");
120   EXPECT_EQ(1u, message_center->NotificationCount());
121   EXPECT_TRUE(HasNotification(message_center,
122                               FindNotificationIdByDelegateId("test_id1")));
123   EXPECT_FALSE(HasNotification(message_center, "test_id2"));
124   AddNotification("test_id2", "replace_id2");
125   AddNotification("test_id2", "replace_id2");
126   EXPECT_EQ(2u, message_center->NotificationCount());
127   EXPECT_TRUE(HasNotification(message_center,
128                               FindNotificationIdByDelegateId("test_id1")));
129
130   // Ensure that updating a notification does not affect the count.
131   UpdateNotification("replace_id2", "test_id3");
132   UpdateNotification("replace_id2", "test_id3");
133   EXPECT_EQ(2u, message_center->NotificationCount());
134   EXPECT_FALSE(HasNotification(message_center, "test_id2"));
135
136   // Ensure that Removing the first notification removes it from the tray.
137   RemoveNotification("test_id1");
138   EXPECT_FALSE(HasNotification(message_center, "test_id1"));
139   EXPECT_EQ(1u, message_center->NotificationCount());
140
141   // Remove the remaining notification.
142   RemoveNotification("test_id3");
143   EXPECT_EQ(0u, message_center->NotificationCount());
144   EXPECT_FALSE(HasNotification(message_center, "test_id1"));
145 }
146
147 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) {
148   scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
149   tray->message_center();
150
151   // Adding a notification should show the popup bubble.
152   AddNotification("test_id1", "replace_id1");
153   EXPECT_TRUE(tray->message_center_tray_->popups_visible());
154
155   // Updating a notification should not hide the popup bubble.
156   AddNotification("test_id2", "replace_id2");
157   UpdateNotification("replace_id2", "test_id3");
158   EXPECT_TRUE(tray->message_center_tray_->popups_visible());
159
160   // Removing the first notification should not hide the popup bubble.
161   RemoveNotification("test_id1");
162   EXPECT_TRUE(tray->message_center_tray_->popups_visible());
163
164   // Removing the visible notification should hide the popup bubble.
165   RemoveNotification("test_id3");
166   EXPECT_FALSE(tray->message_center_tray_->popups_visible());
167 }
168
169 using message_center::NotificationList;
170
171 // Flaky, see http://crbug.com/222500 .
172 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest,
173                        DISABLED_ManyMessageCenterNotifications) {
174   scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
175   message_center::MessageCenter* message_center = tray->message_center();
176
177   // Add the max visible notifications +1, ensure the correct visible number.
178   size_t notifications_to_add = kMaxVisibleMessageCenterNotifications + 1;
179   for (size_t i = 0; i < notifications_to_add; ++i) {
180     std::string id = base::StringPrintf("test_id%d", static_cast<int>(i));
181     std::string replace_id =
182         base::StringPrintf("replace_id%d", static_cast<int>(i));
183     AddNotification(id, replace_id);
184   }
185   bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
186   EXPECT_TRUE(shown);
187   content::RunAllPendingInMessageLoop();
188   EXPECT_TRUE(tray->message_center_delegate_ != NULL);
189   EXPECT_EQ(notifications_to_add, message_center->NotificationCount());
190   EXPECT_EQ(kMaxVisibleMessageCenterNotifications,
191             tray->message_center_delegate_->NumMessageViewsForTest());
192 }
193
194 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, ManyPopupNotifications) {
195   scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
196   message_center::MessageCenter* message_center = tray->message_center();
197
198   // Add the max visible popup notifications +1, ensure the correct num visible.
199   size_t notifications_to_add = kMaxVisiblePopupNotifications + 1;
200   for (size_t i = 0; i < notifications_to_add; ++i) {
201     std::string id = base::StringPrintf("test_id%d", static_cast<int>(i));
202     std::string replace_id =
203         base::StringPrintf("replace_id%d", static_cast<int>(i));
204     AddNotification(id, replace_id);
205   }
206   // Hide and reshow the bubble so that it is updated immediately, not delayed.
207   tray->message_center_tray_->HidePopupBubble();
208   tray->message_center_tray_->ShowPopupBubble();
209   EXPECT_TRUE(tray->message_center_tray_->popups_visible());
210   EXPECT_EQ(notifications_to_add, message_center->NotificationCount());
211   NotificationList::PopupNotifications popups =
212       message_center->GetPopupNotifications();
213   EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size());
214 }
215
216 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest,
217                        ManuallyCloseMessageCenter) {
218   NotificationUIManager* manager = g_browser_process->notification_ui_manager();
219   MessageCenterNotificationManager* mc_manager =
220       static_cast<MessageCenterNotificationManager*>(manager);
221
222   WebNotificationTray* tray =
223       static_cast<WebNotificationTray*>(mc_manager->tray_.get());
224   ASSERT_TRUE(NULL != tray);
225
226   message_center::MessageCenter* message_center = tray->message_center();
227
228   bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
229   EXPECT_TRUE(shown);
230   EXPECT_TRUE(message_center->IsMessageCenterVisible());
231
232   mc_manager->EnsureMessageCenterClosed();
233
234   EXPECT_FALSE(message_center->IsMessageCenterVisible());
235   if (NULL != tray->message_center_delegate_)
236     EXPECT_TRUE(tray->message_center_delegate_->GetWidget()->IsClosed());
237 }
238
239 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
240 #define MAYBE_StatusIconBehavior DISABLED_StatusIconBehavior
241 #else
242 #define MAYBE_StatusIconBehavior StatusIconBehavior
243 #endif
244 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, MAYBE_StatusIconBehavior) {
245   scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
246
247   EXPECT_TRUE(tray->status_icon_ == NULL);
248   tray->OnMessageCenterTrayChanged();
249   base::RunLoop().RunUntilIdle();
250   EXPECT_TRUE(tray->status_icon_ == NULL);
251   AddNotification("test_id", "replace_id");
252   base::RunLoop().RunUntilIdle();
253   EXPECT_TRUE(tray->status_icon_ != NULL);
254   RemoveNotification("test_id");
255   base::RunLoop().RunUntilIdle();
256   EXPECT_TRUE(tray->status_icon_ == NULL);
257 }
258 }  // namespace message_center