diff options
author | jsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-20 06:10:46 +0000 |
---|---|---|
committer | jsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-20 06:10:46 +0000 |
commit | 81e76e5edeb5c5d57304b25f932e4e2cfe0bb5fe (patch) | |
tree | 49eddafff757d21170995ed9b54499c5144168e0 /content | |
parent | fdc0723e660013d2a71c319bb56d8478964d3c41 (diff) | |
download | chromium_src-81e76e5edeb5c5d57304b25f932e4e2cfe0bb5fe.zip chromium_src-81e76e5edeb5c5d57304b25f932e4e2cfe0bb5fe.tar.gz chromium_src-81e76e5edeb5c5d57304b25f932e4e2cfe0bb5fe.tar.bz2 |
IndexedDB: Move IndexedDBFactoryTest unit tests to own file
No new tests, just code shuffle.
TBR=jochen@chromium.org
R=dgrogan@chromium.org
Review URL: https://codereview.chromium.org/28903007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@229676 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
5 files changed, 226 insertions, 199 deletions
diff --git a/content/browser/indexed_db/indexed_db_backing_store_unittest.cc b/content/browser/indexed_db/indexed_db_backing_store_unittest.cc index 49f8174..4e25f93 100644 --- a/content/browser/indexed_db/indexed_db_backing_store_unittest.cc +++ b/content/browser/indexed_db/indexed_db_backing_store_unittest.cc @@ -4,19 +4,12 @@ #include "content/browser/indexed_db/indexed_db_backing_store.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" #include "base/logging.h" -#include "base/message_loop/message_loop.h" #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" -#include "content/browser/indexed_db/indexed_db_factory.h" #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" #include "testing/gtest/include/gtest/gtest.h" -#include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" #include "third_party/WebKit/public/platform/WebIDBTypes.h" -#include "url/gurl.h" -#include "webkit/common/database/database_identifier.h" namespace content { @@ -48,15 +41,9 @@ class IndexedDBBackingStoreTest : public testing::Test { std::string m_value1; std::string m_value2; std::string m_value3; -}; - -class IndexedDBFactoryTest : public testing::Test { - public: - IndexedDBFactoryTest() {} - protected: - // For timers to post events. - base::MessageLoop loop_; + private: + DISALLOW_COPY_AND_ASSIGN(IndexedDBBackingStoreTest); }; TEST_F(IndexedDBBackingStoreTest, PutGetConsistency) { @@ -329,190 +316,6 @@ TEST_F(IndexedDBBackingStoreTest, CreateDatabase) { } } -class MockIDBFactory : public IndexedDBFactory { - public: - scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( - const GURL& origin, - const base::FilePath& data_directory) { - WebKit::WebIDBCallbacks::DataLoss data_loss = - WebKit::WebIDBCallbacks::DataLossNone; - bool disk_full; - scoped_refptr<IndexedDBBackingStore> backing_store = - OpenBackingStore(webkit_database::GetIdentifierFromOrigin(origin), - data_directory, - &data_loss, - &disk_full); - EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); - return backing_store; - } - - void TestCloseBackingStore( - IndexedDBBackingStore* backing_store) { - CloseBackingStore(backing_store->identifier()); - } - - void TestReleaseBackingStore( - IndexedDBBackingStore* backing_store, bool immediate) { - ReleaseBackingStore(backing_store->identifier(), immediate); - } - - private: - virtual ~MockIDBFactory() {} -}; - -TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { - GURL origin1("http://localhost:81"); - GURL origin2("http://localhost:82"); - - scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); - - base::ScopedTempDir temp_directory; - ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); - scoped_refptr<IndexedDBBackingStore> disk_store1 = - factory->TestOpenBackingStore(origin1, temp_directory.path()); - - scoped_refptr<IndexedDBBackingStore> disk_store2 = - factory->TestOpenBackingStore(origin1, temp_directory.path()); - EXPECT_EQ(disk_store1.get(), disk_store2.get()); - - scoped_refptr<IndexedDBBackingStore> disk_store3 = - factory->TestOpenBackingStore(origin2, temp_directory.path()); - - factory->TestCloseBackingStore(disk_store1); - factory->TestCloseBackingStore(disk_store2); - factory->TestCloseBackingStore(disk_store3); - - EXPECT_FALSE(disk_store1->HasOneRef()); - EXPECT_FALSE(disk_store2->HasOneRef()); - EXPECT_TRUE(disk_store3->HasOneRef()); - - disk_store2 = NULL; - EXPECT_TRUE(disk_store1->HasOneRef()); -} - -TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) { - GURL origin("http://localhost:81"); - - scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); - - base::ScopedTempDir temp_directory; - ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); - scoped_refptr<IndexedDBBackingStore> store1 = - factory->TestOpenBackingStore(origin, temp_directory.path()); - - // Give up the local refptr so that the factory has the only - // outstanding reference. - IndexedDBBackingStore* store_ptr = store1.get(); - store1 = NULL; - EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); - factory->TestReleaseBackingStore(store_ptr, false); - EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); - - factory->TestOpenBackingStore(origin, temp_directory.path()); - EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); - factory->TestReleaseBackingStore(store_ptr, false); - EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); - - store_ptr->close_timer()->Stop(); -} - -TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { - GURL origin1("http://localhost:81"); - GURL origin2("http://localhost:82"); - - scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); - scoped_refptr<IndexedDBBackingStore> mem_store1 = - factory->TestOpenBackingStore(origin1, base::FilePath()); - - scoped_refptr<IndexedDBBackingStore> mem_store2 = - factory->TestOpenBackingStore(origin1, base::FilePath()); - EXPECT_EQ(mem_store1.get(), mem_store2.get()); - - scoped_refptr<IndexedDBBackingStore> mem_store3 = - factory->TestOpenBackingStore(origin2, base::FilePath()); - - factory->TestCloseBackingStore(mem_store1); - factory->TestCloseBackingStore(mem_store2); - factory->TestCloseBackingStore(mem_store3); - - EXPECT_FALSE(mem_store1->HasOneRef()); - EXPECT_FALSE(mem_store2->HasOneRef()); - EXPECT_FALSE(mem_store3->HasOneRef()); - - factory = NULL; - EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 - EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 - EXPECT_TRUE(mem_store3->HasOneRef()); - - mem_store2 = NULL; - EXPECT_TRUE(mem_store1->HasOneRef()); -} - -TEST_F(IndexedDBFactoryTest, RejectLongOrigins) { - base::ScopedTempDir temp_directory; - ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); - const base::FilePath base_path = temp_directory.path(); - scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); - - int limit = file_util::GetMaximumPathComponentLength(base_path); - EXPECT_GT(limit, 0); - - std::string origin(limit + 1, 'x'); - GURL too_long_origin("http://" + origin + ":81/"); - scoped_refptr<IndexedDBBackingStore> diskStore1 = - factory->TestOpenBackingStore(too_long_origin, base_path); - EXPECT_FALSE(diskStore1); - - GURL ok_origin("http://someorigin.com:82/"); - scoped_refptr<IndexedDBBackingStore> diskStore2 = - factory->TestOpenBackingStore(ok_origin, base_path); - EXPECT_TRUE(diskStore2); -} - -class DiskFullFactory : public IndexedDBFactory { - private: - virtual ~DiskFullFactory() {} - virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore( - const std::string& origin_identifier, - const base::FilePath& data_directory, - WebKit::WebIDBCallbacks::DataLoss* data_loss, - bool* disk_full) OVERRIDE { - *disk_full = true; - return scoped_refptr<IndexedDBBackingStore>(); - } -}; - -class LookingForQuotaErrorMockCallbacks : public IndexedDBCallbacks { - public: - LookingForQuotaErrorMockCallbacks() - : IndexedDBCallbacks(NULL, 0, 0), error_called_(false) {} - virtual void OnError(const IndexedDBDatabaseError& error) OVERRIDE { - error_called_ = true; - EXPECT_EQ(WebKit::WebIDBDatabaseExceptionQuotaError, error.code()); - } - private: - virtual ~LookingForQuotaErrorMockCallbacks() { - EXPECT_TRUE(error_called_); - } - bool error_called_; -}; - -TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) { - scoped_refptr<DiskFullFactory> factory = new DiskFullFactory; - scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = - new LookingForQuotaErrorMockCallbacks; - scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = - new IndexedDBDatabaseCallbacks(NULL, 0, 0); - const string16 name(ASCIIToUTF16("name")); - factory->Open(name, - 1, /* version */ - 2, /* transaction_id */ - callbacks, - dummy_database_callbacks, - "origin", - base::FilePath(FILE_PATH_LITERAL("/dummy"))); -} - } // namespace } // namespace content diff --git a/content/browser/indexed_db/indexed_db_factory_unittest.cc b/content/browser/indexed_db/indexed_db_factory_unittest.cc new file mode 100644 index 0000000..4e1402c --- /dev/null +++ b/content/browser/indexed_db/indexed_db_factory_unittest.cc @@ -0,0 +1,219 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/indexed_db/indexed_db_factory.h" + +#include "base/file_util.h" +#include "base/files/scoped_temp_dir.h" +#include "base/logging.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/utf_string_conversions.h" +#include "content/browser/indexed_db/indexed_db_connection.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/public/platform/WebIDBDatabaseException.h" +#include "third_party/WebKit/public/platform/WebIDBTypes.h" +#include "url/gurl.h" +#include "webkit/common/database/database_identifier.h" + +namespace content { + +namespace { + +class IndexedDBFactoryTest : public testing::Test { + public: + IndexedDBFactoryTest() {} + + protected: + // For timers to post events. + base::MessageLoop loop_; + + private: + DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); +}; + +class MockIDBFactory : public IndexedDBFactory { + public: + scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( + const GURL& origin, + const base::FilePath& data_directory) { + WebKit::WebIDBCallbacks::DataLoss data_loss = + WebKit::WebIDBCallbacks::DataLossNone; + bool disk_full; + scoped_refptr<IndexedDBBackingStore> backing_store = + OpenBackingStore(webkit_database::GetIdentifierFromOrigin(origin), + data_directory, + &data_loss, + &disk_full); + EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); + return backing_store; + } + + void TestCloseBackingStore(IndexedDBBackingStore* backing_store) { + CloseBackingStore(backing_store->identifier()); + } + + void TestReleaseBackingStore(IndexedDBBackingStore* backing_store, + bool immediate) { + ReleaseBackingStore(backing_store->identifier(), immediate); + } + + private: + virtual ~MockIDBFactory() {} +}; + +TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { + GURL origin1("http://localhost:81"); + GURL origin2("http://localhost:82"); + + scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); + + base::ScopedTempDir temp_directory; + ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); + scoped_refptr<IndexedDBBackingStore> disk_store1 = + factory->TestOpenBackingStore(origin1, temp_directory.path()); + + scoped_refptr<IndexedDBBackingStore> disk_store2 = + factory->TestOpenBackingStore(origin1, temp_directory.path()); + EXPECT_EQ(disk_store1.get(), disk_store2.get()); + + scoped_refptr<IndexedDBBackingStore> disk_store3 = + factory->TestOpenBackingStore(origin2, temp_directory.path()); + + factory->TestCloseBackingStore(disk_store1); + factory->TestCloseBackingStore(disk_store2); + factory->TestCloseBackingStore(disk_store3); + + EXPECT_FALSE(disk_store1->HasOneRef()); + EXPECT_FALSE(disk_store2->HasOneRef()); + EXPECT_TRUE(disk_store3->HasOneRef()); + + disk_store2 = NULL; + EXPECT_TRUE(disk_store1->HasOneRef()); +} + +TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) { + GURL origin("http://localhost:81"); + + scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); + + base::ScopedTempDir temp_directory; + ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); + scoped_refptr<IndexedDBBackingStore> store1 = + factory->TestOpenBackingStore(origin, temp_directory.path()); + + // Give up the local refptr so that the factory has the only + // outstanding reference. + IndexedDBBackingStore* store_ptr = store1.get(); + store1 = NULL; + EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); + factory->TestReleaseBackingStore(store_ptr, false); + EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); + + factory->TestOpenBackingStore(origin, temp_directory.path()); + EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); + factory->TestReleaseBackingStore(store_ptr, false); + EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); + + store_ptr->close_timer()->Stop(); +} + +TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { + GURL origin1("http://localhost:81"); + GURL origin2("http://localhost:82"); + + scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); + scoped_refptr<IndexedDBBackingStore> mem_store1 = + factory->TestOpenBackingStore(origin1, base::FilePath()); + + scoped_refptr<IndexedDBBackingStore> mem_store2 = + factory->TestOpenBackingStore(origin1, base::FilePath()); + EXPECT_EQ(mem_store1.get(), mem_store2.get()); + + scoped_refptr<IndexedDBBackingStore> mem_store3 = + factory->TestOpenBackingStore(origin2, base::FilePath()); + + factory->TestCloseBackingStore(mem_store1); + factory->TestCloseBackingStore(mem_store2); + factory->TestCloseBackingStore(mem_store3); + + EXPECT_FALSE(mem_store1->HasOneRef()); + EXPECT_FALSE(mem_store2->HasOneRef()); + EXPECT_FALSE(mem_store3->HasOneRef()); + + factory = NULL; + EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 + EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 + EXPECT_TRUE(mem_store3->HasOneRef()); + + mem_store2 = NULL; + EXPECT_TRUE(mem_store1->HasOneRef()); +} + +TEST_F(IndexedDBFactoryTest, RejectLongOrigins) { + base::ScopedTempDir temp_directory; + ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); + const base::FilePath base_path = temp_directory.path(); + scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); + + int limit = file_util::GetMaximumPathComponentLength(base_path); + EXPECT_GT(limit, 0); + + std::string origin(limit + 1, 'x'); + GURL too_long_origin("http://" + origin + ":81/"); + scoped_refptr<IndexedDBBackingStore> diskStore1 = + factory->TestOpenBackingStore(too_long_origin, base_path); + EXPECT_FALSE(diskStore1); + + GURL ok_origin("http://someorigin.com:82/"); + scoped_refptr<IndexedDBBackingStore> diskStore2 = + factory->TestOpenBackingStore(ok_origin, base_path); + EXPECT_TRUE(diskStore2); +} + +class DiskFullFactory : public IndexedDBFactory { + private: + virtual ~DiskFullFactory() {} + virtual scoped_refptr<IndexedDBBackingStore> OpenBackingStore( + const std::string& origin_identifier, + const base::FilePath& data_directory, + WebKit::WebIDBCallbacks::DataLoss* data_loss, + bool* disk_full) OVERRIDE { + *disk_full = true; + return scoped_refptr<IndexedDBBackingStore>(); + } +}; + +class LookingForQuotaErrorMockCallbacks : public IndexedDBCallbacks { + public: + LookingForQuotaErrorMockCallbacks() + : IndexedDBCallbacks(NULL, 0, 0), error_called_(false) {} + virtual void OnError(const IndexedDBDatabaseError& error) OVERRIDE { + error_called_ = true; + EXPECT_EQ(WebKit::WebIDBDatabaseExceptionQuotaError, error.code()); + } + + private: + virtual ~LookingForQuotaErrorMockCallbacks() { EXPECT_TRUE(error_called_); } + bool error_called_; +}; + +TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) { + scoped_refptr<DiskFullFactory> factory = new DiskFullFactory; + scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = + new LookingForQuotaErrorMockCallbacks; + scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = + new IndexedDBDatabaseCallbacks(NULL, 0, 0); + const string16 name(ASCIIToUTF16("name")); + factory->Open(name, + 1, /* version */ + 2, /* transaction_id */ + callbacks, + dummy_database_callbacks, + "origin", + base::FilePath(FILE_PATH_LITERAL("/dummy"))); +} + +} // namespace + +} // namespace content diff --git a/content/browser/indexed_db/indexed_db_quota_client_unittest.cc b/content/browser/indexed_db/indexed_db_quota_client_unittest.cc index 685b080..25f6620 100644 --- a/content/browser/indexed_db/indexed_db_quota_client_unittest.cc +++ b/content/browser/indexed_db/indexed_db_quota_client_unittest.cc @@ -170,6 +170,8 @@ class IndexedDBQuotaClientTest : public testing::Test { scoped_ptr<TestBrowserContext> browser_context_; quota::QuotaStatusCode delete_status_; base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest); }; TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { diff --git a/content/browser/indexed_db/indexed_db_unittest.cc b/content/browser/indexed_db/indexed_db_unittest.cc index 0467c1c..6180c86 100644 --- a/content/browser/indexed_db/indexed_db_unittest.cc +++ b/content/browser/indexed_db/indexed_db_unittest.cc @@ -46,6 +46,8 @@ class IndexedDBTest : public testing::Test { private: BrowserThreadImpl file_thread_; BrowserThreadImpl io_thread_; + + DISALLOW_COPY_AND_ASSIGN(IndexedDBTest); }; TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { diff --git a/content/content_tests.gypi b/content/content_tests.gypi index b4a3193..bd170c3 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -379,6 +379,7 @@ 'browser/indexed_db/indexed_db_backing_store_unittest.cc', 'browser/indexed_db/indexed_db_cleanup_on_io_error_unittest.cc', 'browser/indexed_db/indexed_db_database_unittest.cc', + 'browser/indexed_db/indexed_db_factory_unittest.cc', 'browser/indexed_db/indexed_db_fake_backing_store.cc', 'browser/indexed_db/indexed_db_fake_backing_store.h', 'browser/indexed_db/indexed_db_leveldb_coding_unittest.cc', |