Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: content/browser/loader/mojo_async_resource_handler_unittest.cc

Issue 2574143003: Implement upload progress handling in Mojo loading (Closed)
Patch Set: +comment Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/loader/mojo_async_resource_handler.h" 5 #include "content/browser/loader/mojo_async_resource_handler.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
16 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
17 #include "base/run_loop.h" 19 #include "base/run_loop.h"
20 #include "base/test/test_simple_task_runner.h"
18 #include "content/browser/loader/mock_resource_loader.h" 21 #include "content/browser/loader/mock_resource_loader.h"
19 #include "content/browser/loader/resource_controller.h" 22 #include "content/browser/loader/resource_controller.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h" 23 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/loader/resource_request_info_impl.h" 24 #include "content/browser/loader/resource_request_info_impl.h"
22 #include "content/browser/loader/test_url_loader_client.h" 25 #include "content/browser/loader/test_url_loader_client.h"
23 #include "content/common/resource_request_completion_status.h" 26 #include "content/common/resource_request_completion_status.h"
24 #include "content/common/url_loader.mojom.h" 27 #include "content/common/url_loader.mojom.h"
25 #include "content/public/browser/appcache_service.h" 28 #include "content/public/browser/appcache_service.h"
26 #include "content/public/browser/navigation_data.h" 29 #include "content/public/browser/navigation_data.h"
27 #include "content/public/browser/resource_context.h" 30 #include "content/public/browser/resource_context.h"
28 #include "content/public/browser/resource_dispatcher_host_delegate.h" 31 #include "content/public/browser/resource_dispatcher_host_delegate.h"
29 #include "content/public/browser/resource_throttle.h" 32 #include "content/public/browser/resource_throttle.h"
30 #include "content/public/browser/stream_info.h" 33 #include "content/public/browser/stream_info.h"
31 #include "content/public/common/previews_state.h" 34 #include "content/public/common/previews_state.h"
32 #include "content/public/common/resource_response.h" 35 #include "content/public/common/resource_response.h"
33 #include "content/public/common/resource_type.h" 36 #include "content/public/common/resource_type.h"
34 #include "content/public/test/test_browser_context.h" 37 #include "content/public/test/test_browser_context.h"
35 #include "content/public/test/test_browser_thread_bundle.h" 38 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "mojo/public/c/system/data_pipe.h" 39 #include "mojo/public/c/system/data_pipe.h"
37 #include "mojo/public/c/system/types.h" 40 #include "mojo/public/c/system/types.h"
38 #include "mojo/public/cpp/bindings/strong_binding.h" 41 #include "mojo/public/cpp/bindings/strong_binding.h"
39 #include "mojo/public/cpp/system/data_pipe.h" 42 #include "mojo/public/cpp/system/data_pipe.h"
40 #include "net/base/auth.h" 43 #include "net/base/auth.h"
41 #include "net/base/net_errors.h" 44 #include "net/base/net_errors.h"
42 #include "net/http/http_response_headers.h" 45 #include "net/http/http_response_headers.h"
43 #include "net/http/http_response_info.h" 46 #include "net/http/http_response_info.h"
44 #include "net/http/http_status_code.h" 47 #include "net/http/http_status_code.h"
45 #include "net/http/http_util.h" 48 #include "net/http/http_util.h"
46 #include "net/ssl/client_cert_store.h" 49 #include "net/ssl/client_cert_store.h"
50 #include "net/test/url_request/url_request_mock_data_job.h"
47 #include "net/url_request/url_request.h" 51 #include "net/url_request/url_request.h"
48 #include "net/url_request/url_request_context.h" 52 #include "net/url_request/url_request_context.h"
49 #include "net/url_request/url_request_status.h" 53 #include "net/url_request/url_request_status.h"
50 #include "net/url_request/url_request_test_util.h" 54 #include "net/url_request/url_request_test_util.h"
51 #include "testing/gtest/include/gtest/gtest.h" 55 #include "testing/gtest/include/gtest/gtest.h"
52 #include "ui/base/page_transition_types.h" 56 #include "ui/base/page_transition_types.h"
53 57
54 namespace content { 58 namespace content {
55 namespace { 59 namespace {
56 60
57 constexpr int kSizeMimeSnifferRequiresForFirstOnWillRead = 2048; 61 constexpr int kSizeMimeSnifferRequiresForFirstOnWillRead = 2048;
58 62
63 class DummyUploadDataStream : public net::UploadDataStream {
64 public:
65 DummyUploadDataStream() : UploadDataStream(false, 0) {}
66
67 int InitInternal(const net::NetLogWithSource& net_log) override {
68 NOTREACHED();
69 return 0;
70 }
71 int ReadInternal(net::IOBuffer* buf, int buf_len) override {
72 NOTREACHED();
73 return 0;
74 }
75 void ResetInternal() override { NOTREACHED(); }
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(DummyUploadDataStream);
79 };
80
81 class FakeUploadProgressTracker : public UploadProgressTracker {
82 public:
83 using UploadProgressTracker::UploadProgressTracker;
84
85 net::UploadProgress GetUploadProgress() const override {
86 return upload_progress_;
87 }
88 base::TimeTicks GetCurrentTime() const override { return current_time_; }
89
90 net::UploadProgress upload_progress_;
91 base::TimeTicks current_time_;
92
93 private:
94 DISALLOW_COPY_AND_ASSIGN(FakeUploadProgressTracker);
95 };
96
59 class TestResourceDispatcherHostDelegate final 97 class TestResourceDispatcherHostDelegate final
60 : public ResourceDispatcherHostDelegate { 98 : public ResourceDispatcherHostDelegate {
61 public: 99 public:
62 TestResourceDispatcherHostDelegate() = default; 100 TestResourceDispatcherHostDelegate() = default;
63 ~TestResourceDispatcherHostDelegate() override { 101 ~TestResourceDispatcherHostDelegate() override {
64 EXPECT_EQ(num_on_response_started_calls_expectation_, 102 EXPECT_EQ(num_on_response_started_calls_expectation_,
65 num_on_response_started_calls_); 103 num_on_response_started_calls_);
66 } 104 }
67 105
68 bool ShouldBeginRequest(const std::string& method, 106 bool ShouldBeginRequest(const std::string& method,
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 public: 218 public:
181 MojoAsyncResourceHandlerWithStubOperations( 219 MojoAsyncResourceHandlerWithStubOperations(
182 net::URLRequest* request, 220 net::URLRequest* request,
183 ResourceDispatcherHostImpl* rdh, 221 ResourceDispatcherHostImpl* rdh,
184 mojom::URLLoaderAssociatedRequest mojo_request, 222 mojom::URLLoaderAssociatedRequest mojo_request,
185 mojom::URLLoaderClientAssociatedPtr url_loader_client) 223 mojom::URLLoaderClientAssociatedPtr url_loader_client)
186 : MojoAsyncResourceHandler(request, 224 : MojoAsyncResourceHandler(request,
187 rdh, 225 rdh,
188 std::move(mojo_request), 226 std::move(mojo_request),
189 std::move(url_loader_client), 227 std::move(url_loader_client),
190 RESOURCE_TYPE_MAIN_FRAME) {} 228 RESOURCE_TYPE_MAIN_FRAME),
229 task_runner_(new base::TestSimpleTaskRunner) {}
191 ~MojoAsyncResourceHandlerWithStubOperations() override {} 230 ~MojoAsyncResourceHandlerWithStubOperations() override {}
192 231
193 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } 232 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; }
194 233
195 void set_begin_write_expectation(MojoResult begin_write_expectation) { 234 void set_begin_write_expectation(MojoResult begin_write_expectation) {
196 is_begin_write_expectation_set_ = true; 235 is_begin_write_expectation_set_ = true;
197 begin_write_expectation_ = begin_write_expectation; 236 begin_write_expectation_ = begin_write_expectation;
198 } 237 }
199 void set_end_write_expectation(MojoResult end_write_expectation) { 238 void set_end_write_expectation(MojoResult end_write_expectation) {
200 is_end_write_expectation_set_ = true; 239 is_end_write_expectation_set_ = true;
201 end_write_expectation_ = end_write_expectation; 240 end_write_expectation_ = end_write_expectation;
202 } 241 }
203 bool has_received_bad_message() const { return has_received_bad_message_; } 242 bool has_received_bad_message() const { return has_received_bad_message_; }
204 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) { 243 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) {
205 metadata_ = std::move(metadata); 244 metadata_ = std::move(metadata);
206 } 245 }
207 246
247 FakeUploadProgressTracker* upload_progress_tracker() const {
248 return upload_progress_tracker_;
249 }
250
251 void PollUploadProgress() {
252 task_runner_->RunPendingTasks();
253 base::RunLoop().RunUntilIdle();
254 }
255
208 private: 256 private:
209 MojoResult BeginWrite(void** data, uint32_t* available) override { 257 MojoResult BeginWrite(void** data, uint32_t* available) override {
210 if (is_begin_write_expectation_set_) 258 if (is_begin_write_expectation_set_)
211 return begin_write_expectation_; 259 return begin_write_expectation_;
212 return MojoAsyncResourceHandler::BeginWrite(data, available); 260 return MojoAsyncResourceHandler::BeginWrite(data, available);
213 } 261 }
214 MojoResult EndWrite(uint32_t written) override { 262 MojoResult EndWrite(uint32_t written) override {
215 if (is_end_write_expectation_set_) 263 if (is_end_write_expectation_set_)
216 return end_write_expectation_; 264 return end_write_expectation_;
217 return MojoAsyncResourceHandler::EndWrite(written); 265 return MojoAsyncResourceHandler::EndWrite(written);
218 } 266 }
219 net::IOBufferWithSize* GetResponseMetadata( 267 net::IOBufferWithSize* GetResponseMetadata(
220 net::URLRequest* request) override { 268 net::URLRequest* request) override {
221 return metadata_.get(); 269 return metadata_.get();
222 } 270 }
223 271
224 void ReportBadMessage(const std::string& error) override { 272 void ReportBadMessage(const std::string& error) override {
225 has_received_bad_message_ = true; 273 has_received_bad_message_ = true;
226 } 274 }
227 275
276 std::unique_ptr<UploadProgressTracker> CreateUploadProgressTracker(
277 const tracked_objects::Location& from_here,
278 UploadProgressTracker::UploadProgressReportCallback callback) override {
279 DCHECK(!upload_progress_tracker_);
280
281 auto upload_progress_tracker = base::MakeUnique<FakeUploadProgressTracker>(
282 from_here, std::move(callback), request(), task_runner_);
283 upload_progress_tracker_ = upload_progress_tracker.get();
284 return std::move(upload_progress_tracker);
285 }
286
228 bool is_begin_write_expectation_set_ = false; 287 bool is_begin_write_expectation_set_ = false;
229 bool is_end_write_expectation_set_ = false; 288 bool is_end_write_expectation_set_ = false;
230 bool has_received_bad_message_ = false; 289 bool has_received_bad_message_ = false;
231 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 290 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
232 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 291 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
233 scoped_refptr<net::IOBufferWithSize> metadata_; 292 scoped_refptr<net::IOBufferWithSize> metadata_;
234 293
294 FakeUploadProgressTracker* upload_progress_tracker_ = nullptr;
295 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
296
235 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations); 297 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations);
236 }; 298 };
237 299
238 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { 300 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
239 public: 301 public:
240 TestURLLoaderFactory() {} 302 TestURLLoaderFactory() {}
241 ~TestURLLoaderFactory() override {} 303 ~TestURLLoaderFactory() override {}
242 304
243 void CreateLoaderAndStart( 305 void CreateLoaderAndStart(
244 mojom::URLLoaderAssociatedRequest request, 306 mojom::URLLoaderAssociatedRequest request,
(...skipping 22 matching lines...) Expand all
267 329
268 private: 330 private:
269 mojom::URLLoaderAssociatedRequest loader_request_; 331 mojom::URLLoaderAssociatedRequest loader_request_;
270 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info_; 332 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info_;
271 333
272 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory); 334 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory);
273 }; 335 };
274 336
275 class MojoAsyncResourceHandlerTestBase { 337 class MojoAsyncResourceHandlerTestBase {
276 public: 338 public:
277 MojoAsyncResourceHandlerTestBase() 339 explicit MojoAsyncResourceHandlerTestBase(
340 std::unique_ptr<net::UploadDataStream> upload_stream)
278 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 341 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
279 browser_context_(new TestBrowserContext()) { 342 browser_context_(new TestBrowserContext()) {
280 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024); 343 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024);
281 rdh_.SetDelegate(&rdh_delegate_); 344 rdh_.SetDelegate(&rdh_delegate_);
282 345
283 // Create and initialize |request_|. None of this matters, for these tests, 346 // Create and initialize |request_|. None of this matters, for these tests,
284 // just need something non-NULL. 347 // just need something non-NULL.
285 net::URLRequestContext* request_context = 348 net::URLRequestContext* request_context =
286 browser_context_->GetResourceContext()->GetRequestContext(); 349 browser_context_->GetResourceContext()->GetRequestContext();
287 request_ = request_context->CreateRequest( 350 request_ = request_context->CreateRequest(
288 GURL("http://foo/"), net::DEFAULT_PRIORITY, &url_request_delegate_); 351 GURL("http://foo/"), net::DEFAULT_PRIORITY, &url_request_delegate_);
352 request_->set_upload(std::move(upload_stream));
289 ResourceRequestInfo::AllocateForTesting( 353 ResourceRequestInfo::AllocateForTesting(
290 request_.get(), // request 354 request_.get(), // request
291 RESOURCE_TYPE_XHR, // resource_type 355 RESOURCE_TYPE_XHR, // resource_type
292 browser_context_->GetResourceContext(), // context 356 browser_context_->GetResourceContext(), // context
293 2, // render_process_id 357 2, // render_process_id
294 0, // render_view_id 358 0, // render_view_id
295 0, // render_frame_id 359 0, // render_frame_id
296 true, // is_main_frame 360 true, // is_main_frame
297 false, // parent_is_main_frame 361 false, // parent_is_main_frame
298 false, // allow_download 362 false, // allow_download
(...skipping 25 matching lines...) Expand all
324 mock_loader_.reset(new MockResourceLoader(handler_.get())); 388 mock_loader_.reset(new MockResourceLoader(handler_.get()));
325 } 389 }
326 390
327 virtual ~MojoAsyncResourceHandlerTestBase() { 391 virtual ~MojoAsyncResourceHandlerTestBase() {
328 MojoAsyncResourceHandler::SetAllocationSizeForTesting( 392 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
329 MojoAsyncResourceHandler::kDefaultAllocationSize); 393 MojoAsyncResourceHandler::kDefaultAllocationSize);
330 base::RunLoop().RunUntilIdle(); 394 base::RunLoop().RunUntilIdle();
331 } 395 }
332 396
333 // Returns false if something bad happens. 397 // Returns false if something bad happens.
334 bool CallOnWillStartAndOnResponseStarted() { 398 bool CallOnWillStart() {
335 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
336 MockResourceLoader::Status result = 399 MockResourceLoader::Status result =
337 mock_loader_->OnWillStart(request_->url()); 400 mock_loader_->OnWillStart(request_->url());
338 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); 401 EXPECT_EQ(MockResourceLoader::Status::IDLE, result);
339 if (result != MockResourceLoader::Status::IDLE) 402 return result == MockResourceLoader::Status::IDLE;
340 return false; 403 }
341 404
342 result = mock_loader_->OnResponseStarted( 405 // Returns false if something bad happens.
406 bool CallOnResponseStarted() {
407 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
408 MockResourceLoader::Status result = mock_loader_->OnResponseStarted(
343 make_scoped_refptr(new ResourceResponse())); 409 make_scoped_refptr(new ResourceResponse()));
344 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); 410 EXPECT_EQ(MockResourceLoader::Status::IDLE, result);
345 if (result != MockResourceLoader::Status::IDLE) 411 if (result != MockResourceLoader::Status::IDLE)
346 return false; 412 return false;
347 413
348 if (url_loader_client_.has_received_response()) { 414 if (url_loader_client_.has_received_response()) {
349 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; 415 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response.";
350 return false; 416 return false;
351 } 417 }
352 url_loader_client_.RunUntilResponseReceived(); 418 url_loader_client_.RunUntilResponseReceived();
353 return true; 419 return true;
354 } 420 }
355 421
422 // Returns false if something bad happens.
423 bool CallOnWillStartAndOnResponseStarted() {
424 return CallOnWillStart() && CallOnResponseStarted();
425 }
426
427 void set_upload_progress(const net::UploadProgress& upload_progress) {
428 handler_->upload_progress_tracker()->upload_progress_ = upload_progress;
429 }
430 void AdvanceCurrentTime(const base::TimeDelta& delta) {
431 handler_->upload_progress_tracker()->current_time_ += delta;
432 }
433
356 TestBrowserThreadBundle thread_bundle_; 434 TestBrowserThreadBundle thread_bundle_;
357 TestResourceDispatcherHostDelegate rdh_delegate_; 435 TestResourceDispatcherHostDelegate rdh_delegate_;
358 ResourceDispatcherHostImpl rdh_; 436 ResourceDispatcherHostImpl rdh_;
359 mojom::URLLoaderFactoryPtr url_loader_factory_; 437 mojom::URLLoaderFactoryPtr url_loader_factory_;
360 mojom::URLLoaderAssociatedPtr url_loader_proxy_; 438 mojom::URLLoaderAssociatedPtr url_loader_proxy_;
361 TestURLLoaderClient url_loader_client_; 439 TestURLLoaderClient url_loader_client_;
362 std::unique_ptr<TestBrowserContext> browser_context_; 440 std::unique_ptr<TestBrowserContext> browser_context_;
363 net::TestDelegate url_request_delegate_; 441 net::TestDelegate url_request_delegate_;
364 std::unique_ptr<net::URLRequest> request_; 442 std::unique_ptr<net::URLRequest> request_;
365 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_; 443 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_;
366 std::unique_ptr<MockResourceLoader> mock_loader_; 444 std::unique_ptr<MockResourceLoader> mock_loader_;
367 445
368 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); 446 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase);
369 }; 447 };
370 448
371 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, 449 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase,
372 public ::testing::Test {}; 450 public ::testing::Test {
451 protected:
452 MojoAsyncResourceHandlerTest() : MojoAsyncResourceHandlerTestBase(nullptr) {}
453 };
373 454
374 // This test class is parameterized with MojoAsyncResourceHandler's allocation 455 // This test class is parameterized with MojoAsyncResourceHandler's allocation
375 // size. 456 // size.
376 class MojoAsyncResourceHandlerWithAllocationSizeTest 457 class MojoAsyncResourceHandlerWithAllocationSizeTest
377 : public MojoAsyncResourceHandlerTestBase, 458 : public MojoAsyncResourceHandlerTestBase,
378 public ::testing::TestWithParam<size_t> { 459 public ::testing::TestWithParam<size_t> {
379 protected: 460 protected:
380 MojoAsyncResourceHandlerWithAllocationSizeTest() { 461 MojoAsyncResourceHandlerWithAllocationSizeTest()
462 : MojoAsyncResourceHandlerTestBase(nullptr) {
381 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); 463 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam());
382 } 464 }
383 }; 465 };
384 466
467 class MojoAsyncResourceHandlerUploadTest
468 : public MojoAsyncResourceHandlerTestBase,
469 public ::testing::Test {
470 protected:
471 MojoAsyncResourceHandlerUploadTest()
472 : MojoAsyncResourceHandlerTestBase(
473 base::MakeUnique<DummyUploadDataStream>()) {}
474 };
475
385 TEST_F(MojoAsyncResourceHandlerTest, InFlightRequests) { 476 TEST_F(MojoAsyncResourceHandlerTest, InFlightRequests) {
386 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 477 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
387 handler_ = nullptr; 478 handler_ = nullptr;
388 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 479 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
389 } 480 }
390 481
391 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { 482 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) {
392 EXPECT_EQ(MockResourceLoader::Status::IDLE, 483 EXPECT_EQ(MockResourceLoader::Status::IDLE,
393 mock_loader_->OnWillStart(request_->url())); 484 mock_loader_->OnWillStart(request_->url()));
394 } 485 }
(...skipping 27 matching lines...) Expand all
422 513
423 url_loader_client_.RunUntilResponseReceived(); 514 url_loader_client_.RunUntilResponseReceived();
424 EXPECT_EQ(response->head.request_start, 515 EXPECT_EQ(response->head.request_start,
425 url_loader_client_.response_head().request_start); 516 url_loader_client_.response_head().request_start);
426 EXPECT_EQ(response->head.response_start, 517 EXPECT_EQ(response->head.response_start,
427 url_loader_client_.response_head().response_start); 518 url_loader_client_.response_head().response_start);
428 EXPECT_EQ(99, url_loader_client_.response_head().content_length); 519 EXPECT_EQ(99, url_loader_client_.response_head().content_length);
429 520
430 url_loader_client_.RunUntilCachedMetadataReceived(); 521 url_loader_client_.RunUntilCachedMetadataReceived();
431 EXPECT_EQ("hello", url_loader_client_.cached_metadata()); 522 EXPECT_EQ("hello", url_loader_client_.cached_metadata());
523
524 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
432 } 525 }
433 526
434 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { 527 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) {
435 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 528 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
436 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 529 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
437 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); 530 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1));
438 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); 531 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
439 handler_ = nullptr; 532 handler_ = nullptr;
440 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 533 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
441 } 534 }
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 ASSERT_EQ(MOJO_RESULT_OK, result); 885 ASSERT_EQ(MOJO_RESULT_OK, result);
793 } 886 }
794 887
795 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT); 888 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
796 mock_loader_->WaitUntilIdleOrCanceled(); 889 mock_loader_->WaitUntilIdleOrCanceled();
797 EXPECT_FALSE(url_loader_client_.has_received_completion()); 890 EXPECT_FALSE(url_loader_client_.has_received_completion());
798 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); 891 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status());
799 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code()); 892 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code());
800 } 893 }
801 894
895 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) {
896 ASSERT_TRUE(CallOnWillStart());
897
898 // Expect no report for no progress.
899 set_upload_progress(net::UploadProgress(0, 1000));
900 handler_->PollUploadProgress();
901 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
902 EXPECT_EQ(0, url_loader_client_.current_upload_position());
903 EXPECT_EQ(0, url_loader_client_.total_upload_size());
904
905 // Expect a upload progress report for a good amount of progress.
906 url_loader_client_.reset_has_received_upload_progress();
907 set_upload_progress(net::UploadProgress(100, 1000));
908 handler_->PollUploadProgress();
909 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
910 EXPECT_EQ(100, url_loader_client_.current_upload_position());
911 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
912
913 // Expect a upload progress report for the passed time.
914 url_loader_client_.reset_has_received_upload_progress();
915 set_upload_progress(net::UploadProgress(101, 1000));
916 AdvanceCurrentTime(base::TimeDelta::FromSeconds(5));
917 handler_->PollUploadProgress();
918 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
919 EXPECT_EQ(101, url_loader_client_.current_upload_position());
920 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
921
922 // A redirect rewinds the upload progress. Expect no report for the rewound
923 // progress.
924 url_loader_client_.reset_has_received_upload_progress();
925 set_upload_progress(net::UploadProgress(0, 1000));
926 AdvanceCurrentTime(base::TimeDelta::FromSeconds(5));
927 handler_->PollUploadProgress();
928 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
929
930 // Set the progress to almost-finished state to prepare for the completion
931 // report below.
932 url_loader_client_.reset_has_received_upload_progress();
933 set_upload_progress(net::UploadProgress(999, 1000));
934 handler_->PollUploadProgress();
935 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
936 EXPECT_EQ(999, url_loader_client_.current_upload_position());
937 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
938
939 // Expect a upload progress report for the upload completion.
940 url_loader_client_.reset_has_received_upload_progress();
941 set_upload_progress(net::UploadProgress(1000, 1000));
942 ASSERT_TRUE(CallOnResponseStarted());
943 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
944 EXPECT_EQ(1000, url_loader_client_.current_upload_position());
945 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
946 }
947
802 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 948 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
803 OnWillReadWithLongContents) { 949 OnWillReadWithLongContents) {
804 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 950 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
805 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); 951 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1));
806 std::string expected; 952 std::string expected;
807 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i) 953 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i)
808 expected += ('A' + i % 26); 954 expected += ('A' + i % 26);
809 955
810 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0)); 956 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0));
811 957
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 } 1262 }
1117 } 1263 }
1118 EXPECT_EQ("B", body); 1264 EXPECT_EQ("B", body);
1119 } 1265 }
1120 1266
1121 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1267 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1122 MojoAsyncResourceHandlerWithAllocationSizeTest, 1268 MojoAsyncResourceHandlerWithAllocationSizeTest,
1123 ::testing::Values(8, 32 * 2014)); 1269 ::testing::Values(8, 32 * 2014));
1124 } // namespace 1270 } // namespace
1125 } // namespace content 1271 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.cc ('k') | content/browser/loader/test_url_loader_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698