Chromium Code Reviews| 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 |
| 33 // Must be called only when expecting a clean message center. | |
|
miket_OOO
2013/01/25 17:14:48
I'm not sure what this means. Is there a way to ex
dewittj
2013/01/25 19:38:46
This is a vestigal comment from when the message c
| |
| 32 WebNotificationTray* GetWebNotificationTray() { | 34 WebNotificationTray* GetWebNotificationTray() { |
| 33 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> | 35 return Shell::GetPrimaryRootWindowController()->status_area_widget()-> |
| 34 web_notification_tray(); | 36 web_notification_tray(); |
| 35 } | 37 } |
| 36 | 38 |
| 39 message_center::MessageCenter* get_message_center() { | |
| 40 return GetWebNotificationTray()->message_center(); | |
| 41 } | |
| 42 | |
| 43 | |
| 37 class TestDelegate : public message_center::MessageCenter::Delegate { | 44 class TestDelegate : public message_center::MessageCenter::Delegate { |
| 38 public: | 45 public: |
| 39 TestDelegate(message_center::MessageCenter* message_center) | 46 TestDelegate(message_center::MessageCenter* message_center) |
| 40 : message_center_(message_center) { | 47 : message_center_(message_center) { |
| 41 message_center_->SetDelegate(this); | 48 message_center_->SetDelegate(this); |
| 42 } | 49 } |
| 43 virtual ~TestDelegate() { | 50 virtual ~TestDelegate() { |
| 44 message_center_->SetDelegate(NULL); | 51 message_center_->SetDelegate(NULL); |
| 45 message_center_->notification_list()->RemoveAllNotifications(); | 52 message_center_->notification_list()->RemoveAllNotifications(); |
| 46 } | 53 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 58 } | 65 } |
| 59 | 66 |
| 60 virtual void ShowSettings(const std::string& notifcation_id) { | 67 virtual void ShowSettings(const std::string& notifcation_id) { |
| 61 } | 68 } |
| 62 | 69 |
| 63 virtual void OnClicked(const std::string& notifcation_id) { | 70 virtual void OnClicked(const std::string& notifcation_id) { |
| 64 } | 71 } |
| 65 | 72 |
| 66 void AddNotification(WebNotificationTray* tray, const std::string& id) { | 73 void AddNotification(WebNotificationTray* tray, const std::string& id) { |
| 67 notification_ids_.insert(id); | 74 notification_ids_.insert(id); |
| 68 tray->message_center()->AddNotification( | 75 get_message_center()->AddNotification( |
| 69 ui::notifications::NOTIFICATION_TYPE_SIMPLE, | 76 ui::notifications::NOTIFICATION_TYPE_SIMPLE, |
| 70 id, | 77 id, |
| 71 ASCIIToUTF16("Test Web Notification"), | 78 ASCIIToUTF16("Test Web Notification"), |
| 72 ASCIIToUTF16("Notification message body."), | 79 ASCIIToUTF16("Notification message body."), |
| 73 ASCIIToUTF16("www.test.org"), | 80 ASCIIToUTF16("www.test.org"), |
| 74 "" /* extension id */, | 81 "" /* extension id */, |
| 75 NULL /* optional_fields */); | 82 NULL /* optional_fields */); |
| 76 } | 83 } |
| 77 | 84 |
| 78 void UpdateNotification(WebNotificationTray* tray, | 85 void UpdateNotification(WebNotificationTray* tray, |
| 79 const std::string& old_id, | 86 const std::string& old_id, |
| 80 const std::string& new_id) { | 87 const std::string& new_id) { |
| 81 notification_ids_.erase(old_id); | 88 notification_ids_.erase(old_id); |
| 82 notification_ids_.insert(new_id); | 89 notification_ids_.insert(new_id); |
| 83 tray->message_center()->UpdateNotification( | 90 get_message_center()->UpdateNotification( |
| 84 old_id, new_id, | 91 old_id, new_id, |
| 85 ASCIIToUTF16("Updated Web Notification"), | 92 ASCIIToUTF16("Updated Web Notification"), |
| 86 ASCIIToUTF16("Updated message body."), | 93 ASCIIToUTF16("Updated message body."), |
| 87 NULL); | 94 NULL); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { | 97 void RemoveNotification(WebNotificationTray* tray, const std::string& id) { |
| 91 tray->message_center()->RemoveNotification(id); | 98 get_message_center()->RemoveNotification(id); |
| 92 notification_ids_.erase(id); | 99 notification_ids_.erase(id); |
| 93 } | 100 } |
| 94 | 101 |
| 95 bool HasNotificationId(const std::string& id) { | 102 bool HasNotificationId(const std::string& id) { |
| 96 return notification_ids_.find(id) != notification_ids_.end(); | 103 return notification_ids_.find(id) != notification_ids_.end(); |
| 97 } | 104 } |
| 98 | 105 |
| 99 private: | 106 private: |
| 100 std::set<std::string> notification_ids_; | 107 std::set<std::string> notification_ids_; |
| 101 message_center::MessageCenter* message_center_; | 108 message_center::MessageCenter* message_center_; |
| 102 | 109 |
| 103 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 110 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 104 }; | 111 }; |
| 105 | 112 |
| 106 } // namespace | 113 } // namespace |
| 107 | 114 |
| 108 typedef test::AshTestBase WebNotificationTrayTest; | 115 typedef test::AshTestBase WebNotificationTrayTest; |
| 109 | 116 |
| 110 TEST_F(WebNotificationTrayTest, WebNotifications) { | 117 TEST_F(WebNotificationTrayTest, WebNotifications) { |
| 111 WebNotificationTray* tray = GetWebNotificationTray(); | 118 WebNotificationTray* tray = GetWebNotificationTray(); |
| 112 message_center::MessageCenter* message_center = tray->message_center(); | 119 message_center::MessageCenter* message_center = tray->message_center(); |
| 113 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); | 120 scoped_ptr<TestDelegate> delegate(new TestDelegate(message_center)); |
| 114 ASSERT_TRUE(tray->GetWidget()); | 121 ASSERT_TRUE(tray->GetWidget()); |
| 115 | 122 |
| 116 // Add a notification. | 123 // Add a notification. |
| 117 delegate->AddNotification(tray, "test_id1"); | 124 delegate->AddNotification(tray, "test_id1"); |
| 118 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); | 125 EXPECT_EQ(1u, get_message_center()->NotificationCount()); |
| 119 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); | 126 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id1")); |
| 120 delegate->AddNotification(tray, "test_id2"); | 127 delegate->AddNotification(tray, "test_id2"); |
| 121 delegate->AddNotification(tray, "test_id2"); | 128 delegate->AddNotification(tray, "test_id2"); |
| 122 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); | 129 EXPECT_EQ(2u, get_message_center()->NotificationCount()); |
| 123 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); | 130 EXPECT_TRUE(message_center->notification_list()->HasNotification("test_id2")); |
| 124 | 131 |
| 125 // Ensure that updating a notification does not affect the count. | 132 // Ensure that updating a notification does not affect the count. |
| 126 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 133 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 127 delegate->UpdateNotification(tray, "test_id3", "test_id3"); | 134 delegate->UpdateNotification(tray, "test_id3", "test_id3"); |
| 128 EXPECT_EQ(2u, tray->message_center()->NotificationCount()); | 135 EXPECT_EQ(2u, get_message_center()->NotificationCount()); |
| 129 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); | 136 EXPECT_FALSE(delegate->HasNotificationId("test_id2")); |
| 130 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 137 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 131 "test_id2")); | 138 "test_id2")); |
| 132 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); | 139 EXPECT_TRUE(delegate->HasNotificationId("test_id3")); |
| 133 | 140 |
| 134 // Ensure that Removing the first notification removes it from the tray. | 141 // Ensure that Removing the first notification removes it from the tray. |
| 135 delegate->RemoveNotification(tray, "test_id1"); | 142 delegate->RemoveNotification(tray, "test_id1"); |
| 136 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); | 143 EXPECT_FALSE(delegate->HasNotificationId("test_id1")); |
| 137 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 144 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 138 "test_id1")); | 145 "test_id1")); |
| 139 EXPECT_EQ(1u, tray->message_center()->NotificationCount()); | 146 EXPECT_EQ(1u, get_message_center()->NotificationCount()); |
| 140 | 147 |
| 141 // Remove the remianing notification. | 148 // Remove the remianing notification. |
| 142 delegate->RemoveNotification(tray, "test_id3"); | 149 delegate->RemoveNotification(tray, "test_id3"); |
| 143 EXPECT_EQ(0u, tray->message_center()->NotificationCount()); | 150 EXPECT_EQ(0u, get_message_center()->NotificationCount()); |
| 144 EXPECT_FALSE(message_center->notification_list()->HasNotification( | 151 EXPECT_FALSE(message_center->notification_list()->HasNotification( |
| 145 "test_id3")); | 152 "test_id3")); |
| 146 } | 153 } |
| 147 | 154 |
| 148 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { | 155 TEST_F(WebNotificationTrayTest, WebNotificationPopupBubble) { |
| 149 WebNotificationTray* tray = GetWebNotificationTray(); | 156 WebNotificationTray* tray = GetWebNotificationTray(); |
| 150 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 157 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 151 | 158 |
| 152 ASSERT_TRUE(tray->GetWidget()); | 159 ASSERT_TRUE(tray->GetWidget()); |
| 153 | 160 |
| 154 // Adding a notification should show the popup bubble. | 161 // Adding a notification should show the popup bubble. |
| 155 delegate->AddNotification(tray, "test_id1"); | 162 delegate->AddNotification(tray, "test_id1"); |
| 156 EXPECT_TRUE(tray->popup_bubble() != NULL); | 163 EXPECT_TRUE(tray->IsPopupVisible()); |
| 157 | 164 |
| 158 // Updating a notification should not hide the popup bubble. | 165 // Updating a notification should not hide the popup bubble. |
| 159 delegate->AddNotification(tray, "test_id2"); | 166 delegate->AddNotification(tray, "test_id2"); |
| 160 delegate->UpdateNotification(tray, "test_id2", "test_id3"); | 167 delegate->UpdateNotification(tray, "test_id2", "test_id3"); |
| 161 EXPECT_TRUE(tray->popup_bubble() != NULL); | 168 EXPECT_TRUE(tray->IsPopupVisible()); |
| 162 | 169 |
| 163 // Removing the first notification should not hide the popup bubble. | 170 // Removing the first notification should not hide the popup bubble. |
| 164 delegate->RemoveNotification(tray, "test_id1"); | 171 delegate->RemoveNotification(tray, "test_id1"); |
| 165 EXPECT_TRUE(tray->popup_bubble() != NULL); | 172 EXPECT_TRUE(tray->IsPopupVisible()); |
| 166 | 173 |
| 167 // Removing the visible notification should hide the popup bubble. | 174 // Removing the visible notification should hide the popup bubble. |
| 168 delegate->RemoveNotification(tray, "test_id3"); | 175 delegate->RemoveNotification(tray, "test_id3"); |
| 169 EXPECT_TRUE(tray->popup_bubble() == NULL); | 176 EXPECT_FALSE(tray->IsPopupVisible()); |
| 170 } | 177 } |
| 171 | 178 |
| 172 using message_center::NotificationList; | 179 using message_center::NotificationList; |
| 173 | 180 |
| 174 | 181 |
| 175 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { | 182 TEST_F(WebNotificationTrayTest, ManyMessageCenterNotifications) { |
| 176 WebNotificationTray* tray = GetWebNotificationTray(); | 183 WebNotificationTray* tray = GetWebNotificationTray(); |
| 177 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 184 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 178 | 185 |
| 179 // Add the max visible notifications +1, ensure the correct visible number. | 186 // Add the max visible notifications +1, ensure the correct visible number. |
| 180 size_t notifications_to_add = | 187 size_t notifications_to_add = |
| 181 NotificationList::kMaxVisibleMessageCenterNotifications + 1; | 188 NotificationList::kMaxVisibleMessageCenterNotifications + 1; |
| 182 for (size_t i = 0; i < notifications_to_add; ++i) { | 189 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 183 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 190 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 184 delegate->AddNotification(tray, id); | 191 delegate->AddNotification(tray, id); |
| 185 } | 192 } |
| 186 tray->ShowMessageCenterBubble(); | 193 tray->message_center_tray_->ShowMessageCenterBubble(); |
| 187 RunAllPendingInMessageLoop(); | 194 RunAllPendingInMessageLoop(); |
| 188 EXPECT_TRUE(tray->message_center_bubble() != NULL); | 195 EXPECT_TRUE(tray->message_center_bubble() != NULL); |
| 189 EXPECT_EQ(notifications_to_add, | 196 EXPECT_EQ(notifications_to_add, |
| 190 tray->message_center()->NotificationCount()); | 197 get_message_center()->NotificationCount()); |
| 191 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, | 198 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, |
| 192 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); | 199 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); |
| 193 } | 200 } |
| 194 | 201 |
| 195 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { | 202 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { |
| 196 WebNotificationTray* tray = GetWebNotificationTray(); | 203 WebNotificationTray* tray = GetWebNotificationTray(); |
| 197 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); | 204 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center())); |
| 198 | 205 |
| 199 // Add the max visible popup notifications +1, ensure the correct num visible. | 206 // Add the max visible popup notifications +1, ensure the correct num visible. |
| 200 size_t notifications_to_add = | 207 size_t notifications_to_add = |
| 201 NotificationList::kMaxVisiblePopupNotifications + 1; | 208 NotificationList::kMaxVisiblePopupNotifications + 1; |
| 202 for (size_t i = 0; i < notifications_to_add; ++i) { | 209 for (size_t i = 0; i < notifications_to_add; ++i) { |
| 203 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); | 210 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); |
| 204 delegate->AddNotification(tray, id); | 211 delegate->AddNotification(tray, id); |
| 205 } | 212 } |
| 206 // Hide and reshow the bubble so that it is updated immediately, not delayed. | 213 // Hide and reshow the bubble so that it is updated immediately, not delayed. |
| 207 tray->HidePopupBubble(); | 214 tray->SetHidePopupBubble(true); |
| 208 tray->ShowPopupBubble(); | 215 tray->SetHidePopupBubble(false); |
| 209 EXPECT_TRUE(tray->popup_bubble() != NULL); | 216 EXPECT_TRUE(tray->IsPopupVisible()); |
| 210 EXPECT_EQ(notifications_to_add, | 217 EXPECT_EQ(notifications_to_add, |
| 211 tray->message_center()->NotificationCount()); | 218 get_message_center()->NotificationCount()); |
| 212 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, | 219 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, |
| 213 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); | 220 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); |
| 221 get_message_center()->SetDelegate(NULL); | |
| 222 get_message_center()->notification_list()->RemoveAllNotifications(); | |
| 214 } | 223 } |
| 215 | 224 |
| 216 } // namespace ash | 225 } // namespace ash |
| OLD | NEW |