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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/scheduler/internal/scheduled_notification_manager.h"
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "base/uuid.h"
#include "chrome/browser/notifications/scheduler/internal/collection_store.h"
#include "chrome/browser/notifications/scheduler/internal/icon_store.h"
#include "chrome/browser/notifications/scheduler/internal/notification_entry.h"
#include "chrome/browser/notifications/scheduler/internal/scheduler_config.h"
#include "chrome/browser/notifications/scheduler/public/notification_params.h"
#include "chrome/browser/notifications/scheduler/public/notification_scheduler_constant.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using ::testing::Invoke;
using Entries = std::vector<std::unique_ptr<notifications::NotificationEntry>>;
namespace notifications {
namespace {
const char kGuid[] = "test_guid_1234";
const char kNonExistentGuid[] = "guid_non_existent";
const char16_t kTitle[] = u"test_title";
const char kSmallIconUuid[] = "test_small_icon_uuid";
const char kLargeIconUuid[] = "test_large_icon_uuid";
NotificationEntry CreateNotificationEntry(SchedulerClientType type) {
NotificationEntry entry(type,
base::Uuid::GenerateRandomV4().AsLowercaseString());
entry.schedule_params.deliver_time_start = base::Time::Now() + base::Days(1);
entry.schedule_params.deliver_time_end = base::Time::Now() + base::Days(2);
return entry;
}
// Verifies notification entry is same as expected.
void VerifyNotificationEntry(const NotificationEntry* entry,
const NotificationEntry* expected) {
if (expected == nullptr) {
EXPECT_FALSE(entry);
return;
}
EXPECT_TRUE(entry);
EXPECT_EQ(*entry, *expected);
const auto& entry_icons = entry->notification_data.icons;
const auto& expected_icons = expected->notification_data.icons;
for (const auto& icon : entry_icons) {
auto icon_type = icon.first;
EXPECT_TRUE(base::Contains(expected_icons, icon_type));
EXPECT_EQ(entry_icons.at(icon_type).bitmap.width(),
expected_icons.at(icon_type).bitmap.width());
EXPECT_EQ(entry_icons.at(icon_type).bitmap.height(),
expected_icons.at(icon_type).bitmap.height());
}
}
IconStore::IconTypeBundleMap CreateIcons() {
IconStore::IconTypeBundleMap result;
SkBitmap large_icon;
large_icon.allocN32Pixels(12, 13);
SkBitmap small_icon;
large_icon.allocN32Pixels(3, 4);
result.emplace(IconType::kLargeIcon, IconBundle(std::move(large_icon)));
result.emplace(IconType::kSmallIcon, IconBundle(std::move(small_icon)));
return result;
}
class MockNotificationStore : public CollectionStore<NotificationEntry> {
public:
MockNotificationStore() = default;
MockNotificationStore(const MockNotificationStore&) = delete;
MockNotificationStore& operator=(const MockNotificationStore&) = delete;
MOCK_METHOD1(InitAndLoad,
void(CollectionStore<NotificationEntry>::LoadCallback));
MOCK_METHOD3(Add,
void(const std::string&,
const NotificationEntry&,
base::OnceCallback<void(bool)>));
MOCK_METHOD3(Update,
void(const std::string&,
const NotificationEntry&,
base::OnceCallback<void(bool)>));
MOCK_METHOD2(Delete,
void(const std::string&, base::OnceCallback<void(bool)>));
};
class MockIconStore : public IconStore {
public:
MockIconStore() = default;
MockIconStore(const MockIconStore&) = delete;
MockIconStore& operator=(const MockIconStore&) = delete;
MOCK_METHOD1(InitAndLoadKeys, void(IconStore::InitAndLoadKeysCallback));
MOCK_METHOD2(LoadIcons,
void(const std::vector<std::string>&,
IconStore::LoadIconsCallback));
MOCK_METHOD2(AddIcons,
void(IconStore::IconTypeBundleMap, IconStore::AddCallback));
MOCK_METHOD2(DeleteIcons,
void(const std::vector<std::string>&,
IconStore::UpdateCallback));
};
class ScheduledNotificationManagerTest : public testing::Test {
public:
ScheduledNotificationManagerTest()
: notification_store_(nullptr), icon_store_(nullptr) {}
ScheduledNotificationManagerTest(const ScheduledNotificationManagerTest&) =
delete;
ScheduledNotificationManagerTest& operator=(
const ScheduledNotificationManagerTest&) = delete;
~ScheduledNotificationManagerTest() override = default;
void SetUp() override {
auto notification_store = std::make_unique<MockNotificationStore>();
auto icon_store = std::make_unique<MockIconStore>();
notification_store_ = notification_store.get();
icon_store_ = icon_store.get();
config_.notification_expiration = base::Days(1);
manager_ = ScheduledNotificationManager::Create(
std::move(notification_store), std::move(icon_store),
{SchedulerClientType::kTest1, SchedulerClientType::kTest2}, config_);
}
protected:
ScheduledNotificationManager* manager() { return manager_.get(); }
MockNotificationStore* notification_store() { return notification_store_; }
MockIconStore* icon_store() { return icon_store_; }
const SchedulerConfig& config() const { return config_; }
// Initializes the manager with predefined data in the store.
void InitWithData(std::vector<NotificationEntry> data) {
Entries entries;
auto icon_keys = std::make_unique<std::vector<std::string>>();
for (auto it = data.begin(); it != data.end(); ++it) {
for (const auto& pair : it->icons_uuid) {
icon_keys->emplace_back(pair.second);
}
auto entry_ptr = std::make_unique<NotificationEntry>(it->type, it->guid);
*(entry_ptr.get()) = *it;
entries.emplace_back(std::move(entry_ptr));
}
// Initialize the store and call the callback.
EXPECT_CALL(*notification_store(), InitAndLoad(_))
.WillOnce(
Invoke([&entries](base::OnceCallback<void(bool, Entries)> cb) {
std::move(cb).Run(true, std::move(entries));
}));
EXPECT_CALL(*icon_store(), InitAndLoadKeys(_))
.WillOnce(Invoke(
[&icon_keys](
base::OnceCallback<void(bool, IconStore::LoadedIconKeys)> cb) {
std::move(cb).Run(true, std::move(icon_keys));
}));
EXPECT_CALL(*icon_store(), DeleteIcons(_, _)).RetiresOnSaturation();
base::RunLoop loop;
manager()->Init(base::BindOnce(
[](base::RepeatingClosure closure, bool success) {
EXPECT_TRUE(success);
std::move(closure).Run();
},
loop.QuitClosure()));
loop.Run();
}
// Schedules a notification and wait until the callback is called.
void ScheduleNotification(std::unique_ptr<NotificationParams> params,
bool expected_success) {
base::RunLoop loop;
auto schedule_callback = base::BindOnce(
[](base::RepeatingClosure quit_closure, bool expected_success,
bool success) {
EXPECT_EQ(success, expected_success);
quit_closure.Run();
},
loop.QuitClosure(), expected_success);
manager()->ScheduleNotification(std::move(params),
std::move(schedule_callback));
loop.Run();
}
void DisplayNotification(const std::string& guid,
const NotificationEntry* expected_entry) {
base::RunLoop loop;
auto display_callback = base::BindOnce(
[](base::RepeatingClosure quit_closure,
const NotificationEntry* expected_entry,
std::unique_ptr<NotificationEntry> entry) {
VerifyNotificationEntry(entry.get(), expected_entry);
quit_closure.Run();
},
loop.QuitClosure(), expected_entry);
manager()->DisplayNotification(guid, std::move(display_callback));
loop.Run();
}
private:
base::test::TaskEnvironment task_environment_;
raw_ptr<MockNotificationStore, DanglingUntriaged> notification_store_;
raw_ptr<MockIconStore, DanglingUntriaged> icon_store_;
std::vector<SchedulerClientType> clients_;
std::unique_ptr<ScheduledNotificationManager> manager_;
SchedulerConfig config_;
};
// Verify that error is received when notification database failed to
// initialize.
TEST_F(ScheduledNotificationManagerTest, NotificationDbInitFailed) {
EXPECT_CALL(*notification_store(), InitAndLoad(_))
.WillOnce(Invoke([](base::OnceCallback<void(bool, Entries)> callback) {
std::move(callback).Run(false, Entries());
}));
EXPECT_CALL(*icon_store(), InitAndLoadKeys(_))
.WillOnce(Invoke(
[](base::OnceCallback<void(bool, IconStore::LoadedIconKeys)> cb) {
std::move(cb).Run(true, nullptr);
}));
base::RunLoop loop;
manager()->Init(base::BindOnce(
[](base::RepeatingClosure closure, bool success) {
// Expected to receive error.
EXPECT_FALSE(success);
std::move(closure).Run();
},
loop.QuitClosure()));
loop.Run();
}
// Verify that error is received when icon database failed to initialize.
TEST_F(ScheduledNotificationManagerTest, IconDbInitFailed) {
ON_CALL(*notification_store(), InitAndLoad(_))
.WillByDefault(
Invoke([](base::OnceCallback<void(bool, Entries)> callback) {
std::move(callback).Run(true, Entries());
}));
EXPECT_CALL(*icon_store(), InitAndLoadKeys(_))
.WillOnce(Invoke(
[](base::OnceCallback<void(bool, IconStore::LoadedIconKeys)> cb) {
std::move(cb).Run(false, nullptr);
}));
base::RunLoop loop;
manager()->Init(base::BindOnce(
[](base::RepeatingClosure closure, bool success) {
// Expected to receive error.
EXPECT_FALSE(success);
std::move(closure).Run();
},
loop.QuitClosure()));
loop.Run();
}
// Verify that residual expired icons are deleted when icon database is
// initialized.
TEST_F(ScheduledNotificationManagerTest, IconDbInitAndLoadKeys) {
EXPECT_CALL(*notification_store(), InitAndLoad(_))
.WillOnce(Invoke([](base::OnceCallback<void(bool, Entries)> callback) {
std::move(callback).Run(true, Entries());
}));
auto icon_keys = std::make_unique<std::vector<std::string>>();
auto* icon_keys_ptr = icon_keys.get();
icon_keys->emplace_back(kSmallIconUuid);
icon_keys->emplace_back(kLargeIconUuid);
EXPECT_CALL(*icon_store(), InitAndLoadKeys(_))
.WillOnce(Invoke(
[&icon_keys](
base::OnceCallback<void(bool, IconStore::LoadedIconKeys)> cb) {
std::move(cb).Run(true, std::move(icon_keys));
}));
EXPECT_CALL(*icon_store(), DeleteIcons(*icon_keys_ptr, _));
base::RunLoop loop;
manager()->Init(base::BindOnce(
[](base::RepeatingClosure closure, bool success) {
EXPECT_TRUE(success);
std::move(closure).Run();
},
loop.QuitClosure()));
loop.Run();
}
// Test to schedule a notification.
TEST_F(ScheduledNotificationManagerTest, ScheduleNotification) {
InitWithData(std::vector<NotificationEntry>());
NotificationData notification_data;
notification_data.title = kTitle;
ScheduleParams schedule_params;
schedule_params.priority = ScheduleParams::Priority::kLow;
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, notification_data, schedule_params);
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
params->enable_ihnr_buttons = true;
std::string guid = params->guid;
EXPECT_FALSE(guid.empty());
// Verify call contract.
EXPECT_CALL(*icon_store(), AddIcons(_, _))
.WillOnce(Invoke([](IconStore::IconTypeBundleMap icons,
IconStore::AddCallback callback) {
std::move(callback).Run(IconStore::IconTypeUuidMap{}, true);
}));
EXPECT_CALL(*notification_store(), Add(guid, _, _))
.WillOnce(Invoke([guid](const std::string&, const NotificationEntry&,
base::OnceCallback<void(bool)> cb) {
std::move(cb).Run(true);
}));
ScheduleNotification(std::move(params), true /*expected_success*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
const NotificationEntry* entry = *(notifications.begin()->second.begin());
EXPECT_EQ(entry->guid, guid);
EXPECT_NE(entry->create_time, base::Time());
// TODO(xingliu): change these to compare with operator==.
EXPECT_EQ(entry->notification_data.title, kTitle);
EXPECT_EQ(entry->schedule_params.priority, ScheduleParams::Priority::kLow);
// Verify that |enable_ihnr_buttons| will add the helpful/unhelpful buttons.
auto buttons = entry->notification_data.buttons;
EXPECT_EQ(buttons.size(), 2u);
EXPECT_EQ(buttons[0].id, notifications::kDefaultHelpfulButtonId);
EXPECT_EQ(buttons[0].type, ActionButtonType::kHelpful);
EXPECT_EQ(buttons[1].id, notifications::kDefaultUnhelpfulButtonId);
EXPECT_EQ(buttons[1].type, ActionButtonType::kUnhelpful);
}
// Verifies that schedules a notification with unregistered client will fail.
TEST_F(ScheduledNotificationManagerTest, ScheduleInvalidNotification) {
InitWithData(std::vector<NotificationEntry>());
NotificationData notification_data;
notification_data.title = kTitle;
ScheduleParams schedule_params;
// Client type kTest3 is not registered.
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest3, notification_data, schedule_params);
EXPECT_CALL(*icon_store(), AddIcons(_, _)).Times(0);
EXPECT_CALL(*notification_store(), Add(_, _, _)).Times(0);
ScheduleNotification(std::move(params), false /*expected_success*/);
}
// Verifies that schedules a notification with duplicate guid will fail.
TEST_F(ScheduledNotificationManagerTest, ScheduleNotificationDuplicateGuid) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
entry.guid = kGuid;
InitWithData(std::vector<NotificationEntry>({entry}));
NotificationData notification_data;
notification_data.title = kTitle;
ScheduleParams schedule_params;
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, notification_data, schedule_params);
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
// Duplicate guid.
params->guid = kGuid;
EXPECT_CALL(*icon_store(), AddIcons(_, _)).Times(0);
EXPECT_CALL(*notification_store(), Add(_, _, _)).Times(0);
ScheduleNotification(std::move(params), false /*expected_success*/);
}
// Test to schedule a notification without guid, we will auto generated one.
TEST_F(ScheduledNotificationManagerTest, ScheduleNotificationEmptyGuid) {
InitWithData(std::vector<NotificationEntry>());
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, NotificationData(), ScheduleParams());
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
// Verify call contract.
EXPECT_CALL(*icon_store(), AddIcons(_, _))
.WillOnce(Invoke([](IconStore::IconTypeBundleMap icons,
IconStore::AddCallback callback) {
std::move(callback).Run(IconStore::IconTypeUuidMap{}, true);
}));
EXPECT_CALL(*notification_store(), Add(_, _, _))
.WillOnce(Invoke(
[&](const std::string&, const NotificationEntry&,
base::OnceCallback<void(bool)> cb) { std::move(cb).Run(true); }));
ScheduleNotification(std::move(params), true /*expected_success*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
const NotificationEntry* entry = *(notifications.begin()->second.begin());
EXPECT_NE(entry->guid, std::string());
EXPECT_NE(entry->create_time, base::Time());
}
// Test to display a notification.
TEST_F(ScheduledNotificationManagerTest, DisplayNotification) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
entry.guid = kGuid;
InitWithData(std::vector<NotificationEntry>({entry}));
EXPECT_CALL(*icon_store(), LoadIcons(_, _))
.WillOnce(Invoke([](std::vector<std::string> keys,
IconStore::LoadIconsCallback callback) {
std::move(callback).Run(true, IconStore::LoadedIconsMap());
}));
EXPECT_CALL(*notification_store(), Delete(kGuid, _));
EXPECT_CALL(*icon_store(), DeleteIcons(_, _));
DisplayNotification(kGuid, &entry);
// Before display, the notification is removed from the store.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_TRUE(notifications.empty());
}
// Test to display non-existing notification.
TEST_F(ScheduledNotificationManagerTest, DisplayNotificationWithoutEntry) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
entry.guid = kGuid;
InitWithData(std::vector<NotificationEntry>({entry}));
EXPECT_CALL(*icon_store(), LoadIcons(_, _)).Times(0);
EXPECT_CALL(*notification_store(), Delete(_, _)).Times(0);
EXPECT_CALL(*icon_store(), DeleteIcons(_, _)).Times(0);
DisplayNotification(kNonExistentGuid, nullptr);
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(*notifications.at(SchedulerClientType::kTest1).back(), entry);
}
// Verify GetAllNotifications API, the notification should be sorted based on
// creation timestamp.
TEST_F(ScheduledNotificationManagerTest, GetAllNotifications) {
// Ordering: entry1.create_time < entry0.create_time < entry2.create_time.
auto now = base::Time::Now();
auto entry0 = CreateNotificationEntry(SchedulerClientType::kTest1);
entry0.create_time = now;
auto entry1 = CreateNotificationEntry(SchedulerClientType::kTest1);
entry1.create_time = now - base::Minutes(1);
auto entry2 = CreateNotificationEntry(SchedulerClientType::kTest1);
entry2.create_time = now + base::Minutes(1);
InitWithData(std::vector<NotificationEntry>({entry0, entry1, entry2}));
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
EXPECT_EQ(notifications.begin()->second.size(), 3u);
// Entries returned by GetAllNotifications() should be sorted by creation
// timestamp.
const NotificationEntry* output0 = notifications.begin()->second.front();
const NotificationEntry* output2 = notifications.begin()->second.back();
EXPECT_EQ(output0->create_time, entry1.create_time);
EXPECT_EQ(output2->create_time, entry2.create_time);
}
// Verifies GetNotifications() can return the correct values.
TEST_F(ScheduledNotificationManagerTest, GetNotifications) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
InitWithData(std::vector<NotificationEntry>({entry}));
std::vector<const NotificationEntry*> entries;
manager()->GetNotifications(SchedulerClientType::kTest2, &entries);
EXPECT_TRUE(entries.empty());
manager()->GetNotifications(SchedulerClientType::kTest1, &entries);
EXPECT_EQ(entries.size(), 1u);
EXPECT_EQ(*entries.back(), entry);
manager()->GetNotifications(SchedulerClientType::kTest2, &entries);
EXPECT_TRUE(entries.empty());
}
// Verify DeleteNotifications API, all notifications with given
// SchedulerClientType should be deleted.
TEST_F(ScheduledNotificationManagerTest, DeleteNotifications) {
// Type1: entry0
// Type2: entry1, entry2
auto entry0 = CreateNotificationEntry(SchedulerClientType::kTest1);
auto entry1 = CreateNotificationEntry(SchedulerClientType::kTest2);
auto entry2 = CreateNotificationEntry(SchedulerClientType::kTest2);
InitWithData(std::vector<NotificationEntry>({entry0, entry1, entry2}));
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 2u);
EXPECT_CALL(*notification_store(), Delete(_, _))
.Times(2)
.RetiresOnSaturation();
EXPECT_CALL(*icon_store(), DeleteIcons(_, _)).Times(2).RetiresOnSaturation();
manager()->DeleteNotifications(SchedulerClientType::kTest2);
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
// Ensure deleting non-existing key will not crash, and store will not call
// Delete.
EXPECT_CALL(*notification_store(), Delete(_, _))
.Times(0)
.RetiresOnSaturation();
manager()->DeleteNotifications(SchedulerClientType::kTest2);
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
EXPECT_CALL(*notification_store(), Delete(_, _)).RetiresOnSaturation();
EXPECT_CALL(*icon_store(), DeleteIcons(_, _)).RetiresOnSaturation();
manager()->DeleteNotifications(SchedulerClientType::kTest1);
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 0u);
// Unregistered client.
EXPECT_CALL(*notification_store(), Delete(_, _)).Times(0);
manager()->DeleteNotifications(SchedulerClientType::kTest3);
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 0u);
}
// Verifies that unused notifications will be deleted during initialization.
TEST_F(ScheduledNotificationManagerTest, PruneNotifications) {
// Type1: entry0
// Type2: entry1(invalid), entry2(expired), entry3
// Type3: entry4(unregistered client)
auto now = base::Time::Now();
auto entry0 = CreateNotificationEntry(SchedulerClientType::kTest1);
entry0.create_time = now - base::Hours(12);
auto entry1 = CreateNotificationEntry(SchedulerClientType::kTest2);
entry1.create_time = now - base::Hours(14);
entry1.schedule_params.deliver_time_start = base::Time::Now() - base::Days(2);
entry1.schedule_params.deliver_time_end = base::Time::Now() - base::Days(1);
auto entry2 = CreateNotificationEntry(SchedulerClientType::kTest2);
entry2.create_time = now - base::Hours(24);
auto entry3 = CreateNotificationEntry(SchedulerClientType::kTest2);
entry3.create_time = now - base::Hours(23);
auto entry4 = CreateNotificationEntry(SchedulerClientType::kTest3);
EXPECT_CALL(*notification_store(), Delete(_, _)).Times(3);
EXPECT_CALL(*icon_store(), DeleteIcons(_, _)).Times(3).RetiresOnSaturation();
InitWithData(
std::vector<NotificationEntry>({entry0, entry1, entry2, entry3, entry4}));
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 2u);
EXPECT_EQ(notifications[SchedulerClientType::kTest1].size(), 1u);
EXPECT_EQ(notifications[SchedulerClientType::kTest2].size(), 1u);
}
// Test to schedule a notification with two icons in notification data.
TEST_F(ScheduledNotificationManagerTest, ScheduleNotificationWithIcons) {
InitWithData(std::vector<NotificationEntry>());
NotificationData notification_data;
notification_data.icons = CreateIcons();
ScheduleParams schedule_params;
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, notification_data, schedule_params);
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
std::string guid = params->guid;
EXPECT_FALSE(guid.empty());
// Verify call contract.
EXPECT_CALL(*icon_store(), AddIcons(_, _))
.WillOnce(Invoke([](IconStore::IconTypeBundleMap icons,
IconStore::AddCallback callback) {
IconStore::IconTypeUuidMap icons_uuid_map;
for (const auto& pair : icons) {
if (pair.first == IconType::kLargeIcon)
icons_uuid_map.emplace(pair.first, kLargeIconUuid);
else if (pair.first == IconType::kSmallIcon)
icons_uuid_map.emplace(pair.first, kSmallIconUuid);
}
std::move(callback).Run(std::move(icons_uuid_map), true);
}));
EXPECT_CALL(*notification_store(), Add(guid, _, _))
.WillOnce(Invoke([guid](const std::string&, const NotificationEntry&,
base::OnceCallback<void(bool)> cb) {
std::move(cb).Run(true);
}));
ScheduleNotification(std::move(params), true /*expected_success*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_EQ(notifications.size(), 1u);
const NotificationEntry* entry = *(notifications.begin()->second.begin());
EXPECT_EQ(entry->guid, guid);
EXPECT_NE(entry->create_time, base::Time());
EXPECT_EQ(entry->icons_uuid.size(), 2u);
EXPECT_EQ(entry->icons_uuid.at(IconType::kLargeIcon), kLargeIconUuid);
EXPECT_EQ(entry->icons_uuid.at(IconType::kSmallIcon), kSmallIconUuid);
}
// Test to schedule a notification failed on saving icons.
TEST_F(ScheduledNotificationManagerTest, ScheduleNotificationWithIconsFailed) {
InitWithData(std::vector<NotificationEntry>());
NotificationData notification_data;
notification_data.icons = CreateIcons();
ScheduleParams schedule_params;
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, notification_data, schedule_params);
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
// Verify call contract.
EXPECT_CALL(*icon_store(), AddIcons(_, _))
.WillOnce(Invoke([](IconStore::IconTypeBundleMap icons,
IconStore::AddCallback callback) {
IconStore::IconTypeUuidMap icons_uuid_map;
for (const auto& pair : icons) {
if (pair.first == IconType::kLargeIcon)
icons_uuid_map.emplace(pair.first, kLargeIconUuid);
else if (pair.first == IconType::kSmallIcon)
icons_uuid_map.emplace(pair.first, kSmallIconUuid);
}
std::move(callback).Run(std::move(icons_uuid_map), false);
}));
ScheduleNotification(std::move(params), false /*expected_success*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_TRUE(notifications.empty());
}
// Verifies the case that failed to add to the notification store.
TEST_F(ScheduledNotificationManagerTest, ScheduleAddNotificationFailed) {
InitWithData(std::vector<NotificationEntry>());
NotificationData notification_data;
notification_data.icons = CreateIcons();
ScheduleParams schedule_params;
auto params = std::make_unique<NotificationParams>(
SchedulerClientType::kTest1, notification_data, schedule_params);
params->schedule_params.deliver_time_start = base::Time::Now();
params->schedule_params.deliver_time_end = base::Time::Now() + base::Days(1);
// Succeeded to add icons.
EXPECT_CALL(*icon_store(), AddIcons(_, _))
.WillOnce(Invoke([](IconStore::IconTypeBundleMap icons,
IconStore::AddCallback callback) {
IconStore::IconTypeUuidMap icons_uuid_map;
for (const auto& pair : icons) {
if (pair.first == IconType::kLargeIcon)
icons_uuid_map.emplace(pair.first, kLargeIconUuid);
else if (pair.first == IconType::kSmallIcon)
icons_uuid_map.emplace(pair.first, kSmallIconUuid);
}
std::move(callback).Run(std::move(icons_uuid_map), true);
}));
std::vector<std::string> icons_to_delete{kSmallIconUuid, kLargeIconUuid};
EXPECT_CALL(*icon_store(), DeleteIcons(icons_to_delete, _));
// Failed to add notifications.
EXPECT_CALL(*notification_store(), Add(_, _, _))
.WillOnce(Invoke(
[](const std::string&, const NotificationEntry&,
base::OnceCallback<void(bool)> cb) { std::move(cb).Run(false); }));
ScheduleNotification(std::move(params), false /*expected_success*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_TRUE(notifications.empty());
}
// Test to display a notification with icons.
TEST_F(ScheduledNotificationManagerTest, DisplayNotificationWithIcons) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
entry.guid = kGuid;
entry.icons_uuid[IconType::kLargeIcon] = kLargeIconUuid;
entry.icons_uuid[IconType::kSmallIcon] = kSmallIconUuid;
InitWithData(std::vector<NotificationEntry>({entry}));
auto icons = CreateIcons();
EXPECT_CALL(*icon_store(), LoadIcons(_, _))
.WillOnce(Invoke([&icons](std::vector<std::string> keys,
IconStore::LoadIconsCallback callback) {
IconStore::LoadedIconsMap result;
result.emplace(kSmallIconUuid, icons.at(IconType::kSmallIcon));
result.emplace(kLargeIconUuid, icons.at(IconType::kLargeIcon));
std::move(callback).Run(true, std::move(result));
}));
EXPECT_CALL(*notification_store(), Delete(kGuid, _));
EXPECT_CALL(*icon_store(), DeleteIcons(_, _));
auto expected_entry(entry);
expected_entry.notification_data.icons = icons;
DisplayNotification(kGuid, &expected_entry);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_TRUE(notifications.empty());
}
// Test to display a notification with icons loaded failed.
TEST_F(ScheduledNotificationManagerTest, DisplayNotificationWithIconsFailed) {
auto entry = CreateNotificationEntry(SchedulerClientType::kTest1);
entry.guid = kGuid;
entry.icons_uuid[IconType::kLargeIcon] = kLargeIconUuid;
entry.icons_uuid[IconType::kSmallIcon] = kSmallIconUuid;
InitWithData(std::vector<NotificationEntry>({entry}));
EXPECT_CALL(*icon_store(), LoadIcons(_, _))
.WillOnce(Invoke([](std::vector<std::string> keys,
IconStore::LoadIconsCallback callback) {
std::move(callback).Run(false, IconStore::LoadedIconsMap{});
}));
EXPECT_CALL(*notification_store(), Delete(kGuid, _));
EXPECT_CALL(*icon_store(), DeleteIcons(_, _));
DisplayNotification(kGuid, nullptr /*expected_entry*/);
// Verify in-memory data.
ScheduledNotificationManager::Notifications notifications;
manager()->GetAllNotifications(¬ifications);
EXPECT_TRUE(notifications.empty());
}
} // namespace
} // namespace notifications
|