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

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

Issue 2472213003: [IndexedDB] Refactoring to remove ref ptrs and host transaction ids. (Closed)
Patch Set: comments Created 4 years 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 <stdint.h> 7 #include <stdint.h>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 request_ = new MockIndexedDBCallbacks(); 354 request_ = new MockIndexedDBCallbacks();
355 callbacks_ = new MockIndexedDBDatabaseCallbacks(); 355 callbacks_ = new MockIndexedDBDatabaseCallbacks();
356 const int64_t transaction_id = 1; 356 const int64_t transaction_id = 1;
357 std::unique_ptr<IndexedDBPendingConnection> connection( 357 std::unique_ptr<IndexedDBPendingConnection> connection(
358 base::MakeUnique<IndexedDBPendingConnection>( 358 base::MakeUnique<IndexedDBPendingConnection>(
359 request_, callbacks_, kFakeChildProcessId, transaction_id, 359 request_, callbacks_, kFakeChildProcessId, transaction_id,
360 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 360 IndexedDBDatabaseMetadata::DEFAULT_VERSION));
361 db_->OpenConnection(std::move(connection)); 361 db_->OpenConnection(std::move(connection));
362 EXPECT_EQ(IndexedDBDatabaseMetadata::NO_VERSION, db_->metadata().version); 362 EXPECT_EQ(IndexedDBDatabaseMetadata::NO_VERSION, db_->metadata().version);
363 363
364 connection_ = base::MakeUnique<IndexedDBConnection>(db_, callbacks_); 364 connection_ = base::MakeUnique<IndexedDBConnection>(kFakeChildProcessId,
365 transaction_ = IndexedDBClassFactory::Get()->CreateIndexedDBTransaction( 365 db_, callbacks_);
366 transaction_id, connection_->GetWeakPtr(), 366 transaction_ = connection_->CreateTransaction(
367 std::set<int64_t>() /*scope*/, 367 transaction_id, std::set<int64_t>() /*scope*/,
368 blink::WebIDBTransactionModeVersionChange, 368 blink::WebIDBTransactionModeVersionChange,
369 new IndexedDBFakeBackingStore::FakeTransaction(commit_success_)); 369 new IndexedDBFakeBackingStore::FakeTransaction(commit_success_));
370 db_->TransactionCreated(transaction_.get()); 370 db_->TransactionCreated(transaction_);
371 371
372 // Add a dummy task which takes the place of the VersionChangeOperation 372 // Add a dummy task which takes the place of the VersionChangeOperation
373 // which kicks off the upgrade. This ensures that the transaction has 373 // which kicks off the upgrade. This ensures that the transaction has
374 // processed at least one task before the CreateObjectStore call. 374 // processed at least one task before the CreateObjectStore call.
375 transaction_->ScheduleTask(base::Bind(&DummyOperation)); 375 transaction_->ScheduleTask(base::Bind(&DummyOperation));
376 } 376 }
377 377
378 void RunPostedTasks() { base::RunLoop().RunUntilIdle(); } 378 void RunPostedTasks() { base::RunLoop().RunUntilIdle(); }
379 379
380 protected: 380 protected:
381 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; 381 scoped_refptr<IndexedDBFakeBackingStore> backing_store_;
382 scoped_refptr<IndexedDBDatabase> db_; 382 scoped_refptr<IndexedDBDatabase> db_;
383 scoped_refptr<MockIndexedDBCallbacks> request_; 383 scoped_refptr<MockIndexedDBCallbacks> request_;
384 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_; 384 scoped_refptr<MockIndexedDBDatabaseCallbacks> callbacks_;
385 scoped_refptr<IndexedDBTransaction> transaction_; 385 IndexedDBTransaction* transaction_;
386 std::unique_ptr<IndexedDBConnection> connection_; 386 std::unique_ptr<IndexedDBConnection> connection_;
387 387
388 leveldb::Status commit_success_; 388 leveldb::Status commit_success_;
389 389
390 private: 390 private:
391 scoped_refptr<MockIndexedDBFactory> factory_; 391 scoped_refptr<MockIndexedDBFactory> factory_;
392 content::TestBrowserThreadBundle thread_bundle_; 392 content::TestBrowserThreadBundle thread_bundle_;
393 393
394 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest); 394 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationTest);
395 }; 395 };
396 396
397 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) { 397 TEST_F(IndexedDBDatabaseOperationTest, CreateObjectStore) {
398 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 398 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
399 const int64_t store_id = 1001; 399 const int64_t store_id = 1001;
400 db_->CreateObjectStore(transaction_->id(), 400 db_->CreateObjectStore(transaction_, store_id, ASCIIToUTF16("store"),
401 store_id, 401 IndexedDBKeyPath(), false /*auto_increment*/);
402 ASCIIToUTF16("store"),
403 IndexedDBKeyPath(),
404 false /*auto_increment*/);
405 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 402 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
406 RunPostedTasks(); 403 RunPostedTasks();
407 transaction_->Commit(); 404 transaction_->Commit();
408 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 405 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
409 } 406 }
410 407
411 TEST_F(IndexedDBDatabaseOperationTest, CreateIndex) { 408 TEST_F(IndexedDBDatabaseOperationTest, CreateIndex) {
412 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 409 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
413 const int64_t store_id = 1001; 410 const int64_t store_id = 1001;
414 db_->CreateObjectStore(transaction_->id(), 411 db_->CreateObjectStore(transaction_, store_id, ASCIIToUTF16("store"),
415 store_id, 412 IndexedDBKeyPath(), false /*auto_increment*/);
416 ASCIIToUTF16("store"),
417 IndexedDBKeyPath(),
418 false /*auto_increment*/);
419 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 413 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
420 const int64_t index_id = 2002; 414 const int64_t index_id = 2002;
421 db_->CreateIndex(transaction_->id(), 415 db_->CreateIndex(transaction_, store_id, index_id, ASCIIToUTF16("index"),
422 store_id, 416 IndexedDBKeyPath(), false /*unique*/, false /*multi_entry*/);
423 index_id,
424 ASCIIToUTF16("index"),
425 IndexedDBKeyPath(),
426 false /*unique*/,
427 false /*multi_entry*/);
428 EXPECT_EQ( 417 EXPECT_EQ(
429 1ULL, 418 1ULL,
430 db_->metadata().object_stores.find(store_id)->second.indexes.size()); 419 db_->metadata().object_stores.find(store_id)->second.indexes.size());
431 RunPostedTasks(); 420 RunPostedTasks();
432 transaction_->Commit(); 421 transaction_->Commit();
433 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 422 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
434 EXPECT_EQ( 423 EXPECT_EQ(
435 1ULL, 424 1ULL,
436 db_->metadata().object_stores.find(store_id)->second.indexes.size()); 425 db_->metadata().object_stores.find(store_id)->second.indexes.size());
437 } 426 }
438 427
439 class IndexedDBDatabaseOperationAbortTest 428 class IndexedDBDatabaseOperationAbortTest
440 : public IndexedDBDatabaseOperationTest { 429 : public IndexedDBDatabaseOperationTest {
441 public: 430 public:
442 IndexedDBDatabaseOperationAbortTest() { 431 IndexedDBDatabaseOperationAbortTest() {
443 commit_success_ = leveldb::Status::NotFound("Bummer."); 432 commit_success_ = leveldb::Status::NotFound("Bummer.");
444 } 433 }
445 434
446 private: 435 private:
447 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationAbortTest); 436 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabaseOperationAbortTest);
448 }; 437 };
449 438
450 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateObjectStore) { 439 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateObjectStore) {
451 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 440 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
452 const int64_t store_id = 1001; 441 const int64_t store_id = 1001;
453 db_->CreateObjectStore(transaction_->id(), 442 db_->CreateObjectStore(transaction_, store_id, ASCIIToUTF16("store"),
454 store_id, 443 IndexedDBKeyPath(), false /*auto_increment*/);
455 ASCIIToUTF16("store"),
456 IndexedDBKeyPath(),
457 false /*auto_increment*/);
458 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 444 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
459 RunPostedTasks(); 445 RunPostedTasks();
460 transaction_->Commit(); 446 transaction_->Commit();
461 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 447 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
462 } 448 }
463 449
464 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateIndex) { 450 TEST_F(IndexedDBDatabaseOperationAbortTest, CreateIndex) {
465 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 451 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
466 const int64_t store_id = 1001; 452 const int64_t store_id = 1001;
467 db_->CreateObjectStore(transaction_->id(), 453 db_->CreateObjectStore(transaction_, store_id, ASCIIToUTF16("store"),
468 store_id, 454 IndexedDBKeyPath(), false /*auto_increment*/);
469 ASCIIToUTF16("store"),
470 IndexedDBKeyPath(),
471 false /*auto_increment*/);
472 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 455 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
473 const int64_t index_id = 2002; 456 const int64_t index_id = 2002;
474 db_->CreateIndex(transaction_->id(), 457 db_->CreateIndex(transaction_, store_id, index_id, ASCIIToUTF16("index"),
475 store_id, 458 IndexedDBKeyPath(), false /*unique*/, false /*multi_entry*/);
476 index_id,
477 ASCIIToUTF16("index"),
478 IndexedDBKeyPath(),
479 false /*unique*/,
480 false /*multi_entry*/);
481 EXPECT_EQ( 459 EXPECT_EQ(
482 1ULL, 460 1ULL,
483 db_->metadata().object_stores.find(store_id)->second.indexes.size()); 461 db_->metadata().object_stores.find(store_id)->second.indexes.size());
484 RunPostedTasks(); 462 RunPostedTasks();
485 transaction_->Commit(); 463 transaction_->Commit();
486 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 464 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
487 } 465 }
488 466
489 TEST_F(IndexedDBDatabaseOperationTest, CreatePutDelete) { 467 TEST_F(IndexedDBDatabaseOperationTest, CreatePutDelete) {
490 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 468 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
491 const int64_t store_id = 1001; 469 const int64_t store_id = 1001;
492 470
493 // Creation is synchronous. 471 // Creation is synchronous.
494 db_->CreateObjectStore(transaction_->id(), 472 db_->CreateObjectStore(transaction_, store_id, ASCIIToUTF16("store"),
495 store_id, 473 IndexedDBKeyPath(), false /*auto_increment*/);
496 ASCIIToUTF16("store"),
497 IndexedDBKeyPath(),
498 false /*auto_increment*/);
499 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 474 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
500 475
501 476
502 // Put is asynchronous 477 // Put is asynchronous
503 IndexedDBValue value("value1", std::vector<IndexedDBBlobInfo>()); 478 IndexedDBValue value("value1", std::vector<IndexedDBBlobInfo>());
504 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles; 479 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles;
505 std::unique_ptr<IndexedDBKey> key(base::MakeUnique<IndexedDBKey>("key")); 480 std::unique_ptr<IndexedDBKey> key(base::MakeUnique<IndexedDBKey>("key"));
506 std::vector<IndexedDBIndexKeys> index_keys; 481 std::vector<IndexedDBIndexKeys> index_keys;
507 scoped_refptr<MockIndexedDBCallbacks> request( 482 scoped_refptr<MockIndexedDBCallbacks> request(
508 new MockIndexedDBCallbacks(false)); 483 new MockIndexedDBCallbacks(false));
509 db_->Put(transaction_->id(), store_id, &value, &handles, std::move(key), 484 db_->Put(transaction_, store_id, &value, &handles, std::move(key),
510 blink::WebIDBPutModeAddOnly, request, index_keys); 485 blink::WebIDBPutModeAddOnly, request, index_keys);
511 486
512 // Deletion is asynchronous. 487 // Deletion is asynchronous.
513 db_->DeleteObjectStore(transaction_->id(), 488 db_->DeleteObjectStore(transaction_, store_id);
514 store_id);
515 EXPECT_EQ(1ULL, db_->metadata().object_stores.size()); 489 EXPECT_EQ(1ULL, db_->metadata().object_stores.size());
516 490
517 // This will execute the Put then Delete. 491 // This will execute the Put then Delete.
518 RunPostedTasks(); 492 RunPostedTasks();
519 EXPECT_EQ(0ULL, db_->metadata().object_stores.size()); 493 EXPECT_EQ(0ULL, db_->metadata().object_stores.size());
520 494
521 transaction_->Commit(); // Cleans up the object hierarchy. 495 transaction_->Commit(); // Cleans up the object hierarchy.
522 } 496 }
523 497
524 } // namespace content 498 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698