OLD | NEW |
(Empty) | |
| 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(const 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 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 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()); |
| 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 IN_PROC_BROWSER_TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { |
| 172 scoped_ptr<WebNotificationTray> tray(new WebNotificationTray()); |
| 173 message_center::MessageCenter* message_center = tray->message_center(); |
| 174 |
| 175 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 176 size_t notifications_to_add = kMaxVisiblePopupNotifications + 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 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 184 tray->message_center_tray_->HidePopupBubble(); |
| 185 tray->message_center_tray_->ShowPopupBubble(); |
| 186 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); |
| 187 EXPECT_EQ(notifications_to_add, message_center->NotificationCount()); |
| 188 NotificationList::PopupNotifications popups = |
| 189 message_center->GetPopupNotifications(); |
| 190 EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size()); |
| 191 } |
| 192 |
| 193 } // namespace message_center |
OLD | NEW |