| 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 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace ash { | 23 namespace ash { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 WebNotificationTray* GetWebNotificationTray() { | 27 WebNotificationTray* GetWebNotificationTray() { |
| 28 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> | 28 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> |
| 29 web_notification_tray(); | 29 web_notification_tray(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 class TestDelegate : public WebNotificationTray::Delegate { | 32 class TestDelegate : public message_center::MessageCenter::Delegate { |
| 33 public: | 33 public: |
| 34 TestDelegate() {} | 34 TestDelegate() {} |
| 35 virtual ~TestDelegate() {} | 35 virtual ~TestDelegate() {} |
| 36 | 36 |
| 37 // WebNotificationTray::Delegate overrides. | 37 // WebNotificationTray::Delegate overrides. |
| 38 virtual void NotificationRemoved(const std::string& notifcation_id) { | 38 virtual void NotificationRemoved(const std::string& notifcation_id) { |
| 39 notification_ids_.erase(notifcation_id); | 39 notification_ids_.erase(notifcation_id); |
| 40 } | 40 } |
| 41 | 41 |
| 42 virtual void DisableExtension(const std::string& notifcation_id) { | 42 virtual void DisableExtension(const std::string& notifcation_id) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 virtual void DisableNotificationsFromSource( | 45 virtual void DisableNotificationsFromSource( |
| 46 const std::string& notifcation_id) { | 46 const std::string& notifcation_id) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 virtual void ShowSettings(const std::string& notifcation_id) { | 49 virtual void ShowSettings(const std::string& notifcation_id) { |
| 50 } | 50 } |
| 51 | 51 |
| 52 virtual void OnClicked(const std::string& notifcation_id) { | 52 virtual void OnClicked(const std::string& notifcation_id) { |
| 53 } | 53 } |
| 54 | 54 |
| 55 void AddNotification(WebNotificationTray* tray, const std::string& id) { | 55 void AddNotification(WebNotificationTray* tray, const std::string& id) { |
| 56 notification_ids_.insert(id); | 56 notification_ids_.insert(id); |
| 57 tray->AddNotification(id, | 57 tray->message_center()->AddNotification( |
| 58 ASCIIToUTF16("Test Web Notification"), | 58 id, |
| 59 ASCIIToUTF16("Notification message body."), | 59 ASCIIToUTF16("Test Web Notification"), |
| 60 ASCIIToUTF16("www.test.org"), | 60 ASCIIToUTF16("Notification message body."), |
| 61 "" /* extension id */); | 61 ASCIIToUTF16("www.test.org"), |
| 62 "" /* extension id */); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void UpdateNotification(WebNotificationTray* tray, | 65 void UpdateNotification(WebNotificationTray* tray, |
| 65 const std::string& old_id, | 66 const std::string& old_id, |
| 66 const std::string& new_id) { | 67 const std::string& new_id) { |
| 67 notification_ids_.erase(old_id); | 68 notification_ids_.erase(old_id); |
| 68 notification_ids_.insert(new_id); | 69 notification_ids_.insert(new_id); |
| 69 tray->UpdateNotification(old_id, new_id, | 70 tray->message_center()->UpdateNotification( |
| 70 ASCIIToUTF16("Updated Web Notification"), | 71 old_id, new_id, |
| 71 ASCIIToUTF16("Updated message body.")); | 72 ASCIIToUTF16("Updated Web Notification"), |
| 73 ASCIIToUTF16("Updated message body.")); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { | 76 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { |
| 75 tray->RemoveNotification(id); | 77 tray->message_center()->RemoveNotification(id); |
| 76 notification_ids_.erase(id); | 78 notification_ids_.erase(id); |
| 77 } | 79 } |
| 78 | 80 |
| 79 bool HasNotificationId(const std::string& id) { | 81 bool HasNotificationId(const std::string& id) { |
| 80 return notification_ids_.find(id) != notification_ids_.end(); | 82 return notification_ids_.find(id) != notification_ids_.end(); |
| 81 } | 83 } |
| 82 | 84 |
| 83 private: | 85 private: |
| 84 std::set<std::string> notification_ids_; | 86 std::set<std::string> notification_ids_; |
| 85 | 87 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 88 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 } // namespace | 91 } // namespace |
| 90 | 92 |
| 91 typedef test::AshTestBase WebNotificationTrayTest; | 93 typedef test::AshTestBase WebNotificationTrayTest; |
| 92 | 94 |
| 93 TEST_F(WebNotificationTrayTest, WebNotifications) { | 95 TEST_F(WebNotificationTrayTest, WebNotifications) { |
| 94 WebNotificationTray* tray = GetWebNotificationTray(); | 96 WebNotificationTray* tray = GetWebNotificationTray(); |
| 95 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 97 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
| 96 tray->SetDelegate(delegate.get()); | 98 message_center::MessageCenter* message_center = tray->message_center(); |
| 97 | 99 message_center->SetDelegate(delegate.get()); |
| 98 ASSERT_TRUE(tray->GetWidget()); | 100 ASSERT_TRUE(tray->GetWidget()); |
| 99 | 101 |
| 100 // Add a notification. | 102 // Add a notification. |
| 101 delegate->AddNotification(tray, "test_id1"); | 103 delegate->AddNotification(tray, "test_id1"); |
| 102 EXPECT_EQ(1u, tray->notification_list()->notifications().size()); | 104 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); |
| 103 EXPECT_TRUE(tray->notification_list()->HasNotification("test_id1")); | 105 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); |
| 104 delegate->AddNotification(tray, "test_id2"); | 106 delegate->AddNotification(tray, "test_id2"); |
| 105 delegate->AddNotification(tray, "test_id2"); | 107 delegate->AddNotification(tray, "test_id2"); |
| 106 EXPECT_EQ(2u, tray->notification_list()->notifications().size()); | 108 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); |
| 107 EXPECT_TRUE(tray->notification_list()->HasNotification("test_id2")); | 109 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); |
| 108 | 110 |
| 109 // Ensure that updating a notification does not affect the count. | 111 // Ensure that updating a notification does not affect the count. |
| 110 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 112 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 111 delegate->UpdateNotification(tray, "test_id3", "test_id3"); | 113 delegate->UpdateNotification(tray, "test_id3", "test_id3"); |
| 112 EXPECT_EQ(2u, tray->notification_list()->notifications().size()); | 114 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); |
| 113 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); | 115 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); |
| 114 EXPECT_FALSE(tray->notification_list()->HasNotification("test_id2")); | 116 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 117 "test_id2")); |
| 115 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); | 118 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); |
| 116 | 119 |
| 117 // Ensure that Removing the first notification removes it from the tray. | 120 // Ensure that Removing the first notification removes it from the tray. |
| 118 delegate->RemoveNotification(tray, "test_id1"); | 121 delegate->RemoveNotification(tray, "test_id1"); |
| 119 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); | 122 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); |
| 120 EXPECT_FALSE(tray->notification_list()->HasNotification("test_id1")); | 123 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 121 EXPECT_EQ(1u, tray->notification_list()->notifications().size()); | 124 "test_id1")); |
| 125 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); |
| 122 | 126 |
| 123 // Remove the remianing notification. | 127 // Remove the remianing notification. |
| 124 delegate->RemoveNotification(tray, "test_id3"); | 128 delegate->RemoveNotification(tray, "test_id3"); |
| 125 EXPECT_EQ(0u, tray->notification_list()->notifications().size()); | 129 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); |
| 126 EXPECT_FALSE(tray->notification_list()->HasNotification("test_id3")); | 130 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 131 "test_id3")); |
| 127 } | 132 } |
| 128 | 133 |
| 129 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { | 134 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { |
| 130 WebNotificationTray* tray = GetWebNotificationTray(); | 135 WebNotificationTray* tray = GetWebNotificationTray(); |
| 131 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 136 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
| 132 tray->SetDelegate(delegate.get()); | 137 tray->message_center()->SetDelegate(delegate.get()); |
| 133 | 138 |
| 134 ASSERT_TRUE(tray->GetWidget()); | 139 ASSERT_TRUE(tray->GetWidget()); |
| 135 | 140 |
| 136 // Adding a notification should show the popup bubble. | 141 // Adding a notification should show the popup bubble. |
| 137 delegate->AddNotification(tray, "test_id1"); | 142 delegate->AddNotification(tray, "test_id1"); |
| 138 EXPECT_TRUE(tray->popup_bubble() != NULL); | 143 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 139 | 144 |
| 140 // Updating a notification should not hide the popup bubble. | 145 // Updating a notification should not hide the popup bubble. |
| 141 delegate->AddNotification(tray, "test_id2"); | 146 delegate->AddNotification(tray, "test_id2"); |
| 142 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 147 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 143 EXPECT_TRUE(tray->popup_bubble() != NULL); | 148 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 144 | 149 |
| 145 // Removing the first notification should not hide the popup bubble. | 150 // Removing the first notification should not hide the popup bubble. |
| 146 delegate->RemoveNotification(tray, "test_id1"); | 151 delegate->RemoveNotification(tray, "test_id1"); |
| 147 EXPECT_TRUE(tray->popup_bubble() != NULL); | 152 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 148 | 153 |
| 149 // Removing the visible notification should hide the popup bubble. | 154 // Removing the visible notification should hide the popup bubble. |
| 150 delegate->RemoveNotification(tray, "test_id3"); | 155 delegate->RemoveNotification(tray, "test_id3"); |
| 151 EXPECT_TRUE(tray->popup_bubble() == NULL); | 156 EXPECT_TRUE(tray->popup_bubble() == NULL); |
| 152 } | 157 } |
| 153 | 158 |
| 154 using message_center::WebNotificationList; | 159 using message_center::WebNotificationList; |
| 155 | 160 |
| 156 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { | 161 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { |
| 157 WebNotificationTray* tray = GetWebNotificationTray(); | 162 WebNotificationTray* tray = GetWebNotificationTray(); |
| 158 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 163 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
| 159 tray->SetDelegate(delegate.get()); | 164 tray->message_center()->SetDelegate(delegate.get()); |
| 160 | 165 |
| 161 // Add the max visible notifications +1, ensure the correct visible number. | 166 // Add the max visible notifications +1, ensure the correct visible number. |
| 162 size_t notifications_to_add = | 167 size_t notifications_to_add = |
| 163 WebNotificationList::kMaxVisibleMessageCenterNotifications + 1; | 168 WebNotificationList::kMaxVisibleMessageCenterNotifications + 1; |
| 164 for (size_t i = 0; i < notifications_to_add; ++i) { | 169 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 165 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 170 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 166 delegate->AddNotification(tray, id); | 171 delegate->AddNotification(tray, id); |
| 167 } | 172 } |
| 168 tray->ShowMessageCenterBubble(); | 173 tray->ShowMessageCenterBubble(); |
| 169 RunAllPendingInMessageLoop(); | 174 RunAllPendingInMessageLoop(); |
| 170 EXPECT_TRUE(tray->message_center_bubble() != NULL); | 175 EXPECT_TRUE(tray->message_center_bubble() != NULL); |
| 171 EXPECT_EQ(notifications_to_add, | 176 EXPECT_EQ(notifications_to_add, |
| 172 tray->notification_list()->notifications().size()); | 177 tray->message_center()->NotificationCount()); |
| 173 EXPECT_EQ(WebNotificationList::kMaxVisibleMessageCenterNotifications, | 178 EXPECT_EQ(WebNotificationList::kMaxVisibleMessageCenterNotifications, |
| 174 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); | 179 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); |
| 175 } | 180 } |
| 176 | 181 |
| 177 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { | 182 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { |
| 178 WebNotificationTray* tray = GetWebNotificationTray(); | 183 WebNotificationTray* tray = GetWebNotificationTray(); |
| 179 scoped_ptr<TestDelegate> delegate(new TestDelegate); | 184 scoped_ptr<TestDelegate> delegate(new TestDelegate); |
| 180 tray->SetDelegate(delegate.get()); | 185 tray->message_center()->SetDelegate(delegate.get()); |
| 181 | 186 |
| 182 // Add the max visible popup notifications +1, ensure the correct num visible. | 187 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 183 size_t notifications_to_add = | 188 size_t notifications_to_add = |
| 184 WebNotificationList::kMaxVisiblePopupNotifications + 1; | 189 WebNotificationList::kMaxVisiblePopupNotifications + 1; |
| 185 for (size_t i = 0; i < notifications_to_add; ++i) { | 190 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 186 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 191 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 187 delegate->AddNotification(tray, id); | 192 delegate->AddNotification(tray, id); |
| 188 } | 193 } |
| 189 // Hide and reshow the bubble so that it is updated immediately, not delayed. | 194 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 190 tray->HidePopupBubble(); | 195 tray->HidePopupBubble(); |
| 191 tray->ShowPopupBubble(); | 196 tray->ShowPopupBubble(); |
| 192 EXPECT_TRUE(tray->popup_bubble() != NULL); | 197 EXPECT_TRUE(tray->popup_bubble() != NULL); |
| 193 EXPECT_EQ(notifications_to_add, | 198 EXPECT_EQ(notifications_to_add, |
| 194 tray->notification_list()->notifications().size()); | 199 tray->message_center()->NotificationCount()); |
| 195 EXPECT_EQ(WebNotificationList::kMaxVisiblePopupNotifications, | 200 EXPECT_EQ(WebNotificationList::kMaxVisiblePopupNotifications, |
| 196 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); | 201 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); |
| 197 } | 202 } |
| 198 | 203 |
| 199 } // namespace ash | 204 } // namespace ash |
| OLD | NEW |