web_notification_tray_browsertest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/notification.h" 16#include "chrome/browser/notifications/notification_delegate.h" 17#include "chrome/browser/notifications/notification_ui_manager.h" 18#include "chrome/browser/ui/browser.h" 19#include "chrome/test/base/in_process_browser_test.h" 20#include "content/public/test/test_utils.h" 21#include "ui/message_center/message_center_style.h" 22#include "ui/message_center/message_center_tray.h" 23#include "ui/message_center/notification_list.h" 24#include "ui/message_center/notification_types.h" 25#include "ui/message_center/views/message_center_bubble.h" 26#include "ui/message_center/views/message_popup_collection.h" 27#include "ui/views/controls/label.h" 28#include "ui/views/layout/fill_layout.h" 29#include "ui/views/view.h" 30#include "ui/views/widget/widget.h" 31 32namespace message_center { 33 34namespace { 35 36class WebNotificationTrayTest : public InProcessBrowserTest { 37 public: 38 WebNotificationTrayTest() {} 39 virtual ~WebNotificationTrayTest() {} 40 41 virtual void CleanUpOnMainThread() OVERRIDE { 42 message_center::MessageCenter::Get()->RemoveAllNotifications(false); 43 } 44 45 protected: 46 class TestNotificationDelegate : public ::NotificationDelegate { 47 public: 48 explicit TestNotificationDelegate(std::string id) : id_(id) {} 49 virtual void Display() {} 50 virtual void Error() {} 51 virtual void Close(bool by_user) {} 52 virtual void Click() {} 53 virtual std::string id() const { return id_; } 54 virtual content::RenderViewHost* GetRenderViewHost() const { return NULL; } 55 56 private: 57 std::string id_; 58 }; 59 60 void AddNotification(const std::string& id, const std::string& replace_id) { 61 ::Notification notification(GURL("chrome-extension://abbccedd"), 62 GURL(), 63 ASCIIToUTF16("Test Web Notification"), 64 ASCIIToUTF16("Notification message body."), 65 WebKit::WebTextDirectionDefault, 66 string16(), 67 ASCIIToUTF16(replace_id), 68 new TestNotificationDelegate(id)); 69 70 g_browser_process->notification_ui_manager()->Add( 71 notification, browser()->profile()); 72 } 73 74 void UpdateNotification(const std::string& replace_id, 75 const std::string& new_id) { 76 ::Notification notification(GURL("chrome-extension://abbccedd"), 77 GURL(""), 78 ASCIIToUTF16("Updated Web Notification"), 79 ASCIIToUTF16("Updated message body."), 80 WebKit::WebTextDirectionDefault, 81 string16(), 82 ASCIIToUTF16(replace_id), 83 new TestNotificationDelegate(new_id)); 84 85 g_browser_process->notification_ui_manager()->Add( 86 notification, browser()->profile()); 87 } 88 89 void RemoveNotification(const std::string& id) { 90 g_browser_process->notification_ui_manager()->CancelById(id); 91 } 92 93 private: 94 DISALLOW_COPY_AND_ASSIGN(WebNotificationTrayTest); 95}; 96 97} // namespace 98 99// TODO(dewittj): More exhaustive testing. 100IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotifications) { 101 message_center::MessageCenter* message_center = 102 message_center::MessageCenter::Get(); 103 104 // Add a notification. 105 AddNotification("test_id1", "replace_id1"); 106 EXPECT_EQ(1u, message_center->NotificationCount()); 107 EXPECT_TRUE(message_center->HasNotification("test_id1")); 108 EXPECT_FALSE(message_center->HasNotification("test_id2")); 109 AddNotification("test_id2", "replace_id2"); 110 AddNotification("test_id2", "replace_id2"); 111 EXPECT_EQ(2u, message_center->NotificationCount()); 112 EXPECT_TRUE(message_center->HasNotification("test_id2")); 113 114 // Ensure that updating a notification does not affect the count. 115 UpdateNotification("replace_id2", "test_id3"); 116 UpdateNotification("replace_id2", "test_id3"); 117 EXPECT_EQ(2u, message_center->NotificationCount()); 118 EXPECT_FALSE(message_center->HasNotification("test_id2")); 119 120 // Ensure that Removing the first notification removes it from the tray. 121 RemoveNotification("test_id1"); 122 EXPECT_FALSE(message_center->HasNotification("test_id1")); 123 EXPECT_EQ(1u, message_center->NotificationCount()); 124 125 // Remove the remaining notification. 126 RemoveNotification("test_id3"); 127 EXPECT_EQ(0u, message_center->NotificationCount()); 128 EXPECT_FALSE(message_center->HasNotification("test_id3")); 129} 130 131IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { 132 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 133 tray->message_center(); 134 135 // Adding a notification should show the popup bubble. 136 AddNotification("test_id1", "replace_id1"); 137 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 138 139 // Updating a notification should not hide the popup bubble. 140 AddNotification("test_id2", "replace_id2"); 141 UpdateNotification("replace_id2", "test_id3"); 142 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 143 144 // Removing the first notification should not hide the popup bubble. 145 RemoveNotification("test_id1"); 146 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 147 148 // Removing the visible notification should hide the popup bubble. 149 RemoveNotification("test_id3"); 150 EXPECT_FALSE(tray->message_center_tray_->popups_visible()); 151} 152 153using message_center::NotificationList; 154 155// Flaky, see http://crbug.com/222500 . 156IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, 157 DISABLED_ManyMessageCenterNotifications) { 158 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 159 message_center::MessageCenter* message_center = tray->message_center(); 160 161 // Add the max visible notifications +1, ensure the correct visible number. 162 size_t notifications_to_add = kMaxVisibleMessageCenterNotifications + 1; 163 for (size_t i = 0; i < notifications_to_add; ++i) { 164 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i)); 165 std::string replace_id = 166 base::StringPrintf("replace_id%d", static_cast<int>(i)); 167 AddNotification(id, replace_id); 168 } 169 bool shown = tray->message_center_tray_->ShowMessageCenterBubble(); 170 EXPECT_TRUE(shown); 171 content::RunAllPendingInMessageLoop(); 172 EXPECT_TRUE(tray->message_center_bubble_.get() != NULL); 173 EXPECT_EQ(notifications_to_add, message_center->NotificationCount()); 174 EXPECT_EQ(kMaxVisibleMessageCenterNotifications, 175 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); 176} 177 178IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { 179 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); 180 message_center::MessageCenter* message_center = tray->message_center(); 181 182 // Add the max visible popup notifications +1, ensure the correct num visible. 183 size_t notifications_to_add = kMaxVisiblePopupNotifications + 1; 184 for (size_t i = 0; i < notifications_to_add; ++i) { 185 std::string id = base::StringPrintf("test_id%d", static_cast<int>(i)); 186 std::string replace_id = 187 base::StringPrintf("replace_id%d", static_cast<int>(i)); 188 AddNotification(id, replace_id); 189 } 190 // Hide and reshow the bubble so that it is updated immediately, not delayed. 191 tray->message_center_tray_->HidePopupBubble(); 192 tray->message_center_tray_->ShowPopupBubble(); 193 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); 194 EXPECT_EQ(notifications_to_add, message_center->NotificationCount()); 195 NotificationList::PopupNotifications popups = 196 message_center->GetPopupNotifications(); 197 EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size()); 198} 199 200} // namespace message_center 201