Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: ash/system/web_notification/web_notification_tray_unittest.cc

Issue 11819048: Implement message center on Windows (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge with master, address comments. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.
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
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 #if defined(OS_WIN) 183 #if defined(OS_WIN)
177 // This test seems to tickle a race condition on Metro/Ash causing the test 184 // This test seems to tickle a race condition on Metro/Ash causing the test
178 // suite to crash. 185 // suite to crash.
179 // TODO(robertshield): Fix this. http://crbug.com/170418 186 // TODO(robertshield): Fix this. http://crbug.com/170418
180 if (base::win::GetVersion() >= base::win::VERSION_WIN8) 187 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
181 return; 188 return;
182 #endif 189 #endif
183 190
184 WebNotificationTray* tray = GetWebNotificationTray(); 191 WebNotificationTray* tray = GetWebNotificationTray();
185 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); 192 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center()));
186 193
187 // Add the max visible notifications +1, ensure the correct visible number. 194 // Add the max visible notifications +1, ensure the correct visible number.
188 size_t notifications_to_add = 195 size_t notifications_to_add =
189 NotificationList::kMaxVisibleMessageCenterNotifications + 1; 196 NotificationList::kMaxVisibleMessageCenterNotifications + 1;
190 for (size_t i = 0; i < notifications_to_add; ++i) { 197 for (size_t i = 0; i < notifications_to_add; ++i) {
191 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); 198 std::string id = StringPrintf("test_id%d", static_cast<int>(i));
192 delegate->AddNotification(tray, id); 199 delegate->AddNotification(tray, id);
193 } 200 }
194 tray->ShowMessageCenterBubble(); 201 tray->message_center_tray_->ShowMessageCenterBubble();
195 RunAllPendingInMessageLoop(); 202 RunAllPendingInMessageLoop();
196 EXPECT_TRUE(tray->message_center_bubble() != NULL); 203 EXPECT_TRUE(tray->message_center_bubble() != NULL);
197 EXPECT_EQ(notifications_to_add, 204 EXPECT_EQ(notifications_to_add,
198 tray->message_center()->NotificationCount()); 205 get_message_center()->NotificationCount());
199 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications, 206 EXPECT_EQ(NotificationList::kMaxVisibleMessageCenterNotifications,
200 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest()); 207 tray->GetMessageCenterBubbleForTest()->NumMessageViewsForTest());
201 } 208 }
202 209
203 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) { 210 TEST_F(WebNotificationTrayTest, ManyPopupNotifications) {
204 #if defined(OS_WIN) 211 #if defined(OS_WIN)
205 // This test seems to tickle a race condition on Metro/Ash causing the test 212 // This test seems to tickle a race condition on Metro/Ash causing the test
206 // suite to crash. 213 // suite to crash.
207 // TODO(robertshield): Fix this. http://crbug.com/170418 214 // TODO(robertshield): Fix this. http://crbug.com/170418
208 if (base::win::GetVersion() >= base::win::VERSION_WIN8) 215 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
209 return; 216 return;
210 #endif 217 #endif
211 218
212 WebNotificationTray* tray = GetWebNotificationTray(); 219 WebNotificationTray* tray = GetWebNotificationTray();
213 scoped_ptr<TestDelegate> delegate(new TestDelegate(tray->message_center())); 220 scoped_ptr<TestDelegate> delegate(new TestDelegate(get_message_center()));
214 221
215 // Add the max visible popup notifications +1, ensure the correct num visible. 222 // Add the max visible popup notifications +1, ensure the correct num visible.
216 size_t notifications_to_add = 223 size_t notifications_to_add =
217 NotificationList::kMaxVisiblePopupNotifications + 1; 224 NotificationList::kMaxVisiblePopupNotifications + 1;
218 for (size_t i = 0; i < notifications_to_add; ++i) { 225 for (size_t i = 0; i < notifications_to_add; ++i) {
219 std::string id = StringPrintf("test_id%d", static_cast<int>(i)); 226 std::string id = StringPrintf("test_id%d", static_cast<int>(i));
220 delegate->AddNotification(tray, id); 227 delegate->AddNotification(tray, id);
221 } 228 }
222 // Hide and reshow the bubble so that it is updated immediately, not delayed. 229 // Hide and reshow the bubble so that it is updated immediately, not delayed.
223 tray->HidePopupBubble(); 230 tray->SetHidePopupBubble(true);
224 tray->ShowPopupBubble(); 231 tray->SetHidePopupBubble(false);
225 EXPECT_TRUE(tray->popup_bubble() != NULL); 232 EXPECT_TRUE(tray->IsPopupVisible());
226 EXPECT_EQ(notifications_to_add, 233 EXPECT_EQ(notifications_to_add,
227 tray->message_center()->NotificationCount()); 234 get_message_center()->NotificationCount());
228 EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, 235 // EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications,
229 tray->GetPopupBubbleForTest()->NumMessageViewsForTest()); 236 // tray->GetPopupBubbleForTest()->NumMessageViewsForTest());
stevenjb 2013/01/23 23:14:18 ??
dewittj 2013/01/25 00:49:04 Done.
237 get_message_center()->SetDelegate(NULL);
238 get_message_center()->notification_list()->RemoveAllNotifications();
230 } 239 }
231 240
232 } // namespace ash 241 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698