Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: content/browser/indexed_db/indexed_db_database_unittest.cc

Issue 308103004: IndexedDB: Using a mock IndexedDBFactory for unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Forgot to add mock_indexed_db_factory.* files in last patch set. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "content/browser/indexed_db/indexed_db.h" 12 #include "content/browser/indexed_db/indexed_db.h"
13 #include "content/browser/indexed_db/indexed_db_backing_store.h" 13 #include "content/browser/indexed_db/indexed_db_backing_store.h"
14 #include "content/browser/indexed_db/indexed_db_callbacks.h" 14 #include "content/browser/indexed_db/indexed_db_callbacks.h"
15 #include "content/browser/indexed_db/indexed_db_connection.h" 15 #include "content/browser/indexed_db/indexed_db_connection.h"
16 #include "content/browser/indexed_db/indexed_db_cursor.h" 16 #include "content/browser/indexed_db/indexed_db_cursor.h"
17 #include "content/browser/indexed_db/indexed_db_database.h" 17 #include "content/browser/indexed_db/indexed_db_database.h"
18 #include "content/browser/indexed_db/indexed_db_factory.h" 18 #include "content/browser/indexed_db/indexed_db_factory.h"
19 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" 19 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h"
20 #include "content/browser/indexed_db/indexed_db_transaction.h" 20 #include "content/browser/indexed_db/indexed_db_transaction.h"
21 #include "content/browser/indexed_db/indexed_db_value.h" 21 #include "content/browser/indexed_db/indexed_db_value.h"
22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" 22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
23 #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 "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 using base::ASCIIToUTF16; 27 using base::ASCIIToUTF16;
27 28
28 namespace { 29 namespace {
29 const int kFakeChildProcessId = 0; 30 const int kFakeChildProcessId = 0;
30 } 31
32 class MockIDBFactory : public content::IndexedDBFactory {
33 public:
34 MOCK_METHOD2(ReleaseDatabase,
35 void(const content::IndexedDBDatabase::Identifier& identifier,
36 bool forcedClose));
37 MOCK_METHOD4(GetDatabaseNames,
38 void(scoped_refptr<content::IndexedDBCallbacks> callbacks,
39 const GURL& origin_url,
40 const base::FilePath& data_directory,
41 net::URLRequestContext* request_context));
42 MOCK_METHOD5(Open,
43 void(const base::string16& name,
44 const content::IndexedDBPendingConnection& connection,
45 net::URLRequestContext* request_context,
46 const GURL& origin_url,
47 const base::FilePath& data_directory));
48 MOCK_METHOD5(DeleteDatabase,
49 void(const base::string16& name,
50 net::URLRequestContext* request_context,
51 scoped_refptr<content::IndexedDBCallbacks> callbacks,
52 const GURL& origin_url,
53 const base::FilePath& data_directory));
54 MOCK_METHOD1(HandleBackingStoreFailure, void(const GURL& origin_url));
55 MOCK_METHOD2(HandleBackingStoreCorruption,
56 void(const GURL& origin_url,
57 const content::IndexedDBDatabaseError& error));
58 // mock template gets confused and needs typedef to compile.
59 typedef std::pair<OriginDBMapIterator, OriginDBMapIterator> OriginDBs;
60 MOCK_CONST_METHOD1(GetOpenDatabasesForOrigin,
61 OriginDBs(const GURL& origin_url));
62 MOCK_METHOD1(ForceClose, void(const GURL& origin_url));
63 MOCK_METHOD0(ContextDestroyed, void());
64 MOCK_METHOD1(DatabaseDeleted,
65 void(const content::IndexedDBDatabase::Identifier& identifier));
66 MOCK_CONST_METHOD1(GetConnectionCount, size_t(const GURL& origin_url));
67 MOCK_METHOD2(ReportOutstandingBlobs,
68 void(const GURL& origin_url, bool blobs_outstanding));
69
70 protected:
71 virtual ~MockIDBFactory() {}
72
73 MOCK_METHOD6(OpenBackingStore,
74 scoped_refptr<content::IndexedDBBackingStore>(
75 const GURL& origin_url,
76 const base::FilePath& data_directory,
77 net::URLRequestContext* request_context,
78 blink::WebIDBDataLoss* data_loss,
79 std::string* data_loss_reason,
80 bool* disk_full));
81
82 MOCK_METHOD7(OpenBackingStoreHelper,
83 scoped_refptr<content::IndexedDBBackingStore>(
84 const GURL& origin_url,
85 const base::FilePath& data_directory,
86 net::URLRequestContext* request_context,
87 blink::WebIDBDataLoss* data_loss,
88 std::string* data_loss_message,
89 bool* disk_full,
90 bool first_time));
91
92 private:
93 MOCK_CONST_METHOD1(IsBackingStoreOpen, bool(const GURL& origin_url));
94 };
95
96 } // anonymous namespace
31 97
32 namespace content { 98 namespace content {
33 99
34 TEST(IndexedDBDatabaseTest, BackingStoreRetention) { 100 TEST(IndexedDBDatabaseTest, BackingStoreRetention) {
35 scoped_refptr<IndexedDBFakeBackingStore> backing_store = 101 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
36 new IndexedDBFakeBackingStore(); 102 new IndexedDBFakeBackingStore();
37 EXPECT_TRUE(backing_store->HasOneRef()); 103 EXPECT_TRUE(backing_store->HasOneRef());
38 104
39 IndexedDBFactory* factory = 0; 105 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory();
40 leveldb::Status s; 106 leveldb::Status s;
41 scoped_refptr<IndexedDBDatabase> db = 107 scoped_refptr<IndexedDBDatabase> db =
42 IndexedDBDatabase::Create(ASCIIToUTF16("db"), 108 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
43 backing_store, 109 backing_store,
44 factory, 110 factory,
45 IndexedDBDatabase::Identifier(), 111 IndexedDBDatabase::Identifier(),
46 &s); 112 &s);
47 ASSERT_TRUE(s.ok()); 113 ASSERT_TRUE(s.ok());
48 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 114 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
49 db = NULL; 115 db = NULL;
50 EXPECT_TRUE(backing_store->HasOneRef()); // local 116 EXPECT_TRUE(backing_store->HasOneRef()); // local
51 } 117 }
52 118
53 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) { 119 TEST(IndexedDBDatabaseTest, ConnectionLifecycle) {
54 scoped_refptr<IndexedDBFakeBackingStore> backing_store = 120 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
55 new IndexedDBFakeBackingStore(); 121 new IndexedDBFakeBackingStore();
56 EXPECT_TRUE(backing_store->HasOneRef()); // local 122 EXPECT_TRUE(backing_store->HasOneRef()); // local
57 123
58 IndexedDBFactory* factory = 0; 124 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory();
59 leveldb::Status s; 125 leveldb::Status s;
60 scoped_refptr<IndexedDBDatabase> db = 126 scoped_refptr<IndexedDBDatabase> db =
61 IndexedDBDatabase::Create(ASCIIToUTF16("db"), 127 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
62 backing_store, 128 backing_store,
63 factory, 129 factory,
64 IndexedDBDatabase::Identifier(), 130 IndexedDBDatabase::Identifier(),
65 &s); 131 &s);
66 ASSERT_TRUE(s.ok()); 132 ASSERT_TRUE(s.ok());
67 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 133 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
68 134
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 EXPECT_FALSE(db->backing_store()); 172 EXPECT_FALSE(db->backing_store());
107 173
108 db = NULL; 174 db = NULL;
109 } 175 }
110 176
111 TEST(IndexedDBDatabaseTest, ForcedClose) { 177 TEST(IndexedDBDatabaseTest, ForcedClose) {
112 scoped_refptr<IndexedDBFakeBackingStore> backing_store = 178 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
113 new IndexedDBFakeBackingStore(); 179 new IndexedDBFakeBackingStore();
114 EXPECT_TRUE(backing_store->HasOneRef()); 180 EXPECT_TRUE(backing_store->HasOneRef());
115 181
116 IndexedDBFactory* factory = 0; 182 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory();
117 leveldb::Status s; 183 leveldb::Status s;
118 scoped_refptr<IndexedDBDatabase> database = 184 scoped_refptr<IndexedDBDatabase> database =
119 IndexedDBDatabase::Create(ASCIIToUTF16("db"), 185 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
120 backing_store, 186 backing_store,
121 factory, 187 factory,
122 IndexedDBDatabase::Identifier(), 188 IndexedDBDatabase::Identifier(),
123 &s); 189 &s);
124 ASSERT_TRUE(s.ok()); 190 ASSERT_TRUE(s.ok());
125 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 191 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
126 192
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 bool success_called_; 238 bool success_called_;
173 239
174 DISALLOW_COPY_AND_ASSIGN(MockDeleteCallbacks); 240 DISALLOW_COPY_AND_ASSIGN(MockDeleteCallbacks);
175 }; 241 };
176 242
177 TEST(IndexedDBDatabaseTest, PendingDelete) { 243 TEST(IndexedDBDatabaseTest, PendingDelete) {
178 scoped_refptr<IndexedDBFakeBackingStore> backing_store = 244 scoped_refptr<IndexedDBFakeBackingStore> backing_store =
179 new IndexedDBFakeBackingStore(); 245 new IndexedDBFakeBackingStore();
180 EXPECT_TRUE(backing_store->HasOneRef()); // local 246 EXPECT_TRUE(backing_store->HasOneRef()); // local
181 247
182 IndexedDBFactory* factory = 0; 248 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory();
183 leveldb::Status s; 249 leveldb::Status s;
184 scoped_refptr<IndexedDBDatabase> db = 250 scoped_refptr<IndexedDBDatabase> db =
185 IndexedDBDatabase::Create(ASCIIToUTF16("db"), 251 IndexedDBDatabase::Create(ASCIIToUTF16("db"),
186 backing_store, 252 backing_store,
187 factory, 253 factory,
188 IndexedDBDatabase::Identifier(), 254 IndexedDBDatabase::Identifier(),
189 &s); 255 &s);
190 ASSERT_TRUE(s.ok()); 256 ASSERT_TRUE(s.ok());
191 EXPECT_FALSE(backing_store->HasOneRef()); // local and db 257 EXPECT_FALSE(backing_store->HasOneRef()); // local and db
192 258
(...skipping 22 matching lines...) Expand all
215 EXPECT_FALSE(db->backing_store()); 281 EXPECT_FALSE(db->backing_store());
216 EXPECT_TRUE(backing_store->HasOneRef()); // local 282 EXPECT_TRUE(backing_store->HasOneRef()); // local
217 EXPECT_TRUE(request2->success_called()); 283 EXPECT_TRUE(request2->success_called());
218 } 284 }
219 285
220 void DummyOperation(IndexedDBTransaction* transaction) { 286 void DummyOperation(IndexedDBTransaction* transaction) {
221 } 287 }
222 288
223 class IndexedDBDatabaseOperationTest : public testing::Test { 289 class IndexedDBDatabaseOperationTest : public testing::Test {
224 public: 290 public:
225 IndexedDBDatabaseOperationTest() : commit_success_(leveldb::Status::OK()) {} 291 IndexedDBDatabaseOperationTest()
292 : commit_success_(leveldb::Status::OK()),
293 factory_(new MockIDBFactory()) {}
226 294
227 virtual void SetUp() { 295 virtual void SetUp() {
228 backing_store_ = new IndexedDBFakeBackingStore(); 296 backing_store_ = new IndexedDBFakeBackingStore();
229 leveldb::Status s; 297 leveldb::Status s;
230 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"), 298 db_ = IndexedDBDatabase::Create(ASCIIToUTF16("db"),
231 backing_store_, 299 backing_store_,
232 NULL /*factory*/, 300 factory_,
233 IndexedDBDatabase::Identifier(), 301 IndexedDBDatabase::Identifier(),
234 &s); 302 &s);
235 ASSERT_TRUE(s.ok()); 303 ASSERT_TRUE(s.ok());
236 304
237 request_ = new MockIndexedDBCallbacks(); 305 request_ = new MockIndexedDBCallbacks();
238 callbacks_ = new MockIndexedDBDatabaseCallbacks(); 306 callbacks_ = new MockIndexedDBDatabaseCallbacks();
239 const int64 transaction_id = 1; 307 const int64 transaction_id = 1;
240 db_->OpenConnection(IndexedDBPendingConnection( 308 db_->OpenConnection(IndexedDBPendingConnection(
241 request_, 309 request_,
242 callbacks_, 310 callbacks_,
(...skipping 24 matching lines...) Expand all
267 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; 335 scoped_refptr<IndexedDBFakeBackingStore> backing_store_;
268 scoped_refptr<IndexedDBDatabase> db_; 336 scoped_refptr<IndexedDBDatabase> db_;
269 scoped_refptr<MockIndexedDBCallbacks> request_; 337 scoped_refptr<MockIndexedDBCallbacks> request_;
270 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; 338 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_;
271 scoped_refptr<IndexedDBTransaction> transaction_; 339 scoped_refptr<IndexedDBTransaction> transaction_;
272 340
273 leveldb::Status commit_success_; 341 leveldb::Status commit_success_;
274 342
275 private: 343 private:
276 base::MessageLoop message_loop_; 344 base::MessageLoop message_loop_;
345 scoped_refptr<MockIDBFactory> factory_;
277 346
278 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); 347 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest);
279 }; 348 };
280 349
281 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { 350 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) {
282 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 351 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
283 const int64 store_id = 1001; 352 const int64 store_id = 1001;
284 db_->CreateObjectStore(transaction_->id(), 353 db_->CreateObjectStore(transaction_->id(),
285 store_id, 354 store_id,
286 ASCIIToUTF16("store"), 355 ASCIIToUTF16("store"),
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 474 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
406 475
407 // This will execute the Put then Delete. 476 // This will execute the Put then Delete.
408 RunPostedTasks(); 477 RunPostedTasks();
409 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 478 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
410 479
411 transaction_->Commit(); // Cleans up the object hierarchy. 480 transaction_->Commit(); // Cleans up the object hierarchy.
412 } 481 }
413 482
414 } // namespace content 483 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_database.cc ('k') | content/browser/indexed_db/indexed_db_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698