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

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

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years 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 2015 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/offline_pages/offline_page_metadata_store.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10
11 #include "base/bind.h"
12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/test_simple_task_runner.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "components/offline_pages/offline_page_item.h"
19 #include "components/offline_pages/offline_page_metadata_store_sql.h"
20 #include "components/offline_pages/offline_page_model.h"
21 #include "sql/connection.h"
22 #include "sql/statement.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace offline_pages {
26
27 namespace {
28
29 #define OFFLINE_PAGES_TABLE_V1 "offlinepages_v1"
30
31 const char kTestClientNamespace[] = "CLIENT_NAMESPACE";
32 const char kTestURL[] = "https://example.com";
33 const char kOriginalTestURL[] = "https://example.com/foo";
34 const ClientId kTestClientId1(kTestClientNamespace, "1234");
35 const ClientId kTestClientId2(kTestClientNamespace, "5678");
36 const base::FilePath::CharType kFilePath[] =
37 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml");
38 int64_t kFileSize = 234567LL;
39 int64_t kOfflineId = 12345LL;
40
41 // Build a store with outdated schema to simulate the upgrading process.
42 // TODO(romax): move it to sql_unittests.
43 void BuildTestStoreWithSchemaFromM52(const base::FilePath& file) {
44 sql::Connection connection;
45 ASSERT_TRUE(
46 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
47 ASSERT_TRUE(connection.is_open());
48 ASSERT_TRUE(connection.BeginTransaction());
49 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
50 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
51 "creation_time INTEGER NOT NULL, "
52 "file_size INTEGER NOT NULL, "
53 "version INTEGER NOT NULL, "
54 "last_access_time INTEGER NOT NULL, "
55 "access_count INTEGER NOT NULL, "
56 "status INTEGER NOT NULL DEFAULT 0, "
57 "user_initiated INTEGER, "
58 "client_namespace VARCHAR NOT NULL, "
59 "client_id VARCHAR NOT NULL, "
60 "online_url VARCHAR NOT NULL, "
61 "offline_url VARCHAR NOT NULL DEFAULT '', "
62 "file_path VARCHAR NOT NULL "
63 ")"));
64 ASSERT_TRUE(connection.CommitTransaction());
65 sql::Statement statement(connection.GetUniqueStatement(
66 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
67 "(offline_id, creation_time, file_size, version, "
68 "last_access_time, access_count, client_namespace, "
69 "client_id, online_url, file_path) "
70 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
71 statement.BindInt64(0, kOfflineId);
72 statement.BindInt(1, 0);
73 statement.BindInt64(2, kFileSize);
74 statement.BindInt(3, 0);
75 statement.BindInt(4, 0);
76 statement.BindInt(5, 1);
77 statement.BindCString(6, kTestClientNamespace);
78 statement.BindString(7, kTestClientId2.id);
79 statement.BindCString(8, kTestURL);
80 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII());
81 ASSERT_TRUE(statement.Run());
82 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
83 ASSERT_FALSE(
84 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "expiration_time"));
85 }
86
87 void BuildTestStoreWithSchemaFromM53(const base::FilePath& file) {
88 sql::Connection connection;
89 ASSERT_TRUE(
90 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
91 ASSERT_TRUE(connection.is_open());
92 ASSERT_TRUE(connection.BeginTransaction());
93 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
94 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
95 "creation_time INTEGER NOT NULL, "
96 "file_size INTEGER NOT NULL, "
97 "version INTEGER NOT NULL, "
98 "last_access_time INTEGER NOT NULL, "
99 "access_count INTEGER NOT NULL, "
100 "status INTEGER NOT NULL DEFAULT 0, "
101 "user_initiated INTEGER, "
102 "expiration_time INTEGER NOT NULL DEFAULT 0, "
103 "client_namespace VARCHAR NOT NULL, "
104 "client_id VARCHAR NOT NULL, "
105 "online_url VARCHAR NOT NULL, "
106 "offline_url VARCHAR NOT NULL DEFAULT '', "
107 "file_path VARCHAR NOT NULL "
108 ")"));
109 ASSERT_TRUE(connection.CommitTransaction());
110 sql::Statement statement(connection.GetUniqueStatement(
111 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
112 "(offline_id, creation_time, file_size, version, "
113 "last_access_time, access_count, client_namespace, "
114 "client_id, online_url, file_path, expiration_time) "
115 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
116 statement.BindInt64(0, kOfflineId);
117 statement.BindInt(1, 0);
118 statement.BindInt64(2, kFileSize);
119 statement.BindInt(3, 0);
120 statement.BindInt(4, 0);
121 statement.BindInt(5, 1);
122 statement.BindCString(6, kTestClientNamespace);
123 statement.BindString(7, kTestClientId2.id);
124 statement.BindCString(8, kTestURL);
125 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII());
126 statement.BindInt64(10, base::Time::Now().ToInternalValue());
127 ASSERT_TRUE(statement.Run());
128 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
129 ASSERT_FALSE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "title"));
130 }
131
132 void BuildTestStoreWithSchemaFromM54(const base::FilePath& file) {
133 sql::Connection connection;
134 ASSERT_TRUE(
135 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
136 ASSERT_TRUE(connection.is_open());
137 ASSERT_TRUE(connection.BeginTransaction());
138 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
139 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
140 "creation_time INTEGER NOT NULL, "
141 "file_size INTEGER NOT NULL, "
142 "version INTEGER NOT NULL, "
143 "last_access_time INTEGER NOT NULL, "
144 "access_count INTEGER NOT NULL, "
145 "status INTEGER NOT NULL DEFAULT 0, "
146 "user_initiated INTEGER, "
147 "expiration_time INTEGER NOT NULL DEFAULT 0, "
148 "client_namespace VARCHAR NOT NULL, "
149 "client_id VARCHAR NOT NULL, "
150 "online_url VARCHAR NOT NULL, "
151 "offline_url VARCHAR NOT NULL DEFAULT '', "
152 "file_path VARCHAR NOT NULL, "
153 "title VARCHAR NOT NULL DEFAULT ''"
154 ")"));
155 ASSERT_TRUE(connection.CommitTransaction());
156 sql::Statement statement(connection.GetUniqueStatement(
157 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
158 "(offline_id, creation_time, file_size, version, "
159 "last_access_time, access_count, client_namespace, "
160 "client_id, online_url, file_path, expiration_time, title) "
161 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
162 statement.BindInt64(0, kOfflineId);
163 statement.BindInt(1, 0);
164 statement.BindInt64(2, kFileSize);
165 statement.BindInt(3, 0);
166 statement.BindInt(4, 0);
167 statement.BindInt(5, 1);
168 statement.BindCString(6, kTestClientNamespace);
169 statement.BindString(7, kTestClientId2.id);
170 statement.BindCString(8, kTestURL);
171 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII());
172 statement.BindInt64(10, base::Time::Now().ToInternalValue());
173 statement.BindString16(11, base::UTF8ToUTF16("Test title"));
174 ASSERT_TRUE(statement.Run());
175 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
176 ASSERT_TRUE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "version"));
177 ASSERT_TRUE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "status"));
178 ASSERT_TRUE(
179 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "user_initiated"));
180 ASSERT_TRUE(
181 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "offline_url"));
182 }
183
184 void BuildTestStoreWithSchemaFromM55(const base::FilePath& file) {
185 sql::Connection connection;
186 ASSERT_TRUE(
187 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
188 ASSERT_TRUE(connection.is_open());
189 ASSERT_TRUE(connection.BeginTransaction());
190 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
191 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
192 "creation_time INTEGER NOT NULL, "
193 "file_size INTEGER NOT NULL, "
194 "last_access_time INTEGER NOT NULL, "
195 "access_count INTEGER NOT NULL, "
196 "expiration_time INTEGER NOT NULL DEFAULT 0, "
197 "client_namespace VARCHAR NOT NULL, "
198 "client_id VARCHAR NOT NULL, "
199 "online_url VARCHAR NOT NULL, "
200 "file_path VARCHAR NOT NULL, "
201 "title VARCHAR NOT NULL DEFAULT ''"
202 ")"));
203 ASSERT_TRUE(connection.CommitTransaction());
204 sql::Statement statement(connection.GetUniqueStatement(
205 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
206 "(offline_id, creation_time, file_size, "
207 "last_access_time, access_count, client_namespace, "
208 "client_id, online_url, file_path, expiration_time, title) "
209 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
210 statement.BindInt64(0, kOfflineId);
211 statement.BindInt(1, 0);
212 statement.BindInt64(2, kFileSize);
213 statement.BindInt(3, 0);
214 statement.BindInt(4, 1);
215 statement.BindCString(5, kTestClientNamespace);
216 statement.BindString(6, kTestClientId2.id);
217 statement.BindCString(7, kTestURL);
218 statement.BindString(8, base::FilePath(kFilePath).MaybeAsASCII());
219 statement.BindInt64(9, base::Time::Now().ToInternalValue());
220 statement.BindString16(10, base::UTF8ToUTF16("Test title"));
221 ASSERT_TRUE(statement.Run());
222 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
223 ASSERT_TRUE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "title"));
224 ASSERT_FALSE(
225 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "original_url"));
226 }
227
228 void BuildTestStoreWithSchemaFromM56(const base::FilePath& file) {
229 sql::Connection connection;
230 ASSERT_TRUE(
231 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
232 ASSERT_TRUE(connection.is_open());
233 ASSERT_TRUE(connection.BeginTransaction());
234 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
235 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
236 "creation_time INTEGER NOT NULL, "
237 "file_size INTEGER NOT NULL, "
238 "last_access_time INTEGER NOT NULL, "
239 "access_count INTEGER NOT NULL, "
240 "expiration_time INTEGER NOT NULL DEFAULT 0, "
241 "client_namespace VARCHAR NOT NULL, "
242 "client_id VARCHAR NOT NULL, "
243 "online_url VARCHAR NOT NULL, "
244 "file_path VARCHAR NOT NULL, "
245 "title VARCHAR NOT NULL DEFAULT '', "
246 "original_url VARCHAR NOT NULL DEFAULT ''"
247 ")"));
248 ASSERT_TRUE(connection.CommitTransaction());
249 sql::Statement statement(connection.GetUniqueStatement(
250 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
251 "(offline_id, creation_time, file_size, "
252 "last_access_time, access_count, client_namespace, "
253 "client_id, online_url, file_path, expiration_time, title, original_url) "
254 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
255 statement.BindInt64(0, kOfflineId);
256 statement.BindInt(1, 0);
257 statement.BindInt64(2, kFileSize);
258 statement.BindInt(3, 0);
259 statement.BindInt(4, 1);
260 statement.BindCString(5, kTestClientNamespace);
261 statement.BindString(6, kTestClientId2.id);
262 statement.BindCString(7, kTestURL);
263 statement.BindString(8, base::FilePath(kFilePath).MaybeAsASCII());
264 statement.BindInt64(9, base::Time::Now().ToInternalValue());
265 statement.BindString16(10, base::UTF8ToUTF16("Test title"));
266 statement.BindCString(11, kOriginalTestURL);
267 ASSERT_TRUE(statement.Run());
268 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
269 ASSERT_TRUE(
270 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "expiration_time"));
271 }
272
273 class OfflinePageMetadataStoreFactory {
274 public:
275 OfflinePageMetadataStore* BuildStore(const base::FilePath& file_path) {
276 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
277 base::ThreadTaskRunnerHandle::Get(), file_path);
278 return store;
279 }
280
281 OfflinePageMetadataStore* BuildStoreM52(const base::FilePath& file_path) {
282 BuildTestStoreWithSchemaFromM52(file_path);
283 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
284 base::ThreadTaskRunnerHandle::Get(), file_path);
285 return store;
286 }
287
288 OfflinePageMetadataStore* BuildStoreM53(const base::FilePath& file_path) {
289 BuildTestStoreWithSchemaFromM53(file_path);
290 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
291 base::ThreadTaskRunnerHandle::Get(), file_path);
292 return store;
293 }
294
295 OfflinePageMetadataStore* BuildStoreM54(const base::FilePath& file_path) {
296 BuildTestStoreWithSchemaFromM54(file_path);
297 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
298 base::ThreadTaskRunnerHandle::Get(), file_path);
299 return store;
300 }
301
302 OfflinePageMetadataStore* BuildStoreM55(const base::FilePath& file_path) {
303 BuildTestStoreWithSchemaFromM55(file_path);
304 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
305 base::ThreadTaskRunnerHandle::Get(), file_path);
306 return store;
307 }
308
309 OfflinePageMetadataStore* BuildStoreM56(const base::FilePath& file_path) {
310 BuildTestStoreWithSchemaFromM56(file_path);
311 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
312 base::ThreadTaskRunnerHandle::Get(), file_path);
313 return store;
314 }
315 };
316
317 enum CalledCallback { NONE, LOAD, ADD, UPDATE, REMOVE, RESET };
318 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE };
319
320 class OfflinePageMetadataStoreTest : public testing::Test {
321 public:
322 OfflinePageMetadataStoreTest();
323 ~OfflinePageMetadataStoreTest() override;
324
325 void TearDown() override {
326 // Wait for all the pieces of the store to delete itself properly.
327 PumpLoop();
328 }
329
330 std::unique_ptr<OfflinePageMetadataStore> BuildStore();
331 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM52();
332 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM53();
333 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM54();
334 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM55();
335 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM56();
336
337 void PumpLoop();
338
339 void InitializeCallback(bool success);
340 void GetOfflinePagesCallback(
341 const std::vector<OfflinePageItem>& offline_pages);
342 void AddCallback(ItemActionStatus status);
343 void UpdateCallback(CalledCallback called_callback,
344 std::unique_ptr<OfflinePagesUpdateResult> result);
345 void ResetCallback(bool success);
346
347 void ClearResults();
348
349 OfflinePageItem CheckThatStoreHasOneItem();
350 void CheckThatOfflinePageCanBeSaved(
351 std::unique_ptr<OfflinePageMetadataStore> store);
352
353 OfflinePagesUpdateResult* last_update_result() {
354 return last_update_result_.get();
355 }
356
357 protected:
358 CalledCallback last_called_callback_;
359 Status last_status_;
360 std::unique_ptr<OfflinePagesUpdateResult> last_update_result_;
361 std::vector<OfflinePageItem> offline_pages_;
362 OfflinePageMetadataStoreFactory factory_;
363
364 base::ScopedTempDir temp_directory_;
365 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
366 base::ThreadTaskRunnerHandle task_runner_handle_;
367 };
368
369 OfflinePageMetadataStoreTest::OfflinePageMetadataStoreTest()
370 : last_called_callback_(NONE),
371 last_status_(STATUS_NONE),
372 task_runner_(new base::TestSimpleTaskRunner),
373 task_runner_handle_(task_runner_) {
374 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir());
375 }
376
377 OfflinePageMetadataStoreTest::~OfflinePageMetadataStoreTest() {}
378
379 void OfflinePageMetadataStoreTest::PumpLoop() {
380 task_runner_->RunUntilIdle();
381 }
382
383 void OfflinePageMetadataStoreTest::InitializeCallback(bool success) {
384 last_status_ = success ? STATUS_TRUE : STATUS_FALSE;
385 }
386
387 void OfflinePageMetadataStoreTest::GetOfflinePagesCallback(
388 const std::vector<OfflinePageItem>& offline_pages) {
389 last_called_callback_ = LOAD;
390 offline_pages_.swap(const_cast<std::vector<OfflinePageItem>&>(offline_pages));
391 }
392
393 void OfflinePageMetadataStoreTest::AddCallback(ItemActionStatus status) {
394 last_called_callback_ = ADD;
395 // TODO(fgorski): Add specific add status.
396 // last_item_status_ = status;
397 last_status_ =
398 status == ItemActionStatus::SUCCESS ? STATUS_TRUE : STATUS_FALSE;
399 }
400
401 void OfflinePageMetadataStoreTest::UpdateCallback(
402 CalledCallback called_callback,
403 std::unique_ptr<OfflinePagesUpdateResult> result) {
404 last_called_callback_ = called_callback;
405 last_status_ = result->updated_items.size() > 0 ? STATUS_TRUE : STATUS_FALSE;
406 last_update_result_ = std::move(result);
407 }
408
409 void OfflinePageMetadataStoreTest::ResetCallback(bool success) {
410 last_called_callback_ = RESET;
411 last_status_ = success ? STATUS_TRUE : STATUS_FALSE;
412 }
413
414 void OfflinePageMetadataStoreTest::ClearResults() {
415 last_called_callback_ = NONE;
416 last_status_ = STATUS_NONE;
417 offline_pages_.clear();
418 last_update_result_.reset(nullptr);
419 }
420
421 OfflinePageItem OfflinePageMetadataStoreTest::CheckThatStoreHasOneItem() {
422 EXPECT_EQ(LOAD, last_called_callback_);
423 EXPECT_EQ(STATUS_TRUE, last_status_);
424 EXPECT_EQ(1U, offline_pages_.size());
425
426 return offline_pages_[0];
427 }
428
429 void OfflinePageMetadataStoreTest::CheckThatOfflinePageCanBeSaved(
430 std::unique_ptr<OfflinePageMetadataStore> store) {
431 size_t store_size = offline_pages_.size();
432 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
433 base::FilePath(kFilePath), kFileSize);
434 offline_page.title = base::UTF8ToUTF16("a title");
435 offline_page.original_url = GURL(kOriginalTestURL);
436
437 store->AddOfflinePage(offline_page,
438 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
439 base::Unretained(this)));
440 PumpLoop();
441 EXPECT_EQ(ADD, last_called_callback_);
442 EXPECT_EQ(STATUS_TRUE, last_status_);
443 ClearResults();
444
445 // Close the store first to ensure file lock is removed.
446 store.reset();
447 store = BuildStore();
448 PumpLoop();
449
450 EXPECT_EQ(LOAD, last_called_callback_);
451 EXPECT_EQ(STATUS_TRUE, last_status_);
452 ASSERT_EQ(store_size + 1, offline_pages_.size());
453 if (store_size > 0 &&
454 offline_pages_[0].offline_id != offline_page.offline_id) {
455 std::swap(offline_pages_[0], offline_pages_[1]);
456 }
457 EXPECT_EQ(offline_page, offline_pages_[0]);
458 }
459
460 std::unique_ptr<OfflinePageMetadataStore>
461 OfflinePageMetadataStoreTest::BuildStore() {
462 std::unique_ptr<OfflinePageMetadataStore> store(
463 factory_.BuildStore(temp_directory_.GetPath()));
464 PumpLoop();
465 store->Initialize(
466 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
467 base::Unretained(this)));
468 PumpLoop();
469 store->GetOfflinePages(
470 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
471 base::Unretained(this)));
472 PumpLoop();
473 return store;
474 }
475
476 std::unique_ptr<OfflinePageMetadataStore>
477 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM52() {
478 std::unique_ptr<OfflinePageMetadataStore> store(
479 factory_.BuildStoreM52(temp_directory_.GetPath()));
480 PumpLoop();
481 store->Initialize(
482 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
483 base::Unretained(this)));
484 PumpLoop();
485 store->GetOfflinePages(
486 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
487 base::Unretained(this)));
488 PumpLoop();
489 return store;
490 }
491
492 std::unique_ptr<OfflinePageMetadataStore>
493 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM53() {
494 std::unique_ptr<OfflinePageMetadataStore> store(
495 factory_.BuildStoreM53(temp_directory_.GetPath()));
496 PumpLoop();
497 store->Initialize(
498 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
499 base::Unretained(this)));
500 PumpLoop();
501 store->GetOfflinePages(
502 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
503 base::Unretained(this)));
504 PumpLoop();
505 return store;
506 }
507
508 std::unique_ptr<OfflinePageMetadataStore>
509 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM54() {
510 std::unique_ptr<OfflinePageMetadataStore> store(
511 factory_.BuildStoreM54(temp_directory_.GetPath()));
512 PumpLoop();
513 store->Initialize(
514 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
515 base::Unretained(this)));
516 PumpLoop();
517 store->GetOfflinePages(
518 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
519 base::Unretained(this)));
520 PumpLoop();
521 return store;
522 }
523
524 std::unique_ptr<OfflinePageMetadataStore>
525 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM55() {
526 std::unique_ptr<OfflinePageMetadataStore> store(
527 factory_.BuildStoreM55(temp_directory_.GetPath()));
528 PumpLoop();
529 store->Initialize(
530 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
531 base::Unretained(this)));
532 PumpLoop();
533 store->GetOfflinePages(
534 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
535 base::Unretained(this)));
536 PumpLoop();
537 return store;
538 }
539
540 std::unique_ptr<OfflinePageMetadataStore>
541 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM56() {
542 std::unique_ptr<OfflinePageMetadataStore> store(
543 factory_.BuildStoreM56(temp_directory_.GetPath()));
544 PumpLoop();
545 store->Initialize(
546 base::Bind(&OfflinePageMetadataStoreTest::InitializeCallback,
547 base::Unretained(this)));
548 PumpLoop();
549 store->GetOfflinePages(
550 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
551 base::Unretained(this)));
552 PumpLoop();
553 return store;
554 }
555
556 // Loads empty store and makes sure that there are no offline pages stored in
557 // it.
558 TEST_F(OfflinePageMetadataStoreTest, LoadEmptyStore) {
559 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
560 EXPECT_EQ(LOAD, last_called_callback_);
561 EXPECT_EQ(STATUS_TRUE, last_status_);
562 EXPECT_EQ(0U, offline_pages_.size());
563 }
564
565 TEST_F(OfflinePageMetadataStoreTest, GetOfflinePagesFromInvalidStore) {
566 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
567 OfflinePageMetadataStoreSQL* sql_store =
568 static_cast<OfflinePageMetadataStoreSQL*>(store.get());
569
570 sql_store->SetStateForTesting(StoreState::NOT_LOADED, false);
571 store->GetOfflinePages(
572 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
573 base::Unretained(this)));
574 PumpLoop();
575 EXPECT_EQ(LOAD, last_called_callback_);
576 EXPECT_EQ(0UL, offline_pages_.size());
577 EXPECT_EQ(StoreState::NOT_LOADED, store->state());
578
579 ClearResults();
580 sql_store->SetStateForTesting(StoreState::FAILED_LOADING, false);
581 store->GetOfflinePages(
582 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
583 base::Unretained(this)));
584 PumpLoop();
585 EXPECT_EQ(LOAD, last_called_callback_);
586 EXPECT_EQ(0UL, offline_pages_.size());
587 EXPECT_EQ(StoreState::FAILED_LOADING, store->state());
588
589 ClearResults();
590 sql_store->SetStateForTesting(StoreState::FAILED_RESET, false);
591 store->GetOfflinePages(
592 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
593 base::Unretained(this)));
594 PumpLoop();
595 EXPECT_EQ(LOAD, last_called_callback_);
596 EXPECT_EQ(0UL, offline_pages_.size());
597 EXPECT_EQ(StoreState::FAILED_RESET, store->state());
598
599 ClearResults();
600 sql_store->SetStateForTesting(StoreState::LOADED, true);
601 store->GetOfflinePages(
602 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
603 base::Unretained(this)));
604 PumpLoop();
605 EXPECT_EQ(LOAD, last_called_callback_);
606 EXPECT_EQ(0UL, offline_pages_.size());
607
608 ClearResults();
609 sql_store->SetStateForTesting(StoreState::NOT_LOADED, true);
610 store->GetOfflinePages(
611 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
612 base::Unretained(this)));
613 PumpLoop();
614 EXPECT_EQ(LOAD, last_called_callback_);
615 EXPECT_EQ(0UL, offline_pages_.size());
616
617 ClearResults();
618 sql_store->SetStateForTesting(StoreState::FAILED_LOADING, false);
619 store->GetOfflinePages(
620 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
621 base::Unretained(this)));
622 PumpLoop();
623 EXPECT_EQ(LOAD, last_called_callback_);
624 EXPECT_EQ(0UL, offline_pages_.size());
625
626 ClearResults();
627 sql_store->SetStateForTesting(StoreState::FAILED_RESET, false);
628 store->GetOfflinePages(
629 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
630 base::Unretained(this)));
631 PumpLoop();
632 EXPECT_EQ(LOAD, last_called_callback_);
633 EXPECT_EQ(0UL, offline_pages_.size());
634 }
635
636 // Loads a store which has an outdated schema.
637 // This test case would crash if it's not handling correctly when we're loading
638 // old version stores.
639 // TODO(romax): Move this to sql_unittest.
640 TEST_F(OfflinePageMetadataStoreTest, LoadVersion52Store) {
641 std::unique_ptr<OfflinePageMetadataStore> store(
642 BuildStoreWithSchemaFromM52());
643
644 OfflinePageItem item = CheckThatStoreHasOneItem();
645 CheckThatOfflinePageCanBeSaved(std::move(store));
646 }
647
648 // Loads a store which has an outdated schema.
649 // This test case would crash if it's not handling correctly when we're loading
650 // old version stores.
651 // TODO(romax): Move this to sql_unittest.
652 TEST_F(OfflinePageMetadataStoreTest, LoadVersion53Store) {
653 std::unique_ptr<OfflinePageMetadataStore> store(
654 BuildStoreWithSchemaFromM53());
655
656 OfflinePageItem item = CheckThatStoreHasOneItem();
657 CheckThatOfflinePageCanBeSaved(std::move(store));
658 }
659
660 // Loads a string with schema from M54.
661 // This test case would crash if it's not handling correctly when we're loading
662 // old version stores.
663 // TODO(romax): Move this to sql_unittest.
664 TEST_F(OfflinePageMetadataStoreTest, LoadVersion54Store) {
665 std::unique_ptr<OfflinePageMetadataStore> store(
666 BuildStoreWithSchemaFromM54());
667
668 OfflinePageItem item = CheckThatStoreHasOneItem();
669 CheckThatOfflinePageCanBeSaved(std::move(store));
670 }
671
672 // Loads a string with schema from M55.
673 // This test case would crash if it's not handling correctly when we're loading
674 // old version stores.
675 // TODO(romax): Move this to sql_unittest.
676 TEST_F(OfflinePageMetadataStoreTest, LoadVersion55Store) {
677 std::unique_ptr<OfflinePageMetadataStore> store(
678 BuildStoreWithSchemaFromM55());
679
680 OfflinePageItem item = CheckThatStoreHasOneItem();
681 CheckThatOfflinePageCanBeSaved(std::move(store));
682 }
683
684 // Loads a string with schema from M56.
685 // This test case would crash if it's not handling correctly when we're loading
686 // old version stores.
687 // TODO(romax): Move this to sql_unittest.
688 TEST_F(OfflinePageMetadataStoreTest, LoadVersion56Store) {
689 std::unique_ptr<OfflinePageMetadataStore> store(
690 BuildStoreWithSchemaFromM56());
691
692 OfflinePageItem item = CheckThatStoreHasOneItem();
693 CheckThatOfflinePageCanBeSaved(std::move(store));
694 }
695
696 // Adds metadata of an offline page into a store and then opens the store
697 // again to make sure that stored metadata survives store restarts.
698 TEST_F(OfflinePageMetadataStoreTest, AddOfflinePage) {
699 CheckThatOfflinePageCanBeSaved(BuildStore());
700 }
701
702 TEST_F(OfflinePageMetadataStoreTest, AddSameOfflinePageTwice) {
703 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
704
705 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
706 base::FilePath(kFilePath), kFileSize);
707 offline_page.title = base::UTF8ToUTF16("a title");
708
709 store->AddOfflinePage(offline_page,
710 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
711 base::Unretained(this)));
712 PumpLoop();
713 EXPECT_EQ(ADD, last_called_callback_);
714 EXPECT_EQ(STATUS_TRUE, last_status_);
715 ClearResults();
716
717 store->AddOfflinePage(offline_page,
718 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
719 base::Unretained(this)));
720 PumpLoop();
721 EXPECT_EQ(ADD, last_called_callback_);
722 EXPECT_EQ(STATUS_FALSE, last_status_);
723 }
724
725 // Tests removing offline page metadata from the store, for which it first adds
726 // metadata of an offline page.
727 TEST_F(OfflinePageMetadataStoreTest, RemoveOfflinePage) {
728 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
729
730 // Add an offline page.
731 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
732 base::FilePath(kFilePath), kFileSize);
733 store->AddOfflinePage(offline_page,
734 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
735 base::Unretained(this)));
736 PumpLoop();
737 EXPECT_EQ(ADD, last_called_callback_);
738 EXPECT_EQ(STATUS_TRUE, last_status_);
739
740 ClearResults();
741
742 // Get all pages from the store.
743 store->GetOfflinePages(
744 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
745 base::Unretained(this)));
746 PumpLoop();
747 EXPECT_EQ(LOAD, last_called_callback_);
748 EXPECT_EQ(1U, offline_pages_.size());
749
750 // Remove the offline page.
751 std::vector<int64_t> ids_to_remove;
752 ids_to_remove.push_back(offline_page.offline_id);
753 store->RemoveOfflinePages(
754 ids_to_remove, base::Bind(&OfflinePageMetadataStoreTest::UpdateCallback,
755 base::Unretained(this), REMOVE));
756 PumpLoop();
757 EXPECT_EQ(REMOVE, last_called_callback_);
758 EXPECT_EQ(STATUS_TRUE, last_status_);
759 ASSERT_TRUE(last_update_result() != nullptr);
760 EXPECT_EQ(1UL, last_update_result()->item_statuses.size());
761 EXPECT_EQ(ItemActionStatus::SUCCESS,
762 last_update_result()->item_statuses.begin()->second);
763 EXPECT_EQ(1UL, last_update_result()->updated_items.size());
764 EXPECT_EQ(offline_page, *(last_update_result()->updated_items.begin()));
765
766 ClearResults();
767
768 // Get all pages from the store.
769 store->GetOfflinePages(
770 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
771 base::Unretained(this)));
772 PumpLoop();
773 EXPECT_EQ(LOAD, last_called_callback_);
774 EXPECT_EQ(0U, offline_pages_.size());
775
776 ClearResults();
777
778 // Close and reload the store.
779 store.reset();
780 store = BuildStore();
781 EXPECT_EQ(LOAD, last_called_callback_);
782 EXPECT_EQ(STATUS_TRUE, last_status_);
783 EXPECT_EQ(0U, offline_pages_.size());
784 }
785
786 // Adds metadata of multiple offline pages into a store and removes some.
787 TEST_F(OfflinePageMetadataStoreTest, AddRemoveMultipleOfflinePages) {
788 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
789
790 // Add an offline page.
791 OfflinePageItem offline_page_1(GURL(kTestURL), 12345LL, kTestClientId1,
792 base::FilePath(kFilePath), kFileSize);
793 store->AddOfflinePage(offline_page_1,
794 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
795 base::Unretained(this)));
796 PumpLoop();
797 EXPECT_EQ(ADD, last_called_callback_);
798 EXPECT_EQ(STATUS_TRUE, last_status_);
799
800 ClearResults();
801
802 // Add anther offline page.
803 base::FilePath file_path_2 =
804 base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml"));
805 OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL,
806 kTestClientId2, file_path_2, 12345,
807 base::Time::Now());
808 offline_page_2.original_url = GURL("https://example.com/bar");
809 store->AddOfflinePage(offline_page_2,
810 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
811 base::Unretained(this)));
812 PumpLoop();
813 EXPECT_EQ(ADD, last_called_callback_);
814 EXPECT_EQ(STATUS_TRUE, last_status_);
815
816 ClearResults();
817
818 // Get all pages from the store.
819 store->GetOfflinePages(
820 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
821 base::Unretained(this)));
822 PumpLoop();
823
824 EXPECT_EQ(LOAD, last_called_callback_);
825 EXPECT_EQ(2U, offline_pages_.size());
826
827 // Remove the offline page.
828 std::vector<int64_t> ids_to_remove;
829 ids_to_remove.push_back(offline_page_1.offline_id);
830 store->RemoveOfflinePages(
831 ids_to_remove, base::Bind(&OfflinePageMetadataStoreTest::UpdateCallback,
832 base::Unretained(this), REMOVE));
833 PumpLoop();
834 EXPECT_EQ(REMOVE, last_called_callback_);
835 EXPECT_EQ(STATUS_TRUE, last_status_);
836 ASSERT_TRUE(last_update_result() != nullptr);
837 EXPECT_EQ(1UL, last_update_result()->item_statuses.size());
838 EXPECT_EQ(ItemActionStatus::SUCCESS,
839 last_update_result()->item_statuses.begin()->second);
840 EXPECT_EQ(1UL, last_update_result()->updated_items.size());
841 EXPECT_EQ(offline_page_1, *(last_update_result()->updated_items.begin()));
842
843 ClearResults();
844
845 // Close and reload the store.
846 store.reset();
847 store = BuildStore();
848 store->GetOfflinePages(
849 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
850 base::Unretained(this)));
851 PumpLoop();
852
853 EXPECT_EQ(LOAD, last_called_callback_);
854 EXPECT_EQ(STATUS_TRUE, last_status_);
855 ASSERT_EQ(1U, offline_pages_.size());
856 EXPECT_EQ(offline_page_2.url, offline_pages_[0].url);
857 EXPECT_EQ(offline_page_2.offline_id, offline_pages_[0].offline_id);
858 EXPECT_EQ(offline_page_2.file_path, offline_pages_[0].file_path);
859 EXPECT_EQ(offline_page_2.file_size, offline_pages_[0].file_size);
860 EXPECT_EQ(offline_page_2.creation_time, offline_pages_[0].creation_time);
861 EXPECT_EQ(offline_page_2.last_access_time,
862 offline_pages_[0].last_access_time);
863 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count);
864 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id);
865 }
866
867 // Tests updating offline page metadata from the store.
868 TEST_F(OfflinePageMetadataStoreTest, UpdateOfflinePage) {
869 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
870
871 // First, adds a fresh page.
872 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
873 base::FilePath(kFilePath), kFileSize);
874 store->AddOfflinePage(offline_page,
875 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
876 base::Unretained(this)));
877 PumpLoop();
878 EXPECT_EQ(ADD, last_called_callback_);
879 EXPECT_EQ(STATUS_TRUE, last_status_);
880
881 ClearResults();
882 store->GetOfflinePages(
883 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
884 base::Unretained(this)));
885 PumpLoop();
886
887 EXPECT_EQ(LOAD, last_called_callback_);
888 ASSERT_EQ(1U, offline_pages_.size());
889 EXPECT_EQ(offline_page, offline_pages_[0]);
890
891 // Then update some data.
892 offline_page.file_size = kFileSize + 1;
893 offline_page.access_count++;
894 offline_page.original_url = GURL("https://example.com/bar");
895 std::vector<OfflinePageItem> items_to_update;
896 items_to_update.push_back(offline_page);
897 store->UpdateOfflinePages(
898 items_to_update, base::Bind(&OfflinePageMetadataStoreTest::UpdateCallback,
899 base::Unretained(this), UPDATE));
900 PumpLoop();
901 EXPECT_EQ(UPDATE, last_called_callback_);
902 EXPECT_EQ(STATUS_TRUE, last_status_);
903 ASSERT_TRUE(last_update_result() != nullptr);
904 EXPECT_EQ(1UL, last_update_result()->item_statuses.size());
905 EXPECT_EQ(ItemActionStatus::SUCCESS,
906 last_update_result()->item_statuses.begin()->second);
907 EXPECT_EQ(1UL, last_update_result()->updated_items.size());
908 EXPECT_EQ(offline_page, *(last_update_result()->updated_items.begin()));
909
910 ClearResults();
911 store->GetOfflinePages(
912 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
913 base::Unretained(this)));
914 PumpLoop();
915
916 EXPECT_EQ(LOAD, last_called_callback_);
917 ASSERT_EQ(1U, offline_pages_.size());
918 EXPECT_EQ(offline_page, offline_pages_[0]);
919 }
920
921 TEST_F(OfflinePageMetadataStoreTest, ClearAllOfflinePages) {
922 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
923
924 // Add 2 offline pages.
925 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1,
926 base::FilePath(kFilePath), kFileSize);
927 store->AddOfflinePage(offline_page,
928 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
929 base::Unretained(this)));
930 PumpLoop();
931 EXPECT_EQ(ADD, last_called_callback_);
932 EXPECT_EQ(STATUS_TRUE, last_status_);
933
934 ClearResults();
935
936 OfflinePageItem offline_page2(GURL("http://test.com"), 5678LL, kTestClientId2,
937 base::FilePath(kFilePath), kFileSize);
938 store->AddOfflinePage(offline_page2,
939 base::Bind(&OfflinePageMetadataStoreTest::AddCallback,
940 base::Unretained(this)));
941 PumpLoop();
942 EXPECT_EQ(ADD, last_called_callback_);
943 EXPECT_EQ(STATUS_TRUE, last_status_);
944
945 ClearResults();
946
947 // Get all pages from the store.
948 store->GetOfflinePages(
949 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
950 base::Unretained(this)));
951 PumpLoop();
952
953 EXPECT_EQ(LOAD, last_called_callback_);
954 EXPECT_EQ(2U, offline_pages_.size());
955
956 // Clear all records from the store.
957 store->Reset(base::Bind(&OfflinePageMetadataStoreTest::ResetCallback,
958 base::Unretained(this)));
959 PumpLoop();
960 EXPECT_EQ(RESET, last_called_callback_);
961 EXPECT_EQ(STATUS_TRUE, last_status_);
962
963 ClearResults();
964
965 // Get all pages from the store.
966 store->GetOfflinePages(
967 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
968 base::Unretained(this)));
969 PumpLoop();
970
971 EXPECT_EQ(LOAD, last_called_callback_);
972 ASSERT_EQ(0U, offline_pages_.size());
973 }
974
975 TEST_F(OfflinePageMetadataStoreTest, ResetStore) {
976 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
977
978 store->Reset(base::Bind(&OfflinePageMetadataStoreTest::ResetCallback,
979 base::Unretained(this)));
980 PumpLoop();
981 EXPECT_EQ(STATUS_TRUE, last_status_);
982 }
983
984 } // namespace
985 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_metadata_store.cc ('k') | components/offline_pages/offline_page_metadata_store_sql.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698