OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
10 #include "content/browser/indexed_db/indexed_db_connection.h" | 10 #include "content/browser/indexed_db/indexed_db_connection.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 109 } |
110 | 110 |
111 // Make sure we wait until the destructor has run. | 111 // Make sure we wait until the destructor has run. |
112 message_loop_.RunUntilIdle(); | 112 message_loop_.RunUntilIdle(); |
113 | 113 |
114 // No data was cleared because of SetForceKeepSessionState. | 114 // No data was cleared because of SetForceKeepSessionState. |
115 EXPECT_TRUE(base::DirectoryExists(normal_path)); | 115 EXPECT_TRUE(base::DirectoryExists(normal_path)); |
116 EXPECT_TRUE(base::DirectoryExists(session_only_path)); | 116 EXPECT_TRUE(base::DirectoryExists(session_only_path)); |
117 } | 117 } |
118 | 118 |
119 class MockConnection : public IndexedDBConnection { | 119 class ForceCloseDBCallbacks : public IndexedDBCallbacks { |
120 public: | 120 public: |
121 explicit MockConnection(bool expect_force_close) | 121 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context, |
122 : IndexedDBConnection(NULL, NULL), | 122 const GURL& origin_url) |
123 expect_force_close_(expect_force_close), | 123 : IndexedDBCallbacks(NULL, 0, 0), |
124 force_close_called_(false) {} | 124 idb_context_(idb_context), |
| 125 origin_url_(origin_url) {} |
125 | 126 |
126 virtual ~MockConnection() { | 127 virtual void OnSuccess() OVERRIDE {} |
127 EXPECT_TRUE(force_close_called_ == expect_force_close_); | 128 virtual void OnSuccess(const std::vector<base::string16>&) OVERRIDE {} |
| 129 virtual void OnSuccess(scoped_ptr<IndexedDBConnection> connection, |
| 130 const IndexedDBDatabaseMetadata& metadata) OVERRIDE { |
| 131 connection_ = connection.Pass(); |
| 132 idb_context_->ConnectionOpened(origin_url_, connection_.get()); |
128 } | 133 } |
129 | 134 |
130 virtual void ForceClose() OVERRIDE { | 135 IndexedDBConnection* connection() { return connection_.get(); } |
131 ASSERT_TRUE(expect_force_close_); | |
132 force_close_called_ = true; | |
133 } | |
134 | 136 |
135 virtual bool IsConnected() OVERRIDE { | 137 protected: |
136 return !force_close_called_; | 138 virtual ~ForceCloseDBCallbacks() {} |
137 } | |
138 | 139 |
139 private: | 140 private: |
140 bool expect_force_close_; | 141 scoped_refptr<IndexedDBContextImpl> idb_context_; |
141 bool force_close_called_; | 142 GURL origin_url_; |
| 143 scoped_ptr<IndexedDBConnection> connection_; |
| 144 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks); |
142 }; | 145 }; |
143 | 146 |
144 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { | 147 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { |
145 base::ScopedTempDir temp_dir; | 148 base::ScopedTempDir temp_dir; |
146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 149 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
147 | 150 |
| 151 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks( |
| 152 new MockIndexedDBDatabaseCallbacks()); |
| 153 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks( |
| 154 new MockIndexedDBDatabaseCallbacks()); |
| 155 |
148 base::FilePath test_path; | 156 base::FilePath test_path; |
149 | 157 |
150 // Create the scope which will ensure we run the destructor of the context. | 158 // Create the scope which will ensure we run the destructor of the context. |
151 { | 159 { |
152 TestBrowserContext browser_context; | 160 TestBrowserContext browser_context; |
153 | 161 |
154 const GURL kTestOrigin("http://test/"); | 162 const GURL kTestOrigin("http://test/"); |
155 | 163 |
156 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( | 164 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
157 temp_dir.path(), special_storage_policy_, NULL, task_runner_); | 165 temp_dir.path(), special_storage_policy_, NULL, task_runner_); |
158 | 166 |
| 167 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = |
| 168 new ForceCloseDBCallbacks(idb_context, kTestOrigin); |
| 169 |
| 170 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks = |
| 171 new ForceCloseDBCallbacks(idb_context, kTestOrigin); |
| 172 |
| 173 IndexedDBFactory* factory = idb_context->GetIDBFactory(); |
| 174 |
159 test_path = idb_context->GetFilePathForTesting( | 175 test_path = idb_context->GetFilePathForTesting( |
160 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); | 176 webkit_database::GetIdentifierFromOrigin(kTestOrigin)); |
161 ASSERT_TRUE(base::CreateDirectory(test_path)); | |
162 | 177 |
163 const bool kExpectForceClose = true; | 178 factory->Open(base::ASCIIToUTF16("opendb"), |
| 179 0, |
| 180 0, |
| 181 open_callbacks, |
| 182 open_db_callbacks, |
| 183 kTestOrigin, |
| 184 idb_context->data_path()); |
| 185 factory->Open(base::ASCIIToUTF16("closeddb"), |
| 186 0, |
| 187 0, |
| 188 closed_callbacks, |
| 189 closed_db_callbacks, |
| 190 kTestOrigin, |
| 191 idb_context->data_path()); |
164 | 192 |
165 MockConnection connection1(kExpectForceClose); | 193 closed_callbacks->connection()->Close(); |
166 idb_context->TaskRunner()->PostTask( | |
167 FROM_HERE, | |
168 base::Bind(&IndexedDBContextImpl::ConnectionOpened, | |
169 idb_context, | |
170 kTestOrigin, | |
171 &connection1)); | |
172 | |
173 MockConnection connection2(!kExpectForceClose); | |
174 idb_context->TaskRunner()->PostTask( | |
175 FROM_HERE, | |
176 base::Bind(&IndexedDBContextImpl::ConnectionOpened, | |
177 idb_context, | |
178 kTestOrigin, | |
179 &connection2)); | |
180 idb_context->TaskRunner()->PostTask( | |
181 FROM_HERE, | |
182 base::Bind(&IndexedDBContextImpl::ConnectionClosed, | |
183 idb_context, | |
184 kTestOrigin, | |
185 &connection2)); | |
186 | 194 |
187 idb_context->TaskRunner()->PostTask( | 195 idb_context->TaskRunner()->PostTask( |
188 FROM_HERE, | 196 FROM_HERE, |
189 base::Bind( | 197 base::Bind( |
190 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); | 198 &IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin)); |
191 FlushIndexedDBTaskRunner(); | 199 FlushIndexedDBTaskRunner(); |
192 message_loop_.RunUntilIdle(); | 200 message_loop_.RunUntilIdle(); |
193 } | 201 } |
194 | 202 |
195 // Make sure we wait until the destructor has run. | 203 // Make sure we wait until the destructor has run. |
196 message_loop_.RunUntilIdle(); | 204 message_loop_.RunUntilIdle(); |
197 | 205 |
| 206 EXPECT_TRUE(open_db_callbacks->forced_close_called()); |
| 207 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); |
198 EXPECT_FALSE(base::DirectoryExists(test_path)); | 208 EXPECT_FALSE(base::DirectoryExists(test_path)); |
199 } | 209 } |
200 | 210 |
201 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { | 211 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { |
202 base::ScopedTempDir temp_dir; | 212 base::ScopedTempDir temp_dir; |
203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 213 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
204 const GURL kTestOrigin("http://test/"); | 214 const GURL kTestOrigin("http://test/"); |
205 | 215 |
206 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( | 216 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( |
207 temp_dir.path(), special_storage_policy_, NULL, task_runner_); | 217 temp_dir.path(), special_storage_policy_, NULL, task_runner_); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); | 264 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); |
255 | 265 |
256 // Simulate the write failure. | 266 // Simulate the write failure. |
257 callbacks->connection()->database()->TransactionCommitFailed(); | 267 callbacks->connection()->database()->TransactionCommitFailed(); |
258 | 268 |
259 EXPECT_TRUE(db_callbacks->forced_close_called()); | 269 EXPECT_TRUE(db_callbacks->forced_close_called()); |
260 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); | 270 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); |
261 } | 271 } |
262 | 272 |
263 } // namespace content | 273 } // namespace content |
OLD | NEW |