| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 EXPECT_TRUE(area->HasUncommittedChanges()); | 61 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 62 // Make additional change and verify that a new commit batch | 62 // Make additional change and verify that a new commit batch |
| 63 // is created for that change. | 63 // is created for that change. |
| 64 base::NullableString16 old_value; | 64 base::NullableString16 old_value; |
| 65 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); | 65 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); |
| 66 EXPECT_TRUE(area->commit_batch_.get()); | 66 EXPECT_TRUE(area->commit_batch_.get()); |
| 67 EXPECT_EQ(1, area->commit_batches_in_flight_); | 67 EXPECT_EQ(1, area->commit_batches_in_flight_); |
| 68 EXPECT_TRUE(area->HasUncommittedChanges()); | 68 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Class used in the CommitChangesAtShutdown test case. | |
| 72 class VerifyChangesCommittedDatabase : public DOMStorageDatabase { | |
| 73 public: | |
| 74 VerifyChangesCommittedDatabase() {} | |
| 75 ~VerifyChangesCommittedDatabase() override { | |
| 76 const base::string16 kKey(ASCIIToUTF16("key")); | |
| 77 const base::string16 kValue(ASCIIToUTF16("value")); | |
| 78 DOMStorageValuesMap values; | |
| 79 ReadAllValues(&values); | |
| 80 EXPECT_EQ(1u, values.size()); | |
| 81 EXPECT_EQ(kValue, values[kKey].string()); | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 private: | 71 private: |
| 86 base::MessageLoop message_loop_; | 72 base::MessageLoop message_loop_; |
| 87 }; | 73 }; |
| 88 | 74 |
| 89 TEST_F(DOMStorageAreaTest, DOMStorageAreaBasics) { | 75 TEST_F(DOMStorageAreaTest, DOMStorageAreaBasics) { |
| 90 scoped_refptr<DOMStorageArea> area( | 76 scoped_refptr<DOMStorageArea> area( |
| 91 new DOMStorageArea(1, std::string(), kOrigin, NULL, NULL)); | 77 new DOMStorageArea(1, std::string(), kOrigin, NULL, NULL)); |
| 92 base::string16 old_value; | 78 base::string16 old_value; |
| 93 base::NullableString16 old_nullable_value; | 79 base::NullableString16 old_nullable_value; |
| 94 scoped_refptr<DOMStorageArea> copy; | 80 scoped_refptr<DOMStorageArea> copy; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 EXPECT_TRUE(area->HasOneRef()); | 275 EXPECT_TRUE(area->HasOneRef()); |
| 290 EXPECT_FALSE(area->HasUncommittedChanges()); | 276 EXPECT_FALSE(area->HasUncommittedChanges()); |
| 291 // Verify the changes made it to the database. | 277 // Verify the changes made it to the database. |
| 292 values.clear(); | 278 values.clear(); |
| 293 area->backing_->ReadAllValues(&values); | 279 area->backing_->ReadAllValues(&values); |
| 294 EXPECT_EQ(2u, values.size()); | 280 EXPECT_EQ(2u, values.size()); |
| 295 EXPECT_EQ(kValue, values[kKey].string()); | 281 EXPECT_EQ(kValue, values[kKey].string()); |
| 296 EXPECT_EQ(kValue2, values[kKey2].string()); | 282 EXPECT_EQ(kValue2, values[kKey2].string()); |
| 297 } | 283 } |
| 298 | 284 |
| 299 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { | |
| 300 base::ScopedTempDir temp_dir; | |
| 301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 302 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | |
| 303 kOrigin, | |
| 304 temp_dir.path(), | |
| 305 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | |
| 306 | |
| 307 // Inject an in-memory db to speed up the test and also to verify | |
| 308 // the final changes are commited in it's dtor. | |
| 309 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | |
| 310 new VerifyChangesCommittedDatabase()); | |
| 311 | |
| 312 DOMStorageValuesMap values; | |
| 313 base::NullableString16 old_value; | |
| 314 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | |
| 315 EXPECT_TRUE(area->HasUncommittedChanges()); | |
| 316 area->backing_->ReadAllValues(&values); | |
| 317 EXPECT_TRUE(values.empty()); // not committed yet | |
| 318 area->Shutdown(); | |
| 319 base::MessageLoop::current()->RunUntilIdle(); | |
| 320 EXPECT_TRUE(area->HasOneRef()); | |
| 321 EXPECT_FALSE(area->backing_.get()); | |
| 322 // The VerifyChangesCommittedDatabase destructor verifies values | |
| 323 // were committed. | |
| 324 } | |
| 325 | |
| 326 TEST_F(DOMStorageAreaTest, DeleteOrigin) { | 285 TEST_F(DOMStorageAreaTest, DeleteOrigin) { |
| 327 base::ScopedTempDir temp_dir; | 286 base::ScopedTempDir temp_dir; |
| 328 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 287 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 329 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 288 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 330 kOrigin, | 289 kOrigin, |
| 331 temp_dir.path(), | 290 temp_dir.path(), |
| 332 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 291 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
| 333 | 292 |
| 334 // This test puts files on disk. | 293 // This test puts files on disk. |
| 335 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 294 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 base::TimeDelta::FromMilliseconds(750))); | 487 base::TimeDelta::FromMilliseconds(750))); |
| 529 EXPECT_EQ(base::TimeDelta(), | 488 EXPECT_EQ(base::TimeDelta(), |
| 530 rate_limiter.ComputeDelayNeeded( | 489 rate_limiter.ComputeDelayNeeded( |
| 531 base::TimeDelta::FromMilliseconds(1500))); | 490 base::TimeDelta::FromMilliseconds(1500))); |
| 532 EXPECT_EQ(base::TimeDelta(), | 491 EXPECT_EQ(base::TimeDelta(), |
| 533 rate_limiter.ComputeDelayNeeded( | 492 rate_limiter.ComputeDelayNeeded( |
| 534 base::TimeDelta::FromDays(1))); | 493 base::TimeDelta::FromDays(1))); |
| 535 } | 494 } |
| 536 | 495 |
| 537 } // namespace content | 496 } // namespace content |
| OLD | NEW |