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