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 <stdint.h> | 7 #include <stdint.h> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "content/browser/indexed_db/indexed_db.h" | 18 #include "content/browser/indexed_db/indexed_db.h" |
19 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 19 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
20 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 20 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
21 #include "content/browser/indexed_db/indexed_db_class_factory.h" | 21 #include "content/browser/indexed_db/indexed_db_class_factory.h" |
22 #include "content/browser/indexed_db/indexed_db_connection.h" | 22 #include "content/browser/indexed_db/indexed_db_connection.h" |
23 #include "content/browser/indexed_db/indexed_db_cursor.h" | 23 #include "content/browser/indexed_db/indexed_db_cursor.h" |
24 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 24 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
25 #include "content/browser/indexed_db/indexed_db_transaction.h" | 25 #include "content/browser/indexed_db/indexed_db_transaction.h" |
26 #include "content/browser/indexed_db/indexed_db_value.h" | 26 #include "content/browser/indexed_db/indexed_db_value.h" |
27 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" | 27 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" |
28 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 28 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" |
29 #include "content/browser/indexed_db/mock_indexed_db_factory.h" | 29 #include "content/browser/indexed_db/mock_indexed_db_factory.h" |
| 30 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
31 | 32 |
32 using base::ASCIIToUTF16; | 33 using base::ASCIIToUTF16; |
33 | 34 |
34 namespace { | 35 namespace { |
35 const int kFakeChildProcessId = 0; | 36 const int kFakeChildProcessId = 0; |
36 } | 37 } |
37 | 38 |
38 namespace content { | 39 namespace content { |
39 | 40 |
40 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { | 41 class IndexedDBDatabaseTest : public ::testing::Test { |
| 42 private: |
| 43 TestBrowserThreadBundle thread_bundle_; |
| 44 }; |
| 45 |
| 46 TEST_F(IndexedDBDatabaseTest, BackingStoreRetention) { |
41 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 47 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
42 new IndexedDBFakeBackingStore(); | 48 new IndexedDBFakeBackingStore(); |
43 EXPECT_TRUE(backing_store->HasOneRef()); | 49 EXPECT_TRUE(backing_store->HasOneRef()); |
44 | 50 |
45 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); | 51 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
46 leveldb::Status s; | 52 leveldb::Status s; |
47 scoped_refptr<IndexedDBDatabase> db = | 53 scoped_refptr<IndexedDBDatabase> db = |
48 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 54 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
49 backing_store.get(), | 55 backing_store.get(), |
50 factory.get(), | 56 factory.get(), |
51 IndexedDBDatabase::Identifier(), | 57 IndexedDBDatabase::Identifier(), |
52 &s); | 58 &s); |
53 ASSERT_TRUE(s.ok()); | 59 ASSERT_TRUE(s.ok()); |
54 EXPECT_FALSE(backing_store->HasOneRef()); // local and db | 60 EXPECT_FALSE(backing_store->HasOneRef()); // local and db |
55 db = NULL; | 61 db = NULL; |
56 EXPECT_TRUE(backing_store->HasOneRef()); // local | 62 EXPECT_TRUE(backing_store->HasOneRef()); // local |
57 } | 63 } |
58 | 64 |
59 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { | 65 TEST_F(IndexedDBDatabaseTest, ConnectionLifecycle) { |
60 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 66 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
61 new IndexedDBFakeBackingStore(); | 67 new IndexedDBFakeBackingStore(); |
62 EXPECT_TRUE(backing_store->HasOneRef()); // local | 68 EXPECT_TRUE(backing_store->HasOneRef()); // local |
63 | 69 |
64 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); | 70 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
65 leveldb::Status s; | 71 leveldb::Status s; |
66 scoped_refptr<IndexedDBDatabase> db = | 72 scoped_refptr<IndexedDBDatabase> db = |
67 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 73 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
68 backing_store.get(), | 74 backing_store.get(), |
69 factory.get(), | 75 factory.get(), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 109 |
104 request2->connection()->ForceClose(); | 110 request2->connection()->ForceClose(); |
105 EXPECT_FALSE(request2->connection()->IsConnected()); | 111 EXPECT_FALSE(request2->connection()->IsConnected()); |
106 | 112 |
107 EXPECT_TRUE(backing_store->HasOneRef()); | 113 EXPECT_TRUE(backing_store->HasOneRef()); |
108 EXPECT_FALSE(db->backing_store()); | 114 EXPECT_FALSE(db->backing_store()); |
109 | 115 |
110 db = NULL; | 116 db = NULL; |
111 } | 117 } |
112 | 118 |
113 TEST(IndexedDBDatabaseTest, ForcedClose) { | 119 TEST_F(IndexedDBDatabaseTest, ForcedClose) { |
114 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 120 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
115 new IndexedDBFakeBackingStore(); | 121 new IndexedDBFakeBackingStore(); |
116 EXPECT_TRUE(backing_store->HasOneRef()); | 122 EXPECT_TRUE(backing_store->HasOneRef()); |
117 | 123 |
118 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); | 124 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
119 leveldb::Status s; | 125 leveldb::Status s; |
120 scoped_refptr<IndexedDBDatabase> database = | 126 scoped_refptr<IndexedDBDatabase> database = |
121 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 127 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
122 backing_store.get(), | 128 backing_store.get(), |
123 factory.get(), | 129 factory.get(), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 ~MockCallbacks() override {} | 176 ~MockCallbacks() override {} |
171 | 177 |
172 bool blocked_called_ = false; | 178 bool blocked_called_ = false; |
173 bool success_called_ = false; | 179 bool success_called_ = false; |
174 bool error_called_ = false; | 180 bool error_called_ = false; |
175 bool valid_ = true; | 181 bool valid_ = true; |
176 | 182 |
177 DISALLOW_COPY_AND_ASSIGN(MockCallbacks); | 183 DISALLOW_COPY_AND_ASSIGN(MockCallbacks); |
178 }; | 184 }; |
179 | 185 |
180 TEST(IndexedDBDatabaseTest, PendingDelete) { | 186 TEST_F(IndexedDBDatabaseTest, PendingDelete) { |
181 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 187 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
182 new IndexedDBFakeBackingStore(); | 188 new IndexedDBFakeBackingStore(); |
183 EXPECT_TRUE(backing_store->HasOneRef()); // local | 189 EXPECT_TRUE(backing_store->HasOneRef()); // local |
184 | 190 |
185 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); | 191 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
186 leveldb::Status s; | 192 leveldb::Status s; |
187 scoped_refptr<IndexedDBDatabase> db = | 193 scoped_refptr<IndexedDBDatabase> db = |
188 IndexedDBDatabase::Create(ASCIIToUTF16("db"), | 194 IndexedDBDatabase::Create(ASCIIToUTF16("db"), |
189 backing_store.get(), | 195 backing_store.get(), |
190 factory.get(), | 196 factory.get(), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 db->Close(request1->connection(), true /* forced */); | 232 db->Close(request1->connection(), true /* forced */); |
227 EXPECT_EQ(db->ConnectionCount(), 0UL); | 233 EXPECT_EQ(db->ConnectionCount(), 0UL); |
228 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL); | 234 EXPECT_EQ(db->ActiveOpenDeleteCount(), 0UL); |
229 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); | 235 EXPECT_EQ(db->PendingOpenDeleteCount(), 0UL); |
230 | 236 |
231 EXPECT_FALSE(db->backing_store()); | 237 EXPECT_FALSE(db->backing_store()); |
232 EXPECT_TRUE(backing_store->HasOneRef()); // local | 238 EXPECT_TRUE(backing_store->HasOneRef()); // local |
233 EXPECT_TRUE(request2->success_called()); | 239 EXPECT_TRUE(request2->success_called()); |
234 } | 240 } |
235 | 241 |
236 TEST(IndexedDBDatabaseTest, ConnectionRequestsNoLongerValid) { | 242 TEST_F(IndexedDBDatabaseTest, ConnectionRequestsNoLongerValid) { |
237 scoped_refptr<IndexedDBFakeBackingStore> backing_store = | 243 scoped_refptr<IndexedDBFakeBackingStore> backing_store = |
238 new IndexedDBFakeBackingStore(); | 244 new IndexedDBFakeBackingStore(); |
239 | 245 |
240 const int64_t transaction_id1 = 1; | 246 const int64_t transaction_id1 = 1; |
241 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); | 247 scoped_refptr<MockIndexedDBFactory> factory = new MockIndexedDBFactory(); |
242 leveldb::Status s; | 248 leveldb::Status s; |
243 scoped_refptr<IndexedDBDatabase> db = IndexedDBDatabase::Create( | 249 scoped_refptr<IndexedDBDatabase> db = IndexedDBDatabase::Create( |
244 ASCIIToUTF16("db"), backing_store.get(), factory.get(), | 250 ASCIIToUTF16("db"), backing_store.get(), factory.get(), |
245 IndexedDBDatabase::Identifier(), &s); | 251 IndexedDBDatabase::Identifier(), &s); |
246 | 252 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; | 389 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; |
384 scoped_refptr<IndexedDBDatabase> db_; | 390 scoped_refptr<IndexedDBDatabase> db_; |
385 scoped_refptr<MockIndexedDBCallbacks> request_; | 391 scoped_refptr<MockIndexedDBCallbacks> request_; |
386 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; | 392 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; |
387 scoped_refptr<IndexedDBTransaction> transaction_; | 393 scoped_refptr<IndexedDBTransaction> transaction_; |
388 std::unique_ptr<IndexedDBConnection> connection_; | 394 std::unique_ptr<IndexedDBConnection> connection_; |
389 | 395 |
390 leveldb::Status commit_success_; | 396 leveldb::Status commit_success_; |
391 | 397 |
392 private: | 398 private: |
393 base::MessageLoop message_loop_; | |
394 scoped_refptr<MockIndexedDBFactory> factory_; | 399 scoped_refptr<MockIndexedDBFactory> factory_; |
| 400 content::TestBrowserThreadBundle thread_bundle_; |
395 | 401 |
396 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); | 402 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); |
397 }; | 403 }; |
398 | 404 |
399 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { | 405 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { |
400 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 406 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
401 const int64_t store_id = 1001; | 407 const int64_t store_id = 1001; |
402 db_->CreateObjectStore(transaction_->id(), | 408 db_->CreateObjectStore(transaction_->id(), |
403 store_id, | 409 store_id, |
404 ASCIIToUTF16("store"), | 410 ASCIIToUTF16("store"), |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); | 523 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); |
518 | 524 |
519 // This will execute the Put then Delete. | 525 // This will execute the Put then Delete. |
520 RunPostedTasks(); | 526 RunPostedTasks(); |
521 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); | 527 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); |
522 | 528 |
523 transaction_->Commit(); // Cleans up the object hierarchy. | 529 transaction_->Commit(); // Cleans up the object hierarchy. |
524 } | 530 } |
525 | 531 |
526 } // namespace content | 532 } // namespace content |
OLD | NEW |