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.
4
5 #include "ui/message_center/message_center_impl.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop_current.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/task_environment.h"
19 #include "base/test/test_mock_time_task_runner.h"
20 #include "base/threading/thread_task_runner_handle.h"
21 #include "build/build_config.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/canvas.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/message_center/lock_screen/fake_lock_screen_controller.h"
26 #include "ui/message_center/message_center.h"
27 #include "ui/message_center/message_center_types.h"
28 #include "ui/message_center/notification_blocker.h"
29 #include "ui/message_center/public/cpp/message_center_constants.h"
30 #include "ui/message_center/public/cpp/notification_types.h"
31 #include "ui/message_center/public/cpp/notifier_id.h"
32
33 using base::UTF8ToUTF16;
34
35 namespace message_center {
36
37 namespace {
38
39 class CheckObserver : public MessageCenterObserver {
40 public:
CheckObserver(MessageCenter * message_center,const std::string & target_id)41 CheckObserver(MessageCenter* message_center, const std::string& target_id)
42 : message_center_(message_center), target_id_(target_id) {
43 DCHECK(message_center);
44 DCHECK(!target_id.empty());
45 }
46
OnNotificationUpdated(const std::string & notification_id)47 void OnNotificationUpdated(const std::string& notification_id) override {
48 EXPECT_TRUE(message_center_->FindVisibleNotificationById(target_id_));
49 }
50
51 private:
52 MessageCenter* message_center_;
53 std::string target_id_;
54
55 DISALLOW_COPY_AND_ASSIGN(CheckObserver);
56 };
57
58 class RemoveObserver : public MessageCenterObserver {
59 public:
RemoveObserver(MessageCenter * message_center,const std::string & target_id)60 RemoveObserver(MessageCenter* message_center, const std::string& target_id)
61 : message_center_(message_center), target_id_(target_id) {
62 DCHECK(message_center);
63 DCHECK(!target_id.empty());
64 }
65
OnNotificationUpdated(const std::string & notification_id)66 void OnNotificationUpdated(const std::string& notification_id) override {
67 message_center_->RemoveNotification(target_id_, false);
68 }
69
70 private:
71 MessageCenter* message_center_;
72 std::string target_id_;
73
74 DISALLOW_COPY_AND_ASSIGN(RemoveObserver);
75 };
76
77 class TestAddObserver : public MessageCenterObserver {
78 public:
TestAddObserver(MessageCenter * message_center)79 explicit TestAddObserver(MessageCenter* message_center)
80 : message_center_(message_center) {
81 message_center_->AddObserver(this);
82 }
83
~TestAddObserver()84 ~TestAddObserver() override { message_center_->RemoveObserver(this); }
85
OnNotificationAdded(const std::string & id)86 void OnNotificationAdded(const std::string& id) override {
87 std::string log = logs_[id];
88 if (!log.empty())
89 log += "_";
90 logs_[id] = log + "add-" + id;
91 }
92
OnNotificationUpdated(const std::string & id)93 void OnNotificationUpdated(const std::string& id) override {
94 std::string log = logs_[id];
95 if (!log.empty())
96 log += "_";
97 logs_[id] = log + "update-" + id;
98 }
99
log(const std::string & id)100 const std::string log(const std::string& id) { return logs_[id]; }
reset_logs()101 void reset_logs() { logs_.clear(); }
102
103 private:
104 std::map<std::string, std::string> logs_;
105 MessageCenter* message_center_;
106 };
107
108 class TestDelegate : public NotificationDelegate {
109 public:
110 TestDelegate() = default;
Close(bool by_user)111 void Close(bool by_user) override {
112 log_ += "Close_";
113 log_ += (by_user ? "by_user_" : "programmatically_");
114 }
Click(const base::Optional<int> & button_index,const base::Optional<base::string16> & reply)115 void Click(const base::Optional<int>& button_index,
116 const base::Optional<base::string16>& reply) override {
117 if (button_index) {
118 if (!reply) {
119 log_ += "ButtonClick_";
120 log_ += base::NumberToString(*button_index) + "_";
121 } else {
122 log_ += "ReplyButtonClick_";
123 log_ += base::NumberToString(*button_index) + "_";
124 log_ += base::UTF16ToUTF8(*reply) + "_";
125 }
126 } else {
127 log_ += "Click_";
128 }
129 }
log()130 const std::string& log() { return log_; }
131
132 private:
~TestDelegate()133 ~TestDelegate() override {}
134 std::string log_;
135
136 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
137 };
138
139 // The default app id used to create simple notifications.
140 const std::string kDefaultAppId = "app1";
141
142 } // anonymous namespace
143
144 class MessageCenterImplTest : public testing::Test {
145 public:
MessageCenterImplTest()146 MessageCenterImplTest() {}
147
SetUp()148 void SetUp() override {
149 MessageCenter::Initialize(std::make_unique<FakeLockScreenController>());
150 message_center_ = MessageCenter::Get();
151 task_environment_ =
152 std::make_unique<base::test::SingleThreadTaskEnvironment>();
153 run_loop_ = std::make_unique<base::RunLoop>();
154 closure_ = run_loop_->QuitClosure();
155 }
156
TearDown()157 void TearDown() override {
158 run_loop_.reset();
159 task_environment_.reset();
160 message_center_ = nullptr;
161 MessageCenter::Shutdown();
162 }
163
message_center() const164 MessageCenter* message_center() const { return message_center_; }
message_center_impl() const165 MessageCenterImpl* message_center_impl() const {
166 return reinterpret_cast<MessageCenterImpl*>(message_center_);
167 }
168
run_loop() const169 base::RunLoop* run_loop() const { return run_loop_.get(); }
closure() const170 base::RepeatingClosure closure() const { return closure_; }
171
172 protected:
CreateSimpleNotification(const std::string & id)173 std::unique_ptr<Notification> CreateSimpleNotification(
174 const std::string& id) {
175 return CreateNotificationWithNotifierId(id, kDefaultAppId,
176 NOTIFICATION_TYPE_SIMPLE);
177 }
178
CreateSimpleNotificationWithNotifierId(const std::string & id,const std::string & notifier_id)179 std::unique_ptr<Notification> CreateSimpleNotificationWithNotifierId(
180 const std::string& id,
181 const std::string& notifier_id) {
182 return CreateNotificationWithNotifierId(
183 id,
184 notifier_id,
185 NOTIFICATION_TYPE_SIMPLE);
186 }
187
CreateNotification(const std::string & id,NotificationType type)188 std::unique_ptr<Notification> CreateNotification(const std::string& id,
189 NotificationType type) {
190 return CreateNotificationWithNotifierId(id, kDefaultAppId, type);
191 }
192
CreateNotificationWithNotifierId(const std::string & id,const std::string & notifier_id,NotificationType type)193 std::unique_ptr<Notification> CreateNotificationWithNotifierId(
194 const std::string& id,
195 const std::string& notifier_id,
196 NotificationType type) {
197 RichNotificationData optional_fields;
198 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
199 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
200 return std::make_unique<Notification>(
201 type, id, UTF8ToUTF16("title"), UTF8ToUTF16(id),
202 gfx::Image() /* icon */, base::string16() /* display_source */, GURL(),
203 NotifierId(NotifierType::APPLICATION, notifier_id), optional_fields,
204 base::MakeRefCounted<TestDelegate>());
205 }
206
GetDelegate(const std::string & id) const207 TestDelegate* GetDelegate(const std::string& id) const {
208 Notification* n = message_center()->FindVisibleNotificationById(id);
209 return n ? static_cast<TestDelegate*>(n->delegate()) : nullptr;
210 }
211
lock_screen_controller() const212 FakeLockScreenController* lock_screen_controller() const {
213 return static_cast<FakeLockScreenController*>(
214 message_center_impl()->lock_screen_controller());
215 }
216
217 private:
218 MessageCenter* message_center_;
219 std::unique_ptr<base::test::SingleThreadTaskEnvironment> task_environment_;
220 std::unique_ptr<base::RunLoop> run_loop_;
221 base::RepeatingClosure closure_;
222
223 DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest);
224 };
225
226 namespace {
227
228 class ToggledNotificationBlocker : public NotificationBlocker {
229 public:
ToggledNotificationBlocker(MessageCenter * message_center)230 explicit ToggledNotificationBlocker(MessageCenter* message_center)
231 : NotificationBlocker(message_center),
232 notifications_enabled_(true) {}
~ToggledNotificationBlocker()233 ~ToggledNotificationBlocker() override {}
234
SetNotificationsEnabled(bool enabled)235 void SetNotificationsEnabled(bool enabled) {
236 if (notifications_enabled_ != enabled) {
237 notifications_enabled_ = enabled;
238 NotifyBlockingStateChanged();
239 }
240 }
241
242 // NotificationBlocker overrides:
ShouldShowNotificationAsPopup(const Notification & notification) const243 bool ShouldShowNotificationAsPopup(
244 const Notification& notification) const override {
245 return notifications_enabled_;
246 }
247
248 private:
249 bool notifications_enabled_;
250
251 DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker);
252 };
253
254 class PopupNotificationBlocker : public ToggledNotificationBlocker {
255 public:
PopupNotificationBlocker(MessageCenter * message_center,const NotifierId & allowed_notifier)256 PopupNotificationBlocker(MessageCenter* message_center,
257 const NotifierId& allowed_notifier)
258 : ToggledNotificationBlocker(message_center),
259 allowed_notifier_(allowed_notifier) {}
~PopupNotificationBlocker()260 ~PopupNotificationBlocker() override {}
261
262 // NotificationBlocker overrides:
ShouldShowNotificationAsPopup(const Notification & notification) const263 bool ShouldShowNotificationAsPopup(
264 const Notification& notification) const override {
265 return (notification.notifier_id() == allowed_notifier_) ||
266 ToggledNotificationBlocker::ShouldShowNotificationAsPopup(
267 notification);
268 }
269
270 private:
271 NotifierId allowed_notifier_;
272
273 DISALLOW_COPY_AND_ASSIGN(PopupNotificationBlocker);
274 };
275
276 class TotalNotificationBlocker : public PopupNotificationBlocker {
277 public:
TotalNotificationBlocker(MessageCenter * message_center,const NotifierId & allowed_notifier)278 TotalNotificationBlocker(MessageCenter* message_center,
279 const NotifierId& allowed_notifier)
280 : PopupNotificationBlocker(message_center, allowed_notifier) {}
~TotalNotificationBlocker()281 ~TotalNotificationBlocker() override {}
282
283 // NotificationBlocker overrides:
ShouldShowNotification(const Notification & notification) const284 bool ShouldShowNotification(const Notification& notification) const override {
285 return ShouldShowNotificationAsPopup(notification);
286 }
287
288 private:
289 DISALLOW_COPY_AND_ASSIGN(TotalNotificationBlocker);
290 };
291
PopupNotificationsContain(const NotificationList::PopupNotifications & popups,const std::string & id)292 bool PopupNotificationsContain(
293 const NotificationList::PopupNotifications& popups,
294 const std::string& id) {
295 for (auto iter = popups.begin(); iter != popups.end(); ++iter) {
296 if ((*iter)->id() == id)
297 return true;
298 }
299 return false;
300 }
301
302 // Right now, MessageCenter::HasNotification() returns regardless of blockers.
NotificationsContain(const NotificationList::Notifications & notifications,const std::string & id)303 bool NotificationsContain(
304 const NotificationList::Notifications& notifications,
305 const std::string& id) {
306 for (auto iter = notifications.begin(); iter != notifications.end(); ++iter) {
307 if ((*iter)->id() == id)
308 return true;
309 }
310 return false;
311 }
312
313 } // namespace
314
315 namespace internal {
316
317 class MockPopupTimersController : public PopupTimersController {
318 public:
MockPopupTimersController(MessageCenter * message_center,base::RepeatingClosure quit_closure)319 MockPopupTimersController(MessageCenter* message_center,
320 base::RepeatingClosure quit_closure)
321 : PopupTimersController(message_center),
322 timer_finished_(0),
323 quit_closure_(quit_closure) {}
~MockPopupTimersController()324 ~MockPopupTimersController() override {}
325
TimerFinished(const std::string & id)326 void TimerFinished(const std::string& id) override {
327 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
328 timer_finished_++;
329 last_id_ = id;
330 }
331
timer_finished() const332 int timer_finished() const { return timer_finished_; }
last_id() const333 const std::string& last_id() const { return last_id_; }
334
335 private:
336 int timer_finished_;
337 std::string last_id_;
338 base::RepeatingClosure quit_closure_;
339 };
340
TEST_F(MessageCenterImplTest,PopupTimersEmptyController)341 TEST_F(MessageCenterImplTest, PopupTimersEmptyController) {
342 std::unique_ptr<PopupTimersController> popup_timers_controller =
343 std::make_unique<PopupTimersController>(message_center());
344
345 // Test that all functions succed without any timers created.
346 popup_timers_controller->PauseAll();
347 popup_timers_controller->StartAll();
348 popup_timers_controller->CancelAll();
349 popup_timers_controller->TimerFinished("unknown");
350 popup_timers_controller->CancelTimer("unknown");
351 }
352
TEST_F(MessageCenterImplTest,PopupTimersControllerStartTimer)353 TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) {
354 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
355 std::make_unique<MockPopupTimersController>(message_center(), closure());
356 popup_timers_controller->StartTimer("test",
357 base::TimeDelta::FromMilliseconds(1));
358 run_loop()->Run();
359 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
360 }
361
TEST_F(MessageCenterImplTest,PopupTimersControllerCancelTimer)362 TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) {
363 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
364 std::make_unique<MockPopupTimersController>(message_center(), closure());
365 popup_timers_controller->StartTimer("test",
366 base::TimeDelta::FromMilliseconds(1));
367 popup_timers_controller->CancelTimer("test");
368 run_loop()->RunUntilIdle();
369
370 EXPECT_EQ(popup_timers_controller->timer_finished(), 0);
371 }
372
TEST_F(MessageCenterImplTest,PopupTimersControllerPauseAllTimers)373 TEST_F(MessageCenterImplTest, PopupTimersControllerPauseAllTimers) {
374 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
375 std::make_unique<MockPopupTimersController>(message_center(), closure());
376 popup_timers_controller->StartTimer("test",
377 base::TimeDelta::FromMilliseconds(1));
378 popup_timers_controller->PauseAll();
379 run_loop()->RunUntilIdle();
380
381 EXPECT_EQ(popup_timers_controller->timer_finished(), 0);
382 }
383
TEST_F(MessageCenterImplTest,PopupTimersControllerStartAllTimers)384 TEST_F(MessageCenterImplTest, PopupTimersControllerStartAllTimers) {
385 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
386 std::make_unique<MockPopupTimersController>(message_center(), closure());
387 popup_timers_controller->StartTimer("test",
388 base::TimeDelta::FromMilliseconds(1));
389 popup_timers_controller->PauseAll();
390 popup_timers_controller->StartAll();
391 run_loop()->Run();
392
393 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
394 }
395
TEST_F(MessageCenterImplTest,PopupTimersControllerStartMultipleTimers)396 TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimers) {
397 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
398 std::make_unique<MockPopupTimersController>(message_center(), closure());
399 popup_timers_controller->StartTimer("test", base::TimeDelta::Max());
400 popup_timers_controller->StartTimer("test2",
401 base::TimeDelta::FromMilliseconds(1));
402 popup_timers_controller->StartTimer("test3", base::TimeDelta::Max());
403 popup_timers_controller->PauseAll();
404 popup_timers_controller->StartAll();
405 run_loop()->Run();
406
407 EXPECT_EQ(popup_timers_controller->last_id(), "test2");
408 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
409 }
410
TEST_F(MessageCenterImplTest,PopupTimersControllerRestartOnUpdate)411 TEST_F(MessageCenterImplTest, PopupTimersControllerRestartOnUpdate) {
412 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner =
413 base::ThreadTaskRunnerHandle::Get();
414
415 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
416 new base::TestMockTimeTaskRunner(base::Time::Now(),
417 base::TimeTicks::Now()));
418 base::MessageLoopCurrent::Get()->SetTaskRunner(task_runner);
419
420 NotifierId notifier_id(GURL("https://example.com"));
421
422 message_center()->AddNotification(std::unique_ptr<Notification>(
423 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
424 UTF8ToUTF16("message"), gfx::Image() /* icon */,
425 base::string16() /* display_source */, GURL(),
426 notifier_id, RichNotificationData(), nullptr)));
427
428 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
429 std::make_unique<MockPopupTimersController>(message_center(), closure());
430
431 popup_timers_controller->OnNotificationDisplayed("id1", DISPLAY_SOURCE_POPUP);
432 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
433
434 #if defined(OS_CHROMEOS)
435 const int dismiss_time =
436 popup_timers_controller->GetNotificationTimeoutDefault();
437 #else
438 const int dismiss_time = kAutocloseHighPriorityDelaySeconds;
439 #endif
440
441 // Fast forward the |task_runner| by one second less than the auto-close timer
442 // frequency for Web Notifications. (As set by the |notifier_id|.)
443 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(dismiss_time - 1));
444 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
445
446 // Trigger a replacement of the notification in the timer controller.
447 popup_timers_controller->OnNotificationUpdated("id1");
448
449 // Fast forward the |task_runner| by one second less than the auto-close timer
450 // frequency for Web Notifications again. It should have been reset.
451 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(dismiss_time - 1));
452 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
453
454 // Now fast forward the |task_runner| by two seconds (to avoid flakiness),
455 // after which the timer should have fired.
456 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(2));
457 ASSERT_EQ(popup_timers_controller->timer_finished(), 1);
458
459 base::MessageLoopCurrent::Get()->SetTaskRunner(old_task_runner);
460 }
461
TEST_F(MessageCenterImplTest,Renotify)462 TEST_F(MessageCenterImplTest, Renotify) {
463 message_center()->SetHasMessageCenterView(true);
464 const std::string id("id");
465
466 // Add notification initially.
467 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
468 message_center()->AddNotification(std::move(notification));
469 auto popups = message_center()->GetPopupNotifications();
470 EXPECT_EQ(1u, popups.size());
471 EXPECT_TRUE(PopupNotificationsContain(popups, id));
472
473 // Mark notification as shown.
474 message_center()->MarkSinglePopupAsShown(id, true);
475 EXPECT_EQ(0u, message_center()->GetPopupNotifications().size());
476
477 // Add notification again without |renotify| flag. It should not pop-up again.
478 notification = CreateSimpleNotification(id);
479 message_center()->AddNotification(std::move(notification));
480 EXPECT_EQ(0u, message_center()->GetPopupNotifications().size());
481
482 // Add notification again with |renotify| flag. It should pop-up again.
483 notification = CreateSimpleNotification(id);
484 notification->set_renotify(true);
485 message_center()->AddNotification(std::move(notification));
486 popups = message_center()->GetPopupNotifications();
487 EXPECT_EQ(1u, popups.size());
488 EXPECT_TRUE(PopupNotificationsContain(popups, id));
489 }
490
TEST_F(MessageCenterImplTest,NotificationBlocker)491 TEST_F(MessageCenterImplTest, NotificationBlocker) {
492 NotifierId notifier_id(NotifierType::APPLICATION, "app1");
493 // Multiple blockers to verify the case that one blocker blocks but another
494 // doesn't.
495 ToggledNotificationBlocker blocker1(message_center());
496 ToggledNotificationBlocker blocker2(message_center());
497
498 message_center()->AddNotification(std::unique_ptr<Notification>(
499 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
500 UTF8ToUTF16("message"), gfx::Image() /* icon */,
501 base::string16() /* display_source */, GURL(),
502 notifier_id, RichNotificationData(), nullptr)));
503 message_center()->AddNotification(std::unique_ptr<Notification>(
504 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
505 UTF8ToUTF16("message"), gfx::Image() /* icon */,
506 base::string16() /* display_source */, GURL(),
507 notifier_id, RichNotificationData(), nullptr)));
508 EXPECT_EQ(2u, message_center()->GetPopupNotifications().size());
509 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
510
511 // "id1" is displayed as a pop-up so that it will be closed when blocked.
512 message_center()->DisplayedNotification("id1", DISPLAY_SOURCE_POPUP);
513
514 // Block all notifications. All popups are gone and message center should be
515 // hidden.
516 blocker1.SetNotificationsEnabled(false);
517 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
518 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
519
520 // Updates |blocker2| state, which doesn't affect the global state.
521 blocker2.SetNotificationsEnabled(false);
522 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
523 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
524
525 blocker2.SetNotificationsEnabled(true);
526 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
527 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
528
529 // If |blocker2| blocks, then unblocking blocker1 doesn't change the global
530 // state.
531 blocker2.SetNotificationsEnabled(false);
532 blocker1.SetNotificationsEnabled(true);
533 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
534 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
535
536 // Unblock both blockers, which recovers the global state, the displayed
537 // pop-ups before blocking aren't shown but the never-displayed ones will
538 // be shown.
539 blocker2.SetNotificationsEnabled(true);
540 NotificationList::PopupNotifications popups =
541 message_center()->GetPopupNotifications();
542 EXPECT_EQ(1u, popups.size());
543 EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
544 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
545 }
546
TEST_F(MessageCenterImplTest,NotificationsDuringBlocked)547 TEST_F(MessageCenterImplTest, NotificationsDuringBlocked) {
548 NotifierId notifier_id(NotifierType::APPLICATION, "app1");
549 ToggledNotificationBlocker blocker(message_center());
550
551 message_center()->AddNotification(std::unique_ptr<Notification>(
552 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
553 UTF8ToUTF16("message"), gfx::Image() /* icon */,
554 base::string16() /* display_source */, GURL(),
555 notifier_id, RichNotificationData(), nullptr)));
556 EXPECT_EQ(1u, message_center()->GetPopupNotifications().size());
557 EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size());
558
559 // "id1" is displayed as a pop-up so that it will be closed when blocked.
560 message_center()->DisplayedNotification("id1", DISPLAY_SOURCE_POPUP);
561
562 // Create a notification during blocked. Still no popups.
563 blocker.SetNotificationsEnabled(false);
564 message_center()->AddNotification(std::unique_ptr<Notification>(
565 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
566 UTF8ToUTF16("message"), gfx::Image() /* icon */,
567 base::string16() /* display_source */, GURL(),
568 notifier_id, RichNotificationData(), nullptr)));
569 EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
570 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
571
572 // Unblock notifications, the id1 should appear as a popup.
573 blocker.SetNotificationsEnabled(true);
574 NotificationList::PopupNotifications popups =
575 message_center()->GetPopupNotifications();
576 EXPECT_EQ(1u, popups.size());
577 EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
578 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
579 }
580
581 // Similar to other blocker cases but this test case allows |notifier_id2| even
582 // in blocked.
TEST_F(MessageCenterImplTest,NotificationBlockerAllowsPopups)583 TEST_F(MessageCenterImplTest, NotificationBlockerAllowsPopups) {
584 NotifierId notifier_id1(NotifierType::APPLICATION, "app1");
585 NotifierId notifier_id2(NotifierType::APPLICATION, "app2");
586 PopupNotificationBlocker blocker(message_center(), notifier_id2);
587
588 message_center()->AddNotification(std::unique_ptr<Notification>(
589 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
590 UTF8ToUTF16("message"), gfx::Image() /* icon */,
591 base::string16() /* display_source */, GURL(),
592 notifier_id1, RichNotificationData(), nullptr)));
593 message_center()->AddNotification(std::unique_ptr<Notification>(
594 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
595 UTF8ToUTF16("message"), gfx::Image() /* icon */,
596 base::string16() /* display_source */, GURL(),
597 notifier_id2, RichNotificationData(), nullptr)));
598
599 // "id1" is displayed as a pop-up so that it will be closed when blocked.
600 message_center()->DisplayedNotification("id1", DISPLAY_SOURCE_POPUP);
601
602 // "id1" is closed but "id2" is still visible as a popup.
603 blocker.SetNotificationsEnabled(false);
604 NotificationList::PopupNotifications popups =
605 message_center()->GetPopupNotifications();
606 EXPECT_EQ(1u, popups.size());
607 EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
608 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
609
610 message_center()->AddNotification(std::unique_ptr<Notification>(
611 new Notification(NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title"),
612 UTF8ToUTF16("message"), gfx::Image() /* icon */,
613 base::string16() /* display_source */, GURL(),
614 notifier_id1, RichNotificationData(), nullptr)));
615 message_center()->AddNotification(std::unique_ptr<Notification>(
616 new Notification(NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title"),
617 UTF8ToUTF16("message"), gfx::Image() /* icon */,
618 base::string16() /* display_source */, GURL(),
619 notifier_id2, RichNotificationData(), nullptr)));
620 popups = message_center()->GetPopupNotifications();
621 EXPECT_EQ(2u, popups.size());
622 EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
623 EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
624 EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
625
626 blocker.SetNotificationsEnabled(true);
627 popups = message_center()->GetPopupNotifications();
628 EXPECT_EQ(3u, popups.size());
629 EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
630 EXPECT_TRUE(PopupNotificationsContain(popups, "id3"));
631 EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
632 EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
633 }
634
635 // TotalNotificationBlocker suppresses showing notifications even from the list.
636 // This would provide the feature to 'separated' message centers per-profile for
637 // ChromeOS multi-login.
TEST_F(MessageCenterImplTest,TotalNotificationBlocker)638 TEST_F(MessageCenterImplTest, TotalNotificationBlocker) {
639 NotifierId notifier_id1(NotifierType::APPLICATION, "app1");
640 NotifierId notifier_id2(NotifierType::APPLICATION, "app2");
641 TotalNotificationBlocker blocker(message_center(), notifier_id2);
642
643 message_center()->AddNotification(std::unique_ptr<Notification>(
644 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
645 UTF8ToUTF16("message"), gfx::Image() /* icon */,
646 base::string16() /* display_source */, GURL(),
647 notifier_id1, RichNotificationData(), nullptr)));
648 message_center()->AddNotification(std::unique_ptr<Notification>(
649 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
650 UTF8ToUTF16("message"), gfx::Image() /* icon */,
651 base::string16() /* display_source */, GURL(),
652 notifier_id2, RichNotificationData(), nullptr)));
653
654 // "id1" becomes invisible while "id2" is still visible.
655 blocker.SetNotificationsEnabled(false);
656 EXPECT_EQ(1u, message_center()->NotificationCount());
657 NotificationList::Notifications notifications =
658 message_center()->GetVisibleNotifications();
659 EXPECT_FALSE(NotificationsContain(notifications, "id1"));
660 EXPECT_TRUE(NotificationsContain(notifications, "id2"));
661
662 message_center()->AddNotification(std::unique_ptr<Notification>(
663 new Notification(NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title"),
664 UTF8ToUTF16("message"), gfx::Image() /* icon */,
665 base::string16() /* display_source */, GURL(),
666 notifier_id1, RichNotificationData(), nullptr)));
667 message_center()->AddNotification(std::unique_ptr<Notification>(
668 new Notification(NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title"),
669 UTF8ToUTF16("message"), gfx::Image() /* icon */,
670 base::string16() /* display_source */, GURL(),
671 notifier_id2, RichNotificationData(), nullptr)));
672 EXPECT_EQ(2u, message_center()->NotificationCount());
673 notifications = message_center()->GetVisibleNotifications();
674 EXPECT_FALSE(NotificationsContain(notifications, "id1"));
675 EXPECT_TRUE(NotificationsContain(notifications, "id2"));
676 EXPECT_FALSE(NotificationsContain(notifications, "id3"));
677 EXPECT_TRUE(NotificationsContain(notifications, "id4"));
678
679 blocker.SetNotificationsEnabled(true);
680 EXPECT_EQ(4u, message_center()->NotificationCount());
681 notifications = message_center()->GetVisibleNotifications();
682 EXPECT_TRUE(NotificationsContain(notifications, "id1"));
683 EXPECT_TRUE(NotificationsContain(notifications, "id2"));
684 EXPECT_TRUE(NotificationsContain(notifications, "id3"));
685 EXPECT_TRUE(NotificationsContain(notifications, "id4"));
686
687 // Remove just visible notifications.
688 blocker.SetNotificationsEnabled(false);
689 message_center()->RemoveAllNotifications(
690 false /* by_user */, MessageCenter::RemoveType::NON_PINNED);
691 EXPECT_EQ(0u, message_center()->NotificationCount());
692 blocker.SetNotificationsEnabled(true);
693 EXPECT_EQ(2u, message_center()->NotificationCount());
694 notifications = message_center()->GetVisibleNotifications();
695 EXPECT_TRUE(NotificationsContain(notifications, "id1"));
696 EXPECT_FALSE(NotificationsContain(notifications, "id2"));
697 EXPECT_TRUE(NotificationsContain(notifications, "id3"));
698 EXPECT_FALSE(NotificationsContain(notifications, "id4"));
699
700 // And remove all including invisible notifications.
701 blocker.SetNotificationsEnabled(false);
702 message_center()->RemoveAllNotifications(false /* by_user */,
703 MessageCenter::RemoveType::ALL);
704 EXPECT_EQ(0u, message_center()->NotificationCount());
705 }
706
TEST_F(MessageCenterImplTest,RemoveAllNotifications)707 TEST_F(MessageCenterImplTest, RemoveAllNotifications) {
708 NotifierId notifier_id1(NotifierType::APPLICATION, "app1");
709 NotifierId notifier_id2(NotifierType::APPLICATION, "app2");
710
711 TotalNotificationBlocker blocker(message_center(), notifier_id1);
712 blocker.SetNotificationsEnabled(false);
713
714 // Notification 1: Visible, non-pinned
715 message_center()->AddNotification(std::unique_ptr<Notification>(
716 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
717 UTF8ToUTF16("message"), gfx::Image() /* icon */,
718 base::string16() /* display_source */, GURL(),
719 notifier_id1, RichNotificationData(), nullptr)));
720
721 // Notification 2: Invisible, non-pinned
722 message_center()->AddNotification(std::unique_ptr<Notification>(
723 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
724 UTF8ToUTF16("message"), gfx::Image() /* icon */,
725 base::string16() /* display_source */, GURL(),
726 notifier_id2, RichNotificationData(), nullptr)));
727
728 // Remove all the notifications which are visible and non-pinned.
729 message_center()->RemoveAllNotifications(
730 false /* by_user */, MessageCenter::RemoveType::NON_PINNED);
731
732 EXPECT_EQ(0u, message_center()->NotificationCount());
733 blocker.SetNotificationsEnabled(true); // Show invisible notifications.
734 EXPECT_EQ(1u, message_center()->NotificationCount());
735
736 NotificationList::Notifications notifications =
737 message_center()->GetVisibleNotifications();
738 // Notification 1 should be removed.
739 EXPECT_FALSE(NotificationsContain(notifications, "id1"));
740 // Notification 2 shouldn't be removed since it was invisible.
741 EXPECT_TRUE(NotificationsContain(notifications, "id2"));
742 }
743
744 #if defined(OS_CHROMEOS)
TEST_F(MessageCenterImplTest,RemoveAllNotificationsWithPinned)745 TEST_F(MessageCenterImplTest, RemoveAllNotificationsWithPinned) {
746 NotifierId notifier_id1(NotifierType::APPLICATION, "app1");
747 NotifierId notifier_id2(NotifierType::APPLICATION, "app2");
748
749 TotalNotificationBlocker blocker(message_center(), notifier_id1);
750 blocker.SetNotificationsEnabled(false);
751
752 // Notification 1: Visible, non-pinned
753 message_center()->AddNotification(std::unique_ptr<Notification>(
754 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
755 UTF8ToUTF16("message"), gfx::Image() /* icon */,
756 base::string16() /* display_source */, GURL(),
757 notifier_id1, RichNotificationData(), nullptr)));
758
759 // Notification 2: Invisible, non-pinned
760 message_center()->AddNotification(std::unique_ptr<Notification>(
761 new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"),
762 UTF8ToUTF16("message"), gfx::Image() /* icon */,
763 base::string16() /* display_source */, GURL(),
764 notifier_id2, RichNotificationData(), nullptr)));
765
766 // Notification 3: Visible, pinned
767 std::unique_ptr<Notification> notification3(
768 new Notification(NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title"),
769 UTF8ToUTF16("message"), gfx::Image() /* icon */,
770 base::string16() /* display_source */, GURL(),
771 notifier_id1, RichNotificationData(), nullptr));
772 notification3->set_pinned(true);
773 message_center()->AddNotification(std::move(notification3));
774
775 // Notification 4: Invisible, pinned
776 std::unique_ptr<Notification> notification4(
777 new Notification(NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title"),
778 UTF8ToUTF16("message"), gfx::Image() /* icon */,
779 base::string16() /* display_source */, GURL(),
780 notifier_id2, RichNotificationData(), nullptr));
781 notification4->set_pinned(true);
782 message_center()->AddNotification(std::move(notification4));
783
784 // Remove all the notifications which are visible and non-pinned.
785 message_center()->RemoveAllNotifications(
786 false /* by_user */, MessageCenter::RemoveType::NON_PINNED);
787
788 EXPECT_EQ(1u, message_center()->NotificationCount());
789 blocker.SetNotificationsEnabled(true); // Show invisible notifications.
790 EXPECT_EQ(3u, message_center()->NotificationCount());
791
792 NotificationList::Notifications notifications =
793 message_center()->GetVisibleNotifications();
794 // Notification 1 should be removed.
795 EXPECT_FALSE(NotificationsContain(notifications, "id1"));
796 // Notification 2 shouldn't be removed since it was invisible.
797 EXPECT_TRUE(NotificationsContain(notifications, "id2"));
798 // Notification 3 shouldn't be removed since it was pinned.
799 EXPECT_TRUE(NotificationsContain(notifications, "id3"));
800 // Notification 4 shouldn't be removed since it was invisible and pinned.
801 EXPECT_TRUE(NotificationsContain(notifications, "id4"));
802 }
803 #endif
804
TEST_F(MessageCenterImplTest,NotifierEnabledChanged)805 TEST_F(MessageCenterImplTest, NotifierEnabledChanged) {
806 ASSERT_EQ(0u, message_center()->NotificationCount());
807 message_center()->AddNotification(
808 CreateSimpleNotificationWithNotifierId("id1-1", "app1"));
809 message_center()->AddNotification(
810 CreateSimpleNotificationWithNotifierId("id1-2", "app1"));
811 message_center()->AddNotification(
812 CreateSimpleNotificationWithNotifierId("id1-3", "app1"));
813 message_center()->AddNotification(
814 CreateSimpleNotificationWithNotifierId("id2-1", "app2"));
815 message_center()->AddNotification(
816 CreateSimpleNotificationWithNotifierId("id2-2", "app2"));
817 message_center()->AddNotification(
818 CreateSimpleNotificationWithNotifierId("id2-3", "app2"));
819 message_center()->AddNotification(
820 CreateSimpleNotificationWithNotifierId("id2-4", "app2"));
821 message_center()->AddNotification(
822 CreateSimpleNotificationWithNotifierId("id2-5", "app2"));
823 ASSERT_EQ(8u, message_center()->NotificationCount());
824
825 // Removing all of app2's notifications should only leave app1's.
826 message_center()->RemoveNotificationsForNotifierId(
827 NotifierId(NotifierType::APPLICATION, "app2"));
828 ASSERT_EQ(3u, message_center()->NotificationCount());
829
830 // Removal operations should be idempotent.
831 message_center()->RemoveNotificationsForNotifierId(
832 NotifierId(NotifierType::APPLICATION, "app2"));
833 ASSERT_EQ(3u, message_center()->NotificationCount());
834
835 // Now we remove the remaining notifications.
836 message_center()->RemoveNotificationsForNotifierId(
837 NotifierId(NotifierType::APPLICATION, "app1"));
838 ASSERT_EQ(0u, message_center()->NotificationCount());
839 }
840
TEST_F(MessageCenterImplTest,UpdateWhileMessageCenterVisible)841 TEST_F(MessageCenterImplTest, UpdateWhileMessageCenterVisible) {
842 std::string id1("id1");
843 std::string id2("id2");
844 NotifierId notifier_id1(NotifierType::APPLICATION, "app1");
845
846 // First, add and update a notification to ensure updates happen
847 // normally.
848 std::unique_ptr<Notification> notification = CreateSimpleNotification(id1);
849 message_center()->AddNotification(std::move(notification));
850 notification = CreateSimpleNotification(id2);
851 message_center()->UpdateNotification(id1, std::move(notification));
852 EXPECT_TRUE(message_center()->FindVisibleNotificationById(id2));
853 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id1));
854
855 // Then open the message center.
856 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
857
858 // Then update a notification; the update should have propagated.
859 notification = CreateSimpleNotification(id1);
860 message_center()->UpdateNotification(id2, std::move(notification));
861 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id2));
862 EXPECT_TRUE(message_center()->FindVisibleNotificationById(id1));
863 }
864
TEST_F(MessageCenterImplTest,AddWhileMessageCenterVisible)865 TEST_F(MessageCenterImplTest, AddWhileMessageCenterVisible) {
866 std::string id("id1");
867
868 // Then open the message center.
869 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
870
871 // Add a notification and confirm the adding should have propagated.
872 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
873 message_center()->AddNotification(std::move(notification));
874 EXPECT_TRUE(message_center()->FindVisibleNotificationById(id));
875 }
876
TEST_F(MessageCenterImplTest,RemoveWhileMessageCenterVisible)877 TEST_F(MessageCenterImplTest, RemoveWhileMessageCenterVisible) {
878 std::string id("id1");
879
880 // First, add a notification to ensure updates happen normally.
881 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
882 message_center()->AddNotification(std::move(notification));
883 EXPECT_TRUE(message_center()->FindVisibleNotificationById(id));
884
885 // Then open the message center.
886 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
887
888 // Then update a notification; the update should have propagated.
889 message_center()->RemoveNotification(id, false);
890 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
891 }
892
TEST_F(MessageCenterImplTest,RemoveNonVisibleNotification)893 TEST_F(MessageCenterImplTest, RemoveNonVisibleNotification) {
894 // Add two notifications.
895 message_center()->AddNotification(CreateSimpleNotification("id1"));
896 message_center()->AddNotification(CreateSimpleNotification("id2"));
897 EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
898
899 // Add a blocker to block all notifications.
900 NotifierId allowed_notifier_id(NotifierType::APPLICATION, "notifier");
901 TotalNotificationBlocker blocker(message_center(), allowed_notifier_id);
902 blocker.SetNotificationsEnabled(false);
903 EXPECT_EQ(0u, message_center()->GetVisibleNotifications().size());
904
905 // Removing a non-visible notification should work.
906 message_center()->RemoveNotification("id1", false);
907 blocker.SetNotificationsEnabled(true);
908 EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size());
909
910 // Also try removing a visible notification.
911 message_center()->RemoveNotification("id2", false);
912 EXPECT_EQ(0u, message_center()->GetVisibleNotifications().size());
913 }
914
TEST_F(MessageCenterImplTest,FindNotificationsByAppId)915 TEST_F(MessageCenterImplTest, FindNotificationsByAppId) {
916 message_center()->SetHasMessageCenterView(true);
917
918 const std::string app_id1("app_id1");
919 const std::string id1("id1");
920
921 // Add a notification for |app_id1|.
922 std::unique_ptr<Notification> notification =
923 CreateNotificationWithNotifierId(id1, app_id1, NOTIFICATION_TYPE_SIMPLE);
924 message_center()->AddNotification(std::move(notification));
925 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id1).size());
926
927 // Mark the notification as shown but not read.
928 message_center()->MarkSinglePopupAsShown(id1, false);
929 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id1).size());
930
931 // Mark the notification as shown and read.
932 message_center()->MarkSinglePopupAsShown(id1, true);
933 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id1).size());
934
935 // Remove the notification.
936 message_center()->RemoveNotification(id1, true);
937 EXPECT_EQ(0u, message_center()->FindNotificationsByAppId(app_id1).size());
938
939 // Add two notifications for |app_id1|.
940 notification =
941 CreateNotificationWithNotifierId(id1, app_id1, NOTIFICATION_TYPE_SIMPLE);
942 message_center()->AddNotification(std::move(notification));
943
944 const std::string id2("id2");
945 notification =
946 CreateNotificationWithNotifierId(id2, app_id1, NOTIFICATION_TYPE_SIMPLE);
947 message_center()->AddNotification(std::move(notification));
948 EXPECT_EQ(2u, message_center()->FindNotificationsByAppId(app_id1).size());
949
950 // Remove |id2|,there should only be one notification for |app_id1|.
951 message_center()->RemoveNotification(id2, true);
952 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id1).size());
953
954 // Add a notification for |app_id2|.
955 const std::string app_id2("app_id2");
956 const std::string id3("id3");
957 notification =
958 CreateNotificationWithNotifierId(id3, app_id2, NOTIFICATION_TYPE_SIMPLE);
959 message_center()->AddNotification(std::move(notification));
960 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id1).size());
961 EXPECT_EQ(1u, message_center()->FindNotificationsByAppId(app_id2).size());
962
963 for (std::string app_id : {app_id1, app_id2}) {
964 for (auto* notification :
965 message_center()->FindNotificationsByAppId(app_id)) {
966 EXPECT_EQ(app_id, notification->notifier_id().id);
967 }
968 }
969
970 // Remove all notifications.
971 message_center()->RemoveAllNotifications(true,
972 MessageCenterImpl::RemoveType::ALL);
973
974 EXPECT_EQ(0u, message_center()->FindNotificationsByAppId(app_id1).size());
975 EXPECT_EQ(0u, message_center()->FindNotificationsByAppId(app_id2).size());
976 }
977
TEST_F(MessageCenterImplTest,QueueWhenCenterVisible)978 TEST_F(MessageCenterImplTest, QueueWhenCenterVisible) {
979 TestAddObserver observer(message_center());
980
981 message_center()->AddNotification(CreateSimpleNotification("n"));
982 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
983 message_center()->AddNotification(CreateSimpleNotification("n2"));
984
985 // 'update-n' should happen since SetVisibility updates is_read status of n.
986 EXPECT_EQ("add-n_update-n", observer.log("n"));
987
988 message_center()->SetVisibility(VISIBILITY_TRANSIENT);
989
990 EXPECT_EQ("add-n2", observer.log("n2"));
991 }
992
TEST_F(MessageCenterImplTest,UpdateProgressNotificationWhenCenterVisible)993 TEST_F(MessageCenterImplTest, UpdateProgressNotificationWhenCenterVisible) {
994 TestAddObserver observer(message_center());
995
996 // Add a progress notification and update it while the message center
997 // is visible.
998 std::unique_ptr<Notification> notification = CreateSimpleNotification("n");
999 notification->set_type(NOTIFICATION_TYPE_PROGRESS);
1000 Notification notification_copy = *notification;
1001 message_center()->AddNotification(std::move(notification));
1002 message_center()->ClickOnNotification("n");
1003 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
1004 observer.reset_logs();
1005 notification_copy.set_progress(50);
1006 message_center()->UpdateNotification(
1007 "n", std::make_unique<Notification>(notification_copy));
1008
1009 // Expect that the progress notification update is performed.
1010 EXPECT_EQ("update-n", observer.log("n"));
1011 }
1012
TEST_F(MessageCenterImplTest,UpdateNonProgressNotificationWhenCenterVisible)1013 TEST_F(MessageCenterImplTest, UpdateNonProgressNotificationWhenCenterVisible) {
1014 TestAddObserver observer(message_center());
1015
1016 // Add a non-progress notification and update it while the message center
1017 // is visible.
1018 std::unique_ptr<Notification> notification = CreateSimpleNotification("n");
1019 Notification notification_copy = *notification;
1020 message_center()->AddNotification(std::move(notification));
1021 message_center()->ClickOnNotification("n");
1022 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
1023 observer.reset_logs();
1024 notification_copy.set_title(base::ASCIIToUTF16("title2"));
1025 message_center()->UpdateNotification(
1026 notification_copy.id(),
1027 std::make_unique<Notification>(notification_copy));
1028
1029 // Expect that the notification update is done.
1030 EXPECT_NE("", observer.log("n"));
1031
1032 message_center()->SetVisibility(VISIBILITY_TRANSIENT);
1033 EXPECT_EQ("update-n", observer.log("n"));
1034 }
1035
TEST_F(MessageCenterImplTest,UpdateNonProgressToProgressNotificationWhenCenterVisible)1036 TEST_F(MessageCenterImplTest,
1037 UpdateNonProgressToProgressNotificationWhenCenterVisible) {
1038 TestAddObserver observer(message_center());
1039
1040 // Add a non-progress notification and change the type to progress while the
1041 // message center is visible.
1042 std::unique_ptr<Notification> notification = CreateSimpleNotification("n");
1043 Notification notification_copy = *notification;
1044 message_center()->AddNotification(std::move(notification));
1045 message_center()->ClickOnNotification("n");
1046 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
1047 observer.reset_logs();
1048 notification_copy.set_type(NOTIFICATION_TYPE_PROGRESS);
1049 message_center()->UpdateNotification(
1050 "n", std::make_unique<Notification>(notification_copy));
1051
1052 // Expect that the notification update is done.
1053 EXPECT_NE("", observer.log("n"));
1054
1055 message_center()->SetVisibility(VISIBILITY_TRANSIENT);
1056 EXPECT_EQ("update-n", observer.log("n"));
1057 }
1058
TEST_F(MessageCenterImplTest,Click)1059 TEST_F(MessageCenterImplTest, Click) {
1060 TestAddObserver observer(message_center());
1061 std::string id("n");
1062
1063 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1064 message_center()->AddNotification(std::move(notification));
1065 message_center()->ClickOnNotification(id);
1066
1067 EXPECT_EQ("Click_", GetDelegate(id)->log());
1068 }
1069
TEST_F(MessageCenterImplTest,ButtonClick)1070 TEST_F(MessageCenterImplTest, ButtonClick) {
1071 TestAddObserver observer(message_center());
1072 std::string id("n");
1073
1074 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1075 message_center()->AddNotification(std::move(notification));
1076 message_center()->ClickOnNotificationButton(id, 1);
1077
1078 EXPECT_EQ("ButtonClick_1_", GetDelegate(id)->log());
1079 }
1080
TEST_F(MessageCenterImplTest,ButtonClickWithReply)1081 TEST_F(MessageCenterImplTest, ButtonClickWithReply) {
1082 TestAddObserver observer(message_center());
1083 std::string id("n");
1084
1085 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1086 message_center()->AddNotification(std::move(notification));
1087 message_center()->ClickOnNotificationButtonWithReply(
1088 id, 1, base::UTF8ToUTF16("REPLYTEXT"));
1089
1090 EXPECT_EQ("ReplyButtonClick_1_REPLYTEXT_", GetDelegate(id)->log());
1091 }
1092
TEST_F(MessageCenterImplTest,Unlock)1093 TEST_F(MessageCenterImplTest, Unlock) {
1094 lock_screen_controller()->set_is_screen_locked(true);
1095
1096 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1097 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1098
1099 lock_screen_controller()->SimulateUnlock();
1100
1101 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1102 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1103
1104 lock_screen_controller()->set_is_screen_locked(true);
1105
1106 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1107 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1108
1109 lock_screen_controller()->SimulateUnlock();
1110
1111 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1112 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1113 }
1114
TEST_F(MessageCenterImplTest,ClickOnLockScreen)1115 TEST_F(MessageCenterImplTest, ClickOnLockScreen) {
1116 lock_screen_controller()->set_is_screen_locked(true);
1117
1118 TestAddObserver observer(message_center());
1119 std::string id("n");
1120
1121 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1122 message_center()->AddNotification(std::move(notification));
1123 message_center()->ClickOnNotification(id);
1124
1125 EXPECT_EQ("", GetDelegate(id)->log());
1126 EXPECT_TRUE(lock_screen_controller()->HasPendingCallback());
1127 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1128
1129 lock_screen_controller()->SimulateUnlock();
1130
1131 EXPECT_EQ("Click_", GetDelegate(id)->log());
1132 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1133 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1134 }
1135
TEST_F(MessageCenterImplTest,ClickAndCancelOnLockScreen)1136 TEST_F(MessageCenterImplTest, ClickAndCancelOnLockScreen) {
1137 lock_screen_controller()->set_is_screen_locked(true);
1138
1139 TestAddObserver observer(message_center());
1140 std::string id("n");
1141
1142 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1143 message_center()->AddNotification(std::move(notification));
1144 message_center()->ClickOnNotification(id);
1145
1146 EXPECT_EQ("", GetDelegate(id)->log());
1147 EXPECT_TRUE(lock_screen_controller()->HasPendingCallback());
1148 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1149
1150 lock_screen_controller()->CancelClick();
1151
1152 EXPECT_EQ("", GetDelegate(id)->log());
1153 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1154 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1155
1156 lock_screen_controller()->SimulateUnlock();
1157
1158 EXPECT_EQ("", GetDelegate(id)->log());
1159 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1160 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1161 }
1162
TEST_F(MessageCenterImplTest,ButtonClickOnLockScreen)1163 TEST_F(MessageCenterImplTest, ButtonClickOnLockScreen) {
1164 lock_screen_controller()->set_is_screen_locked(true);
1165
1166 TestAddObserver observer(message_center());
1167 std::string id("n");
1168
1169 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1170 message_center()->AddNotification(std::move(notification));
1171 message_center()->ClickOnNotificationButton(id, 1);
1172
1173 EXPECT_EQ("", GetDelegate(id)->log());
1174 EXPECT_TRUE(lock_screen_controller()->HasPendingCallback());
1175 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1176
1177 lock_screen_controller()->SimulateUnlock();
1178
1179 EXPECT_EQ("ButtonClick_1_", GetDelegate(id)->log());
1180 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1181 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1182 }
1183
TEST_F(MessageCenterImplTest,ButtonClickWithReplyOnLockScreen)1184 TEST_F(MessageCenterImplTest, ButtonClickWithReplyOnLockScreen) {
1185 lock_screen_controller()->set_is_screen_locked(true);
1186
1187 TestAddObserver observer(message_center());
1188 std::string id("n");
1189
1190 std::unique_ptr<Notification> notification = CreateSimpleNotification(id);
1191 message_center()->AddNotification(std::move(notification));
1192 message_center()->ClickOnNotificationButtonWithReply(
1193 id, 1, base::UTF8ToUTF16("REPLYTEXT"));
1194
1195 EXPECT_EQ("", GetDelegate(id)->log());
1196 EXPECT_TRUE(lock_screen_controller()->HasPendingCallback());
1197 EXPECT_TRUE(lock_screen_controller()->IsScreenLocked());
1198
1199 lock_screen_controller()->SimulateUnlock();
1200
1201 EXPECT_EQ("ReplyButtonClick_1_REPLYTEXT_", GetDelegate(id)->log());
1202 EXPECT_FALSE(lock_screen_controller()->HasPendingCallback());
1203 EXPECT_FALSE(lock_screen_controller()->IsScreenLocked());
1204 }
1205
1206 } // namespace internal
1207 } // namespace message_center
1208