1 // Copyright 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.
5 #include "ui/message_center/message_center_impl.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/size.h"
14 #include "ui/message_center/message_center.h"
15 #include "ui/message_center/message_center_types.h"
16 #include "ui/message_center/notification_blocker.h"
17 #include "ui/message_center/notification_types.h"
19 using base::UTF8ToUTF16
;
21 namespace message_center
{
24 class MessageCenterImplTest
: public testing::Test
,
25 public MessageCenterObserver
{
27 MessageCenterImplTest() {}
29 virtual void SetUp() OVERRIDE
{
30 MessageCenter::Initialize();
31 message_center_
= MessageCenter::Get();
32 loop_
.reset(new base::MessageLoop
);
33 run_loop_
.reset(new base::RunLoop());
34 closure_
= run_loop_
->QuitClosure();
37 virtual void TearDown() OVERRIDE
{
40 message_center_
= NULL
;
41 MessageCenter::Shutdown();
44 MessageCenter
* message_center() const { return message_center_
; }
45 base::RunLoop
* run_loop() const { return run_loop_
.get(); }
46 base::Closure
closure() const { return closure_
; }
49 Notification
* CreateSimpleNotification(const std::string
& id
) {
50 return CreateNotification(id
, NOTIFICATION_TYPE_SIMPLE
);
53 Notification
* CreateNotification(const std::string
& id
,
54 message_center::NotificationType type
) {
55 RichNotificationData optional_fields
;
56 optional_fields
.buttons
.push_back(ButtonInfo(UTF8ToUTF16("foo")));
57 optional_fields
.buttons
.push_back(ButtonInfo(UTF8ToUTF16("foo")));
58 return new Notification(type
,
62 gfx::Image() /* icon */,
63 base::string16() /* display_source */,
64 NotifierId(NotifierId::APPLICATION
, "app1"),
70 MessageCenter
* message_center_
;
71 scoped_ptr
<base::MessageLoop
> loop_
;
72 scoped_ptr
<base::RunLoop
> run_loop_
;
73 base::Closure closure_
;
75 DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest
);
78 class ToggledNotificationBlocker
: public NotificationBlocker
{
80 explicit ToggledNotificationBlocker(MessageCenter
* message_center
)
81 : NotificationBlocker(message_center
),
82 notifications_enabled_(true) {}
83 virtual ~ToggledNotificationBlocker() {}
85 void SetNotificationsEnabled(bool enabled
) {
86 if (notifications_enabled_
!= enabled
) {
87 notifications_enabled_
= enabled
;
88 NotifyBlockingStateChanged();
92 // NotificationBlocker overrides:
93 virtual bool ShouldShowNotificationAsPopup(
94 const message_center::NotifierId
& notifier_id
) const OVERRIDE
{
95 return notifications_enabled_
;
99 bool notifications_enabled_
;
101 DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker
);
104 class PopupNotificationBlocker
: public ToggledNotificationBlocker
{
106 PopupNotificationBlocker(MessageCenter
* message_center
,
107 const NotifierId
& allowed_notifier
)
108 : ToggledNotificationBlocker(message_center
),
109 allowed_notifier_(allowed_notifier
) {}
110 virtual ~PopupNotificationBlocker() {}
112 // NotificationBlocker overrides:
113 virtual bool ShouldShowNotificationAsPopup(
114 const NotifierId
& notifier_id
) const OVERRIDE
{
115 return (notifier_id
== allowed_notifier_
) ||
116 ToggledNotificationBlocker::ShouldShowNotificationAsPopup(notifier_id
);
120 NotifierId allowed_notifier_
;
122 DISALLOW_COPY_AND_ASSIGN(PopupNotificationBlocker
);
125 class TotalNotificationBlocker
: public PopupNotificationBlocker
{
127 TotalNotificationBlocker(MessageCenter
* message_center
,
128 const NotifierId
& allowed_notifier
)
129 : PopupNotificationBlocker(message_center
, allowed_notifier
) {}
130 virtual ~TotalNotificationBlocker() {}
132 // NotificationBlocker overrides:
133 virtual bool ShouldShowNotification(
134 const NotifierId
& notifier_id
) const OVERRIDE
{
135 return ShouldShowNotificationAsPopup(notifier_id
);
139 DISALLOW_COPY_AND_ASSIGN(TotalNotificationBlocker
);
142 bool PopupNotificationsContain(
143 const NotificationList::PopupNotifications
& popups
,
144 const std::string
& id
) {
145 for (NotificationList::PopupNotifications::const_iterator iter
=
146 popups
.begin(); iter
!= popups
.end(); ++iter
) {
147 if ((*iter
)->id() == id
)
153 // Right now, MessageCenter::HasNotification() returns regardless of blockers.
154 bool NotificationsContain(
155 const NotificationList::Notifications
& notifications
,
156 const std::string
& id
) {
157 for (NotificationList::Notifications::const_iterator iter
=
158 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
159 if ((*iter
)->id() == id
)
169 class MockPopupTimersController
: public PopupTimersController
{
171 MockPopupTimersController(MessageCenter
* message_center
,
172 base::Closure quit_closure
)
173 : PopupTimersController(message_center
),
174 timer_finished_(false),
175 quit_closure_(quit_closure
) {}
176 virtual ~MockPopupTimersController() {}
178 virtual void TimerFinished(const std::string
& id
) OVERRIDE
{
179 base::MessageLoop::current()->PostTask(FROM_HERE
, quit_closure_
);
180 timer_finished_
= true;
184 bool timer_finished() const { return timer_finished_
; }
185 const std::string
& last_id() const { return last_id_
; }
188 bool timer_finished_
;
189 std::string last_id_
;
190 base::Closure quit_closure_
;
193 TEST_F(MessageCenterImplTest
, PopupTimersEmptyController
) {
194 scoped_ptr
<PopupTimersController
> popup_timers_controller
=
195 make_scoped_ptr(new PopupTimersController(message_center()));
197 // Test that all functions succed without any timers created.
198 popup_timers_controller
->PauseAll();
199 popup_timers_controller
->StartAll();
200 popup_timers_controller
->CancelAll();
201 popup_timers_controller
->TimerFinished("unknown");
202 popup_timers_controller
->PauseTimer("unknown");
203 popup_timers_controller
->CancelTimer("unknown");
206 TEST_F(MessageCenterImplTest
, PopupTimersControllerStartTimer
) {
207 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
209 new MockPopupTimersController(message_center(), closure()));
210 popup_timers_controller
->StartTimer("test",
211 base::TimeDelta::FromMilliseconds(1));
213 EXPECT_TRUE(popup_timers_controller
->timer_finished());
216 TEST_F(MessageCenterImplTest
, PopupTimersControllerPauseTimer
) {
217 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
219 new MockPopupTimersController(message_center(), closure()));
220 popup_timers_controller
->StartTimer("test",
221 base::TimeDelta::FromMilliseconds(1));
222 popup_timers_controller
->PauseTimer("test");
223 run_loop()->RunUntilIdle();
225 EXPECT_FALSE(popup_timers_controller
->timer_finished());
228 TEST_F(MessageCenterImplTest
, PopupTimersControllerCancelTimer
) {
229 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
231 new MockPopupTimersController(message_center(), closure()));
232 popup_timers_controller
->StartTimer("test",
233 base::TimeDelta::FromMilliseconds(1));
234 popup_timers_controller
->CancelTimer("test");
235 run_loop()->RunUntilIdle();
237 EXPECT_FALSE(popup_timers_controller
->timer_finished());
240 TEST_F(MessageCenterImplTest
, PopupTimersControllerPauseAllTimers
) {
241 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
243 new MockPopupTimersController(message_center(), closure()));
244 popup_timers_controller
->StartTimer("test",
245 base::TimeDelta::FromMilliseconds(1));
246 popup_timers_controller
->PauseAll();
247 run_loop()->RunUntilIdle();
249 EXPECT_FALSE(popup_timers_controller
->timer_finished());
252 TEST_F(MessageCenterImplTest
, PopupTimersControllerStartAllTimers
) {
253 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
255 new MockPopupTimersController(message_center(), closure()));
256 popup_timers_controller
->StartTimer("test",
257 base::TimeDelta::FromMilliseconds(1));
258 popup_timers_controller
->PauseAll();
259 popup_timers_controller
->StartAll();
262 EXPECT_TRUE(popup_timers_controller
->timer_finished());
265 TEST_F(MessageCenterImplTest
, PopupTimersControllerStartMultipleTimers
) {
266 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
268 new MockPopupTimersController(message_center(), closure()));
269 popup_timers_controller
->StartTimer("test",
270 base::TimeDelta::FromMilliseconds(5));
271 popup_timers_controller
->StartTimer("test2",
272 base::TimeDelta::FromMilliseconds(1));
273 popup_timers_controller
->StartTimer("test3",
274 base::TimeDelta::FromMilliseconds(3));
275 popup_timers_controller
->PauseAll();
276 popup_timers_controller
->StartAll();
279 EXPECT_EQ(popup_timers_controller
->last_id(), "test2");
280 EXPECT_TRUE(popup_timers_controller
->timer_finished());
283 TEST_F(MessageCenterImplTest
, PopupTimersControllerStartMultipleTimersPause
) {
284 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
286 new MockPopupTimersController(message_center(), closure()));
287 popup_timers_controller
->StartTimer("test",
288 base::TimeDelta::FromMilliseconds(5));
289 popup_timers_controller
->StartTimer("test2",
290 base::TimeDelta::FromMilliseconds(1));
291 popup_timers_controller
->StartTimer("test3",
292 base::TimeDelta::FromMilliseconds(3));
293 popup_timers_controller
->PauseTimer("test2");
297 EXPECT_EQ(popup_timers_controller
->last_id(), "test3");
298 EXPECT_TRUE(popup_timers_controller
->timer_finished());
301 TEST_F(MessageCenterImplTest
, PopupTimersControllerResetTimer
) {
302 scoped_ptr
<MockPopupTimersController
> popup_timers_controller
=
304 new MockPopupTimersController(message_center(), closure()));
305 popup_timers_controller
->StartTimer("test",
306 base::TimeDelta::FromMilliseconds(5));
307 popup_timers_controller
->StartTimer("test2",
308 base::TimeDelta::FromMilliseconds(1));
309 popup_timers_controller
->StartTimer("test3",
310 base::TimeDelta::FromMilliseconds(3));
311 popup_timers_controller
->PauseTimer("test2");
312 popup_timers_controller
->ResetTimer("test",
313 base::TimeDelta::FromMilliseconds(2));
317 EXPECT_EQ(popup_timers_controller
->last_id(), "test");
318 EXPECT_TRUE(popup_timers_controller
->timer_finished());
321 TEST_F(MessageCenterImplTest
, NotificationBlocker
) {
322 NotifierId
notifier_id(NotifierId::APPLICATION
, "app1");
323 // Multiple blockers to verify the case that one blocker blocks but another
325 ToggledNotificationBlocker
blocker1(message_center());
326 ToggledNotificationBlocker
blocker2(message_center());
328 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
329 NOTIFICATION_TYPE_SIMPLE
,
331 UTF8ToUTF16("title"),
332 UTF8ToUTF16("message"),
333 gfx::Image() /* icon */,
334 base::string16() /* display_source */,
336 RichNotificationData(),
338 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
339 NOTIFICATION_TYPE_SIMPLE
,
341 UTF8ToUTF16("title"),
342 UTF8ToUTF16("message"),
343 gfx::Image() /* icon */,
344 base::string16() /* display_source */,
346 RichNotificationData(),
348 EXPECT_EQ(2u, message_center()->GetPopupNotifications().size());
349 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
351 // Block all notifications. All popups are gone and message center should be
353 blocker1
.SetNotificationsEnabled(false);
354 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
355 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
357 // Updates |blocker2| state, which doesn't affect the global state.
358 blocker2
.SetNotificationsEnabled(false);
359 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
360 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
362 blocker2
.SetNotificationsEnabled(true);
363 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
364 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
366 // If |blocker2| blocks, then unblocking blocker1 doesn't change the global
368 blocker2
.SetNotificationsEnabled(false);
369 blocker1
.SetNotificationsEnabled(true);
370 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
371 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
373 // Unblock both blockers, which recovers the global state, but the popups
375 blocker2
.SetNotificationsEnabled(true);
376 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
377 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
380 TEST_F(MessageCenterImplTest
, NotificationsDuringBlocked
) {
381 NotifierId
notifier_id(NotifierId::APPLICATION
, "app1");
382 ToggledNotificationBlocker
blocker(message_center());
384 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
385 NOTIFICATION_TYPE_SIMPLE
,
387 UTF8ToUTF16("title"),
388 UTF8ToUTF16("message"),
389 gfx::Image() /* icon */,
390 base::string16() /* display_source */,
392 RichNotificationData(),
394 EXPECT_EQ(1u, message_center()->GetPopupNotifications().size());
395 EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size());
397 // Create a notification during blocked. Still no popups.
398 blocker
.SetNotificationsEnabled(false);
399 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
400 NOTIFICATION_TYPE_SIMPLE
,
402 UTF8ToUTF16("title"),
403 UTF8ToUTF16("message"),
404 gfx::Image() /* icon */,
405 base::string16() /* display_source */,
407 RichNotificationData(),
409 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
410 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
412 // Unblock notifications, the id1 should appear as a popup.
413 blocker
.SetNotificationsEnabled(true);
414 NotificationList::PopupNotifications popups
=
415 message_center()->GetPopupNotifications();
416 EXPECT_EQ(1u, popups
.size());
417 EXPECT_TRUE(PopupNotificationsContain(popups
, "id2"));
418 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
421 // Similar to other blocker cases but this test case allows |notifier_id2| even
423 TEST_F(MessageCenterImplTest
, NotificationBlockerAllowsPopups
) {
424 NotifierId
notifier_id1(NotifierId::APPLICATION
, "app1");
425 NotifierId
notifier_id2(NotifierId::APPLICATION
, "app2");
426 PopupNotificationBlocker
blocker(message_center(), notifier_id2
);
428 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
429 NOTIFICATION_TYPE_SIMPLE
,
431 UTF8ToUTF16("title"),
432 UTF8ToUTF16("message"),
433 gfx::Image() /* icon */,
434 base::string16() /* display_source */,
436 RichNotificationData(),
438 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
439 NOTIFICATION_TYPE_SIMPLE
,
441 UTF8ToUTF16("title"),
442 UTF8ToUTF16("message"),
443 gfx::Image() /* icon */,
444 base::string16() /* display_source */,
446 RichNotificationData(),
449 // "id1" is closed but "id2" is still visible as a popup.
450 blocker
.SetNotificationsEnabled(false);
451 NotificationList::PopupNotifications popups
=
452 message_center()->GetPopupNotifications();
453 EXPECT_EQ(1u, popups
.size());
454 EXPECT_TRUE(PopupNotificationsContain(popups
, "id2"));
455 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
457 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
458 NOTIFICATION_TYPE_SIMPLE
,
460 UTF8ToUTF16("title"),
461 UTF8ToUTF16("message"),
462 gfx::Image() /* icon */,
463 base::string16() /* display_source */,
465 RichNotificationData(),
467 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
468 NOTIFICATION_TYPE_SIMPLE
,
470 UTF8ToUTF16("title"),
471 UTF8ToUTF16("message"),
472 gfx::Image() /* icon */,
473 base::string16() /* display_source */,
475 RichNotificationData(),
477 popups
= message_center()->GetPopupNotifications();
478 EXPECT_EQ(2u, popups
.size());
479 EXPECT_TRUE(PopupNotificationsContain(popups
, "id2"));
480 EXPECT_TRUE(PopupNotificationsContain(popups
, "id4"));
481 EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
483 blocker
.SetNotificationsEnabled(true);
484 popups
= message_center()->GetPopupNotifications();
485 EXPECT_EQ(3u, popups
.size());
486 EXPECT_TRUE(PopupNotificationsContain(popups
, "id2"));
487 EXPECT_TRUE(PopupNotificationsContain(popups
, "id3"));
488 EXPECT_TRUE(PopupNotificationsContain(popups
, "id4"));
489 EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
492 // TotalNotificationBlocker suppresses showing notifications even from the list.
493 // This would provide the feature to 'separated' message centers per-profile for
494 // ChromeOS multi-login.
495 TEST_F(MessageCenterImplTest
, TotalNotificationBlocker
) {
496 NotifierId
notifier_id1(NotifierId::APPLICATION
, "app1");
497 NotifierId
notifier_id2(NotifierId::APPLICATION
, "app2");
498 TotalNotificationBlocker
blocker(message_center(), notifier_id2
);
500 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
501 NOTIFICATION_TYPE_SIMPLE
,
503 UTF8ToUTF16("title"),
504 UTF8ToUTF16("message"),
505 gfx::Image() /* icon */,
506 base::string16() /* display_source */,
508 RichNotificationData(),
510 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
511 NOTIFICATION_TYPE_SIMPLE
,
513 UTF8ToUTF16("title"),
514 UTF8ToUTF16("message"),
515 gfx::Image() /* icon */,
516 base::string16() /* display_source */,
518 RichNotificationData(),
521 // "id1" becomes invisible while "id2" is still visible.
522 blocker
.SetNotificationsEnabled(false);
523 EXPECT_EQ(1u, message_center()->NotificationCount());
524 NotificationList::Notifications notifications
=
525 message_center()->GetVisibleNotifications();
526 EXPECT_FALSE(NotificationsContain(notifications
, "id1"));
527 EXPECT_TRUE(NotificationsContain(notifications
, "id2"));
529 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
530 NOTIFICATION_TYPE_SIMPLE
,
532 UTF8ToUTF16("title"),
533 UTF8ToUTF16("message"),
534 gfx::Image() /* icon */,
535 base::string16() /* display_source */,
537 RichNotificationData(),
539 message_center()->AddNotification(scoped_ptr
<Notification
>(new Notification(
540 NOTIFICATION_TYPE_SIMPLE
,
542 UTF8ToUTF16("title"),
543 UTF8ToUTF16("message"),
544 gfx::Image() /* icon */,
545 base::string16() /* display_source */,
547 RichNotificationData(),
549 EXPECT_EQ(2u, message_center()->NotificationCount());
550 notifications
= message_center()->GetVisibleNotifications();
551 EXPECT_FALSE(NotificationsContain(notifications
, "id1"));
552 EXPECT_TRUE(NotificationsContain(notifications
, "id2"));
553 EXPECT_FALSE(NotificationsContain(notifications
, "id3"));
554 EXPECT_TRUE(NotificationsContain(notifications
, "id4"));
556 blocker
.SetNotificationsEnabled(true);
557 EXPECT_EQ(4u, message_center()->NotificationCount());
558 notifications
= message_center()->GetVisibleNotifications();
559 EXPECT_TRUE(NotificationsContain(notifications
, "id1"));
560 EXPECT_TRUE(NotificationsContain(notifications
, "id2"));
561 EXPECT_TRUE(NotificationsContain(notifications
, "id3"));
562 EXPECT_TRUE(NotificationsContain(notifications
, "id4"));
564 // RemoveAllVisibleNotifications should remove just visible notifications.
565 blocker
.SetNotificationsEnabled(false);
566 message_center()->RemoveAllVisibleNotifications(false /* by_user */);
567 EXPECT_EQ(0u, message_center()->NotificationCount());
568 blocker
.SetNotificationsEnabled(true);
569 EXPECT_EQ(2u, message_center()->NotificationCount());
570 notifications
= message_center()->GetVisibleNotifications();
571 EXPECT_TRUE(NotificationsContain(notifications
, "id1"));
572 EXPECT_FALSE(NotificationsContain(notifications
, "id2"));
573 EXPECT_TRUE(NotificationsContain(notifications
, "id3"));
574 EXPECT_FALSE(NotificationsContain(notifications
, "id4"));
576 // And RemoveAllNotifications should remove all.
577 blocker
.SetNotificationsEnabled(false);
578 message_center()->RemoveAllNotifications(false /* by_user */);
579 EXPECT_EQ(0u, message_center()->NotificationCount());
582 TEST_F(MessageCenterImplTest
, QueueUpdatesWithCenterVisible
) {
583 std::string
id("id1");
584 std::string
id2("id2");
585 NotifierId
notifier_id1(NotifierId::APPLICATION
, "app1");
587 // First, add and update a notification to ensure updates happen
589 scoped_ptr
<Notification
> notification(CreateSimpleNotification(id
));
590 message_center()->AddNotification(notification
.Pass());
591 notification
.reset(CreateSimpleNotification(id2
));
592 message_center()->UpdateNotification(id
, notification
.Pass());
593 EXPECT_TRUE(message_center()->HasNotification(id2
));
594 EXPECT_FALSE(message_center()->HasNotification(id
));
596 // Then open the message center.
597 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER
);
599 // Then update a notification; nothing should have happened.
600 notification
.reset(CreateSimpleNotification(id
));
601 message_center()->UpdateNotification(id2
, notification
.Pass());
602 EXPECT_TRUE(message_center()->HasNotification(id2
));
603 EXPECT_FALSE(message_center()->HasNotification(id
));
605 // Close the message center; then the update should have propagated.
606 message_center()->SetVisibility(VISIBILITY_TRANSIENT
);
607 EXPECT_FALSE(message_center()->HasNotification(id2
));
608 EXPECT_TRUE(message_center()->HasNotification(id
));
611 TEST_F(MessageCenterImplTest
, ComplexQueueing
) {
612 std::string ids
[5] = {"0", "1", "2", "3", "4p"};
613 NotifierId
notifier_id1(NotifierId::APPLICATION
, "app1");
615 scoped_ptr
<Notification
> notification
;
616 // Add some notifications
619 notification
.reset(CreateSimpleNotification(ids
[i
]));
620 message_center()->AddNotification(notification
.Pass());
622 for (i
= 0; i
< 3; i
++) {
623 EXPECT_TRUE(message_center()->HasNotification(ids
[i
]));
626 EXPECT_FALSE(message_center()->HasNotification(ids
[i
]));
629 notification
.reset(CreateNotification(ids
[4], NOTIFICATION_TYPE_PROGRESS
));
630 message_center()->AddNotification(notification
.Pass());
632 // Now start queueing.
633 // NL: ["0", "1", "2", "4p"]
634 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER
);
636 // This should update notification "1" to have id "3".
637 notification
.reset(CreateSimpleNotification(ids
[3]));
638 message_center()->UpdateNotification(ids
[1], notification
.Pass());
640 notification
.reset(CreateSimpleNotification(ids
[4]));
641 message_center()->UpdateNotification(ids
[4], notification
.Pass());
643 notification
.reset(CreateNotification(ids
[4], NOTIFICATION_TYPE_PROGRESS
));
644 message_center()->UpdateNotification(ids
[4], notification
.Pass());
646 // This should update notification "3" to a new ID after we go TRANSIENT.
647 notification
.reset(CreateSimpleNotification("New id"));
648 message_center()->UpdateNotification(ids
[3], notification
.Pass());
650 // This should create a new "3", that doesn't overwrite the update to 3
652 notification
.reset(CreateSimpleNotification(ids
[3]));
653 message_center()->AddNotification(notification
.Pass());
655 // The NL should still be the same: ["0", "1", "2", "4p"]
656 EXPECT_TRUE(message_center()->HasNotification(ids
[0]));
657 EXPECT_TRUE(message_center()->HasNotification(ids
[1]));
658 EXPECT_TRUE(message_center()->HasNotification(ids
[2]));
659 EXPECT_FALSE(message_center()->HasNotification(ids
[3]));
660 EXPECT_TRUE(message_center()->HasNotification(ids
[4]));
661 EXPECT_EQ(message_center()->GetVisibleNotifications().size(), 4u);
662 message_center()->SetVisibility(VISIBILITY_TRANSIENT
);
664 EXPECT_TRUE(message_center()->HasNotification(ids
[0]));
665 EXPECT_FALSE(message_center()->HasNotification(ids
[1]));
666 EXPECT_TRUE(message_center()->HasNotification(ids
[2]));
667 EXPECT_TRUE(message_center()->HasNotification(ids
[3]));
668 EXPECT_TRUE(message_center()->HasNotification(ids
[4]));
669 EXPECT_TRUE(message_center()->HasNotification("New id"));
670 EXPECT_EQ(message_center()->GetVisibleNotifications().size(), 5u);
673 TEST_F(MessageCenterImplTest
, QueuedDirectUpdates
) {
674 std::string
id("id1");
675 std::string
id2("id2");
676 NotifierId
notifier_id1(NotifierId::APPLICATION
, "app1");
678 gfx::Size
original_size(0, 0);
679 // Open the message center to prevent adding notifications
680 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER
);
682 // Create new notification to be added to the queue; images all have the same
684 scoped_ptr
<Notification
> notification(CreateSimpleNotification(id
));
686 // Double-check that sizes all match.
687 const std::vector
<ButtonInfo
>& original_buttons
= notification
->buttons();
688 ASSERT_EQ(2u, original_buttons
.size());
690 EXPECT_EQ(original_size
, notification
->icon().Size());
691 EXPECT_EQ(original_size
, notification
->image().Size());
692 EXPECT_EQ(original_size
, original_buttons
[0].icon
.Size());
693 EXPECT_EQ(original_size
, original_buttons
[1].icon
.Size());
695 message_center()->AddNotification(notification
.Pass());
697 // The notification should be in the queue.
698 EXPECT_FALSE(message_center()->HasNotification(id
));
700 // Now try setting the icon to a different size.
701 gfx::Size
new_size(16, 16);
702 EXPECT_NE(original_size
, new_size
);
704 gfx::Canvas
canvas(new_size
, 1.0f
, true);
705 canvas
.DrawColor(SK_ColorBLUE
);
706 gfx::Image
testImage(gfx::Image(gfx::ImageSkia(canvas
.ExtractImageRep())));
707 message_center()->SetNotificationIcon(id
, testImage
);
708 message_center()->SetNotificationImage(id
, testImage
);
709 message_center()->SetNotificationButtonIcon(id
, 0, testImage
);
710 message_center()->SetNotificationButtonIcon(id
, 1, testImage
);
712 // The notification should be in the queue.
713 EXPECT_FALSE(message_center()->HasNotification(id
));
715 // Close the message center; then the update should have propagated.
716 message_center()->SetVisibility(VISIBILITY_TRANSIENT
);
717 // The notification should no longer be in the queue.
718 EXPECT_TRUE(message_center()->HasNotification(id
));
720 Notification
* mc_notification
=
721 *(message_center()->GetVisibleNotifications().begin());
722 const std::vector
<ButtonInfo
>& buttons
= mc_notification
->buttons();
723 ASSERT_EQ(2u, buttons
.size());
725 EXPECT_EQ(new_size
, mc_notification
->icon().Size());
726 EXPECT_EQ(new_size
, mc_notification
->image().Size());
727 EXPECT_EQ(new_size
, buttons
[0].icon
.Size());
728 EXPECT_EQ(new_size
, buttons
[1].icon
.Size());
731 TEST_F(MessageCenterImplTest
, CachedUnreadCount
) {
732 message_center()->AddNotification(
733 scoped_ptr
<Notification
>(CreateSimpleNotification("id1")));
734 message_center()->AddNotification(
735 scoped_ptr
<Notification
>(CreateSimpleNotification("id2")));
736 message_center()->AddNotification(
737 scoped_ptr
<Notification
>(CreateSimpleNotification("id3")));
738 ASSERT_EQ(3u, message_center()->UnreadNotificationCount());
740 // Mark 'displayed' on all notifications by using for-loop. This shouldn't
741 // recreate |notifications| inside of the loop.
742 const NotificationList::Notifications
& notifications
=
743 message_center()->GetVisibleNotifications();
744 for (NotificationList::Notifications::const_iterator iter
=
745 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
746 message_center()->DisplayedNotification((*iter
)->id());
748 EXPECT_EQ(0u, message_center()->UnreadNotificationCount());
750 // Imitate the timeout, which recovers the unread count. Again, this shouldn't
751 // recreate |notifications| inside of the loop.
752 for (NotificationList::Notifications::const_iterator iter
=
753 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
754 message_center()->MarkSinglePopupAsShown((*iter
)->id(), false);
756 EXPECT_EQ(3u, message_center()->UnreadNotificationCount());
758 // Opening the message center will reset the unread count.
759 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER
);
760 EXPECT_EQ(0u, message_center()->UnreadNotificationCount());
763 } // namespace internal
764 } // namespace message_center