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

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

Issue 1963293002: Replacing Indexed DB Chromium IPC with Mojo Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactoring after Passing URLRequestContextGetter. Created 4 years, 4 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 (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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/test/test_simple_task_runner.h" 11 #include "base/test/test_simple_task_runner.h"
12 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
13 #include "content/browser/browser_thread_impl.h" 13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/indexed_db/indexed_db_connection.h" 14 #include "content/browser/indexed_db/indexed_db_connection.h"
15 #include "content/browser/indexed_db/indexed_db_context_impl.h" 15 #include "content/browser/indexed_db/indexed_db_context_impl.h"
16 #include "content/browser/indexed_db/indexed_db_factory_impl.h" 16 #include "content/browser/indexed_db/indexed_db_factory_impl.h"
17 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" 17 #include "content/browser/indexed_db/mock_indexed_db_change_handler.h"
18 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
19 #include "content/browser/quota/mock_quota_manager_proxy.h" 18 #include "content/browser/quota/mock_quota_manager_proxy.h"
20 #include "content/public/browser/storage_partition.h" 19 #include "content/public/browser/storage_partition.h"
21 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
22 #include "content/public/test/mock_special_storage_policy.h" 21 #include "content/public/test/mock_special_storage_policy.h"
23 #include "content/public/test/test_browser_context.h" 22 #include "content/public/test/test_browser_context.h"
24 #include "storage/browser/quota/quota_manager.h" 23 #include "storage/browser/quota/quota_manager.h"
25 #include "storage/browser/quota/special_storage_policy.h" 24 #include "storage/browser/quota/special_storage_policy.h"
26 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
27 #include "url/origin.h" 26 #include "url/origin.h"
28 27
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 124 }
126 125
127 // Make sure we wait until the destructor has run. 126 // Make sure we wait until the destructor has run.
128 message_loop_.RunUntilIdle(); 127 message_loop_.RunUntilIdle();
129 128
130 // No data was cleared because of SetForceKeepSessionState. 129 // No data was cleared because of SetForceKeepSessionState.
131 EXPECT_TRUE(base::DirectoryExists(normal_path)); 130 EXPECT_TRUE(base::DirectoryExists(normal_path));
132 EXPECT_TRUE(base::DirectoryExists(session_only_path)); 131 EXPECT_TRUE(base::DirectoryExists(session_only_path));
133 } 132 }
134 133
134 #ifdef CJM_NEED_CALLBACK
135 class ForceCloseDBCallbacks : public IndexedDBCallbacks { 135 class ForceCloseDBCallbacks : public IndexedDBCallbacks {
136 public: 136 public:
137 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context, 137 ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context,
138 const Origin& origin) 138 const Origin& origin)
139 : IndexedDBCallbacks(nullptr, 0, 0), 139 : IndexedDBCallbacks(nullptr, 0, 0),
140 idb_context_(idb_context), 140 idb_context_(idb_context),
141 origin_(origin) {} 141 origin_(origin) {}
142 142
143 void OnSuccess() override {} 143 void OnSuccess() override {}
144 void OnSuccess(const std::vector<base::string16>&) override {} 144 void OnSuccess(const std::vector<base::string16>&) override {}
145 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, 145 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection,
146 const IndexedDBDatabaseMetadata& metadata) override { 146 const IndexedDBDatabaseMetadata& metadata) override {
147 connection_ = std::move(connection); 147 connection_ = std::move(connection);
148 idb_context_->ConnectionOpened(origin_, connection_.get()); 148 idb_context_->ConnectionOpened(origin_, connection_.get());
149 } 149 }
150 150
151 IndexedDBConnection* connection() { return connection_.get(); } 151 IndexedDBConnection* connection() { return connection_.get(); }
152 152
153 protected: 153 protected:
154 ~ForceCloseDBCallbacks() override {} 154 ~ForceCloseDBCallbacks() override {}
155 155
156 private: 156 private:
157 scoped_refptr<IndexedDBContextImpl> idb_context_; 157 scoped_refptr<IndexedDBContextImpl> idb_context_;
158 Origin origin_; 158 Origin origin_;
159 std::unique_ptr<IndexedDBConnection> connection_; 159 std::unique_ptr<IndexedDBConnection> connection_;
160 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks); 160 DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks);
161 }; 161 };
162 #endif
162 163
163 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) { 164 TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
164 base::ScopedTempDir temp_dir; 165 base::ScopedTempDir temp_dir;
165 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 166 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
166 167
167 scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks( 168 scoped_refptr<MockIndexedDBChangeHandler> open_db_change_handler(
168 new MockIndexedDBDatabaseCallbacks()); 169 new MockIndexedDBChangeHandler());
169 scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks( 170 scoped_refptr<MockIndexedDBChangeHandler> closed_db_change_handler(
170 new MockIndexedDBDatabaseCallbacks()); 171 new MockIndexedDBChangeHandler());
171 172
172 base::FilePath test_path; 173 base::FilePath test_path;
173 174
175 #ifdef CJM_NEED_CALLBACK
174 // Create the scope which will ensure we run the destructor of the context. 176 // Create the scope which will ensure we run the destructor of the context.
175 { 177 {
176 TestBrowserContext browser_context; 178 TestBrowserContext browser_context;
177 179
178 const Origin kTestOrigin(GURL("http://test/")); 180 const Origin kTestOrigin(GURL("http://test/"));
179 181
180 scoped_refptr<IndexedDBContextImpl> idb_context = 182 scoped_refptr<IndexedDBContextImpl> idb_context =
181 new IndexedDBContextImpl(temp_dir.path(), 183 new IndexedDBContextImpl(temp_dir.path(),
182 special_storage_policy_.get(), 184 special_storage_policy_.get(),
183 quota_manager_proxy_.get(), 185 quota_manager_proxy_.get(),
184 task_runner_.get()); 186 task_runner_.get());
185 187
186 scoped_refptr<ForceCloseDBCallbacks> open_callbacks = 188 scoped_refptr<ForceCloseDBCallbacks> open_callbacks =
187 new ForceCloseDBCallbacks(idb_context, kTestOrigin); 189 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
188 190
189 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks = 191 scoped_refptr<ForceCloseDBCallbacks> closed_callbacks =
190 new ForceCloseDBCallbacks(idb_context, kTestOrigin); 192 new ForceCloseDBCallbacks(idb_context, kTestOrigin);
191 193
192 IndexedDBFactory* factory = idb_context->GetIDBFactory(); 194 IndexedDBFactory* factory = idb_context->GetIDBFactory();
193 195
194 test_path = idb_context->GetFilePathForTesting(kTestOrigin); 196 test_path = idb_context->GetFilePathForTesting(kTestOrigin);
195 197
196 std::unique_ptr<IndexedDBPendingConnection> open_connection( 198 std::unique_ptr<IndexedDBPendingConnection> open_connection(
197 base::MakeUnique<IndexedDBPendingConnection>( 199 base::MakeUnique<IndexedDBPendingConnection>(
198 open_callbacks, open_db_callbacks, 0 /* child_process_id */, 200 open_callbacks, open_db_change_handler, 0 /* child_process_id */,
199 0 /* host_transaction_id */, 0 /* version */)); 201 0 /* host_transaction_id */, 0 /* version */));
200 factory->Open(base::ASCIIToUTF16("opendb"), std::move(open_connection), 202 factory->Open(base::ASCIIToUTF16("opendb"), std::move(open_connection),
201 nullptr /* request_context */, Origin(kTestOrigin), 203 nullptr /* request_context */, Origin(kTestOrigin),
202 idb_context->data_path()); 204 idb_context->data_path());
203 std::unique_ptr<IndexedDBPendingConnection> closed_connection( 205 std::unique_ptr<IndexedDBPendingConnection> closed_connection(
204 base::MakeUnique<IndexedDBPendingConnection>( 206 base::MakeUnique<IndexedDBPendingConnection>(
205 closed_callbacks, closed_db_callbacks, 0 /* child_process_id */, 207 closed_callbacks, closed_db_change_handler,
206 0 /* host_transaction_id */, 0 /* version */)); 208 0 /* child_process_id */, 0 /* host_transaction_id */,
209 0 /* version */));
207 factory->Open(base::ASCIIToUTF16("closeddb"), std::move(closed_connection), 210 factory->Open(base::ASCIIToUTF16("closeddb"), std::move(closed_connection),
208 nullptr /* request_context */, Origin(kTestOrigin), 211 nullptr /* request_context */, Origin(kTestOrigin),
209 idb_context->data_path()); 212 idb_context->data_path());
210 213
211 closed_callbacks->connection()->Close(); 214 closed_callbacks->connection()->Close();
212 215
213 // TODO(jsbell): Remove static_cast<> when overloads are eliminated. 216 // TODO(jsbell): Remove static_cast<> when overloads are eliminated.
214 idb_context->TaskRunner()->PostTask( 217 idb_context->TaskRunner()->PostTask(
215 FROM_HERE, 218 FROM_HERE,
216 base::Bind(static_cast<void (IndexedDBContextImpl::*)(const Origin&)>( 219 base::Bind(static_cast<void (IndexedDBContextImpl::*)(const Origin&)>(
217 &IndexedDBContextImpl::DeleteForOrigin), 220 &IndexedDBContextImpl::DeleteForOrigin),
218 idb_context, kTestOrigin)); 221 idb_context, kTestOrigin));
219 FlushIndexedDBTaskRunner(); 222 FlushIndexedDBTaskRunner();
220 message_loop_.RunUntilIdle(); 223 message_loop_.RunUntilIdle();
221 } 224 }
222 225
223 // Make sure we wait until the destructor has run. 226 // Make sure we wait until the destructor has run.
224 message_loop_.RunUntilIdle(); 227 message_loop_.RunUntilIdle();
225 228
226 EXPECT_TRUE(open_db_callbacks->forced_close_called()); 229 EXPECT_TRUE(open_db_callbacks->forced_close_called());
227 EXPECT_FALSE(closed_db_callbacks->forced_close_called()); 230 EXPECT_FALSE(closed_db_callbacks->forced_close_called());
228 EXPECT_FALSE(base::DirectoryExists(test_path)); 231 EXPECT_FALSE(base::DirectoryExists(test_path));
232 #endif
229 } 233 }
230 234
231 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) { 235 TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) {
232 base::ScopedTempDir temp_dir; 236 base::ScopedTempDir temp_dir;
233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
234 const Origin kTestOrigin(GURL("http://test/")); 238 const Origin kTestOrigin(GURL("http://test/"));
235 239
236 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl( 240 scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
237 temp_dir.path(), special_storage_policy_.get(), 241 temp_dir.path(), special_storage_policy_.get(),
238 quota_manager_proxy_.get(), task_runner_.get()); 242 quota_manager_proxy_.get(), task_runner_.get());
(...skipping 22 matching lines...) Expand all
261 base::ScopedTempDir temp_dir; 265 base::ScopedTempDir temp_dir;
262 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 266 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
263 267
264 scoped_refptr<IndexedDBContextImpl> context = 268 scoped_refptr<IndexedDBContextImpl> context =
265 new IndexedDBContextImpl(temp_dir.path(), special_storage_policy_.get(), 269 new IndexedDBContextImpl(temp_dir.path(), special_storage_policy_.get(),
266 quota_manager_proxy_.get(), task_runner_.get()); 270 quota_manager_proxy_.get(), task_runner_.get());
267 271
268 scoped_refptr<IndexedDBFactoryImpl> factory = 272 scoped_refptr<IndexedDBFactoryImpl> factory =
269 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory()); 273 static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory());
270 274
275 #ifdef CJM_NEED_CALLBACK
271 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); 276 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
272 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( 277 scoped_refptr<MockIndexedDBChangeHandler> db_change_handler(
273 new MockIndexedDBDatabaseCallbacks()); 278 new MockIndexedDBChangeHandler());
274 const int64_t transaction_id = 1; 279 const int64_t transaction_id = 1;
275 std::unique_ptr<IndexedDBPendingConnection> connection( 280 std::unique_ptr<IndexedDBPendingConnection> connection(
276 base::MakeUnique<IndexedDBPendingConnection>( 281 base::MakeUnique<IndexedDBPendingConnection>(
277 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, 282 callbacks, db_change_handler, 0 /* child_process_id */,
278 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); 283 transaction_id, IndexedDBDatabaseMetadata::DEFAULT_VERSION));
279 factory->Open(base::ASCIIToUTF16("db"), std::move(connection), 284 factory->Open(base::ASCIIToUTF16("db"), std::move(connection),
280 nullptr /* request_context */, Origin(kTestOrigin), 285 nullptr /* request_context */, Origin(kTestOrigin),
281 temp_dir.path()); 286 temp_dir.path());
282 287
283 EXPECT_TRUE(callbacks->connection()); 288 EXPECT_TRUE(callbacks->connection());
284 289
285 // ConnectionOpened() is usually called by the dispatcher. 290 // ConnectionOpened() is usually called by the dispatcher.
286 context->ConnectionOpened(kTestOrigin, callbacks->connection()); 291 context->ConnectionOpened(kTestOrigin, callbacks->connection());
287 292
288 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin)); 293 EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin));
289 294
290 // Simulate the write failure. 295 // Simulate the write failure.
291 leveldb::Status status = leveldb::Status::IOError("Simulated failure"); 296 leveldb::Status status = leveldb::Status::IOError("Simulated failure");
292 callbacks->connection()->database()->TransactionCommitFailed(status); 297 callbacks->connection()->database()->TransactionCommitFailed(status);
293 298
294 EXPECT_TRUE(db_callbacks->forced_close_called()); 299 EXPECT_TRUE(db_change_handler->forced_close_called());
295 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin)); 300 EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin));
301 #endif
296 } 302 }
297 303
298 } // namespace content 304 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_transaction_unittest.cc ('k') | content/browser/indexed_db/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698