OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
14 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
15 #include "net/url_request/url_request_error_job.h" | 15 #include "net/url_request/url_request_error_job.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "webkit/appcache/appcache.h" | 17 #include "webkit/appcache/appcache.h" |
18 #include "webkit/appcache/appcache_backend_impl.h" | 18 #include "webkit/appcache/appcache_backend_impl.h" |
19 #include "webkit/appcache/appcache_request_handler.h" | 19 #include "webkit/appcache/appcache_request_handler.h" |
20 #include "webkit/appcache/appcache_url_request_job.h" | 20 #include "webkit/appcache/appcache_url_request_job.h" |
| 21 #include "webkit/appcache/mock_appcache_policy.h" |
21 #include "webkit/appcache/mock_appcache_service.h" | 22 #include "webkit/appcache/mock_appcache_service.h" |
22 | 23 |
23 namespace appcache { | 24 namespace appcache { |
24 | 25 |
25 static const int kMockProcessId = 1; | 26 static const int kMockProcessId = 1; |
26 | 27 |
27 class AppCacheRequestHandlerTest : public testing::Test { | 28 class AppCacheRequestHandlerTest : public testing::Test { |
28 public: | 29 public: |
29 class MockFrontend : public AppCacheFrontend { | 30 class MockFrontend : public AppCacheFrontend { |
30 public: | 31 public: |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 io_thread_->message_loop()->PostTask( | 166 io_thread_->message_loop()->PostTask( |
166 FROM_HERE, new WrapperTask<Method>(this, method)); | 167 FROM_HERE, new WrapperTask<Method>(this, method)); |
167 test_finished_event_->Wait(); | 168 test_finished_event_->Wait(); |
168 } | 169 } |
169 | 170 |
170 void SetUpTest() { | 171 void SetUpTest() { |
171 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 172 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
172 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( | 173 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( |
173 "http", MockHttpJobFactory); | 174 "http", MockHttpJobFactory); |
174 mock_service_.reset(new MockAppCacheService); | 175 mock_service_.reset(new MockAppCacheService); |
| 176 mock_policy_.reset(new MockAppCachePolicy); |
| 177 mock_service_->set_appcache_policy(mock_policy_.get()); |
175 mock_frontend_.reset(new MockFrontend); | 178 mock_frontend_.reset(new MockFrontend); |
176 backend_impl_.reset(new AppCacheBackendImpl); | 179 backend_impl_.reset(new AppCacheBackendImpl); |
177 backend_impl_->Initialize(mock_service_.get(), mock_frontend_.get(), | 180 backend_impl_->Initialize(mock_service_.get(), mock_frontend_.get(), |
178 kMockProcessId); | 181 kMockProcessId); |
179 const int kHostId = 1; | 182 const int kHostId = 1; |
180 backend_impl_->RegisterHost(kHostId); | 183 backend_impl_->RegisterHost(kHostId); |
181 host_ = backend_impl_->GetHost(kHostId); | 184 host_ = backend_impl_->GetHost(kHostId); |
182 } | 185 } |
183 | 186 |
184 void TearDownTest() { | 187 void TearDownTest() { |
185 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 188 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
186 DCHECK(!mock_factory_job_); | 189 DCHECK(!mock_factory_job_); |
187 net::URLRequest::Deprecated::RegisterProtocolFactory( | 190 net::URLRequest::Deprecated::RegisterProtocolFactory( |
188 "http", orig_http_factory_); | 191 "http", orig_http_factory_); |
189 orig_http_factory_ = NULL; | 192 orig_http_factory_ = NULL; |
190 job_ = NULL; | 193 job_ = NULL; |
191 handler_.reset(); | 194 handler_.reset(); |
192 request_.reset(); | 195 request_.reset(); |
193 backend_impl_.reset(); | 196 backend_impl_.reset(); |
194 mock_frontend_.reset(); | 197 mock_frontend_.reset(); |
195 mock_service_.reset(); | 198 mock_service_.reset(); |
| 199 mock_policy_.reset(); |
196 host_ = NULL; | 200 host_ = NULL; |
197 } | 201 } |
198 | 202 |
199 void TestFinished() { | 203 void TestFinished() { |
200 // We unwind the stack prior to finishing up to let stack | 204 // We unwind the stack prior to finishing up to let stack |
201 // based objects get deleted. | 205 // based objects get deleted. |
202 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 206 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
203 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 207 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
204 this, &AppCacheRequestHandlerTest::TestFinishedUnwound)); | 208 this, &AppCacheRequestHandlerTest::TestFinishedUnwound)); |
205 } | 209 } |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 worker_host = backend_impl_->GetHost(kAbandonedWorkerHostId); | 733 worker_host = backend_impl_->GetHost(kAbandonedWorkerHostId); |
730 EXPECT_EQ(NULL, backend_impl_->GetHost(kNonExsitingHostId)); | 734 EXPECT_EQ(NULL, backend_impl_->GetHost(kNonExsitingHostId)); |
731 worker_host->SelectCacheForWorker(kNonExsitingHostId, kMockProcessId); | 735 worker_host->SelectCacheForWorker(kNonExsitingHostId, kMockProcessId); |
732 handler_.reset(worker_host->CreateRequestHandler( | 736 handler_.reset(worker_host->CreateRequestHandler( |
733 request_.get(), ResourceType::SHARED_WORKER)); | 737 request_.get(), ResourceType::SHARED_WORKER)); |
734 EXPECT_FALSE(handler_.get()); | 738 EXPECT_FALSE(handler_.get()); |
735 | 739 |
736 TestFinished(); | 740 TestFinished(); |
737 } | 741 } |
738 | 742 |
| 743 // MainResource_Blocked -------------------------------------------------- |
| 744 |
| 745 void MainResource_Blocked() { |
| 746 PushNextTask(NewRunnableMethod( |
| 747 this, &AppCacheRequestHandlerTest::Verify_MainResource_Blocked)); |
| 748 |
| 749 request_.reset(new MockURLRequest(GURL("http://blah/"))); |
| 750 handler_.reset(host_->CreateRequestHandler(request_.get(), |
| 751 ResourceType::MAIN_FRAME)); |
| 752 EXPECT_TRUE(handler_.get()); |
| 753 |
| 754 mock_policy_->can_load_return_value_ = false; |
| 755 mock_storage()->SimulateFindMainResource( |
| 756 AppCacheEntry(AppCacheEntry::EXPLICIT, 1), |
| 757 GURL(), AppCacheEntry(), |
| 758 1, GURL("http://blah/manifest/")); |
| 759 |
| 760 job_ = handler_->MaybeLoadResource(request_.get()); |
| 761 EXPECT_TRUE(job_.get()); |
| 762 EXPECT_TRUE(job_->is_waiting()); |
| 763 |
| 764 // We have to wait for completion of storage->FindResponseForMainRequest. |
| 765 ScheduleNextTask(); |
| 766 } |
| 767 |
| 768 void Verify_MainResource_Blocked() { |
| 769 EXPECT_FALSE(job_->is_waiting()); |
| 770 EXPECT_FALSE(job_->is_delivering_appcache_response()); |
| 771 |
| 772 EXPECT_EQ(0, handler_->found_cache_id_); |
| 773 EXPECT_TRUE(handler_->found_manifest_url_.is_empty()); |
| 774 EXPECT_TRUE(host_->preferred_manifest_url().is_empty()); |
| 775 EXPECT_TRUE(host_->main_resource_blocked_); |
| 776 EXPECT_TRUE(host_->blocked_manifest_url_ == GURL("http://blah/manifest/")); |
| 777 |
| 778 TestFinished(); |
| 779 } |
| 780 |
739 // Test case helpers -------------------------------------------------- | 781 // Test case helpers -------------------------------------------------- |
740 | 782 |
741 AppCache* MakeNewCache() { | 783 AppCache* MakeNewCache() { |
742 AppCache* cache = new AppCache( | 784 AppCache* cache = new AppCache( |
743 mock_service_.get(), mock_storage()->NewCacheId()); | 785 mock_service_.get(), mock_storage()->NewCacheId()); |
744 cache->set_complete(true); | 786 cache->set_complete(true); |
745 AppCacheGroup* group = new AppCacheGroup( | 787 AppCacheGroup* group = new AppCacheGroup( |
746 mock_service_.get(), GURL("http://blah/manifest"), | 788 mock_service_.get(), GURL("http://blah/manifest"), |
747 mock_storage()->NewGroupId()); | 789 mock_storage()->NewGroupId()); |
748 group->AddCache(cache); | 790 group->AddCache(cache); |
749 return cache; | 791 return cache; |
750 } | 792 } |
751 | 793 |
752 MockAppCacheStorage* mock_storage() { | 794 MockAppCacheStorage* mock_storage() { |
753 return reinterpret_cast<MockAppCacheStorage*>(mock_service_->storage()); | 795 return reinterpret_cast<MockAppCacheStorage*>(mock_service_->storage()); |
754 } | 796 } |
755 | 797 |
756 // Data members -------------------------------------------------- | 798 // Data members -------------------------------------------------- |
757 | 799 |
758 scoped_ptr<base::WaitableEvent> test_finished_event_; | 800 scoped_ptr<base::WaitableEvent> test_finished_event_; |
759 std::stack<Task*> task_stack_; | 801 std::stack<Task*> task_stack_; |
760 scoped_ptr<MockAppCacheService> mock_service_; | 802 scoped_ptr<MockAppCacheService> mock_service_; |
761 scoped_ptr<AppCacheBackendImpl> backend_impl_; | 803 scoped_ptr<AppCacheBackendImpl> backend_impl_; |
762 scoped_ptr<MockFrontend> mock_frontend_; | 804 scoped_ptr<MockFrontend> mock_frontend_; |
| 805 scoped_ptr<MockAppCachePolicy> mock_policy_; |
763 AppCacheHost* host_; | 806 AppCacheHost* host_; |
764 scoped_ptr<MockURLRequest> request_; | 807 scoped_ptr<MockURLRequest> request_; |
765 scoped_ptr<AppCacheRequestHandler> handler_; | 808 scoped_ptr<AppCacheRequestHandler> handler_; |
766 scoped_refptr<AppCacheURLRequestJob> job_; | 809 scoped_refptr<AppCacheURLRequestJob> job_; |
767 net::URLRequest::ProtocolFactory* orig_http_factory_; | 810 net::URLRequest::ProtocolFactory* orig_http_factory_; |
768 | 811 |
769 static scoped_ptr<base::Thread> io_thread_; | 812 static scoped_ptr<base::Thread> io_thread_; |
770 static net::URLRequestJob* mock_factory_job_; | 813 static net::URLRequestJob* mock_factory_job_; |
771 }; | 814 }; |
772 | 815 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 } | 880 } |
838 | 881 |
839 TEST_F(AppCacheRequestHandlerTest, CanceledRequest) { | 882 TEST_F(AppCacheRequestHandlerTest, CanceledRequest) { |
840 RunTestOnIOThread(&AppCacheRequestHandlerTest::CanceledRequest); | 883 RunTestOnIOThread(&AppCacheRequestHandlerTest::CanceledRequest); |
841 } | 884 } |
842 | 885 |
843 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { | 886 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { |
844 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); | 887 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); |
845 } | 888 } |
846 | 889 |
| 890 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { |
| 891 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); |
| 892 } |
| 893 |
847 } // namespace appcache | 894 } // namespace appcache |
848 | 895 |
849 // AppCacheRequestHandlerTest is expected to always live longer than the | 896 // AppCacheRequestHandlerTest is expected to always live longer than the |
850 // runnable methods. This lets us call NewRunnableMethod on its instances. | 897 // runnable methods. This lets us call NewRunnableMethod on its instances. |
851 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheRequestHandlerTest); | 898 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheRequestHandlerTest); |
OLD | NEW |