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

Side by Side Diff: components/offline_pages/offline_page_metadata_store_impl_unittest.cc

Issue 1834563002: initial add of SQL based storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments. Created 4 years, 9 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/offline_pages/offline_page_metadata_store_impl.h" 5 #include "components/offline_pages/offline_page_metadata_store_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/test_simple_task_runner.h" 14 #include "base/test/test_simple_task_runner.h"
15 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
16 #include "components/leveldb_proto/proto_database_impl.h" 16 #include "components/leveldb_proto/proto_database_impl.h"
17 #include "components/offline_pages/offline_page_item.h" 17 #include "components/offline_pages/offline_page_item.h"
18 #include "components/offline_pages/offline_page_metadata_store_sql.h"
18 #include "components/offline_pages/offline_page_model.h" 19 #include "components/offline_pages/offline_page_model.h"
19 #include "components/offline_pages/proto/offline_pages.pb.h" 20 #include "components/offline_pages/proto/offline_pages.pb.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 using leveldb_proto::ProtoDatabaseImpl; 23 using leveldb_proto::ProtoDatabaseImpl;
23 24
24 namespace offline_pages { 25 namespace offline_pages {
25 26
26 namespace { 27 namespace {
27 28
28 const char kTestURL[] = "https://example.com"; 29 const char kTestURL[] = "https://example.com";
29 const ClientId kTestBookmarkId(BOOKMARK_NAMESPACE, "1234"); 30 const ClientId kTestBookmarkId(BOOKMARK_NAMESPACE, "1234");
30 const ClientId kTestBookmarkId2(BOOKMARK_NAMESPACE, "5678"); 31 const ClientId kTestBookmarkId2(BOOKMARK_NAMESPACE, "5678");
31 const base::FilePath::CharType kFilePath[] = 32 const base::FilePath::CharType kFilePath[] =
32 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml"); 33 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml");
33 int64_t kFileSize = 234567; 34 int64_t kFileSize = 234567;
34 35
35 class OfflinePageMetadataStoreImplTest : public testing::Test { 36 class OfflinePageMetadataStoreFactory {
36 public: 37 public:
37 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY }; 38 virtual OfflinePageMetadataStore* BuildStore(const base::FilePath& file) = 0;
38 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; 39 };
39 40
40 OfflinePageMetadataStoreImplTest(); 41 class OfflinePageMetadataStoreImplFactory
41 ~OfflinePageMetadataStoreImplTest() override; 42 : public OfflinePageMetadataStoreFactory {
43 public:
44 OfflinePageMetadataStore* BuildStore(const base::FilePath& file) {
45 return new OfflinePageMetadataStoreImpl(base::ThreadTaskRunnerHandle::Get(),
46 file);
47 }
48 };
49
50 class OfflinePageMetadataStoreSQLFactory
51 : public OfflinePageMetadataStoreFactory {
52 public:
53 OfflinePageMetadataStore* BuildStore(const base::FilePath& file) {
54 bool use_in_memory = true;
55 return new OfflinePageMetadataStoreSQL(base::ThreadTaskRunnerHandle::Get(),
56 file, use_in_memory);
57 }
58 };
59
60 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY };
61 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE };
62
63 class OfflinePageMetadataStoreTestBase : public testing::Test {
64 public:
65 OfflinePageMetadataStoreTestBase();
66 ~OfflinePageMetadataStoreTestBase() override;
42 67
43 void TearDown() override { 68 void TearDown() override {
44 // Wait for all the pieces of the store to delete itself properly. 69 // Wait for all the pieces of the store to delete itself properly.
45 PumpLoop(); 70 PumpLoop();
46 } 71 }
47 72
48 scoped_ptr<OfflinePageMetadataStoreImpl> BuildStore(); 73 virtual scoped_ptr<OfflinePageMetadataStore> BuildStore() = 0;
49 void PumpLoop(); 74 void PumpLoop();
50 75
51 void LoadCallback(OfflinePageMetadataStore::LoadStatus load_status, 76 void LoadCallback(OfflinePageMetadataStore::LoadStatus load_status,
52 const std::vector<OfflinePageItem>& offline_pages); 77 const std::vector<OfflinePageItem>& offline_pages);
53 void UpdateCallback(CalledCallback called_callback, bool success); 78 void UpdateCallback(CalledCallback called_callback, bool success);
54 79
55 void ClearResults(); 80 void ClearResults();
56 81
57 protected: 82 protected:
58 CalledCallback last_called_callback_; 83 CalledCallback last_called_callback_;
59 Status last_status_; 84 Status last_status_;
60 std::vector<OfflinePageItem> offline_pages_; 85 std::vector<OfflinePageItem> offline_pages_;
61 86
62 base::ScopedTempDir temp_directory_; 87 base::ScopedTempDir temp_directory_;
63 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 88 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
64 base::ThreadTaskRunnerHandle task_runner_handle_; 89 base::ThreadTaskRunnerHandle task_runner_handle_;
65 }; 90 };
66 91
67 OfflinePageMetadataStoreImplTest::OfflinePageMetadataStoreImplTest() 92 OfflinePageMetadataStoreTestBase::OfflinePageMetadataStoreTestBase()
68 : last_called_callback_(NONE), 93 : last_called_callback_(NONE),
69 last_status_(STATUS_NONE), 94 last_status_(STATUS_NONE),
70 task_runner_(new base::TestSimpleTaskRunner), 95 task_runner_(new base::TestSimpleTaskRunner),
71 task_runner_handle_(task_runner_) { 96 task_runner_handle_(task_runner_) {
72 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); 97 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir());
73 } 98 }
74 99
75 OfflinePageMetadataStoreImplTest::~OfflinePageMetadataStoreImplTest() { 100 OfflinePageMetadataStoreTestBase::~OfflinePageMetadataStoreTestBase() {}
76 }
77 101
78 void OfflinePageMetadataStoreImplTest::PumpLoop() { 102 void OfflinePageMetadataStoreTestBase::PumpLoop() {
79 task_runner_->RunUntilIdle(); 103 task_runner_->RunUntilIdle();
80 } 104 }
81 105
82 scoped_ptr<OfflinePageMetadataStoreImpl> 106 void OfflinePageMetadataStoreTestBase::LoadCallback(
83 OfflinePageMetadataStoreImplTest::BuildStore() {
84 scoped_ptr<OfflinePageMetadataStoreImpl> store(
85 new OfflinePageMetadataStoreImpl(base::ThreadTaskRunnerHandle::Get(),
86 temp_directory_.path()));
87 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
88 base::Unretained(this)));
89 PumpLoop();
90 return store;
91 }
92
93 void OfflinePageMetadataStoreImplTest::LoadCallback(
94 OfflinePageMetadataStore::LoadStatus load_status, 107 OfflinePageMetadataStore::LoadStatus load_status,
95 const std::vector<OfflinePageItem>& offline_pages) { 108 const std::vector<OfflinePageItem>& offline_pages) {
96 last_called_callback_ = LOAD; 109 last_called_callback_ = LOAD;
97 last_status_ = load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ? 110 last_status_ = load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ?
98 STATUS_TRUE : STATUS_FALSE; 111 STATUS_TRUE : STATUS_FALSE;
99 offline_pages_.swap(const_cast<std::vector<OfflinePageItem>&>(offline_pages)); 112 offline_pages_.swap(const_cast<std::vector<OfflinePageItem>&>(offline_pages));
100 } 113 }
101 114
102 void OfflinePageMetadataStoreImplTest::UpdateCallback( 115 void OfflinePageMetadataStoreTestBase::UpdateCallback(
103 CalledCallback called_callback, 116 CalledCallback called_callback,
104 bool status) { 117 bool status) {
105 last_called_callback_ = called_callback; 118 last_called_callback_ = called_callback;
106 last_status_ = status ? STATUS_TRUE : STATUS_FALSE; 119 last_status_ = status ? STATUS_TRUE : STATUS_FALSE;
107 } 120 }
108 121
109 void OfflinePageMetadataStoreImplTest::ClearResults() { 122 void OfflinePageMetadataStoreTestBase::ClearResults() {
110 last_called_callback_ = NONE; 123 last_called_callback_ = NONE;
111 last_status_ = STATUS_NONE; 124 last_status_ = STATUS_NONE;
112 offline_pages_.clear(); 125 offline_pages_.clear();
113 } 126 }
114 127
128 template <typename T>
129 class OfflinePageMetadataStoreTest : public OfflinePageMetadataStoreTestBase {
130 public:
131 scoped_ptr<OfflinePageMetadataStore> BuildStore();
132
133 protected:
134 T factory_;
135 };
136
137 template <typename T>
138 scoped_ptr<OfflinePageMetadataStore>
139 OfflinePageMetadataStoreTest<T>::BuildStore() {
140 scoped_ptr<OfflinePageMetadataStore> store(
141 factory_.BuildStore(temp_directory_.path()));
142 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
143 base::Unretained(this)));
144 PumpLoop();
145 return store;
146 }
147
148 typedef testing::Types<OfflinePageMetadataStoreImplFactory,
149 OfflinePageMetadataStoreSQLFactory>
150 MyTypes;
151 TYPED_TEST_CASE(OfflinePageMetadataStoreTest, MyTypes);
152
115 // Loads empty store and makes sure that there are no offline pages stored in 153 // Loads empty store and makes sure that there are no offline pages stored in
116 // it. 154 // it.
117 TEST_F(OfflinePageMetadataStoreImplTest, LoadEmptyStore) { 155 TYPED_TEST(OfflinePageMetadataStoreTest, LoadEmptyStore) {
118 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 156 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
119 EXPECT_EQ(LOAD, last_called_callback_); 157 EXPECT_EQ(LOAD, this->last_called_callback_);
120 EXPECT_EQ(STATUS_TRUE, last_status_); 158 EXPECT_EQ(STATUS_TRUE, this->last_status_);
121 EXPECT_EQ(0U, offline_pages_.size()); 159 EXPECT_EQ(0U, this->offline_pages_.size());
122 } 160 }
123 161
124 // Adds metadata of an offline page into a store and then opens the store 162 // Adds metadata of an offline page into a store and then opens the store
125 // again to make sure that stored metadata survives store restarts. 163 // again to make sure that stored metadata survives store restarts.
126 TEST_F(OfflinePageMetadataStoreImplTest, AddOfflinePage) { 164 TYPED_TEST(OfflinePageMetadataStoreTest, AddOfflinePage) {
127 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 165 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
128 166
129 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, 167 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId,
130 base::FilePath(kFilePath), kFileSize); 168 base::FilePath(kFilePath), kFileSize);
131 store->AddOrUpdateOfflinePage( 169 store->AddOrUpdateOfflinePage(
132 offline_page, 170 offline_page,
133 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 171 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
134 base::Unretained(this), ADD)); 172 base::Unretained(this), ADD));
135 PumpLoop(); 173 this->PumpLoop();
136 EXPECT_EQ(ADD, last_called_callback_); 174 EXPECT_EQ(ADD, this->last_called_callback_);
137 EXPECT_EQ(STATUS_TRUE, last_status_); 175 EXPECT_EQ(STATUS_TRUE, this->last_status_);
138 176
139 ClearResults(); 177 this->ClearResults();
140 178
141 // Close the store first to ensure file lock is removed. 179 // Close the store first to ensure file lock is removed.
142 store.reset(); 180 store.reset();
143 store = BuildStore(); 181 store = this->BuildStore();
144 PumpLoop(); 182 this->PumpLoop();
145 183
146 EXPECT_EQ(LOAD, last_called_callback_); 184 EXPECT_EQ(LOAD, this->last_called_callback_);
147 EXPECT_EQ(STATUS_TRUE, last_status_); 185 EXPECT_EQ(STATUS_TRUE, this->last_status_);
148 EXPECT_EQ(1U, offline_pages_.size()); 186 EXPECT_EQ(1U, this->offline_pages_.size());
149 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 187 EXPECT_EQ(offline_page.url, this->offline_pages_[0].url);
150 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 188 EXPECT_EQ(offline_page.offline_id, this->offline_pages_[0].offline_id);
151 EXPECT_EQ(offline_page.version, offline_pages_[0].version); 189 EXPECT_EQ(offline_page.version, this->offline_pages_[0].version);
152 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 190 EXPECT_EQ(offline_page.file_path, this->offline_pages_[0].file_path);
153 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 191 EXPECT_EQ(offline_page.file_size, this->offline_pages_[0].file_size);
154 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 192 EXPECT_EQ(offline_page.creation_time, this->offline_pages_[0].creation_time);
155 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 193 EXPECT_EQ(offline_page.last_access_time,
156 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 194 this->offline_pages_[0].last_access_time);
157 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); 195 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count);
196 EXPECT_EQ(offline_page.client_id, this->offline_pages_[0].client_id);
158 } 197 }
159 198
160 // Tests removing offline page metadata from the store, for which it first adds 199 // Tests removing offline page metadata from the store, for which it first adds
161 // metadata of an offline page. 200 // metadata of an offline page.
162 TEST_F(OfflinePageMetadataStoreImplTest, RemoveOfflinePage) { 201 TYPED_TEST(OfflinePageMetadataStoreTest, RemoveOfflinePage) {
163 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 202 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
164 203
165 // Add an offline page. 204 // Add an offline page.
166 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, 205 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId,
167 base::FilePath(kFilePath), kFileSize); 206 base::FilePath(kFilePath), kFileSize);
168 store->AddOrUpdateOfflinePage( 207 store->AddOrUpdateOfflinePage(
169 offline_page, 208 offline_page,
170 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 209 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
171 base::Unretained(this), ADD)); 210 base::Unretained(this), ADD));
172 PumpLoop(); 211 this->PumpLoop();
173 EXPECT_EQ(ADD, last_called_callback_); 212 EXPECT_EQ(ADD, this->last_called_callback_);
174 EXPECT_EQ(STATUS_TRUE, last_status_); 213 EXPECT_EQ(STATUS_TRUE, this->last_status_);
175 214
176 ClearResults(); 215 this->ClearResults();
177 216
178 // Load the store. 217 // Load the store.
179 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 218 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
180 base::Unretained(this))); 219 base::Unretained(this)));
181 PumpLoop(); 220 this->PumpLoop();
182 EXPECT_EQ(LOAD, last_called_callback_); 221 EXPECT_EQ(LOAD, this->last_called_callback_);
183 EXPECT_EQ(1U, offline_pages_.size()); 222 EXPECT_EQ(1U, this->offline_pages_.size());
184 223
185 // Remove the offline page. 224 // Remove the offline page.
186 std::vector<int64_t> ids_to_remove; 225 std::vector<int64_t> ids_to_remove;
187 ids_to_remove.push_back(offline_page.offline_id); 226 ids_to_remove.push_back(offline_page.offline_id);
188 store->RemoveOfflinePages( 227 store->RemoveOfflinePages(
189 ids_to_remove, 228 ids_to_remove,
190 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 229 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
191 base::Unretained(this), REMOVE)); 230 base::Unretained(this), REMOVE));
192 PumpLoop(); 231 this->PumpLoop();
193 EXPECT_EQ(REMOVE, last_called_callback_); 232 EXPECT_EQ(REMOVE, this->last_called_callback_);
194 EXPECT_EQ(STATUS_TRUE, last_status_); 233 EXPECT_EQ(STATUS_TRUE, this->last_status_);
195 234
196 ClearResults(); 235 this->ClearResults();
197 236
198 // Load the store. 237 // Load the store.
199 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 238 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
200 base::Unretained(this))); 239 base::Unretained(this)));
201 PumpLoop(); 240 this->PumpLoop();
202 EXPECT_EQ(LOAD, last_called_callback_); 241 EXPECT_EQ(LOAD, this->last_called_callback_);
203 EXPECT_EQ(0U, offline_pages_.size()); 242 EXPECT_EQ(0U, this->offline_pages_.size());
204 243
205 ClearResults(); 244 this->ClearResults();
206 245
207 // Close and reload the store. 246 // Close and reload the store.
208 store.reset(); 247 store.reset();
209 store = BuildStore(); 248 store = this->BuildStore();
210 EXPECT_EQ(LOAD, last_called_callback_); 249 EXPECT_EQ(LOAD, this->last_called_callback_);
211 EXPECT_EQ(STATUS_TRUE, last_status_); 250 EXPECT_EQ(STATUS_TRUE, this->last_status_);
212 EXPECT_EQ(0U, offline_pages_.size()); 251 EXPECT_EQ(0U, this->offline_pages_.size());
213 } 252 }
214 253
215 // Adds metadata of multiple offline pages into a store and removes some. 254 // Adds metadata of multiple offline pages into a store and removes some.
216 TEST_F(OfflinePageMetadataStoreImplTest, AddRemoveMultipleOfflinePages) { 255 TYPED_TEST(OfflinePageMetadataStoreTest, AddRemoveMultipleOfflinePages) {
217 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 256 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
218 257
219 // Add an offline page. 258 // Add an offline page.
220 OfflinePageItem offline_page_1(GURL(kTestURL), 12345LL, kTestBookmarkId, 259 OfflinePageItem offline_page_1(GURL(kTestURL), 12345LL, kTestBookmarkId,
221 base::FilePath(kFilePath), kFileSize); 260 base::FilePath(kFilePath), kFileSize);
222 base::FilePath file_path_2 = 261 base::FilePath file_path_2 =
223 base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml")); 262 base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml"));
224 OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL, 263 OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL,
225 kTestBookmarkId2, file_path_2, 12345, 264 kTestBookmarkId2, file_path_2, 12345,
226 base::Time::Now()); 265 base::Time::Now());
227 store->AddOrUpdateOfflinePage( 266 store->AddOrUpdateOfflinePage(
228 offline_page_1, 267 offline_page_1,
229 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 268 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
230 base::Unretained(this), ADD)); 269 base::Unretained(this), ADD));
231 PumpLoop(); 270 this->PumpLoop();
232 EXPECT_EQ(ADD, last_called_callback_); 271 EXPECT_EQ(ADD, this->last_called_callback_);
233 EXPECT_EQ(STATUS_TRUE, last_status_); 272 EXPECT_EQ(STATUS_TRUE, this->last_status_);
234 273
235 ClearResults(); 274 this->ClearResults();
236 275
237 // Add anther offline page. 276 // Add anther offline page.
238 store->AddOrUpdateOfflinePage( 277 store->AddOrUpdateOfflinePage(
239 offline_page_2, 278 offline_page_2,
240 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 279 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
241 base::Unretained(this), ADD)); 280 base::Unretained(this), ADD));
242 PumpLoop(); 281 this->PumpLoop();
243 EXPECT_EQ(ADD, last_called_callback_); 282 EXPECT_EQ(ADD, this->last_called_callback_);
244 EXPECT_EQ(STATUS_TRUE, last_status_); 283 EXPECT_EQ(STATUS_TRUE, this->last_status_);
245 284
246 ClearResults(); 285 this->ClearResults();
247 286
248 // Load the store. 287 // Load the store.
249 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 288 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
250 base::Unretained(this))); 289 base::Unretained(this)));
251 PumpLoop(); 290 this->PumpLoop();
252 291
253 EXPECT_EQ(LOAD, last_called_callback_); 292 EXPECT_EQ(LOAD, this->last_called_callback_);
254 EXPECT_EQ(STATUS_TRUE, last_status_); 293 EXPECT_EQ(STATUS_TRUE, this->last_status_);
255 EXPECT_EQ(2U, offline_pages_.size()); 294 EXPECT_EQ(2U, this->offline_pages_.size());
256 295
257 // Remove the offline page. 296 // Remove the offline page.
258 std::vector<int64_t> ids_to_remove; 297 std::vector<int64_t> ids_to_remove;
259 ids_to_remove.push_back(offline_page_1.offline_id); 298 ids_to_remove.push_back(offline_page_1.offline_id);
260 store->RemoveOfflinePages( 299 store->RemoveOfflinePages(
261 ids_to_remove, 300 ids_to_remove,
262 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 301 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
263 base::Unretained(this), REMOVE)); 302 base::Unretained(this), REMOVE));
264 PumpLoop(); 303 this->PumpLoop();
265 EXPECT_EQ(REMOVE, last_called_callback_); 304 EXPECT_EQ(REMOVE, this->last_called_callback_);
266 EXPECT_EQ(STATUS_TRUE, last_status_); 305 EXPECT_EQ(STATUS_TRUE, this->last_status_);
267 306
268 ClearResults(); 307 this->ClearResults();
269 308
270 // Close and reload the store. 309 // Close and reload the store.
271 store.reset(); 310 store.reset();
272 store = BuildStore(); 311 store = this->BuildStore();
273 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 312 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
274 base::Unretained(this))); 313 base::Unretained(this)));
275 PumpLoop(); 314 this->PumpLoop();
276 315
277 EXPECT_EQ(LOAD, last_called_callback_); 316 EXPECT_EQ(LOAD, this->last_called_callback_);
278 EXPECT_EQ(STATUS_TRUE, last_status_); 317 EXPECT_EQ(STATUS_TRUE, this->last_status_);
279 EXPECT_EQ(1U, offline_pages_.size()); 318 EXPECT_EQ(1U, this->offline_pages_.size());
280 EXPECT_EQ(offline_page_2.url, offline_pages_[0].url); 319 EXPECT_EQ(offline_page_2.url, this->offline_pages_[0].url);
281 EXPECT_EQ(offline_page_2.offline_id, offline_pages_[0].offline_id); 320 EXPECT_EQ(offline_page_2.offline_id, this->offline_pages_[0].offline_id);
282 EXPECT_EQ(offline_page_2.version, offline_pages_[0].version); 321 EXPECT_EQ(offline_page_2.version, this->offline_pages_[0].version);
283 EXPECT_EQ(offline_page_2.file_path, offline_pages_[0].file_path); 322 EXPECT_EQ(offline_page_2.file_path, this->offline_pages_[0].file_path);
284 EXPECT_EQ(offline_page_2.file_size, offline_pages_[0].file_size); 323 EXPECT_EQ(offline_page_2.file_size, this->offline_pages_[0].file_size);
285 EXPECT_EQ(offline_page_2.creation_time, offline_pages_[0].creation_time); 324 EXPECT_EQ(offline_page_2.creation_time,
325 this->offline_pages_[0].creation_time);
286 EXPECT_EQ(offline_page_2.last_access_time, 326 EXPECT_EQ(offline_page_2.last_access_time,
287 offline_pages_[0].last_access_time); 327 this->offline_pages_[0].last_access_time);
288 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count); 328 EXPECT_EQ(offline_page_2.access_count, this->offline_pages_[0].access_count);
289 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id); 329 EXPECT_EQ(offline_page_2.client_id, this->offline_pages_[0].client_id);
290 } 330 }
291 331
292 // Tests updating offline page metadata from the store. 332 // Tests updating offline page metadata from the store.
293 TEST_F(OfflinePageMetadataStoreImplTest, UpdateOfflinePage) { 333 TYPED_TEST(OfflinePageMetadataStoreTest, UpdateOfflinePage) {
294 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 334 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
295 335
296 // First, adds a fresh page. 336 // First, adds a fresh page.
297 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, 337 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId,
298 base::FilePath(kFilePath), kFileSize); 338 base::FilePath(kFilePath), kFileSize);
299 store->AddOrUpdateOfflinePage( 339 store->AddOrUpdateOfflinePage(
300 offline_page, 340 offline_page,
301 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 341 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
302 base::Unretained(this), ADD)); 342 base::Unretained(this), ADD));
303 PumpLoop(); 343 this->PumpLoop();
304 EXPECT_EQ(ADD, last_called_callback_); 344 EXPECT_EQ(ADD, this->last_called_callback_);
305 EXPECT_EQ(STATUS_TRUE, last_status_); 345 EXPECT_EQ(STATUS_TRUE, this->last_status_);
306 346
307 ClearResults(); 347 this->ClearResults();
308 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 348 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
309 base::Unretained(this))); 349 base::Unretained(this)));
310 PumpLoop(); 350 this->PumpLoop();
311 351
312 EXPECT_EQ(LOAD, last_called_callback_); 352 EXPECT_EQ(LOAD, this->last_called_callback_);
313 EXPECT_EQ(STATUS_TRUE, last_status_); 353 EXPECT_EQ(STATUS_TRUE, this->last_status_);
314 EXPECT_EQ(1U, offline_pages_.size()); 354 EXPECT_EQ(1U, this->offline_pages_.size());
315 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 355 EXPECT_EQ(offline_page.url, this->offline_pages_[0].url);
316 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 356 EXPECT_EQ(offline_page.offline_id, this->offline_pages_[0].offline_id);
317 EXPECT_EQ(offline_page.version, offline_pages_[0].version); 357 EXPECT_EQ(offline_page.version, this->offline_pages_[0].version);
318 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 358 EXPECT_EQ(offline_page.file_path, this->offline_pages_[0].file_path);
319 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 359 EXPECT_EQ(offline_page.file_size, this->offline_pages_[0].file_size);
320 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 360 EXPECT_EQ(offline_page.creation_time, this->offline_pages_[0].creation_time);
321 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 361 EXPECT_EQ(offline_page.last_access_time,
322 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 362 this->offline_pages_[0].last_access_time);
323 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); 363 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count);
364 EXPECT_EQ(offline_page.client_id, this->offline_pages_[0].client_id);
324 365
325 // Then updates some data. 366 // Then updates some data.
326 offline_page.file_size = kFileSize + 1; 367 offline_page.file_size = kFileSize + 1;
327 offline_page.access_count++; 368 offline_page.access_count++;
328 store->AddOrUpdateOfflinePage( 369 store->AddOrUpdateOfflinePage(
329 offline_page, 370 offline_page,
330 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 371 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
331 base::Unretained(this), ADD)); 372 base::Unretained(this), ADD));
332 PumpLoop(); 373 this->PumpLoop();
333 EXPECT_EQ(ADD, last_called_callback_); 374 EXPECT_EQ(ADD, this->last_called_callback_);
334 EXPECT_EQ(STATUS_TRUE, last_status_); 375 EXPECT_EQ(STATUS_TRUE, this->last_status_);
335 376
336 ClearResults(); 377 this->ClearResults();
337 store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback, 378 store->Load(base::Bind(&OfflinePageMetadataStoreTestBase::LoadCallback,
338 base::Unretained(this))); 379 base::Unretained(this)));
339 PumpLoop(); 380 this->PumpLoop();
340 381
341 EXPECT_EQ(LOAD, last_called_callback_); 382 EXPECT_EQ(LOAD, this->last_called_callback_);
342 EXPECT_EQ(STATUS_TRUE, last_status_); 383 EXPECT_EQ(STATUS_TRUE, this->last_status_);
343 EXPECT_EQ(1U, offline_pages_.size()); 384 EXPECT_EQ(1U, this->offline_pages_.size());
344 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 385 EXPECT_EQ(offline_page.url, this->offline_pages_[0].url);
345 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 386 EXPECT_EQ(offline_page.offline_id, this->offline_pages_[0].offline_id);
346 EXPECT_EQ(offline_page.version, offline_pages_[0].version); 387 EXPECT_EQ(offline_page.version, this->offline_pages_[0].version);
347 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 388 EXPECT_EQ(offline_page.file_path, this->offline_pages_[0].file_path);
348 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 389 EXPECT_EQ(offline_page.file_size, this->offline_pages_[0].file_size);
349 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 390 EXPECT_EQ(offline_page.creation_time, this->offline_pages_[0].creation_time);
350 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 391 EXPECT_EQ(offline_page.last_access_time,
351 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 392 this->offline_pages_[0].last_access_time);
352 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); 393 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count);
394 EXPECT_EQ(offline_page.client_id, this->offline_pages_[0].client_id);
353 } 395 }
354 396
355 } // namespace 397 } // namespace
356 398
399 class OfflinePageMetadataStoreImplTest
400 : public OfflinePageMetadataStoreTest<OfflinePageMetadataStoreImplFactory> {
401 };
402
357 // Test that loading a store with a bad value still loads. 403 // Test that loading a store with a bad value still loads.
358 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST 404 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST
359 // to work. 405 // to work.
360 TEST_F(OfflinePageMetadataStoreImplTest, LoadCorruptedStore) { 406 TEST_F(OfflinePageMetadataStoreImplTest, LoadCorruptedStore) {
361 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 407 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
362 408
363 // Write one ok page. 409 // Write one ok page.
364 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, 410 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId,
365 base::FilePath(kFilePath), kFileSize); 411 base::FilePath(kFilePath), kFileSize);
366 store->AddOrUpdateOfflinePage( 412 store->AddOrUpdateOfflinePage(
367 offline_page, 413 offline_page,
368 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 414 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
369 base::Unretained(this), ADD)); 415 base::Unretained(this), ADD));
370 PumpLoop(); 416 this->PumpLoop();
371 EXPECT_EQ(ADD, last_called_callback_); 417 EXPECT_EQ(ADD, this->last_called_callback_);
372 EXPECT_EQ(STATUS_TRUE, last_status_); 418 EXPECT_EQ(STATUS_TRUE, this->last_status_);
373 419
374 // Manually write one broken page (no id) 420 // Manually write one broken page (no id)
375 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> 421 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector>
376 entries_to_save( 422 entries_to_save(
377 new leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); 423 new leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector());
378 scoped_ptr<std::vector<std::string>> keys_to_remove( 424 scoped_ptr<std::vector<std::string>> keys_to_remove(
379 new std::vector<std::string>()); 425 new std::vector<std::string>());
380 426
381 OfflinePageEntry offline_page_proto; 427 OfflinePageEntry offline_page_proto;
382 entries_to_save->push_back(std::make_pair("0", offline_page_proto)); 428 entries_to_save->push_back(std::make_pair("0", offline_page_proto));
383 429
384 store->UpdateEntries( 430 ((OfflinePageMetadataStoreImpl*)store.get())
385 std::move(entries_to_save), std::move(keys_to_remove), 431 ->UpdateEntries(
386 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 432 std::move(entries_to_save), std::move(keys_to_remove),
387 base::Unretained(this), ADD)); 433 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
388 PumpLoop(); 434 base::Unretained(this), ADD));
435 this->PumpLoop();
389 436
390 EXPECT_EQ(ADD, last_called_callback_); 437 EXPECT_EQ(ADD, this->last_called_callback_);
391 EXPECT_EQ(STATUS_TRUE, last_status_); 438 EXPECT_EQ(STATUS_TRUE, this->last_status_);
392 439
393 ClearResults(); 440 this->ClearResults();
394 441
395 // Close the store first to ensure file lock is removed. 442 // Close the store first to ensure file lock is removed.
396 store.reset(); 443 store.reset();
397 store = BuildStore(); 444 store = this->BuildStore();
398 PumpLoop(); 445 this->PumpLoop();
399 446
400 // One of the pages was busted, so only expect one page. 447 // One of the pages was busted, so only expect one page.
401 EXPECT_EQ(LOAD, last_called_callback_); 448 EXPECT_EQ(LOAD, this->last_called_callback_);
402 EXPECT_EQ(STATUS_TRUE, last_status_); 449 EXPECT_EQ(STATUS_TRUE, this->last_status_);
403 EXPECT_EQ(1U, offline_pages_.size()); 450 EXPECT_EQ(1U, this->offline_pages_.size());
404 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 451 EXPECT_EQ(offline_page.url, this->offline_pages_[0].url);
405 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 452 EXPECT_EQ(offline_page.offline_id, this->offline_pages_[0].offline_id);
406 EXPECT_EQ(offline_page.version, offline_pages_[0].version); 453 EXPECT_EQ(offline_page.version, this->offline_pages_[0].version);
407 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 454 EXPECT_EQ(offline_page.file_path, this->offline_pages_[0].file_path);
408 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 455 EXPECT_EQ(offline_page.file_size, this->offline_pages_[0].file_size);
409 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 456 EXPECT_EQ(offline_page.creation_time, this->offline_pages_[0].creation_time);
410 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 457 EXPECT_EQ(offline_page.last_access_time,
411 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 458 this->offline_pages_[0].last_access_time);
459 EXPECT_EQ(offline_page.access_count, this->offline_pages_[0].access_count);
412 } 460 }
413 461
414 // Test that loading a store with nothing but bad values errors. 462 // Test that loading a store with nothing but bad values errors.
415 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST 463 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST
416 // to work. 464 // to work.
417 TEST_F(OfflinePageMetadataStoreImplTest, LoadTotallyCorruptedStore) { 465 TEST_F(OfflinePageMetadataStoreImplTest, LoadTotallyCorruptedStore) {
418 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); 466 scoped_ptr<OfflinePageMetadataStore> store(this->BuildStore());
419 467
420 // Manually write two broken pages (no id) 468 // Manually write two broken pages (no id)
421 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> 469 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector>
422 entries_to_save( 470 entries_to_save(
423 new leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); 471 new leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector());
424 scoped_ptr<std::vector<std::string>> keys_to_remove( 472 scoped_ptr<std::vector<std::string>> keys_to_remove(
425 new std::vector<std::string>()); 473 new std::vector<std::string>());
426 474
427 OfflinePageEntry offline_page_proto; 475 OfflinePageEntry offline_page_proto;
428 entries_to_save->push_back(std::make_pair("0", offline_page_proto)); 476 entries_to_save->push_back(std::make_pair("0", offline_page_proto));
429 entries_to_save->push_back(std::make_pair("1", offline_page_proto)); 477 entries_to_save->push_back(std::make_pair("1", offline_page_proto));
430 478
431 store->UpdateEntries( 479 ((OfflinePageMetadataStoreImpl*)store.get())
432 std::move(entries_to_save), std::move(keys_to_remove), 480 ->UpdateEntries(
433 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, 481 std::move(entries_to_save), std::move(keys_to_remove),
434 base::Unretained(this), ADD)); 482 base::Bind(&OfflinePageMetadataStoreTestBase::UpdateCallback,
435 PumpLoop(); 483 base::Unretained(this), ADD));
484 this->PumpLoop();
436 485
437 EXPECT_EQ(ADD, last_called_callback_); 486 EXPECT_EQ(ADD, this->last_called_callback_);
438 EXPECT_EQ(STATUS_TRUE, last_status_); 487 EXPECT_EQ(STATUS_TRUE, this->last_status_);
439 488
440 ClearResults(); 489 this->ClearResults();
441 490
442 // Close the store first to ensure file lock is removed. 491 // Close the store first to ensure file lock is removed.
443 store.reset(); 492 store.reset();
444 store = BuildStore(); 493 store = this->BuildStore();
445 PumpLoop(); 494 this->PumpLoop();
446 495
447 // One of the pages was busted, so only expect one page. 496 // One of the pages was busted, so only expect one page.
448 EXPECT_EQ(LOAD, last_called_callback_); 497 EXPECT_EQ(LOAD, this->last_called_callback_);
449 EXPECT_EQ(STATUS_FALSE, last_status_); 498 EXPECT_EQ(STATUS_FALSE, this->last_status_);
450 } 499 }
451 500
452 } // namespace offline_pages 501 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698