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