OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stack> | 5 #include <stack> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/location.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/thread_task_runner_handle.h" |
15 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
16 #include "content/browser/appcache/appcache.h" | 18 #include "content/browser/appcache/appcache.h" |
17 #include "content/browser/appcache/appcache_backend_impl.h" | 19 #include "content/browser/appcache/appcache_backend_impl.h" |
18 #include "content/browser/appcache/appcache_database.h" | 20 #include "content/browser/appcache/appcache_database.h" |
19 #include "content/browser/appcache/appcache_entry.h" | 21 #include "content/browser/appcache/appcache_entry.h" |
20 #include "content/browser/appcache/appcache_group.h" | 22 #include "content/browser/appcache/appcache_group.h" |
21 #include "content/browser/appcache/appcache_host.h" | 23 #include "content/browser/appcache/appcache_host.h" |
22 #include "content/browser/appcache/appcache_interceptor.h" | 24 #include "content/browser/appcache/appcache_interceptor.h" |
23 #include "content/browser/appcache/appcache_request_handler.h" | 25 #include "content/browser/appcache/appcache_request_handler.h" |
24 #include "content/browser/appcache/appcache_service_impl.h" | 26 #include "content/browser/appcache/appcache_service_impl.h" |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 int64 found_group_id_; | 264 int64 found_group_id_; |
263 GURL found_manifest_url_; | 265 GURL found_manifest_url_; |
264 AppCacheStorageImplTest* test_; | 266 AppCacheStorageImplTest* test_; |
265 }; | 267 }; |
266 | 268 |
267 class MockQuotaManager : public storage::QuotaManager { | 269 class MockQuotaManager : public storage::QuotaManager { |
268 public: | 270 public: |
269 MockQuotaManager() | 271 MockQuotaManager() |
270 : QuotaManager(true /* is_incognito */, | 272 : QuotaManager(true /* is_incognito */, |
271 base::FilePath(), | 273 base::FilePath(), |
272 io_thread->message_loop_proxy().get(), | 274 io_thread->task_runner().get(), |
273 db_thread->message_loop_proxy().get(), | 275 db_thread->task_runner().get(), |
274 NULL), | 276 NULL), |
275 async_(false) {} | 277 async_(false) {} |
276 | 278 |
277 void GetUsageAndQuota(const GURL& origin, | 279 void GetUsageAndQuota(const GURL& origin, |
278 storage::StorageType type, | 280 storage::StorageType type, |
279 const GetUsageAndQuotaCallback& callback) override { | 281 const GetUsageAndQuotaCallback& callback) override { |
280 EXPECT_EQ(storage::kStorageTypeTemporary, type); | 282 EXPECT_EQ(storage::kStorageTypeTemporary, type); |
281 if (async_) { | 283 if (async_) { |
282 base::MessageLoop::current()->PostTask( | 284 base::ThreadTaskRunnerHandle::Get()->PostTask( |
283 FROM_HERE, | 285 FROM_HERE, base::Bind(&MockQuotaManager::CallCallback, |
284 base::Bind(&MockQuotaManager::CallCallback, | 286 base::Unretained(this), callback)); |
285 base::Unretained(this), | |
286 callback)); | |
287 return; | 287 return; |
288 } | 288 } |
289 CallCallback(callback); | 289 CallCallback(callback); |
290 } | 290 } |
291 | 291 |
292 void CallCallback(const GetUsageAndQuotaCallback& callback) { | 292 void CallCallback(const GetUsageAndQuotaCallback& callback) { |
293 callback.Run(storage::kQuotaStatusOk, 0, kMockQuota); | 293 callback.Run(storage::kQuotaStatusOk, 0, kMockQuota); |
294 } | 294 } |
295 | 295 |
296 bool async_; | 296 bool async_; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 template <class Method> | 363 template <class Method> |
364 void MethodWrapper(Method method) { | 364 void MethodWrapper(Method method) { |
365 SetUpTest(); | 365 SetUpTest(); |
366 | 366 |
367 // Ensure InitTask execution prior to conducting a test. | 367 // Ensure InitTask execution prior to conducting a test. |
368 FlushDbThreadTasks(); | 368 FlushDbThreadTasks(); |
369 | 369 |
370 // We also have to wait for InitTask completion call to be performed | 370 // We also have to wait for InitTask completion call to be performed |
371 // on the IO thread prior to running the test. Its guaranteed to be | 371 // on the IO thread prior to running the test. Its guaranteed to be |
372 // queued by this time. | 372 // queued by this time. |
373 base::MessageLoop::current()->PostTask( | 373 base::ThreadTaskRunnerHandle::Get()->PostTask( |
374 FROM_HERE, | 374 FROM_HERE, base::Bind(&AppCacheStorageImplTest::RunMethod<Method>, |
375 base::Bind(&AppCacheStorageImplTest::RunMethod<Method>, | 375 base::Unretained(this), method)); |
376 base::Unretained(this), | |
377 method)); | |
378 } | 376 } |
379 | 377 |
380 static void SetUpTestCase() { | 378 static void SetUpTestCase() { |
381 // We start both threads as TYPE_IO because we also use the db_thead | 379 // We start both threads as TYPE_IO because we also use the db_thead |
382 // for the disk_cache which needs to be of TYPE_IO. | 380 // for the disk_cache which needs to be of TYPE_IO. |
383 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 381 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
384 io_thread.reset(new IOThread("AppCacheTest.IOThread")); | 382 io_thread.reset(new IOThread("AppCacheTest.IOThread")); |
385 ASSERT_TRUE(io_thread->StartWithOptions(options)); | 383 ASSERT_TRUE(io_thread->StartWithOptions(options)); |
386 db_thread.reset(new base::Thread("AppCacheTest::DBThread")); | 384 db_thread.reset(new base::Thread("AppCacheTest::DBThread")); |
387 ASSERT_TRUE(db_thread->StartWithOptions(options)); | 385 ASSERT_TRUE(db_thread->StartWithOptions(options)); |
388 } | 386 } |
389 | 387 |
390 static void TearDownTestCase() { | 388 static void TearDownTestCase() { |
391 io_thread.reset(NULL); | 389 io_thread.reset(NULL); |
392 db_thread.reset(NULL); | 390 db_thread.reset(NULL); |
393 } | 391 } |
394 | 392 |
395 // Test harness -------------------------------------------------- | 393 // Test harness -------------------------------------------------- |
396 | 394 |
397 AppCacheStorageImplTest() { | 395 AppCacheStorageImplTest() { |
398 } | 396 } |
399 | 397 |
400 template <class Method> | 398 template <class Method> |
401 void RunTestOnIOThread(Method method) { | 399 void RunTestOnIOThread(Method method) { |
402 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 400 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
403 io_thread->message_loop()->PostTask( | 401 io_thread->task_runner()->PostTask( |
404 FROM_HERE, base::Bind(&AppCacheStorageImplTest::MethodWrapper<Method>, | 402 FROM_HERE, base::Bind(&AppCacheStorageImplTest::MethodWrapper<Method>, |
405 base::Unretained(this), method)); | 403 base::Unretained(this), method)); |
406 test_finished_event_->Wait(); | 404 test_finished_event_->Wait(); |
407 } | 405 } |
408 | 406 |
409 void SetUpTest() { | 407 void SetUpTest() { |
410 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); | 408 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); |
411 service_.reset(new AppCacheServiceImpl(NULL)); | 409 service_.reset(new AppCacheServiceImpl(NULL)); |
412 service_->Initialize(base::FilePath(), db_thread->task_runner(), NULL); | 410 service_->Initialize(base::FilePath(), db_thread->task_runner(), NULL); |
413 mock_quota_manager_proxy_ = new MockQuotaManagerProxy(); | 411 mock_quota_manager_proxy_ = new MockQuotaManagerProxy(); |
(...skipping 10 matching lines...) Expand all Loading... |
424 mock_quota_manager_proxy_ = NULL; | 422 mock_quota_manager_proxy_ = NULL; |
425 delegate_.reset(); | 423 delegate_.reset(); |
426 service_.reset(); | 424 service_.reset(); |
427 FlushDbThreadTasks(); | 425 FlushDbThreadTasks(); |
428 } | 426 } |
429 | 427 |
430 void TestFinished() { | 428 void TestFinished() { |
431 // We unwind the stack prior to finishing up to let stack | 429 // We unwind the stack prior to finishing up to let stack |
432 // based objects get deleted. | 430 // based objects get deleted. |
433 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); | 431 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); |
434 base::MessageLoop::current()->PostTask( | 432 base::ThreadTaskRunnerHandle::Get()->PostTask( |
435 FROM_HERE, | 433 FROM_HERE, base::Bind(&AppCacheStorageImplTest::TestFinishedUnwound, |
436 base::Bind(&AppCacheStorageImplTest::TestFinishedUnwound, | 434 base::Unretained(this))); |
437 base::Unretained(this))); | |
438 } | 435 } |
439 | 436 |
440 void TestFinishedUnwound() { | 437 void TestFinishedUnwound() { |
441 TearDownTest(); | 438 TearDownTest(); |
442 test_finished_event_->Signal(); | 439 test_finished_event_->Signal(); |
443 } | 440 } |
444 | 441 |
445 void PushNextTask(const base::Closure& task) { | 442 void PushNextTask(const base::Closure& task) { |
446 task_stack_.push(task); | 443 task_stack_.push(task); |
447 } | 444 } |
448 | 445 |
449 void ScheduleNextTask() { | 446 void ScheduleNextTask() { |
450 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); | 447 DCHECK(base::MessageLoop::current() == io_thread->message_loop()); |
451 if (task_stack_.empty()) { | 448 if (task_stack_.empty()) { |
452 return; | 449 return; |
453 } | 450 } |
454 base::MessageLoop::current()->PostTask(FROM_HERE, task_stack_.top()); | 451 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task_stack_.top()); |
455 task_stack_.pop(); | 452 task_stack_.pop(); |
456 } | 453 } |
457 | 454 |
458 static void SignalEvent(base::WaitableEvent* event) { | 455 static void SignalEvent(base::WaitableEvent* event) { |
459 event->Signal(); | 456 event->Signal(); |
460 } | 457 } |
461 | 458 |
462 void FlushDbThreadTasks() { | 459 void FlushDbThreadTasks() { |
463 // We pump a task thru the db thread to ensure any tasks previously | 460 // We pump a task thru the db thread to ensure any tasks previously |
464 // scheduled on that thread have been performed prior to return. | 461 // scheduled on that thread have been performed prior to return. |
465 base::WaitableEvent event(false, false); | 462 base::WaitableEvent event(false, false); |
466 db_thread->message_loop()->PostTask( | 463 db_thread->task_runner()->PostTask( |
467 FROM_HERE, base::Bind(&AppCacheStorageImplTest::SignalEvent, &event)); | 464 FROM_HERE, base::Bind(&AppCacheStorageImplTest::SignalEvent, &event)); |
468 event.Wait(); | 465 event.Wait(); |
469 } | 466 } |
470 | 467 |
471 // LoadCache_Miss ---------------------------------------------------- | 468 // LoadCache_Miss ---------------------------------------------------- |
472 | 469 |
473 void LoadCache_Miss() { | 470 void LoadCache_Miss() { |
474 // Attempt to load a cache that doesn't exist. Should | 471 // Attempt to load a cache that doesn't exist. Should |
475 // complete asynchronously. | 472 // complete asynchronously. |
476 PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_LoadCache_Miss, | 473 PushNextTask(base::Bind(&AppCacheStorageImplTest::Verify_LoadCache_Miss, |
(...skipping 1248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1725 service_->quota_manager_proxy_ = mock_quota_manager_proxy_; | 1722 service_->quota_manager_proxy_ = mock_quota_manager_proxy_; |
1726 delegate_.reset(new MockStorageDelegate(this)); | 1723 delegate_.reset(new MockStorageDelegate(this)); |
1727 | 1724 |
1728 // Additional setup to observe reinitailize happens. | 1725 // Additional setup to observe reinitailize happens. |
1729 observer_.reset(new MockServiceObserver(this)); | 1726 observer_.reset(new MockServiceObserver(this)); |
1730 service_->AddObserver(observer_.get()); | 1727 service_->AddObserver(observer_.get()); |
1731 | 1728 |
1732 // We continue after the init task is complete including the callback | 1729 // We continue after the init task is complete including the callback |
1733 // on the current thread. | 1730 // on the current thread. |
1734 FlushDbThreadTasks(); | 1731 FlushDbThreadTasks(); |
1735 base::MessageLoop::current()->PostTask( | 1732 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1736 FROM_HERE, | 1733 FROM_HERE, base::Bind(&AppCacheStorageImplTest::Continue_Reinitialize, |
1737 base::Bind(&AppCacheStorageImplTest::Continue_Reinitialize, | 1734 base::Unretained(this), test_case)); |
1738 base::Unretained(this), | |
1739 test_case)); | |
1740 } | 1735 } |
1741 | 1736 |
1742 void Continue_Reinitialize(ReinitTestCase test_case) { | 1737 void Continue_Reinitialize(ReinitTestCase test_case) { |
1743 const int kMockProcessId = 1; | 1738 const int kMockProcessId = 1; |
1744 backend_.reset(new AppCacheBackendImpl); | 1739 backend_.reset(new AppCacheBackendImpl); |
1745 backend_->Initialize(service_.get(), &frontend_, kMockProcessId); | 1740 backend_->Initialize(service_.get(), &frontend_, kMockProcessId); |
1746 | 1741 |
1747 if (test_case == CORRUPT_SQL_ON_INSTALL) { | 1742 if (test_case == CORRUPT_SQL_ON_INSTALL) { |
1748 // Break the db file | 1743 // Break the db file |
1749 EXPECT_FALSE(database()->was_corruption_detected()); | 1744 EXPECT_FALSE(database()->was_corruption_detected()); |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2026 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); | 2021 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); |
2027 } | 2022 } |
2028 | 2023 |
2029 TEST_F(AppCacheStorageImplTest, Reinitialize3) { | 2024 TEST_F(AppCacheStorageImplTest, Reinitialize3) { |
2030 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); | 2025 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); |
2031 } | 2026 } |
2032 | 2027 |
2033 // That's all folks! | 2028 // That's all folks! |
2034 | 2029 |
2035 } // namespace content | 2030 } // namespace content |
OLD | NEW |