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
3 // found in the LICENSE file.
5 #include "ui/message_center/notification_list.h"
7 #include "base/basictypes.h"
8 #include "base/i18n/time_formatting.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/message_center/message_center_style.h"
14 #include "ui/message_center/notification_blocker.h"
15 #include "ui/message_center/notification_types.h"
16 #include "ui/message_center/notifier_settings.h"
18 using base::UTF8ToUTF16
;
20 namespace message_center
{
22 class NotificationListTest
: public testing::Test
{
24 NotificationListTest() {}
25 ~NotificationListTest() override
{}
27 void SetUp() override
{
28 notification_list_
.reset(new NotificationList());
33 // Currently NotificationListTest doesn't care about some fields like title or
34 // message, so put a simple template on it. Returns the id of the new
36 std::string
AddNotification(
37 const message_center::RichNotificationData
& optional_fields
) {
39 scoped_ptr
<Notification
> notification(
40 MakeNotification(optional_fields
, &new_id
));
41 notification_list_
->AddNotification(notification
.Pass());
46 std::string
AddNotification() {
47 return AddNotification(message_center::RichNotificationData());
50 // Construct a new notification for testing, but don't add it to the list yet.
51 scoped_ptr
<Notification
> MakeNotification(
52 const message_center::RichNotificationData
& optional_fields
,
53 std::string
* id_out
) {
54 *id_out
= base::StringPrintf(kIdFormat
, counter_
);
55 scoped_ptr
<Notification
> notification(new Notification(
56 message_center::NOTIFICATION_TYPE_SIMPLE
, *id_out
,
57 UTF8ToUTF16(base::StringPrintf(kTitleFormat
, counter_
)),
58 UTF8ToUTF16(base::StringPrintf(kMessageFormat
, counter_
)), gfx::Image(),
59 UTF8ToUTF16(kDisplaySource
), GURL(),
60 NotifierId(NotifierId::APPLICATION
, kExtensionId
), optional_fields
,
62 return notification
.Pass();
65 scoped_ptr
<Notification
> MakeNotification(std::string
* id_out
) {
66 return MakeNotification(message_center::RichNotificationData(), id_out
);
69 // Utility methods of AddNotification.
70 std::string
AddPriorityNotification(NotificationPriority priority
) {
71 message_center::RichNotificationData optional
;
72 optional
.priority
= priority
;
73 return AddNotification(optional
);
76 NotificationList::PopupNotifications
GetPopups() {
77 return notification_list()->GetPopupNotifications(blockers_
, NULL
);
80 size_t GetPopupCounts() {
81 return GetPopups().size();
84 Notification
* GetNotification(const std::string
& id
) {
85 NotificationList::Notifications::iterator iter
=
86 notification_list()->GetNotification(id
);
87 if (iter
== notification_list()->notifications_
.end())
92 NotificationList
* notification_list() { return notification_list_
.get(); }
93 const NotificationBlockers
& blockers() const { return blockers_
; }
95 static const char kIdFormat
[];
96 static const char kTitleFormat
[];
97 static const char kMessageFormat
[];
98 static const char kDisplaySource
[];
99 static const char kExtensionId
[];
102 scoped_ptr
<NotificationList
> notification_list_
;
103 NotificationBlockers blockers_
;
106 DISALLOW_COPY_AND_ASSIGN(NotificationListTest
);
109 bool IsInNotifications(const NotificationList::Notifications
& notifications
,
110 const std::string
& id
) {
111 for (NotificationList::Notifications::const_iterator iter
=
112 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
113 if ((*iter
)->id() == id
)
119 const char NotificationListTest::kIdFormat
[] = "id%ld";
120 const char NotificationListTest::kTitleFormat
[] = "id%ld";
121 const char NotificationListTest::kMessageFormat
[] = "message%ld";
122 const char NotificationListTest::kDisplaySource
[] = "source";
123 const char NotificationListTest::kExtensionId
[] = "ext";
125 TEST_F(NotificationListTest
, Basic
) {
126 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
127 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
129 std::string id0
= AddNotification();
130 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
131 std::string id1
= AddNotification();
132 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
133 EXPECT_EQ(2u, notification_list()->UnreadCount(blockers()));
135 EXPECT_TRUE(notification_list()->HasPopupNotifications(blockers()));
136 EXPECT_TRUE(notification_list()->GetNotificationById(id0
));
137 EXPECT_TRUE(notification_list()->GetNotificationById(id1
));
138 EXPECT_FALSE(notification_list()->GetNotificationById(id1
+ "foo"));
140 EXPECT_EQ(2u, GetPopupCounts());
142 notification_list()->MarkSinglePopupAsShown(id0
, true);
143 notification_list()->MarkSinglePopupAsShown(id1
, true);
144 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
145 EXPECT_EQ(0u, GetPopupCounts());
147 notification_list()->RemoveNotification(id0
);
148 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
149 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
152 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
155 TEST_F(NotificationListTest
, MessageCenterVisible
) {
157 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
158 ASSERT_EQ(1u, notification_list()->UnreadCount(blockers()));
159 ASSERT_EQ(1u, GetPopupCounts());
161 // Make the message center visible. It resets the unread count and popup
163 notification_list()->SetMessageCenterVisible(true, NULL
);
164 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
165 ASSERT_EQ(0u, GetPopupCounts());
168 TEST_F(NotificationListTest
, UnreadCount
) {
169 std::string id0
= AddNotification();
170 std::string id1
= AddNotification();
171 ASSERT_EQ(2u, notification_list()->UnreadCount(blockers()));
173 notification_list()->MarkSinglePopupAsDisplayed(id0
);
174 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
175 notification_list()->MarkSinglePopupAsDisplayed(id0
);
176 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
177 notification_list()->MarkSinglePopupAsDisplayed(id1
);
178 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
181 TEST_F(NotificationListTest
, UpdateNotification
) {
182 std::string id0
= AddNotification();
183 std::string replaced
= id0
+ "_replaced";
184 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
185 scoped_ptr
<Notification
> notification(
186 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
, replaced
,
187 UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"),
188 gfx::Image(), UTF8ToUTF16(kDisplaySource
), GURL(),
189 NotifierId(NotifierId::APPLICATION
, kExtensionId
),
190 message_center::RichNotificationData(), NULL
));
191 notification_list()->UpdateNotificationMessage(id0
, notification
.Pass());
192 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
193 const NotificationList::Notifications notifications
=
194 notification_list()->GetVisibleNotifications(blockers());
195 EXPECT_EQ(replaced
, (*notifications
.begin())->id());
196 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications
.begin())->title());
197 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications
.begin())->message());
200 TEST_F(NotificationListTest
, GetNotificationsByNotifierId
) {
201 NotifierId
id0(NotifierId::APPLICATION
, "ext0");
202 NotifierId
id1(NotifierId::APPLICATION
, "ext1");
203 NotifierId
id2(GURL("http://example.com"));
204 NotifierId
id3(NotifierId::SYSTEM_COMPONENT
, "system-notifier");
205 scoped_ptr
<Notification
> notification(new Notification(
206 message_center::NOTIFICATION_TYPE_SIMPLE
, "id0", UTF8ToUTF16("title0"),
207 UTF8ToUTF16("message0"), gfx::Image(), UTF8ToUTF16("source0"), GURL(),
208 id0
, message_center::RichNotificationData(), NULL
));
209 notification_list()->AddNotification(notification
.Pass());
210 notification
.reset(new Notification(
211 message_center::NOTIFICATION_TYPE_SIMPLE
, "id1", UTF8ToUTF16("title1"),
212 UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source0"), GURL(),
213 id0
, message_center::RichNotificationData(), NULL
));
214 notification_list()->AddNotification(notification
.Pass());
215 notification
.reset(new Notification(
216 message_center::NOTIFICATION_TYPE_SIMPLE
, "id2", UTF8ToUTF16("title1"),
217 UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source1"), GURL(),
218 id0
, message_center::RichNotificationData(), NULL
));
219 notification_list()->AddNotification(notification
.Pass());
220 notification
.reset(new Notification(
221 message_center::NOTIFICATION_TYPE_SIMPLE
, "id3", UTF8ToUTF16("title1"),
222 UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(),
223 id1
, message_center::RichNotificationData(), NULL
));
224 notification_list()->AddNotification(notification
.Pass());
225 notification
.reset(new Notification(
226 message_center::NOTIFICATION_TYPE_SIMPLE
, "id4", UTF8ToUTF16("title1"),
227 UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(),
228 id2
, message_center::RichNotificationData(), NULL
));
229 notification_list()->AddNotification(notification
.Pass());
230 notification
.reset(new Notification(
231 message_center::NOTIFICATION_TYPE_SIMPLE
, "id5", UTF8ToUTF16("title1"),
232 UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(),
233 id3
, message_center::RichNotificationData(), NULL
));
234 notification_list()->AddNotification(notification
.Pass());
236 NotificationList::Notifications by_notifier_id
=
237 notification_list()->GetNotificationsByNotifierId(id0
);
238 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id0"));
239 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id1"));
240 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id2"));
241 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id3"));
242 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id4"));
243 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id5"));
245 by_notifier_id
= notification_list()->GetNotificationsByNotifierId(id1
);
246 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id0"));
247 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id1"));
248 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id2"));
249 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id3"));
250 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id4"));
251 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id5"));
253 by_notifier_id
= notification_list()->GetNotificationsByNotifierId(id2
);
254 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id0"));
255 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id1"));
256 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id2"));
257 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id3"));
258 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id4"));
259 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id5"));
261 by_notifier_id
= notification_list()->GetNotificationsByNotifierId(id3
);
262 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id0"));
263 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id1"));
264 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id2"));
265 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id3"));
266 EXPECT_FALSE(IsInNotifications(by_notifier_id
, "id4"));
267 EXPECT_TRUE(IsInNotifications(by_notifier_id
, "id5"));
270 TEST_F(NotificationListTest
, OldPopupShouldNotBeHidden
) {
271 std::vector
<std::string
> ids
;
272 for (size_t i
= 0; i
<= kMaxVisiblePopupNotifications
; i
++)
273 ids
.push_back(AddNotification());
275 NotificationList::PopupNotifications popups
= GetPopups();
276 // The popup should contain the oldest kMaxVisiblePopupNotifications. Newer
277 // one should come earlier in the popup list. It means, the last element
278 // of |popups| should be the firstly added one, and so on.
279 EXPECT_EQ(kMaxVisiblePopupNotifications
, popups
.size());
280 NotificationList::PopupNotifications::const_reverse_iterator iter
=
282 for (size_t i
= 0; i
< kMaxVisiblePopupNotifications
; ++i
, ++iter
) {
283 EXPECT_EQ(ids
[i
], (*iter
)->id()) << i
;
286 for (NotificationList::PopupNotifications::const_iterator iter
=
287 popups
.begin(); iter
!= popups
.end(); ++iter
) {
288 notification_list()->MarkSinglePopupAsShown((*iter
)->id(), false);
291 popups
= GetPopups();
292 EXPECT_EQ(1u, popups
.size());
293 EXPECT_EQ(ids
[ids
.size() - 1], (*popups
.begin())->id());
296 TEST_F(NotificationListTest
, Priority
) {
297 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
298 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
300 // Default priority has the limit on the number of the popups.
301 for (size_t i
= 0; i
<= kMaxVisiblePopupNotifications
; ++i
)
303 EXPECT_EQ(kMaxVisiblePopupNotifications
+ 1,
304 notification_list()->NotificationCount(blockers()));
305 EXPECT_EQ(kMaxVisiblePopupNotifications
, GetPopupCounts());
307 // Low priority: not visible to popups.
308 notification_list()->SetMessageCenterVisible(true, NULL
);
309 notification_list()->SetMessageCenterVisible(false, NULL
);
310 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
311 AddPriorityNotification(LOW_PRIORITY
);
312 EXPECT_EQ(kMaxVisiblePopupNotifications
+ 2,
313 notification_list()->NotificationCount(blockers()));
314 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
315 EXPECT_EQ(0u, GetPopupCounts());
317 // Minimum priority: doesn't update the unread count.
318 AddPriorityNotification(MIN_PRIORITY
);
319 EXPECT_EQ(kMaxVisiblePopupNotifications
+ 3,
320 notification_list()->NotificationCount(blockers()));
321 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
322 EXPECT_EQ(0u, GetPopupCounts());
324 NotificationList::Notifications notifications
=
325 notification_list()->GetVisibleNotifications(blockers());
326 for (NotificationList::Notifications::const_iterator iter
=
327 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
328 notification_list()->RemoveNotification((*iter
)->id());
331 // Higher priority: no limits to the number of popups.
332 for (size_t i
= 0; i
< kMaxVisiblePopupNotifications
* 2; ++i
)
333 AddPriorityNotification(HIGH_PRIORITY
);
334 for (size_t i
= 0; i
< kMaxVisiblePopupNotifications
* 2; ++i
)
335 AddPriorityNotification(MAX_PRIORITY
);
336 EXPECT_EQ(kMaxVisiblePopupNotifications
* 4,
337 notification_list()->NotificationCount(blockers()));
338 EXPECT_EQ(kMaxVisiblePopupNotifications
* 4, GetPopupCounts());
341 TEST_F(NotificationListTest
, HasPopupsWithPriority
) {
342 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
343 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
345 AddPriorityNotification(MIN_PRIORITY
);
346 AddPriorityNotification(MAX_PRIORITY
);
348 EXPECT_EQ(1u, GetPopupCounts());
351 TEST_F(NotificationListTest
, HasPopupsWithSystemPriority
) {
352 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
353 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
355 std::string normal_id
= AddPriorityNotification(DEFAULT_PRIORITY
);
356 std::string system_id
= AddNotification();
357 GetNotification(system_id
)->SetSystemPriority();
359 EXPECT_EQ(2u, GetPopupCounts());
361 notification_list()->MarkSinglePopupAsDisplayed(normal_id
);
362 notification_list()->MarkSinglePopupAsDisplayed(system_id
);
364 notification_list()->MarkSinglePopupAsShown(normal_id
, false);
365 notification_list()->MarkSinglePopupAsShown(system_id
, false);
367 notification_list()->SetMessageCenterVisible(true, NULL
);
368 notification_list()->SetMessageCenterVisible(false, NULL
);
369 EXPECT_EQ(1u, GetPopupCounts());
371 // Mark as read -- emulation of mouse click.
372 notification_list()->MarkSinglePopupAsShown(system_id
, true);
373 EXPECT_EQ(0u, GetPopupCounts());
376 TEST_F(NotificationListTest
, PriorityPromotion
) {
377 std::string id0
= AddPriorityNotification(LOW_PRIORITY
);
378 std::string replaced
= id0
+ "_replaced";
379 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
380 EXPECT_EQ(0u, GetPopupCounts());
381 message_center::RichNotificationData optional
;
382 optional
.priority
= 1;
383 scoped_ptr
<Notification
> notification(new Notification(
384 message_center::NOTIFICATION_TYPE_SIMPLE
, replaced
,
385 UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), gfx::Image(),
386 UTF8ToUTF16(kDisplaySource
), GURL(),
387 NotifierId(NotifierId::APPLICATION
, kExtensionId
), optional
, NULL
));
388 notification_list()->UpdateNotificationMessage(id0
, notification
.Pass());
389 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
390 EXPECT_EQ(1u, GetPopupCounts());
391 const NotificationList::Notifications notifications
=
392 notification_list()->GetVisibleNotifications(blockers());
393 EXPECT_EQ(replaced
, (*notifications
.begin())->id());
394 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications
.begin())->title());
395 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications
.begin())->message());
396 EXPECT_EQ(1, (*notifications
.begin())->priority());
399 TEST_F(NotificationListTest
, PriorityPromotionWithPopups
) {
400 std::string id0
= AddPriorityNotification(LOW_PRIORITY
);
401 std::string id1
= AddPriorityNotification(DEFAULT_PRIORITY
);
402 EXPECT_EQ(1u, GetPopupCounts());
403 notification_list()->MarkSinglePopupAsShown(id1
, true);
404 EXPECT_EQ(0u, GetPopupCounts());
406 // id0 promoted to LOW->DEFAULT, it'll appear as toast (popup).
407 message_center::RichNotificationData priority
;
408 priority
.priority
= DEFAULT_PRIORITY
;
409 scoped_ptr
<Notification
> notification(new Notification(
410 message_center::NOTIFICATION_TYPE_SIMPLE
, id0
, UTF8ToUTF16("newtitle"),
411 UTF8ToUTF16("newbody"), gfx::Image(), UTF8ToUTF16(kDisplaySource
), GURL(),
412 NotifierId(NotifierId::APPLICATION
, kExtensionId
), priority
, NULL
));
413 notification_list()->UpdateNotificationMessage(id0
, notification
.Pass());
414 EXPECT_EQ(1u, GetPopupCounts());
415 notification_list()->MarkSinglePopupAsShown(id0
, true);
416 EXPECT_EQ(0u, GetPopupCounts());
418 // update with no promotion change for id0, it won't appear as a toast.
419 notification
.reset(new Notification(
420 message_center::NOTIFICATION_TYPE_SIMPLE
, id0
, UTF8ToUTF16("newtitle2"),
421 UTF8ToUTF16("newbody2"), gfx::Image(), UTF8ToUTF16(kDisplaySource
),
422 GURL(), NotifierId(NotifierId::APPLICATION
, kExtensionId
), priority
,
424 notification_list()->UpdateNotificationMessage(id0
, notification
.Pass());
425 EXPECT_EQ(0u, GetPopupCounts());
427 // id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup).
428 priority
.priority
= HIGH_PRIORITY
;
429 notification
.reset(new Notification(
430 message_center::NOTIFICATION_TYPE_SIMPLE
, id1
, UTF8ToUTF16("newtitle"),
431 UTF8ToUTF16("newbody"), gfx::Image(), UTF8ToUTF16(kDisplaySource
), GURL(),
432 NotifierId(NotifierId::APPLICATION
, kExtensionId
), priority
, NULL
));
433 notification_list()->UpdateNotificationMessage(id1
, notification
.Pass());
434 EXPECT_EQ(1u, GetPopupCounts());
435 notification_list()->MarkSinglePopupAsShown(id1
, true);
436 EXPECT_EQ(0u, GetPopupCounts());
438 // id1 promoted to HIGH->MAX, it'll appear as toast again.
439 priority
.priority
= MAX_PRIORITY
;
440 notification
.reset(new Notification(
441 message_center::NOTIFICATION_TYPE_SIMPLE
, id1
, UTF8ToUTF16("newtitle2"),
442 UTF8ToUTF16("newbody2"), gfx::Image(), UTF8ToUTF16(kDisplaySource
),
443 GURL(), NotifierId(NotifierId::APPLICATION
, kExtensionId
), priority
,
445 notification_list()->UpdateNotificationMessage(id1
, notification
.Pass());
446 EXPECT_EQ(1u, GetPopupCounts());
447 notification_list()->MarkSinglePopupAsShown(id1
, true);
448 EXPECT_EQ(0u, GetPopupCounts());
450 // id1 demoted to MAX->DEFAULT, no appearing as toast.
451 priority
.priority
= DEFAULT_PRIORITY
;
452 notification
.reset(new Notification(
453 message_center::NOTIFICATION_TYPE_SIMPLE
, id1
, UTF8ToUTF16("newtitle3"),
454 UTF8ToUTF16("newbody3"), gfx::Image(), UTF8ToUTF16(kDisplaySource
),
455 GURL(), NotifierId(NotifierId::APPLICATION
, kExtensionId
), priority
,
457 notification_list()->UpdateNotificationMessage(id1
, notification
.Pass());
458 EXPECT_EQ(0u, GetPopupCounts());
461 TEST_F(NotificationListTest
, WebNotificationUpdatePromotion
) {
462 std::string notification_id
= "replaced-web-notification";
463 scoped_ptr
<Notification
> original_notification(new Notification(
464 message_center::NOTIFICATION_TYPE_SIMPLE
, notification_id
,
465 UTF8ToUTF16("Web Notification"), UTF8ToUTF16("Notification contents"),
466 gfx::Image(), UTF8ToUTF16(kDisplaySource
), GURL(),
467 NotifierId(GURL("https://example.com/")),
468 message_center::RichNotificationData(), NULL
));
470 EXPECT_EQ(0u, GetPopupCounts());
471 notification_list()->AddNotification(original_notification
.Pass());
472 EXPECT_EQ(1u, GetPopupCounts());
474 notification_list()->MarkSinglePopupAsShown(notification_id
, true);
475 EXPECT_EQ(0u, GetPopupCounts());
477 scoped_ptr
<Notification
> replaced_notification(new Notification(
478 message_center::NOTIFICATION_TYPE_SIMPLE
, notification_id
,
479 UTF8ToUTF16("Web Notification Replacement"),
480 UTF8ToUTF16("New notification contents"), gfx::Image(),
481 UTF8ToUTF16(kDisplaySource
), GURL(),
482 NotifierId(GURL("https://example.com/")),
483 message_center::RichNotificationData(), NULL
));
485 // Web Notifications will be re-shown as popups even if their priority didn't
486 // change, to match the behavior of the Web Notification API.
487 notification_list()->UpdateNotificationMessage(notification_id
,
488 replaced_notification
.Pass());
489 EXPECT_EQ(1u, GetPopupCounts());
492 TEST_F(NotificationListTest
, NotificationOrderAndPriority
) {
493 base::Time now
= base::Time::Now();
494 message_center::RichNotificationData optional
;
495 optional
.timestamp
= now
;
496 optional
.priority
= 2;
497 std::string max_id
= AddNotification(optional
);
499 now
+= base::TimeDelta::FromSeconds(1);
500 optional
.timestamp
= now
;
501 optional
.priority
= 1;
502 std::string high_id
= AddNotification(optional
);
504 now
+= base::TimeDelta::FromSeconds(1);
505 optional
.timestamp
= now
;
506 optional
.priority
= 0;
507 std::string default_id
= AddNotification(optional
);
510 // Popups: latest comes first.
511 NotificationList::PopupNotifications popups
= GetPopups();
512 EXPECT_EQ(3u, popups
.size());
513 NotificationList::PopupNotifications::const_iterator iter
= popups
.begin();
514 EXPECT_EQ(default_id
, (*iter
)->id());
516 EXPECT_EQ(high_id
, (*iter
)->id());
518 EXPECT_EQ(max_id
, (*iter
)->id());
521 // Notifications: high priority comes ealier.
522 const NotificationList::Notifications notifications
=
523 notification_list()->GetVisibleNotifications(blockers());
524 EXPECT_EQ(3u, notifications
.size());
525 NotificationList::Notifications::const_iterator iter
=
526 notifications
.begin();
527 EXPECT_EQ(max_id
, (*iter
)->id());
529 EXPECT_EQ(high_id
, (*iter
)->id());
531 EXPECT_EQ(default_id
, (*iter
)->id());
535 TEST_F(NotificationListTest
, MarkSinglePopupAsShown
) {
536 std::string id1
= AddNotification();
537 std::string id2
= AddNotification();
538 std::string id3
= AddNotification();
539 ASSERT_EQ(3u, notification_list()->NotificationCount(blockers()));
540 ASSERT_EQ(std::min(static_cast<size_t>(3u), kMaxVisiblePopupNotifications
),
542 notification_list()->MarkSinglePopupAsDisplayed(id1
);
543 notification_list()->MarkSinglePopupAsDisplayed(id2
);
544 notification_list()->MarkSinglePopupAsDisplayed(id3
);
546 notification_list()->MarkSinglePopupAsShown(id2
, true);
547 notification_list()->MarkSinglePopupAsShown(id3
, false);
548 EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
549 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
550 EXPECT_EQ(1u, GetPopupCounts());
551 NotificationList::PopupNotifications popups
= GetPopups();
552 EXPECT_EQ(id1
, (*popups
.begin())->id());
554 // The notifications in the NotificationCenter are unaffected by popups shown.
555 NotificationList::Notifications notifications
=
556 notification_list()->GetVisibleNotifications(blockers());
557 NotificationList::Notifications::const_iterator iter
= notifications
.begin();
558 EXPECT_EQ(id3
, (*iter
)->id());
560 EXPECT_EQ(id2
, (*iter
)->id());
562 EXPECT_EQ(id1
, (*iter
)->id());
565 TEST_F(NotificationListTest
, UpdateAfterMarkedAsShown
) {
566 std::string id1
= AddNotification();
567 std::string id2
= AddNotification();
568 notification_list()->MarkSinglePopupAsDisplayed(id1
);
569 notification_list()->MarkSinglePopupAsDisplayed(id2
);
571 EXPECT_EQ(2u, GetPopupCounts());
573 const Notification
* n1
= GetNotification(id1
);
574 EXPECT_FALSE(n1
->shown_as_popup());
575 EXPECT_TRUE(n1
->IsRead());
577 notification_list()->MarkSinglePopupAsShown(id1
, true);
579 n1
= GetNotification(id1
);
580 EXPECT_TRUE(n1
->shown_as_popup());
581 EXPECT_TRUE(n1
->IsRead());
583 const std::string
replaced("test-replaced-id");
584 scoped_ptr
<Notification
> notification(
585 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
, replaced
,
586 UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"),
587 gfx::Image(), UTF8ToUTF16(kDisplaySource
), GURL(),
588 NotifierId(NotifierId::APPLICATION
, kExtensionId
),
589 message_center::RichNotificationData(), NULL
));
590 notification_list()->UpdateNotificationMessage(id1
, notification
.Pass());
591 n1
= GetNotification(id1
);
592 EXPECT_TRUE(n1
== NULL
);
593 const Notification
* nr
= GetNotification(replaced
);
594 EXPECT_TRUE(nr
->shown_as_popup());
595 EXPECT_TRUE(nr
->IsRead());
598 TEST_F(NotificationListTest
, QuietMode
) {
599 notification_list()->SetQuietMode(true);
601 AddPriorityNotification(HIGH_PRIORITY
);
602 AddPriorityNotification(MAX_PRIORITY
);
603 EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
604 EXPECT_EQ(0u, GetPopupCounts());
606 notification_list()->SetQuietMode(false);
608 EXPECT_EQ(4u, notification_list()->NotificationCount(blockers()));
609 EXPECT_EQ(1u, GetPopupCounts());
611 // TODO(mukai): Add test of quiet mode with expiration.
614 // Verifies that unread_count doesn't become negative.
615 TEST_F(NotificationListTest
, UnreadCountNoNegative
) {
616 std::string id
= AddNotification();
617 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
619 notification_list()->MarkSinglePopupAsDisplayed(id
);
620 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
621 notification_list()->MarkSinglePopupAsShown(
622 id
, false /* mark_notification_as_read */);
623 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
625 // Updates the notification and verifies unread_count doesn't change.
626 scoped_ptr
<Notification
> updated_notification(new Notification(
627 message_center::NOTIFICATION_TYPE_SIMPLE
, id
, UTF8ToUTF16("updated"),
628 UTF8ToUTF16("updated"), gfx::Image(), base::string16(), GURL(),
629 NotifierId(), RichNotificationData(), NULL
));
630 notification_list()->AddNotification(updated_notification
.Pass());
631 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
634 TEST_F(NotificationListTest
, TestPushingShownNotification
) {
635 // Create a notification and mark it as shown.
637 scoped_ptr
<Notification
> notification(MakeNotification(&id1
));
638 notification
->set_shown_as_popup(true);
640 // Call PushNotification on this notification.
641 notification_list()->PushNotification(notification
.Pass());
643 // Ensure it is still marked as shown.
644 EXPECT_TRUE(GetNotification(id1
)->shown_as_popup());
647 TEST_F(NotificationListTest
, TestHasNotificationOfType
) {
648 std::string id
= AddNotification();
650 EXPECT_TRUE(notification_list()->HasNotificationOfType(
651 id
, message_center::NOTIFICATION_TYPE_SIMPLE
));
652 EXPECT_FALSE(notification_list()->HasNotificationOfType(
653 id
, message_center::NOTIFICATION_TYPE_PROGRESS
));
655 scoped_ptr
<Notification
> updated_notification(new Notification(
656 message_center::NOTIFICATION_TYPE_PROGRESS
, id
, UTF8ToUTF16("updated"),
657 UTF8ToUTF16("updated"), gfx::Image(), base::string16(), GURL(),
658 NotifierId(), RichNotificationData(), NULL
));
659 notification_list()->AddNotification(updated_notification
.Pass());
661 EXPECT_FALSE(notification_list()->HasNotificationOfType(
662 id
, message_center::NOTIFICATION_TYPE_SIMPLE
));
663 EXPECT_TRUE(notification_list()->HasNotificationOfType(
664 id
, message_center::NOTIFICATION_TYPE_PROGRESS
));
667 } // namespace message_center