OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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_win.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/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/test/base/in_process_browser_test.h" |
| 15 #include "content/public/test/test_utils.h" |
| 16 #include "ui/message_center/message_center_bubble.h" |
| 17 #include "ui/message_center/message_center_tray.h" |
| 18 #include "ui/message_center/message_popup_bubble.h" |
| 19 #include "ui/message_center/notification_list.h" |
| 20 #include "ui/notifications/notification_types.h" |
| 21 #include "ui/views/controls/label.h" |
| 22 #include "ui/views/layout/fill_layout.h" |
| 23 #include "ui/views/view.h" |
| 24 #include "ui/views/widget/widget.h" |
| 25 |
| 26 namespace message_center { |
| 27 |
| 28 namespace { |
| 29 |
| 30 class TestDelegate : public message_center::MessageCenter::Delegate { |
| 31 public: |
| 32 explicit TestDelegate(message_center::MessageCenter* message_center) |
| 33 : message_center_(message_center) { |
| 34 message_center_->SetDelegate(this); |
| 35 } |
| 36 |
| 37 virtual ~TestDelegate() { |
| 38 message_center_->SetDelegate(NULL); |
| 39 message_center_->notification_list()->RemoveAllNotifications(); |
| 40 } |
| 41 |
| 42 // WebNotificationTray::Delegate overrides. |
| 43 virtual void NotificationRemoved( |
| 44 const std::string& notification_id) OVERRIDE { |
| 45 notification_ids_.erase(notification_id); |
| 46 } |
| 47 |
| 48 virtual void DisableNotificationsFromSource( |
| 49 const std::string& notification_id) OVERRIDE { |
| 50 } |
| 51 |
| 52 virtual void DisableExtension(const std::string& notification_id) OVERRIDE { } |
| 53 virtual void ShowSettings(const std::string& notification_id) OVERRIDE { } |
| 54 virtual void OnClicked(const std::string& notification_id) OVERRIDE { } |
| 55 virtual void ShowSettingsDialog(gfx::NativeView context) OVERRIDE { } |
| 56 |
| 57 |
| 58 void AddNotification(const std::string& id) { |
| 59 notification_ids_.insert(id); |
| 60 message_center_->AddNotification( |
| 61 ui::notifications::NOTIFICATION_TYPE_SIMPLE, |
| 62 id, |
| 63 ASCIIToUTF16("Test Web Notification"), |
| 64 ASCIIToUTF16("Notification message body."), |
| 65 ASCIIToUTF16("www.test.org"), |
| 66 "" /* extension id */, |
| 67 NULL /* optional_fields */); |
| 68 } |
| 69 |
| 70 void UpdateNotification(const std::string& old_id, |
| 71 const std::string& new_id) { |
| 72 notification_ids_.erase(old_id); |
| 73 notification_ids_.insert(new_id); |
| 74 message_center_->UpdateNotification( |
| 75 old_id, new_id, |
| 76 ASCIIToUTF16("Updated Web Notification"), |
| 77 ASCIIToUTF16("Updated message body."), |
| 78 NULL); |
| 79 } |
| 80 |
| 81 void RemoveNotification(const std::string& id) { |
| 82 message_center_->RemoveNotification(id); |
| 83 notification_ids_.erase(id); |
| 84 } |
| 85 |
| 86 bool HasNotificationId(const std::string& id) { |
| 87 return notification_ids_.find(id) != notification_ids_.end(); |
| 88 } |
| 89 |
| 90 private: |
| 91 std::set<std::string> notification_ids_; |
| 92 // Weak pointer. |
| 93 message_center::MessageCenter* message_center_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 96 }; |
| 97 |
| 98 } // namespace |
| 99 |
| 100 typedef InProcessBrowserTest WebNotificationTrayWinTest; |
| 101 |
| 102 // TODO(dewittj): More exhaustive testing. |
| 103 IN_PROC_BROWSER_TEST_F(WebNotificationTrayWinTest, WebNotifications) { |
| 104 scoped_ptr<WebNotificationTrayWin> tray(new WebNotificationTrayWin()); |
| 105 message_center::MessageCenter* message_center = tray->message_center(); |
| 106 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 107 |
| 108 // Add a notification. |
| 109 delegate->AddNotification("test_id1"); |
| 110 EXPECT_EQ(1u, message_center->NotificationCount()); |
| 111 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); |
| 112 EXPECT_FALSE( |
| 113 message_center->notification_list()->HasNotification("test_id2")); |
| 114 delegate->AddNotification("test_id2"); |
| 115 delegate->AddNotification("test_id2"); |
| 116 EXPECT_EQ(2u, message_center->NotificationCount()); |
| 117 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); |
| 118 |
| 119 // Ensure that updating a notification does not affect the count. |
| 120 delegate->UpdateNotification("test_id2", "test_id3"); |
| 121 delegate->UpdateNotification("test_id3", "test_id3"); |
| 122 EXPECT_EQ(2u, message_center->NotificationCount()); |
| 123 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); |
| 124 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 125 "test_id2")); |
| 126 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); |
| 127 |
| 128 // Ensure that Removing the first notification removes it from the tray. |
| 129 delegate->RemoveNotification("test_id1"); |
| 130 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); |
| 131 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 132 "test_id1")); |
| 133 EXPECT_EQ(1u, message_center->NotificationCount()); |
| 134 |
| 135 // Remove the remaining notification. |
| 136 delegate->RemoveNotification("test_id3"); |
| 137 EXPECT_EQ(0u, message_center->NotificationCount()); |
| 138 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 139 "test_id3")); |
| 140 } |
| 141 |
| 142 IN_PROC_BROWSER_TEST_F(WebNotificationTrayWinTest, WebNotificationPopupBubble) { |
| 143 scoped_ptr<WebNotificationTrayWin> tray(new WebNotificationTrayWin()); |
| 144 message_center::MessageCenter* message_center = tray->message_center(); |
| 145 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 146 |
| 147 // Adding a notification should show the popup bubble. |
| 148 delegate->AddNotification("test_id1"); |
| 149 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); |
| 150 |
| 151 // Updating a notification should not hide the popup bubble. |
| 152 delegate->AddNotification("test_id2"); |
| 153 delegate->UpdateNotification("test_id2", "test_id3"); |
| 154 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); |
| 155 |
| 156 // Removing the first notification should not hide the popup bubble. |
| 157 delegate->RemoveNotification("test_id1"); |
| 158 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); |
| 159 |
| 160 // Removing the visible notification should hide the popup bubble. |
| 161 delegate->RemoveNotification("test_id3"); |
| 162 EXPECT_FALSE(tray->message_center_tray_->popups_visible()); |
| 163 } |
| 164 |
| 165 using message_center::NotificationList; |
| 166 |
| 167 IN_PROC_BROWSER_TEST_F(WebNotificationTrayWinTest, |
| 168 ManyMessageCenterNotifications) { |
| 169 scoped_ptr<WebNotificationTrayWin> tray(new WebNotificationTrayWin()); |
| 170 message_center::MessageCenter* message_center = tray->message_center(); |
| 171 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 172 |
| 173 // Add the max visible notifications +1, ensure the correct visible number. |
| 174 size_t notifications_to_add = |
| 175 NotificationList::kMaxVisibleMessageCenterNotifications + 1; |
| 176 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 177 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 178 delegate->AddNotification(id); |
| 179 } |
| 180 bool shown = tray->message_center_tray_->ShowMessageCenterBubble(); |
| 181 EXPECT_TRUE(shown); |
| 182 content::RunAllPendingInMessageLoop(); |
| 183 EXPECT_TRUE(tray->message_center_bubble_.get() != NULL); |
| 184 EXPECT_EQ(notifications_to_add, |
| 185 message_center->NotificationCount()); |
| 186 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, |
| 187 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); |
| 188 } |
| 189 |
| 190 IN_PROC_BROWSER_TEST_F(WebNotificationTrayWinTest, ManyPopupNotifications) { |
| 191 scoped_ptr<WebNotificationTrayWin> tray(new WebNotificationTrayWin()); |
| 192 message_center::MessageCenter* message_center = tray->message_center(); |
| 193 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 194 |
| 195 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 196 size_t notifications_to_add = |
| 197 NotificationList::kMaxVisiblePopupNotifications + 1; |
| 198 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 199 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 200 delegate->AddNotification(id); |
| 201 } |
| 202 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 203 tray->message_center_tray_->HidePopupBubble(); |
| 204 tray->message_center_tray_->ShowPopupBubble(); |
| 205 EXPECT_TRUE(tray->message_center_tray_->popups_visible()); |
| 206 EXPECT_EQ(notifications_to_add, |
| 207 message_center->NotificationCount()); |
| 208 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, |
| 209 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); |
| 210 message_center->SetDelegate(NULL); |
| 211 message_center->notification_list()->RemoveAllNotifications(); |
| 212 } |
| 213 |
| 214 } // namespace message_center |
OLD | NEW |