| 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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 WebNotificationTray* GetWebNotificationTray() { | 28 WebNotificationTray* GetWebNotificationTray() { |
| 29 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> | 29 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> |
| 30 web_notification_tray(); | 30 web_notification_tray(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 class TestDelegate : public message_center::MessageCenter::Delegate { | 33 class TestDelegate : public message_center::MessageCenter::Delegate { |
| 34 public: | 34 public: |
| 35 TestDelegate() {} | 35 TestDelegate(message_center::MessageCenter* message_center) |
| 36 virtual ~TestDelegate() {} | 36 : message_center_(message_center) { |
| 37 message_center_->SetDelegate(this); |
| 38 } |
| 39 virtual ~TestDelegate() { |
| 40 message_center_->SetDelegate(NULL); |
| 41 message_center_->notification_list()->RemoveAllNotifications(); |
| 42 } |
| 37 | 43 |
| 38 // WebNotificationTray::Delegate overrides. | 44 // WebNotificationTray::Delegate overrides. |
| 39 virtual void NotificationRemoved(const std::string& notifcation_id) { | 45 virtual void NotificationRemoved(const std::string& notifcation_id) { |
| 40 notification_ids_.erase(notifcation_id); | 46 notification_ids_.erase(notifcation_id); |
| 41 } | 47 } |
| 42 | 48 |
| 43 virtual void DisableExtension(const std::string& notifcation_id) { | 49 virtual void DisableExtension(const std::string& notifcation_id) { |
| 44 } | 50 } |
| 45 | 51 |
| 46 virtual void DisableNotificationsFromSource( | 52 virtual void DisableNotificationsFromSource( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 tray->message_center()->RemoveNotification(id); | 87 tray->message_center()->RemoveNotification(id); |
| 82 notification_ids_.erase(id); | 88 notification_ids_.erase(id); |
| 83 } | 89 } |
| 84 | 90 |
| 85 bool HasNotificationId(const std::string& id) { | 91 bool HasNotificationId(const std::string& id) { |
| 86 return notification_ids_.find(id) != notification_ids_.end(); | 92 return notification_ids_.find(id) != notification_ids_.end(); |
| 87 } | 93 } |
| 88 | 94 |
| 89 private: | 95 private: |
| 90 std::set<std::string> notification_ids_; | 96 std::set<std::string> notification_ids_; |
| 97 message_center::MessageCenter* message_center_; |
| 91 | 98 |
| 92 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 99 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 93 }; | 100 }; |
| 94 | 101 |
| 95 } // namespace | 102 } // namespace |
| 96 | 103 |
| 97 typedef test::AshTestBase WebNotificationTrayTest; | 104 typedef test::AshTestBase WebNotificationTrayTest; |
| 98 | 105 |
| 99 TEST_F(WebNotificationTrayTest, WebNotifications) { | 106 TEST_F(WebNotificationTrayTest, WebNotifications) { |
| 100 WebNotificationTray* tray = GetWebNotificationTray(); | 107 WebNotificationTray* tray = GetWebNotificationTray(); |
| 101 scoped_ptr<TestDelegate> delegate(new TestDelegate); | |
| 102 message_center::MessageCenter* message_center = tray->message_center(); | 108 message_center::MessageCenter* message_center = tray->message_center(); |
| 103 message_center->SetDelegate(delegate.get()); | 109 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 104 ASSERT_TRUE(tray->GetWidget()); | 110 ASSERT_TRUE(tray->GetWidget()); |
| 105 | 111 |
| 106 // Add a notification. | 112 // Add a notification. |
| 107 delegate->AddNotification(tray, "test_id1"); | 113 delegate->AddNotification(tray, "test_id1"); |
| 108 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); | 114 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); |
| 109 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); | 115 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); |
| 110 delegate->AddNotification(tray, "test_id2"); | 116 delegate->AddNotification(tray, "test_id2"); |
| 111 delegate->AddNotification(tray, "test_id2"); | 117 delegate->AddNotification(tray, "test_id2"); |
| 112 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); | 118 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); |
| 113 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); | 119 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 130 | 136 |
| 131 // Remove the remianing notification. | 137 // Remove the remianing notification. |
| 132 delegate->RemoveNotification(tray, "test_id3"); | 138 delegate->RemoveNotification(tray, "test_id3"); |
| 133 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); | 139 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); |
| 134 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 140 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 135 "test_id3")); | 141 "test_id3")); |
| 136 } | 142 } |
| 137 | 143 |
| 138 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { | 144 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { |
| 139 WebNotificationTray* tray = GetWebNotificationTray(); | 145 WebNotificationTray* tray = GetWebNotificationTray(); |
| 140 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 146 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); |
| 141 tray->message_center()->SetDelegate(delegate.get()); | |
| 142 | 147 |
| 143 ASSERT_TRUE(tray->GetWidget()); | 148 ASSERT_TRUE(tray->GetWidget()); |
| 144 | 149 |
| 145 // Adding a notification should show the popup bubble. | 150 // Adding a notification should show the popup bubble. |
| 146 delegate->AddNotification(tray, "test_id1"); | 151 delegate->AddNotification(tray, "test_id1"); |
| 147 EXPECT_TRUE(tray->popup_bubble() != NULL); | 152 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 148 | 153 |
| 149 // Updating a notification should not hide the popup bubble. | 154 // Updating a notification should not hide the popup bubble. |
| 150 delegate->AddNotification(tray, "test_id2"); | 155 delegate->AddNotification(tray, "test_id2"); |
| 151 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 156 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 152 EXPECT_TRUE(tray->popup_bubble() != NULL); | 157 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 153 | 158 |
| 154 // Removing the first notification should not hide the popup bubble. | 159 // Removing the first notification should not hide the popup bubble. |
| 155 delegate->RemoveNotification(tray, "test_id1"); | 160 delegate->RemoveNotification(tray, "test_id1"); |
| 156 EXPECT_TRUE(tray->popup_bubble() != NULL); | 161 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 157 | 162 |
| 158 // Removing the visible notification should hide the popup bubble. | 163 // Removing the visible notification should hide the popup bubble. |
| 159 delegate->RemoveNotification(tray, "test_id3"); | 164 delegate->RemoveNotification(tray, "test_id3"); |
| 160 EXPECT_TRUE(tray->popup_bubble() == NULL); | 165 EXPECT_TRUE(tray->popup_bubble() == NULL); |
| 161 } | 166 } |
| 162 | 167 |
| 163 using message_center::NotificationList; | 168 using message_center::NotificationList; |
| 164 | 169 |
| 165 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { | 170 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { |
| 166 WebNotificationTray* tray = GetWebNotificationTray(); | 171 WebNotificationTray* tray = GetWebNotificationTray(); |
| 167 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 172 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); |
| 168 tray->message_center()->SetDelegate(delegate.get()); | |
| 169 | 173 |
| 170 // Add the max visible notifications +1, ensure the correct visible number. | 174 // Add the max visible notifications +1, ensure the correct visible number. |
| 171 size_t notifications_to_add = | 175 size_t notifications_to_add = |
| 172 NotificationList::kMaxVisibleMessageCenterNotifications + 1; | 176 NotificationList::kMaxVisibleMessageCenterNotifications + 1; |
| 173 for (size_t i = 0; i < notifications_to_add; ++i) { | 177 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 174 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 178 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 175 delegate->AddNotification(tray, id); | 179 delegate->AddNotification(tray, id); |
| 176 } | 180 } |
| 177 tray->ShowMessageCenterBubble(); | 181 tray->ShowMessageCenterBubble(); |
| 178 RunAllPendingInMessageLoop(); | 182 RunAllPendingInMessageLoop(); |
| 179 EXPECT_TRUE(tray->message_center_bubble() != NULL); | 183 EXPECT_TRUE(tray->message_center_bubble() != NULL); |
| 180 EXPECT_EQ(notifications_to_add, | 184 EXPECT_EQ(notifications_to_add, |
| 181 tray->message_center()->NotificationCount()); | 185 tray->message_center()->NotificationCount()); |
| 182 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, | 186 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, |
| 183 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); | 187 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); |
| 184 } | 188 } |
| 185 | 189 |
| 186 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { | 190 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { |
| 187 WebNotificationTray* tray = GetWebNotificationTray(); | 191 WebNotificationTray* tray = GetWebNotificationTray(); |
| 188 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 192 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); |
| 189 tray->message_center()->SetDelegate(delegate.get()); | |
| 190 | 193 |
| 191 // Add the max visible popup notifications +1, ensure the correct num visible. | 194 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 192 size_t notifications_to_add = | 195 size_t notifications_to_add = |
| 193 NotificationList::kMaxVisiblePopupNotifications + 1; | 196 NotificationList::kMaxVisiblePopupNotifications + 1; |
| 194 for (size_t i = 0; i < notifications_to_add; ++i) { | 197 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 195 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 198 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 196 delegate->AddNotification(tray, id); | 199 delegate->AddNotification(tray, id); |
| 197 } | 200 } |
| 198 // Hide and reshow the bubble so that it is updated immediately, not delayed. | 201 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 199 tray->HidePopupBubble(); | 202 tray->HidePopupBubble(); |
| 200 tray->ShowPopupBubble(); | 203 tray->ShowPopupBubble(); |
| 201 EXPECT_TRUE(tray->popup_bubble() != NULL); | 204 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 202 EXPECT_EQ(notifications_to_add, | 205 EXPECT_EQ(notifications_to_add, |
| 203 tray->message_center()->NotificationCount()); | 206 tray->message_center()->NotificationCount()); |
| 204 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, | 207 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, |
| 205 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); | 208 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); |
| 206 } | 209 } |
| 207 | 210 |
| 208 } // namespace ash | 211 } // namespace ash |
| OLD | NEW |