| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/thread.h" | 10 #include "base/thread.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 private: | 66 private: |
| 67 AppCacheRequestHandlerTest* test_; | 67 AppCacheRequestHandlerTest* test_; |
| 68 Method method_; | 68 Method method_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 // Subclasses to simulate particular response codes so test cases can | 71 // Subclasses to simulate particular response codes so test cases can |
| 72 // exercise fallback code paths. | 72 // exercise fallback code paths. |
| 73 | 73 |
| 74 class MockURLRequestJob : public URLRequestJob { | 74 class MockURLRequestJob : public URLRequestJob { |
| 75 public: | 75 public: |
| 76 MockURLRequestJob(URLRequest* request, int response_code) | 76 MockURLRequestJob(net::URLRequest* request, int response_code) |
| 77 : URLRequestJob(request), response_code_(response_code) {} | 77 : URLRequestJob(request), response_code_(response_code) {} |
| 78 virtual void Start() {} | 78 virtual void Start() {} |
| 79 virtual int GetResponseCode() const { return response_code_; } | 79 virtual int GetResponseCode() const { return response_code_; } |
| 80 int response_code_; | 80 int response_code_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 class MockURLRequest : public URLRequest { | 83 class MockURLRequest : public net::URLRequest { |
| 84 public: | 84 public: |
| 85 explicit MockURLRequest(const GURL& url) : URLRequest(url, NULL) {} | 85 explicit MockURLRequest(const GURL& url) : net::URLRequest(url, NULL) {} |
| 86 | 86 |
| 87 void SimulateResponseCode(int http_response_code) { | 87 void SimulateResponseCode(int http_response_code) { |
| 88 mock_factory_job_ = new MockURLRequestJob(this, http_response_code); | 88 mock_factory_job_ = new MockURLRequestJob(this, http_response_code); |
| 89 Start(); | 89 Start(); |
| 90 DCHECK(!mock_factory_job_); | 90 DCHECK(!mock_factory_job_); |
| 91 // All our simulation need to do satisfy are the following two DCHECKs | 91 // All our simulation need to do satisfy are the following two DCHECKs |
| 92 DCHECK(status().is_success()); | 92 DCHECK(status().is_success()); |
| 93 DCHECK_EQ(http_response_code, GetResponseCode()); | 93 DCHECK_EQ(http_response_code, GetResponseCode()); |
| 94 } | 94 } |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 static URLRequestJob* MockHttpJobFactory(URLRequest* request, | 97 static URLRequestJob* MockHttpJobFactory(net::URLRequest* request, |
| 98 const std::string& scheme) { | 98 const std::string& scheme) { |
| 99 if (mock_factory_job_) { | 99 if (mock_factory_job_) { |
| 100 URLRequestJob* temp = mock_factory_job_; | 100 URLRequestJob* temp = mock_factory_job_; |
| 101 mock_factory_job_ = NULL; | 101 mock_factory_job_ = NULL; |
| 102 return temp; | 102 return temp; |
| 103 } else { | 103 } else { |
| 104 // Some of these tests trigger UpdateJobs which start URLRequests. | 104 // Some of these tests trigger UpdateJobs which start URLRequests. |
| 105 // We short circuit those be returning error jobs. | 105 // We short circuit those be returning error jobs. |
| 106 return new URLRequestErrorJob(request, net::ERR_INTERNET_DISCONNECTED); | 106 return new URLRequestErrorJob(request, net::ERR_INTERNET_DISCONNECTED); |
| 107 } | 107 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 126 template <class Method> | 126 template <class Method> |
| 127 void RunTestOnIOThread(Method method) { | 127 void RunTestOnIOThread(Method method) { |
| 128 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 128 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
| 129 io_thread_->message_loop()->PostTask( | 129 io_thread_->message_loop()->PostTask( |
| 130 FROM_HERE, new WrapperTask<Method>(this, method)); | 130 FROM_HERE, new WrapperTask<Method>(this, method)); |
| 131 test_finished_event_->Wait(); | 131 test_finished_event_->Wait(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void SetUpTest() { | 134 void SetUpTest() { |
| 135 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 135 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 136 orig_http_factory_ = URLRequest::RegisterProtocolFactory( | 136 orig_http_factory_ = net::URLRequest::RegisterProtocolFactory( |
| 137 "http", MockHttpJobFactory); | 137 "http", MockHttpJobFactory); |
| 138 mock_service_.reset(new MockAppCacheService); | 138 mock_service_.reset(new MockAppCacheService); |
| 139 mock_frontend_.reset(new MockFrontend); | 139 mock_frontend_.reset(new MockFrontend); |
| 140 backend_impl_.reset(new AppCacheBackendImpl); | 140 backend_impl_.reset(new AppCacheBackendImpl); |
| 141 backend_impl_->Initialize(mock_service_.get(), mock_frontend_.get(), | 141 backend_impl_->Initialize(mock_service_.get(), mock_frontend_.get(), |
| 142 kMockProcessId); | 142 kMockProcessId); |
| 143 const int kHostId = 1; | 143 const int kHostId = 1; |
| 144 backend_impl_->RegisterHost(kHostId); | 144 backend_impl_->RegisterHost(kHostId); |
| 145 host_ = backend_impl_->GetHost(kHostId); | 145 host_ = backend_impl_->GetHost(kHostId); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void TearDownTest() { | 148 void TearDownTest() { |
| 149 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 149 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 150 DCHECK(!mock_factory_job_); | 150 DCHECK(!mock_factory_job_); |
| 151 URLRequest::RegisterProtocolFactory("http", orig_http_factory_); | 151 net::URLRequest::RegisterProtocolFactory("http", orig_http_factory_); |
| 152 orig_http_factory_ = NULL; | 152 orig_http_factory_ = NULL; |
| 153 job_ = NULL; | 153 job_ = NULL; |
| 154 handler_.reset(); | 154 handler_.reset(); |
| 155 request_.reset(); | 155 request_.reset(); |
| 156 backend_impl_.reset(); | 156 backend_impl_.reset(); |
| 157 mock_frontend_.reset(); | 157 mock_frontend_.reset(); |
| 158 mock_service_.reset(); | 158 mock_service_.reset(); |
| 159 host_ = NULL; | 159 host_ = NULL; |
| 160 } | 160 } |
| 161 | 161 |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 | 658 |
| 659 scoped_ptr<base::WaitableEvent> test_finished_event_; | 659 scoped_ptr<base::WaitableEvent> test_finished_event_; |
| 660 std::stack<Task*> task_stack_; | 660 std::stack<Task*> task_stack_; |
| 661 scoped_ptr<MockAppCacheService> mock_service_; | 661 scoped_ptr<MockAppCacheService> mock_service_; |
| 662 scoped_ptr<AppCacheBackendImpl> backend_impl_; | 662 scoped_ptr<AppCacheBackendImpl> backend_impl_; |
| 663 scoped_ptr<MockFrontend> mock_frontend_; | 663 scoped_ptr<MockFrontend> mock_frontend_; |
| 664 AppCacheHost* host_; | 664 AppCacheHost* host_; |
| 665 scoped_ptr<MockURLRequest> request_; | 665 scoped_ptr<MockURLRequest> request_; |
| 666 scoped_ptr<AppCacheRequestHandler> handler_; | 666 scoped_ptr<AppCacheRequestHandler> handler_; |
| 667 scoped_refptr<AppCacheURLRequestJob> job_; | 667 scoped_refptr<AppCacheURLRequestJob> job_; |
| 668 URLRequest::ProtocolFactory* orig_http_factory_; | 668 net::URLRequest::ProtocolFactory* orig_http_factory_; |
| 669 | 669 |
| 670 static scoped_ptr<base::Thread> io_thread_; | 670 static scoped_ptr<base::Thread> io_thread_; |
| 671 static URLRequestJob* mock_factory_job_; | 671 static URLRequestJob* mock_factory_job_; |
| 672 }; | 672 }; |
| 673 | 673 |
| 674 // static | 674 // static |
| 675 scoped_ptr<base::Thread> AppCacheRequestHandlerTest::io_thread_; | 675 scoped_ptr<base::Thread> AppCacheRequestHandlerTest::io_thread_; |
| 676 URLRequestJob* AppCacheRequestHandlerTest::mock_factory_job_ = NULL; | 676 URLRequestJob* AppCacheRequestHandlerTest::mock_factory_job_ = NULL; |
| 677 | 677 |
| 678 TEST_F(AppCacheRequestHandlerTest, MainResource_Miss) { | 678 TEST_F(AppCacheRequestHandlerTest, MainResource_Miss) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 | 738 |
| 739 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { | 739 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { |
| 740 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); | 740 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); |
| 741 } | 741 } |
| 742 | 742 |
| 743 } // namespace appcache | 743 } // namespace appcache |
| 744 | 744 |
| 745 // AppCacheRequestHandlerTest is expected to always live longer than the | 745 // AppCacheRequestHandlerTest is expected to always live longer than the |
| 746 // runnable methods. This lets us call NewRunnableMethod on its instances. | 746 // runnable methods. This lets us call NewRunnableMethod on its instances. |
| 747 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheRequestHandlerTest); | 747 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheRequestHandlerTest); |
| OLD | NEW |