| 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 "content/browser/indexed_db/indexed_db_database.h" | 5 #include "content/browser/indexed_db/indexed_db_database.h" |
| 6 | 6 |
| 7 #include <gtest/gtest.h> | 7 #include <gtest/gtest.h> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/browser/indexed_db/indexed_db.h" | 13 #include "content/browser/indexed_db/indexed_db.h" |
| 14 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 14 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 15 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 15 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
| 16 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 16 #include "content/browser/indexed_db/indexed_db_cursor.h" | 17 #include "content/browser/indexed_db/indexed_db_cursor.h" |
| 17 #include "content/browser/indexed_db/indexed_db_database.h" | 18 #include "content/browser/indexed_db/indexed_db_database.h" |
| 18 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" | 19 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" |
| 19 #include "content/browser/indexed_db/indexed_db_factory.h" | 20 #include "content/browser/indexed_db/indexed_db_factory.h" |
| 20 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 21 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
| 21 #include "content/browser/indexed_db/indexed_db_transaction.h" | 22 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 22 #include "content/browser/indexed_db/webidbdatabase_impl.h" | |
| 23 | 23 |
| 24 namespace content { | 24 namespace content { |
| 25 | 25 |
| 26 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { | 26 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { |
| 27 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 27 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
| 28 new IndexedDBFakeBackingStore(); | 28 new IndexedDBFakeBackingStore(); |
| 29 EXPECT_TRUE(backing_store->HasOneRef()); | 29 EXPECT_TRUE(backing_store->HasOneRef()); |
| 30 | 30 |
| 31 IndexedDBFactory* factory = 0; | 31 IndexedDBFactory* factory = 0; |
| 32 scoped_refptr<IndexedDBDatabase> db = | 32 scoped_refptr<IndexedDBDatabase> db = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 43 public: | 43 public: |
| 44 static scoped_refptr<MockIDBCallbacks> Create() { | 44 static scoped_refptr<MockIDBCallbacks> Create() { |
| 45 return make_scoped_refptr(new MockIDBCallbacks()); | 45 return make_scoped_refptr(new MockIDBCallbacks()); |
| 46 } | 46 } |
| 47 virtual void OnError(const IndexedDBDatabaseError& error) OVERRIDE {} | 47 virtual void OnError(const IndexedDBDatabaseError& error) OVERRIDE {} |
| 48 virtual void OnSuccess(const std::vector<string16>& value) OVERRIDE {} | 48 virtual void OnSuccess(const std::vector<string16>& value) OVERRIDE {} |
| 49 virtual void OnSuccess(scoped_refptr<IndexedDBCursor> cursor, | 49 virtual void OnSuccess(scoped_refptr<IndexedDBCursor> cursor, |
| 50 const IndexedDBKey& key, | 50 const IndexedDBKey& key, |
| 51 const IndexedDBKey& primary_key, | 51 const IndexedDBKey& primary_key, |
| 52 std::vector<char>* value) OVERRIDE {} | 52 std::vector<char>* value) OVERRIDE {} |
| 53 virtual void OnSuccess(scoped_refptr<IndexedDBDatabase> db, | 53 virtual void OnSuccess(scoped_ptr<IndexedDBConnection> connection, |
| 54 const IndexedDBDatabaseMetadata& metadata) OVERRIDE { | 54 const IndexedDBDatabaseMetadata& metadata) OVERRIDE { |
| 55 was_success_db_called_ = true; | 55 connection_ = connection.Pass(); |
| 56 } | 56 } |
| 57 virtual void OnSuccess(const IndexedDBKey& key) OVERRIDE {} | 57 virtual void OnSuccess(const IndexedDBKey& key) OVERRIDE {} |
| 58 virtual void OnSuccess(std::vector<char>* value) OVERRIDE {} | 58 virtual void OnSuccess(std::vector<char>* value) OVERRIDE {} |
| 59 virtual void OnSuccess(std::vector<char>* value, | 59 virtual void OnSuccess(std::vector<char>* value, |
| 60 const IndexedDBKey& key, | 60 const IndexedDBKey& key, |
| 61 const IndexedDBKeyPath& key_path) OVERRIDE {} | 61 const IndexedDBKeyPath& key_path) OVERRIDE {} |
| 62 virtual void OnSuccess(int64 value) OVERRIDE {} | 62 virtual void OnSuccess(int64 value) OVERRIDE {} |
| 63 virtual void OnSuccess() OVERRIDE {} | 63 virtual void OnSuccess() OVERRIDE {} |
| 64 virtual void OnSuccess(const IndexedDBKey& key, | 64 virtual void OnSuccess(const IndexedDBKey& key, |
| 65 const IndexedDBKey& primary_key, | 65 const IndexedDBKey& primary_key, |
| 66 std::vector<char>* value) OVERRIDE {} | 66 std::vector<char>* value) OVERRIDE {} |
| 67 virtual void OnSuccessWithPrefetch( | 67 virtual void OnSuccessWithPrefetch( |
| 68 const std::vector<IndexedDBKey>& keys, | 68 const std::vector<IndexedDBKey>& keys, |
| 69 const std::vector<IndexedDBKey>& primary_keys, | 69 const std::vector<IndexedDBKey>& primary_keys, |
| 70 const std::vector<std::vector<char> >& values) OVERRIDE {} | 70 const std::vector<std::vector<char> >& values) OVERRIDE {} |
| 71 | 71 |
| 72 IndexedDBConnection* connection() { return connection_.get(); } |
| 73 |
| 72 private: | 74 private: |
| 73 virtual ~MockIDBCallbacks() { EXPECT_TRUE(was_success_db_called_); } | 75 virtual ~MockIDBCallbacks() { EXPECT_TRUE(connection_); } |
| 74 MockIDBCallbacks() | 76 MockIDBCallbacks() : IndexedDBCallbacks(NULL, 0, 0) {} |
| 75 : IndexedDBCallbacks(NULL, 0, 0), was_success_db_called_(false) {} | 77 scoped_ptr<IndexedDBConnection> connection_; |
| 76 bool was_success_db_called_; | |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 class FakeIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacks { | 80 class FakeIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacks { |
| 80 public: | 81 public: |
| 81 static scoped_refptr<FakeIDBDatabaseCallbacks> Create() { | 82 static scoped_refptr<FakeIDBDatabaseCallbacks> Create() { |
| 82 return make_scoped_refptr(new FakeIDBDatabaseCallbacks()); | 83 return make_scoped_refptr(new FakeIDBDatabaseCallbacks()); |
| 83 } | 84 } |
| 84 virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {} | 85 virtual void OnVersionChange(int64 old_version, int64 new_version) OVERRIDE {} |
| 85 virtual void OnForcedClose() OVERRIDE {} | 86 virtual void OnForcedClose() OVERRIDE {} |
| 86 virtual void OnAbort(int64 transaction_id, | 87 virtual void OnAbort(int64 transaction_id, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 101 IndexedDBFactory* factory = 0; | 102 IndexedDBFactory* factory = 0; |
| 102 scoped_refptr<IndexedDBDatabase> db = | 103 scoped_refptr<IndexedDBDatabase> db = |
| 103 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 104 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
| 104 backing_store.get(), | 105 backing_store.get(), |
| 105 factory, | 106 factory, |
| 106 ASCIIToUTF16("uniqueid")); | 107 ASCIIToUTF16("uniqueid")); |
| 107 | 108 |
| 108 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 109 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
| 109 | 110 |
| 110 scoped_refptr<MockIDBCallbacks> request1 = MockIDBCallbacks::Create(); | 111 scoped_refptr<MockIDBCallbacks> request1 = MockIDBCallbacks::Create(); |
| 111 scoped_refptr<FakeIDBDatabaseCallbacks> connection1 = | 112 scoped_refptr<FakeIDBDatabaseCallbacks> callbacks1 = |
| 112 FakeIDBDatabaseCallbacks::Create(); | 113 FakeIDBDatabaseCallbacks::Create(); |
| 113 const int64 transaction_id1 = 1; | 114 const int64 transaction_id1 = 1; |
| 114 db->OpenConnection(request1, | 115 db->OpenConnection(request1, |
| 115 connection1, | 116 callbacks1, |
| 116 transaction_id1, | 117 transaction_id1, |
| 117 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 118 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 118 | 119 |
| 119 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0 | 120 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0 |
| 120 | 121 |
| 121 scoped_refptr<MockIDBCallbacks> request2 = MockIDBCallbacks::Create(); | 122 scoped_refptr<MockIDBCallbacks> request2 = MockIDBCallbacks::Create(); |
| 122 scoped_refptr<FakeIDBDatabaseCallbacks> connection2 = | 123 scoped_refptr<FakeIDBDatabaseCallbacks> callbacks2 = |
| 123 FakeIDBDatabaseCallbacks::Create(); | 124 FakeIDBDatabaseCallbacks::Create(); |
| 124 const int64 transaction_id2 = 2; | 125 const int64 transaction_id2 = 2; |
| 125 db->OpenConnection(request2, | 126 db->OpenConnection(request2, |
| 126 connection2, | 127 callbacks2, |
| 127 transaction_id2, | 128 transaction_id2, |
| 128 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 129 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 129 | 130 |
| 130 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection | 131 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection |
| 131 | 132 |
| 132 db->Close(connection1); | 133 db->Close(request1->connection()); |
| 133 | 134 |
| 134 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection | 135 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection |
| 135 | 136 |
| 136 db->Close(connection2); | 137 db->Close(request2->connection()); |
| 137 EXPECT_TRUE(backing_store->HasOneRef()); | 138 EXPECT_TRUE(backing_store->HasOneRef()); |
| 138 EXPECT_FALSE(db->BackingStore().get()); | 139 EXPECT_FALSE(db->BackingStore().get()); |
| 139 | 140 |
| 140 db = NULL; | 141 db = NULL; |
| 141 } | 142 } |
| 142 | 143 |
| 143 class MockIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacks { | 144 class MockIDBDatabaseCallbacks : public IndexedDBDatabaseCallbacks { |
| 144 public: | 145 public: |
| 145 static scoped_refptr<MockIDBDatabaseCallbacks> Create() { | 146 static scoped_refptr<MockIDBDatabaseCallbacks> Create() { |
| 146 return make_scoped_refptr(new MockIDBDatabaseCallbacks()); | 147 return make_scoped_refptr(new MockIDBDatabaseCallbacks()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 167 | 168 |
| 168 IndexedDBFactory* factory = 0; | 169 IndexedDBFactory* factory = 0; |
| 169 scoped_refptr<IndexedDBDatabase> backend = | 170 scoped_refptr<IndexedDBDatabase> backend = |
| 170 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 171 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
| 171 backing_store.get(), | 172 backing_store.get(), |
| 172 factory, | 173 factory, |
| 173 ASCIIToUTF16("uniqueid")); | 174 ASCIIToUTF16("uniqueid")); |
| 174 | 175 |
| 175 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 176 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
| 176 | 177 |
| 177 scoped_refptr<MockIDBDatabaseCallbacks> connection = | 178 scoped_refptr<MockIDBDatabaseCallbacks> callbacks = |
| 178 MockIDBDatabaseCallbacks::Create(); | 179 MockIDBDatabaseCallbacks::Create(); |
| 179 WebIDBDatabaseImpl web_database(backend, connection); | |
| 180 | 180 |
| 181 scoped_refptr<MockIDBCallbacks> request = MockIDBCallbacks::Create(); | 181 scoped_refptr<MockIDBCallbacks> request = MockIDBCallbacks::Create(); |
| 182 const int64 upgrade_transaction_id = 3; | 182 const int64 upgrade_transaction_id = 3; |
| 183 backend->OpenConnection(request, | 183 backend->OpenConnection(request, |
| 184 connection, | 184 callbacks, |
| 185 upgrade_transaction_id, | 185 upgrade_transaction_id, |
| 186 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); | 186 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION); |
| 187 | 187 |
| 188 const int64 transaction_id = 123; | 188 const int64 transaction_id = 123; |
| 189 const std::vector<int64> scope; | 189 const std::vector<int64> scope; |
| 190 web_database.createTransaction( | 190 request->connection()->database()->CreateTransaction( |
| 191 transaction_id, scope, indexed_db::TRANSACTION_READ_ONLY); | 191 transaction_id, |
| 192 request->connection(), |
| 193 scope, |
| 194 indexed_db::TRANSACTION_READ_ONLY); |
| 192 | 195 |
| 193 web_database.forceClose(); | 196 request->connection()->ForceClose(); |
| 194 | 197 |
| 195 EXPECT_TRUE(backing_store->HasOneRef()); // local | 198 EXPECT_TRUE(backing_store->HasOneRef()); // local |
| 196 } | 199 } |
| 197 | 200 |
| 198 } // namespace content | 201 } // namespace content |
| OLD | NEW |