| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "content/public/common/url_constants.h" | 7 #include "content/public/common/url_constants.h" |
| 8 #include "content/renderer/media/buffered_data_source.h" | 8 #include "content/renderer/media/buffered_data_source.h" |
| 9 #include "content/renderer/media/test_response_generator.h" | 9 #include "content/renderer/media/test_response_generator.h" |
| 10 #include "content/test/mock_webframeclient.h" | 10 #include "content/test/mock_webframeclient.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost); | 43 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost); |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 // Overrides CreateResourceLoader() to permit injecting a MockWebURLLoader. | 46 // Overrides CreateResourceLoader() to permit injecting a MockWebURLLoader. |
| 47 // Also keeps track of whether said MockWebURLLoader is actively loading. | 47 // Also keeps track of whether said MockWebURLLoader is actively loading. |
| 48 class MockBufferedDataSource : public BufferedDataSource { | 48 class MockBufferedDataSource : public BufferedDataSource { |
| 49 public: | 49 public: |
| 50 MockBufferedDataSource( | 50 MockBufferedDataSource( |
| 51 const GURL& url, |
| 51 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 52 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 52 WebLocalFrame* frame, | 53 WebLocalFrame* frame, |
| 53 BufferedDataSourceHost* host) | 54 BufferedDataSourceHost* host) |
| 54 : BufferedDataSource(message_loop, frame, new media::MediaLog(), host, | 55 : BufferedDataSource(url, |
| 56 BufferedResourceLoader::kUnspecified, |
| 57 message_loop, |
| 58 frame, |
| 59 new media::MediaLog(), |
| 60 host, |
| 55 base::Bind(&MockBufferedDataSource::set_downloading, | 61 base::Bind(&MockBufferedDataSource::set_downloading, |
| 56 base::Unretained(this))), | 62 base::Unretained(this))), |
| 57 downloading_(false), | 63 downloading_(false), |
| 58 loading_(false) { | 64 loading_(false) {} |
| 59 } | |
| 60 virtual ~MockBufferedDataSource() {} | 65 virtual ~MockBufferedDataSource() {} |
| 61 | 66 |
| 62 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(int64, int64)); | 67 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(int64, int64)); |
| 63 BufferedResourceLoader* CreateMockResourceLoader(int64 first_byte_position, | 68 BufferedResourceLoader* CreateMockResourceLoader(int64 first_byte_position, |
| 64 int64 last_byte_position) { | 69 int64 last_byte_position) { |
| 65 CHECK(!loading_) << "Previous resource load wasn't cancelled"; | 70 CHECK(!loading_) << "Previous resource load wasn't cancelled"; |
| 66 | 71 |
| 67 BufferedResourceLoader* loader = | 72 BufferedResourceLoader* loader = |
| 68 BufferedDataSource::CreateResourceLoader(first_byte_position, | 73 BufferedDataSource::CreateResourceLoader(first_byte_position, |
| 69 last_byte_position); | 74 last_byte_position); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 100 static const int kDataSize = 1024; | 105 static const int kDataSize = 1024; |
| 101 | 106 |
| 102 static const char kHttpUrl[] = "http://localhost/foo.webm"; | 107 static const char kHttpUrl[] = "http://localhost/foo.webm"; |
| 103 static const char kFileUrl[] = "file:///tmp/bar.webm"; | 108 static const char kFileUrl[] = "file:///tmp/bar.webm"; |
| 104 | 109 |
| 105 class BufferedDataSourceTest : public testing::Test { | 110 class BufferedDataSourceTest : public testing::Test { |
| 106 public: | 111 public: |
| 107 BufferedDataSourceTest() | 112 BufferedDataSourceTest() |
| 108 : view_(WebView::create(NULL)), frame_(WebLocalFrame::create(&client_)) { | 113 : view_(WebView::create(NULL)), frame_(WebLocalFrame::create(&client_)) { |
| 109 view_->setMainFrame(frame_); | 114 view_->setMainFrame(frame_); |
| 110 | |
| 111 data_source_.reset( | |
| 112 new MockBufferedDataSource(message_loop_.message_loop_proxy(), | |
| 113 view_->mainFrame()->toWebLocalFrame(), | |
| 114 &host_)); | |
| 115 } | 115 } |
| 116 | 116 |
| 117 virtual ~BufferedDataSourceTest() { | 117 virtual ~BufferedDataSourceTest() { |
| 118 view_->close(); | 118 view_->close(); |
| 119 frame_->close(); | 119 frame_->close(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 MOCK_METHOD1(OnInitialize, void(bool)); | 122 MOCK_METHOD1(OnInitialize, void(bool)); |
| 123 | 123 |
| 124 void Initialize(const char* url, bool expected) { | 124 void Initialize(const char* url, bool expected) { |
| 125 GURL gurl(url); | 125 GURL gurl(url); |
| 126 data_source_.reset( |
| 127 new MockBufferedDataSource(gurl, |
| 128 message_loop_.message_loop_proxy(), |
| 129 view_->mainFrame()->toWebLocalFrame(), |
| 130 &host_)); |
| 131 |
| 126 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 132 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
| 127 | |
| 128 ExpectCreateResourceLoader(); | 133 ExpectCreateResourceLoader(); |
| 129 EXPECT_CALL(*this, OnInitialize(expected)); | 134 EXPECT_CALL(*this, OnInitialize(expected)); |
| 130 data_source_->Initialize( | 135 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize, |
| 131 gurl, BufferedResourceLoader::kUnspecified, base::Bind( | 136 base::Unretained(this))); |
| 132 &BufferedDataSourceTest::OnInitialize, base::Unretained(this))); | |
| 133 message_loop_.RunUntilIdle(); | 137 message_loop_.RunUntilIdle(); |
| 134 | 138 |
| 135 bool is_http = | 139 bool is_http = gurl.SchemeIsHTTPOrHTTPS(); |
| 136 gurl.SchemeIs(url::kHttpScheme) || gurl.SchemeIs(url::kHttpsScheme); | |
| 137 EXPECT_EQ(data_source_->downloading(), is_http); | 140 EXPECT_EQ(data_source_->downloading(), is_http); |
| 138 } | 141 } |
| 139 | 142 |
| 140 // Helper to initialize tests with a valid 206 response. | 143 // Helper to initialize tests with a valid 206 response. |
| 141 void InitializeWith206Response() { | 144 void InitializeWith206Response() { |
| 142 Initialize(kHttpUrl, true); | 145 Initialize(kHttpUrl, true); |
| 143 | 146 |
| 144 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 147 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 145 Respond(response_generator_->Generate206(0)); | 148 Respond(response_generator_->Generate206(0)); |
| 146 } | 149 } |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 InitializeWithFileResponse(); | 666 InitializeWithFileResponse(); |
| 664 | 667 |
| 665 EXPECT_FALSE(data_source_->downloading()); | 668 EXPECT_FALSE(data_source_->downloading()); |
| 666 FinishLoading(); | 669 FinishLoading(); |
| 667 EXPECT_FALSE(data_source_->downloading()); | 670 EXPECT_FALSE(data_source_->downloading()); |
| 668 | 671 |
| 669 Stop(); | 672 Stop(); |
| 670 } | 673 } |
| 671 | 674 |
| 672 } // namespace content | 675 } // namespace content |
| OLD | NEW |