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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 static const int64 kFileSize = 5000000; | 103 static const int64 kFileSize = 5000000; |
104 static const int64 kFarReadPosition = 4000000; | 104 static const int64 kFarReadPosition = 4000000; |
105 static const int kDataSize = 1024; | 105 static const int kDataSize = 1024; |
106 | 106 |
107 static const char kHttpUrl[] = "http://localhost/foo.webm"; | 107 static const char kHttpUrl[] = "http://localhost/foo.webm"; |
108 static const char kFileUrl[] = "file:///tmp/bar.webm"; | 108 static const char kFileUrl[] = "file:///tmp/bar.webm"; |
109 | 109 |
110 class BufferedDataSourceTest : public testing::Test { | 110 class BufferedDataSourceTest : public testing::Test { |
111 public: | 111 public: |
112 BufferedDataSourceTest() | 112 BufferedDataSourceTest() |
113 : view_(WebView::create(NULL)), frame_(WebLocalFrame::create(&client_)) { | 113 : view_(WebView::create(NULL)), |
| 114 frame_(WebLocalFrame::create(&client_)), |
| 115 preload_(AUTO) { |
114 view_->setMainFrame(frame_); | 116 view_->setMainFrame(frame_); |
115 } | 117 } |
116 | 118 |
117 virtual ~BufferedDataSourceTest() { | 119 virtual ~BufferedDataSourceTest() { |
118 view_->close(); | 120 view_->close(); |
119 frame_->close(); | 121 frame_->close(); |
120 } | 122 } |
121 | 123 |
122 MOCK_METHOD1(OnInitialize, void(bool)); | 124 MOCK_METHOD1(OnInitialize, void(bool)); |
123 | 125 |
124 void Initialize(const char* url, bool expected) { | 126 void Initialize(const char* url, bool expected) { |
125 GURL gurl(url); | 127 GURL gurl(url); |
126 data_source_.reset( | 128 data_source_.reset( |
127 new MockBufferedDataSource(gurl, | 129 new MockBufferedDataSource(gurl, |
128 message_loop_.message_loop_proxy(), | 130 message_loop_.message_loop_proxy(), |
129 view_->mainFrame()->toWebLocalFrame(), | 131 view_->mainFrame()->toWebLocalFrame(), |
130 &host_)); | 132 &host_)); |
| 133 data_source_->SetPreload(preload_); |
131 | 134 |
132 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 135 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
133 ExpectCreateResourceLoader(); | 136 ExpectCreateResourceLoader(); |
134 EXPECT_CALL(*this, OnInitialize(expected)); | 137 EXPECT_CALL(*this, OnInitialize(expected)); |
135 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize, | 138 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize, |
136 base::Unretained(this))); | 139 base::Unretained(this))); |
137 message_loop_.RunUntilIdle(); | 140 message_loop_.RunUntilIdle(); |
138 | 141 |
139 bool is_http = gurl.SchemeIsHTTPOrHTTPS(); | 142 bool is_http = gurl.SchemeIsHTTPOrHTTPS(); |
140 EXPECT_EQ(data_source_->downloading(), is_http); | 143 EXPECT_EQ(data_source_->downloading(), is_http); |
141 } | 144 } |
142 | 145 |
| 146 // Helper to initialize tests with a valid 200 response. |
| 147 void InitializeWith200Response() { |
| 148 Initialize(kHttpUrl, true); |
| 149 |
| 150 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 151 Respond(response_generator_->Generate200()); |
| 152 } |
| 153 |
143 // Helper to initialize tests with a valid 206 response. | 154 // Helper to initialize tests with a valid 206 response. |
144 void InitializeWith206Response() { | 155 void InitializeWith206Response() { |
145 Initialize(kHttpUrl, true); | 156 Initialize(kHttpUrl, true); |
146 | 157 |
147 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 158 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
148 Respond(response_generator_->Generate206(0)); | 159 Respond(response_generator_->Generate206(0)); |
149 } | 160 } |
150 | 161 |
151 // Helper to initialize tests with a valid file:// response. | 162 // Helper to initialize tests with a valid file:// response. |
152 void InitializeWithFileResponse() { | 163 void InitializeWithFileResponse() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 219 |
209 // Accessors for private variables on |data_source_|. | 220 // Accessors for private variables on |data_source_|. |
210 BufferedResourceLoader* loader() { | 221 BufferedResourceLoader* loader() { |
211 return data_source_->loader_.get(); | 222 return data_source_->loader_.get(); |
212 } | 223 } |
213 WebURLLoader* url_loader() { | 224 WebURLLoader* url_loader() { |
214 return loader()->active_loader_->loader_.get(); | 225 return loader()->active_loader_->loader_.get(); |
215 } | 226 } |
216 | 227 |
217 Preload preload() { return data_source_->preload_; } | 228 Preload preload() { return data_source_->preload_; } |
| 229 void set_preload(Preload preload) { preload_ = preload; } |
218 BufferedResourceLoader::DeferStrategy defer_strategy() { | 230 BufferedResourceLoader::DeferStrategy defer_strategy() { |
219 return loader()->defer_strategy_; | 231 return loader()->defer_strategy_; |
220 } | 232 } |
221 int data_source_bitrate() { return data_source_->bitrate_; } | 233 int data_source_bitrate() { return data_source_->bitrate_; } |
222 int data_source_playback_rate() { return data_source_->playback_rate_; } | 234 int data_source_playback_rate() { return data_source_->playback_rate_; } |
223 int loader_bitrate() { return loader()->bitrate_; } | 235 int loader_bitrate() { return loader()->bitrate_; } |
224 int loader_playback_rate() { return loader()->playback_rate_; } | 236 int loader_playback_rate() { return loader()->playback_rate_; } |
| 237 bool is_local_source() { return data_source_->assume_fully_buffered(); } |
| 238 void set_might_be_reused_from_cache_in_future(bool value) { |
| 239 loader()->might_be_reused_from_cache_in_future_ = value; |
| 240 } |
225 | 241 |
226 scoped_ptr<MockBufferedDataSource> data_source_; | 242 scoped_ptr<MockBufferedDataSource> data_source_; |
227 | 243 |
228 scoped_ptr<TestResponseGenerator> response_generator_; | 244 scoped_ptr<TestResponseGenerator> response_generator_; |
229 MockWebFrameClient client_; | 245 MockWebFrameClient client_; |
230 WebView* view_; | 246 WebView* view_; |
231 WebLocalFrame* frame_; | 247 WebLocalFrame* frame_; |
232 | 248 |
233 StrictMock<MockBufferedDataSourceHost> host_; | 249 StrictMock<MockBufferedDataSourceHost> host_; |
234 base::MessageLoop message_loop_; | 250 base::MessageLoop message_loop_; |
235 | 251 |
236 private: | 252 private: |
237 // Used for calling BufferedDataSource::Read(). | 253 // Used for calling BufferedDataSource::Read(). |
238 uint8 buffer_[kDataSize]; | 254 uint8 buffer_[kDataSize]; |
239 | 255 |
| 256 Preload preload_; |
| 257 |
240 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); | 258 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); |
241 }; | 259 }; |
242 | 260 |
243 TEST_F(BufferedDataSourceTest, Range_Supported) { | 261 TEST_F(BufferedDataSourceTest, Range_Supported) { |
244 Initialize(kHttpUrl, true); | 262 InitializeWith206Response(); |
245 | |
246 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | |
247 Respond(response_generator_->Generate206(0)); | |
248 | 263 |
249 EXPECT_TRUE(data_source_->loading()); | 264 EXPECT_TRUE(data_source_->loading()); |
250 EXPECT_FALSE(data_source_->IsStreaming()); | 265 EXPECT_FALSE(data_source_->IsStreaming()); |
251 Stop(); | 266 Stop(); |
252 } | 267 } |
253 | 268 |
254 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) { | 269 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) { |
255 Initialize(kHttpUrl, true); | 270 Initialize(kHttpUrl, true); |
256 | 271 |
257 Respond(response_generator_->Generate206( | 272 Respond(response_generator_->Generate206( |
258 0, TestResponseGenerator::kNoContentRangeInstanceSize)); | 273 0, TestResponseGenerator::kNoContentRangeInstanceSize)); |
259 | 274 |
260 EXPECT_TRUE(data_source_->loading()); | 275 EXPECT_TRUE(data_source_->loading()); |
261 EXPECT_TRUE(data_source_->IsStreaming()); | 276 EXPECT_TRUE(data_source_->IsStreaming()); |
262 Stop(); | 277 Stop(); |
263 } | 278 } |
264 | 279 |
265 TEST_F(BufferedDataSourceTest, Range_NotFound) { | 280 TEST_F(BufferedDataSourceTest, Range_NotFound) { |
266 Initialize(kHttpUrl, false); | 281 Initialize(kHttpUrl, false); |
267 Respond(response_generator_->Generate404()); | 282 Respond(response_generator_->Generate404()); |
268 | 283 |
269 EXPECT_FALSE(data_source_->loading()); | 284 EXPECT_FALSE(data_source_->loading()); |
270 Stop(); | 285 Stop(); |
271 } | 286 } |
272 | 287 |
273 TEST_F(BufferedDataSourceTest, Range_NotSupported) { | 288 TEST_F(BufferedDataSourceTest, Range_NotSupported) { |
274 Initialize(kHttpUrl, true); | 289 InitializeWith200Response(); |
275 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | |
276 Respond(response_generator_->Generate200()); | |
277 | 290 |
278 EXPECT_TRUE(data_source_->loading()); | 291 EXPECT_TRUE(data_source_->loading()); |
279 EXPECT_TRUE(data_source_->IsStreaming()); | 292 EXPECT_TRUE(data_source_->IsStreaming()); |
280 Stop(); | 293 Stop(); |
281 } | 294 } |
282 | 295 |
283 // Special carve-out for Apache versions that choose to return a 200 for | 296 // Special carve-out for Apache versions that choose to return a 200 for |
284 // Range:0- ("because it's more efficient" than a 206) | 297 // Range:0- ("because it's more efficient" than a 206) |
285 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) { | 298 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) { |
286 Initialize(kHttpUrl, true); | 299 Initialize(kHttpUrl, true); |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 TEST_F(BufferedDataSourceTest, File_FinishLoading) { | 678 TEST_F(BufferedDataSourceTest, File_FinishLoading) { |
666 InitializeWithFileResponse(); | 679 InitializeWithFileResponse(); |
667 | 680 |
668 EXPECT_FALSE(data_source_->downloading()); | 681 EXPECT_FALSE(data_source_->downloading()); |
669 FinishLoading(); | 682 FinishLoading(); |
670 EXPECT_FALSE(data_source_->downloading()); | 683 EXPECT_FALSE(data_source_->downloading()); |
671 | 684 |
672 Stop(); | 685 Stop(); |
673 } | 686 } |
674 | 687 |
| 688 TEST_F(BufferedDataSourceTest, LocalResource_DeferStrategy) { |
| 689 InitializeWithFileResponse(); |
| 690 |
| 691 EXPECT_EQ(AUTO, preload()); |
| 692 EXPECT_TRUE(is_local_source()); |
| 693 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 694 |
| 695 data_source_->MediaIsPlaying(); |
| 696 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 697 |
| 698 data_source_->MediaIsPaused(); |
| 699 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 700 |
| 701 Stop(); |
| 702 } |
| 703 |
| 704 TEST_F(BufferedDataSourceTest, LocalResource_PreloadMetadata_DeferStrategy) { |
| 705 set_preload(METADATA); |
| 706 InitializeWithFileResponse(); |
| 707 |
| 708 EXPECT_EQ(METADATA, preload()); |
| 709 EXPECT_TRUE(is_local_source()); |
| 710 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy()); |
| 711 |
| 712 data_source_->MediaIsPlaying(); |
| 713 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 714 |
| 715 data_source_->MediaIsPaused(); |
| 716 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 717 |
| 718 Stop(); |
| 719 } |
| 720 |
| 721 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse200_DeferStrategy) { |
| 722 InitializeWith200Response(); |
| 723 |
| 724 EXPECT_EQ(AUTO, preload()); |
| 725 EXPECT_FALSE(is_local_source()); |
| 726 EXPECT_FALSE(loader()->range_supported()); |
| 727 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 728 |
| 729 data_source_->MediaIsPlaying(); |
| 730 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 731 |
| 732 data_source_->MediaIsPaused(); |
| 733 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 734 |
| 735 Stop(); |
| 736 } |
| 737 |
| 738 TEST_F(BufferedDataSourceTest, |
| 739 ExternalResource_Response200_PreloadMetadata_DeferStrategy) { |
| 740 set_preload(METADATA); |
| 741 InitializeWith200Response(); |
| 742 |
| 743 EXPECT_EQ(METADATA, preload()); |
| 744 EXPECT_FALSE(is_local_source()); |
| 745 EXPECT_FALSE(loader()->range_supported()); |
| 746 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy()); |
| 747 |
| 748 data_source_->MediaIsPlaying(); |
| 749 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 750 |
| 751 data_source_->MediaIsPaused(); |
| 752 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 753 |
| 754 Stop(); |
| 755 } |
| 756 |
| 757 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse206_DeferStrategy) { |
| 758 InitializeWith206Response(); |
| 759 |
| 760 EXPECT_EQ(AUTO, preload()); |
| 761 EXPECT_FALSE(is_local_source()); |
| 762 EXPECT_TRUE(loader()->range_supported()); |
| 763 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 764 |
| 765 data_source_->MediaIsPlaying(); |
| 766 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 767 set_might_be_reused_from_cache_in_future(true); |
| 768 data_source_->MediaIsPaused(); |
| 769 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy()); |
| 770 |
| 771 data_source_->MediaIsPlaying(); |
| 772 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 773 set_might_be_reused_from_cache_in_future(false); |
| 774 data_source_->MediaIsPaused(); |
| 775 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 776 |
| 777 Stop(); |
| 778 } |
| 779 |
| 780 TEST_F(BufferedDataSourceTest, |
| 781 ExternalResource_Response206_PreloadMetadata_DeferStrategy) { |
| 782 set_preload(METADATA); |
| 783 InitializeWith206Response(); |
| 784 |
| 785 EXPECT_EQ(METADATA, preload()); |
| 786 EXPECT_FALSE(is_local_source()); |
| 787 EXPECT_TRUE(loader()->range_supported()); |
| 788 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy()); |
| 789 |
| 790 data_source_->MediaIsPlaying(); |
| 791 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 792 set_might_be_reused_from_cache_in_future(true); |
| 793 data_source_->MediaIsPaused(); |
| 794 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy()); |
| 795 |
| 796 data_source_->MediaIsPlaying(); |
| 797 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 798 set_might_be_reused_from_cache_in_future(false); |
| 799 data_source_->MediaIsPaused(); |
| 800 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
| 801 |
| 802 Stop(); |
| 803 } |
| 804 |
675 } // namespace content | 805 } // namespace content |
OLD | NEW |