| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 EXPECT_TRUE(area->GetItem(kKey).is_null()); | 140 EXPECT_TRUE(area->GetItem(kKey).is_null()); |
| 141 EXPECT_FALSE(area->SetItem(kKey, kValue, &old_nullable_value)); | 141 EXPECT_FALSE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 142 EXPECT_FALSE(area->RemoveItem(kKey, &old_value)); | 142 EXPECT_FALSE(area->RemoveItem(kKey, &old_value)); |
| 143 EXPECT_FALSE(area->Clear()); | 143 EXPECT_FALSE(area->Clear()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 TEST_F(DOMStorageAreaTest, BackingDatabaseOpened) { | 146 TEST_F(DOMStorageAreaTest, BackingDatabaseOpened) { |
| 147 const int64_t kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1; | 147 const int64_t kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1; |
| 148 base::ScopedTempDir temp_dir; | 148 base::ScopedTempDir temp_dir; |
| 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 150 const base::FilePath kExpectedOriginFilePath = temp_dir.path().Append( | 150 const base::FilePath kExpectedOriginFilePath = temp_dir.GetPath().Append( |
| 151 DOMStorageArea::DatabaseFileNameFromOrigin(kOrigin)); | 151 DOMStorageArea::DatabaseFileNameFromOrigin(kOrigin)); |
| 152 | 152 |
| 153 // No directory, backing should be null. | 153 // No directory, backing should be null. |
| 154 { | 154 { |
| 155 scoped_refptr<DOMStorageArea> area( | 155 scoped_refptr<DOMStorageArea> area( |
| 156 new DOMStorageArea(kOrigin, base::FilePath(), NULL)); | 156 new DOMStorageArea(kOrigin, base::FilePath(), NULL)); |
| 157 EXPECT_EQ(NULL, area->backing_.get()); | 157 EXPECT_EQ(NULL, area->backing_.get()); |
| 158 EXPECT_TRUE(area->is_initial_import_done_); | 158 EXPECT_TRUE(area->is_initial_import_done_); |
| 159 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); | 159 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); |
| 160 } | 160 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 173 ASSERT_TRUE(old_value.is_null()); | 173 ASSERT_TRUE(old_value.is_null()); |
| 174 | 174 |
| 175 // Check that saving a value has still left us without a backing database. | 175 // Check that saving a value has still left us without a backing database. |
| 176 EXPECT_EQ(NULL, area->backing_.get()); | 176 EXPECT_EQ(NULL, area->backing_.get()); |
| 177 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); | 177 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 // This should set up a DOMStorageArea that is correctly backed to disk. | 180 // This should set up a DOMStorageArea that is correctly backed to disk. |
| 181 { | 181 { |
| 182 scoped_refptr<DOMStorageArea> area( | 182 scoped_refptr<DOMStorageArea> area( |
| 183 new DOMStorageArea(kOrigin, temp_dir.path(), | 183 new DOMStorageArea(kOrigin, temp_dir.GetPath(), |
| 184 new MockDOMStorageTaskRunner( | 184 new MockDOMStorageTaskRunner( |
| 185 base::ThreadTaskRunnerHandle::Get().get()))); | 185 base::ThreadTaskRunnerHandle::Get().get()))); |
| 186 | 186 |
| 187 EXPECT_TRUE(area->backing_.get()); | 187 EXPECT_TRUE(area->backing_.get()); |
| 188 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( | 188 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( |
| 189 area->backing_.get())->db_.get(); | 189 area->backing_.get())->db_.get(); |
| 190 EXPECT_FALSE(database->IsOpen()); | 190 EXPECT_FALSE(database->IsOpen()); |
| 191 EXPECT_FALSE(area->is_initial_import_done_); | 191 EXPECT_FALSE(area->is_initial_import_done_); |
| 192 | 192 |
| 193 // Inject an in-memory db to speed up the test. | 193 // Inject an in-memory db to speed up the test. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 220 EXPECT_EQ(1u, values.size()); | 220 EXPECT_EQ(1u, values.size()); |
| 221 EXPECT_EQ(kValue, values[kKey].string()); | 221 EXPECT_EQ(kValue, values[kKey].string()); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 TEST_F(DOMStorageAreaTest, CommitTasks) { | 225 TEST_F(DOMStorageAreaTest, CommitTasks) { |
| 226 base::ScopedTempDir temp_dir; | 226 base::ScopedTempDir temp_dir; |
| 227 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 227 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 228 | 228 |
| 229 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 229 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 230 kOrigin, temp_dir.path(), | 230 kOrigin, temp_dir.GetPath(), |
| 231 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 231 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 232 // Inject an in-memory db to speed up the test. | 232 // Inject an in-memory db to speed up the test. |
| 233 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 233 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 234 | 234 |
| 235 // Unrelated to commits, but while we're here, see that querying Length() | 235 // Unrelated to commits, but while we're here, see that querying Length() |
| 236 // causes the backing database to be opened and presumably read from. | 236 // causes the backing database to be opened and presumably read from. |
| 237 EXPECT_FALSE(area->is_initial_import_done_); | 237 EXPECT_FALSE(area->is_initial_import_done_); |
| 238 EXPECT_EQ(0u, area->Length()); | 238 EXPECT_EQ(0u, area->Length()); |
| 239 EXPECT_TRUE(area->is_initial_import_done_); | 239 EXPECT_TRUE(area->is_initial_import_done_); |
| 240 | 240 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 area->backing_->ReadAllValues(&values); | 298 area->backing_->ReadAllValues(&values); |
| 299 EXPECT_EQ(2u, values.size()); | 299 EXPECT_EQ(2u, values.size()); |
| 300 EXPECT_EQ(kValue, values[kKey].string()); | 300 EXPECT_EQ(kValue, values[kKey].string()); |
| 301 EXPECT_EQ(kValue2, values[kKey2].string()); | 301 EXPECT_EQ(kValue2, values[kKey2].string()); |
| 302 } | 302 } |
| 303 | 303 |
| 304 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { | 304 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { |
| 305 base::ScopedTempDir temp_dir; | 305 base::ScopedTempDir temp_dir; |
| 306 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 306 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 307 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 307 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 308 kOrigin, temp_dir.path(), | 308 kOrigin, temp_dir.GetPath(), |
| 309 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 309 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 310 | 310 |
| 311 // Inject an in-memory db to speed up the test and also to verify | 311 // Inject an in-memory db to speed up the test and also to verify |
| 312 // the final changes are commited in it's dtor. | 312 // the final changes are commited in it's dtor. |
| 313 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | 313 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
| 314 new VerifyChangesCommittedDatabase()); | 314 new VerifyChangesCommittedDatabase()); |
| 315 | 315 |
| 316 DOMStorageValuesMap values; | 316 DOMStorageValuesMap values; |
| 317 base::NullableString16 old_value; | 317 base::NullableString16 old_value; |
| 318 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 318 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 319 EXPECT_TRUE(area->HasUncommittedChanges()); | 319 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 320 area->backing_->ReadAllValues(&values); | 320 area->backing_->ReadAllValues(&values); |
| 321 EXPECT_TRUE(values.empty()); // not committed yet | 321 EXPECT_TRUE(values.empty()); // not committed yet |
| 322 area->Shutdown(); | 322 area->Shutdown(); |
| 323 base::RunLoop().RunUntilIdle(); | 323 base::RunLoop().RunUntilIdle(); |
| 324 EXPECT_TRUE(area->HasOneRef()); | 324 EXPECT_TRUE(area->HasOneRef()); |
| 325 EXPECT_FALSE(area->backing_.get()); | 325 EXPECT_FALSE(area->backing_.get()); |
| 326 // The VerifyChangesCommittedDatabase destructor verifies values | 326 // The VerifyChangesCommittedDatabase destructor verifies values |
| 327 // were committed. | 327 // were committed. |
| 328 } | 328 } |
| 329 | 329 |
| 330 TEST_F(DOMStorageAreaTest, DeleteOrigin) { | 330 TEST_F(DOMStorageAreaTest, DeleteOrigin) { |
| 331 base::ScopedTempDir temp_dir; | 331 base::ScopedTempDir temp_dir; |
| 332 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 332 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 333 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 333 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 334 kOrigin, temp_dir.path(), | 334 kOrigin, temp_dir.GetPath(), |
| 335 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 335 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 336 | 336 |
| 337 // This test puts files on disk. | 337 // This test puts files on disk. |
| 338 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 338 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
| 339 area->backing_.get())->db_->file_path(); | 339 area->backing_.get())->db_->file_path(); |
| 340 base::FilePath db_journal_file_path = | 340 base::FilePath db_journal_file_path = |
| 341 DOMStorageDatabase::GetJournalFilePath(db_file_path); | 341 DOMStorageDatabase::GetJournalFilePath(db_file_path); |
| 342 | 342 |
| 343 // Nothing bad should happen when invoked w/o any files on disk. | 343 // Nothing bad should happen when invoked w/o any files on disk. |
| 344 area->DeleteOrigin(); | 344 area->DeleteOrigin(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 EXPECT_TRUE(base::PathExists(db_file_path)); | 384 EXPECT_TRUE(base::PathExists(db_file_path)); |
| 385 area->Shutdown(); | 385 area->Shutdown(); |
| 386 base::RunLoop().RunUntilIdle(); | 386 base::RunLoop().RunUntilIdle(); |
| 387 EXPECT_FALSE(base::PathExists(db_file_path)); | 387 EXPECT_FALSE(base::PathExists(db_file_path)); |
| 388 } | 388 } |
| 389 | 389 |
| 390 TEST_F(DOMStorageAreaTest, PurgeMemory) { | 390 TEST_F(DOMStorageAreaTest, PurgeMemory) { |
| 391 base::ScopedTempDir temp_dir; | 391 base::ScopedTempDir temp_dir; |
| 392 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 392 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 393 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 393 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 394 kOrigin, temp_dir.path(), | 394 kOrigin, temp_dir.GetPath(), |
| 395 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 395 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 396 | 396 |
| 397 // Inject an in-memory db to speed up the test. | 397 // Inject an in-memory db to speed up the test. |
| 398 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 398 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 399 | 399 |
| 400 // Unowned ptrs we use to verify that 'purge' has happened. | 400 // Unowned ptrs we use to verify that 'purge' has happened. |
| 401 DOMStorageDatabase* original_backing = | 401 DOMStorageDatabase* original_backing = |
| 402 static_cast<LocalStorageDatabaseAdapter*>( | 402 static_cast<LocalStorageDatabaseAdapter*>( |
| 403 area->backing_.get())->db_.get(); | 403 area->backing_.get())->db_.get(); |
| 404 DOMStorageMap* original_map = area->map_.get(); | 404 DOMStorageMap* original_map = area->map_.get(); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 base::TimeDelta::FromMilliseconds(750))); | 530 base::TimeDelta::FromMilliseconds(750))); |
| 531 EXPECT_EQ(base::TimeDelta(), | 531 EXPECT_EQ(base::TimeDelta(), |
| 532 rate_limiter.ComputeDelayNeeded( | 532 rate_limiter.ComputeDelayNeeded( |
| 533 base::TimeDelta::FromMilliseconds(1500))); | 533 base::TimeDelta::FromMilliseconds(1500))); |
| 534 EXPECT_EQ(base::TimeDelta(), | 534 EXPECT_EQ(base::TimeDelta(), |
| 535 rate_limiter.ComputeDelayNeeded( | 535 rate_limiter.ComputeDelayNeeded( |
| 536 base::TimeDelta::FromDays(1))); | 536 base::TimeDelta::FromDays(1))); |
| 537 } | 537 } |
| 538 | 538 |
| 539 } // namespace content | 539 } // namespace content |
| OLD | NEW |