Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_database_unittest.cc
blob46a26ecf9c1c9e7abbd5dfbf72db628483f4206c
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 scoped_refptr<IndexedDBDatabase> db = IndexedDBDatabase::Create(
39 ASCIIToUTF16("db"),
40 backing_store,
41 factory,
42 IndexedDBDatabase::Identifier());
43 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
44 db = NULL;
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"),
56 backing_store,
57 factory,
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(
67 request1,
68 callbacks1,
69 FAKE_CHILD_PROCESS_ID,
70 transaction_id1,
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(
81 request2,
82 callbacks2,
83 FAKE_CHILD_PROCESS_ID,
84 transaction_id2,
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());
101 db = NULL;
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"),
112 backing_store,
113 factory,
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(
123 request,
124 callbacks,
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(),
135 scope,
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 {
145 public:
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_; }
158 private:
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"),
173 backing_store,
174 factory,
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(
184 request1,
185 callbacks1,
186 FAKE_CHILD_PROCESS_ID,
187 transaction_id1,
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