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 <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
| 8 #include "base/auto_reset.h" |
8 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
16 #include "content/browser/indexed_db/indexed_db_connection.h" | 17 #include "content/browser/indexed_db/indexed_db_connection.h" |
17 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 18 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 19 #include "content/browser/indexed_db/indexed_db_data_format_version.h" |
18 #include "content/browser/indexed_db/indexed_db_factory_impl.h" | 20 #include "content/browser/indexed_db/indexed_db_factory_impl.h" |
19 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" | 21 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" |
20 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 22 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" |
21 #include "content/browser/quota/mock_quota_manager_proxy.h" | 23 #include "content/browser/quota/mock_quota_manager_proxy.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" | 26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" |
25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
26 #include "url/gurl.h" | 28 #include "url/gurl.h" |
27 #include "url/origin.h" | 29 #include "url/origin.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 private: | 64 private: |
63 ~MockIDBFactory() override {} | 65 ~MockIDBFactory() override {} |
64 | 66 |
65 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); | 67 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); |
66 }; | 68 }; |
67 | 69 |
68 } // namespace | 70 } // namespace |
69 | 71 |
70 class IndexedDBFactoryTest : public testing::Test { | 72 class IndexedDBFactoryTest : public testing::Test { |
71 public: | 73 public: |
72 IndexedDBFactoryTest() { | 74 void SetUp() override { |
| 75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 task_runner_ = new base::TestSimpleTaskRunner(); | 76 task_runner_ = new base::TestSimpleTaskRunner(); |
74 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); | 77 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); |
75 context_ = new IndexedDBContextImpl( | 78 context_ = new IndexedDBContextImpl( |
76 base::FilePath(), nullptr /* special_storage_policy */, | 79 temp_dir_.GetPath(), nullptr /* special_storage_policy */, |
77 quota_manager_proxy_.get(), task_runner_.get()); | 80 quota_manager_proxy_.get(), task_runner_.get()); |
78 idb_factory_ = new MockIDBFactory(context_.get()); | 81 idb_factory_ = new MockIDBFactory(context_.get()); |
79 } | 82 } |
80 ~IndexedDBFactoryTest() override { | 83 |
| 84 void TearDown() override { |
81 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 85 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
82 } | 86 } |
83 | 87 |
84 protected: | 88 protected: |
| 89 IndexedDBFactoryTest() {} |
85 MockIDBFactory* factory() const { return idb_factory_.get(); } | 90 MockIDBFactory* factory() const { return idb_factory_.get(); } |
86 void clear_factory() { idb_factory_ = nullptr; } | 91 void clear_factory() { idb_factory_ = nullptr; } |
87 IndexedDBContextImpl* context() const { return context_.get(); } | 92 IndexedDBContextImpl* context() const { return context_.get(); } |
88 | 93 |
89 private: | 94 private: |
| 95 base::ScopedTempDir temp_dir_; |
90 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 96 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
91 scoped_refptr<IndexedDBContextImpl> context_; | 97 scoped_refptr<IndexedDBContextImpl> context_; |
92 scoped_refptr<MockIDBFactory> idb_factory_; | 98 scoped_refptr<MockIDBFactory> idb_factory_; |
93 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 99 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
94 TestBrowserThreadBundle thread_bundle_; | 100 TestBrowserThreadBundle thread_bundle_; |
95 | 101 |
96 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); | 102 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); |
97 }; | 103 }; |
98 | 104 |
99 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { | 105 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 nullptr /* request_context */, origin, | 510 nullptr /* request_context */, origin, |
505 temp_directory.GetPath()); | 511 temp_directory.GetPath()); |
506 EXPECT_TRUE(callbacks->saw_error()); | 512 EXPECT_TRUE(callbacks->saw_error()); |
507 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); | 513 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); |
508 } | 514 } |
509 | 515 |
510 // Terminate all pending-close timers. | 516 // Terminate all pending-close timers. |
511 factory()->ForceClose(origin); | 517 factory()->ForceClose(origin); |
512 } | 518 } |
513 | 519 |
| 520 namespace { |
| 521 |
| 522 class DataLossCallbacks final : public MockIndexedDBCallbacks { |
| 523 public: |
| 524 blink::WebIDBDataLoss data_loss() const { return data_loss_; } |
| 525 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, |
| 526 const IndexedDBDatabaseMetadata& metadata) override { |
| 527 if (!connection_) |
| 528 connection_ = std::move(connection); |
| 529 } |
| 530 void OnError(const IndexedDBDatabaseError& error) final { |
| 531 ADD_FAILURE() << "Unexpected IDB error: " << error.message(); |
| 532 } |
| 533 void OnUpgradeNeeded(int64_t old_version, |
| 534 std::unique_ptr<IndexedDBConnection> connection, |
| 535 const content::IndexedDBDatabaseMetadata& metadata, |
| 536 const IndexedDBDataLossInfo& data_loss) final { |
| 537 connection_ = std::move(connection); |
| 538 data_loss_ = data_loss.status; |
| 539 } |
| 540 |
| 541 private: |
| 542 ~DataLossCallbacks() final {} |
| 543 blink::WebIDBDataLoss data_loss_ = blink::WebIDBDataLossNone; |
| 544 }; |
| 545 |
| 546 TEST_F(IndexedDBFactoryTest, DataFormatVersion) { |
| 547 auto try_open = [this](const Origin& origin, |
| 548 const IndexedDBDataFormatVersion& version) { |
| 549 base::AutoReset<IndexedDBDataFormatVersion> override_version( |
| 550 &IndexedDBDataFormatVersion::GetMutableCurrentForTesting(), version); |
| 551 auto db_callbacks = base::MakeShared<MockIndexedDBDatabaseCallbacks>(); |
| 552 auto callbacks = base::MakeShared<DataLossCallbacks>(); |
| 553 const int64_t transaction_id = 1; |
| 554 factory()->Open(ASCIIToUTF16("test_db"), |
| 555 base::MakeUnique<IndexedDBPendingConnection>( |
| 556 callbacks, db_callbacks, 0 /* child_process_id */, |
| 557 transaction_id, 1 /* version */), |
| 558 nullptr /* request_context */, origin, |
| 559 context()->data_path()); |
| 560 base::RunLoop().RunUntilIdle(); |
| 561 auto* connection = callbacks->connection(); |
| 562 EXPECT_TRUE(connection); |
| 563 connection->database()->Commit(connection->GetTransaction(transaction_id)); |
| 564 connection->Close(); |
| 565 factory()->ForceClose(origin); |
| 566 return callbacks->data_loss(); |
| 567 }; |
| 568 |
| 569 using blink::WebIDBDataLossNone; |
| 570 using blink::WebIDBDataLossTotal; |
| 571 static const struct { |
| 572 const char* origin; |
| 573 IndexedDBDataFormatVersion open_version_1; |
| 574 IndexedDBDataFormatVersion open_version_2; |
| 575 blink::WebIDBDataLoss expected_data_loss; |
| 576 } kTestCases[] = { |
| 577 {"http://same-version.com/", {3, 4}, {3, 4}, WebIDBDataLossNone}, |
| 578 {"http://blink-upgrade.com/", {3, 4}, {3, 5}, WebIDBDataLossNone}, |
| 579 {"http://v8-upgrade.com/", {3, 4}, {4, 4}, WebIDBDataLossNone}, |
| 580 {"http://both-upgrade.com/", {3, 4}, {4, 5}, WebIDBDataLossNone}, |
| 581 {"http://blink-downgrade.com/", {3, 4}, {3, 3}, WebIDBDataLossTotal}, |
| 582 {"http://v8-downgrade.com/", {3, 4}, {2, 4}, WebIDBDataLossTotal}, |
| 583 {"http://both-downgrade.com/", {3, 4}, {2, 3}, WebIDBDataLossTotal}, |
| 584 {"http://v8-up-blink-down.com/", {3, 4}, {4, 2}, WebIDBDataLossTotal}, |
| 585 {"http://v8-down-blink-up.com/", {3, 4}, {2, 5}, WebIDBDataLossTotal}, |
| 586 }; |
| 587 for (const auto& test : kTestCases) { |
| 588 SCOPED_TRACE(test.origin); |
| 589 const Origin origin(GURL(test.origin)); |
| 590 ASSERT_EQ(WebIDBDataLossNone, try_open(origin, test.open_version_1)); |
| 591 EXPECT_EQ(test.expected_data_loss, try_open(origin, test.open_version_2)); |
| 592 } |
| 593 } |
| 594 |
| 595 } // namespace |
| 596 |
514 } // namespace content | 597 } // namespace content |
OLD | NEW |