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

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

Issue 2336813002: [Offline pages] OPMStoreSQL: Upgrade to M55, Upgrade refactoring, conversion refactoring (Closed)
Patch Set: Removing the version field Created 4 years, 3 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.h" 5 #include "components/offline_pages/offline_page_metadata_store.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 statement.BindCString(6, kTestClientNamespace); 121 statement.BindCString(6, kTestClientNamespace);
122 statement.BindString(7, kTestClientId2.id); 122 statement.BindString(7, kTestClientId2.id);
123 statement.BindCString(8, kTestURL); 123 statement.BindCString(8, kTestURL);
124 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII()); 124 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII());
125 statement.BindInt64(10, base::Time::Now().ToInternalValue()); 125 statement.BindInt64(10, base::Time::Now().ToInternalValue());
126 ASSERT_TRUE(statement.Run()); 126 ASSERT_TRUE(statement.Run());
127 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1)); 127 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
128 ASSERT_FALSE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "title")); 128 ASSERT_FALSE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "title"));
129 } 129 }
130 130
131 void BuildTestStoreWithSchemaFromM54(const base::FilePath& file) {
132 sql::Connection connection;
133 ASSERT_TRUE(
134 connection.Open(file.Append(FILE_PATH_LITERAL("OfflinePages.db"))));
135 ASSERT_TRUE(connection.is_open());
136 ASSERT_TRUE(connection.BeginTransaction());
137 ASSERT_TRUE(connection.Execute("CREATE TABLE " OFFLINE_PAGES_TABLE_V1
138 "(offline_id INTEGER PRIMARY KEY NOT NULL, "
139 "creation_time INTEGER NOT NULL, "
140 "file_size INTEGER NOT NULL, "
141 "version INTEGER NOT NULL, "
142 "last_access_time INTEGER NOT NULL, "
143 "access_count INTEGER NOT NULL, "
144 "status INTEGER NOT NULL DEFAULT 0, "
145 "user_initiated INTEGER, "
146 "expiration_time INTEGER NOT NULL DEFAULT 0, "
147 "client_namespace VARCHAR NOT NULL, "
148 "client_id VARCHAR NOT NULL, "
149 "online_url VARCHAR NOT NULL, "
150 "offline_url VARCHAR NOT NULL DEFAULT '', "
151 "file_path VARCHAR NOT NULL "
152 "title VARCHAR NOT NULL DEFAULT ''"
153 ")"));
154 ASSERT_TRUE(connection.CommitTransaction());
155 sql::Statement statement(connection.GetUniqueStatement(
156 "INSERT INTO " OFFLINE_PAGES_TABLE_V1
157 "(offline_id, creation_time, file_size, version, "
158 "last_access_time, access_count, client_namespace, "
159 "client_id, online_url, file_path, expiration_time, title) "
160 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
161 statement.BindInt64(0, kOfflineId);
162 statement.BindInt(1, 0);
163 statement.BindInt64(2, kFileSize);
164 statement.BindInt(3, 0);
165 statement.BindInt(4, 0);
166 statement.BindInt(5, 1);
167 statement.BindCString(6, kTestClientNamespace);
168 statement.BindString(7, kTestClientId2.id);
169 statement.BindCString(8, kTestURL);
170 statement.BindString(9, base::FilePath(kFilePath).MaybeAsASCII());
171 statement.BindInt64(10, base::Time::Now().ToInternalValue());
172 statement.BindString16(11, base::UTF8ToUTF16("Test title"));
173 ASSERT_TRUE(statement.Run());
174 ASSERT_TRUE(connection.DoesTableExist(OFFLINE_PAGES_TABLE_V1));
175 ASSERT_TRUE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "version"));
176 ASSERT_TRUE(connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "status"));
177 ASSERT_TRUE(
178 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "user_initiated"));
179 ASSERT_TRUE(
180 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "offline_url"));
181 }
182
131 class OfflinePageMetadataStoreFactory { 183 class OfflinePageMetadataStoreFactory {
132 public: 184 public:
133 OfflinePageMetadataStore* BuildStore(const base::FilePath& file_path) { 185 OfflinePageMetadataStore* BuildStore(const base::FilePath& file_path) {
134 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( 186 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
135 base::ThreadTaskRunnerHandle::Get(), file_path); 187 base::ThreadTaskRunnerHandle::Get(), file_path);
136 return store; 188 return store;
137 } 189 }
138 190
139 OfflinePageMetadataStore* BuildStoreM52(const base::FilePath& file_path) { 191 OfflinePageMetadataStore* BuildStoreM52(const base::FilePath& file_path) {
140 BuildTestStoreWithSchemaFromM52(file_path); 192 BuildTestStoreWithSchemaFromM52(file_path);
141 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( 193 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
142 base::ThreadTaskRunnerHandle::Get(), file_path); 194 base::ThreadTaskRunnerHandle::Get(), file_path);
143 return store; 195 return store;
144 } 196 }
145 197
146 OfflinePageMetadataStore* BuildStoreM53(const base::FilePath& file_path) { 198 OfflinePageMetadataStore* BuildStoreM53(const base::FilePath& file_path) {
147 BuildTestStoreWithSchemaFromM53(file_path); 199 BuildTestStoreWithSchemaFromM53(file_path);
148 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( 200 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
149 base::ThreadTaskRunnerHandle::Get(), file_path); 201 base::ThreadTaskRunnerHandle::Get(), file_path);
150 return store; 202 return store;
151 } 203 }
204
205 OfflinePageMetadataStore* BuildStoreM54(const base::FilePath& file_path) {
206 BuildTestStoreWithSchemaFromM54(file_path);
207 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL(
208 base::ThreadTaskRunnerHandle::Get(), file_path);
209 return store;
210 }
152 }; 211 };
153 212
154 enum CalledCallback { NONE, LOAD, ADD, REMOVE, RESET }; 213 enum CalledCallback { NONE, LOAD, ADD, REMOVE, RESET };
155 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; 214 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE };
156 215
157 class OfflinePageMetadataStoreTest : public testing::Test { 216 class OfflinePageMetadataStoreTest : public testing::Test {
158 public: 217 public:
159 OfflinePageMetadataStoreTest(); 218 OfflinePageMetadataStoreTest();
160 ~OfflinePageMetadataStoreTest() override; 219 ~OfflinePageMetadataStoreTest() override;
161 220
162 void TearDown() override { 221 void TearDown() override {
163 // Wait for all the pieces of the store to delete itself properly. 222 // Wait for all the pieces of the store to delete itself properly.
164 PumpLoop(); 223 PumpLoop();
165 } 224 }
166 225
167 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); 226 std::unique_ptr<OfflinePageMetadataStore> BuildStore();
168 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM52(); 227 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM52();
169 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM53(); 228 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM53();
229 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM54();
170 230
171 void PumpLoop(); 231 void PumpLoop();
172 232
173 void GetOfflinePagesCallback( 233 void GetOfflinePagesCallback(
174 OfflinePageMetadataStore::LoadStatus load_status, 234 OfflinePageMetadataStore::LoadStatus load_status,
175 const std::vector<OfflinePageItem>& offline_pages); 235 const std::vector<OfflinePageItem>& offline_pages);
176 void UpdateCallback(CalledCallback called_callback, bool success); 236 void UpdateCallback(CalledCallback called_callback, bool success);
177 237
178 void ClearResults(); 238 void ClearResults();
179 239
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM53() { 363 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM53() {
304 std::unique_ptr<OfflinePageMetadataStore> store( 364 std::unique_ptr<OfflinePageMetadataStore> store(
305 factory_.BuildStoreM53(temp_directory_.path())); 365 factory_.BuildStoreM53(temp_directory_.path()));
306 store->GetOfflinePages( 366 store->GetOfflinePages(
307 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, 367 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
308 base::Unretained(this))); 368 base::Unretained(this)));
309 PumpLoop(); 369 PumpLoop();
310 return store; 370 return store;
311 } 371 }
312 372
373 std::unique_ptr<OfflinePageMetadataStore>
374 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM54() {
375 std::unique_ptr<OfflinePageMetadataStore> store(
376 factory_.BuildStoreM53(temp_directory_.path()));
377 store->GetOfflinePages(
378 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
379 base::Unretained(this)));
380 PumpLoop();
381 return store;
382 }
383
313 // Loads empty store and makes sure that there are no offline pages stored in 384 // Loads empty store and makes sure that there are no offline pages stored in
314 // it. 385 // it.
315 TEST_F(OfflinePageMetadataStoreTest, LoadEmptyStore) { 386 TEST_F(OfflinePageMetadataStoreTest, LoadEmptyStore) {
316 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); 387 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
317 EXPECT_EQ(LOAD, last_called_callback_); 388 EXPECT_EQ(LOAD, last_called_callback_);
318 EXPECT_EQ(STATUS_TRUE, last_status_); 389 EXPECT_EQ(STATUS_TRUE, last_status_);
319 EXPECT_EQ(0U, offline_pages_.size()); 390 EXPECT_EQ(0U, offline_pages_.size());
320 } 391 }
321 392
322 // Loads a store which has an outdated schema. 393 // Loads a store which has an outdated schema.
(...skipping 17 matching lines...) Expand all
340 BuildStoreWithSchemaFromM53()); 411 BuildStoreWithSchemaFromM53());
341 412
342 OfflinePageItem item = CheckThatStoreHasOneItem(); 413 OfflinePageItem item = CheckThatStoreHasOneItem();
343 // We should have a valid expiration time after upgrade. 414 // We should have a valid expiration time after upgrade.
344 EXPECT_NE(base::Time::FromInternalValue(0), 415 EXPECT_NE(base::Time::FromInternalValue(0),
345 offline_pages_[0].expiration_time); 416 offline_pages_[0].expiration_time);
346 417
347 CheckThatOfflinePageCanBeSaved(std::move(store)); 418 CheckThatOfflinePageCanBeSaved(std::move(store));
348 } 419 }
349 420
421 // Loads a string with schema from M54.
422 // Because for now we only reduce the number of fields it just makes sure there
423 // are no crashes in the process.
424 // TODO(romax): Move this to sql_unittest.
dewittj 2016/09/13 18:10:07 I'm sure he will be happy about more todos :)
romax 2016/09/13 18:32:14 :S
fgorski 2016/09/13 21:40:21 Acknowledged.
425 TEST_F(OfflinePageMetadataStoreTest, LoadVersion54Store) {
426 std::unique_ptr<OfflinePageMetadataStore> store(
427 BuildStoreWithSchemaFromM54());
428
429 OfflinePageItem item = CheckThatStoreHasOneItem();
430
431 CheckThatOfflinePageCanBeSaved(std::move(store));
432 }
433
350 // Adds metadata of an offline page into a store and then opens the store 434 // Adds metadata of an offline page into a store and then opens the store
351 // again to make sure that stored metadata survives store restarts. 435 // again to make sure that stored metadata survives store restarts.
352 TEST_F(OfflinePageMetadataStoreTest, AddOfflinePage) { 436 TEST_F(OfflinePageMetadataStoreTest, AddOfflinePage) {
353 CheckThatOfflinePageCanBeSaved(BuildStore()); 437 CheckThatOfflinePageCanBeSaved(BuildStore());
354 } 438 }
355 439
356 // Tests removing offline page metadata from the store, for which it first adds 440 // Tests removing offline page metadata from the store, for which it first adds
357 // metadata of an offline page. 441 // metadata of an offline page.
358 TEST_F(OfflinePageMetadataStoreTest, RemoveOfflinePage) { 442 TEST_F(OfflinePageMetadataStoreTest, RemoveOfflinePage) {
359 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); 443 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore());
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 store->GetOfflinePages( 552 store->GetOfflinePages(
469 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, 553 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
470 base::Unretained(this))); 554 base::Unretained(this)));
471 PumpLoop(); 555 PumpLoop();
472 556
473 EXPECT_EQ(LOAD, last_called_callback_); 557 EXPECT_EQ(LOAD, last_called_callback_);
474 EXPECT_EQ(STATUS_TRUE, last_status_); 558 EXPECT_EQ(STATUS_TRUE, last_status_);
475 ASSERT_EQ(1U, offline_pages_.size()); 559 ASSERT_EQ(1U, offline_pages_.size());
476 EXPECT_EQ(offline_page_2.url, offline_pages_[0].url); 560 EXPECT_EQ(offline_page_2.url, offline_pages_[0].url);
477 EXPECT_EQ(offline_page_2.offline_id, offline_pages_[0].offline_id); 561 EXPECT_EQ(offline_page_2.offline_id, offline_pages_[0].offline_id);
478 EXPECT_EQ(offline_page_2.version, offline_pages_[0].version);
479 EXPECT_EQ(offline_page_2.file_path, offline_pages_[0].file_path); 562 EXPECT_EQ(offline_page_2.file_path, offline_pages_[0].file_path);
480 EXPECT_EQ(offline_page_2.file_size, offline_pages_[0].file_size); 563 EXPECT_EQ(offline_page_2.file_size, offline_pages_[0].file_size);
481 EXPECT_EQ(offline_page_2.creation_time, offline_pages_[0].creation_time); 564 EXPECT_EQ(offline_page_2.creation_time, offline_pages_[0].creation_time);
482 EXPECT_EQ(offline_page_2.last_access_time, 565 EXPECT_EQ(offline_page_2.last_access_time,
483 offline_pages_[0].last_access_time); 566 offline_pages_[0].last_access_time);
484 EXPECT_EQ(offline_page_2.expiration_time, offline_pages_[0].expiration_time); 567 EXPECT_EQ(offline_page_2.expiration_time, offline_pages_[0].expiration_time);
485 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count); 568 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count);
486 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id); 569 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id);
487 } 570 }
488 571
(...skipping 15 matching lines...) Expand all
504 store->GetOfflinePages( 587 store->GetOfflinePages(
505 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, 588 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
506 base::Unretained(this))); 589 base::Unretained(this)));
507 PumpLoop(); 590 PumpLoop();
508 591
509 EXPECT_EQ(LOAD, last_called_callback_); 592 EXPECT_EQ(LOAD, last_called_callback_);
510 EXPECT_EQ(STATUS_TRUE, last_status_); 593 EXPECT_EQ(STATUS_TRUE, last_status_);
511 ASSERT_EQ(1U, offline_pages_.size()); 594 ASSERT_EQ(1U, offline_pages_.size());
512 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 595 EXPECT_EQ(offline_page.url, offline_pages_[0].url);
513 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 596 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id);
514 EXPECT_EQ(offline_page.version, offline_pages_[0].version);
515 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 597 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path);
516 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 598 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size);
517 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 599 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time);
518 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 600 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time);
519 EXPECT_EQ(offline_page.expiration_time, offline_pages_[0].expiration_time); 601 EXPECT_EQ(offline_page.expiration_time, offline_pages_[0].expiration_time);
520 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 602 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count);
521 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); 603 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id);
522 604
523 // Then update some data. 605 // Then update some data.
524 offline_page.file_size = kFileSize + 1; 606 offline_page.file_size = kFileSize + 1;
(...skipping 10 matching lines...) Expand all
535 store->GetOfflinePages( 617 store->GetOfflinePages(
536 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, 618 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback,
537 base::Unretained(this))); 619 base::Unretained(this)));
538 PumpLoop(); 620 PumpLoop();
539 621
540 EXPECT_EQ(LOAD, last_called_callback_); 622 EXPECT_EQ(LOAD, last_called_callback_);
541 EXPECT_EQ(STATUS_TRUE, last_status_); 623 EXPECT_EQ(STATUS_TRUE, last_status_);
542 ASSERT_EQ(1U, offline_pages_.size()); 624 ASSERT_EQ(1U, offline_pages_.size());
543 EXPECT_EQ(offline_page.url, offline_pages_[0].url); 625 EXPECT_EQ(offline_page.url, offline_pages_[0].url);
544 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id); 626 EXPECT_EQ(offline_page.offline_id, offline_pages_[0].offline_id);
545 EXPECT_EQ(offline_page.version, offline_pages_[0].version);
546 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path); 627 EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path);
547 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size); 628 EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size);
548 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time); 629 EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time);
549 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time); 630 EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time);
550 EXPECT_EQ(offline_page.expiration_time, offline_pages_[0].expiration_time); 631 EXPECT_EQ(offline_page.expiration_time, offline_pages_[0].expiration_time);
551 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); 632 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count);
552 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); 633 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id);
553 } 634 }
554 635
555 TEST_F(OfflinePageMetadataStoreTest, ClearAllOfflinePages) { 636 TEST_F(OfflinePageMetadataStoreTest, ClearAllOfflinePages) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 base::Unretained(this))); 682 base::Unretained(this)));
602 PumpLoop(); 683 PumpLoop();
603 684
604 EXPECT_EQ(LOAD, last_called_callback_); 685 EXPECT_EQ(LOAD, last_called_callback_);
605 EXPECT_EQ(STATUS_TRUE, last_status_); 686 EXPECT_EQ(STATUS_TRUE, last_status_);
606 ASSERT_EQ(0U, offline_pages_.size()); 687 ASSERT_EQ(0U, offline_pages_.size());
607 } 688 }
608 689
609 } // namespace 690 } // namespace
610 } // namespace offline_pages 691 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698