Chromium Code Reviews| Index: content/browser/indexed_db/indexed_db_database_unittest.cc |
| diff --git a/content/browser/indexed_db/indexed_db_database_unittest.cc b/content/browser/indexed_db/indexed_db_database_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4b6a8a26c19fe90d207a783c01bfa665ff6045bb |
| --- /dev/null |
| +++ b/content/browser/indexed_db/indexed_db_database_unittest.cc |
| @@ -0,0 +1,221 @@ |
| +// Copyright (c) 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_database.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/browser/indexed_db/indexed_db.h" |
| +#include "content/browser/indexed_db/indexed_db_backing_store.h" |
| +#include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h" |
| +#include "content/browser/indexed_db/indexed_db_cursor.h" |
| +#include "content/browser/indexed_db/indexed_db_database_impl.h" |
| +#include "content/browser/indexed_db/indexed_db_factory_impl.h" |
| +#include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
| +#include "content/browser/indexed_db/indexed_db_transaction.h" |
| +#include "content/browser/indexed_db/webidbdatabase_impl.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebData.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebIDBDatabaseCallbacks.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebIDBDatabaseError.h" |
| + |
| +#include <gtest/gtest.h> |
| + |
| +using namespace content; |
|
jsbell
2013/05/28 19:27:32
"using namespace" is not permitted.
Wrap the tes
alecflett
2013/05/28 23:07:47
Done.
|
| +using WebKit::WebIDBDatabase; |
| +using WebKit::WebIDBDatabaseError; |
| +using WebKit::WebIDBDatabaseCallbacks; |
| + |
| +namespace { |
| + |
| +TEST(IndexedDBDatabaseTest, BackingStoreRetention) { |
| + scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| + new IndexedDBFakeBackingStore(); |
|
jsbell
2013/05/28 19:27:32
ISTM that we need a Mock backing store that has ob
|
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| + |
| + IndexedDBFactoryImpl* factory = 0; |
| + scoped_refptr<IndexedDBDatabaseImpl> db = |
| + IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| + backing_store.get(), |
| + factory, |
| + ASCIIToUTF16("uniqueid")); |
| + |
| + // TODO(alecflett): make sure it is still alive before and after the local |
| + // variable clears it. |
| + // EXPECT_GT(backing_store->RefCount(), 1); |
| + |
| + db = NULL; |
| + |
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| +} |
| + |
| +class MockIDBCallbacks : public IndexedDBCallbacksWrapper { |
| + public: |
| + static scoped_refptr<MockIDBCallbacks> Create() { |
| + return make_scoped_refptr(new MockIDBCallbacks()); |
| + } |
| + virtual void OnError(scoped_refptr<IndexedDBDatabaseError>) OVERRIDE {} |
| + virtual void OnSuccess(const std::vector<string16>&) OVERRIDE {} |
| + virtual void OnSuccess(scoped_refptr<IndexedDBCursor> cursor, |
| + const IndexedDBKey&, |
| + const IndexedDBKey&, |
| + std::vector<char>*) OVERRIDE {} |
| + virtual void OnSuccess(scoped_refptr<IndexedDBDatabase>, |
| + const IndexedDBDatabaseMetadata&) OVERRIDE { |
| + was_success_db_called_ = true; |
| + } |
| + virtual void OnSuccess(const IndexedDBKey&) OVERRIDE {} |
| + virtual void OnSuccess(std::vector<char>*) OVERRIDE {} |
| + virtual void OnSuccess(std::vector<char>*, |
| + const IndexedDBKey&, |
| + const IndexedDBKeyPath&) OVERRIDE{}; |
| + virtual void OnSuccess(int64_t) OVERRIDE {} |
| + virtual void OnSuccess() OVERRIDE {} |
| + virtual void OnSuccess(const IndexedDBKey&, |
| + const IndexedDBKey&, |
| + std::vector<char>*) OVERRIDE{}; |
| + virtual void OnSuccessWithPrefetch( |
| + const std::vector<IndexedDBKey>& keys, |
| + const std::vector<IndexedDBKey>& primary_keys, |
| + const std::vector<std::vector<char> >& values) OVERRIDE {} |
| + |
| + private: |
| + virtual ~MockIDBCallbacks() { EXPECT_TRUE(was_success_db_called_); } |
| + MockIDBCallbacks() |
| + : IndexedDBCallbacksWrapper(NULL), was_success_db_called_(false) {} |
| + bool was_success_db_called_; |
| +}; |
| + |
| +class FakeIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper { |
| + public: |
| + static scoped_refptr<FakeIDBDatabaseCallbacks> Create() { |
| + return make_scoped_refptr(new FakeIDBDatabaseCallbacks()); |
| + } |
| + virtual void OnVersionChange(int64_t old_version, int64_t new_version) |
| + OVERRIDE {} |
| + virtual void OnForcedClose() OVERRIDE {} |
| + virtual void OnAbort(int64_t transaction_id, |
| + scoped_refptr<IndexedDBDatabaseError> error) OVERRIDE {} |
| + virtual void OnComplete(int64_t transaction_id) OVERRIDE {} |
| + |
| + private: |
| + friend class base::RefCounted<FakeIDBDatabaseCallbacks>; |
| + virtual ~FakeIDBDatabaseCallbacks() {} |
| + FakeIDBDatabaseCallbacks() : IndexedDBDatabaseCallbacksWrapper(NULL) {} |
| +}; |
| + |
| +TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { |
| + scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| + new IndexedDBFakeBackingStore(); |
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| + |
| + IndexedDBFactoryImpl* factory = 0; |
| + scoped_refptr<IndexedDBDatabaseImpl> db = |
| + IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| + backing_store.get(), |
| + factory, |
| + ASCIIToUTF16("uniqueid")); |
| + // EXPECT_GT(backing_store->RefCount(), 1); |
| + |
| + scoped_refptr<MockIDBCallbacks> request1 = MockIDBCallbacks::Create(); |
| + scoped_refptr<FakeIDBDatabaseCallbacks> connection1 = |
| + FakeIDBDatabaseCallbacks::Create(); |
| + db->OpenConnection( |
| + request1, connection1, 1, IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
|
jsbell
2013/05/28 19:27:32
Make a local const int64 for the upgrade transacti
alecflett
2013/05/28 23:07:47
Done.
|
| + |
| + scoped_refptr<MockIDBCallbacks> request2 = MockIDBCallbacks::Create(); |
| + scoped_refptr<FakeIDBDatabaseCallbacks> connection2 = |
| + FakeIDBDatabaseCallbacks::Create(); |
| + db->OpenConnection( |
| + request2, connection2, 2, IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
|
jsbell
2013/05/28 19:27:32
Ditto?
alecflett
2013/05/28 23:07:47
Done.
|
| + |
| + db->Close(connection1); |
| + // EXPECT_GT(backing_store->RefCount(), 1); |
| + |
| + db->Close(connection2); |
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| +} |
| + |
| +class MockIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper { |
| + public: |
| + static scoped_refptr<MockIDBDatabaseCallbacks> Create() { |
| + return make_scoped_refptr(new MockIDBDatabaseCallbacks()); |
| + } |
| + virtual void OnVersionChange(int64_t old_version, int64_t new_version) |
|
jsbell
2013/05/28 19:27:32
int64_t -> int64 here and elsewhere. :(
alecflett
2013/05/28 23:07:47
Done.
|
| + OVERRIDE {} |
| + virtual void OnForcedClose() OVERRIDE {} |
| + virtual void OnAbort(int64_t transaction_id, |
| + scoped_refptr<IndexedDBDatabaseError> error) OVERRIDE { |
| + was_abort_called_ = true; |
| + } |
| + virtual void OnComplete(int64_t transaction_id) OVERRIDE {} |
| + |
| + private: |
| + MockIDBDatabaseCallbacks() |
| + : IndexedDBDatabaseCallbacksWrapper(NULL), was_abort_called_(false) {} |
| + virtual ~MockIDBDatabaseCallbacks() { EXPECT_TRUE(was_abort_called_); } |
| + bool was_abort_called_; |
| +}; |
| + |
| +class WebIDBDatabaseCallbacksImpl : public WebIDBDatabaseCallbacks { |
| + public: |
| + explicit WebIDBDatabaseCallbacksImpl( |
| + scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks) |
| + : callbacks_(callbacks) {} |
| + virtual ~WebIDBDatabaseCallbacksImpl() {} |
| + |
| + virtual void onForcedClose() { callbacks_->OnForcedClose(); } |
| + virtual void onVersionChange(long long old_version, long long new_version) { |
| + callbacks_->OnVersionChange(old_version, new_version); |
| + } |
| + virtual void onAbort(long long transaction_id, |
| + const WebIDBDatabaseError& error) { |
| + callbacks_->OnAbort(transaction_id, IndexedDBDatabaseError::Create(error)); |
| + } |
| + virtual void onComplete(long long transaction_id) { |
| + callbacks_->OnComplete(transaction_id); |
| + } |
| + |
| + private: |
| + scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks_; |
| +}; |
| + |
| +TEST(IndexedDBDatabaseTest, ForcedClose) { |
| + scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| + new IndexedDBFakeBackingStore(); |
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| + |
| + IndexedDBFactoryImpl* factory = 0; |
| + scoped_refptr<IndexedDBDatabaseImpl> backend = |
| + IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| + backing_store.get(), |
| + factory, |
| + ASCIIToUTF16("uniqueid")); |
| + // EXPECT_GT(backing_store->RefCount(), 1); |
| + |
| + scoped_refptr<MockIDBDatabaseCallbacks> connection = |
| + MockIDBDatabaseCallbacks::Create(); |
| + scoped_refptr<IndexedDBDatabaseCallbacksWrapper> connection_proxy = |
| + IndexedDBDatabaseCallbacksWrapper::Create( |
| + new WebIDBDatabaseCallbacksImpl(connection)); |
| + WebIDBDatabaseImpl web_database(backend, connection_proxy); |
| + |
| + scoped_refptr<MockIDBCallbacks> request = MockIDBCallbacks::Create(); |
| + const int64_t upgrade_transaction_id = 3; |
| + backend->OpenConnection(request, |
| + connection_proxy, |
| + upgrade_transaction_id, |
| + IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| + |
| + const int64_t transaction_id = 123; |
| + const std::vector<int64_t> scope; |
| + web_database.createTransaction( |
| + transaction_id, 0, scope, indexed_db::TRANSACTION_READ_ONLY); |
| + |
| + web_database.forceClose(); |
| + |
| + EXPECT_TRUE(backing_store->HasOneRef()); |
| +} |
| + |
| +} // namespace |