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 #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 17 matching lines...) Expand all Loading... |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 | 29 |
30 namespace content { | 30 namespace content { |
31 | 31 |
32 static const int kMockProcessId = 1; | 32 static const int kMockProcessId = 1; |
33 | 33 |
34 class AppCacheRequestHandlerTest : public testing::Test { | 34 class AppCacheRequestHandlerTest : public testing::Test { |
35 public: | 35 public: |
36 class MockFrontend : public AppCacheFrontend { | 36 class MockFrontend : public AppCacheFrontend { |
37 public: | 37 public: |
38 virtual void OnCacheSelected( | 38 void OnCacheSelected(int host_id, const AppCacheInfo& info) override {} |
39 int host_id, const AppCacheInfo& info) override {} | |
40 | 39 |
41 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 40 void OnStatusChanged(const std::vector<int>& host_ids, |
42 AppCacheStatus status) override {} | 41 AppCacheStatus status) override {} |
43 | 42 |
44 virtual void OnEventRaised(const std::vector<int>& host_ids, | 43 void OnEventRaised(const std::vector<int>& host_ids, |
45 AppCacheEventID event_id) override {} | 44 AppCacheEventID event_id) override {} |
46 | 45 |
47 virtual void OnErrorEventRaised( | 46 void OnErrorEventRaised(const std::vector<int>& host_ids, |
48 const std::vector<int>& host_ids, | 47 const AppCacheErrorDetails& details) override {} |
49 const AppCacheErrorDetails& details) override {} | |
50 | 48 |
51 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, | 49 void OnProgressEventRaised(const std::vector<int>& host_ids, |
52 const GURL& url, | 50 const GURL& url, |
53 int num_total, | 51 int num_total, |
54 int num_complete) override { | 52 int num_complete) override {} |
55 } | |
56 | 53 |
57 virtual void OnLogMessage(int host_id, | 54 void OnLogMessage(int host_id, |
58 AppCacheLogLevel log_level, | 55 AppCacheLogLevel log_level, |
59 const std::string& message) override { | 56 const std::string& message) override {} |
60 } | |
61 | 57 |
62 virtual void OnContentBlocked(int host_id, | 58 void OnContentBlocked(int host_id, const GURL& manifest_url) override {} |
63 const GURL& manifest_url) override {} | |
64 }; | 59 }; |
65 | 60 |
66 // Helper callback to run a test on our io_thread. The io_thread is spun up | 61 // Helper callback to run a test on our io_thread. The io_thread is spun up |
67 // once and reused for all tests. | 62 // once and reused for all tests. |
68 template <class Method> | 63 template <class Method> |
69 void MethodWrapper(Method method) { | 64 void MethodWrapper(Method method) { |
70 SetUpTest(); | 65 SetUpTest(); |
71 (this->*method)(); | 66 (this->*method)(); |
72 } | 67 } |
73 | 68 |
74 // Subclasses to simulate particular responses so test cases can | 69 // Subclasses to simulate particular responses so test cases can |
75 // exercise fallback code paths. | 70 // exercise fallback code paths. |
76 | 71 |
77 class MockURLRequestDelegate : public net::URLRequest::Delegate { | 72 class MockURLRequestDelegate : public net::URLRequest::Delegate { |
78 virtual void OnResponseStarted(net::URLRequest* request) override {} | 73 void OnResponseStarted(net::URLRequest* request) override {} |
79 virtual void OnReadCompleted(net::URLRequest* request, | 74 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
80 int bytes_read) override { | |
81 } | |
82 }; | 75 }; |
83 | 76 |
84 class MockURLRequestJob : public net::URLRequestJob { | 77 class MockURLRequestJob : public net::URLRequestJob { |
85 public: | 78 public: |
86 MockURLRequestJob(net::URLRequest* request, | 79 MockURLRequestJob(net::URLRequest* request, |
87 net::NetworkDelegate* network_delegate, | 80 net::NetworkDelegate* network_delegate, |
88 int response_code) | 81 int response_code) |
89 : net::URLRequestJob(request, network_delegate), | 82 : net::URLRequestJob(request, network_delegate), |
90 response_code_(response_code), | 83 response_code_(response_code), |
91 has_response_info_(false) {} | 84 has_response_info_(false) {} |
92 MockURLRequestJob(net::URLRequest* request, | 85 MockURLRequestJob(net::URLRequest* request, |
93 net::NetworkDelegate* network_delegate, | 86 net::NetworkDelegate* network_delegate, |
94 const net::HttpResponseInfo& info) | 87 const net::HttpResponseInfo& info) |
95 : net::URLRequestJob(request, network_delegate), | 88 : net::URLRequestJob(request, network_delegate), |
96 response_code_(info.headers->response_code()), | 89 response_code_(info.headers->response_code()), |
97 has_response_info_(true), | 90 has_response_info_(true), |
98 response_info_(info) {} | 91 response_info_(info) {} |
99 | 92 |
100 protected: | 93 protected: |
101 virtual ~MockURLRequestJob() {} | 94 ~MockURLRequestJob() override {} |
102 virtual void Start() override { | 95 void Start() override { NotifyHeadersComplete(); } |
103 NotifyHeadersComplete(); | 96 int GetResponseCode() const override { return response_code_; } |
104 } | 97 void GetResponseInfo(net::HttpResponseInfo* info) override { |
105 virtual int GetResponseCode() const override { | |
106 return response_code_; | |
107 } | |
108 virtual void GetResponseInfo( | |
109 net::HttpResponseInfo* info) override { | |
110 if (!has_response_info_) | 98 if (!has_response_info_) |
111 return; | 99 return; |
112 *info = response_info_; | 100 *info = response_info_; |
113 } | 101 } |
114 | 102 |
115 private: | 103 private: |
116 int response_code_; | 104 int response_code_; |
117 bool has_response_info_; | 105 bool has_response_info_; |
118 net::HttpResponseInfo response_info_; | 106 net::HttpResponseInfo response_info_; |
119 }; | 107 }; |
120 | 108 |
121 class MockURLRequestJobFactory : public net::URLRequestJobFactory { | 109 class MockURLRequestJobFactory : public net::URLRequestJobFactory { |
122 public: | 110 public: |
123 MockURLRequestJobFactory() : job_(NULL) { | 111 MockURLRequestJobFactory() : job_(NULL) { |
124 } | 112 } |
125 | 113 |
126 virtual ~MockURLRequestJobFactory() { | 114 ~MockURLRequestJobFactory() override { DCHECK(!job_); } |
127 DCHECK(!job_); | |
128 } | |
129 | 115 |
130 void SetJob(net::URLRequestJob* job) { | 116 void SetJob(net::URLRequestJob* job) { |
131 job_ = job; | 117 job_ = job; |
132 } | 118 } |
133 | 119 |
134 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 120 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
135 const std::string& scheme, | 121 const std::string& scheme, |
136 net::URLRequest* request, | 122 net::URLRequest* request, |
137 net::NetworkDelegate* network_delegate) const override { | 123 net::NetworkDelegate* network_delegate) const override { |
138 if (job_) { | 124 if (job_) { |
139 net::URLRequestJob* temp = job_; | 125 net::URLRequestJob* temp = job_; |
140 job_ = NULL; | 126 job_ = NULL; |
141 return temp; | 127 return temp; |
142 } else { | 128 } else { |
143 // Some of these tests trigger UpdateJobs which start URLRequests. | 129 // Some of these tests trigger UpdateJobs which start URLRequests. |
144 // We short circuit those be returning error jobs. | 130 // We short circuit those be returning error jobs. |
145 return new net::URLRequestErrorJob(request, | 131 return new net::URLRequestErrorJob(request, |
146 network_delegate, | 132 network_delegate, |
147 net::ERR_INTERNET_DISCONNECTED); | 133 net::ERR_INTERNET_DISCONNECTED); |
148 } | 134 } |
149 } | 135 } |
150 | 136 |
151 virtual bool IsHandledProtocol(const std::string& scheme) const override { | 137 bool IsHandledProtocol(const std::string& scheme) const override { |
152 return scheme == "http"; | 138 return scheme == "http"; |
153 }; | 139 }; |
154 | 140 |
155 virtual bool IsHandledURL(const GURL& url) const override { | 141 bool IsHandledURL(const GURL& url) const override { |
156 return url.SchemeIs("http"); | 142 return url.SchemeIs("http"); |
157 } | 143 } |
158 | 144 |
159 virtual bool IsSafeRedirectTarget(const GURL& location) const override { | 145 bool IsSafeRedirectTarget(const GURL& location) const override { |
160 return false; | 146 return false; |
161 } | 147 } |
162 | 148 |
163 private: | 149 private: |
164 mutable net::URLRequestJob* job_; | 150 mutable net::URLRequestJob* job_; |
165 }; | 151 }; |
166 | 152 |
167 static void SetUpTestCase() { | 153 static void SetUpTestCase() { |
168 io_thread_.reset(new base::Thread("AppCacheRequestHandlerTest Thread")); | 154 io_thread_.reset(new base::Thread("AppCacheRequestHandlerTest Thread")); |
169 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 155 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 | 988 |
1003 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { | 989 TEST_F(AppCacheRequestHandlerTest, WorkerRequest) { |
1004 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); | 990 RunTestOnIOThread(&AppCacheRequestHandlerTest::WorkerRequest); |
1005 } | 991 } |
1006 | 992 |
1007 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { | 993 TEST_F(AppCacheRequestHandlerTest, MainResource_Blocked) { |
1008 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); | 994 RunTestOnIOThread(&AppCacheRequestHandlerTest::MainResource_Blocked); |
1009 } | 995 } |
1010 | 996 |
1011 } // namespace content | 997 } // namespace content |
OLD | NEW |