| 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_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/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "components/leveldb_proto/proto_database_impl.h" | 17 #include "components/leveldb_proto/proto_database_impl.h" |
| 18 #include "components/offline_pages/offline_page_item.h" | 18 #include "components/offline_pages/offline_page_item.h" |
| 19 #include "components/offline_pages/offline_page_model.h" | 19 #include "components/offline_pages/offline_page_model.h" |
| 20 #include "components/offline_pages/proto/offline_pages.pb.h" | 20 #include "components/offline_pages/proto/offline_pages.pb.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using leveldb_proto::ProtoDatabaseImpl; | 23 using leveldb_proto::ProtoDatabaseImpl; |
| 24 | 24 |
| 25 namespace offline_pages { | 25 namespace offline_pages { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const char kTestClientNamespace[] = "CLIENT_NAMESPACE"; |
| 29 const char kTestURL[] = "https://example.com"; | 30 const char kTestURL[] = "https://example.com"; |
| 30 const ClientId kTestBookmarkId(BOOKMARK_NAMESPACE, "1234"); | 31 const ClientId kTestClientId1(kTestClientNamespace, "1234"); |
| 31 const ClientId kTestBookmarkId2(BOOKMARK_NAMESPACE, "5678"); | 32 const ClientId kTestClientId2(kTestClientNamespace, "5678"); |
| 32 const base::FilePath::CharType kFilePath[] = | 33 const base::FilePath::CharType kFilePath[] = |
| 33 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml"); | 34 FILE_PATH_LITERAL("/offline_pages/example_com.mhtml"); |
| 34 int64_t kFileSize = 234567; | 35 int64_t kFileSize = 234567; |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| 37 | 38 |
| 38 class OfflinePageMetadataStoreImplTest : public testing::Test { | 39 class OfflinePageMetadataStoreImplTest : public testing::Test { |
| 39 public: | 40 public: |
| 40 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY }; | 41 enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY }; |
| 41 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; | 42 enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE }; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 EXPECT_EQ(LOAD, last_called_callback_); | 140 EXPECT_EQ(LOAD, last_called_callback_); |
| 140 EXPECT_EQ(STATUS_TRUE, last_status_); | 141 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 141 EXPECT_EQ(0U, offline_pages_.size()); | 142 EXPECT_EQ(0U, offline_pages_.size()); |
| 142 } | 143 } |
| 143 | 144 |
| 144 // Adds metadata of an offline page into a store and then opens the store | 145 // Adds metadata of an offline page into a store and then opens the store |
| 145 // again to make sure that stored metadata survives store restarts. | 146 // again to make sure that stored metadata survives store restarts. |
| 146 TEST_F(OfflinePageMetadataStoreImplTest, AddOfflinePage) { | 147 TEST_F(OfflinePageMetadataStoreImplTest, AddOfflinePage) { |
| 147 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); | 148 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); |
| 148 | 149 |
| 149 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, | 150 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1, |
| 150 base::FilePath(kFilePath), kFileSize); | 151 base::FilePath(kFilePath), kFileSize); |
| 151 store->AddOrUpdateOfflinePage( | 152 store->AddOrUpdateOfflinePage( |
| 152 offline_page, | 153 offline_page, |
| 153 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, | 154 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, |
| 154 base::Unretained(this), ADD)); | 155 base::Unretained(this), ADD)); |
| 155 PumpLoop(); | 156 PumpLoop(); |
| 156 EXPECT_EQ(ADD, last_called_callback_); | 157 EXPECT_EQ(ADD, last_called_callback_); |
| 157 EXPECT_EQ(STATUS_TRUE, last_status_); | 158 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 158 | 159 |
| 159 ClearResults(); | 160 ClearResults(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 176 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); | 177 EXPECT_EQ(offline_page.access_count, offline_pages_[0].access_count); |
| 177 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); | 178 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); |
| 178 } | 179 } |
| 179 | 180 |
| 180 // Tests removing offline page metadata from the store, for which it first adds | 181 // Tests removing offline page metadata from the store, for which it first adds |
| 181 // metadata of an offline page. | 182 // metadata of an offline page. |
| 182 TEST_F(OfflinePageMetadataStoreImplTest, RemoveOfflinePage) { | 183 TEST_F(OfflinePageMetadataStoreImplTest, RemoveOfflinePage) { |
| 183 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); | 184 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); |
| 184 | 185 |
| 185 // Add an offline page. | 186 // Add an offline page. |
| 186 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, | 187 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1, |
| 187 base::FilePath(kFilePath), kFileSize); | 188 base::FilePath(kFilePath), kFileSize); |
| 188 store->AddOrUpdateOfflinePage( | 189 store->AddOrUpdateOfflinePage( |
| 189 offline_page, | 190 offline_page, |
| 190 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, | 191 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, |
| 191 base::Unretained(this), ADD)); | 192 base::Unretained(this), ADD)); |
| 192 PumpLoop(); | 193 PumpLoop(); |
| 193 EXPECT_EQ(ADD, last_called_callback_); | 194 EXPECT_EQ(ADD, last_called_callback_); |
| 194 EXPECT_EQ(STATUS_TRUE, last_status_); | 195 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 195 | 196 |
| 196 ClearResults(); | 197 ClearResults(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 EXPECT_EQ(LOAD, last_called_callback_); | 231 EXPECT_EQ(LOAD, last_called_callback_); |
| 231 EXPECT_EQ(STATUS_TRUE, last_status_); | 232 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 232 EXPECT_EQ(0U, offline_pages_.size()); | 233 EXPECT_EQ(0U, offline_pages_.size()); |
| 233 } | 234 } |
| 234 | 235 |
| 235 // Adds metadata of multiple offline pages into a store and removes some. | 236 // Adds metadata of multiple offline pages into a store and removes some. |
| 236 TEST_F(OfflinePageMetadataStoreImplTest, AddRemoveMultipleOfflinePages) { | 237 TEST_F(OfflinePageMetadataStoreImplTest, AddRemoveMultipleOfflinePages) { |
| 237 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); | 238 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); |
| 238 | 239 |
| 239 // Add an offline page. | 240 // Add an offline page. |
| 240 OfflinePageItem offline_page_1(GURL(kTestURL), 12345LL, kTestBookmarkId, | 241 OfflinePageItem offline_page_1(GURL(kTestURL), 12345LL, kTestClientId1, |
| 241 base::FilePath(kFilePath), kFileSize); | 242 base::FilePath(kFilePath), kFileSize); |
| 242 base::FilePath file_path_2 = | 243 base::FilePath file_path_2 = |
| 243 base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml")); | 244 base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml")); |
| 244 OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL, | 245 OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL, |
| 245 kTestBookmarkId2, file_path_2, 12345, | 246 kTestClientId2, file_path_2, 12345, |
| 246 base::Time::Now()); | 247 base::Time::Now()); |
| 247 store->AddOrUpdateOfflinePage( | 248 store->AddOrUpdateOfflinePage( |
| 248 offline_page_1, | 249 offline_page_1, |
| 249 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, | 250 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, |
| 250 base::Unretained(this), ADD)); | 251 base::Unretained(this), ADD)); |
| 251 PumpLoop(); | 252 PumpLoop(); |
| 252 EXPECT_EQ(ADD, last_called_callback_); | 253 EXPECT_EQ(ADD, last_called_callback_); |
| 253 EXPECT_EQ(STATUS_TRUE, last_status_); | 254 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 254 | 255 |
| 255 ClearResults(); | 256 ClearResults(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 offline_pages_[0].last_access_time); | 308 offline_pages_[0].last_access_time); |
| 308 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count); | 309 EXPECT_EQ(offline_page_2.access_count, offline_pages_[0].access_count); |
| 309 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id); | 310 EXPECT_EQ(offline_page_2.client_id, offline_pages_[0].client_id); |
| 310 } | 311 } |
| 311 | 312 |
| 312 // Tests updating offline page metadata from the store. | 313 // Tests updating offline page metadata from the store. |
| 313 TEST_F(OfflinePageMetadataStoreImplTest, UpdateOfflinePage) { | 314 TEST_F(OfflinePageMetadataStoreImplTest, UpdateOfflinePage) { |
| 314 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); | 315 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); |
| 315 | 316 |
| 316 // First, adds a fresh page. | 317 // First, adds a fresh page. |
| 317 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, | 318 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1, |
| 318 base::FilePath(kFilePath), kFileSize); | 319 base::FilePath(kFilePath), kFileSize); |
| 319 store->AddOrUpdateOfflinePage( | 320 store->AddOrUpdateOfflinePage( |
| 320 offline_page, | 321 offline_page, |
| 321 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, | 322 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, |
| 322 base::Unretained(this), ADD)); | 323 base::Unretained(this), ADD)); |
| 323 PumpLoop(); | 324 PumpLoop(); |
| 324 EXPECT_EQ(ADD, last_called_callback_); | 325 EXPECT_EQ(ADD, last_called_callback_); |
| 325 EXPECT_EQ(STATUS_TRUE, last_status_); | 326 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 326 | 327 |
| 327 ClearResults(); | 328 ClearResults(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); | 373 EXPECT_EQ(offline_page.client_id, offline_pages_[0].client_id); |
| 373 } | 374 } |
| 374 | 375 |
| 375 // Test that loading a store with a bad value still loads. | 376 // Test that loading a store with a bad value still loads. |
| 376 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST | 377 // Needs to be outside of the anonymous namespace in order for FRIEND_TEST |
| 377 // to work. | 378 // to work. |
| 378 TEST_F(OfflinePageMetadataStoreImplTest, LoadCorruptedStore) { | 379 TEST_F(OfflinePageMetadataStoreImplTest, LoadCorruptedStore) { |
| 379 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); | 380 scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore()); |
| 380 | 381 |
| 381 // Write one ok page. | 382 // Write one ok page. |
| 382 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestBookmarkId, | 383 OfflinePageItem offline_page(GURL(kTestURL), 1234LL, kTestClientId1, |
| 383 base::FilePath(kFilePath), kFileSize); | 384 base::FilePath(kFilePath), kFileSize); |
| 384 store->AddOrUpdateOfflinePage( | 385 store->AddOrUpdateOfflinePage( |
| 385 offline_page, | 386 offline_page, |
| 386 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, | 387 base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback, |
| 387 base::Unretained(this), ADD)); | 388 base::Unretained(this), ADD)); |
| 388 PumpLoop(); | 389 PumpLoop(); |
| 389 EXPECT_EQ(ADD, last_called_callback_); | 390 EXPECT_EQ(ADD, last_called_callback_); |
| 390 EXPECT_EQ(STATUS_TRUE, last_status_); | 391 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 391 | 392 |
| 392 // Manually write one broken page (no id) | 393 // Manually write one broken page (no id) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 // Close the store first to ensure file lock is removed. | 484 // Close the store first to ensure file lock is removed. |
| 484 store.reset(); | 485 store.reset(); |
| 485 store = BuildStore(); | 486 store = BuildStore(); |
| 486 PumpLoop(); | 487 PumpLoop(); |
| 487 | 488 |
| 488 // The page should be upgraded with new Client ID format. | 489 // The page should be upgraded with new Client ID format. |
| 489 EXPECT_EQ(LOAD, last_called_callback_); | 490 EXPECT_EQ(LOAD, last_called_callback_); |
| 490 EXPECT_EQ(STATUS_TRUE, last_status_); | 491 EXPECT_EQ(STATUS_TRUE, last_status_); |
| 491 EXPECT_EQ(1U, offline_pages_.size()); | 492 EXPECT_EQ(1U, offline_pages_.size()); |
| 492 EXPECT_TRUE(offline_pages_[0].offline_id != 0); | 493 EXPECT_TRUE(offline_pages_[0].offline_id != 0); |
| 493 EXPECT_EQ(offline_pages::BOOKMARK_NAMESPACE, | 494 EXPECT_EQ(offline_pages::kBookmarkNamespace, |
| 494 offline_pages_[0].client_id.name_space); | 495 offline_pages_[0].client_id.name_space); |
| 495 EXPECT_EQ(base::Int64ToString(offline_page_proto.deprecated_bookmark_id()), | 496 EXPECT_EQ(base::Int64ToString(offline_page_proto.deprecated_bookmark_id()), |
| 496 offline_pages_[0].client_id.id); | 497 offline_pages_[0].client_id.id); |
| 497 EXPECT_EQ(GURL(kTestURL), offline_pages_[0].url); | 498 EXPECT_EQ(GURL(kTestURL), offline_pages_[0].url); |
| 498 EXPECT_EQ(offline_page_proto.version(), offline_pages_[0].version); | 499 EXPECT_EQ(offline_page_proto.version(), offline_pages_[0].version); |
| 499 EXPECT_EQ(offline_page_proto.file_path(), | 500 EXPECT_EQ(offline_page_proto.file_path(), |
| 500 offline_pages_[0].file_path.MaybeAsASCII()); | 501 offline_pages_[0].file_path.MaybeAsASCII()); |
| 501 } | 502 } |
| 502 | 503 |
| 503 } // namespace offline_pages | 504 } // namespace offline_pages |
| OLD | NEW |