Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
| 9 #include "media/base/media_log.h" | 9 #include "media/base/media_log.h" |
| 10 #include "media/base/mock_callback.h" | 10 #include "media/base/mock_callback.h" |
| 11 #include "media/base/mock_filter_host.h" | 11 #include "media/base/mock_filter_host.h" |
| 12 #include "media/base/mock_filters.h" | 12 #include "media/base/mock_filters.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 #include "webkit/glue/media/buffered_data_source.h" | 17 #include "webkit/glue/media/buffered_data_source.h" |
| 16 #include "webkit/mocks/mock_webframeclient.h" | 18 #include "webkit/mocks/mock_webframeclient.h" |
| 19 #include "webkit/mocks/mock_weburlloader.h" | |
| 17 | 20 |
| 18 using ::testing::_; | 21 using ::testing::_; |
| 19 using ::testing::Assign; | 22 using ::testing::Assign; |
| 20 using ::testing::AtLeast; | 23 using ::testing::AtLeast; |
| 21 using ::testing::DeleteArg; | 24 using ::testing::DeleteArg; |
| 22 using ::testing::DoAll; | 25 using ::testing::DoAll; |
| 23 using ::testing::InSequence; | 26 using ::testing::InSequence; |
| 24 using ::testing::Invoke; | 27 using ::testing::Invoke; |
| 25 using ::testing::InvokeWithoutArgs; | 28 using ::testing::InvokeWithoutArgs; |
| 26 using ::testing::NotNull; | 29 using ::testing::NotNull; |
| 27 using ::testing::Return; | 30 using ::testing::Return; |
| 28 using ::testing::ReturnRef; | 31 using ::testing::ReturnRef; |
| 29 using ::testing::SetArgumentPointee; | 32 using ::testing::SetArgumentPointee; |
| 30 using ::testing::StrictMock; | 33 using ::testing::StrictMock; |
| 31 using ::testing::NiceMock; | 34 using ::testing::NiceMock; |
| 32 using ::testing::WithArgs; | 35 using ::testing::WithArgs; |
| 33 | 36 |
| 34 using WebKit::WebFrame; | 37 using WebKit::WebFrame; |
| 38 using WebKit::WebString; | |
| 39 using WebKit::WebURLResponse; | |
| 35 using WebKit::WebView; | 40 using WebKit::WebView; |
| 36 | 41 |
| 37 namespace webkit_glue { | 42 namespace webkit_glue { |
| 38 | 43 |
| 39 static const char* kHttpUrl = "http://test"; | 44 static const char* kHttpUrl = "http://test"; |
| 40 static const char* kFileUrl = "file://test"; | 45 static const char* kFileUrl = "file://test"; |
| 41 static const int kDataSize = 1024; | 46 static const int kDataSize = 1024; |
| 42 static const int kMaxCacheMissesBeforeFailTest = 20; | 47 static const int kMaxCacheMissesBeforeFailTest = 20; |
| 43 | 48 |
| 44 enum NetworkState { | 49 enum NetworkState { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 64 BufferedResourceLoader*(int64 first_position, | 69 BufferedResourceLoader*(int64 first_position, |
| 65 int64 last_position)); | 70 int64 last_position)); |
| 66 | 71 |
| 67 private: | 72 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); | 73 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); |
| 69 }; | 74 }; |
| 70 | 75 |
| 71 class MockBufferedResourceLoader : public BufferedResourceLoader { | 76 class MockBufferedResourceLoader : public BufferedResourceLoader { |
| 72 public: | 77 public: |
| 73 MockBufferedResourceLoader() | 78 MockBufferedResourceLoader() |
| 74 : BufferedResourceLoader(GURL(), 0, 0, new media::MediaLog()) { | 79 : BufferedResourceLoader(GURL(), 0, 0, kThresholdDefer, |
| 80 0, 0, new media::MediaLog()) { | |
| 75 } | 81 } |
| 76 | 82 |
| 77 MOCK_METHOD3(Start, void(net::OldCompletionCallback* read_callback, | 83 MOCK_METHOD3(Start, void(net::OldCompletionCallback* read_callback, |
| 78 const base::Closure& network_callback, | 84 const base::Closure& network_callback, |
| 79 WebFrame* frame)); | 85 WebFrame* frame)); |
| 80 MOCK_METHOD0(Stop, void()); | 86 MOCK_METHOD0(Stop, void()); |
| 81 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, | 87 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, |
| 82 net::OldCompletionCallback* callback)); | 88 net::OldCompletionCallback* callback)); |
| 83 MOCK_METHOD0(content_length, int64()); | 89 MOCK_METHOD0(content_length, int64()); |
| 84 MOCK_METHOD0(instance_size, int64()); | 90 MOCK_METHOD0(instance_size, int64()); |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 | 588 |
| 583 // Test that we only allow a limited number of cache misses for a | 589 // Test that we only allow a limited number of cache misses for a |
| 584 // single Read() request. | 590 // single Read() request. |
| 585 TEST_F(BufferedDataSourceTest, BoundedCacheMisses) { | 591 TEST_F(BufferedDataSourceTest, BoundedCacheMisses) { |
| 586 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); | 592 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); |
| 587 | 593 |
| 588 ReadDataSourceAlwaysCacheMiss(0, 10); | 594 ReadDataSourceAlwaysCacheMiss(0, 10); |
| 589 | 595 |
| 590 StopDataSource(); | 596 StopDataSource(); |
| 591 } | 597 } |
| 592 | 598 |
|
vrk (LEFT CHROMIUM)
2011/10/11 20:44:40
nit: remove line
scherkus (not reviewing)
2011/10/11 22:28:58
Done.
| |
| 599 | |
| 600 // A more realistic BufferedDataSource that uses BufferedResourceLoader instead | |
| 601 // of a mocked version but injects a MockWebURLLoader. | |
| 602 // | |
| 603 // TODO(scherkus): re-write these tests to use this class then drop the "2" | |
| 604 // suffix. | |
| 605 class MockBufferedDataSource2 : public BufferedDataSource { | |
| 606 public: | |
| 607 MockBufferedDataSource2(MessageLoop* message_loop, WebFrame* frame) | |
| 608 : BufferedDataSource(message_loop, frame, new media::MediaLog()), | |
| 609 url_loader_(NULL) { | |
| 610 } | |
| 611 | |
| 612 virtual base::TimeDelta GetTimeoutMilliseconds() { | |
| 613 return base::TimeDelta::FromMilliseconds( | |
| 614 TestTimeouts::tiny_timeout_ms()); | |
| 615 } | |
| 616 | |
| 617 virtual BufferedResourceLoader* CreateResourceLoader(int64 first_position, | |
| 618 int64 last_position) { | |
| 619 loader_ = BufferedDataSource::CreateResourceLoader(first_position, | |
| 620 last_position); | |
| 621 url_loader_ = new NiceMock<MockWebURLLoader>(); | |
| 622 loader_->SetURLLoaderForTest(url_loader_); | |
| 623 return loader_; | |
| 624 } | |
| 625 | |
| 626 scoped_refptr<BufferedResourceLoader> loader() { return loader_; } | |
| 627 NiceMock<MockWebURLLoader>* url_loader() { return url_loader_; } | |
| 628 | |
| 629 private: | |
| 630 scoped_refptr<BufferedResourceLoader> loader_; | |
| 631 NiceMock<MockWebURLLoader>* url_loader_; | |
| 632 | |
| 633 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource2); | |
| 634 }; | |
| 635 | |
| 636 class BufferedDataSourceTest2 : public testing::Test { | |
| 637 public: | |
| 638 BufferedDataSourceTest2() | |
| 639 : view_(WebView::create(NULL)), | |
| 640 message_loop_(MessageLoop::current()) { | |
| 641 view_->initializeMainFrame(&client_); | |
| 642 } | |
| 643 | |
| 644 virtual ~BufferedDataSourceTest2() { | |
| 645 view_->close(); | |
| 646 } | |
| 647 | |
| 648 void InitializeDataSource(const char* url) { | |
| 649 gurl_ = GURL(url); | |
| 650 | |
| 651 data_source_ = new MockBufferedDataSource2(message_loop_, | |
| 652 view_->mainFrame()); | |
| 653 data_source_->set_host(&host_); | |
| 654 data_source_->Initialize(url, | |
| 655 media::NewExpectedStatusCB(media::PIPELINE_OK)); | |
| 656 message_loop_->RunAllPending(); | |
| 657 | |
| 658 // Simulate 206 response for a 5,000,000 byte length file. | |
| 659 WebURLResponse response(gurl_); | |
| 660 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), | |
| 661 WebString::fromUTF8("bytes")); | |
| 662 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), | |
| 663 WebString::fromUTF8("bytes 0-4999999/5000000")); | |
| 664 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), | |
| 665 WebString::fromUTF8("5000000")); | |
| 666 response.setExpectedContentLength(5000000); | |
| 667 response.setHTTPStatusCode(206); | |
| 668 | |
| 669 // We should receive corresponding information about the media resource. | |
| 670 EXPECT_CALL(host_, SetLoaded(false)); | |
| 671 EXPECT_CALL(host_, SetTotalBytes(5000000)); | |
| 672 EXPECT_CALL(host_, SetBufferedBytes(0)); | |
| 673 | |
| 674 data_source_->loader()->didReceiveResponse(data_source_->url_loader(), | |
| 675 response); | |
| 676 | |
| 677 message_loop_->RunAllPending(); | |
| 678 } | |
| 679 | |
| 680 void StopDataSource() { | |
| 681 data_source_->Stop(media::NewExpectedClosure()); | |
| 682 message_loop_->RunAllPending(); | |
| 683 } | |
| 684 | |
| 685 MOCK_METHOD1(ReadCallback, void(size_t size)); | |
| 686 media::DataSource::ReadCallback NewReadCallback(size_t size) { | |
| 687 EXPECT_CALL(*this, ReadCallback(size)); | |
| 688 return base::Bind(&BufferedDataSourceTest2::ReadCallback, | |
| 689 base::Unretained(this)); | |
| 690 } | |
| 691 | |
| 692 // Accessors for private variables. | |
|
vrk (LEFT CHROMIUM)
2011/10/11 20:44:40
nit: "for |data_source_|'s private variables"
scherkus (not reviewing)
2011/10/11 22:28:58
Done.
| |
| 693 media::Preload preload() { return data_source_->preload_; } | |
| 694 BufferedResourceLoader::DeferStrategy defer_strategy() { | |
| 695 return data_source_->loader()->defer_strategy_; | |
| 696 } | |
| 697 int data_source_bitrate() { return data_source_->bitrate_; } | |
| 698 int data_source_playback_rate() { return data_source_->playback_rate_; } | |
| 699 int loader_bitrate() { return data_source_->loader()->bitrate_; } | |
| 700 int loader_playback_rate() { return data_source_->loader()->playback_rate_; } | |
| 701 | |
| 702 scoped_refptr<MockBufferedDataSource2> data_source_; | |
| 703 | |
| 704 GURL gurl_; | |
| 705 MockWebFrameClient client_; | |
| 706 WebView* view_; | |
| 707 | |
| 708 StrictMock<media::MockFilterHost> host_; | |
| 709 MessageLoop* message_loop_; | |
|
vrk (LEFT CHROMIUM)
2011/10/11 20:44:40
nit: add private:
DISALLOW_COPY_AND_ASSIGN(Buffere
scherkus (not reviewing)
2011/10/11 22:28:58
Done.
| |
| 710 }; | |
| 711 | |
| 712 TEST_F(BufferedDataSourceTest2, Default) { | |
| 713 InitializeDataSource("http://localhost/foo.webm"); | |
| 714 | |
| 715 // Ensure we have sane values for default loading scenario. | |
| 716 EXPECT_EQ(media::AUTO, preload()); | |
| 717 EXPECT_EQ(BufferedResourceLoader::kThresholdDefer, defer_strategy()); | |
| 718 | |
| 719 EXPECT_EQ(0, data_source_bitrate()); | |
| 720 EXPECT_EQ(0.0f, data_source_playback_rate()); | |
| 721 EXPECT_EQ(0, loader_bitrate()); | |
| 722 EXPECT_EQ(0.0f, loader_playback_rate()); | |
| 723 | |
| 724 StopDataSource(); | |
| 725 } | |
| 726 | |
| 727 TEST_F(BufferedDataSourceTest2, SetBitrate) { | |
| 728 InitializeDataSource("http://localhost/foo.webm"); | |
| 729 | |
| 730 data_source_->SetBitrate(1234); | |
| 731 message_loop_->RunAllPending(); | |
| 732 EXPECT_EQ(1234, data_source_bitrate()); | |
| 733 EXPECT_EQ(1234, loader_bitrate()); | |
| 734 | |
| 735 // Read so far ahead to cause the loader to get recreated. | |
| 736 BufferedResourceLoader* old_loader = data_source_->loader(); | |
| 737 | |
| 738 uint8 buffer[1024]; | |
| 739 data_source_->Read(4000000, 1024, buffer, | |
| 740 NewReadCallback(media::DataSource::kReadError)); | |
| 741 message_loop_->RunAllPending(); | |
| 742 | |
| 743 // Verify loader changed but still has same bitrate. | |
| 744 EXPECT_NE(old_loader, data_source_->loader().get()); | |
| 745 EXPECT_EQ(1234, loader_bitrate()); | |
| 746 | |
| 747 StopDataSource(); | |
| 748 } | |
| 749 | |
| 750 TEST_F(BufferedDataSourceTest2, SetPlaybackRate) { | |
| 751 InitializeDataSource("http://localhost/foo.webm"); | |
| 752 | |
| 753 data_source_->SetPlaybackRate(2.0f); | |
| 754 message_loop_->RunAllPending(); | |
| 755 EXPECT_EQ(2.0f, data_source_playback_rate()); | |
| 756 EXPECT_EQ(2.0f, loader_playback_rate()); | |
| 757 | |
| 758 // Read so far ahead to cause the loader to get recreated. | |
| 759 BufferedResourceLoader* old_loader = data_source_->loader(); | |
| 760 | |
| 761 uint8 buffer[1024]; | |
| 762 data_source_->Read(4000000, 1024, buffer, | |
| 763 NewReadCallback(media::DataSource::kReadError)); | |
| 764 message_loop_->RunAllPending(); | |
| 765 | |
| 766 // Verify loader changed but still has same bitrate. | |
| 767 EXPECT_NE(old_loader, data_source_->loader().get()); | |
| 768 EXPECT_EQ(2.0f, loader_playback_rate()); | |
| 769 | |
| 770 StopDataSource(); | |
| 771 } | |
| 772 | |
| 593 } // namespace webkit_glue | 773 } // namespace webkit_glue |
| OLD | NEW |