Chromium Code Reviews| 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 class to run a test on our io_thread. The io_thread is spun up once |
| 59 // is spun up once and reused for all tests. | 59 // and reused for all tests. |
| 60 template <class Method> | 60 template <class Method> |
| 61 class WrapperTask : public Task { | 61 class MethodWrapper |
| 62 : public base::RefCountedThreadSafe<MethodWrapper<Method> > { | |
| 62 public: | 63 public: |
| 63 WrapperTask(AppCacheRequestHandlerTest* test, Method method) | 64 MethodWrapper(AppCacheRequestHandlerTest* test, Method method) |
| 64 : test_(test), method_(method) { | 65 : test_(test), method_(method) { |
| 65 } | 66 } |
| 66 | 67 |
| 67 virtual void Run() { | 68 void Run() { |
| 68 test_->SetUpTest(); | 69 test_->SetUpTest(); |
| 69 (test_->*method_)(); | 70 (test_->*method_)(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 private: | 73 private: |
| 73 AppCacheRequestHandlerTest* test_; | 74 AppCacheRequestHandlerTest* test_; |
| 74 Method method_; | 75 Method method_; |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(MethodWrapper); | |
| 75 }; | 78 }; |
| 76 | 79 |
| 77 // Subclasses to simulate particular responses so test cases can | 80 // Subclasses to simulate particular responses so test cases can |
| 78 // exercise fallback code paths. | 81 // exercise fallback code paths. |
| 79 | 82 |
| 80 class MockURLRequestDelegate : public net::URLRequest::Delegate { | 83 class MockURLRequestDelegate : public net::URLRequest::Delegate { |
| 81 virtual void OnResponseStarted(net::URLRequest* request) {} | 84 virtual void OnResponseStarted(net::URLRequest* request) {} |
| 82 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) {} | 85 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) {} |
| 83 }; | 86 }; |
| 84 | 87 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 // Test harness -------------------------------------------------- | 164 // Test harness -------------------------------------------------- |
| 162 | 165 |
| 163 AppCacheRequestHandlerTest() | 166 AppCacheRequestHandlerTest() |
| 164 : host_(NULL), orig_http_factory_(NULL) { | 167 : host_(NULL), orig_http_factory_(NULL) { |
| 165 } | 168 } |
| 166 | 169 |
| 167 template <class Method> | 170 template <class Method> |
| 168 void RunTestOnIOThread(Method method) { | 171 void RunTestOnIOThread(Method method) { |
| 169 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 172 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
| 170 io_thread_->message_loop()->PostTask( | 173 io_thread_->message_loop()->PostTask( |
| 171 FROM_HERE, new WrapperTask<Method>(this, method)); | 174 FROM_HERE, base::Bind(&MethodWrapper<Method>::Run, |
|
awong
2011/12/20 23:51:47
I think this can just be base::Bind(method, base::
James Hawkins
2011/12/21 00:43:08
The wrapper calls test->SetUpTest() before calling
awong
2011/12/21 00:50:32
Gotcha. Then can we change MethodWrapper to just
| |
| 175 new MethodWrapper<Method>(this, method))); | |
| 172 test_finished_event_->Wait(); | 176 test_finished_event_->Wait(); |
| 173 } | 177 } |
| 174 | 178 |
| 175 void SetUpTest() { | 179 void SetUpTest() { |
| 176 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 180 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
| 177 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( | 181 orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( |
| 178 "http", MockHttpJobFactory); | 182 "http", MockHttpJobFactory); |
| 179 mock_service_.reset(new MockAppCacheService); | 183 mock_service_.reset(new MockAppCacheService); |
| 180 mock_policy_.reset(new MockAppCachePolicy); | 184 mock_policy_.reset(new MockAppCachePolicy); |
| 181 mock_service_->set_appcache_policy(mock_policy_.get()); | 185 mock_service_->set_appcache_policy(mock_policy_.get()); |
| (...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 898 | 902 |
| 899 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { | 903 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { |
| 900 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); | 904 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); |
| 901 } | 905 } |
| 902 | 906 |
| 903 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { | 907 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { |
| 904 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); | 908 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); |
| 905 } | 909 } |
| 906 | 910 |
| 907 } // namespace appcache | 911 } // namespace appcache |
| OLD | NEW |