NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / ui / message_center / notification_list_unittest.cc
blob7b27eb36c655f8b8abdca88c46c1a4256988fadf
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 {
23 public:
24 NotificationListTest() {}
25 virtual ~NotificationListTest() {}
27 virtual void SetUp() {
28 notification_list_.reset(new NotificationList());
29 counter_ = 0;
32 protected:
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
35 // notification.
36 std::string AddNotification(
37 const message_center::RichNotificationData& optional_fields) {
38 std::string new_id;
39 scoped_ptr<Notification> notification(
40 MakeNotification(optional_fields, &new_id));
41 notification_list_->AddNotification(notification.Pass());
42 counter_++;
43 return new_id;
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,
57 *id_out,
58 UTF8ToUTF16(base::StringPrintf(kTitleFormat, counter_)),
59 UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)),
60 gfx::Image(),
61 UTF8ToUTF16(kDisplaySource),
62 NotifierId(NotifierId::APPLICATION, kExtensionId),
63 optional_fields,
64 NULL));
65 return notification.Pass();
68 scoped_ptr<Notification> MakeNotification(std::string* id_out) {
69 return MakeNotification(message_center::RichNotificationData(), id_out);
72 // Utility methods of AddNotification.
73 std::string AddPriorityNotification(NotificationPriority priority) {
74 message_center::RichNotificationData optional;
75 optional.priority = priority;
76 return AddNotification(optional);
79 NotificationList::PopupNotifications GetPopups() {
80 return notification_list()->GetPopupNotifications(blockers_, NULL);
83 size_t GetPopupCounts() {
84 return GetPopups().size();
87 Notification* GetNotification(const std::string& id) {
88 NotificationList::Notifications::iterator iter =
89 notification_list()->GetNotification(id);
90 if (iter == notification_list()->notifications_.end())
91 return NULL;
92 return *iter;
95 NotificationList* notification_list() { return notification_list_.get(); }
96 const NotificationBlockers& blockers() const { return blockers_; }
98 static const char kIdFormat[];
99 static const char kTitleFormat[];
100 static const char kMessageFormat[];
101 static const char kDisplaySource[];
102 static const char kExtensionId[];
104 private:
105 scoped_ptr<NotificationList> notification_list_;
106 NotificationBlockers blockers_;
107 size_t counter_;
109 DISALLOW_COPY_AND_ASSIGN(NotificationListTest);
112 bool IsInNotifications(const NotificationList::Notifications& notifications,
113 const std::string& id) {
114 for (NotificationList::Notifications::const_iterator iter =
115 notifications.begin(); iter != notifications.end(); ++iter) {
116 if ((*iter)->id() == id)
117 return true;
119 return false;
122 const char NotificationListTest::kIdFormat[] = "id%ld";
123 const char NotificationListTest::kTitleFormat[] = "id%ld";
124 const char NotificationListTest::kMessageFormat[] = "message%ld";
125 const char NotificationListTest::kDisplaySource[] = "source";
126 const char NotificationListTest::kExtensionId[] = "ext";
128 TEST_F(NotificationListTest, Basic) {
129 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
130 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
132 std::string id0 = AddNotification();
133 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
134 std::string id1 = AddNotification();
135 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
136 EXPECT_EQ(2u, notification_list()->UnreadCount(blockers()));
138 EXPECT_TRUE(notification_list()->HasPopupNotifications(blockers()));
139 EXPECT_TRUE(notification_list()->HasNotification(id0));
140 EXPECT_TRUE(notification_list()->HasNotification(id1));
141 EXPECT_FALSE(notification_list()->HasNotification(id1 + "foo"));
143 EXPECT_EQ(2u, GetPopupCounts());
145 notification_list()->MarkSinglePopupAsShown(id0, true);
146 notification_list()->MarkSinglePopupAsShown(id1, true);
147 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
148 EXPECT_EQ(0u, GetPopupCounts());
150 notification_list()->RemoveNotification(id0);
151 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
152 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
154 AddNotification();
155 EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
158 TEST_F(NotificationListTest, MessageCenterVisible) {
159 AddNotification();
160 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
161 ASSERT_EQ(1u, notification_list()->UnreadCount(blockers()));
162 ASSERT_EQ(1u, GetPopupCounts());
164 // Make the message center visible. It resets the unread count and popup
165 // counts.
166 notification_list()->SetMessageCenterVisible(true, NULL);
167 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
168 ASSERT_EQ(0u, GetPopupCounts());
171 TEST_F(NotificationListTest, UnreadCount) {
172 std::string id0 = AddNotification();
173 std::string id1 = AddNotification();
174 ASSERT_EQ(2u, notification_list()->UnreadCount(blockers()));
176 notification_list()->MarkSinglePopupAsDisplayed(id0);
177 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
178 notification_list()->MarkSinglePopupAsDisplayed(id0);
179 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
180 notification_list()->MarkSinglePopupAsDisplayed(id1);
181 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
184 TEST_F(NotificationListTest, UpdateNotification) {
185 std::string id0 = AddNotification();
186 std::string replaced = id0 + "_replaced";
187 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
188 scoped_ptr<Notification> notification(
189 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
190 replaced,
191 UTF8ToUTF16("newtitle"),
192 UTF8ToUTF16("newbody"),
193 gfx::Image(),
194 UTF8ToUTF16(kDisplaySource),
195 NotifierId(NotifierId::APPLICATION, kExtensionId),
196 message_center::RichNotificationData(),
197 NULL));
198 notification_list()->UpdateNotificationMessage(id0, notification.Pass());
199 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
200 const NotificationList::Notifications notifications =
201 notification_list()->GetVisibleNotifications(blockers());
202 EXPECT_EQ(replaced, (*notifications.begin())->id());
203 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title());
204 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message());
207 TEST_F(NotificationListTest, GetNotificationsByNotifierId) {
208 NotifierId id0(NotifierId::APPLICATION, "ext0");
209 NotifierId id1(NotifierId::APPLICATION, "ext1");
210 NotifierId id2(GURL("http://example.com"));
211 NotifierId id3(NotifierId::SYSTEM_COMPONENT, "system-notifier");
212 scoped_ptr<Notification> notification(
213 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
214 "id0",
215 UTF8ToUTF16("title0"),
216 UTF8ToUTF16("message0"),
217 gfx::Image(),
218 UTF8ToUTF16("source0"),
219 id0,
220 message_center::RichNotificationData(),
221 NULL));
222 notification_list()->AddNotification(notification.Pass());
223 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
224 "id1",
225 UTF8ToUTF16("title1"),
226 UTF8ToUTF16("message1"),
227 gfx::Image(),
228 UTF8ToUTF16("source0"),
229 id0,
230 message_center::RichNotificationData(),
231 NULL));
232 notification_list()->AddNotification(notification.Pass());
233 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
234 "id2",
235 UTF8ToUTF16("title1"),
236 UTF8ToUTF16("message1"),
237 gfx::Image(),
238 UTF8ToUTF16("source1"),
239 id0,
240 message_center::RichNotificationData(),
241 NULL));
242 notification_list()->AddNotification(notification.Pass());
243 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
244 "id3",
245 UTF8ToUTF16("title1"),
246 UTF8ToUTF16("message1"),
247 gfx::Image(),
248 UTF8ToUTF16("source2"),
249 id1,
250 message_center::RichNotificationData(),
251 NULL));
252 notification_list()->AddNotification(notification.Pass());
253 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
254 "id4",
255 UTF8ToUTF16("title1"),
256 UTF8ToUTF16("message1"),
257 gfx::Image(),
258 UTF8ToUTF16("source2"),
259 id2,
260 message_center::RichNotificationData(),
261 NULL));
262 notification_list()->AddNotification(notification.Pass());
263 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
264 "id5",
265 UTF8ToUTF16("title1"),
266 UTF8ToUTF16("message1"),
267 gfx::Image(),
268 UTF8ToUTF16("source2"),
269 id3,
270 message_center::RichNotificationData(),
271 NULL));
272 notification_list()->AddNotification(notification.Pass());
274 NotificationList::Notifications by_notifier_id =
275 notification_list()->GetNotificationsByNotifierId(id0);
276 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id0"));
277 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id1"));
278 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id2"));
279 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
280 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
281 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
283 by_notifier_id = notification_list()->GetNotificationsByNotifierId(id1);
284 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
285 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
286 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
287 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id3"));
288 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
289 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
291 by_notifier_id = notification_list()->GetNotificationsByNotifierId(id2);
292 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
293 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
294 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
295 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
296 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id4"));
297 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
299 by_notifier_id = notification_list()->GetNotificationsByNotifierId(id3);
300 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
301 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
302 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
303 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
304 EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
305 EXPECT_TRUE(IsInNotifications(by_notifier_id, "id5"));
308 TEST_F(NotificationListTest, OldPopupShouldNotBeHidden) {
309 std::vector<std::string> ids;
310 for (size_t i = 0; i <= kMaxVisiblePopupNotifications; i++)
311 ids.push_back(AddNotification());
313 NotificationList::PopupNotifications popups = GetPopups();
314 // The popup should contain the oldest kMaxVisiblePopupNotifications. Newer
315 // one should come earlier in the popup list. It means, the last element
316 // of |popups| should be the firstly added one, and so on.
317 EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size());
318 NotificationList::PopupNotifications::const_reverse_iterator iter =
319 popups.rbegin();
320 for (size_t i = 0; i < kMaxVisiblePopupNotifications; ++i, ++iter) {
321 EXPECT_EQ(ids[i], (*iter)->id()) << i;
324 for (NotificationList::PopupNotifications::const_iterator iter =
325 popups.begin(); iter != popups.end(); ++iter) {
326 notification_list()->MarkSinglePopupAsShown((*iter)->id(), false);
328 popups.clear();
329 popups = GetPopups();
330 EXPECT_EQ(1u, popups.size());
331 EXPECT_EQ(ids[ids.size() - 1], (*popups.begin())->id());
334 TEST_F(NotificationListTest, Priority) {
335 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
336 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
338 // Default priority has the limit on the number of the popups.
339 for (size_t i = 0; i <= kMaxVisiblePopupNotifications; ++i)
340 AddNotification();
341 EXPECT_EQ(kMaxVisiblePopupNotifications + 1,
342 notification_list()->NotificationCount(blockers()));
343 EXPECT_EQ(kMaxVisiblePopupNotifications, GetPopupCounts());
345 // Low priority: not visible to popups.
346 notification_list()->SetMessageCenterVisible(true, NULL);
347 notification_list()->SetMessageCenterVisible(false, NULL);
348 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
349 AddPriorityNotification(LOW_PRIORITY);
350 EXPECT_EQ(kMaxVisiblePopupNotifications + 2,
351 notification_list()->NotificationCount(blockers()));
352 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
353 EXPECT_EQ(0u, GetPopupCounts());
355 // Minimum priority: doesn't update the unread count.
356 AddPriorityNotification(MIN_PRIORITY);
357 EXPECT_EQ(kMaxVisiblePopupNotifications + 3,
358 notification_list()->NotificationCount(blockers()));
359 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
360 EXPECT_EQ(0u, GetPopupCounts());
362 NotificationList::Notifications notifications =
363 notification_list()->GetVisibleNotifications(blockers());
364 for (NotificationList::Notifications::const_iterator iter =
365 notifications.begin(); iter != notifications.end(); ++iter) {
366 notification_list()->RemoveNotification((*iter)->id());
369 // Higher priority: no limits to the number of popups.
370 for (size_t i = 0; i < kMaxVisiblePopupNotifications * 2; ++i)
371 AddPriorityNotification(HIGH_PRIORITY);
372 for (size_t i = 0; i < kMaxVisiblePopupNotifications * 2; ++i)
373 AddPriorityNotification(MAX_PRIORITY);
374 EXPECT_EQ(kMaxVisiblePopupNotifications * 4,
375 notification_list()->NotificationCount(blockers()));
376 EXPECT_EQ(kMaxVisiblePopupNotifications * 4, GetPopupCounts());
379 TEST_F(NotificationListTest, HasPopupsWithPriority) {
380 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
381 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
383 AddPriorityNotification(MIN_PRIORITY);
384 AddPriorityNotification(MAX_PRIORITY);
386 EXPECT_EQ(1u, GetPopupCounts());
389 TEST_F(NotificationListTest, HasPopupsWithSystemPriority) {
390 ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
391 ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
393 std::string normal_id = AddPriorityNotification(DEFAULT_PRIORITY);
394 std::string system_id = AddNotification();
395 GetNotification(system_id)->SetSystemPriority();
397 EXPECT_EQ(2u, GetPopupCounts());
399 notification_list()->MarkSinglePopupAsDisplayed(normal_id);
400 notification_list()->MarkSinglePopupAsDisplayed(system_id);
402 notification_list()->MarkSinglePopupAsShown(normal_id, false);
403 notification_list()->MarkSinglePopupAsShown(system_id, false);
405 notification_list()->SetMessageCenterVisible(true, NULL);
406 notification_list()->SetMessageCenterVisible(false, NULL);
407 EXPECT_EQ(1u, GetPopupCounts());
409 // Mark as read -- emulation of mouse click.
410 notification_list()->MarkSinglePopupAsShown(system_id, true);
411 EXPECT_EQ(0u, GetPopupCounts());
414 TEST_F(NotificationListTest, PriorityPromotion) {
415 std::string id0 = AddPriorityNotification(LOW_PRIORITY);
416 std::string replaced = id0 + "_replaced";
417 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
418 EXPECT_EQ(0u, GetPopupCounts());
419 message_center::RichNotificationData optional;
420 optional.priority = 1;
421 scoped_ptr<Notification> notification(
422 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
423 replaced,
424 UTF8ToUTF16("newtitle"),
425 UTF8ToUTF16("newbody"),
426 gfx::Image(),
427 UTF8ToUTF16(kDisplaySource),
428 NotifierId(NotifierId::APPLICATION, kExtensionId),
429 optional,
430 NULL));
431 notification_list()->UpdateNotificationMessage(id0, notification.Pass());
432 EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
433 EXPECT_EQ(1u, GetPopupCounts());
434 const NotificationList::Notifications notifications =
435 notification_list()->GetVisibleNotifications(blockers());
436 EXPECT_EQ(replaced, (*notifications.begin())->id());
437 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title());
438 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message());
439 EXPECT_EQ(1, (*notifications.begin())->priority());
442 TEST_F(NotificationListTest, PriorityPromotionWithPopups) {
443 std::string id0 = AddPriorityNotification(LOW_PRIORITY);
444 std::string id1 = AddPriorityNotification(DEFAULT_PRIORITY);
445 EXPECT_EQ(1u, GetPopupCounts());
446 notification_list()->MarkSinglePopupAsShown(id1, true);
447 EXPECT_EQ(0u, GetPopupCounts());
449 // id0 promoted to LOW->DEFAULT, it'll appear as toast (popup).
450 message_center::RichNotificationData priority;
451 priority.priority = DEFAULT_PRIORITY;
452 scoped_ptr<Notification> notification(
453 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
454 id0,
455 UTF8ToUTF16("newtitle"),
456 UTF8ToUTF16("newbody"),
457 gfx::Image(),
458 UTF8ToUTF16(kDisplaySource),
459 NotifierId(NotifierId::APPLICATION, kExtensionId),
460 priority,
461 NULL));
462 notification_list()->UpdateNotificationMessage(id0, notification.Pass());
463 EXPECT_EQ(1u, GetPopupCounts());
464 notification_list()->MarkSinglePopupAsShown(id0, true);
465 EXPECT_EQ(0u, GetPopupCounts());
467 // update with no promotion change for id0, it won't appear as a toast.
468 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
469 id0,
470 UTF8ToUTF16("newtitle2"),
471 UTF8ToUTF16("newbody2"),
472 gfx::Image(),
473 UTF8ToUTF16(kDisplaySource),
474 NotifierId(NotifierId::APPLICATION,
475 kExtensionId),
476 priority,
477 NULL));
478 notification_list()->UpdateNotificationMessage(id0, notification.Pass());
479 EXPECT_EQ(0u, GetPopupCounts());
481 // id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup).
482 priority.priority = HIGH_PRIORITY;
483 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
484 id1,
485 UTF8ToUTF16("newtitle"),
486 UTF8ToUTF16("newbody"),
487 gfx::Image(),
488 UTF8ToUTF16(kDisplaySource),
489 NotifierId(NotifierId::APPLICATION,
490 kExtensionId),
491 priority,
492 NULL));
493 notification_list()->UpdateNotificationMessage(id1, notification.Pass());
494 EXPECT_EQ(1u, GetPopupCounts());
495 notification_list()->MarkSinglePopupAsShown(id1, true);
496 EXPECT_EQ(0u, GetPopupCounts());
498 // id1 promoted to HIGH->MAX, it'll appear as toast again.
499 priority.priority = MAX_PRIORITY;
500 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
501 id1,
502 UTF8ToUTF16("newtitle2"),
503 UTF8ToUTF16("newbody2"),
504 gfx::Image(),
505 UTF8ToUTF16(kDisplaySource),
506 NotifierId(NotifierId::APPLICATION,
507 kExtensionId),
508 priority,
509 NULL));
510 notification_list()->UpdateNotificationMessage(id1, notification.Pass());
511 EXPECT_EQ(1u, GetPopupCounts());
512 notification_list()->MarkSinglePopupAsShown(id1, true);
513 EXPECT_EQ(0u, GetPopupCounts());
515 // id1 demoted to MAX->DEFAULT, no appearing as toast.
516 priority.priority = DEFAULT_PRIORITY;
517 notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
518 id1,
519 UTF8ToUTF16("newtitle3"),
520 UTF8ToUTF16("newbody3"),
521 gfx::Image(),
522 UTF8ToUTF16(kDisplaySource),
523 NotifierId(NotifierId::APPLICATION,
524 kExtensionId),
525 priority,
526 NULL));
527 notification_list()->UpdateNotificationMessage(id1, notification.Pass());
528 EXPECT_EQ(0u, GetPopupCounts());
531 TEST_F(NotificationListTest, NotificationOrderAndPriority) {
532 base::Time now = base::Time::Now();
533 message_center::RichNotificationData optional;
534 optional.timestamp = now;
535 optional.priority = 2;
536 std::string max_id = AddNotification(optional);
538 now += base::TimeDelta::FromSeconds(1);
539 optional.timestamp = now;
540 optional.priority = 1;
541 std::string high_id = AddNotification(optional);
543 now += base::TimeDelta::FromSeconds(1);
544 optional.timestamp = now;
545 optional.priority = 0;
546 std::string default_id = AddNotification(optional);
549 // Popups: latest comes first.
550 NotificationList::PopupNotifications popups = GetPopups();
551 EXPECT_EQ(3u, popups.size());
552 NotificationList::PopupNotifications::const_iterator iter = popups.begin();
553 EXPECT_EQ(default_id, (*iter)->id());
554 iter++;
555 EXPECT_EQ(high_id, (*iter)->id());
556 iter++;
557 EXPECT_EQ(max_id, (*iter)->id());
560 // Notifications: high priority comes ealier.
561 const NotificationList::Notifications notifications =
562 notification_list()->GetVisibleNotifications(blockers());
563 EXPECT_EQ(3u, notifications.size());
564 NotificationList::Notifications::const_iterator iter =
565 notifications.begin();
566 EXPECT_EQ(max_id, (*iter)->id());
567 iter++;
568 EXPECT_EQ(high_id, (*iter)->id());
569 iter++;
570 EXPECT_EQ(default_id, (*iter)->id());
574 TEST_F(NotificationListTest, MarkSinglePopupAsShown) {
575 std::string id1 = AddNotification();
576 std::string id2 = AddNotification();
577 std::string id3 = AddNotification();
578 ASSERT_EQ(3u, notification_list()->NotificationCount(blockers()));
579 ASSERT_EQ(std::min(static_cast<size_t>(3u), kMaxVisiblePopupNotifications),
580 GetPopupCounts());
581 notification_list()->MarkSinglePopupAsDisplayed(id1);
582 notification_list()->MarkSinglePopupAsDisplayed(id2);
583 notification_list()->MarkSinglePopupAsDisplayed(id3);
585 notification_list()->MarkSinglePopupAsShown(id2, true);
586 notification_list()->MarkSinglePopupAsShown(id3, false);
587 EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
588 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
589 EXPECT_EQ(1u, GetPopupCounts());
590 NotificationList::PopupNotifications popups = GetPopups();
591 EXPECT_EQ(id1, (*popups.begin())->id());
593 // The notifications in the NotificationCenter are unaffected by popups shown.
594 NotificationList::Notifications notifications =
595 notification_list()->GetVisibleNotifications(blockers());
596 NotificationList::Notifications::const_iterator iter = notifications.begin();
597 EXPECT_EQ(id3, (*iter)->id());
598 iter++;
599 EXPECT_EQ(id2, (*iter)->id());
600 iter++;
601 EXPECT_EQ(id1, (*iter)->id());
604 TEST_F(NotificationListTest, UpdateAfterMarkedAsShown) {
605 std::string id1 = AddNotification();
606 std::string id2 = AddNotification();
607 notification_list()->MarkSinglePopupAsDisplayed(id1);
608 notification_list()->MarkSinglePopupAsDisplayed(id2);
610 EXPECT_EQ(2u, GetPopupCounts());
612 const Notification* n1 = GetNotification(id1);
613 EXPECT_FALSE(n1->shown_as_popup());
614 EXPECT_TRUE(n1->IsRead());
616 notification_list()->MarkSinglePopupAsShown(id1, true);
618 n1 = GetNotification(id1);
619 EXPECT_TRUE(n1->shown_as_popup());
620 EXPECT_TRUE(n1->IsRead());
622 const std::string replaced("test-replaced-id");
623 scoped_ptr<Notification> notification(
624 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
625 replaced,
626 UTF8ToUTF16("newtitle"),
627 UTF8ToUTF16("newbody"),
628 gfx::Image(),
629 UTF8ToUTF16(kDisplaySource),
630 NotifierId(NotifierId::APPLICATION, kExtensionId),
631 message_center::RichNotificationData(),
632 NULL));
633 notification_list()->UpdateNotificationMessage(id1, notification.Pass());
634 n1 = GetNotification(id1);
635 EXPECT_TRUE(n1 == NULL);
636 const Notification* nr = GetNotification(replaced);
637 EXPECT_TRUE(nr->shown_as_popup());
638 EXPECT_TRUE(nr->IsRead());
641 TEST_F(NotificationListTest, QuietMode) {
642 notification_list()->SetQuietMode(true);
643 AddNotification();
644 AddPriorityNotification(HIGH_PRIORITY);
645 AddPriorityNotification(MAX_PRIORITY);
646 EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
647 EXPECT_EQ(0u, GetPopupCounts());
649 notification_list()->SetQuietMode(false);
650 AddNotification();
651 EXPECT_EQ(4u, notification_list()->NotificationCount(blockers()));
652 EXPECT_EQ(1u, GetPopupCounts());
654 // TODO(mukai): Add test of quiet mode with expiration.
657 // Verifies that unread_count doesn't become negative.
658 TEST_F(NotificationListTest, UnreadCountNoNegative) {
659 std::string id = AddNotification();
660 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
662 notification_list()->MarkSinglePopupAsDisplayed(id);
663 EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
664 notification_list()->MarkSinglePopupAsShown(
665 id, false /* mark_notification_as_read */);
666 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
668 // Updates the notification and verifies unread_count doesn't change.
669 scoped_ptr<Notification> updated_notification(new Notification(
670 message_center::NOTIFICATION_TYPE_SIMPLE,
672 UTF8ToUTF16("updated"),
673 UTF8ToUTF16("updated"),
674 gfx::Image(),
675 base::string16(),
676 NotifierId(),
677 RichNotificationData(),
678 NULL));
679 notification_list()->AddNotification(updated_notification.Pass());
680 EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
683 TEST_F(NotificationListTest, TestPushingShownNotification) {
684 // Create a notification and mark it as shown.
685 std::string id1;
686 scoped_ptr<Notification> notification(MakeNotification(&id1));
687 notification->set_shown_as_popup(true);
689 // Call PushNotification on this notification.
690 notification_list()->PushNotification(notification.Pass());
692 // Ensure it is still marked as shown.
693 EXPECT_TRUE(GetNotification(id1)->shown_as_popup());
696 TEST_F(NotificationListTest, TestHasNotificationOfType) {
697 std::string id = AddNotification();
699 EXPECT_TRUE(notification_list()->HasNotificationOfType(
700 id, message_center::NOTIFICATION_TYPE_SIMPLE));
701 EXPECT_FALSE(notification_list()->HasNotificationOfType(
702 id, message_center::NOTIFICATION_TYPE_PROGRESS));
704 scoped_ptr<Notification> updated_notification(new Notification(
705 message_center::NOTIFICATION_TYPE_PROGRESS,
707 UTF8ToUTF16("updated"),
708 UTF8ToUTF16("updated"),
709 gfx::Image(),
710 base::string16(),
711 NotifierId(),
712 RichNotificationData(),
713 NULL));
714 notification_list()->AddNotification(updated_notification.Pass());
716 EXPECT_FALSE(notification_list()->HasNotificationOfType(
717 id, message_center::NOTIFICATION_TYPE_SIMPLE));
718 EXPECT_TRUE(notification_list()->HasNotificationOfType(
719 id, message_center::NOTIFICATION_TYPE_PROGRESS));
722 } // namespace message_center