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;
38 scoped_refptr
<IndexedDBDatabase
> db
= IndexedDBDatabase::Create(
42 IndexedDBDatabase::Identifier());
43 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
45 EXPECT_TRUE(backing_store
->HasOneRef()); // local
48 TEST(IndexedDBDatabaseTest
, ConnectionLifecycle
) {
49 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
50 new IndexedDBFakeBackingStore();
51 EXPECT_TRUE(backing_store
->HasOneRef()); // local
53 IndexedDBFactory
* factory
= 0;
54 scoped_refptr
<IndexedDBDatabase
> db
=
55 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
58 IndexedDBDatabase::Identifier());
60 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
62 scoped_refptr
<MockIndexedDBCallbacks
> request1(new MockIndexedDBCallbacks());
63 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks1(
64 new MockIndexedDBDatabaseCallbacks());
65 const int64 transaction_id1
= 1;
66 IndexedDBPendingConnection
connection1(
69 FAKE_CHILD_PROCESS_ID
,
71 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
72 db
->OpenConnection(connection1
);
74 EXPECT_FALSE(backing_store
->HasOneRef()); // db, connection count > 0
76 scoped_refptr
<MockIndexedDBCallbacks
> request2(new MockIndexedDBCallbacks());
77 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks2(
78 new MockIndexedDBDatabaseCallbacks());
79 const int64 transaction_id2
= 2;
80 IndexedDBPendingConnection
connection2(
83 FAKE_CHILD_PROCESS_ID
,
85 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
86 db
->OpenConnection(connection2
);
88 EXPECT_FALSE(backing_store
->HasOneRef()); // local and connection
90 request1
->connection()->ForceClose();
91 EXPECT_FALSE(request1
->connection()->IsConnected());
93 EXPECT_FALSE(backing_store
->HasOneRef()); // local and connection
95 request2
->connection()->ForceClose();
96 EXPECT_FALSE(request2
->connection()->IsConnected());
98 EXPECT_TRUE(backing_store
->HasOneRef());
99 EXPECT_FALSE(db
->backing_store());
104 TEST(IndexedDBDatabaseTest
, ForcedClose
) {
105 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
106 new IndexedDBFakeBackingStore();
107 EXPECT_TRUE(backing_store
->HasOneRef());
109 IndexedDBFactory
* factory
= 0;
110 scoped_refptr
<IndexedDBDatabase
> database
=
111 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
114 IndexedDBDatabase::Identifier());
116 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
118 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks(
119 new MockIndexedDBDatabaseCallbacks());
120 scoped_refptr
<MockIndexedDBCallbacks
> request(new MockIndexedDBCallbacks());
121 const int64 upgrade_transaction_id
= 3;
122 IndexedDBPendingConnection
connection(
125 FAKE_CHILD_PROCESS_ID
,
126 upgrade_transaction_id
,
127 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
128 database
->OpenConnection(connection
);
129 EXPECT_EQ(database
, request
->connection()->database());
131 const int64 transaction_id
= 123;
132 const std::vector
<int64
> scope
;
133 database
->CreateTransaction(transaction_id
,
134 request
->connection(),
136 indexed_db::TRANSACTION_READ_ONLY
);
138 request
->connection()->ForceClose();
140 EXPECT_TRUE(backing_store
->HasOneRef()); // local
141 EXPECT_TRUE(callbacks
->abort_called());
144 class MockDeleteCallbacks
: public IndexedDBCallbacks
{
146 MockDeleteCallbacks()
147 : IndexedDBCallbacks(NULL
, 0, 0),
148 blocked_called_(false),
149 success_void_called_(false) {}
151 virtual void OnBlocked(int64 existing_version
) OVERRIDE
{
152 blocked_called_
= true;
154 virtual void OnSuccess() OVERRIDE
{ success_void_called_
= true; }
156 bool blocked_called() const { return blocked_called_
; }
159 virtual ~MockDeleteCallbacks() { EXPECT_TRUE(success_void_called_
); }
161 bool blocked_called_
;
162 bool success_void_called_
;
165 TEST(IndexedDBDatabaseTest
, PendingDelete
) {
166 scoped_refptr
<IndexedDBFakeBackingStore
> backing_store
=
167 new IndexedDBFakeBackingStore();
168 EXPECT_TRUE(backing_store
->HasOneRef()); // local
170 IndexedDBFactory
* factory
= 0;
171 scoped_refptr
<IndexedDBDatabase
> db
=
172 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
175 IndexedDBDatabase::Identifier());
177 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
179 scoped_refptr
<MockIndexedDBCallbacks
> request1(new MockIndexedDBCallbacks());
180 scoped_refptr
<MockIndexedDBDatabaseCallbacks
> callbacks1(
181 new MockIndexedDBDatabaseCallbacks());
182 const int64 transaction_id1
= 1;
183 IndexedDBPendingConnection
connection(
186 FAKE_CHILD_PROCESS_ID
,
188 IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION
);
189 db
->OpenConnection(connection
);
191 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
193 scoped_refptr
<MockDeleteCallbacks
> request2(new MockDeleteCallbacks());
194 db
->DeleteDatabase(request2
);
196 EXPECT_TRUE(request2
->blocked_called());
197 EXPECT_FALSE(backing_store
->HasOneRef()); // local and db
199 db
->Close(request1
->connection(), true /* forced */);
201 EXPECT_FALSE(db
->backing_store());
202 EXPECT_TRUE(backing_store
->HasOneRef()); // local
205 } // namespace content