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

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: s/OnMesnsageReceived/Dispatch/ 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 : public MojoAsyncResourceHandler { 217 : public MojoAsyncResourceHandler {
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)),
228 task_runner_(new base::TestSimpleTaskRunner) {}
190 ~MojoAsyncResourceHandlerWithStubOperations() override {} 229 ~MojoAsyncResourceHandlerWithStubOperations() override {}
191 230
192 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } 231 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; }
193 232
194 void set_begin_write_expectation(MojoResult begin_write_expectation) { 233 void set_begin_write_expectation(MojoResult begin_write_expectation) {
195 is_begin_write_expectation_set_ = true; 234 is_begin_write_expectation_set_ = true;
196 begin_write_expectation_ = begin_write_expectation; 235 begin_write_expectation_ = begin_write_expectation;
197 } 236 }
198 void set_end_write_expectation(MojoResult end_write_expectation) { 237 void set_end_write_expectation(MojoResult end_write_expectation) {
199 is_end_write_expectation_set_ = true; 238 is_end_write_expectation_set_ = true;
200 end_write_expectation_ = end_write_expectation; 239 end_write_expectation_ = end_write_expectation;
201 } 240 }
202 bool has_received_bad_message() const { return has_received_bad_message_; } 241 bool has_received_bad_message() const { return has_received_bad_message_; }
203 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) { 242 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) {
204 metadata_ = std::move(metadata); 243 metadata_ = std::move(metadata);
205 } 244 }
206 245
246 FakeUploadProgressTracker* upload_progress_tracker() const {
247 return upload_progress_tracker_;
248 }
249
250 void PollUploadProgress() {
251 task_runner_->RunPendingTasks();
252 base::RunLoop().RunUntilIdle();
253 }
254
207 private: 255 private:
208 MojoResult BeginWrite(void** data, uint32_t* available) override { 256 MojoResult BeginWrite(void** data, uint32_t* available) override {
209 if (is_begin_write_expectation_set_) 257 if (is_begin_write_expectation_set_)
210 return begin_write_expectation_; 258 return begin_write_expectation_;
211 return MojoAsyncResourceHandler::BeginWrite(data, available); 259 return MojoAsyncResourceHandler::BeginWrite(data, available);
212 } 260 }
213 MojoResult EndWrite(uint32_t written) override { 261 MojoResult EndWrite(uint32_t written) override {
214 if (is_end_write_expectation_set_) 262 if (is_end_write_expectation_set_)
215 return end_write_expectation_; 263 return end_write_expectation_;
216 return MojoAsyncResourceHandler::EndWrite(written); 264 return MojoAsyncResourceHandler::EndWrite(written);
217 } 265 }
218 net::IOBufferWithSize* GetResponseMetadata( 266 net::IOBufferWithSize* GetResponseMetadata(
219 net::URLRequest* request) override { 267 net::URLRequest* request) override {
220 return metadata_.get(); 268 return metadata_.get();
221 } 269 }
222 270
223 void ReportBadMessage(const std::string& error) override { 271 void ReportBadMessage(const std::string& error) override {
224 has_received_bad_message_ = true; 272 has_received_bad_message_ = true;
225 } 273 }
226 274
275 std::unique_ptr<UploadProgressTracker> CreateUploadProgressTracker(
276 const tracked_objects::Location& from_here,
277 UploadProgressTracker::UploadProgressReportCallback callback) override {
278 DCHECK(!upload_progress_tracker_);
279
280 auto upload_progress_tracker = base::MakeUnique<FakeUploadProgressTracker>(
281 from_here, std::move(callback), request(), task_runner_);
282 upload_progress_tracker_ = upload_progress_tracker.get();
283 return std::move(upload_progress_tracker);
284 }
285
227 bool is_begin_write_expectation_set_ = false; 286 bool is_begin_write_expectation_set_ = false;
228 bool is_end_write_expectation_set_ = false; 287 bool is_end_write_expectation_set_ = false;
229 bool has_received_bad_message_ = false; 288 bool has_received_bad_message_ = false;
230 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 289 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
231 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 290 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
232 scoped_refptr<net::IOBufferWithSize> metadata_; 291 scoped_refptr<net::IOBufferWithSize> metadata_;
233 292
293 FakeUploadProgressTracker* upload_progress_tracker_ = nullptr;
294 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
295
234 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations); 296 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations);
235 }; 297 };
236 298
237 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { 299 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
238 public: 300 public:
239 TestURLLoaderFactory() {} 301 TestURLLoaderFactory() {}
240 ~TestURLLoaderFactory() override {} 302 ~TestURLLoaderFactory() override {}
241 303
242 void CreateLoaderAndStart( 304 void CreateLoaderAndStart(
243 mojom::URLLoaderAssociatedRequest request, 305 mojom::URLLoaderAssociatedRequest request,
(...skipping 22 matching lines...) Expand all
266 328
267 private: 329 private:
268 mojom::URLLoaderAssociatedRequest loader_request_; 330 mojom::URLLoaderAssociatedRequest loader_request_;
269 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info_; 331 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info_;
270 332
271 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory); 333 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory);
272 }; 334 };
273 335
274 class MojoAsyncResourceHandlerTestBase { 336 class MojoAsyncResourceHandlerTestBase {
275 public: 337 public:
276 MojoAsyncResourceHandlerTestBase() 338 explicit MojoAsyncResourceHandlerTestBase(
339 std::unique_ptr<net::UploadDataStream> upload_stream)
277 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 340 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
278 browser_context_(new TestBrowserContext()) { 341 browser_context_(new TestBrowserContext()) {
279 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024); 342 MojoAsyncResourceHandler::SetAllocationSizeForTesting(32 * 1024);
280 rdh_.SetDelegate(&rdh_delegate_); 343 rdh_.SetDelegate(&rdh_delegate_);
281 344
282 // Create and initialize |request_|. None of this matters, for these tests, 345 // Create and initialize |request_|. None of this matters, for these tests,
283 // just need something non-NULL. 346 // just need something non-NULL.
284 net::URLRequestContext* request_context = 347 net::URLRequestContext* request_context =
285 browser_context_->GetResourceContext()->GetRequestContext(); 348 browser_context_->GetResourceContext()->GetRequestContext();
286 request_ = request_context->CreateRequest( 349 request_ = request_context->CreateRequest(
287 GURL("http://foo/"), net::DEFAULT_PRIORITY, &url_request_delegate_); 350 GURL("http://foo/"), net::DEFAULT_PRIORITY, &url_request_delegate_);
351 request_->set_upload(std::move(upload_stream));
288 ResourceRequestInfo::AllocateForTesting( 352 ResourceRequestInfo::AllocateForTesting(
289 request_.get(), // request 353 request_.get(), // request
290 RESOURCE_TYPE_XHR, // resource_type 354 RESOURCE_TYPE_XHR, // resource_type
291 browser_context_->GetResourceContext(), // context 355 browser_context_->GetResourceContext(), // context
292 2, // render_process_id 356 2, // render_process_id
293 0, // render_view_id 357 0, // render_view_id
294 0, // render_frame_id 358 0, // render_frame_id
295 true, // is_main_frame 359 true, // is_main_frame
296 false, // parent_is_main_frame 360 false, // parent_is_main_frame
297 false, // allow_download 361 false, // allow_download
(...skipping 25 matching lines...) Expand all
323 mock_loader_.reset(new MockResourceLoader(handler_.get())); 387 mock_loader_.reset(new MockResourceLoader(handler_.get()));
324 } 388 }
325 389
326 virtual ~MojoAsyncResourceHandlerTestBase() { 390 virtual ~MojoAsyncResourceHandlerTestBase() {
327 MojoAsyncResourceHandler::SetAllocationSizeForTesting( 391 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
328 MojoAsyncResourceHandler::kDefaultAllocationSize); 392 MojoAsyncResourceHandler::kDefaultAllocationSize);
329 base::RunLoop().RunUntilIdle(); 393 base::RunLoop().RunUntilIdle();
330 } 394 }
331 395
332 // Returns false if something bad happens. 396 // Returns false if something bad happens.
333 bool CallOnWillStartAndOnResponseStarted() { 397 bool CallOnWillStart() {
334 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
335 MockResourceLoader::Status result = 398 MockResourceLoader::Status result =
336 mock_loader_->OnWillStart(request_->url()); 399 mock_loader_->OnWillStart(request_->url());
337 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); 400 EXPECT_EQ(MockResourceLoader::Status::IDLE, result);
338 if (result != MockResourceLoader::Status::IDLE) 401 return result == MockResourceLoader::Status::IDLE;
339 return false; 402 }
340 403
341 result = mock_loader_->OnResponseStarted( 404 // Returns false if something bad happens.
405 bool CallOnResponseStarted() {
406 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
407 MockResourceLoader::Status result = mock_loader_->OnResponseStarted(
342 make_scoped_refptr(new ResourceResponse())); 408 make_scoped_refptr(new ResourceResponse()));
343 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); 409 EXPECT_EQ(MockResourceLoader::Status::IDLE, result);
344 if (result != MockResourceLoader::Status::IDLE) 410 if (result != MockResourceLoader::Status::IDLE)
345 return false; 411 return false;
346 412
347 if (url_loader_client_.has_received_response()) { 413 if (url_loader_client_.has_received_response()) {
348 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; 414 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response.";
349 return false; 415 return false;
350 } 416 }
351 url_loader_client_.RunUntilResponseReceived(); 417 url_loader_client_.RunUntilResponseReceived();
352 return true; 418 return true;
353 } 419 }
354 420
421 // Returns false if something bad happens.
422 bool CallOnWillStartAndOnResponseStarted() {
423 return CallOnWillStart() && CallOnResponseStarted();
424 }
425
426 void set_upload_progress(const net::UploadProgress& upload_progress) {
427 handler_->upload_progress_tracker()->upload_progress_ = upload_progress;
428 }
429 void AdvanceCurrentTime(const base::TimeDelta& delta) {
430 handler_->upload_progress_tracker()->current_time_ += delta;
431 }
432
355 TestBrowserThreadBundle thread_bundle_; 433 TestBrowserThreadBundle thread_bundle_;
356 TestResourceDispatcherHostDelegate rdh_delegate_; 434 TestResourceDispatcherHostDelegate rdh_delegate_;
357 ResourceDispatcherHostImpl rdh_; 435 ResourceDispatcherHostImpl rdh_;
358 mojom::URLLoaderFactoryPtr url_loader_factory_; 436 mojom::URLLoaderFactoryPtr url_loader_factory_;
359 mojom::URLLoaderAssociatedPtr url_loader_proxy_; 437 mojom::URLLoaderAssociatedPtr url_loader_proxy_;
360 TestURLLoaderClient url_loader_client_; 438 TestURLLoaderClient url_loader_client_;
361 std::unique_ptr<TestBrowserContext> browser_context_; 439 std::unique_ptr<TestBrowserContext> browser_context_;
362 net::TestDelegate url_request_delegate_; 440 net::TestDelegate url_request_delegate_;
363 std::unique_ptr<net::URLRequest> request_; 441 std::unique_ptr<net::URLRequest> request_;
364 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_; 442 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_;
365 std::unique_ptr<MockResourceLoader> mock_loader_; 443 std::unique_ptr<MockResourceLoader> mock_loader_;
366 444
367 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); 445 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase);
368 }; 446 };
369 447
370 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, 448 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase,
371 public ::testing::Test {}; 449 public ::testing::Test {
450 protected:
451 MojoAsyncResourceHandlerTest() : MojoAsyncResourceHandlerTestBase(nullptr) {}
452 };
372 453
373 // This test class is parameterized with MojoAsyncResourceHandler's allocation 454 // This test class is parameterized with MojoAsyncResourceHandler's allocation
374 // size. 455 // size.
375 class MojoAsyncResourceHandlerWithAllocationSizeTest 456 class MojoAsyncResourceHandlerWithAllocationSizeTest
376 : public MojoAsyncResourceHandlerTestBase, 457 : public MojoAsyncResourceHandlerTestBase,
377 public ::testing::TestWithParam<size_t> { 458 public ::testing::TestWithParam<size_t> {
378 protected: 459 protected:
379 MojoAsyncResourceHandlerWithAllocationSizeTest() { 460 MojoAsyncResourceHandlerWithAllocationSizeTest()
461 : MojoAsyncResourceHandlerTestBase(nullptr) {
380 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); 462 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam());
381 } 463 }
382 }; 464 };
383 465
466 class MojoAsyncResourceHandlerUploadTest
467 : public MojoAsyncResourceHandlerTestBase,
468 public ::testing::Test {
469 protected:
470 MojoAsyncResourceHandlerUploadTest()
471 : MojoAsyncResourceHandlerTestBase(
472 base::MakeUnique<DummyUploadDataStream>()) {}
473 };
474
384 TEST_F(MojoAsyncResourceHandlerTest, InFlightRequests) { 475 TEST_F(MojoAsyncResourceHandlerTest, InFlightRequests) {
385 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 476 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
386 handler_ = nullptr; 477 handler_ = nullptr;
387 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 478 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
388 } 479 }
389 480
390 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { 481 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) {
391 EXPECT_EQ(MockResourceLoader::Status::IDLE, 482 EXPECT_EQ(MockResourceLoader::Status::IDLE,
392 mock_loader_->OnWillStart(request_->url())); 483 mock_loader_->OnWillStart(request_->url()));
393 } 484 }
(...skipping 27 matching lines...) Expand all
421 512
422 url_loader_client_.RunUntilResponseReceived(); 513 url_loader_client_.RunUntilResponseReceived();
423 EXPECT_EQ(response->head.request_start, 514 EXPECT_EQ(response->head.request_start,
424 url_loader_client_.response_head().request_start); 515 url_loader_client_.response_head().request_start);
425 EXPECT_EQ(response->head.response_start, 516 EXPECT_EQ(response->head.response_start,
426 url_loader_client_.response_head().response_start); 517 url_loader_client_.response_head().response_start);
427 EXPECT_EQ(99, url_loader_client_.response_head().content_length); 518 EXPECT_EQ(99, url_loader_client_.response_head().content_length);
428 519
429 url_loader_client_.RunUntilCachedMetadataReceived(); 520 url_loader_client_.RunUntilCachedMetadataReceived();
430 EXPECT_EQ("hello", url_loader_client_.cached_metadata()); 521 EXPECT_EQ("hello", url_loader_client_.cached_metadata());
522
523 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
431 } 524 }
432 525
433 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { 526 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) {
434 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 527 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
435 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 528 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
436 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); 529 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1));
437 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); 530 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
438 handler_ = nullptr; 531 handler_ = nullptr;
439 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 532 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
440 } 533 }
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 ASSERT_EQ(MOJO_RESULT_OK, result); 884 ASSERT_EQ(MOJO_RESULT_OK, result);
792 } 885 }
793 886
794 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT); 887 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
795 mock_loader_->WaitUntilIdleOrCanceled(); 888 mock_loader_->WaitUntilIdleOrCanceled();
796 EXPECT_FALSE(url_loader_client_.has_received_completion()); 889 EXPECT_FALSE(url_loader_client_.has_received_completion());
797 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); 890 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status());
798 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code()); 891 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code());
799 } 892 }
800 893
894 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) {
895 ASSERT_TRUE(CallOnWillStart());
896
897 // Expect no report for no progress.
898 set_upload_progress(net::UploadProgress(0, 1000));
899 handler_->PollUploadProgress();
900 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
901 EXPECT_EQ(0, url_loader_client_.current_upload_position());
902 EXPECT_EQ(0, url_loader_client_.total_upload_size());
903
904 // Expect a upload progress report for a good amount of progress.
905 url_loader_client_.reset_has_received_upload_progress();
906 set_upload_progress(net::UploadProgress(100, 1000));
907 handler_->PollUploadProgress();
908 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
909 EXPECT_EQ(100, url_loader_client_.current_upload_position());
910 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
911
912 // Expect a upload progress report for the passed time.
913 url_loader_client_.reset_has_received_upload_progress();
914 set_upload_progress(net::UploadProgress(101, 1000));
915 AdvanceCurrentTime(base::TimeDelta::FromSeconds(5));
916 handler_->PollUploadProgress();
917 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
918 EXPECT_EQ(101, url_loader_client_.current_upload_position());
919 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
920
921 // A redirect rewinds the upload progress. Expect no report for the rewound
922 // progress.
923 url_loader_client_.reset_has_received_upload_progress();
924 set_upload_progress(net::UploadProgress(0, 1000));
925 AdvanceCurrentTime(base::TimeDelta::FromSeconds(5));
926 handler_->PollUploadProgress();
927 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
928
929 // Set the progress to almost-finished state to prepare for the completion
930 // report below.
931 url_loader_client_.reset_has_received_upload_progress();
932 set_upload_progress(net::UploadProgress(999, 1000));
933 handler_->PollUploadProgress();
934 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
935 EXPECT_EQ(999, url_loader_client_.current_upload_position());
936 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
937
938 // Expect a upload progress report for the upload completion.
939 url_loader_client_.reset_has_received_upload_progress();
940 set_upload_progress(net::UploadProgress(1000, 1000));
941 ASSERT_TRUE(CallOnResponseStarted());
942 EXPECT_TRUE(url_loader_client_.has_received_upload_progress());
943 EXPECT_EQ(1000, url_loader_client_.current_upload_position());
944 EXPECT_EQ(1000, url_loader_client_.total_upload_size());
945 }
946
801 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 947 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
802 OnWillReadWithLongContents) { 948 OnWillReadWithLongContents) {
803 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 949 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
804 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); 950 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1));
805 std::string expected; 951 std::string expected;
806 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i) 952 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i)
807 expected += ('A' + i % 26); 953 expected += ('A' + i % 26);
808 954
809 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0)); 955 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0));
810 956
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 } 1261 }
1116 } 1262 }
1117 EXPECT_EQ("B", body); 1263 EXPECT_EQ("B", body);
1118 } 1264 }
1119 1265
1120 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1266 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1121 MojoAsyncResourceHandlerWithAllocationSizeTest, 1267 MojoAsyncResourceHandlerWithAllocationSizeTest,
1122 ::testing::Values(8, 32 * 2014)); 1268 ::testing::Values(8, 32 * 2014));
1123 } // namespace 1269 } // namespace
1124 } // namespace content 1270 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698