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 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
| 599 // A more realistic BufferedDataSource that uses BufferedResourceLoader instead |
| 600 // of a mocked version but injects a MockWebURLLoader. |
| 601 // |
| 602 // TODO(scherkus): re-write these tests to use this class then drop the "2" |
| 603 // suffix. |
| 604 class MockBufferedDataSource2 : public BufferedDataSource { |
| 605 public: |
| 606 MockBufferedDataSource2(MessageLoop* message_loop, WebFrame* frame) |
| 607 : BufferedDataSource(message_loop, frame, new media::MediaLog()), |
| 608 url_loader_(NULL) { |
| 609 } |
| 610 |
| 611 virtual base::TimeDelta GetTimeoutMilliseconds() { |
| 612 return base::TimeDelta::FromMilliseconds( |
| 613 TestTimeouts::tiny_timeout_ms()); |
| 614 } |
| 615 |
| 616 virtual BufferedResourceLoader* CreateResourceLoader(int64 first_position, |
| 617 int64 last_position) { |
| 618 loader_ = BufferedDataSource::CreateResourceLoader(first_position, |
| 619 last_position); |
| 620 url_loader_ = new NiceMock<MockWebURLLoader>(); |
| 621 loader_->SetURLLoaderForTest(url_loader_); |
| 622 return loader_; |
| 623 } |
| 624 |
| 625 scoped_refptr<BufferedResourceLoader> loader() { return loader_; } |
| 626 NiceMock<MockWebURLLoader>* url_loader() { return url_loader_; } |
| 627 |
| 628 private: |
| 629 scoped_refptr<BufferedResourceLoader> loader_; |
| 630 NiceMock<MockWebURLLoader>* url_loader_; |
| 631 |
| 632 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource2); |
| 633 }; |
| 634 |
| 635 class BufferedDataSourceTest2 : public testing::Test { |
| 636 public: |
| 637 BufferedDataSourceTest2() |
| 638 : view_(WebView::create(NULL)), |
| 639 message_loop_(MessageLoop::current()) { |
| 640 view_->initializeMainFrame(&client_); |
| 641 } |
| 642 |
| 643 virtual ~BufferedDataSourceTest2() { |
| 644 view_->close(); |
| 645 } |
| 646 |
| 647 void InitializeDataSource(const char* url) { |
| 648 gurl_ = GURL(url); |
| 649 |
| 650 data_source_ = new MockBufferedDataSource2(message_loop_, |
| 651 view_->mainFrame()); |
| 652 data_source_->set_host(&host_); |
| 653 data_source_->Initialize(url, |
| 654 media::NewExpectedStatusCB(media::PIPELINE_OK)); |
| 655 message_loop_->RunAllPending(); |
| 656 |
| 657 // Simulate 206 response for a 5,000,000 byte length file. |
| 658 WebURLResponse response(gurl_); |
| 659 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), |
| 660 WebString::fromUTF8("bytes")); |
| 661 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), |
| 662 WebString::fromUTF8("bytes 0-4999999/5000000")); |
| 663 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), |
| 664 WebString::fromUTF8("5000000")); |
| 665 response.setExpectedContentLength(5000000); |
| 666 response.setHTTPStatusCode(206); |
| 667 |
| 668 // We should receive corresponding information about the media resource. |
| 669 EXPECT_CALL(host_, SetLoaded(false)); |
| 670 EXPECT_CALL(host_, SetTotalBytes(5000000)); |
| 671 EXPECT_CALL(host_, SetBufferedBytes(0)); |
| 672 |
| 673 data_source_->loader()->didReceiveResponse(data_source_->url_loader(), |
| 674 response); |
| 675 |
| 676 message_loop_->RunAllPending(); |
| 677 } |
| 678 |
| 679 void StopDataSource() { |
| 680 data_source_->Stop(media::NewExpectedClosure()); |
| 681 message_loop_->RunAllPending(); |
| 682 } |
| 683 |
| 684 MOCK_METHOD1(ReadCallback, void(size_t size)); |
| 685 media::DataSource::ReadCallback NewReadCallback(size_t size) { |
| 686 EXPECT_CALL(*this, ReadCallback(size)); |
| 687 return base::Bind(&BufferedDataSourceTest2::ReadCallback, |
| 688 base::Unretained(this)); |
| 689 } |
| 690 |
| 691 // Accessors for private variables on |data_source_|. |
| 692 media::Preload preload() { return data_source_->preload_; } |
| 693 BufferedResourceLoader::DeferStrategy defer_strategy() { |
| 694 return data_source_->loader()->defer_strategy_; |
| 695 } |
| 696 int data_source_bitrate() { return data_source_->bitrate_; } |
| 697 int data_source_playback_rate() { return data_source_->playback_rate_; } |
| 698 int loader_bitrate() { return data_source_->loader()->bitrate_; } |
| 699 int loader_playback_rate() { return data_source_->loader()->playback_rate_; } |
| 700 |
| 701 scoped_refptr<MockBufferedDataSource2> data_source_; |
| 702 |
| 703 GURL gurl_; |
| 704 MockWebFrameClient client_; |
| 705 WebView* view_; |
| 706 |
| 707 StrictMock<media::MockFilterHost> host_; |
| 708 MessageLoop* message_loop_; |
| 709 |
| 710 private: |
| 711 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest2); |
| 712 }; |
| 713 |
| 714 TEST_F(BufferedDataSourceTest2, Default) { |
| 715 InitializeDataSource("http://localhost/foo.webm"); |
| 716 |
| 717 // Ensure we have sane values for default loading scenario. |
| 718 EXPECT_EQ(media::AUTO, preload()); |
| 719 EXPECT_EQ(BufferedResourceLoader::kThresholdDefer, defer_strategy()); |
| 720 |
| 721 EXPECT_EQ(0, data_source_bitrate()); |
| 722 EXPECT_EQ(0.0f, data_source_playback_rate()); |
| 723 EXPECT_EQ(0, loader_bitrate()); |
| 724 EXPECT_EQ(0.0f, loader_playback_rate()); |
| 725 |
| 726 StopDataSource(); |
| 727 } |
| 728 |
| 729 TEST_F(BufferedDataSourceTest2, SetBitrate) { |
| 730 InitializeDataSource("http://localhost/foo.webm"); |
| 731 |
| 732 data_source_->SetBitrate(1234); |
| 733 message_loop_->RunAllPending(); |
| 734 EXPECT_EQ(1234, data_source_bitrate()); |
| 735 EXPECT_EQ(1234, loader_bitrate()); |
| 736 |
| 737 // Read so far ahead to cause the loader to get recreated. |
| 738 BufferedResourceLoader* old_loader = data_source_->loader(); |
| 739 |
| 740 uint8 buffer[1024]; |
| 741 data_source_->Read(4000000, 1024, buffer, |
| 742 NewReadCallback(media::DataSource::kReadError)); |
| 743 message_loop_->RunAllPending(); |
| 744 |
| 745 // Verify loader changed but still has same bitrate. |
| 746 EXPECT_NE(old_loader, data_source_->loader().get()); |
| 747 EXPECT_EQ(1234, loader_bitrate()); |
| 748 |
| 749 StopDataSource(); |
| 750 } |
| 751 |
| 752 TEST_F(BufferedDataSourceTest2, SetPlaybackRate) { |
| 753 InitializeDataSource("http://localhost/foo.webm"); |
| 754 |
| 755 data_source_->SetPlaybackRate(2.0f); |
| 756 message_loop_->RunAllPending(); |
| 757 EXPECT_EQ(2.0f, data_source_playback_rate()); |
| 758 EXPECT_EQ(2.0f, loader_playback_rate()); |
| 759 |
| 760 // Read so far ahead to cause the loader to get recreated. |
| 761 BufferedResourceLoader* old_loader = data_source_->loader(); |
| 762 |
| 763 uint8 buffer[1024]; |
| 764 data_source_->Read(4000000, 1024, buffer, |
| 765 NewReadCallback(media::DataSource::kReadError)); |
| 766 message_loop_->RunAllPending(); |
| 767 |
| 768 // Verify loader changed but still has same bitrate. |
| 769 EXPECT_NE(old_loader, data_source_->loader().get()); |
| 770 EXPECT_EQ(2.0f, loader_playback_rate()); |
| 771 |
| 772 StopDataSource(); |
| 773 } |
| 774 |
593 } // namespace webkit_glue | 775 } // namespace webkit_glue |
OLD | NEW |