OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #import "ios/web/net/request_tracker_impl.h" | 5 #import "ios/web/net/request_tracker_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #import "base/mac/scoped_nsobject.h" | |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
18 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
19 #include "ios/web/public/cert_policy.h" | 18 #include "ios/web/public/cert_policy.h" |
20 #include "ios/web/public/certificate_policy_cache.h" | 19 #include "ios/web/public/certificate_policy_cache.h" |
21 #include "ios/web/public/ssl_status.h" | 20 #include "ios/web/public/ssl_status.h" |
22 #include "ios/web/public/test/fakes/test_browser_state.h" | 21 #include "ios/web/public/test/fakes/test_browser_state.h" |
23 #include "ios/web/public/test/test_web_thread.h" | 22 #include "ios/web/public/test/test_web_thread.h" |
24 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
25 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
26 #include "net/test/cert_test_util.h" | 25 #include "net/test/cert_test_util.h" |
27 #include "net/test/test_data_directory.h" | 26 #include "net/test/test_data_directory.h" |
28 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
29 #include "net/url_request/url_request_context.h" | 28 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_job_factory.h" | 29 #include "net/url_request/url_request_job_factory.h" |
31 #include "net/url_request/url_request_job_factory_impl.h" | 30 #include "net/url_request/url_request_job_factory_impl.h" |
32 #include "net/url_request/url_request_test_job.h" | 31 #include "net/url_request/url_request_test_job.h" |
33 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
35 #import "testing/gtest_mac.h" | 34 #import "testing/gtest_mac.h" |
36 #include "testing/platform_test.h" | 35 #include "testing/platform_test.h" |
37 #import "third_party/ocmock/OCMock/OCMock.h" | 36 #import "third_party/ocmock/OCMock/OCMock.h" |
38 #include "third_party/ocmock/gtest_support.h" | 37 #include "third_party/ocmock/gtest_support.h" |
39 | 38 |
| 39 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 40 #error "This file requires ARC support." |
| 41 #endif |
| 42 |
40 @interface RequestTrackerNotificationReceiverTest | 43 @interface RequestTrackerNotificationReceiverTest |
41 : NSObject<CRWRequestTrackerDelegate> { | 44 : NSObject<CRWRequestTrackerDelegate> { |
42 @public | 45 @public |
43 float value_; | 46 float value_; |
44 float max_; | 47 float max_; |
45 @private | 48 @private |
46 base::scoped_nsobject<NSString> error_; | 49 NSString* error_; |
47 scoped_refptr<net::HttpResponseHeaders> headers_; | 50 scoped_refptr<net::HttpResponseHeaders> headers_; |
48 } | 51 } |
49 | 52 |
50 - (NSString*)error; | 53 - (NSString*)error; |
51 - (net::HttpResponseHeaders*)headers; | 54 - (net::HttpResponseHeaders*)headers; |
52 @end | 55 @end |
53 | 56 |
54 @implementation RequestTrackerNotificationReceiverTest | 57 @implementation RequestTrackerNotificationReceiverTest |
55 | 58 |
56 - (id)init { | 59 - (id)init { |
57 self = [super init]; | 60 self = [super init]; |
58 if (self) { | 61 if (self) { |
59 value_ = 0.0f; | 62 value_ = 0.0f; |
60 max_ = 0.0f; | 63 max_ = 0.0f; |
61 } | 64 } |
62 return self; | 65 return self; |
63 } | 66 } |
64 | 67 |
65 - (void)updatedProgress:(float)progress { | 68 - (void)updatedProgress:(float)progress { |
66 if (progress > 0.0f) { | 69 if (progress > 0.0f) { |
67 if (progress < value_) { | 70 if (progress < value_) { |
68 error_.reset( | 71 error_ = [NSString |
69 [[NSString stringWithFormat: | 72 stringWithFormat:@"going down from %f to %f", value_, progress]; |
70 @"going down from %f to %f", value_, progress] retain]); | |
71 } | 73 } |
72 value_ = progress; | 74 value_ = progress; |
73 } else { | 75 } else { |
74 value_ = 0.0f; | 76 value_ = 0.0f; |
75 } | 77 } |
76 if (value_ > max_) { | 78 if (value_ > max_) { |
77 max_ = value_; | 79 max_ = value_; |
78 } | 80 } |
79 } | 81 } |
80 | 82 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 public: | 133 public: |
132 RequestTrackerTest() | 134 RequestTrackerTest() |
133 : loop_(base::MessageLoop::TYPE_IO), | 135 : loop_(base::MessageLoop::TYPE_IO), |
134 ui_thread_(web::WebThread::UI, &loop_), | 136 ui_thread_(web::WebThread::UI, &loop_), |
135 io_thread_(web::WebThread::IO, &loop_){}; | 137 io_thread_(web::WebThread::IO, &loop_){}; |
136 | 138 |
137 ~RequestTrackerTest() override {} | 139 ~RequestTrackerTest() override {} |
138 | 140 |
139 void SetUp() override { | 141 void SetUp() override { |
140 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 142 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
141 request_group_id_.reset( | 143 request_group_id_ = [NSString stringWithFormat:@"test%d", g_count++]; |
142 [[NSString stringWithFormat:@"test%d", g_count++] retain]); | |
143 | 144 |
144 receiver_.reset([[RequestTrackerNotificationReceiverTest alloc] init]); | 145 receiver_ = [[RequestTrackerNotificationReceiverTest alloc] init]; |
145 tracker_ = web::RequestTrackerImpl::CreateTrackerForRequestGroupID( | 146 tracker_ = web::RequestTrackerImpl::CreateTrackerForRequestGroupID( |
146 request_group_id_, | 147 request_group_id_, |
147 &browser_state_, | 148 &browser_state_, |
148 browser_state_.GetRequestContext(), | 149 browser_state_.GetRequestContext(), |
149 receiver_); | 150 receiver_); |
150 } | 151 } |
151 | 152 |
152 void TearDown() override { | 153 void TearDown() override { |
153 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 154 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
154 tracker_->Close(); | 155 tracker_->Close(); |
155 } | 156 } |
156 | 157 |
157 base::MessageLoop loop_; | 158 base::MessageLoop loop_; |
158 web::TestWebThread ui_thread_; | 159 web::TestWebThread ui_thread_; |
159 web::TestWebThread io_thread_; | 160 web::TestWebThread io_thread_; |
160 | 161 |
161 base::scoped_nsobject<RequestTrackerNotificationReceiverTest> receiver_; | 162 RequestTrackerNotificationReceiverTest* receiver_; |
162 scoped_refptr<web::RequestTrackerImpl> tracker_; | 163 scoped_refptr<web::RequestTrackerImpl> tracker_; |
163 base::scoped_nsobject<NSString> request_group_id_; | 164 NSString* request_group_id_; |
164 web::TestBrowserState browser_state_; | 165 web::TestBrowserState browser_state_; |
165 std::vector<std::unique_ptr<net::URLRequestContext>> contexts_; | 166 std::vector<std::unique_ptr<net::URLRequestContext>> contexts_; |
166 std::vector<std::unique_ptr<net::URLRequest>> requests_; | 167 std::vector<std::unique_ptr<net::URLRequest>> requests_; |
167 net::URLRequestJobFactoryImpl job_factory_; | 168 net::URLRequestJobFactoryImpl job_factory_; |
168 | 169 |
169 GURL GetURL(size_t i) { | 170 GURL GetURL(size_t i) { |
170 std::stringstream ss; | 171 std::stringstream ss; |
171 ss << "http://www/"; | 172 ss << "http://www/"; |
172 ss << i; | 173 ss << i; |
173 return GURL(ss.str()); | 174 return GURL(ss.str()); |
(...skipping 23 matching lines...) Expand all Loading... |
197 if (base::Time::Now() > maxDate) | 198 if (base::Time::Now() > maxDate) |
198 return @"Time is up, too slow to go"; | 199 return @"Time is up, too slow to go"; |
199 base::RunLoop().RunUntilIdle(); | 200 base::RunLoop().RunUntilIdle(); |
200 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 201 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
201 } | 202 } |
202 return nil; | 203 return nil; |
203 } | 204 } |
204 | 205 |
205 NSString* CheckActive() { | 206 NSString* CheckActive() { |
206 NSString* message = WaitUntilLoop(^{ | 207 NSString* message = WaitUntilLoop(^{ |
207 return (receiver_.get()->value_ > 0.0f); | 208 return (receiver_->value_ > 0.0f); |
208 }); | 209 }); |
209 | 210 |
210 if (!message && (receiver_.get()->max_ == 0.0f)) | 211 if (!message && (receiver_->max_ == 0.0f)) |
211 message = @"Max should be greater than 0.0"; | 212 message = @"Max should be greater than 0.0"; |
212 return message; | 213 return message; |
213 } | 214 } |
214 | 215 |
215 void TrimRequest(NSString* tab_id, const GURL& url) { | 216 void TrimRequest(NSString* tab_id, const GURL& url) { |
216 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 217 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
217 receiver_.get()->value_ = 0.0f; | 218 receiver_->value_ = 0.0f; |
218 receiver_.get()->max_ = 0.0f; | 219 receiver_->max_ = 0.0f; |
219 tracker_->StartPageLoad(url, nil); | 220 tracker_->StartPageLoad(url, nil); |
220 } | 221 } |
221 | 222 |
222 void EndPage(NSString* tab_id, const GURL& url) { | 223 void EndPage(NSString* tab_id, const GURL& url) { |
223 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 224 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
224 tracker_->FinishPageLoad(url, false); | 225 tracker_->FinishPageLoad(url, false); |
225 receiver_.get()->value_ = 0.0f; | 226 receiver_->value_ = 0.0f; |
226 receiver_.get()->max_ = 0.0f; | 227 receiver_->max_ = 0.0f; |
227 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
228 } | 229 } |
229 | 230 |
230 net::TestJobInterceptor* AddInterceptorToRequest(size_t i) { | 231 net::TestJobInterceptor* AddInterceptorToRequest(size_t i) { |
231 // |interceptor| will be deleted from |job_factory_|'s destructor. | 232 // |interceptor| will be deleted from |job_factory_|'s destructor. |
232 net::TestJobInterceptor* protocol_handler = new net::TestJobInterceptor(); | 233 net::TestJobInterceptor* protocol_handler = new net::TestJobInterceptor(); |
233 job_factory_.SetProtocolHandler("http", base::WrapUnique(protocol_handler)); | 234 job_factory_.SetProtocolHandler("http", base::WrapUnique(protocol_handler)); |
234 contexts_[i]->set_job_factory(&job_factory_); | 235 contexts_[i]->set_job_factory(&job_factory_); |
235 return protocol_handler; | 236 return protocol_handler; |
236 } | 237 } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 base::Bind(&TwoStartsSSLCallback, | 493 base::Bind(&TwoStartsSSLCallback, |
493 &request_0_called)); | 494 &request_0_called)); |
494 tracker_->OnSSLCertificateError(GetSecureRequest(1), ssl_info, true, | 495 tracker_->OnSSLCertificateError(GetSecureRequest(1), ssl_info, true, |
495 base::Bind(&TwoStartsSSLCallback, | 496 base::Bind(&TwoStartsSSLCallback, |
496 &request_1_called)); | 497 &request_1_called)); |
497 EXPECT_TRUE(request_0_called); | 498 EXPECT_TRUE(request_0_called); |
498 EXPECT_TRUE(request_1_called); | 499 EXPECT_TRUE(request_1_called); |
499 } | 500 } |
500 | 501 |
501 } // namespace | 502 } // namespace |
OLD | NEW |