| OLD | NEW |
| 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 Loading... |
| 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, "status")); |
| 176 ASSERT_TRUE( |
| 177 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "user_initiated")); |
| 178 ASSERT_TRUE( |
| 179 connection.DoesColumnExist(OFFLINE_PAGES_TABLE_V1, "offline_url")); |
| 180 } |
| 181 |
| 131 class OfflinePageMetadataStoreFactory { | 182 class OfflinePageMetadataStoreFactory { |
| 132 public: | 183 public: |
| 133 OfflinePageMetadataStore* BuildStore(const base::FilePath& file_path) { | 184 OfflinePageMetadataStore* BuildStore(const base::FilePath& file_path) { |
| 134 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( | 185 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( |
| 135 base::ThreadTaskRunnerHandle::Get(), file_path); | 186 base::ThreadTaskRunnerHandle::Get(), file_path); |
| 136 return store; | 187 return store; |
| 137 } | 188 } |
| 138 | 189 |
| 139 OfflinePageMetadataStore* BuildStoreM52(const base::FilePath& file_path) { | 190 OfflinePageMetadataStore* BuildStoreM52(const base::FilePath& file_path) { |
| 140 BuildTestStoreWithSchemaFromM52(file_path); | 191 BuildTestStoreWithSchemaFromM52(file_path); |
| 141 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( | 192 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( |
| 142 base::ThreadTaskRunnerHandle::Get(), file_path); | 193 base::ThreadTaskRunnerHandle::Get(), file_path); |
| 143 return store; | 194 return store; |
| 144 } | 195 } |
| 145 | 196 |
| 146 OfflinePageMetadataStore* BuildStoreM53(const base::FilePath& file_path) { | 197 OfflinePageMetadataStore* BuildStoreM53(const base::FilePath& file_path) { |
| 147 BuildTestStoreWithSchemaFromM53(file_path); | 198 BuildTestStoreWithSchemaFromM53(file_path); |
| 148 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( | 199 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( |
| 149 base::ThreadTaskRunnerHandle::Get(), file_path); | 200 base::ThreadTaskRunnerHandle::Get(), file_path); |
| 150 return store; | 201 return store; |
| 151 } | 202 } |
| 203 |
| 204 OfflinePageMetadataStore* BuildStoreM54(const base::FilePath& file_path) { |
| 205 BuildTestStoreWithSchemaFromM54(file_path); |
| 206 OfflinePageMetadataStoreSQL* store = new OfflinePageMetadataStoreSQL( |
| 207 base::ThreadTaskRunnerHandle::Get(), file_path); |
| 208 return store; |
| 209 } |
| 152 }; | 210 }; |
| 153 | 211 |
| 154 enum CalledCallback { NONE, LOAD, ADD, REMOVE, RESET }; | 212 enum CalledCallback { NONE, LOAD, ADD, REMOVE, RESET }; |
| 155 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; | 213 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; |
| 156 | 214 |
| 157 class OfflinePageMetadataStoreTest : public testing::Test { | 215 class OfflinePageMetadataStoreTest : public testing::Test { |
| 158 public: | 216 public: |
| 159 OfflinePageMetadataStoreTest(); | 217 OfflinePageMetadataStoreTest(); |
| 160 ~OfflinePageMetadataStoreTest() override; | 218 ~OfflinePageMetadataStoreTest() override; |
| 161 | 219 |
| 162 void TearDown() override { | 220 void TearDown() override { |
| 163 // Wait for all the pieces of the store to delete itself properly. | 221 // Wait for all the pieces of the store to delete itself properly. |
| 164 PumpLoop(); | 222 PumpLoop(); |
| 165 } | 223 } |
| 166 | 224 |
| 167 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); | 225 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); |
| 168 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM52(); | 226 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM52(); |
| 169 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM53(); | 227 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM53(); |
| 228 std::unique_ptr<OfflinePageMetadataStore> BuildStoreWithSchemaFromM54(); |
| 170 | 229 |
| 171 void PumpLoop(); | 230 void PumpLoop(); |
| 172 | 231 |
| 173 void GetOfflinePagesCallback( | 232 void GetOfflinePagesCallback( |
| 174 OfflinePageMetadataStore::LoadStatus load_status, | 233 OfflinePageMetadataStore::LoadStatus load_status, |
| 175 const std::vector<OfflinePageItem>& offline_pages); | 234 const std::vector<OfflinePageItem>& offline_pages); |
| 176 void UpdateCallback(CalledCallback called_callback, bool success); | 235 void UpdateCallback(CalledCallback called_callback, bool success); |
| 177 | 236 |
| 178 void ClearResults(); | 237 void ClearResults(); |
| 179 | 238 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM53() { | 362 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM53() { |
| 304 std::unique_ptr<OfflinePageMetadataStore> store( | 363 std::unique_ptr<OfflinePageMetadataStore> store( |
| 305 factory_.BuildStoreM53(temp_directory_.path())); | 364 factory_.BuildStoreM53(temp_directory_.path())); |
| 306 store->GetOfflinePages( | 365 store->GetOfflinePages( |
| 307 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, | 366 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, |
| 308 base::Unretained(this))); | 367 base::Unretained(this))); |
| 309 PumpLoop(); | 368 PumpLoop(); |
| 310 return store; | 369 return store; |
| 311 } | 370 } |
| 312 | 371 |
| 372 std::unique_ptr<OfflinePageMetadataStore> |
| 373 OfflinePageMetadataStoreTest::BuildStoreWithSchemaFromM54() { |
| 374 std::unique_ptr<OfflinePageMetadataStore> store( |
| 375 factory_.BuildStoreM53(temp_directory_.path())); |
| 376 store->GetOfflinePages( |
| 377 base::Bind(&OfflinePageMetadataStoreTest::GetOfflinePagesCallback, |
| 378 base::Unretained(this))); |
| 379 PumpLoop(); |
| 380 return store; |
| 381 } |
| 382 |
| 313 // Loads empty store and makes sure that there are no offline pages stored in | 383 // Loads empty store and makes sure that there are no offline pages stored in |
| 314 // it. | 384 // it. |
| 315 TEST_F(OfflinePageMetadataStoreTest, LoadEmptyStore) { | 385 TEST_F(OfflinePageMetadataStoreTest, LoadEmptyStore) { |
| 316 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); | 386 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); |
| 317 EXPECT_EQ(LOAD, last_called_callback_); | 387 EXPECT_EQ(LOAD, last_called_callback_); |
| 318 EXPECT_EQ(STATUS_TRUE, last_status_); | 388 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 319 EXPECT_EQ(0U, offline_pages_.size()); | 389 EXPECT_EQ(0U, offline_pages_.size()); |
| 320 } | 390 } |
| 321 | 391 |
| 322 // Loads a store which has an outdated schema. | 392 // Loads a store which has an outdated schema. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 340 BuildStoreWithSchemaFromM53()); | 410 BuildStoreWithSchemaFromM53()); |
| 341 | 411 |
| 342 OfflinePageItem item = CheckThatStoreHasOneItem(); | 412 OfflinePageItem item = CheckThatStoreHasOneItem(); |
| 343 // We should have a valid expiration time after upgrade. | 413 // We should have a valid expiration time after upgrade. |
| 344 EXPECT_NE(base::Time::FromInternalValue(0), | 414 EXPECT_NE(base::Time::FromInternalValue(0), |
| 345 offline_pages_[0].expiration_time); | 415 offline_pages_[0].expiration_time); |
| 346 | 416 |
| 347 CheckThatOfflinePageCanBeSaved(std::move(store)); | 417 CheckThatOfflinePageCanBeSaved(std::move(store)); |
| 348 } | 418 } |
| 349 | 419 |
| 420 // Loads a string with schema from M54. |
| 421 // Because for now we only reduce the number of fields it just makes sure there |
| 422 // are no crashes in the process. |
| 423 // TODO(romax): Move this to sql_unittest. |
| 424 TEST_F(OfflinePageMetadataStoreTest, LoadVersion54Store) { |
| 425 std::unique_ptr<OfflinePageMetadataStore> store( |
| 426 BuildStoreWithSchemaFromM54()); |
| 427 |
| 428 OfflinePageItem item = CheckThatStoreHasOneItem(); |
| 429 |
| 430 CheckThatOfflinePageCanBeSaved(std::move(store)); |
| 431 } |
| 432 |
| 350 // Adds metadata of an offline page into a store and then opens the store | 433 // 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. | 434 // again to make sure that stored metadata survives store restarts. |
| 352 TEST_F(OfflinePageMetadataStoreTest, AddOfflinePage) { | 435 TEST_F(OfflinePageMetadataStoreTest, AddOfflinePage) { |
| 353 CheckThatOfflinePageCanBeSaved(BuildStore()); | 436 CheckThatOfflinePageCanBeSaved(BuildStore()); |
| 354 } | 437 } |
| 355 | 438 |
| 356 // Tests removing offline page metadata from the store, for which it first adds | 439 // Tests removing offline page metadata from the store, for which it first adds |
| 357 // metadata of an offline page. | 440 // metadata of an offline page. |
| 358 TEST_F(OfflinePageMetadataStoreTest, RemoveOfflinePage) { | 441 TEST_F(OfflinePageMetadataStoreTest, RemoveOfflinePage) { |
| 359 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); | 442 std::unique_ptr<OfflinePageMetadataStore> store(BuildStore()); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 base::Unretained(this))); | 684 base::Unretained(this))); |
| 602 PumpLoop(); | 685 PumpLoop(); |
| 603 | 686 |
| 604 EXPECT_EQ(LOAD, last_called_callback_); | 687 EXPECT_EQ(LOAD, last_called_callback_); |
| 605 EXPECT_EQ(STATUS_TRUE, last_status_); | 688 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 606 ASSERT_EQ(0U, offline_pages_.size()); | 689 ASSERT_EQ(0U, offline_pages_.size()); |
| 607 } | 690 } |
| 608 | 691 |
| 609 } // namespace | 692 } // namespace |
| 610 } // namespace offline_pages | 693 } // namespace offline_pages |
| OLD | NEW |