[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / message_center / web_notification_tray_browsertest.cc
blob1a1889c9f6b786ed734a06bad6edd09358190094
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 virtual ~WebNotificationTrayTest() {}
42 virtual void CleanUpOnMainThread() 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 virtual void Display() OVERRIDE {}
51 virtual void Error() OVERRIDE {}
52 virtual void Close(bool by_user) OVERRIDE {}
53 virtual void Click() OVERRIDE {}
54 virtual std::string id() const OVERRIDE { return id_; }
55 virtual content::WebContents* GetWebContents() const OVERRIDE {
56 return NULL;
59 private:
60 virtual ~TestNotificationDelegate() {}
62 std::string id_;
65 void AddNotification(const std::string& id, const std::string& replace_id) {
66 ::Notification notification(GURL("chrome-extension://abbccedd"),
67 GURL(),
68 base::ASCIIToUTF16("Test Web Notification"),
69 base::ASCIIToUTF16(
70 "Notification message body."),
71 blink::WebTextDirectionDefault,
72 base::string16(),
73 base::ASCIIToUTF16(replace_id),
74 new TestNotificationDelegate(id));
76 g_browser_process->notification_ui_manager()->Add(
77 notification, browser()->profile());
80 void UpdateNotification(const std::string& replace_id,
81 const std::string& new_id) {
82 ::Notification notification(GURL("chrome-extension://abbccedd"),
83 GURL(""),
84 base::ASCIIToUTF16("Updated Web Notification"),
85 base::ASCIIToUTF16("Updated message body."),
86 blink::WebTextDirectionDefault,
87 base::string16(),
88 base::ASCIIToUTF16(replace_id),
89 new TestNotificationDelegate(new_id));
91 g_browser_process->notification_ui_manager()->Add(
92 notification, browser()->profile());
95 void RemoveNotification(const std::string& id) {
96 g_browser_process->notification_ui_manager()->CancelById(id);
99 private:
100 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest);
103 } // namespace
106 // TODO(dewittj): More exhaustive testing.
107 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotifications) {
108 message_center::MessageCenter* message_center =
109 message_center::MessageCenter::Get();
111 // Add a notification.
112 AddNotification("test_id1", "replace_id1");
113 EXPECT_EQ(1u, message_center->NotificationCount());
114 EXPECT_TRUE(message_center->HasNotification("test_id1"));
115 EXPECT_FALSE(message_center->HasNotification("test_id2"));
116 AddNotification("test_id2", "replace_id2");
117 AddNotification("test_id2", "replace_id2");
118 EXPECT_EQ(2u, message_center->NotificationCount());
119 EXPECT_TRUE(message_center->HasNotification("test_id2"));
121 // Ensure that updating a notification does not affect the count.
122 UpdateNotification("replace_id2", "test_id3");
123 UpdateNotification("replace_id2", "test_id3");
124 EXPECT_EQ(2u, message_center->NotificationCount());
125 EXPECT_FALSE(message_center->HasNotification("test_id2"));
127 // Ensure that Removing the first notification removes it from the tray.
128 RemoveNotification("test_id1");
129 EXPECT_FALSE(message_center->HasNotification("test_id1"));
130 EXPECT_EQ(1u, message_center->NotificationCount());
132 // Remove the remaining notification.
133 RemoveNotification("test_id3");
134 EXPECT_EQ(0u, message_center->NotificationCount());
135 EXPECT_FALSE(message_center->HasNotification("test_id3"));
138 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) {
139 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
140 tray->message_center();
142 // Adding a notification should show the popup bubble.
143 AddNotification("test_id1", "replace_id1");
144 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
146 // Updating a notification should not hide the popup bubble.
147 AddNotification("test_id2", "replace_id2");
148 UpdateNotification("replace_id2", "test_id3");
149 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
151 // Removing the first notification should not hide the popup bubble.
152 RemoveNotification("test_id1");
153 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
155 // Removing the visible notification should hide the popup bubble.
156 RemoveNotification("test_id3");
157 EXPECT_FALSE(tray->message_center_tray_->popups_visible());
160 using message_center::NotificationList;
162 // Flaky, see http://crbug.com/222500 .
163 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest,
164 DISABLED_ManyMessageCenterNotifications) {
165 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
166 message_center::MessageCenter* message_center = tray->message_center();
168 // Add the max visible notifications +1, ensure the correct visible number.
169 size_t notifications_to_add = kMaxVisibleMessageCenterNotifications + 1;
170 for (size_t i = 0; i < notifications_to_add; ++i) {
171 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i));
172 std::string replace_id =
173 base::StringPrintf("replace_id%d", static_cast<int>(i));
174 AddNotification(id, replace_id);
176 bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
177 EXPECT_TRUE(shown);
178 content::RunAllPendingInMessageLoop();
179 EXPECT_TRUE(tray->message_center_delegate_ != NULL);
180 EXPECT_EQ(notifications_to_add, message_center->NotificationCount());
181 EXPECT_EQ(kMaxVisibleMessageCenterNotifications,
182 tray->message_center_delegate_->NumMessageViewsForTest());
185 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, ManyPopupNotifications) {
186 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray(NULL));
187 message_center::MessageCenter* message_center = tray->message_center();
189 // Add the max visible popup notifications +1, ensure the correct num visible.
190 size_t notifications_to_add = kMaxVisiblePopupNotifications + 1;
191 for (size_t i = 0; i < notifications_to_add; ++i) {
192 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i));
193 std::string replace_id =
194 base::StringPrintf("replace_id%d", static_cast<int>(i));
195 AddNotification(id, replace_id);
197 // Hide and reshow the bubble so that it is updated immediately, not delayed.
198 tray->message_center_tray_->HidePopupBubble();
199 tray->message_center_tray_->ShowPopupBubble();
200 EXPECT_TRUE(tray->message_center_tray_->popups_visible());
201 EXPECT_EQ(notifications_to_add, message_center->NotificationCount());
202 NotificationList::PopupNotifications popups =
203 message_center->GetPopupNotifications();
204 EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size());
207 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest,
208 ManuallyCloseMessageCenter) {
209 NotificationUIManager* manager = g_browser_process->notification_ui_manager();
210 MessageCenterNotificationManager* mc_manager =
211 static_cast<MessageCenterNotificationManager*>(manager);
213 WebNotificationTray* tray =
214 static_cast<WebNotificationTray*>(mc_manager->tray_.get());
215 ASSERT_TRUE(NULL != tray);
217 message_center::MessageCenter* message_center = tray->message_center();
219 bool shown = tray->message_center_tray_->ShowMessageCenterBubble();
220 EXPECT_TRUE(shown);
221 EXPECT_TRUE(message_center->IsMessageCenterVisible());
223 mc_manager->EnsureMessageCenterClosed();
225 EXPECT_FALSE(message_center->IsMessageCenterVisible());
226 if (NULL != tray->message_center_delegate_)
227 EXPECT_TRUE(tray->message_center_delegate_->GetWidget()->IsClosed());
230 } // namespace message_center