Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / message_center / web_notification_tray_browsertest.cc
blob4f8c0ce7443d0b3240b441b981e71ffc9b68cf7c
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.
5 #include "chrome/browser/ui/views/message_center/web_notification_tray.h"
7 #include <set>
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"
33 namespace message_center {
35 namespace {
37 class WebNotificationTrayTest : public InProcessBrowserTest {
38 public:
39 WebNotificationTrayTest() {}
40 ~WebNotificationTrayTest() override {}
42 void TearDownOnMainThread() override {
43 message_center::MessageCenter::Get()->RemoveAllNotifications(false);
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_; }
52 private:
53 ~TestNotificationDelegate() override {}
55 std::string id_;
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 replace_id,
67 new TestNotificationDelegate(delegate_id));
69 g_browser_process->notification_ui_manager()->Add(notification,
70 browser()->profile());
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();
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 replace_id,
90 new TestNotificationDelegate(new_id));
92 g_browser_process->notification_ui_manager()->Add(notification,
93 browser()->profile());
96 void RemoveNotification(const std::string& id) {
97 g_browser_process->notification_ui_manager()->CancelById(
98 id, NotificationUIManager::GetProfileID(browser()->profile()));
101 bool HasNotification(message_center::MessageCenter* message_center,
102 const std::string& id) {
103 return message_center->FindVisibleNotificationById(id) != NULL;
106 private:
107 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest);
110 } // namespace
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();
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")));
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"));
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());
141 // Remove the remaining notification.
142 RemoveNotification("test_id3");
143 EXPECT_EQ(0u, message_center->NotificationCount());
144 EXPECT_FALSE(HasNotification(message_center, "test_id1"));
147 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) {
148 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
149 tray->message_center();
151 // Adding a notification should show the popup bubble.
152 AddNotification("test_id1", "replace_id1");
153 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
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());
160 // Removing the first notification should not hide the popup bubble.
161 RemoveNotification("test_id1");
162 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
164 // Removing the visible notification should hide the popup bubble.
165 RemoveNotification("test_id3");
166 EXPECT_FALSE(tray->message_center_tray_->popups_visible());
169 using message_center::NotificationList;
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();
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);
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());
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();
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);
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());
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);
222 WebNotificationTray* tray =
223 static_cast<WebNotificationTray*>(mc_manager->tray_.get());
224 ASSERT_TRUE(NULL != tray);
226 message_center::MessageCenter* message_center = tray->message_center();
228 bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
229 EXPECT_TRUE(shown);
230 EXPECT_TRUE(message_center->IsMessageCenterVisible());
232 mc_manager->EnsureMessageCenterClosed();
234 EXPECT_FALSE(message_center->IsMessageCenterVisible());
235 if (NULL != tray->message_center_delegate_)
236 EXPECT_TRUE(tray->message_center_delegate_->GetWidget()->IsClosed());
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));
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);
258 } // namespace message_center