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