Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_database_unittest.cc
blob7feed8201d7ac179ded65f9390f22d8b21885a45
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;
26 namespace {
27 const int FAKE_CHILD_PROCESS_ID = 0;
30 namespace content {
32 TEST(IndexedDBDatabaseTest, BackingStoreRetention) {
33 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
34 new IndexedDBFakeBackingStore();
35 EXPECT_TRUE(backing_store->HasOneRef());
37 IndexedDBFactory* factory = 0;
38 leveldb::Status s;
39 scoped_refptr<IndexedDBDatabase> db =
40 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
41 backing_store,
42 factory,
43 IndexedDBDatabase::Identifier(),
44 &s);
45 ASSERT_TRUE(s.ok());
46 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
47 db = NULL;
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;
57 leveldb::Status s;
58 scoped_refptr<IndexedDBDatabase> db =
59 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
60 backing_store,
61 factory,
62 IndexedDBDatabase::Identifier(),
63 &s);
64 ASSERT_TRUE(s.ok());
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(
72 request1,
73 callbacks1,
74 FAKE_CHILD_PROCESS_ID,
75 transaction_id1,
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(
86 request2,
87 callbacks2,
88 FAKE_CHILD_PROCESS_ID,
89 transaction_id2,
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());
106 db = NULL;
109 TEST(IndexedDBDatabaseTest, ForcedClose) {
110 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
111 new IndexedDBFakeBackingStore();
112 EXPECT_TRUE(backing_store->HasOneRef());
114 IndexedDBFactory* factory = 0;
115 leveldb::Status s;
116 scoped_refptr<IndexedDBDatabase> database =
117 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
118 backing_store,
119 factory,
120 IndexedDBDatabase::Identifier(),
121 &s);
122 ASSERT_TRUE(s.ok());
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(
130 request,
131 callbacks,
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(),
142 scope,
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 {
152 public:
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_; }
166 private:
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;
179 leveldb::Status s;
180 scoped_refptr<IndexedDBDatabase> db =
181 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
182 backing_store,
183 factory,
184 IndexedDBDatabase::Identifier(),
185 &s);
186 ASSERT_TRUE(s.ok());
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(
194 request1,
195 callbacks1,
196 FAKE_CHILD_PROCESS_ID,
197 transaction_id1,
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