web_notification_tray_browsertest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 33namespace message_center { 34 35namespace { 36 37class WebNotificationTrayTest : public InProcessBrowserTest { 38 public: 39 WebNotificationTrayTest() {} 40 virtual ~WebNotificationTrayTest() {} 41 42 virtual void CleanUpOnMainThread() 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 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::RenderViewHost* GetRenderViewHost() const OVERRIDE { 56 return NULL; 57 } 58 59 private: 60 virtual ~TestNotificationDelegate() {} 61 62 std::string id_; 63 }; 64 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)); 75 76 g_browser_process->notification_ui_manager()->Add( 77 notification, browser()->profile()); 78 } 79 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)); 90 91 g_browser_process->notification_ui_manager()->Add( 92 notification, browser()->profile()); 93 } 94 95 void RemoveNotification(const std::string& id) { 96 g_browser_process->notification_ui_manager()->CancelById(id); 97 } 98 99 private: 100 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest); 101}; 102 103} // namespace 104 105 106#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) 107// TODO(erg): linux_aura bringup: http://crbug.com/163931 108#define MAYBE_WebNotifications DISABLED_WebNotifications 109#else 110#define MAYBE_WebNotifications WebNotifications 111#endif 112 113// TODO(dewittj): More exhaustive testing. 114IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, MAYBE_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(message_center->HasNotification("test_id1")); 122 EXPECT_FALSE(message_center->HasNotification("test_id2")); 123 AddNotification("test_id2", "replace_id2"); 124 AddNotification("test_id2", "replace_id2"); 125 EXPECT_EQ(2u, message_center->NotificationCount()); 126 EXPECT_TRUE(message_center->HasNotification("test_id2")); 127 128 // Ensure that updating a notification does not affect the count. 129 UpdateNotification("replace_id2", "test_id3"); 130 UpdateNotification("replace_id2", "test_id3"); 131 EXPECT_EQ(2u, message_center->NotificationCount()); 132 EXPECT_FALSE(message_center->HasNotification("test_id2")); 133 134 // Ensure that Removing the first notification removes it from the tray. 135 RemoveNotification("test_id1"); 136 EXPECT_FALSE(message_center->HasNotification("test_id1")); 137 EXPECT_EQ(1u, message_center->NotificationCount()); 138 139 // Remove the remaining notification. 140 RemoveNotification("test_id3"); 141 EXPECT_EQ(0u, message_center->NotificationCount()); 142 EXPECT_FALSE(message_center->HasNotification("test_id3")); 143} 144 145IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { 146 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 147 tray->message_center(); 148 149 // Adding a notification should show the popup bubble. 150 AddNotification("test_id1", "replace_id1"); 151 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 152 153 // Updating a notification should not hide the popup bubble. 154 AddNotification("test_id2", "replace_id2"); 155 UpdateNotification("replace_id2", "test_id3"); 156 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 157 158 // Removing the first notification should not hide the popup bubble. 159 RemoveNotification("test_id1"); 160 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 161 162 // Removing the visible notification should hide the popup bubble. 163 RemoveNotification("test_id3"); 164 EXPECT_FALSE(tray->message_center_tray_->popups_visible()); 165} 166 167using message_center::NotificationList; 168 169// Flaky, see http://crbug.com/222500 . 170IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, 171 DISABLED_ManyMessageCenterNotifications) { 172 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 173 message_center::MessageCenter* message_center = tray->message_center(); 174 175 // Add the max visible notifications +1, ensure the correct visible number. 176 size_t notifications_to_add = kMaxVisibleMessageCenterNotifications + 1; 177 for (size_t i = 0; i < notifications_to_add; ++i) { 178 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i)); 179 std::string replace_id = 180 base::StringPrintf("replace_id%d", static_cast<int>(i)); 181 AddNotification(id, replace_id); 182 } 183 bool shown = tray->message_center_tray_->ShowMessageCenterBubble(); 184 EXPECT_TRUE(shown); 185 content::RunAllPendingInMessageLoop(); 186 EXPECT_TRUE(tray->message_center_delegate_ != NULL); 187 EXPECT_EQ(notifications_to_add, message_center->NotificationCount()); 188 EXPECT_EQ(kMaxVisibleMessageCenterNotifications, 189 tray->message_center_delegate_->NumMessageViewsForTest()); 190} 191 192#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) 193// TODO(erg): linux_aura bringup: http://crbug.com/163931 194#define MAYBE_ManyPopupNotifications DISABLED_ManyPopupNotifications 195#else 196#define MAYBE_ManyPopupNotifications ManyPopupNotifications 197#endif 198 199IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, MAYBE_ManyPopupNotifications) { 200 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 201 message_center::MessageCenter* message_center = tray->message_center(); 202 203 // Add the max visible popup notifications +1, ensure the correct num visible. 204 size_t notifications_to_add = kMaxVisiblePopupNotifications + 1; 205 for (size_t i = 0; i < notifications_to_add; ++i) { 206 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i)); 207 std::string replace_id = 208 base::StringPrintf("replace_id%d", static_cast<int>(i)); 209 AddNotification(id, replace_id); 210 } 211 // Hide and reshow the bubble so that it is updated immediately, not delayed. 212 tray->message_center_tray_->HidePopupBubble(); 213 tray->message_center_tray_->ShowPopupBubble(); 214 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 215 EXPECT_EQ(notifications_to_add, message_center->NotificationCount()); 216 NotificationList::PopupNotifications popups = 217 message_center->GetPopupNotifications(); 218 EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size()); 219} 220 221IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, 222 ManuallyCloseMessageCenter) { 223 NotificationUIManager* manager = g_browser_process->notification_ui_manager(); 224 ASSERT_TRUE(manager->DelegatesToMessageCenter()); 225 MessageCenterNotificationManager* mc_manager = 226 static_cast<MessageCenterNotificationManager*>(manager); 227 228 WebNotificationTray* tray = 229 static_cast<WebNotificationTray*>(mc_manager->tray_.get()); 230 ASSERT_TRUE(NULL != tray); 231 232 message_center::MessageCenter* message_center = tray->message_center(); 233 234 bool shown = tray->message_center_tray_->ShowMessageCenterBubble(); 235 EXPECT_TRUE(shown); 236 EXPECT_TRUE(message_center->IsMessageCenterVisible()); 237 238 mc_manager->EnsureMessageCenterClosed(); 239 240 EXPECT_FALSE(message_center->IsMessageCenterVisible()); 241 if (NULL != tray->message_center_delegate_) 242 EXPECT_TRUE(tray->message_center_delegate_->GetWidget()->IsClosed()); 243} 244 245} // namespace message_center 246