| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "content/browser/download/byte_stream.h" | 5 #include "content/browser/download/byte_stream.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 class MockTaskRunner : public base::SequencedTaskRunner { | 30 class MockTaskRunner : public base::SequencedTaskRunner { |
| 31 public: | 31 public: |
| 32 MockTaskRunner(); | 32 MockTaskRunner(); |
| 33 | 33 |
| 34 // TaskRunner functions. | 34 // TaskRunner functions. |
| 35 MOCK_METHOD3(PostDelayedTask, bool(const tracked_objects::Location&, | 35 MOCK_METHOD3(PostDelayedTask, bool(const tracked_objects::Location&, |
| 36 const base::Closure&, int64)); | |
| 37 MOCK_METHOD3(PostDelayedTask, bool(const tracked_objects::Location&, | |
| 38 const base::Closure&, base::TimeDelta)); | 36 const base::Closure&, base::TimeDelta)); |
| 39 | 37 |
| 40 MOCK_METHOD3(PostNonNestableDelayedTask, bool( | 38 MOCK_METHOD3(PostNonNestableDelayedTask, bool( |
| 41 const tracked_objects::Location&, | 39 const tracked_objects::Location&, |
| 42 const base::Closure&, | 40 const base::Closure&, |
| 43 int64 delay_ms)); | |
| 44 | |
| 45 MOCK_METHOD3(PostNonNestableDelayedTask, bool( | |
| 46 const tracked_objects::Location&, | |
| 47 const base::Closure&, | |
| 48 base::TimeDelta)); | 41 base::TimeDelta)); |
| 49 | 42 |
| 50 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool()); | 43 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool()); |
| 51 | 44 |
| 52 protected: | 45 protected: |
| 53 ~MockTaskRunner(); | 46 ~MockTaskRunner(); |
| 54 }; | 47 }; |
| 55 | 48 |
| 56 MockTaskRunner::MockTaskRunner() { } | 49 MockTaskRunner::MockTaskRunner() { } |
| 57 | 50 |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 // to how much data is pushed onto the pipe is not (currently) part | 323 // to how much data is pushed onto the pipe is not (currently) part |
| 331 // of the interface contract. If it becomes part of the contract, the | 324 // of the interface contract. If it becomes part of the contract, the |
| 332 // tests below should get much more precise. | 325 // tests below should get much more precise. |
| 333 | 326 |
| 334 // Confirm callback called when you add more than 33% of the buffer. | 327 // Confirm callback called when you add more than 33% of the buffer. |
| 335 | 328 |
| 336 // Setup callback | 329 // Setup callback |
| 337 int num_callbacks = 0; | 330 int num_callbacks = 0; |
| 338 byte_stream_output->RegisterCallback( | 331 byte_stream_output->RegisterCallback( |
| 339 base::Bind(CountCallbacks, &num_callbacks)); | 332 base::Bind(CountCallbacks, &num_callbacks)); |
| 340 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 333 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 341 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 334 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 342 Return(true))); | 335 Return(true))); |
| 343 | 336 |
| 344 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); | 337 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
| 345 message_loop_.RunAllPending(); | 338 message_loop_.RunAllPending(); |
| 346 | 339 |
| 347 // Check callback results match expectations. | 340 // Check callback results match expectations. |
| 348 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 341 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 349 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 342 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 350 .WillRepeatedly(Return(true)); | 343 .WillRepeatedly(Return(true)); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); | 397 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
| 405 | 398 |
| 406 // Allow bytes to transition (needed for message passing implementation), | 399 // Allow bytes to transition (needed for message passing implementation), |
| 407 // and get and validate the data. | 400 // and get and validate the data. |
| 408 message_loop_.RunAllPending(); | 401 message_loop_.RunAllPending(); |
| 409 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 402 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 410 byte_stream_output->Read(&output_io_buffer, &output_length)); | 403 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 411 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 404 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 412 | 405 |
| 413 // Setup expectations. | 406 // Setup expectations. |
| 414 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 407 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 415 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 408 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 416 Return(true))); | 409 Return(true))); |
| 417 | 410 |
| 418 // Grab data, triggering callback. Recorded on dispatch, but doesn't | 411 // Grab data, triggering callback. Recorded on dispatch, but doesn't |
| 419 // happen because it's caught by the mock. | 412 // happen because it's caught by the mock. |
| 420 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 413 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 421 byte_stream_output->Read(&output_io_buffer, &output_length)); | 414 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 422 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 415 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 423 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 416 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 424 .WillRepeatedly(Return(true)); | 417 .WillRepeatedly(Return(true)); |
| 425 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 418 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 426 | 419 |
| 427 // Confirm that the callback passed to the mock does what we expect. | 420 // Confirm that the callback passed to the mock does what we expect. |
| 428 EXPECT_EQ(0, num_callbacks); | 421 EXPECT_EQ(0, num_callbacks); |
| 429 intermediate_callback.Run(); | 422 intermediate_callback.Run(); |
| 430 EXPECT_EQ(1, num_callbacks); | 423 EXPECT_EQ(1, num_callbacks); |
| 431 | 424 |
| 432 // Same drill with final buffer. | 425 // Same drill with final buffer. |
| 433 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 426 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 434 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 427 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 435 Return(true))); | 428 Return(true))); |
| 436 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 429 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 437 byte_stream_output->Read(&output_io_buffer, &output_length)); | 430 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 438 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 431 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 439 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 432 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 440 .WillRepeatedly(Return(true)); | 433 .WillRepeatedly(Return(true)); |
| 441 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 434 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 442 EXPECT_EQ(content::ByteStreamOutput::STREAM_EMPTY, | 435 EXPECT_EQ(content::ByteStreamOutput::STREAM_EMPTY, |
| 443 byte_stream_output->Read(&output_io_buffer, &output_length)); | 436 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 462 10000, &byte_stream_input, &byte_stream_output); | 455 10000, &byte_stream_input, &byte_stream_output); |
| 463 | 456 |
| 464 scoped_refptr<net::IOBuffer> output_io_buffer; | 457 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 465 size_t output_length; | 458 size_t output_length; |
| 466 base::Closure intermediate_callback; | 459 base::Closure intermediate_callback; |
| 467 | 460 |
| 468 // Setup expectations and record initial state. | 461 // Setup expectations and record initial state. |
| 469 int num_callbacks = 0; | 462 int num_callbacks = 0; |
| 470 byte_stream_output->RegisterCallback( | 463 byte_stream_output->RegisterCallback( |
| 471 base::Bind(CountCallbacks, &num_callbacks)); | 464 base::Bind(CountCallbacks, &num_callbacks)); |
| 472 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 465 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 473 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 466 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 474 Return(true))); | 467 Return(true))); |
| 475 | 468 |
| 476 // Add data, and pass it across. | 469 // Add data, and pass it across. |
| 477 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); | 470 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
| 478 message_loop_.RunAllPending(); | 471 message_loop_.RunAllPending(); |
| 479 | 472 |
| 480 // The task runner should have been hit, but the callback count | 473 // The task runner should have been hit, but the callback count |
| 481 // isn't changed until we actually run the callback. | 474 // isn't changed until we actually run the callback. |
| 482 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 475 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); | 521 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
| 529 message_loop_.RunAllPending(); | 522 message_loop_.RunAllPending(); |
| 530 | 523 |
| 531 // Initial get should not trigger callback. | 524 // Initial get should not trigger callback. |
| 532 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 525 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 533 byte_stream_output->Read(&output_io_buffer, &output_length)); | 526 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 534 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 527 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 535 message_loop_.RunAllPending(); | 528 message_loop_.RunAllPending(); |
| 536 | 529 |
| 537 // Setup expectations. | 530 // Setup expectations. |
| 538 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 531 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 539 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 532 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 540 Return(true))); | 533 Return(true))); |
| 541 | 534 |
| 542 // Second get *should* trigger callback. | 535 // Second get *should* trigger callback. |
| 543 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 536 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 544 byte_stream_output->Read(&output_io_buffer, &output_length)); | 537 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 545 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 538 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 546 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 539 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 547 .WillRepeatedly(Return(true)); | 540 .WillRepeatedly(Return(true)); |
| 548 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 541 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 549 | 542 |
| 550 // Which should do the right thing when it's run. | 543 // Which should do the right thing when it's run. |
| 551 int num_alt_callbacks = 0; | 544 int num_alt_callbacks = 0; |
| 552 byte_stream_input->RegisterCallback( | 545 byte_stream_input->RegisterCallback( |
| 553 base::Bind(CountCallbacks, &num_alt_callbacks)); | 546 base::Bind(CountCallbacks, &num_alt_callbacks)); |
| 554 intermediate_callback.Run(); | 547 intermediate_callback.Run(); |
| 555 EXPECT_EQ(0, num_callbacks); | 548 EXPECT_EQ(0, num_callbacks); |
| 556 EXPECT_EQ(1, num_alt_callbacks); | 549 EXPECT_EQ(1, num_alt_callbacks); |
| 557 | 550 |
| 558 // Third get should also trigger callback. | 551 // Third get should also trigger callback. |
| 559 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 552 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 560 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 553 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 561 Return(true))); | 554 Return(true))); |
| 562 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, | 555 EXPECT_EQ(content::ByteStreamOutput::STREAM_HAS_DATA, |
| 563 byte_stream_output->Read(&output_io_buffer, &output_length)); | 556 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 564 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 557 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 565 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 558 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 566 .WillRepeatedly(Return(true)); | 559 .WillRepeatedly(Return(true)); |
| 567 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 560 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 568 EXPECT_EQ(content::ByteStreamOutput::STREAM_EMPTY, | 561 EXPECT_EQ(content::ByteStreamOutput::STREAM_EMPTY, |
| 569 byte_stream_output->Read(&output_io_buffer, &output_length)); | 562 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 581 content::CreateByteStream( | 574 content::CreateByteStream( |
| 582 message_loop_.message_loop_proxy(), task_runner, | 575 message_loop_.message_loop_proxy(), task_runner, |
| 583 10000, &byte_stream_input, &byte_stream_output); | 576 10000, &byte_stream_input, &byte_stream_output); |
| 584 | 577 |
| 585 base::Closure intermediate_callback; | 578 base::Closure intermediate_callback; |
| 586 | 579 |
| 587 // Setup expectations and record initial state. | 580 // Setup expectations and record initial state. |
| 588 int num_callbacks = 0; | 581 int num_callbacks = 0; |
| 589 byte_stream_output->RegisterCallback( | 582 byte_stream_output->RegisterCallback( |
| 590 base::Bind(CountCallbacks, &num_callbacks)); | 583 base::Bind(CountCallbacks, &num_callbacks)); |
| 591 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, 0)) | 584 EXPECT_CALL(*task_runner.get(), PostDelayedTask(_, _, base::TimeDelta())) |
| 592 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), | 585 .WillOnce(DoAll(SaveArg<1>(&intermediate_callback), |
| 593 Return(true))); | 586 Return(true))); |
| 594 | 587 |
| 595 // Immediately close the stream. | 588 // Immediately close the stream. |
| 596 byte_stream_input->Close(content::DOWNLOAD_INTERRUPT_REASON_NONE); | 589 byte_stream_input->Close(content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 597 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); | 590 ::testing::Mock::VerifyAndClearExpectations(task_runner.get()); |
| 598 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) | 591 EXPECT_CALL(*task_runner.get(), RunsTasksOnCurrentThread()) |
| 599 .WillRepeatedly(Return(true)); | 592 .WillRepeatedly(Return(true)); |
| 600 intermediate_callback.Run(); | 593 intermediate_callback.Run(); |
| 601 EXPECT_EQ(1, num_callbacks); | 594 EXPECT_EQ(1, num_callbacks); |
| 602 } | 595 } |
| 603 | 596 |
| 604 | 597 |
| OLD | NEW |