Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: components/feature_engagement_tracker/internal/persistent_store_unittest.cc

Issue 2876633002: Add a PersistentStore to FeatureEngagementTracker (Closed)
Patch Set: Fix deps Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/feature_engagement_tracker/internal/persistent_store.h"
6
7 #include <map>
8
9 #include "base/files/file_path.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/optional.h"
12 #include "components/feature_engagement_tracker/internal/proto/event.pb.h"
13 #include "components/feature_engagement_tracker/internal/test/event_util.h"
14 #include "components/leveldb_proto/proto_database.h"
15 #include "components/leveldb_proto/testing/fake_db.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace feature_engagement_tracker {
19
20 namespace {
21
22 void VerifyEventsInListAndMap(const std::map<std::string, Event>& map,
23 const std::vector<Event>& list) {
24 ASSERT_EQ(map.size(), list.size());
25
26 for (const auto& event : list) {
27 const auto& it = map.find(event.name());
28 ASSERT_NE(map.end(), it);
29 test::VerifyEventsEqual(&event, &it->second);
30 }
31 }
32
33 class PersistentStoreTest : public ::testing::Test {
34 public:
35 PersistentStoreTest()
36 : db_(nullptr),
37 storage_dir_(FILE_PATH_LITERAL("/persistent/store/lalala")) {
38 load_callback_ =
39 base::Bind(&PersistentStoreTest::LoadCallback, base::Unretained(this));
40 }
41
42 void TearDown() override {
43 db_events_.clear();
44 db_ = nullptr;
45 store_.reset();
46 }
47
48 protected:
49 void SetUpDB() {
50 DCHECK(!db_);
51 DCHECK(!store_);
52
53 auto db = base::MakeUnique<leveldb_proto::test::FakeDB<Event>>(&db_events_);
54 db_ = db.get();
55 store_.reset(new PersistentStore(storage_dir_, std::move(db)));
56 }
57
58 void LoadCallback(bool success, std::unique_ptr<std::vector<Event>> events) {
59 load_successful_ = success;
60 load_results_ = std::move(events);
61 }
62
63 // Callback results.
64 base::Optional<bool> load_successful_;
65 std::unique_ptr<std::vector<Event>> load_results_;
66
67 Store::OnLoadedCallback load_callback_;
68 std::map<std::string, Event> db_events_;
69 leveldb_proto::test::FakeDB<Event>* db_;
70 std::unique_ptr<Store> store_;
71
72 // Constant test data.
73 base::FilePath storage_dir_;
74 };
75
76 } // namespace
77
78 TEST_F(PersistentStoreTest, StorageDirectory) {
79 SetUpDB();
80 store_->Load(load_callback_);
81 EXPECT_EQ(storage_dir_, db_->GetDirectory());
82 }
83
84 TEST_F(PersistentStoreTest, SuccessfulInitAndLoadEmptyStore) {
85 SetUpDB();
86
87 store_->Load(load_callback_);
88
89 // The initialize should not trigger a response to the callback.
90 db_->InitCallback(true);
91 EXPECT_FALSE(load_successful_.has_value());
92
93 // The load should trigger a response to the callback.
94 db_->LoadCallback(true);
95 EXPECT_TRUE(load_successful_.value());
96
97 // Validate that we have no entries.
98 EXPECT_NE(nullptr, load_results_);
99 EXPECT_TRUE(load_results_->empty());
100 }
101
102 TEST_F(PersistentStoreTest, SuccessfulInitAndLoadWithEvents) {
103 // Populate fake Event entries.
104 Event event1;
105 event1.set_name("event1");
106 test::SetEventCountForDay(&event1, 1, 1);
107
108 Event event2;
109 event2.set_name("event2");
110 test::SetEventCountForDay(&event2, 1, 3);
111 test::SetEventCountForDay(&event2, 2, 5);
112
113 db_events_.insert(std::pair<std::string, Event>(event1.name(), event1));
114 db_events_.insert(std::pair<std::string, Event>(event2.name(), event2));
115
116 SetUpDB();
117
118 // The initialize should not trigger a response to the callback.
119 store_->Load(load_callback_);
120 db_->InitCallback(true);
121 EXPECT_FALSE(load_successful_.has_value());
122
123 // The load should trigger a response to the callback.
124 db_->LoadCallback(true);
125 EXPECT_TRUE(load_successful_.value());
126 EXPECT_NE(nullptr, load_results_);
127
128 // Validate that we have the two events that we expect.
129 VerifyEventsInListAndMap(db_events_, *load_results_);
130 }
131
132 TEST_F(PersistentStoreTest, SuccessfulInitBadLoad) {
133 SetUpDB();
134
135 store_->Load(load_callback_);
136
137 // The initialize should not trigger a response to the callback.
138 db_->InitCallback(true);
139 EXPECT_FALSE(load_successful_.has_value());
140
141 // The load will fail and should trigger the callback.
142 db_->LoadCallback(false);
143 EXPECT_FALSE(load_successful_.value());
144 EXPECT_FALSE(store_->IsReady());
145 }
146
147 TEST_F(PersistentStoreTest, BadInit) {
148 SetUpDB();
149
150 store_->Load(load_callback_);
151
152 // The initialize will fail and should trigger the callback.
153 db_->InitCallback(false);
154 EXPECT_FALSE(load_successful_.value());
155 EXPECT_FALSE(store_->IsReady());
156 }
157
158 TEST_F(PersistentStoreTest, IsReady) {
159 SetUpDB();
160 EXPECT_FALSE(store_->IsReady());
161
162 store_->Load(load_callback_);
163 EXPECT_FALSE(store_->IsReady());
164
165 db_->InitCallback(true);
166 EXPECT_FALSE(store_->IsReady());
167
168 db_->LoadCallback(true);
169 EXPECT_TRUE(store_->IsReady());
170 }
171
172 TEST_F(PersistentStoreTest, WriteEvent) {
173 SetUpDB();
174
175 store_->Load(load_callback_);
176 db_->InitCallback(true);
177 db_->LoadCallback(true);
178
179 Event event;
180 event.set_name("event");
181 test::SetEventCountForDay(&event, 1, 2);
182
183 store_->WriteEvent(event);
184 db_->UpdateCallback(true);
185
186 EXPECT_EQ(1U, db_events_.size());
187
188 const auto& it = db_events_.find("event");
189 EXPECT_NE(db_events_.end(), it);
190 test::VerifyEventsEqual(&event, &it->second);
191 }
192
193 } // namespace feature_engagement_tracker
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698