OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "content/public/common/url_constants.h" | |
8 #include "content/renderer/media/buffered_data_source.h" | |
9 #include "content/renderer/media/test_response_generator.h" | |
10 #include "content/test/mock_webframeclient.h" | |
11 #include "content/test/mock_weburlloader.h" | |
12 #include "media/base/media_log.h" | |
13 #include "media/base/mock_filters.h" | |
14 #include "media/base/test_helpers.h" | |
15 #include "third_party/WebKit/public/platform/WebURLResponse.h" | |
16 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
17 #include "third_party/WebKit/public/web/WebView.h" | |
18 | |
19 using ::testing::_; | |
20 using ::testing::Assign; | |
21 using ::testing::Invoke; | |
22 using ::testing::InSequence; | |
23 using ::testing::NiceMock; | |
24 using ::testing::StrictMock; | |
25 | |
26 using blink::WebLocalFrame; | |
27 using blink::WebString; | |
28 using blink::WebURLLoader; | |
29 using blink::WebURLResponse; | |
30 using blink::WebView; | |
31 | |
32 namespace content { | |
33 | |
34 class MockBufferedDataSourceHost : public BufferedDataSourceHost { | |
35 public: | |
36 MockBufferedDataSourceHost() {} | |
37 virtual ~MockBufferedDataSourceHost() {} | |
38 | |
39 MOCK_METHOD1(SetTotalBytes, void(int64 total_bytes)); | |
40 MOCK_METHOD2(AddBufferedByteRange, void(int64 start, int64 end)); | |
41 | |
42 private: | |
43 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost); | |
44 }; | |
45 | |
46 // Overrides CreateResourceLoader() to permit injecting a MockWebURLLoader. | |
47 // Also keeps track of whether said MockWebURLLoader is actively loading. | |
48 class MockBufferedDataSource : public BufferedDataSource { | |
49 public: | |
50 MockBufferedDataSource( | |
51 const GURL& url, | |
52 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
53 WebLocalFrame* frame, | |
54 BufferedDataSourceHost* host) | |
55 : BufferedDataSource(url, | |
56 BufferedResourceLoader::kUnspecified, | |
57 message_loop, | |
58 frame, | |
59 new media::MediaLog(), | |
60 host, | |
61 base::Bind(&MockBufferedDataSource::set_downloading, | |
62 base::Unretained(this))), | |
63 downloading_(false), | |
64 loading_(false) {} | |
65 virtual ~MockBufferedDataSource() {} | |
66 | |
67 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(int64, int64)); | |
68 BufferedResourceLoader* CreateMockResourceLoader(int64 first_byte_position, | |
69 int64 last_byte_position) { | |
70 CHECK(!loading_) << "Previous resource load wasn't cancelled"; | |
71 | |
72 BufferedResourceLoader* loader = | |
73 BufferedDataSource::CreateResourceLoader(first_byte_position, | |
74 last_byte_position); | |
75 | |
76 // Keep track of active loading state via loadAsynchronously() and cancel(). | |
77 NiceMock<MockWebURLLoader>* url_loader = new NiceMock<MockWebURLLoader>(); | |
78 ON_CALL(*url_loader, loadAsynchronously(_, _)) | |
79 .WillByDefault(Assign(&loading_, true)); | |
80 ON_CALL(*url_loader, cancel()) | |
81 .WillByDefault(Assign(&loading_, false)); | |
82 | |
83 // |test_loader_| will be used when Start() is called. | |
84 loader->test_loader_ = scoped_ptr<WebURLLoader>(url_loader); | |
85 return loader; | |
86 } | |
87 | |
88 bool loading() { return loading_; } | |
89 void set_loading(bool loading) { loading_ = loading; } | |
90 bool downloading() { return downloading_; } | |
91 void set_downloading(bool downloading) { downloading_ = downloading; } | |
92 | |
93 private: | |
94 // Whether the resource is downloading or deferred. | |
95 bool downloading_; | |
96 | |
97 // Whether the resource load has starting loading but yet to been cancelled. | |
98 bool loading_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); | |
101 }; | |
102 | |
103 static const int64 kFileSize = 5000000; | |
104 static const int64 kFarReadPosition = 4000000; | |
105 static const int kDataSize = 1024; | |
106 | |
107 static const char kHttpUrl[] = "http://localhost/foo.webm"; | |
108 static const char kFileUrl[] = "file:///tmp/bar.webm"; | |
109 | |
110 class BufferedDataSourceTest : public testing::Test { | |
111 public: | |
112 BufferedDataSourceTest() | |
113 : view_(WebView::create(NULL)), | |
114 frame_(WebLocalFrame::create(&client_)), | |
115 preload_(BufferedDataSource::AUTO) { | |
116 view_->setMainFrame(frame_); | |
117 } | |
118 | |
119 virtual ~BufferedDataSourceTest() { | |
120 view_->close(); | |
121 frame_->close(); | |
122 } | |
123 | |
124 MOCK_METHOD1(OnInitialize, void(bool)); | |
125 | |
126 void Initialize(const char* url, bool expected) { | |
127 GURL gurl(url); | |
128 data_source_.reset( | |
129 new MockBufferedDataSource(gurl, | |
130 message_loop_.message_loop_proxy(), | |
131 view_->mainFrame()->toWebLocalFrame(), | |
132 &host_)); | |
133 data_source_->SetPreload(preload_); | |
134 | |
135 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | |
136 ExpectCreateResourceLoader(); | |
137 EXPECT_CALL(*this, OnInitialize(expected)); | |
138 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize, | |
139 base::Unretained(this))); | |
140 message_loop_.RunUntilIdle(); | |
141 | |
142 bool is_http = gurl.SchemeIsHTTPOrHTTPS(); | |
143 EXPECT_EQ(data_source_->downloading(), is_http); | |
144 } | |
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 | |
154 // Helper to initialize tests with a valid 206 response. | |
155 void InitializeWith206Response() { | |
156 Initialize(kHttpUrl, true); | |
157 | |
158 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | |
159 Respond(response_generator_->Generate206(0)); | |
160 } | |
161 | |
162 // Helper to initialize tests with a valid file:// response. | |
163 void InitializeWithFileResponse() { | |
164 Initialize(kFileUrl, true); | |
165 | |
166 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); | |
167 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize)); | |
168 Respond(response_generator_->GenerateFileResponse(0)); | |
169 } | |
170 | |
171 // Stops any active loaders and shuts down the data source. | |
172 // | |
173 // This typically happens when the page is closed and for our purposes is | |
174 // appropriate to do when tearing down a test. | |
175 void Stop() { | |
176 if (data_source_->loading()) { | |
177 loader()->didFail(url_loader(), response_generator_->GenerateError()); | |
178 message_loop_.RunUntilIdle(); | |
179 } | |
180 | |
181 data_source_->Stop(); | |
182 message_loop_.RunUntilIdle(); | |
183 } | |
184 | |
185 void ExpectCreateResourceLoader() { | |
186 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _)) | |
187 .WillOnce(Invoke(data_source_.get(), | |
188 &MockBufferedDataSource::CreateMockResourceLoader)); | |
189 message_loop_.RunUntilIdle(); | |
190 } | |
191 | |
192 void Respond(const WebURLResponse& response) { | |
193 loader()->didReceiveResponse(url_loader(), response); | |
194 message_loop_.RunUntilIdle(); | |
195 } | |
196 | |
197 void ReceiveData(int size) { | |
198 scoped_ptr<char[]> data(new char[size]); | |
199 memset(data.get(), 0xA5, size); // Arbitrary non-zero value. | |
200 | |
201 loader()->didReceiveData(url_loader(), data.get(), size, size); | |
202 message_loop_.RunUntilIdle(); | |
203 } | |
204 | |
205 void FinishLoading() { | |
206 data_source_->set_loading(false); | |
207 loader()->didFinishLoading(url_loader(), 0, -1); | |
208 message_loop_.RunUntilIdle(); | |
209 } | |
210 | |
211 MOCK_METHOD1(ReadCallback, void(int size)); | |
212 | |
213 void ReadAt(int64 position) { | |
214 data_source_->Read(position, kDataSize, buffer_, | |
215 base::Bind(&BufferedDataSourceTest::ReadCallback, | |
216 base::Unretained(this))); | |
217 message_loop_.RunUntilIdle(); | |
218 } | |
219 | |
220 // Accessors for private variables on |data_source_|. | |
221 BufferedResourceLoader* loader() { | |
222 return data_source_->loader_.get(); | |
223 } | |
224 WebURLLoader* url_loader() { | |
225 return loader()->active_loader_->loader_.get(); | |
226 } | |
227 | |
228 BufferedDataSource::Preload preload() { return data_source_->preload_; } | |
229 void set_preload(BufferedDataSource::Preload preload) { preload_ = preload; } | |
230 BufferedResourceLoader::DeferStrategy defer_strategy() { | |
231 return loader()->defer_strategy_; | |
232 } | |
233 int data_source_bitrate() { return data_source_->bitrate_; } | |
234 int data_source_playback_rate() { return data_source_->playback_rate_; } | |
235 int loader_bitrate() { return loader()->bitrate_; } | |
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 } | |
241 | |
242 scoped_ptr<MockBufferedDataSource> data_source_; | |
243 | |
244 scoped_ptr<TestResponseGenerator> response_generator_; | |
245 MockWebFrameClient client_; | |
246 WebView* view_; | |
247 WebLocalFrame* frame_; | |
248 | |
249 StrictMock<MockBufferedDataSourceHost> host_; | |
250 base::MessageLoop message_loop_; | |
251 | |
252 private: | |
253 // Used for calling BufferedDataSource::Read(). | |
254 uint8 buffer_[kDataSize]; | |
255 | |
256 BufferedDataSource::Preload preload_; | |
257 | |
258 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); | |
259 }; | |
260 | |
261 TEST_F(BufferedDataSourceTest, Range_Supported) { | |
262 InitializeWith206Response(); | |
263 | |
264 EXPECT_TRUE(data_source_->loading()); | |
265 EXPECT_FALSE(data_source_->IsStreaming()); | |
266 Stop(); | |
267 } | |
268 | |
269 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) { | |
270 Initialize(kHttpUrl, true); | |
271 | |
272 Respond(response_generator_->Generate206( | |
273 0, TestResponseGenerator::kNoContentRangeInstanceSize)); | |
274 | |
275 EXPECT_TRUE(data_source_->loading()); | |
276 EXPECT_TRUE(data_source_->IsStreaming()); | |
277 Stop(); | |
278 } | |
279 | |
280 TEST_F(BufferedDataSourceTest, Range_NotFound) { | |
281 Initialize(kHttpUrl, false); | |
282 Respond(response_generator_->Generate404()); | |
283 | |
284 EXPECT_FALSE(data_source_->loading()); | |
285 Stop(); | |
286 } | |
287 | |
288 TEST_F(BufferedDataSourceTest, Range_NotSupported) { | |
289 InitializeWith200Response(); | |
290 | |
291 EXPECT_TRUE(data_source_->loading()); | |
292 EXPECT_TRUE(data_source_->IsStreaming()); | |
293 Stop(); | |
294 } | |
295 | |
296 // Special carve-out for Apache versions that choose to return a 200 for | |
297 // Range:0- ("because it's more efficient" than a 206) | |
298 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) { | |
299 Initialize(kHttpUrl, true); | |
300 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | |
301 WebURLResponse response = response_generator_->Generate200(); | |
302 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), | |
303 WebString::fromUTF8("bytes")); | |
304 Respond(response); | |
305 | |
306 EXPECT_TRUE(data_source_->loading()); | |
307 EXPECT_FALSE(data_source_->IsStreaming()); | |
308 Stop(); | |
309 } | |
310 | |
311 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) { | |
312 Initialize(kHttpUrl, false); | |
313 Respond(response_generator_->Generate206( | |
314 0, TestResponseGenerator::kNoContentRange)); | |
315 | |
316 EXPECT_FALSE(data_source_->loading()); | |
317 Stop(); | |
318 } | |
319 | |
320 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) { | |
321 Initialize(kHttpUrl, true); | |
322 | |
323 // It'll manage without a Content-Length response. | |
324 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | |
325 Respond(response_generator_->Generate206( | |
326 0, TestResponseGenerator::kNoContentLength)); | |
327 | |
328 EXPECT_TRUE(data_source_->loading()); | |
329 EXPECT_FALSE(data_source_->IsStreaming()); | |
330 Stop(); | |
331 } | |
332 | |
333 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) { | |
334 Initialize(kHttpUrl, false); | |
335 | |
336 // Now it's done and will fail. | |
337 Respond(response_generator_->Generate206(1337)); | |
338 | |
339 EXPECT_FALSE(data_source_->loading()); | |
340 Stop(); | |
341 } | |
342 | |
343 // Test the case where the initial response from the server indicates that | |
344 // Range requests are supported, but a later request prove otherwise. | |
345 TEST_F(BufferedDataSourceTest, Range_ServerLied) { | |
346 InitializeWith206Response(); | |
347 | |
348 // Read causing a new request to be made -- we'll expect it to error. | |
349 ExpectCreateResourceLoader(); | |
350 ReadAt(kFarReadPosition); | |
351 | |
352 // Return a 200 in response to a range request. | |
353 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
354 Respond(response_generator_->Generate200()); | |
355 | |
356 EXPECT_FALSE(data_source_->loading()); | |
357 Stop(); | |
358 } | |
359 | |
360 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) { | |
361 InitializeWith206Response(); | |
362 | |
363 // Make sure there's a pending read -- we'll expect it to error. | |
364 ReadAt(0); | |
365 | |
366 // Abort!!! | |
367 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
368 data_source_->Abort(); | |
369 message_loop_.RunUntilIdle(); | |
370 | |
371 EXPECT_FALSE(data_source_->loading()); | |
372 Stop(); | |
373 } | |
374 | |
375 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) { | |
376 InitializeWithFileResponse(); | |
377 | |
378 // Make sure there's a pending read -- we'll expect it to error. | |
379 ReadAt(0); | |
380 | |
381 // Abort!!! | |
382 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
383 data_source_->Abort(); | |
384 message_loop_.RunUntilIdle(); | |
385 | |
386 EXPECT_FALSE(data_source_->loading()); | |
387 Stop(); | |
388 } | |
389 | |
390 TEST_F(BufferedDataSourceTest, Http_Retry) { | |
391 InitializeWith206Response(); | |
392 | |
393 // Read to advance our position. | |
394 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
395 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1)); | |
396 ReadAt(0); | |
397 ReceiveData(kDataSize); | |
398 | |
399 // Issue a pending read but terminate the connection to force a retry. | |
400 ReadAt(kDataSize); | |
401 ExpectCreateResourceLoader(); | |
402 FinishLoading(); | |
403 Respond(response_generator_->Generate206(kDataSize)); | |
404 | |
405 // Complete the read. | |
406 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
407 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1)); | |
408 ReceiveData(kDataSize); | |
409 | |
410 EXPECT_TRUE(data_source_->loading()); | |
411 Stop(); | |
412 } | |
413 | |
414 TEST_F(BufferedDataSourceTest, File_Retry) { | |
415 InitializeWithFileResponse(); | |
416 | |
417 // Read to advance our position. | |
418 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
419 ReadAt(0); | |
420 ReceiveData(kDataSize); | |
421 | |
422 // Issue a pending read but terminate the connection to force a retry. | |
423 ReadAt(kDataSize); | |
424 ExpectCreateResourceLoader(); | |
425 FinishLoading(); | |
426 Respond(response_generator_->GenerateFileResponse(kDataSize)); | |
427 | |
428 // Complete the read. | |
429 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
430 ReceiveData(kDataSize); | |
431 | |
432 EXPECT_TRUE(data_source_->loading()); | |
433 Stop(); | |
434 } | |
435 | |
436 TEST_F(BufferedDataSourceTest, Http_TooManyRetries) { | |
437 InitializeWith206Response(); | |
438 | |
439 // Make sure there's a pending read -- we'll expect it to error. | |
440 ReadAt(0); | |
441 | |
442 // It'll try three times. | |
443 ExpectCreateResourceLoader(); | |
444 FinishLoading(); | |
445 Respond(response_generator_->Generate206(0)); | |
446 | |
447 ExpectCreateResourceLoader(); | |
448 FinishLoading(); | |
449 Respond(response_generator_->Generate206(0)); | |
450 | |
451 ExpectCreateResourceLoader(); | |
452 FinishLoading(); | |
453 Respond(response_generator_->Generate206(0)); | |
454 | |
455 // It'll error after this. | |
456 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
457 FinishLoading(); | |
458 | |
459 EXPECT_FALSE(data_source_->loading()); | |
460 Stop(); | |
461 } | |
462 | |
463 TEST_F(BufferedDataSourceTest, File_TooManyRetries) { | |
464 InitializeWithFileResponse(); | |
465 | |
466 // Make sure there's a pending read -- we'll expect it to error. | |
467 ReadAt(0); | |
468 | |
469 // It'll try three times. | |
470 ExpectCreateResourceLoader(); | |
471 FinishLoading(); | |
472 Respond(response_generator_->GenerateFileResponse(0)); | |
473 | |
474 ExpectCreateResourceLoader(); | |
475 FinishLoading(); | |
476 Respond(response_generator_->GenerateFileResponse(0)); | |
477 | |
478 ExpectCreateResourceLoader(); | |
479 FinishLoading(); | |
480 Respond(response_generator_->GenerateFileResponse(0)); | |
481 | |
482 // It'll error after this. | |
483 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
484 FinishLoading(); | |
485 | |
486 EXPECT_FALSE(data_source_->loading()); | |
487 Stop(); | |
488 } | |
489 | |
490 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) { | |
491 Initialize(kFileUrl, false); | |
492 EXPECT_FALSE(data_source_->downloading()); | |
493 | |
494 Respond(response_generator_->GenerateFileResponse(-1)); | |
495 | |
496 EXPECT_FALSE(data_source_->loading()); | |
497 Stop(); | |
498 } | |
499 | |
500 TEST_F(BufferedDataSourceTest, File_Successful) { | |
501 InitializeWithFileResponse(); | |
502 | |
503 EXPECT_TRUE(data_source_->loading()); | |
504 EXPECT_FALSE(data_source_->IsStreaming()); | |
505 Stop(); | |
506 } | |
507 | |
508 TEST_F(BufferedDataSourceTest, StopDuringRead) { | |
509 InitializeWith206Response(); | |
510 | |
511 uint8 buffer[256]; | |
512 data_source_->Read(0, arraysize(buffer), buffer, base::Bind( | |
513 &BufferedDataSourceTest::ReadCallback, base::Unretained(this))); | |
514 | |
515 // The outstanding read should fail before the stop callback runs. | |
516 { | |
517 InSequence s; | |
518 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
519 data_source_->Stop(); | |
520 } | |
521 message_loop_.RunUntilIdle(); | |
522 } | |
523 | |
524 TEST_F(BufferedDataSourceTest, DefaultValues) { | |
525 InitializeWith206Response(); | |
526 | |
527 // Ensure we have sane values for default loading scenario. | |
528 EXPECT_EQ(BufferedDataSource::AUTO, preload()); | |
529 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
530 | |
531 EXPECT_EQ(0, data_source_bitrate()); | |
532 EXPECT_EQ(0.0f, data_source_playback_rate()); | |
533 EXPECT_EQ(0, loader_bitrate()); | |
534 EXPECT_EQ(0.0f, loader_playback_rate()); | |
535 | |
536 EXPECT_TRUE(data_source_->loading()); | |
537 Stop(); | |
538 } | |
539 | |
540 TEST_F(BufferedDataSourceTest, SetBitrate) { | |
541 InitializeWith206Response(); | |
542 | |
543 data_source_->SetBitrate(1234); | |
544 message_loop_.RunUntilIdle(); | |
545 EXPECT_EQ(1234, data_source_bitrate()); | |
546 EXPECT_EQ(1234, loader_bitrate()); | |
547 | |
548 // Read so far ahead to cause the loader to get recreated. | |
549 BufferedResourceLoader* old_loader = loader(); | |
550 ExpectCreateResourceLoader(); | |
551 ReadAt(kFarReadPosition); | |
552 Respond(response_generator_->Generate206(kFarReadPosition)); | |
553 | |
554 // Verify loader changed but still has same bitrate. | |
555 EXPECT_NE(old_loader, loader()); | |
556 EXPECT_EQ(1234, loader_bitrate()); | |
557 | |
558 EXPECT_TRUE(data_source_->loading()); | |
559 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
560 Stop(); | |
561 } | |
562 | |
563 TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) { | |
564 InitializeWith206Response(); | |
565 | |
566 data_source_->MediaPlaybackRateChanged(2.0f); | |
567 message_loop_.RunUntilIdle(); | |
568 EXPECT_EQ(2.0f, data_source_playback_rate()); | |
569 EXPECT_EQ(2.0f, loader_playback_rate()); | |
570 | |
571 // Read so far ahead to cause the loader to get recreated. | |
572 BufferedResourceLoader* old_loader = loader(); | |
573 ExpectCreateResourceLoader(); | |
574 ReadAt(kFarReadPosition); | |
575 Respond(response_generator_->Generate206(kFarReadPosition)); | |
576 | |
577 // Verify loader changed but still has same playback rate. | |
578 EXPECT_NE(old_loader, loader()); | |
579 | |
580 EXPECT_TRUE(data_source_->loading()); | |
581 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | |
582 Stop(); | |
583 } | |
584 | |
585 TEST_F(BufferedDataSourceTest, Http_Read) { | |
586 InitializeWith206Response(); | |
587 | |
588 ReadAt(0); | |
589 | |
590 // Receive first half of the read. | |
591 EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1)); | |
592 ReceiveData(kDataSize / 2); | |
593 | |
594 // Receive last half of the read. | |
595 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
596 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1)); | |
597 ReceiveData(kDataSize / 2); | |
598 | |
599 EXPECT_TRUE(data_source_->downloading()); | |
600 Stop(); | |
601 } | |
602 | |
603 TEST_F(BufferedDataSourceTest, Http_Read_Seek) { | |
604 InitializeWith206Response(); | |
605 | |
606 // Read a bit from the beginning. | |
607 ReadAt(0); | |
608 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
609 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1)); | |
610 ReceiveData(kDataSize); | |
611 | |
612 // Simulate a seek by reading a bit beyond kDataSize. | |
613 ReadAt(kDataSize * 2); | |
614 | |
615 // We receive data leading up to but not including our read. | |
616 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1)); | |
617 ReceiveData(kDataSize); | |
618 | |
619 // We now receive the rest of the data for our read. | |
620 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
621 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1)); | |
622 ReceiveData(kDataSize); | |
623 | |
624 EXPECT_TRUE(data_source_->downloading()); | |
625 Stop(); | |
626 } | |
627 | |
628 TEST_F(BufferedDataSourceTest, File_Read) { | |
629 InitializeWithFileResponse(); | |
630 | |
631 ReadAt(0); | |
632 | |
633 // Receive first half of the read but no buffering update. | |
634 ReceiveData(kDataSize / 2); | |
635 | |
636 // Receive last half of the read but no buffering update. | |
637 EXPECT_CALL(*this, ReadCallback(kDataSize)); | |
638 ReceiveData(kDataSize / 2); | |
639 | |
640 Stop(); | |
641 } | |
642 | |
643 TEST_F(BufferedDataSourceTest, Http_FinishLoading) { | |
644 InitializeWith206Response(); | |
645 | |
646 EXPECT_TRUE(data_source_->downloading()); | |
647 FinishLoading(); | |
648 EXPECT_FALSE(data_source_->downloading()); | |
649 | |
650 Stop(); | |
651 } | |
652 | |
653 TEST_F(BufferedDataSourceTest, File_FinishLoading) { | |
654 InitializeWithFileResponse(); | |
655 | |
656 EXPECT_FALSE(data_source_->downloading()); | |
657 FinishLoading(); | |
658 EXPECT_FALSE(data_source_->downloading()); | |
659 | |
660 Stop(); | |
661 } | |
662 | |
663 TEST_F(BufferedDataSourceTest, LocalResource_DeferStrategy) { | |
664 InitializeWithFileResponse(); | |
665 | |
666 EXPECT_EQ(BufferedDataSource::AUTO, preload()); | |
667 EXPECT_TRUE(is_local_source()); | |
668 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
669 | |
670 data_source_->MediaIsPlaying(); | |
671 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
672 | |
673 data_source_->MediaIsPaused(); | |
674 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
675 | |
676 Stop(); | |
677 } | |
678 | |
679 TEST_F(BufferedDataSourceTest, LocalResource_PreloadMetadata_DeferStrategy) { | |
680 set_preload(BufferedDataSource::METADATA); | |
681 InitializeWithFileResponse(); | |
682 | |
683 EXPECT_EQ(BufferedDataSource::METADATA, preload()); | |
684 EXPECT_TRUE(is_local_source()); | |
685 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy()); | |
686 | |
687 data_source_->MediaIsPlaying(); | |
688 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
689 | |
690 data_source_->MediaIsPaused(); | |
691 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
692 | |
693 Stop(); | |
694 } | |
695 | |
696 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse200_DeferStrategy) { | |
697 InitializeWith200Response(); | |
698 | |
699 EXPECT_EQ(BufferedDataSource::AUTO, preload()); | |
700 EXPECT_FALSE(is_local_source()); | |
701 EXPECT_FALSE(loader()->range_supported()); | |
702 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
703 | |
704 data_source_->MediaIsPlaying(); | |
705 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
706 | |
707 data_source_->MediaIsPaused(); | |
708 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
709 | |
710 Stop(); | |
711 } | |
712 | |
713 TEST_F(BufferedDataSourceTest, | |
714 ExternalResource_Response200_PreloadMetadata_DeferStrategy) { | |
715 set_preload(BufferedDataSource::METADATA); | |
716 InitializeWith200Response(); | |
717 | |
718 EXPECT_EQ(BufferedDataSource::METADATA, preload()); | |
719 EXPECT_FALSE(is_local_source()); | |
720 EXPECT_FALSE(loader()->range_supported()); | |
721 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy()); | |
722 | |
723 data_source_->MediaIsPlaying(); | |
724 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
725 | |
726 data_source_->MediaIsPaused(); | |
727 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
728 | |
729 Stop(); | |
730 } | |
731 | |
732 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse206_DeferStrategy) { | |
733 InitializeWith206Response(); | |
734 | |
735 EXPECT_EQ(BufferedDataSource::AUTO, preload()); | |
736 EXPECT_FALSE(is_local_source()); | |
737 EXPECT_TRUE(loader()->range_supported()); | |
738 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
739 | |
740 data_source_->MediaIsPlaying(); | |
741 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
742 set_might_be_reused_from_cache_in_future(true); | |
743 data_source_->MediaIsPaused(); | |
744 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy()); | |
745 | |
746 data_source_->MediaIsPlaying(); | |
747 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
748 set_might_be_reused_from_cache_in_future(false); | |
749 data_source_->MediaIsPaused(); | |
750 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | |
751 | |
752 Stop(); | |
753 } | |
754 | |
755 TEST_F(BufferedDataSourceTest, | |
756 ExternalResource_Response206_PreloadMetadata_DeferStrategy) { | |
757 set_preload(BufferedDataSource::METADATA); | |
758 InitializeWith206Response(); | |
759 | |
760 EXPECT_EQ(BufferedDataSource::METADATA, preload()); | |
761 EXPECT_FALSE(is_local_source()); | |
762 EXPECT_TRUE(loader()->range_supported()); | |
763 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, 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 } // namespace content | |
OLD | NEW |