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