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" |
(...skipping 30 matching lines...) Expand all Loading... |
41 }; | 41 }; |
42 | 42 |
43 TEST_F(IndexedDBTransactionTest, Timeout) { | 43 TEST_F(IndexedDBTransactionTest, Timeout) { |
44 const int64 id = 0; | 44 const int64 id = 0; |
45 const std::set<int64> scope; | 45 const std::set<int64> scope; |
46 const bool commit_success = true; | 46 const bool commit_success = true; |
47 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( | 47 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
48 id, | 48 id, |
49 new MockIndexedDBDatabaseCallbacks(), | 49 new MockIndexedDBDatabaseCallbacks(), |
50 scope, | 50 scope, |
51 indexed_db::TRANSACTION_READ_ONLY, | 51 indexed_db::TRANSACTION_READ_WRITE, |
52 db_, | 52 db_, |
53 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); | 53 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
54 db_->TransactionCreated(transaction); | 54 db_->TransactionCreated(transaction); |
55 | 55 |
56 // No conflicting transactions, so coordinator will start it immediately: | 56 // No conflicting transactions, so coordinator will start it immediately: |
57 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); | 57 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
58 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 58 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
59 | 59 |
60 // Schedule a task - timer won't be started until it's processed. | 60 // Schedule a task - timer won't be started until it's processed. |
61 transaction->ScheduleTask(base::Bind( | 61 transaction->ScheduleTask(base::Bind( |
62 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); | 62 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); |
63 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 63 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
64 | 64 |
65 RunPostedTasks(); | 65 RunPostedTasks(); |
66 EXPECT_TRUE(transaction->IsTimeoutTimerRunning()); | 66 EXPECT_TRUE(transaction->IsTimeoutTimerRunning()); |
67 | 67 |
68 // Abort should cancel the timer. | 68 // Abort should cancel the timer. |
69 transaction->Abort(); | 69 transaction->Abort(); |
70 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 70 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
71 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 71 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
72 | 72 |
73 // This task will be ignored. | 73 // This task will be ignored. |
74 transaction->ScheduleTask(base::Bind( | 74 transaction->ScheduleTask(base::Bind( |
75 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); | 75 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); |
76 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); | 76 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
77 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); | 77 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
78 } | 78 } |
79 | 79 |
| 80 TEST_F(IndexedDBTransactionTest, NoTimeoutReadOnly) { |
| 81 const int64 id = 0; |
| 82 const std::set<int64> scope; |
| 83 const bool commit_success = true; |
| 84 scoped_refptr<IndexedDBTransaction> transaction = new IndexedDBTransaction( |
| 85 id, |
| 86 new MockIndexedDBDatabaseCallbacks(), |
| 87 scope, |
| 88 indexed_db::TRANSACTION_READ_ONLY, |
| 89 db_, |
| 90 new IndexedDBFakeBackingStore::FakeTransaction(commit_success)); |
| 91 db_->TransactionCreated(transaction); |
| 92 |
| 93 // No conflicting transactions, so coordinator will start it immediately: |
| 94 EXPECT_EQ(IndexedDBTransaction::STARTED, transaction->state()); |
| 95 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 96 |
| 97 // Schedule a task - timer won't be started until it's processed. |
| 98 transaction->ScheduleTask(base::Bind( |
| 99 &IndexedDBTransactionTest::DummyOperation, base::Unretained(this))); |
| 100 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 101 |
| 102 // Transaction is read-only, so no need to time it out. |
| 103 RunPostedTasks(); |
| 104 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 105 |
| 106 // Clean up to avoid leaks. |
| 107 transaction->Abort(); |
| 108 EXPECT_EQ(IndexedDBTransaction::FINISHED, transaction->state()); |
| 109 EXPECT_FALSE(transaction->IsTimeoutTimerRunning()); |
| 110 } |
| 111 |
80 class AbortObserver { | 112 class AbortObserver { |
81 public: | 113 public: |
82 AbortObserver() : abort_task_called_(false) {} | 114 AbortObserver() : abort_task_called_(false) {} |
83 | 115 |
84 void AbortTask(IndexedDBTransaction* transaction) { | 116 void AbortTask(IndexedDBTransaction* transaction) { |
85 abort_task_called_ = true; | 117 abort_task_called_ = true; |
86 } | 118 } |
87 | 119 |
88 bool abort_task_called() const { return abort_task_called_; } | 120 bool abort_task_called() const { return abort_task_called_; } |
89 | 121 |
(...skipping 26 matching lines...) Expand all Loading... |
116 base::MessageLoop::current()->RunUntilIdle(); | 148 base::MessageLoop::current()->RunUntilIdle(); |
117 | 149 |
118 EXPECT_FALSE(observer.abort_task_called()); | 150 EXPECT_FALSE(observer.abort_task_called()); |
119 transaction->Commit(); | 151 transaction->Commit(); |
120 EXPECT_TRUE(observer.abort_task_called()); | 152 EXPECT_TRUE(observer.abort_task_called()); |
121 } | 153 } |
122 | 154 |
123 } // namespace | 155 } // namespace |
124 | 156 |
125 } // namespace content | 157 } // namespace content |
OLD | NEW |