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"
24 using base::ASCIIToUTF16
;
27 const int FAKE_CHILD_PROCESS_ID
= 0;
32 TEST(IndexedDBDatabaseTest
, BackingStoreRetention
) {
33 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
34 new IndexedDBFakeBackingStore();
35 EXPECT_TRUE(backing_store
->HasOneRef());
37 IndexedDBFactory
* factory
= 0;
39 scoped_refptr
<IndexedDBDatabase
> db
=
40 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
43 IndexedDBDatabase::Identifier(),
46 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
48 EXPECT_TRUE(backing_store
->HasOneRef()); // local
51 TEST(IndexedDBDatabaseTest
, ConnectionLifecycle
) {
52 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
53 new IndexedDBFakeBackingStore();
54 EXPECT_TRUE(backing_store
->HasOneRef()); // local
56 IndexedDBFactory
* factory
= 0;
58 scoped_refptr
<IndexedDBDatabase
> db
=
59 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
62 IndexedDBDatabase::Identifier(),
65 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
67 scoped_refptr
<MockIndexedDBCallbacks
> request1(new MockIndexedDBCallbacks());
68 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks1(
69 new MockIndexedDBDatabaseCallbacks());
70 const int64 transaction_id1
= 1;
71 IndexedDBPendingConnection
connection1(
74 FAKE_CHILD_PROCESS_ID
,
76 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
77 db
->OpenConnection(connection1
);
79 EXPECT_FALSE(backing_store
->HasOneRef()); // db, connection count > 0
81 scoped_refptr
<MockIndexedDBCallbacks
> request2(new MockIndexedDBCallbacks());
82 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks2(
83 new MockIndexedDBDatabaseCallbacks());
84 const int64 transaction_id2
= 2;
85 IndexedDBPendingConnection
connection2(
88 FAKE_CHILD_PROCESS_ID
,
90 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
91 db
->OpenConnection(connection2
);
93 EXPECT_FALSE(backing_store
->HasOneRef()); // local and connection
95 request1
->connection()->ForceClose();
96 EXPECT_FALSE(request1
->connection()->IsConnected());
98 EXPECT_FALSE(backing_store
->HasOneRef()); // local and connection
100 request2
->connection()->ForceClose();
101 EXPECT_FALSE(request2
->connection()->IsConnected());
103 EXPECT_TRUE(backing_store
->HasOneRef());
104 EXPECT_FALSE(db
->backing_store());
109 TEST(IndexedDBDatabaseTest
, ForcedClose
) {
110 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
111 new IndexedDBFakeBackingStore();
112 EXPECT_TRUE(backing_store
->HasOneRef());
114 IndexedDBFactory
* factory
= 0;
116 scoped_refptr
<IndexedDBDatabase
> database
=
117 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
120 IndexedDBDatabase::Identifier(),
123 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
125 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks(
126 new MockIndexedDBDatabaseCallbacks());
127 scoped_refptr
<MockIndexedDBCallbacks
> request(new MockIndexedDBCallbacks());
128 const int64 upgrade_transaction_id
= 3;
129 IndexedDBPendingConnection
connection(
132 FAKE_CHILD_PROCESS_ID
,
133 upgrade_transaction_id
,
134 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
135 database
->OpenConnection(connection
);
136 EXPECT_EQ(database
, request
->connection()->database());
138 const int64 transaction_id
= 123;
139 const std::vector
<int64
> scope
;
140 database
->CreateTransaction(transaction_id
,
141 request
->connection(),
143 indexed_db::TRANSACTION_READ_ONLY
);
145 request
->connection()->ForceClose();
147 EXPECT_TRUE(backing_store
->HasOneRef()); // local
148 EXPECT_TRUE(callbacks
->abort_called());
151 class MockDeleteCallbacks
: public IndexedDBCallbacks
{
153 MockDeleteCallbacks()
154 : IndexedDBCallbacks(NULL
, 0, 0),
155 blocked_called_(false),
156 success_called_(false) {}
158 virtual void OnBlocked(int64 existing_version
) OVERRIDE
{
159 blocked_called_
= true;
161 virtual void OnSuccess(int64
) OVERRIDE
{ success_called_
= true; }
163 bool blocked_called() const { return blocked_called_
; }
164 bool success_called() const { return success_called_
; }
167 virtual ~MockDeleteCallbacks() {}
169 bool blocked_called_
;
170 bool success_called_
;
173 TEST(IndexedDBDatabaseTest
, PendingDelete
) {
174 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
175 new IndexedDBFakeBackingStore();
176 EXPECT_TRUE(backing_store
->HasOneRef()); // local
178 IndexedDBFactory
* factory
= 0;
180 scoped_refptr
<IndexedDBDatabase
> db
=
181 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
184 IndexedDBDatabase::Identifier(),
187 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
189 scoped_refptr
<MockIndexedDBCallbacks
> request1(new MockIndexedDBCallbacks());
190 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks1(
191 new MockIndexedDBDatabaseCallbacks());
192 const int64 transaction_id1
= 1;
193 IndexedDBPendingConnection
connection(
196 FAKE_CHILD_PROCESS_ID
,
198 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
199 db
->OpenConnection(connection
);
201 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
203 scoped_refptr
<MockDeleteCallbacks
> request2(new MockDeleteCallbacks());
204 db
->DeleteDatabase(request2
);
206 EXPECT_TRUE(request2
->blocked_called());
207 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
209 db
->Close(request1
->connection(), true /* forced */);
211 EXPECT_FALSE(db
->backing_store());
212 EXPECT_TRUE(backing_store
->HasOneRef()); // local
213 EXPECT_TRUE(request2
->success_called());
216 } // namespace content