OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/indexed_db/indexed_db_database.h" | 5 #include "content/browser/indexed_db/indexed_db_database.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "content/browser/indexed_db/indexed_db.h" | 14 #include "content/browser/indexed_db/indexed_db.h" |
15 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 15 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
16 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 16 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
17 #include "content/browser/indexed_db/indexed_db_connection.h" | 17 #include "content/browser/indexed_db/indexed_db_connection.h" |
18 #include "content/browser/indexed_db/indexed_db_cursor.h" | 18 #include "content/browser/indexed_db/indexed_db_cursor.h" |
19 #include "content/browser/indexed_db/indexed_db_factory.h" | |
20 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 19 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
21 #include "content/browser/indexed_db/indexed_db_transaction.h" | 20 #include "content/browser/indexed_db/indexed_db_transaction.h" |
22 #include "content/browser/indexed_db/indexed_db_value.h" | 21 #include "content/browser/indexed_db/indexed_db_value.h" |
23 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" | 22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" |
24 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 23 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" |
| 24 #include "content/browser/indexed_db/mock_indexed_db_factory.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using base::ASCIIToUTF16; | 27 using base::ASCIIToUTF16; |
28 | 28 |
29 namespace { | 29 namespace { |
30 const int kFakeChildProcessId = 0; | 30 const int kFakeChildProcessId = 0; |
31 } | 31 } |
32 | 32 |
33 namespace content { | 33 namespace content { |
34 | 34 |
35 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { | 35 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { |
36 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 36 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
37 new IndexedDBFakeBackingStore(); | 37 new IndexedDBFakeBackingStore(); |
38 EXPECT_TRUE(backing_store->HasOneRef()); | 38 EXPECT_TRUE(backing_store->HasOneRef()); |
39 | 39 |
40 IndexedDBFactory* factory = 0; | 40 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
41 leveldb::Status s; | 41 leveldb::Status s; |
42 scoped_refptr<IndexedDBDatabase> db = | 42 scoped_refptr<IndexedDBDatabase> db = |
43 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 43 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
44 backing_store, | 44 backing_store, |
45 factory, | 45 factory, |
46 IndexedDBDatabase::Identifier(), | 46 IndexedDBDatabase::Identifier(), |
47 &s); | 47 &s); |
48 ASSERT_TRUE(s.ok()); | 48 ASSERT_TRUE(s.ok()); |
49 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 49 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
50 db = NULL; | 50 db = NULL; |
51 EXPECT_TRUE(backing_store->HasOneRef()); // local | 51 EXPECT_TRUE(backing_store->HasOneRef()); // local |
52 } | 52 } |
53 | 53 |
54 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { | 54 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { |
55 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 55 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
56 new IndexedDBFakeBackingStore(); | 56 new IndexedDBFakeBackingStore(); |
57 EXPECT_TRUE(backing_store->HasOneRef()); // local | 57 EXPECT_TRUE(backing_store->HasOneRef()); // local |
58 | 58 |
59 IndexedDBFactory* factory = 0; | 59 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
60 leveldb::Status s; | 60 leveldb::Status s; |
61 scoped_refptr<IndexedDBDatabase> db = | 61 scoped_refptr<IndexedDBDatabase> db = |
62 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 62 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
63 backing_store, | 63 backing_store, |
64 factory, | 64 factory, |
65 IndexedDBDatabase::Identifier(), | 65 IndexedDBDatabase::Identifier(), |
66 &s); | 66 &s); |
67 ASSERT_TRUE(s.ok()); | 67 ASSERT_TRUE(s.ok()); |
68 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 68 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
69 | 69 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 EXPECT_FALSE(db->backing_store()); | 107 EXPECT_FALSE(db->backing_store()); |
108 | 108 |
109 db = NULL; | 109 db = NULL; |
110 } | 110 } |
111 | 111 |
112 TEST(IndexedDBDatabaseTest, ForcedClose) { | 112 TEST(IndexedDBDatabaseTest, ForcedClose) { |
113 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 113 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
114 new IndexedDBFakeBackingStore(); | 114 new IndexedDBFakeBackingStore(); |
115 EXPECT_TRUE(backing_store->HasOneRef()); | 115 EXPECT_TRUE(backing_store->HasOneRef()); |
116 | 116 |
117 IndexedDBFactory* factory = 0; | 117 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
118 leveldb::Status s; | 118 leveldb::Status s; |
119 scoped_refptr<IndexedDBDatabase> database = | 119 scoped_refptr<IndexedDBDatabase> database = |
120 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 120 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
121 backing_store, | 121 backing_store, |
122 factory, | 122 factory, |
123 IndexedDBDatabase::Identifier(), | 123 IndexedDBDatabase::Identifier(), |
124 &s); | 124 &s); |
125 ASSERT_TRUE(s.ok()); | 125 ASSERT_TRUE(s.ok()); |
126 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 126 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
127 | 127 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 bool success_called_; | 173 bool success_called_; |
174 | 174 |
175 DISALLOW_COPY_AND_ASSIGN(MockDeleteCallbacks); | 175 DISALLOW_COPY_AND_ASSIGN(MockDeleteCallbacks); |
176 }; | 176 }; |
177 | 177 |
178 TEST(IndexedDBDatabaseTest, PendingDelete) { | 178 TEST(IndexedDBDatabaseTest, PendingDelete) { |
179 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 179 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
180 new IndexedDBFakeBackingStore(); | 180 new IndexedDBFakeBackingStore(); |
181 EXPECT_TRUE(backing_store->HasOneRef()); // local | 181 EXPECT_TRUE(backing_store->HasOneRef()); // local |
182 | 182 |
183 IndexedDBFactory* factory = 0; | 183 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
184 leveldb::Status s; | 184 leveldb::Status s; |
185 scoped_refptr<IndexedDBDatabase> db = | 185 scoped_refptr<IndexedDBDatabase> db = |
186 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 186 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
187 backing_store, | 187 backing_store, |
188 factory, | 188 factory, |
189 IndexedDBDatabase::Identifier(), | 189 IndexedDBDatabase::Identifier(), |
190 &s); | 190 &s); |
191 ASSERT_TRUE(s.ok()); | 191 ASSERT_TRUE(s.ok()); |
192 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 192 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
193 | 193 |
(...skipping 25 matching lines...) Expand all Loading... |
219 EXPECT_FALSE(db->backing_store()); | 219 EXPECT_FALSE(db->backing_store()); |
220 EXPECT_TRUE(backing_store->HasOneRef()); // local | 220 EXPECT_TRUE(backing_store->HasOneRef()); // local |
221 EXPECT_TRUE(request2->success_called()); | 221 EXPECT_TRUE(request2->success_called()); |
222 } | 222 } |
223 | 223 |
224 void DummyOperation(IndexedDBTransaction* transaction) { | 224 void DummyOperation(IndexedDBTransaction* transaction) { |
225 } | 225 } |
226 | 226 |
227 class IndexedDBDatabaseOperationTest : public testing::Test { | 227 class IndexedDBDatabaseOperationTest : public testing::Test { |
228 public: | 228 public: |
229 IndexedDBDatabaseOperationTest() : commit_success_(leveldb::Status::OK()) {} | 229 IndexedDBDatabaseOperationTest() |
| 230 : commit_success_(leveldb::Status::OK()), |
| 231 factory_(new MockIndexedDBFactory()) {} |
230 | 232 |
231 virtual void SetUp() { | 233 virtual void SetUp() { |
232 backing_store_ = new IndexedDBFakeBackingStore(); | 234 backing_store_ = new IndexedDBFakeBackingStore(); |
233 leveldb::Status s; | 235 leveldb::Status s; |
234 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 236 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
235 backing_store_, | 237 backing_store_, |
236 NULL /*factory*/, | 238 factory_, |
237 IndexedDBDatabase::Identifier(), | 239 IndexedDBDatabase::Identifier(), |
238 &s); | 240 &s); |
239 ASSERT_TRUE(s.ok()); | 241 ASSERT_TRUE(s.ok()); |
240 | 242 |
241 request_ = new MockIndexedDBCallbacks(); | 243 request_ = new MockIndexedDBCallbacks(); |
242 callbacks_ = new MockIndexedDBDatabaseCallbacks(); | 244 callbacks_ = new MockIndexedDBDatabaseCallbacks(); |
243 const int64 transaction_id = 1; | 245 const int64 transaction_id = 1; |
244 db_->OpenConnection(IndexedDBPendingConnection( | 246 db_->OpenConnection(IndexedDBPendingConnection( |
245 request_, | 247 request_, |
246 callbacks_, | 248 callbacks_, |
(...skipping 24 matching lines...) Expand all Loading... |
271 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; | 273 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; |
272 scoped_refptr<IndexedDBDatabase> db_; | 274 scoped_refptr<IndexedDBDatabase> db_; |
273 scoped_refptr<MockIndexedDBCallbacks> request_; | 275 scoped_refptr<MockIndexedDBCallbacks> request_; |
274 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; | 276 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; |
275 scoped_refptr<IndexedDBTransaction> transaction_; | 277 scoped_refptr<IndexedDBTransaction> transaction_; |
276 | 278 |
277 leveldb::Status commit_success_; | 279 leveldb::Status commit_success_; |
278 | 280 |
279 private: | 281 private: |
280 base::MessageLoop message_loop_; | 282 base::MessageLoop message_loop_; |
| 283 scoped_refptr<MockIndexedDBFactory> factory_; |
281 | 284 |
282 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); | 285 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); |
283 }; | 286 }; |
284 | 287 |
285 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { | 288 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { |
286 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 289 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
287 const int64 store_id = 1001; | 290 const int64 store_id = 1001; |
288 db_->CreateObjectStore(transaction_->id(), | 291 db_->CreateObjectStore(transaction_->id(), |
289 store_id, | 292 store_id, |
290 ASCIIToUTF16("store"), | 293 ASCIIToUTF16("store"), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 412 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
410 | 413 |
411 // This will execute the Put then Delete. | 414 // This will execute the Put then Delete. |
412 RunPostedTasks(); | 415 RunPostedTasks(); |
413 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 416 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
414 | 417 |
415 transaction_->Commit(); // Cleans up the object hierarchy. | 418 transaction_->Commit(); // Cleans up the object hierarchy. |
416 } | 419 } |
417 | 420 |
418 } // namespace content | 421 } // namespace content |
OLD | NEW |