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