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
3 // found in the LICENSE file.
5 #include "content/browser/indexed_db/indexed_db_database.h"
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "base/strings/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.h"
14 #include "content/browser/indexed_db/indexed_db_connection.h"
15 #include "content/browser/indexed_db/indexed_db_cursor.h"
16 #include "content/browser/indexed_db/indexed_db_database.h"
17 #include "content/browser/indexed_db/indexed_db_factory.h"
18 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h"
19 #include "content/browser/indexed_db/indexed_db_transaction.h"
20 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
21 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 TEST(IndexedDBDatabaseTest, BackingStoreRetention) {
27 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
28 new IndexedDBFakeBackingStore();
29 EXPECT_TRUE(backing_store->HasOneRef());
31 IndexedDBFactory* factory = 0;
32 scoped_refptr<IndexedDBDatabase> db = IndexedDBDatabase::Create(
36 IndexedDBDatabase::Identifier());
37 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
39 EXPECT_TRUE(backing_store->HasOneRef()); // local
42 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) {
43 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
44 new IndexedDBFakeBackingStore();
45 EXPECT_TRUE(backing_store->HasOneRef()); // local
47 IndexedDBFactory* factory = 0;
48 scoped_refptr<IndexedDBDatabase> db =
49 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
52 IndexedDBDatabase::Identifier());
54 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
56 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks());
57 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1(
58 new MockIndexedDBDatabaseCallbacks());
59 const int64 transaction_id1 = 1;
60 db->OpenConnection(request1,
63 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
65 EXPECT_FALSE(backing_store->HasOneRef()); // db, connection count > 0
67 scoped_refptr<MockIndexedDBCallbacks> request2(new MockIndexedDBCallbacks());
68 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks2(
69 new MockIndexedDBDatabaseCallbacks());
70 const int64 transaction_id2 = 2;
71 db->OpenConnection(request2,
74 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
76 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection
78 request1->connection()->ForceClose();
79 EXPECT_FALSE(request1->connection()->IsConnected());
81 EXPECT_FALSE(backing_store->HasOneRef()); // local and connection
83 request2->connection()->ForceClose();
84 EXPECT_FALSE(request2->connection()->IsConnected());
86 EXPECT_TRUE(backing_store->HasOneRef());
87 EXPECT_FALSE(db->backing_store());
92 TEST(IndexedDBDatabaseTest, ForcedClose) {
93 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
94 new IndexedDBFakeBackingStore();
95 EXPECT_TRUE(backing_store->HasOneRef());
97 IndexedDBFactory* factory = 0;
98 scoped_refptr<IndexedDBDatabase> database =
99 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
102 IndexedDBDatabase::Identifier());
104 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
106 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks(
107 new MockIndexedDBDatabaseCallbacks());
108 scoped_refptr<MockIndexedDBCallbacks> request(new MockIndexedDBCallbacks());
109 const int64 upgrade_transaction_id = 3;
110 database->OpenConnection(request,
112 upgrade_transaction_id,
113 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
114 EXPECT_EQ(database, request->connection()->database());
116 const int64 transaction_id = 123;
117 const std::vector<int64> scope;
118 database->CreateTransaction(transaction_id,
119 request->connection(),
121 indexed_db::TRANSACTION_READ_ONLY);
123 request->connection()->ForceClose();
125 EXPECT_TRUE(backing_store->HasOneRef()); // local
126 EXPECT_TRUE(callbacks->abort_called());
129 class MockDeleteCallbacks : public IndexedDBCallbacks {
131 MockDeleteCallbacks()
132 : IndexedDBCallbacks(NULL, 0, 0),
133 blocked_called_(false),
134 success_void_called_(false) {}
136 virtual void OnBlocked(int64 existing_version) OVERRIDE {
137 blocked_called_ = true;
139 virtual void OnSuccess() OVERRIDE { success_void_called_ = true; }
141 bool blocked_called() const { return blocked_called_; }
144 virtual ~MockDeleteCallbacks() { EXPECT_TRUE(success_void_called_); }
146 bool blocked_called_;
147 bool success_void_called_;
150 TEST(IndexedDBDatabaseTest, PendingDelete) {
151 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
152 new IndexedDBFakeBackingStore();
153 EXPECT_TRUE(backing_store->HasOneRef()); // local
155 IndexedDBFactory* factory = 0;
156 scoped_refptr<IndexedDBDatabase> db =
157 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
160 IndexedDBDatabase::Identifier());
162 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
164 scoped_refptr<MockIndexedDBCallbacks> request1(new MockIndexedDBCallbacks());
165 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks1(
166 new MockIndexedDBDatabaseCallbacks());
167 const int64 transaction_id1 = 1;
168 db->OpenConnection(request1,
171 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
173 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
175 scoped_refptr<MockDeleteCallbacks> request2(new MockDeleteCallbacks());
176 db->DeleteDatabase(request2);
178 EXPECT_TRUE(request2->blocked_called());
179 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
181 db->Close(request1->connection(), true /* forced */);
183 EXPECT_FALSE(db->backing_store());
184 EXPECT_TRUE(backing_store->HasOneRef()); // local
187 } // namespace content