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