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