OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/indexed_db/indexed_db_database.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "content/browser/indexed_db/indexed_db.h" |
| 12 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 13 #include "content/browser/indexed_db/indexed_db_callbacks_wrapper.h" |
| 14 #include "content/browser/indexed_db/indexed_db_cursor.h" |
| 15 #include "content/browser/indexed_db/indexed_db_database_impl.h" |
| 16 #include "content/browser/indexed_db/indexed_db_factory_impl.h" |
| 17 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
| 18 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 19 #include "content/browser/indexed_db/webidbdatabase_impl.h" |
| 20 |
| 21 #include <gtest/gtest.h> |
| 22 |
| 23 using WebKit::WebIDBDatabase; |
| 24 using WebKit::WebIDBDatabaseError; |
| 25 using WebKit::WebIDBDatabaseCallbacks; |
| 26 |
| 27 namespace content { |
| 28 |
| 29 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { |
| 30 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| 31 new IndexedDBFakeBackingStore(); |
| 32 EXPECT_TRUE(backing_store->HasOneRef()); |
| 33 |
| 34 IndexedDBFactoryImpl* factory = 0; |
| 35 scoped_refptr<IndexedDBDatabaseImpl> db = |
| 36 IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| 37 backing_store.get(), |
| 38 factory, |
| 39 ASCIIToUTF16("uniqueid")); |
| 40 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
| 41 db = NULL; |
| 42 EXPECT_TRUE(backing_store->HasOneRef()); // local |
| 43 } |
| 44 |
| 45 class MockIDBCallbacks : public IndexedDBCallbacksWrapper { |
| 46 public: |
| 47 static scoped_refptr<MockIDBCallbacks> Create() { |
| 48 return make_scoped_refptr(new MockIDBCallbacks()); |
| 49 } |
| 50 virtual void OnError(scoped_refptr<IndexedDBDatabaseError>) OVERRIDE {} |
| 51 virtual void OnSuccess(const std::vector<string16>&) OVERRIDE {} |
| 52 virtual void OnSuccess(scoped_refptr<IndexedDBCursor> cursor, |
| 53 const IndexedDBKey&, |
| 54 const IndexedDBKey&, |
| 55 std::vector<char>*) OVERRIDE {} |
| 56 virtual void OnSuccess(scoped_refptr<IndexedDBDatabase>, |
| 57 const IndexedDBDatabaseMetadata&) OVERRIDE { |
| 58 was_success_db_called_ = true; |
| 59 } |
| 60 virtual void OnSuccess(const IndexedDBKey&) OVERRIDE {} |
| 61 virtual void OnSuccess(std::vector<char>*) OVERRIDE {} |
| 62 virtual void OnSuccess(std::vector<char>*, |
| 63 const IndexedDBKey&, |
| 64 const IndexedDBKeyPath&) OVERRIDE {} |
| 65 virtual void OnSuccess(int64) OVERRIDE {} |
| 66 virtual void OnSuccess() OVERRIDE {} |
| 67 virtual void OnSuccess(const IndexedDBKey&, |
| 68 const IndexedDBKey&, |
| 69 std::vector<char>*) OVERRIDE {} |
| 70 virtual void OnSuccessWithPrefetch( |
| 71 const std::vector<IndexedDBKey>& keys, |
| 72 const std::vector<IndexedDBKey>& primary_keys, |
| 73 const std::vector<std::vector<char> >& values) OVERRIDE {} |
| 74 |
| 75 private: |
| 76 virtual ~MockIDBCallbacks() { EXPECT_TRUE(was_success_db_called_); } |
| 77 MockIDBCallbacks() |
| 78 : IndexedDBCallbacksWrapper(NULL), was_success_db_called_(false) {} |
| 79 bool was_success_db_called_; |
| 80 }; |
| 81 |
| 82 class FakeIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper { |
| 83 public: |
| 84 static scoped_refptr<FakeIDBDatabaseCallbacks> Create() { |
| 85 return make_scoped_refptr(new FakeIDBDatabaseCallbacks()); |
| 86 } |
| 87 virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {} |
| 88 virtual void OnForcedClose() OVERRIDE {} |
| 89 virtual void OnAbort(int64 transaction_id, |
| 90 scoped_refptr<IndexedDBDatabaseError> error) OVERRIDE {} |
| 91 virtual void OnComplete(int64 transaction_id) OVERRIDE {} |
| 92 |
| 93 private: |
| 94 friend class base::RefCounted<FakeIDBDatabaseCallbacks>; |
| 95 virtual ~FakeIDBDatabaseCallbacks() {} |
| 96 FakeIDBDatabaseCallbacks() : IndexedDBDatabaseCallbacksWrapper(NULL) {} |
| 97 }; |
| 98 |
| 99 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { |
| 100 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| 101 new IndexedDBFakeBackingStore(); |
| 102 EXPECT_TRUE(backing_store->HasOneRef()); // local |
| 103 |
| 104 IndexedDBFactoryImpl* factory = 0; |
| 105 scoped_refptr<IndexedDBDatabaseImpl> db = |
| 106 IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| 107 backing_store.get(), |
| 108 factory, |
| 109 ASCIIToUTF16("uniqueid")); |
| 110 |
| 111 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
| 112 |
| 113 scoped_refptr<MockIDBCallbacks> request1 = MockIDBCallbacks::Create(); |
| 114 scoped_refptr<FakeIDBDatabaseCallbacks> connection1 = |
| 115 FakeIDBDatabaseCallbacks::Create(); |
| 116 const int64 transaction_id1 = 1; |
| 117 db->OpenConnection(request1, |
| 118 connection1, |
| 119 transaction_id1, |
| 120 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 121 |
| 122 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0 |
| 123 |
| 124 scoped_refptr<MockIDBCallbacks> request2 = MockIDBCallbacks::Create(); |
| 125 scoped_refptr<FakeIDBDatabaseCallbacks> connection2 = |
| 126 FakeIDBDatabaseCallbacks::Create(); |
| 127 const int64 transaction_id2 = 2; |
| 128 db->OpenConnection(request2, |
| 129 connection2, |
| 130 transaction_id2, |
| 131 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 132 |
| 133 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection |
| 134 |
| 135 db->Close(connection1); |
| 136 |
| 137 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection |
| 138 |
| 139 db->Close(connection2); |
| 140 EXPECT_TRUE(backing_store->HasOneRef()); |
| 141 EXPECT_FALSE(db->BackingStore()); |
| 142 |
| 143 db = NULL; |
| 144 } |
| 145 |
| 146 class MockIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacksWrapper { |
| 147 public: |
| 148 static scoped_refptr<MockIDBDatabaseCallbacks> Create() { |
| 149 return make_scoped_refptr(new MockIDBDatabaseCallbacks()); |
| 150 } |
| 151 virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {} |
| 152 virtual void OnForcedClose() OVERRIDE {} |
| 153 virtual void OnAbort(int64 transaction_id, |
| 154 scoped_refptr<IndexedDBDatabaseError> error) OVERRIDE { |
| 155 was_abort_called_ = true; |
| 156 } |
| 157 virtual void OnComplete(int64 transaction_id) OVERRIDE {} |
| 158 |
| 159 private: |
| 160 MockIDBDatabaseCallbacks() |
| 161 : IndexedDBDatabaseCallbacksWrapper(NULL), was_abort_called_(false) {} |
| 162 virtual ~MockIDBDatabaseCallbacks() { EXPECT_TRUE(was_abort_called_); } |
| 163 bool was_abort_called_; |
| 164 }; |
| 165 |
| 166 class WebIDBDatabaseCallbacksImpl : public WebIDBDatabaseCallbacks { |
| 167 public: |
| 168 explicit WebIDBDatabaseCallbacksImpl( |
| 169 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks) |
| 170 : callbacks_(callbacks) {} |
| 171 virtual ~WebIDBDatabaseCallbacksImpl() {} |
| 172 |
| 173 virtual void onForcedClose() { callbacks_->OnForcedClose(); } |
| 174 virtual void onVersionChange(long long old_version, long long new_version) { |
| 175 callbacks_->OnVersionChange(old_version, new_version); |
| 176 } |
| 177 virtual void onAbort(long long transaction_id, |
| 178 const WebIDBDatabaseError& error) { |
| 179 callbacks_->OnAbort(transaction_id, IndexedDBDatabaseError::Create(error)); |
| 180 } |
| 181 virtual void onComplete(long long transaction_id) { |
| 182 callbacks_->OnComplete(transaction_id); |
| 183 } |
| 184 |
| 185 private: |
| 186 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks_; |
| 187 }; |
| 188 |
| 189 TEST(IndexedDBDatabaseTest, ForcedClose) { |
| 190 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| 191 new IndexedDBFakeBackingStore(); |
| 192 EXPECT_TRUE(backing_store->HasOneRef()); |
| 193 |
| 194 IndexedDBFactoryImpl* factory = 0; |
| 195 scoped_refptr<IndexedDBDatabaseImpl> backend = |
| 196 IndexedDBDatabaseImpl::Create(ASCIIToUTF16("db"), |
| 197 backing_store.get(), |
| 198 factory, |
| 199 ASCIIToUTF16("uniqueid")); |
| 200 |
| 201 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
| 202 |
| 203 scoped_refptr<MockIDBDatabaseCallbacks> connection = |
| 204 MockIDBDatabaseCallbacks::Create(); |
| 205 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> connection_proxy = |
| 206 IndexedDBDatabaseCallbacksWrapper::Create( |
| 207 new WebIDBDatabaseCallbacksImpl(connection)); |
| 208 WebIDBDatabaseImpl web_database(backend, connection_proxy); |
| 209 |
| 210 scoped_refptr<MockIDBCallbacks> request = MockIDBCallbacks::Create(); |
| 211 const int64 upgrade_transaction_id = 3; |
| 212 backend->OpenConnection(request, |
| 213 connection_proxy, |
| 214 upgrade_transaction_id, |
| 215 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 216 |
| 217 const int64 transaction_id = 123; |
| 218 const std::vector<int64> scope; |
| 219 web_database.createTransaction( |
| 220 transaction_id, 0, scope, indexed_db::TRANSACTION_READ_ONLY); |
| 221 |
| 222 web_database.forceClose(); |
| 223 |
| 224 EXPECT_TRUE(backing_store->HasOneRef()); // local |
| 225 } |
| 226 |
| 227 } // namespace content |
OLD | NEW |