| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/web_notification/web_notification_tray.h" | 5 #include "ash/system/web_notification/web_notification_tray.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "ash/root_window_controller.h" | 9 #include "ash/root_window_controller.h" |
| 10 #include "ash/system/status_area_widget.h" | 10 #include "ash/system/status_area_widget.h" |
| 11 #include "ash/system/tray/system_tray_item.h" | 11 #include "ash/system/tray/system_tray_item.h" |
| 12 #include "ash/test/ash_test_base.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" |
| 12 #include "ui/message_center/message_center_bubble.h" | 15 #include "ui/message_center/message_center_bubble.h" |
| 16 #include "ui/message_center/message_center_tray.h" |
| 13 #include "ui/message_center/message_popup_bubble.h" | 17 #include "ui/message_center/message_popup_bubble.h" |
| 14 #include "ui/message_center/notification_list.h" | 18 #include "ui/message_center/notification_list.h" |
| 15 #include "ui/notifications/notification_types.h" | 19 #include "ui/notifications/notification_types.h" |
| 16 #include "ash/test/ash_test_base.h" | |
| 17 #include "base/stringprintf.h" | |
| 18 #include "base/utf_string_conversions.h" | |
| 19 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 20 #include "ui/views/layout/fill_layout.h" | 21 #include "ui/views/layout/fill_layout.h" |
| 21 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
| 22 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 23 | 24 |
| 24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 25 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 28 namespace ash { | 29 namespace ash { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 WebNotificationTray* GetWebNotificationTray() { | 33 WebNotificationTray* GetWebNotificationTray() { |
| 33 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> | 34 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> |
| 34 web_notification_tray(); | 35 web_notification_tray(); |
| 35 } | 36 } |
| 36 | 37 |
| 38 message_center::MessageCenter* get_message_center() { |
| 39 return GetWebNotificationTray()->message_center(); |
| 40 } |
| 41 |
| 37 class TestDelegate : public message_center::MessageCenter::Delegate { | 42 class TestDelegate : public message_center::MessageCenter::Delegate { |
| 38 public: | 43 public: |
| 39 TestDelegate(message_center::MessageCenter* message_center) | 44 TestDelegate(message_center::MessageCenter* message_center) |
| 40 : message_center_(message_center) { | 45 : message_center_(message_center) { |
| 41 message_center_->SetDelegate(this); | 46 message_center_->SetDelegate(this); |
| 42 } | 47 } |
| 43 virtual ~TestDelegate() { | 48 virtual ~TestDelegate() { |
| 44 message_center_->SetDelegate(NULL); | 49 message_center_->SetDelegate(NULL); |
| 45 message_center_->notification_list()->RemoveAllNotifications(); | 50 message_center_->notification_list()->RemoveAllNotifications(); |
| 46 } | 51 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 58 } | 63 } |
| 59 | 64 |
| 60 virtual void ShowSettings(const std::string& notifcation_id) { | 65 virtual void ShowSettings(const std::string& notifcation_id) { |
| 61 } | 66 } |
| 62 | 67 |
| 63 virtual void OnClicked(const std::string& notifcation_id) { | 68 virtual void OnClicked(const std::string& notifcation_id) { |
| 64 } | 69 } |
| 65 | 70 |
| 66 void AddNotification(WebNotificationTray* tray, const std::string& id) { | 71 void AddNotification(WebNotificationTray* tray, const std::string& id) { |
| 67 notification_ids_.insert(id); | 72 notification_ids_.insert(id); |
| 68 tray->message_center()->AddNotification( | 73 get_message_center()->AddNotification( |
| 69 ui::notifications::NOTIFICATION_TYPE_SIMPLE, | 74 ui::notifications::NOTIFICATION_TYPE_SIMPLE, |
| 70 id, | 75 id, |
| 71 ASCIIToUTF16("Test Web Notification"), | 76 ASCIIToUTF16("Test Web Notification"), |
| 72 ASCIIToUTF16("Notification message body."), | 77 ASCIIToUTF16("Notification message body."), |
| 73 ASCIIToUTF16("www.test.org"), | 78 ASCIIToUTF16("www.test.org"), |
| 74 "" /* extension id */, | 79 "" /* extension id */, |
| 75 NULL /* optional_fields */); | 80 NULL /* optional_fields */); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void UpdateNotification(WebNotificationTray* tray, | 83 void UpdateNotification(WebNotificationTray* tray, |
| 79 const std::string& old_id, | 84 const std::string& old_id, |
| 80 const std::string& new_id) { | 85 const std::string& new_id) { |
| 81 notification_ids_.erase(old_id); | 86 notification_ids_.erase(old_id); |
| 82 notification_ids_.insert(new_id); | 87 notification_ids_.insert(new_id); |
| 83 tray->message_center()->UpdateNotification( | 88 get_message_center()->UpdateNotification( |
| 84 old_id, new_id, | 89 old_id, new_id, |
| 85 ASCIIToUTF16("Updated Web Notification"), | 90 ASCIIToUTF16("Updated Web Notification"), |
| 86 ASCIIToUTF16("Updated message body."), | 91 ASCIIToUTF16("Updated message body."), |
| 87 NULL); | 92 NULL); |
| 88 } | 93 } |
| 89 | 94 |
| 90 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { | 95 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { |
| 91 tray->message_center()->RemoveNotification(id); | 96 get_message_center()->RemoveNotification(id); |
| 92 notification_ids_.erase(id); | 97 notification_ids_.erase(id); |
| 93 } | 98 } |
| 94 | 99 |
| 95 bool HasNotificationId(const std::string& id) { | 100 bool HasNotificationId(const std::string& id) { |
| 96 return notification_ids_.find(id) != notification_ids_.end(); | 101 return notification_ids_.find(id) != notification_ids_.end(); |
| 97 } | 102 } |
| 98 | 103 |
| 99 private: | 104 private: |
| 100 std::set<std::string> notification_ids_; | 105 std::set<std::string> notification_ids_; |
| 101 message_center::MessageCenter* message_center_; | 106 message_center::MessageCenter* message_center_; |
| 102 | 107 |
| 103 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 108 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 104 }; | 109 }; |
| 105 | 110 |
| 106 } // namespace | 111 } // namespace |
| 107 | 112 |
| 108 typedef test::AshTestBase WebNotificationTrayTest; | 113 typedef test::AshTestBase WebNotificationTrayTest; |
| 109 | 114 |
| 110 TEST_F(WebNotificationTrayTest, WebNotifications) { | 115 TEST_F(WebNotificationTrayTest, WebNotifications) { |
| 111 WebNotificationTray* tray = GetWebNotificationTray(); | 116 WebNotificationTray* tray = GetWebNotificationTray(); |
| 112 message_center::MessageCenter* message_center = tray->message_center(); | 117 message_center::MessageCenter* message_center = tray->message_center(); |
| 113 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); | 118 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 114 ASSERT_TRUE(tray->GetWidget()); | 119 ASSERT_TRUE(tray->GetWidget()); |
| 115 | 120 |
| 116 // Add a notification. | 121 // Add a notification. |
| 117 delegate->AddNotification(tray, "test_id1"); | 122 delegate->AddNotification(tray, "test_id1"); |
| 118 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); | 123 EXPECT_EQ(1u, get_message_center()->NotificationCount()); |
| 119 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); | 124 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); |
| 120 delegate->AddNotification(tray, "test_id2"); | 125 delegate->AddNotification(tray, "test_id2"); |
| 121 delegate->AddNotification(tray, "test_id2"); | 126 delegate->AddNotification(tray, "test_id2"); |
| 122 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); | 127 EXPECT_EQ(2u, get_message_center()->NotificationCount()); |
| 123 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); | 128 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); |
| 124 | 129 |
| 125 // Ensure that updating a notification does not affect the count. | 130 // Ensure that updating a notification does not affect the count. |
| 126 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 131 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 127 delegate->UpdateNotification(tray, "test_id3", "test_id3"); | 132 delegate->UpdateNotification(tray, "test_id3", "test_id3"); |
| 128 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); | 133 EXPECT_EQ(2u, get_message_center()->NotificationCount()); |
| 129 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); | 134 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); |
| 130 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 135 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 131 "test_id2")); | 136 "test_id2")); |
| 132 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); | 137 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); |
| 133 | 138 |
| 134 // Ensure that Removing the first notification removes it from the tray. | 139 // Ensure that Removing the first notification removes it from the tray. |
| 135 delegate->RemoveNotification(tray, "test_id1"); | 140 delegate->RemoveNotification(tray, "test_id1"); |
| 136 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); | 141 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); |
| 137 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 142 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 138 "test_id1")); | 143 "test_id1")); |
| 139 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); | 144 EXPECT_EQ(1u, get_message_center()->NotificationCount()); |
| 140 | 145 |
| 141 // Remove the remianing notification. | 146 // Remove the remianing notification. |
| 142 delegate->RemoveNotification(tray, "test_id3"); | 147 delegate->RemoveNotification(tray, "test_id3"); |
| 143 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); | 148 EXPECT_EQ(0u, get_message_center()->NotificationCount()); |
| 144 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 149 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 145 "test_id3")); | 150 "test_id3")); |
| 146 } | 151 } |
| 147 | 152 |
| 148 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { | 153 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { |
| 149 WebNotificationTray* tray = GetWebNotificationTray(); | 154 WebNotificationTray* tray = GetWebNotificationTray(); |
| 150 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 155 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 151 | 156 |
| 152 ASSERT_TRUE(tray->GetWidget()); | 157 ASSERT_TRUE(tray->GetWidget()); |
| 153 | 158 |
| 154 // Adding a notification should show the popup bubble. | 159 // Adding a notification should show the popup bubble. |
| 155 delegate->AddNotification(tray, "test_id1"); | 160 delegate->AddNotification(tray, "test_id1"); |
| 156 EXPECT_TRUE(tray->popup_bubble() != NULL); | 161 EXPECT_TRUE(tray->IsPopupVisible()); |
| 157 | 162 |
| 158 // Updating a notification should not hide the popup bubble. | 163 // Updating a notification should not hide the popup bubble. |
| 159 delegate->AddNotification(tray, "test_id2"); | 164 delegate->AddNotification(tray, "test_id2"); |
| 160 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 165 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 161 EXPECT_TRUE(tray->popup_bubble() != NULL); | 166 EXPECT_TRUE(tray->IsPopupVisible()); |
| 162 | 167 |
| 163 // Removing the first notification should not hide the popup bubble. | 168 // Removing the first notification should not hide the popup bubble. |
| 164 delegate->RemoveNotification(tray, "test_id1"); | 169 delegate->RemoveNotification(tray, "test_id1"); |
| 165 EXPECT_TRUE(tray->popup_bubble() != NULL); | 170 EXPECT_TRUE(tray->IsPopupVisible()); |
| 166 | 171 |
| 167 // Removing the visible notification should hide the popup bubble. | 172 // Removing the visible notification should hide the popup bubble. |
| 168 delegate->RemoveNotification(tray, "test_id3"); | 173 delegate->RemoveNotification(tray, "test_id3"); |
| 169 EXPECT_TRUE(tray->popup_bubble() == NULL); | 174 EXPECT_FALSE(tray->IsPopupVisible()); |
| 170 } | 175 } |
| 171 | 176 |
| 172 using message_center::NotificationList; | 177 using message_center::NotificationList; |
| 173 | 178 |
| 174 | 179 |
| 175 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { | 180 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { |
| 176 WebNotificationTray* tray = GetWebNotificationTray(); | 181 WebNotificationTray* tray = GetWebNotificationTray(); |
| 177 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 182 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 178 | 183 |
| 179 // Add the max visible notifications +1, ensure the correct visible number. | 184 // Add the max visible notifications +1, ensure the correct visible number. |
| 180 size_t notifications_to_add = | 185 size_t notifications_to_add = |
| 181 NotificationList::kMaxVisibleMessageCenterNotifications + 1; | 186 NotificationList::kMaxVisibleMessageCenterNotifications + 1; |
| 182 for (size_t i = 0; i < notifications_to_add; ++i) { | 187 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 183 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 188 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 184 delegate->AddNotification(tray, id); | 189 delegate->AddNotification(tray, id); |
| 185 } | 190 } |
| 186 tray->ShowMessageCenterBubble(); | 191 bool shown = tray->message_center_tray_->ShowMessageCenterBubble(); |
| 192 EXPECT_TRUE(shown); |
| 187 RunAllPendingInMessageLoop(); | 193 RunAllPendingInMessageLoop(); |
| 188 EXPECT_TRUE(tray->message_center_bubble() != NULL); | 194 EXPECT_TRUE(tray->message_center_bubble() != NULL); |
| 189 EXPECT_EQ(notifications_to_add, | 195 EXPECT_EQ(notifications_to_add, |
| 190 tray->message_center()->NotificationCount()); | 196 get_message_center()->NotificationCount()); |
| 191 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, | 197 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, |
| 192 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); | 198 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); |
| 193 } | 199 } |
| 194 | 200 |
| 195 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { | 201 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { |
| 196 WebNotificationTray* tray = GetWebNotificationTray(); | 202 WebNotificationTray* tray = GetWebNotificationTray(); |
| 197 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 203 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 198 | 204 |
| 199 // Add the max visible popup notifications +1, ensure the correct num visible. | 205 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 200 size_t notifications_to_add = | 206 size_t notifications_to_add = |
| 201 NotificationList::kMaxVisiblePopupNotifications + 1; | 207 NotificationList::kMaxVisiblePopupNotifications + 1; |
| 202 for (size_t i = 0; i < notifications_to_add; ++i) { | 208 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 203 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 209 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 204 delegate->AddNotification(tray, id); | 210 delegate->AddNotification(tray, id); |
| 205 } | 211 } |
| 206 // Hide and reshow the bubble so that it is updated immediately, not delayed. | 212 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 207 tray->HidePopupBubble(); | 213 tray->SetHidePopupBubble(true); |
| 208 tray->ShowPopupBubble(); | 214 tray->SetHidePopupBubble(false); |
| 209 EXPECT_TRUE(tray->popup_bubble() != NULL); | 215 EXPECT_TRUE(tray->IsPopupVisible()); |
| 210 EXPECT_EQ(notifications_to_add, | 216 EXPECT_EQ(notifications_to_add, |
| 211 tray->message_center()->NotificationCount()); | 217 get_message_center()->NotificationCount()); |
| 212 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, | 218 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, |
| 213 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); | 219 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); |
| 220 get_message_center()->SetDelegate(NULL); |
| 221 get_message_center()->notification_list()->RemoveAllNotifications(); |
| 214 } | 222 } |
| 215 | 223 |
| 216 } // namespace ash | 224 } // namespace ash |
| OLD | NEW |