Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: content/browser/indexed_db/indexed_db_factory_unittest.cc

Issue 2773823002: Use a two-part data format version in IndexedDB metadata. (Closed)
Patch Set: rename blink constants Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
17 #include "content/browser/indexed_db/indexed_db_connection.h" 18 #include "content/browser/indexed_db/indexed_db_connection.h"
18 #include "content/browser/indexed_db/indexed_db_context_impl.h" 19 #include "content/browser/indexed_db/indexed_db_context_impl.h"
20 #include "content/browser/indexed_db/indexed_db_data_format_version.h"
19 #include "content/browser/indexed_db/indexed_db_factory_impl.h" 21 #include "content/browser/indexed_db/indexed_db_factory_impl.h"
20 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" 22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
21 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" 23 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "storage/browser/test/mock_quota_manager_proxy.h" 25 #include "storage/browser/test/mock_quota_manager_proxy.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
26 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
27 #include "url/gurl.h" 29 #include "url/gurl.h"
28 #include "url/origin.h" 30 #include "url/origin.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 private: 65 private:
64 ~MockIDBFactory() override {} 66 ~MockIDBFactory() override {}
65 67
66 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); 68 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory);
67 }; 69 };
68 70
69 } // namespace 71 } // namespace
70 72
71 class IndexedDBFactoryTest : public testing::Test { 73 class IndexedDBFactoryTest : public testing::Test {
72 public: 74 public:
73 IndexedDBFactoryTest() { 75 void SetUp() override {
76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
74 task_runner_ = new base::TestSimpleTaskRunner(); 77 task_runner_ = new base::TestSimpleTaskRunner();
75 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); 78 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr);
76 context_ = new IndexedDBContextImpl( 79 context_ = new IndexedDBContextImpl(
77 base::FilePath(), nullptr /* special_storage_policy */, 80 temp_dir_.GetPath(), nullptr /* special_storage_policy */,
78 quota_manager_proxy_.get(), task_runner_.get()); 81 quota_manager_proxy_.get(), task_runner_.get());
79 idb_factory_ = new MockIDBFactory(context_.get()); 82 idb_factory_ = new MockIDBFactory(context_.get());
80 } 83 }
81 ~IndexedDBFactoryTest() override { 84
85 void TearDown() override {
82 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); 86 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
83 } 87 }
84 88
85 protected: 89 protected:
90 IndexedDBFactoryTest() {}
86 MockIDBFactory* factory() const { return idb_factory_.get(); } 91 MockIDBFactory* factory() const { return idb_factory_.get(); }
87 void clear_factory() { idb_factory_ = nullptr; } 92 void clear_factory() { idb_factory_ = nullptr; }
88 IndexedDBContextImpl* context() const { return context_.get(); } 93 IndexedDBContextImpl* context() const { return context_.get(); }
89 94
90 private: 95 private:
96 base::ScopedTempDir temp_dir_;
91 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 97 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
92 scoped_refptr<IndexedDBContextImpl> context_; 98 scoped_refptr<IndexedDBContextImpl> context_;
93 scoped_refptr<MockIDBFactory> idb_factory_; 99 scoped_refptr<MockIDBFactory> idb_factory_;
94 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 100 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
95 TestBrowserThreadBundle thread_bundle_; 101 TestBrowserThreadBundle thread_bundle_;
96 102
97 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); 103 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest);
98 }; 104 };
99 105
100 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { 106 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) {
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 nullptr /* request_context */, origin, 514 nullptr /* request_context */, origin,
509 temp_directory.GetPath()); 515 temp_directory.GetPath());
510 EXPECT_TRUE(callbacks->saw_error()); 516 EXPECT_TRUE(callbacks->saw_error());
511 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); 517 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name));
512 } 518 }
513 519
514 // Terminate all pending-close timers. 520 // Terminate all pending-close timers.
515 factory()->ForceClose(origin); 521 factory()->ForceClose(origin);
516 } 522 }
517 523
524 namespace {
525
526 class DataLossCallbacks final : public MockIndexedDBCallbacks {
527 public:
528 blink::WebIDBDataLoss data_loss() const { return data_loss_; }
529 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection,
530 const IndexedDBDatabaseMetadata& metadata) override {
531 if (!connection_)
532 connection_ = std::move(connection);
533 }
534 void OnError(const IndexedDBDatabaseError& error) final {
535 ADD_FAILURE() << "Unexpected IDB error: " << error.message();
536 }
537 void OnUpgradeNeeded(int64_t old_version,
538 std::unique_ptr<IndexedDBConnection> connection,
539 const content::IndexedDBDatabaseMetadata& metadata,
540 const IndexedDBDataLossInfo& data_loss) final {
541 connection_ = std::move(connection);
542 data_loss_ = data_loss.status;
543 }
544
545 private:
546 ~DataLossCallbacks() final {}
547 blink::WebIDBDataLoss data_loss_ = blink::kWebIDBDataLossNone;
548 };
549
550 TEST_F(IndexedDBFactoryTest, DataFormatVersion) {
551 auto try_open = [this](const Origin& origin,
552 const IndexedDBDataFormatVersion& version) {
553 base::AutoReset<IndexedDBDataFormatVersion> override_version(
554 &IndexedDBDataFormatVersion::GetMutableCurrentForTesting(), version);
555 auto db_callbacks = base::MakeShared<MockIndexedDBDatabaseCallbacks>();
556 auto callbacks = base::MakeShared<DataLossCallbacks>();
557 const int64_t transaction_id = 1;
558 factory()->Open(ASCIIToUTF16("test_db"),
559 base::MakeUnique<IndexedDBPendingConnection>(
560 callbacks, db_callbacks, 0 /* child_process_id */,
561 transaction_id, 1 /* version */),
562 nullptr /* request_context */, origin,
563 context()->data_path());
564 base::RunLoop().RunUntilIdle();
565 auto* connection = callbacks->connection();
566 EXPECT_TRUE(connection);
567 connection->database()->Commit(connection->GetTransaction(transaction_id));
568 connection->Close();
569 factory()->ForceClose(origin);
570 return callbacks->data_loss();
571 };
572
573 using blink::kWebIDBDataLossNone;
574 using blink::kWebIDBDataLossTotal;
575 static const struct {
576 const char* origin;
577 IndexedDBDataFormatVersion open_version_1;
578 IndexedDBDataFormatVersion open_version_2;
579 blink::WebIDBDataLoss expected_data_loss;
580 } kTestCases[] = {
581 {"http://same-version.com/", {3, 4}, {3, 4}, kWebIDBDataLossNone},
582 {"http://blink-upgrade.com/", {3, 4}, {3, 5}, kWebIDBDataLossNone},
583 {"http://v8-upgrade.com/", {3, 4}, {4, 4}, kWebIDBDataLossNone},
584 {"http://both-upgrade.com/", {3, 4}, {4, 5}, kWebIDBDataLossNone},
585 {"http://blink-downgrade.com/", {3, 4}, {3, 3}, kWebIDBDataLossTotal},
586 {"http://v8-downgrade.com/", {3, 4}, {2, 4}, kWebIDBDataLossTotal},
587 {"http://both-downgrade.com/", {3, 4}, {2, 3}, kWebIDBDataLossTotal},
588 {"http://v8-up-blink-down.com/", {3, 4}, {4, 2}, kWebIDBDataLossTotal},
589 {"http://v8-down-blink-up.com/", {3, 4}, {2, 5}, kWebIDBDataLossTotal},
590 };
591 for (const auto& test : kTestCases) {
592 SCOPED_TRACE(test.origin);
593 const Origin origin(GURL(test.origin));
594 ASSERT_EQ(kWebIDBDataLossNone, try_open(origin, test.open_version_1));
595 EXPECT_EQ(test.expected_data_loss, try_open(origin, test.open_version_2));
596 }
597 }
598
599 } // namespace
600
518 } // namespace content 601 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_data_format_version.cc ('k') | content/browser/indexed_db/leveldb_coding_scheme.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698