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 <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 80 } |
81 | 81 |
82 void Initialize(const char* url, int first_position, int last_position) { | 82 void Initialize(const char* url, int first_position, int last_position) { |
83 gurl_ = GURL(url); | 83 gurl_ = GURL(url); |
84 first_position_ = first_position; | 84 first_position_ = first_position; |
85 last_position_ = last_position; | 85 last_position_ = last_position; |
86 | 86 |
87 loader_.reset(new BufferedResourceLoader( | 87 loader_.reset(new BufferedResourceLoader( |
88 gurl_, BufferedResourceLoader::kUnspecified, | 88 gurl_, BufferedResourceLoader::kUnspecified, |
89 first_position_, last_position_, | 89 first_position_, last_position_, |
90 BufferedResourceLoader::kThresholdDefer, 0, 0, | 90 BufferedResourceLoader::kCapacityDefer, 0, 0, |
91 new media::MediaLog())); | 91 new media::MediaLog())); |
92 | 92 |
93 // |test_loader_| will be used when Start() is called. | 93 // |test_loader_| will be used when Start() is called. |
94 url_loader_ = new NiceMock<MockWebURLLoader>(); | 94 url_loader_ = new NiceMock<MockWebURLLoader>(); |
95 loader_->test_loader_ = scoped_ptr<WebKit::WebURLLoader>(url_loader_); | 95 loader_->test_loader_ = scoped_ptr<WebKit::WebURLLoader>(url_loader_); |
96 } | 96 } |
97 | 97 |
98 void SetLoaderBuffer(int forward_capacity, int backward_capacity) { | 98 void SetLoaderBuffer(int forward_capacity, int backward_capacity) { |
99 loader_->buffer_.set_forward_capacity(forward_capacity); | 99 loader_->buffer_.set_forward_capacity(forward_capacity); |
100 loader_->buffer_.set_backward_capacity(backward_capacity); | 100 loader_->buffer_.set_backward_capacity(backward_capacity); |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 "%d-%d/%d", 1, 10, 1024))); | 390 "%d-%d/%d", 1, 10, 1024))); |
391 response.setExpectedContentLength(10); | 391 response.setExpectedContentLength(10); |
392 response.setHTTPStatusCode(kHttpPartialContent); | 392 response.setHTTPStatusCode(kHttpPartialContent); |
393 loader_->didReceiveResponse(url_loader_, response); | 393 loader_->didReceiveResponse(url_loader_, response); |
394 StopWhenLoad(); | 394 StopWhenLoad(); |
395 } | 395 } |
396 | 396 |
397 // Tests the logic of sliding window for data buffering and reading. | 397 // Tests the logic of sliding window for data buffering and reading. |
398 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { | 398 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { |
399 Initialize(kHttpUrl, 10, 29); | 399 Initialize(kHttpUrl, 10, 29); |
400 loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); | 400 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); |
401 Start(); | 401 Start(); |
402 PartialResponse(10, 29, 30); | 402 PartialResponse(10, 29, 30); |
403 | 403 |
404 uint8 buffer[10]; | 404 uint8 buffer[10]; |
405 InSequence s; | 405 InSequence s; |
406 | 406 |
407 // Writes 10 bytes and read them back. | 407 // Writes 10 bytes and read them back. |
408 WriteLoader(10, 10); | 408 WriteLoader(10, 10); |
409 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 409 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
410 ReadLoader(10, 10, buffer); | 410 ReadLoader(10, 10, buffer); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 // Fulfill requested bytes, then deferring should be enabled again. | 650 // Fulfill requested bytes, then deferring should be enabled again. |
651 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 651 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
652 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 652 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
653 WriteLoader(60, 40); | 653 WriteLoader(60, 40); |
654 | 654 |
655 VerifyBuffer(buffer, 80, 10); | 655 VerifyBuffer(buffer, 80, 10); |
656 | 656 |
657 StopWhenLoad(); | 657 StopWhenLoad(); |
658 } | 658 } |
659 | 659 |
660 // Tests the data buffering logic of ThresholdDefer strategy. | 660 // Tests the data buffering logic of kCapacityDefer strategy. |
661 TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) { | 661 TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) { |
662 Initialize(kHttpUrl, 10, 99); | 662 Initialize(kHttpUrl, 10, 99); |
663 SetLoaderBuffer(10, 20); | 663 SetLoaderBuffer(10, 20); |
664 Start(); | 664 Start(); |
665 PartialResponse(10, 99, 100); | 665 PartialResponse(10, 99, 100); |
666 | 666 |
667 uint8 buffer[10]; | 667 uint8 buffer[10]; |
668 InSequence s; | 668 InSequence s; |
669 | 669 |
670 // Write half of threshold: keep not deferring. | 670 // Write half of capacity: keep not deferring. |
671 WriteData(5); | 671 WriteData(5); |
672 | 672 |
673 // Write rest of space until threshold: start deferring. | 673 // Write rest of space until capacity: start deferring. |
674 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 674 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
675 WriteData(5); | 675 WriteData(5); |
676 | 676 |
677 // Read a little from the buffer: keep deferring. | 677 // Read a byte from the buffer: stop deferring. |
678 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); | 678 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); |
| 679 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
679 ReadLoader(10, 1, buffer); | 680 ReadLoader(10, 1, buffer); |
680 | 681 |
681 // Read a little more and go under threshold: stop deferring. | 682 // Write a byte to hit capacity: start deferring. |
682 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4)); | |
683 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | |
684 ReadLoader(12, 4, buffer); | |
685 | |
686 // Write rest of space until threshold: start deferring. | |
687 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 683 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
688 WriteData(6); | 684 WriteData(6); |
689 | 685 |
690 // Read a little from the buffer: keep deferring. | |
691 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); | |
692 ReadLoader(16, 1, buffer); | |
693 | |
694 StopWhenLoad(); | 686 StopWhenLoad(); |
695 } | 687 } |
696 | 688 |
697 TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) { | 689 TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) { |
698 Initialize(kHttpUrl, 10, 99); | 690 Initialize(kHttpUrl, 10, 99); |
699 SetLoaderBuffer(10, 10); | 691 SetLoaderBuffer(10, 10); |
700 Start(); | 692 Start(); |
701 PartialResponse(10, 99, 100); | 693 PartialResponse(10, 99, 100); |
702 | 694 |
703 uint8 buffer[256]; | 695 uint8 buffer[256]; |
704 InSequence s; | 696 InSequence s; |
705 | 697 |
706 // PRECONDITION | 698 // PRECONDITION |
707 WriteUntilThreshold(); | 699 WriteUntilThreshold(); |
708 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); | 700 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); |
| 701 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
709 ReadLoader(10, 1, buffer); | 702 ReadLoader(10, 1, buffer); |
710 ConfirmBufferState(1, 10, 9, 10); | 703 ConfirmBufferState(1, 10, 9, 10); |
711 ConfirmLoaderOffsets(11, 0, 0); | 704 ConfirmLoaderOffsets(11, 0, 0); |
712 | 705 |
713 // *** TRICKY BUSINESS, PT. I *** | 706 // *** TRICKY BUSINESS, PT. I *** |
714 // Read past buffered: stop deferring. | 707 // Read past buffered: stop deferring. |
715 // | 708 // |
716 // In order for the read to complete we must: | 709 // In order for the read to complete we must: |
717 // 1) Stop deferring to receive more data. | 710 // 1) Stop deferring to receive more data. |
718 // | 711 // |
719 // BEFORE | 712 // BEFORE |
720 // offset=11 [xxxxxxxxx_] | 713 // offset=11 [xxxxxxxxx_] |
721 // ^ ^^^ requested 4 bytes @ offset 20 | 714 // ^ ^^^ requested 4 bytes @ offset 20 |
722 // AFTER | 715 // AFTER |
723 // offset=24 [__________] | 716 // offset=24 [__________] |
724 // | 717 // |
725 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | |
726 ReadLoader(20, 4, buffer); | 718 ReadLoader(20, 4, buffer); |
727 | 719 |
728 // Write a little, make sure we didn't start deferring. | 720 // Write a little, make sure we didn't start deferring. |
729 WriteData(2); | 721 WriteData(2); |
730 | 722 |
731 // Write the rest, read should complete. | 723 // Write the rest, read should complete. |
732 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4)); | 724 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4)); |
733 WriteData(2); | 725 WriteData(2); |
734 | 726 |
735 // POSTCONDITION | 727 // POSTCONDITION |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 | 772 |
781 uint8 buffer[256]; | 773 uint8 buffer[256]; |
782 InSequence s; | 774 InSequence s; |
783 | 775 |
784 // PRECONDITION | 776 // PRECONDITION |
785 WriteUntilThreshold(); | 777 WriteUntilThreshold(); |
786 ConfirmBufferState(0, 10, 10, 10); | 778 ConfirmBufferState(0, 10, 10, 10); |
787 ConfirmLoaderOffsets(10, 0, 0); | 779 ConfirmLoaderOffsets(10, 0, 0); |
788 | 780 |
789 // *** TRICKY BUSINESS, PT. III *** | 781 // *** TRICKY BUSINESS, PT. III *** |
790 // Read past forward capacity but within threshold: stop deferring. | 782 // Read past forward capacity but within capacity: stop deferring. |
791 // | 783 // |
792 // In order for the read to complete we must: | 784 // In order for the read to complete we must: |
793 // 1) Adjust offset forward to create capacity. | 785 // 1) Adjust offset forward to create capacity. |
794 // 2) Stop deferring to receive more data. | 786 // 2) Stop deferring to receive more data. |
795 // | 787 // |
796 // BEFORE | 788 // BEFORE |
797 // offset=10 [xxxxxxxxxx] | 789 // offset=10 [xxxxxxxxxx] |
798 // ^^^^ requested 4 bytes @ offset 24 | 790 // ^^^^ requested 4 bytes @ offset 24 |
799 // ADJUSTED OFFSET | 791 // ADJUSTED OFFSET |
800 // offset=20 [__________] | 792 // offset=20 [__________] |
(...skipping 28 matching lines...) Expand all Loading... |
829 uint8 buffer[256]; | 821 uint8 buffer[256]; |
830 InSequence s; | 822 InSequence s; |
831 | 823 |
832 // PRECONDITION | 824 // PRECONDITION |
833 WriteUntilThreshold(); | 825 WriteUntilThreshold(); |
834 ConfirmBufferState(0, 10, 10, 10); | 826 ConfirmBufferState(0, 10, 10, 10); |
835 ConfirmLoaderOffsets(10, 0, 0); | 827 ConfirmLoaderOffsets(10, 0, 0); |
836 | 828 |
837 // *** TRICKY BUSINESS, PT. IV *** | 829 // *** TRICKY BUSINESS, PT. IV *** |
838 // Read a large amount past forward capacity but within | 830 // Read a large amount past forward capacity but within |
839 // threshold: stop deferring. | 831 // capacity: stop deferring. |
840 // | 832 // |
841 // In order for the read to complete we must: | 833 // In order for the read to complete we must: |
842 // 1) Adjust offset forward to create capacity. | 834 // 1) Adjust offset forward to create capacity. |
843 // 2) Expand capacity to make sure we don't defer as data arrives. | 835 // 2) Expand capacity to make sure we don't defer as data arrives. |
844 // 3) Stop deferring to receive more data. | 836 // 3) Stop deferring to receive more data. |
845 // | 837 // |
846 // BEFORE | 838 // BEFORE |
847 // offset=10 [xxxxxxxxxx] | 839 // offset=10 [xxxxxxxxxx] |
848 // ^^^^^^^^^^^^ requested 12 bytes @ offset 24 | 840 // ^^^^^^^^^^^^ requested 12 bytes @ offset 24 |
849 // ADJUSTED OFFSET | 841 // ADJUSTED OFFSET |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 ExpectContentRangeFailure("bytes 20-10/400"); | 1127 ExpectContentRangeFailure("bytes 20-10/400"); |
1136 | 1128 |
1137 ExpectContentRangeSuccess("bytes 0-499/500", 0, 499, 500); | 1129 ExpectContentRangeSuccess("bytes 0-499/500", 0, 499, 500); |
1138 ExpectContentRangeSuccess("bytes 0-0/500", 0, 0, 500); | 1130 ExpectContentRangeSuccess("bytes 0-0/500", 0, 0, 500); |
1139 ExpectContentRangeSuccess("bytes 10-11/50", 10, 11, 50); | 1131 ExpectContentRangeSuccess("bytes 10-11/50", 10, 11, 50); |
1140 ExpectContentRangeSuccess("bytes 10-11/*", 10, 11, | 1132 ExpectContentRangeSuccess("bytes 10-11/*", 10, 11, |
1141 kPositionNotSpecified); | 1133 kPositionNotSpecified); |
1142 } | 1134 } |
1143 | 1135 |
1144 } // namespace webkit_media | 1136 } // namespace webkit_media |
OLD | NEW |