| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, | 48 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, |
| 49 const GURL& url, | 49 const GURL& url, |
| 50 int num_total, int num_complete) {} | 50 int num_total, int num_complete) {} |
| 51 | 51 |
| 52 virtual void OnLogMessage(int host_id, appcache::LogLevel log_level, | 52 virtual void OnLogMessage(int host_id, appcache::LogLevel log_level, |
| 53 const std::string& message) {} | 53 const std::string& message) {} |
| 54 | 54 |
| 55 virtual void OnContentBlocked(int host_id, const GURL& manifest_url) {} | 55 virtual void OnContentBlocked(int host_id, const GURL& manifest_url) {} |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Helper class run a test on our io_thread. The io_thread | 58 // Helper callback to run a test on our io_thread. The io_thread is spun up |
| 59 // is spun up once and reused for all tests. | 59 // once and reused for all tests. |
| 60 template <class Method> | 60 template <class Method> |
| 61 class WrapperTask : public Task { | 61 void MethodWrapper(Method method) { |
| 62 public: | 62 SetUpTest(); |
| 63 WrapperTask(AppCacheRequestHandlerTest* test, Method method) | 63 (this->*method)(); |
| 64 : test_(test), method_(method) { | 64 } |
| 65 } | |
| 66 | |
| 67 virtual void Run() { | |
| 68 test_->SetUpTest(); | |
| 69 (test_->*method_)(); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 AppCacheRequestHandlerTest* test_; | |
| 74 Method method_; | |
| 75 }; | |
| 76 | 65 |
| 77 // Subclasses to simulate particular responses so test cases can | 66 // Subclasses to simulate particular responses so test cases can |
| 78 // exercise fallback code paths. | 67 // exercise fallback code paths. |
| 79 | 68 |
| 80 class MockURLRequestDelegate : public net::URLRequest::Delegate { | 69 class MockURLRequestDelegate : public net::URLRequest::Delegate { |
| 81 virtual void OnResponseStarted(net::URLRequest* request) {} | 70 virtual void OnResponseStarted(net::URLRequest* request) {} |
| 82 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) {} | 71 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) {} |
| 83 }; | 72 }; |
| 84 | 73 |
| 85 class MockURLRequestJob : public net::URLRequestJob { | 74 class MockURLRequestJob : public net::URLRequestJob { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Test harness -------------------------------------------------- | 150 // Test harness -------------------------------------------------- |
| 162 | 151 |
| 163 AppCacheRequestHandlerTest() | 152 AppCacheRequestHandlerTest() |
| 164 : host_(NULL), orig_http_factory_(NULL) { | 153 : host_(NULL), orig_http_factory_(NULL) { |
| 165 } | 154 } |
| 166 | 155 |
| 167 template <class Method> | 156 template <class Method> |
| 168 void RunTestOnIOThread(Method method) { | 157 void RunTestOnIOThread(Method method) { |
| 169 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 158 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
| 170 io_thread_->message_loop()->PostTask( | 159 io_thread_->message_loop()->PostTask( |
| 171 FROM_HERE, new WrapperTask<Method>(this, method)); | 160 FROM_HERE, |
| 161 base::Bind(&AppCacheRequestHandlerTest::MethodWrapper<Method>, |
| 162 base::Unretained(this), method)); |
| 172 test_finished_event_->Wait(); | 163 test_finished_event_->Wait(); |
| 173 } | 164 } |
| 174 | 165 |
| 175 void SetUpTest() { | 166 void SetUpTest() { |
| 176 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 167 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 177 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( | 168 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 178 "http", MockHttpJobFactory); | 169 "http", MockHttpJobFactory); |
| 179 mock_service_.reset(new MockAppCacheService); | 170 mock_service_.reset(new MockAppCacheService); |
| 180 mock_policy_.reset(new MockAppCachePolicy); | 171 mock_policy_.reset(new MockAppCachePolicy); |
| 181 mock_service_->set_appcache_policy(mock_policy_.get()); | 172 mock_service_->set_appcache_policy(mock_policy_.get()); |
| (...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 | 889 |
| 899 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { | 890 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { |
| 900 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); | 891 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); |
| 901 } | 892 } |
| 902 | 893 |
| 903 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { | 894 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { |
| 904 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); | 895 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); |
| 905 } | 896 } |
| 906 | 897 |
| 907 } // namespace appcache | 898 } // namespace appcache |
| OLD | NEW |