| 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_transaction.h" | 5 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" | 11 #include "content/browser/indexed_db/indexed_db_fake_backing_store.h" |
| 12 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 12 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 class AbortObserver { |
| 18 public: |
| 19 AbortObserver() : abort_task_called_(false) {} |
| 20 |
| 21 void AbortTask(IndexedDBTransaction* transaction) { |
| 22 abort_task_called_ = true; |
| 23 } |
| 24 |
| 25 bool abort_task_called() const { return abort_task_called_; } |
| 26 |
| 27 private: |
| 28 bool abort_task_called_; |
| 29 DISALLOW_COPY_AND_ASSIGN(AbortObserver); |
| 30 }; |
| 31 |
| 17 class IndexedDBTransactionTest : public testing::Test { | 32 class IndexedDBTransactionTest : public testing::Test { |
| 18 public: | 33 public: |
| 19 IndexedDBTransactionTest() { | 34 IndexedDBTransactionTest() { |
| 20 backing_store_ = new IndexedDBFakeBackingStore(); | 35 backing_store_ = new IndexedDBFakeBackingStore(); |
| 21 CreateDB(); | 36 CreateDB(); |
| 22 } | 37 } |
| 23 | 38 |
| 24 void CreateDB() { | 39 void CreateDB() { |
| 25 // DB is created here instead of the constructor to workaround a | 40 // DB is created here instead of the constructor to workaround a |
| 26 // "peculiarity of C++". More info at | 41 // "peculiarity of C++". More info at |
| 27 // https://code.google.com/p/googletest/wiki/FAQ#My_compiler_complains_that_
a_constructor_(or_destructor)_cannot | 42 // https://code.google.com/p/googletest/wiki/FAQ#My_compiler_complains_that_
a_constructor_(or_destructor)_cannot |
| 28 IndexedDBFactory* factory = NULL; | 43 IndexedDBFactory* factory = NULL; |
| 29 leveldb::Status s; | 44 leveldb::Status s; |
| 30 db_ = IndexedDBDatabase::Create(base::ASCIIToUTF16("db"), | 45 db_ = IndexedDBDatabase::Create(base::ASCIIToUTF16("db"), |
| 31 backing_store_, | 46 backing_store_, |
| 32 factory, | 47 factory, |
| 33 IndexedDBDatabase::Identifier(), | 48 IndexedDBDatabase::Identifier(), |
| 34 &s); | 49 &s); |
| 35 ASSERT_TRUE(s.ok()); | 50 ASSERT_TRUE(s.ok()); |
| 36 } | 51 } |
| 37 | 52 |
| 38 void RunPostedTasks() { message_loop_.RunUntilIdle(); } | 53 void RunPostedTasks() { message_loop_.RunUntilIdle(); } |
| 39 void DummyOperation(IndexedDBTransaction* transaction) {} | 54 void DummyOperation(IndexedDBTransaction* transaction) {} |
| 55 void AbortableOperation(AbortObserver* observer, |
| 56 IndexedDBTransaction* transaction) { |
| 57 transaction->ScheduleAbortTask( |
| 58 base::Bind(&AbortObserver::AbortTask, base::Unretained(observer))); |
| 59 } |
| 40 | 60 |
| 41 protected: | 61 protected: |
| 42 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; | 62 scoped_refptr<IndexedDBFakeBackingStore> backing_store_; |
| 43 scoped_refptr<IndexedDBDatabase> db_; | 63 scoped_refptr<IndexedDBDatabase> db_; |
| 44 | 64 |
| 45 private: | 65 private: |
| 46 base::MessageLoop message_loop_; | 66 base::MessageLoop message_loop_; |
| 47 | 67 |
| 48 DISALLOW_COPY_AND_ASSIGN(IndexedDBTransactionTest); | 68 DISALLOW_COPY_AND_ASSIGN(IndexedDBTransactionTest); |
| 49 }; | 69 }; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 // Transaction is read-only, so no need to time it out. | 145 // Transaction is read-only, so no need to time it out. |
| 126 RunPostedTasks(); | 146 RunPostedTasks(); |
| 127 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 147 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 128 | 148 |
| 129 // Clean up to avoid leaks. | 149 // Clean up to avoid leaks. |
| 130 transaction->Abort(); | 150 transaction->Abort(); |
| 131 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 151 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 132 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 152 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 133 } | 153 } |
| 134 | 154 |
| 135 class AbortObserver { | |
| 136 public: | |
| 137 AbortObserver() : abort_task_called_(false) {} | |
| 138 | |
| 139 void AbortTask(IndexedDBTransaction* transaction) { | |
| 140 abort_task_called_ = true; | |
| 141 } | |
| 142 | |
| 143 bool abort_task_called() const { return abort_task_called_; } | |
| 144 | |
| 145 private: | |
| 146 bool abort_task_called_; | |
| 147 DISALLOW_COPY_AND_ASSIGN(AbortObserver); | |
| 148 }; | |
| 149 | |
| 150 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { | 155 TEST_P(IndexedDBTransactionTestMode, ScheduleNormalTask) { |
| 151 const int64 id = 0; | 156 const int64 id = 0; |
| 152 const std::set<int64> scope; | 157 const std::set<int64> scope; |
| 153 const bool commit_failure = false; | 158 const bool commit_failure = false; |
| 154 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 159 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 155 id, | 160 id, |
| 156 new MockIndexedDBDatabaseCallbacks(), | 161 new MockIndexedDBDatabaseCallbacks(), |
| 157 scope, | 162 scope, |
| 158 GetParam(), | 163 GetParam(), |
| 159 db_, | 164 db_, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 id, | 283 id, |
| 279 new MockIndexedDBDatabaseCallbacks(), | 284 new MockIndexedDBDatabaseCallbacks(), |
| 280 scope, | 285 scope, |
| 281 GetParam(), | 286 GetParam(), |
| 282 db_, | 287 db_, |
| 283 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); | 288 new IndexedDBFakeBackingStore::FakeTransaction(commit_failure)); |
| 284 db_->TransactionCreated(transaction); | 289 db_->TransactionCreated(transaction); |
| 285 | 290 |
| 286 AbortObserver observer; | 291 AbortObserver observer; |
| 287 transaction->ScheduleTask( | 292 transaction->ScheduleTask( |
| 288 base::Bind(&IndexedDBTransactionTest::DummyOperation, | 293 base::Bind(&IndexedDBTransactionTest::AbortableOperation, |
| 289 base::Unretained(this)), | 294 base::Unretained(this), |
| 290 base::Bind(&AbortObserver::AbortTask, base::Unretained(&observer))); | 295 base::Unretained(&observer))); |
| 291 | 296 |
| 292 // Pump the message loop so that the transaction completes all pending tasks, | 297 // Pump the message loop so that the transaction completes all pending tasks, |
| 293 // otherwise it will defer the commit. | 298 // otherwise it will defer the commit. |
| 294 base::MessageLoop::current()->RunUntilIdle(); | 299 base::MessageLoop::current()->RunUntilIdle(); |
| 295 | 300 |
| 296 EXPECT_FALSE(observer.abort_task_called()); | 301 EXPECT_FALSE(observer.abort_task_called()); |
| 297 transaction->Commit(); | 302 transaction->Commit(); |
| 298 EXPECT_TRUE(observer.abort_task_called()); | 303 EXPECT_TRUE(observer.abort_task_called()); |
| 299 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 304 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 300 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 305 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 indexed_db::TRANSACTION_READ_ONLY, | 360 indexed_db::TRANSACTION_READ_ONLY, |
| 356 indexed_db::TRANSACTION_READ_WRITE, | 361 indexed_db::TRANSACTION_READ_WRITE, |
| 357 indexed_db::TRANSACTION_VERSION_CHANGE | 362 indexed_db::TRANSACTION_VERSION_CHANGE |
| 358 }; | 363 }; |
| 359 | 364 |
| 360 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, | 365 INSTANTIATE_TEST_CASE_P(IndexedDBTransactions, |
| 361 IndexedDBTransactionTestMode, | 366 IndexedDBTransactionTestMode, |
| 362 ::testing::ValuesIn(kTestModes)); | 367 ::testing::ValuesIn(kTestModes)); |
| 363 | 368 |
| 364 } // namespace content | 369 } // namespace content |
| OLD | NEW |