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
|
// 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/notification_store.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/browser/notifications/scheduler/internal/proto_conversion.h"
#include "chrome/browser/notifications/scheduler/test/test_utils.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/testing/fake_db.h"
#include "testing/gtest/include/gtest/gtest.h"
using leveldb_proto::test::FakeDB;
using InitStatus = leveldb_proto::Enums::InitStatus;
using Entries = notifications::NotificationStore::Entries;
using TestNotificationEntries =
std::map<std::string, notifications::NotificationEntry>;
using DbEntries = std::vector<notifications::NotificationEntry>;
using DbEntriesPtr =
std::unique_ptr<std::vector<notifications::NotificationEntry>>;
namespace notifications {
namespace {
const char kGuid[] = "1234";
class NotificationStoreTest : public testing::Test {
public:
NotificationStoreTest() : load_result_(false) {}
NotificationStoreTest(const NotificationStoreTest&) = delete;
NotificationStoreTest& operator=(const NotificationStoreTest&) = delete;
~NotificationStoreTest() override = default;
void SetUp() override {}
protected:
void Init(const TestNotificationEntries& test_data, InitStatus status) {
CreateTestProtos(test_data);
auto db =
std::make_unique<FakeDB<proto::NotificationEntry, NotificationEntry>>(
&db_protos_);
db_ = db.get();
store_ = std::make_unique<NotificationStore>(std::move(db));
store_->InitAndLoad(base::BindOnce(&NotificationStoreTest::OnEntriesLoaded,
base::Unretained(this)));
db_->InitStatusCallback(status);
}
bool load_result() const { return load_result_; }
const Entries& loaded_entries() const { return loaded_entries_; }
FakeDB<proto::NotificationEntry, NotificationEntry>* db() { return db_; }
CollectionStore<NotificationEntry>* store() { return store_.get(); }
void VerifyDataInDb(DbEntriesPtr expected) {
db_->LoadEntries(base::BindOnce(
[](DbEntriesPtr expected, bool success, DbEntriesPtr entries) {
EXPECT_TRUE(success);
DCHECK(entries);
DCHECK(expected);
EXPECT_EQ(entries->size(), expected->size());
for (size_t i = 0, size = entries->size(); i < size; ++i) {
auto& entry = (*entries)[i];
auto& expected_entry = (*expected)[i];
EXPECT_EQ(entry, expected_entry)
<< " \n Output: " << test::DebugString(&entry)
<< " \n Expected: " << test::DebugString(&expected_entry);
}
},
std::move(expected)));
db_->LoadCallback(true);
}
private:
// Push data into |db_|.
void CreateTestProtos(const TestNotificationEntries& test_data) {
for (const auto& pair : test_data) {
const auto& key = pair.first;
auto entry = pair.second;
proto::NotificationEntry proto;
NotificationEntryToProto(&entry, &proto);
db_protos_.emplace(key, proto);
}
}
void OnEntriesLoaded(bool success, Entries entries) {
load_result_ = success;
loaded_entries_ = std::move(entries);
}
base::test::TaskEnvironment task_environment_;
// Database test objects.
raw_ptr<FakeDB<proto::NotificationEntry, NotificationEntry>,
DanglingUntriaged>
db_;
std::map<std::string, proto::NotificationEntry> db_protos_;
std::unique_ptr<CollectionStore<NotificationEntry>> store_;
Entries loaded_entries_;
bool load_result_;
};
// Verifies initialization with empty database.
TEST_F(NotificationStoreTest, Init) {
Init(TestNotificationEntries(), InitStatus::kOK);
db()->LoadCallback(true);
EXPECT_EQ(load_result(), true);
EXPECT_TRUE(loaded_entries().empty());
}
// Initialize non-empty database should success.
TEST_F(NotificationStoreTest, InitSuccessWithData) {
auto test_data = TestNotificationEntries();
NotificationEntry entry(SchedulerClientType::kTest2, kGuid);
bool success =
base::Time::FromString("04/25/20 01:00:00 AM", &entry.create_time);
DCHECK(success);
test_data.emplace(kGuid, entry);
Init(test_data, InitStatus::kOK);
db()->LoadCallback(true);
EXPECT_EQ(load_result(), true);
EXPECT_EQ(loaded_entries().size(), 1u);
EXPECT_EQ(*loaded_entries().back(), entry);
}
// Failed database initialization will result in error.
TEST_F(NotificationStoreTest, InitFailed) {
Init(TestNotificationEntries(), InitStatus::kCorrupt);
EXPECT_EQ(load_result(), false);
EXPECT_TRUE(loaded_entries().empty());
}
TEST_F(NotificationStoreTest, AddAndUpdate) {
Init(TestNotificationEntries(), InitStatus::kOK);
db()->LoadCallback(true);
EXPECT_EQ(load_result(), true);
EXPECT_TRUE(loaded_entries().empty());
NotificationEntry entry(SchedulerClientType::kTest2, kGuid);
bool success =
base::Time::FromString("04/25/20 01:00:00 AM", &entry.create_time);
DCHECK(success);
// Add data to the store and verify the database.
store()->Add(kGuid, entry,
base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
db()->UpdateCallback(true);
auto expected = std::make_unique<DbEntries>();
expected->emplace_back(entry);
VerifyDataInDb(std::move(expected));
// Update and verified the new data.
entry.notification_data.title = u"test_title";
expected = std::make_unique<DbEntries>();
expected->emplace_back(entry);
store()->Update(kGuid, entry,
base::BindOnce([](bool success) { EXPECT_TRUE(success); }));
db()->UpdateCallback(true);
VerifyDataInDb(std::move(expected));
}
TEST_F(NotificationStoreTest, Delete) {
auto test_data = TestNotificationEntries();
NotificationEntry entry(SchedulerClientType::kTest2, kGuid);
test_data.emplace(kGuid, entry);
Init(test_data, InitStatus::kOK);
db()->LoadCallback(true);
EXPECT_EQ(loaded_entries().size(), 1u);
// Delete the entry and verify data is deleted.
store()->Delete(kGuid, base::DoNothing());
db()->UpdateCallback(true);
VerifyDataInDb(std::make_unique<DbEntries>());
}
} // namespace
} // namespace notifications
|