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
|
// Copyright (c) 2012 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/notification_list.h"
#include "base/basictypes.h"
#include "base/i18n/time_formatting.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center_style.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 NotificationListTest : public testing::Test {
public:
NotificationListTest() {}
~NotificationListTest() override {}
void SetUp() override {
notification_list_.reset(new NotificationList());
counter_ = 0;
}
protected:
// Currently NotificationListTest doesn't care about some fields like title or
// message, so put a simple template on it. Returns the id of the new
// notification.
std::string AddNotification(
const message_center::RichNotificationData& optional_fields) {
std::string new_id;
scoped_ptr<Notification> notification(
MakeNotification(optional_fields, &new_id));
notification_list_->AddNotification(notification.Pass());
counter_++;
return new_id;
}
std::string AddNotification() {
return AddNotification(message_center::RichNotificationData());
}
// Construct a new notification for testing, but don't add it to the list yet.
scoped_ptr<Notification> MakeNotification(
const message_center::RichNotificationData& optional_fields,
std::string* id_out) {
*id_out = base::StringPrintf(kIdFormat, counter_);
scoped_ptr<Notification> notification(new Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
*id_out,
UTF8ToUTF16(base::StringPrintf(kTitleFormat, counter_)),
UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION, kExtensionId),
optional_fields,
NULL));
return notification.Pass();
}
scoped_ptr<Notification> MakeNotification(std::string* id_out) {
return MakeNotification(message_center::RichNotificationData(), id_out);
}
// Utility methods of AddNotification.
std::string AddPriorityNotification(NotificationPriority priority) {
message_center::RichNotificationData optional;
optional.priority = priority;
return AddNotification(optional);
}
NotificationList::PopupNotifications GetPopups() {
return notification_list()->GetPopupNotifications(blockers_, NULL);
}
size_t GetPopupCounts() {
return GetPopups().size();
}
Notification* GetNotification(const std::string& id) {
NotificationList::Notifications::iterator iter =
notification_list()->GetNotification(id);
if (iter == notification_list()->notifications_.end())
return NULL;
return *iter;
}
NotificationList* notification_list() { return notification_list_.get(); }
const NotificationBlockers& blockers() const { return blockers_; }
static const char kIdFormat[];
static const char kTitleFormat[];
static const char kMessageFormat[];
static const char kDisplaySource[];
static const char kExtensionId[];
private:
scoped_ptr<NotificationList> notification_list_;
NotificationBlockers blockers_;
size_t counter_;
DISALLOW_COPY_AND_ASSIGN(NotificationListTest);
};
bool IsInNotifications(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;
}
const char NotificationListTest::kIdFormat[] = "id%ld";
const char NotificationListTest::kTitleFormat[] = "id%ld";
const char NotificationListTest::kMessageFormat[] = "message%ld";
const char NotificationListTest::kDisplaySource[] = "source";
const char NotificationListTest::kExtensionId[] = "ext";
TEST_F(NotificationListTest, Basic) {
ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
std::string id0 = AddNotification();
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
std::string id1 = AddNotification();
EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(2u, notification_list()->UnreadCount(blockers()));
EXPECT_TRUE(notification_list()->HasPopupNotifications(blockers()));
EXPECT_TRUE(notification_list()->GetNotificationById(id0));
EXPECT_TRUE(notification_list()->GetNotificationById(id1));
EXPECT_FALSE(notification_list()->GetNotificationById(id1 + "foo"));
EXPECT_EQ(2u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(id0, true);
notification_list()->MarkSinglePopupAsShown(id1, true);
EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(0u, GetPopupCounts());
notification_list()->RemoveNotification(id0);
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
AddNotification();
EXPECT_EQ(2u, notification_list()->NotificationCount(blockers()));
}
TEST_F(NotificationListTest, MessageCenterVisible) {
AddNotification();
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(1u, notification_list()->UnreadCount(blockers()));
ASSERT_EQ(1u, GetPopupCounts());
// Make the message center visible. It resets the unread count and popup
// counts.
notification_list()->SetMessageCenterVisible(true, NULL);
ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
ASSERT_EQ(0u, GetPopupCounts());
}
TEST_F(NotificationListTest, UnreadCount) {
std::string id0 = AddNotification();
std::string id1 = AddNotification();
ASSERT_EQ(2u, notification_list()->UnreadCount(blockers()));
notification_list()->MarkSinglePopupAsDisplayed(id0);
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
notification_list()->MarkSinglePopupAsDisplayed(id0);
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
notification_list()->MarkSinglePopupAsDisplayed(id1);
EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
}
TEST_F(NotificationListTest, UpdateNotification) {
std::string id0 = AddNotification();
std::string replaced = id0 + "_replaced";
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
replaced,
UTF8ToUTF16("newtitle"),
UTF8ToUTF16("newbody"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION, kExtensionId),
message_center::RichNotificationData(),
NULL));
notification_list()->UpdateNotificationMessage(id0, notification.Pass());
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
const NotificationList::Notifications notifications =
notification_list()->GetVisibleNotifications(blockers());
EXPECT_EQ(replaced, (*notifications.begin())->id());
EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title());
EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message());
}
TEST_F(NotificationListTest, GetNotificationsByNotifierId) {
NotifierId id0(NotifierId::APPLICATION, "ext0");
NotifierId id1(NotifierId::APPLICATION, "ext1");
NotifierId id2(GURL("http://example.com"));
NotifierId id3(NotifierId::SYSTEM_COMPONENT, "system-notifier");
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id0",
UTF8ToUTF16("title0"),
UTF8ToUTF16("message0"),
gfx::Image(),
UTF8ToUTF16("source0"),
id0,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id1",
UTF8ToUTF16("title1"),
UTF8ToUTF16("message1"),
gfx::Image(),
UTF8ToUTF16("source0"),
id0,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id2",
UTF8ToUTF16("title1"),
UTF8ToUTF16("message1"),
gfx::Image(),
UTF8ToUTF16("source1"),
id0,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id3",
UTF8ToUTF16("title1"),
UTF8ToUTF16("message1"),
gfx::Image(),
UTF8ToUTF16("source2"),
id1,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id4",
UTF8ToUTF16("title1"),
UTF8ToUTF16("message1"),
gfx::Image(),
UTF8ToUTF16("source2"),
id2,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"id5",
UTF8ToUTF16("title1"),
UTF8ToUTF16("message1"),
gfx::Image(),
UTF8ToUTF16("source2"),
id3,
message_center::RichNotificationData(),
NULL));
notification_list()->AddNotification(notification.Pass());
NotificationList::Notifications by_notifier_id =
notification_list()->GetNotificationsByNotifierId(id0);
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id0"));
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id1"));
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id2"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
by_notifier_id = notification_list()->GetNotificationsByNotifierId(id1);
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id3"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
by_notifier_id = notification_list()->GetNotificationsByNotifierId(id2);
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id4"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id5"));
by_notifier_id = notification_list()->GetNotificationsByNotifierId(id3);
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id0"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id1"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id2"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id3"));
EXPECT_FALSE(IsInNotifications(by_notifier_id, "id4"));
EXPECT_TRUE(IsInNotifications(by_notifier_id, "id5"));
}
TEST_F(NotificationListTest, OldPopupShouldNotBeHidden) {
std::vector<std::string> ids;
for (size_t i = 0; i <= kMaxVisiblePopupNotifications; i++)
ids.push_back(AddNotification());
NotificationList::PopupNotifications popups = GetPopups();
// The popup should contain the oldest kMaxVisiblePopupNotifications. Newer
// one should come earlier in the popup list. It means, the last element
// of |popups| should be the firstly added one, and so on.
EXPECT_EQ(kMaxVisiblePopupNotifications, popups.size());
NotificationList::PopupNotifications::const_reverse_iterator iter =
popups.rbegin();
for (size_t i = 0; i < kMaxVisiblePopupNotifications; ++i, ++iter) {
EXPECT_EQ(ids[i], (*iter)->id()) << i;
}
for (NotificationList::PopupNotifications::const_iterator iter =
popups.begin(); iter != popups.end(); ++iter) {
notification_list()->MarkSinglePopupAsShown((*iter)->id(), false);
}
popups.clear();
popups = GetPopups();
EXPECT_EQ(1u, popups.size());
EXPECT_EQ(ids[ids.size() - 1], (*popups.begin())->id());
}
TEST_F(NotificationListTest, Priority) {
ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
// Default priority has the limit on the number of the popups.
for (size_t i = 0; i <= kMaxVisiblePopupNotifications; ++i)
AddNotification();
EXPECT_EQ(kMaxVisiblePopupNotifications + 1,
notification_list()->NotificationCount(blockers()));
EXPECT_EQ(kMaxVisiblePopupNotifications, GetPopupCounts());
// Low priority: not visible to popups.
notification_list()->SetMessageCenterVisible(true, NULL);
notification_list()->SetMessageCenterVisible(false, NULL);
EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
AddPriorityNotification(LOW_PRIORITY);
EXPECT_EQ(kMaxVisiblePopupNotifications + 2,
notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
EXPECT_EQ(0u, GetPopupCounts());
// Minimum priority: doesn't update the unread count.
AddPriorityNotification(MIN_PRIORITY);
EXPECT_EQ(kMaxVisiblePopupNotifications + 3,
notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
EXPECT_EQ(0u, GetPopupCounts());
NotificationList::Notifications notifications =
notification_list()->GetVisibleNotifications(blockers());
for (NotificationList::Notifications::const_iterator iter =
notifications.begin(); iter != notifications.end(); ++iter) {
notification_list()->RemoveNotification((*iter)->id());
}
// Higher priority: no limits to the number of popups.
for (size_t i = 0; i < kMaxVisiblePopupNotifications * 2; ++i)
AddPriorityNotification(HIGH_PRIORITY);
for (size_t i = 0; i < kMaxVisiblePopupNotifications * 2; ++i)
AddPriorityNotification(MAX_PRIORITY);
EXPECT_EQ(kMaxVisiblePopupNotifications * 4,
notification_list()->NotificationCount(blockers()));
EXPECT_EQ(kMaxVisiblePopupNotifications * 4, GetPopupCounts());
}
TEST_F(NotificationListTest, HasPopupsWithPriority) {
ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
AddPriorityNotification(MIN_PRIORITY);
AddPriorityNotification(MAX_PRIORITY);
EXPECT_EQ(1u, GetPopupCounts());
}
TEST_F(NotificationListTest, HasPopupsWithSystemPriority) {
ASSERT_EQ(0u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(0u, notification_list()->UnreadCount(blockers()));
std::string normal_id = AddPriorityNotification(DEFAULT_PRIORITY);
std::string system_id = AddNotification();
GetNotification(system_id)->SetSystemPriority();
EXPECT_EQ(2u, GetPopupCounts());
notification_list()->MarkSinglePopupAsDisplayed(normal_id);
notification_list()->MarkSinglePopupAsDisplayed(system_id);
notification_list()->MarkSinglePopupAsShown(normal_id, false);
notification_list()->MarkSinglePopupAsShown(system_id, false);
notification_list()->SetMessageCenterVisible(true, NULL);
notification_list()->SetMessageCenterVisible(false, NULL);
EXPECT_EQ(1u, GetPopupCounts());
// Mark as read -- emulation of mouse click.
notification_list()->MarkSinglePopupAsShown(system_id, true);
EXPECT_EQ(0u, GetPopupCounts());
}
TEST_F(NotificationListTest, PriorityPromotion) {
std::string id0 = AddPriorityNotification(LOW_PRIORITY);
std::string replaced = id0 + "_replaced";
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(0u, GetPopupCounts());
message_center::RichNotificationData optional;
optional.priority = 1;
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
replaced,
UTF8ToUTF16("newtitle"),
UTF8ToUTF16("newbody"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION, kExtensionId),
optional,
NULL));
notification_list()->UpdateNotificationMessage(id0, notification.Pass());
EXPECT_EQ(1u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, GetPopupCounts());
const NotificationList::Notifications notifications =
notification_list()->GetVisibleNotifications(blockers());
EXPECT_EQ(replaced, (*notifications.begin())->id());
EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title());
EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message());
EXPECT_EQ(1, (*notifications.begin())->priority());
}
TEST_F(NotificationListTest, PriorityPromotionWithPopups) {
std::string id0 = AddPriorityNotification(LOW_PRIORITY);
std::string id1 = AddPriorityNotification(DEFAULT_PRIORITY);
EXPECT_EQ(1u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(id1, true);
EXPECT_EQ(0u, GetPopupCounts());
// id0 promoted to LOW->DEFAULT, it'll appear as toast (popup).
message_center::RichNotificationData priority;
priority.priority = DEFAULT_PRIORITY;
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id0,
UTF8ToUTF16("newtitle"),
UTF8ToUTF16("newbody"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION, kExtensionId),
priority,
NULL));
notification_list()->UpdateNotificationMessage(id0, notification.Pass());
EXPECT_EQ(1u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(id0, true);
EXPECT_EQ(0u, GetPopupCounts());
// update with no promotion change for id0, it won't appear as a toast.
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id0,
UTF8ToUTF16("newtitle2"),
UTF8ToUTF16("newbody2"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION,
kExtensionId),
priority,
NULL));
notification_list()->UpdateNotificationMessage(id0, notification.Pass());
EXPECT_EQ(0u, GetPopupCounts());
// id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup).
priority.priority = HIGH_PRIORITY;
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id1,
UTF8ToUTF16("newtitle"),
UTF8ToUTF16("newbody"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION,
kExtensionId),
priority,
NULL));
notification_list()->UpdateNotificationMessage(id1, notification.Pass());
EXPECT_EQ(1u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(id1, true);
EXPECT_EQ(0u, GetPopupCounts());
// id1 promoted to HIGH->MAX, it'll appear as toast again.
priority.priority = MAX_PRIORITY;
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id1,
UTF8ToUTF16("newtitle2"),
UTF8ToUTF16("newbody2"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION,
kExtensionId),
priority,
NULL));
notification_list()->UpdateNotificationMessage(id1, notification.Pass());
EXPECT_EQ(1u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(id1, true);
EXPECT_EQ(0u, GetPopupCounts());
// id1 demoted to MAX->DEFAULT, no appearing as toast.
priority.priority = DEFAULT_PRIORITY;
notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id1,
UTF8ToUTF16("newtitle3"),
UTF8ToUTF16("newbody3"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION,
kExtensionId),
priority,
NULL));
notification_list()->UpdateNotificationMessage(id1, notification.Pass());
EXPECT_EQ(0u, GetPopupCounts());
}
TEST_F(NotificationListTest, WebNotificationUpdatePromotion) {
std::string notification_id = "replaced-web-notification";
scoped_ptr<Notification> original_notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
notification_id,
UTF8ToUTF16("Web Notification"),
UTF8ToUTF16("Notification contents"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(GURL("https://example.com/")),
message_center::RichNotificationData(),
NULL));
EXPECT_EQ(0u, GetPopupCounts());
notification_list()->AddNotification(original_notification.Pass());
EXPECT_EQ(1u, GetPopupCounts());
notification_list()->MarkSinglePopupAsShown(notification_id, true);
EXPECT_EQ(0u, GetPopupCounts());
scoped_ptr<Notification> replaced_notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
notification_id,
UTF8ToUTF16("Web Notification Replacement"),
UTF8ToUTF16("New notification contents"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(GURL("https://example.com/")),
message_center::RichNotificationData(),
NULL));
// Web Notifications will be re-shown as popups even if their priority didn't
// change, to match the behavior of the Web Notification API.
notification_list()->UpdateNotificationMessage(notification_id,
replaced_notification.Pass());
EXPECT_EQ(1u, GetPopupCounts());
}
TEST_F(NotificationListTest, NotificationOrderAndPriority) {
base::Time now = base::Time::Now();
message_center::RichNotificationData optional;
optional.timestamp = now;
optional.priority = 2;
std::string max_id = AddNotification(optional);
now += base::TimeDelta::FromSeconds(1);
optional.timestamp = now;
optional.priority = 1;
std::string high_id = AddNotification(optional);
now += base::TimeDelta::FromSeconds(1);
optional.timestamp = now;
optional.priority = 0;
std::string default_id = AddNotification(optional);
{
// Popups: latest comes first.
NotificationList::PopupNotifications popups = GetPopups();
EXPECT_EQ(3u, popups.size());
NotificationList::PopupNotifications::const_iterator iter = popups.begin();
EXPECT_EQ(default_id, (*iter)->id());
iter++;
EXPECT_EQ(high_id, (*iter)->id());
iter++;
EXPECT_EQ(max_id, (*iter)->id());
}
{
// Notifications: high priority comes ealier.
const NotificationList::Notifications notifications =
notification_list()->GetVisibleNotifications(blockers());
EXPECT_EQ(3u, notifications.size());
NotificationList::Notifications::const_iterator iter =
notifications.begin();
EXPECT_EQ(max_id, (*iter)->id());
iter++;
EXPECT_EQ(high_id, (*iter)->id());
iter++;
EXPECT_EQ(default_id, (*iter)->id());
}
}
TEST_F(NotificationListTest, MarkSinglePopupAsShown) {
std::string id1 = AddNotification();
std::string id2 = AddNotification();
std::string id3 = AddNotification();
ASSERT_EQ(3u, notification_list()->NotificationCount(blockers()));
ASSERT_EQ(std::min(static_cast<size_t>(3u), kMaxVisiblePopupNotifications),
GetPopupCounts());
notification_list()->MarkSinglePopupAsDisplayed(id1);
notification_list()->MarkSinglePopupAsDisplayed(id2);
notification_list()->MarkSinglePopupAsDisplayed(id3);
notification_list()->MarkSinglePopupAsShown(id2, true);
notification_list()->MarkSinglePopupAsShown(id3, false);
EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
EXPECT_EQ(1u, GetPopupCounts());
NotificationList::PopupNotifications popups = GetPopups();
EXPECT_EQ(id1, (*popups.begin())->id());
// The notifications in the NotificationCenter are unaffected by popups shown.
NotificationList::Notifications notifications =
notification_list()->GetVisibleNotifications(blockers());
NotificationList::Notifications::const_iterator iter = notifications.begin();
EXPECT_EQ(id3, (*iter)->id());
iter++;
EXPECT_EQ(id2, (*iter)->id());
iter++;
EXPECT_EQ(id1, (*iter)->id());
}
TEST_F(NotificationListTest, UpdateAfterMarkedAsShown) {
std::string id1 = AddNotification();
std::string id2 = AddNotification();
notification_list()->MarkSinglePopupAsDisplayed(id1);
notification_list()->MarkSinglePopupAsDisplayed(id2);
EXPECT_EQ(2u, GetPopupCounts());
const Notification* n1 = GetNotification(id1);
EXPECT_FALSE(n1->shown_as_popup());
EXPECT_TRUE(n1->IsRead());
notification_list()->MarkSinglePopupAsShown(id1, true);
n1 = GetNotification(id1);
EXPECT_TRUE(n1->shown_as_popup());
EXPECT_TRUE(n1->IsRead());
const std::string replaced("test-replaced-id");
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
replaced,
UTF8ToUTF16("newtitle"),
UTF8ToUTF16("newbody"),
gfx::Image(),
UTF8ToUTF16(kDisplaySource),
NotifierId(NotifierId::APPLICATION, kExtensionId),
message_center::RichNotificationData(),
NULL));
notification_list()->UpdateNotificationMessage(id1, notification.Pass());
n1 = GetNotification(id1);
EXPECT_TRUE(n1 == NULL);
const Notification* nr = GetNotification(replaced);
EXPECT_TRUE(nr->shown_as_popup());
EXPECT_TRUE(nr->IsRead());
}
TEST_F(NotificationListTest, QuietMode) {
notification_list()->SetQuietMode(true);
AddNotification();
AddPriorityNotification(HIGH_PRIORITY);
AddPriorityNotification(MAX_PRIORITY);
EXPECT_EQ(3u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(0u, GetPopupCounts());
notification_list()->SetQuietMode(false);
AddNotification();
EXPECT_EQ(4u, notification_list()->NotificationCount(blockers()));
EXPECT_EQ(1u, GetPopupCounts());
// TODO(mukai): Add test of quiet mode with expiration.
}
// Verifies that unread_count doesn't become negative.
TEST_F(NotificationListTest, UnreadCountNoNegative) {
std::string id = AddNotification();
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
notification_list()->MarkSinglePopupAsDisplayed(id);
EXPECT_EQ(0u, notification_list()->UnreadCount(blockers()));
notification_list()->MarkSinglePopupAsShown(
id, false /* mark_notification_as_read */);
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
// Updates the notification and verifies unread_count doesn't change.
scoped_ptr<Notification> updated_notification(new Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
id,
UTF8ToUTF16("updated"),
UTF8ToUTF16("updated"),
gfx::Image(),
base::string16(),
NotifierId(),
RichNotificationData(),
NULL));
notification_list()->AddNotification(updated_notification.Pass());
EXPECT_EQ(1u, notification_list()->UnreadCount(blockers()));
}
TEST_F(NotificationListTest, TestPushingShownNotification) {
// Create a notification and mark it as shown.
std::string id1;
scoped_ptr<Notification> notification(MakeNotification(&id1));
notification->set_shown_as_popup(true);
// Call PushNotification on this notification.
notification_list()->PushNotification(notification.Pass());
// Ensure it is still marked as shown.
EXPECT_TRUE(GetNotification(id1)->shown_as_popup());
}
TEST_F(NotificationListTest, TestHasNotificationOfType) {
std::string id = AddNotification();
EXPECT_TRUE(notification_list()->HasNotificationOfType(
id, message_center::NOTIFICATION_TYPE_SIMPLE));
EXPECT_FALSE(notification_list()->HasNotificationOfType(
id, message_center::NOTIFICATION_TYPE_PROGRESS));
scoped_ptr<Notification> updated_notification(new Notification(
message_center::NOTIFICATION_TYPE_PROGRESS,
id,
UTF8ToUTF16("updated"),
UTF8ToUTF16("updated"),
gfx::Image(),
base::string16(),
NotifierId(),
RichNotificationData(),
NULL));
notification_list()->AddNotification(updated_notification.Pass());
EXPECT_FALSE(notification_list()->HasNotificationOfType(
id, message_center::NOTIFICATION_TYPE_SIMPLE));
EXPECT_TRUE(notification_list()->HasNotificationOfType(
id, message_center::NOTIFICATION_TYPE_PROGRESS));
}
} // namespace message_center
|