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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "media/base/media_log.h"
9 #include "media/base/mock_filters.h"
10 #include "media/base/test_helpers.h"
11 #include "media/blink/buffered_data_source.h"
12 #include "media/blink/mock_webframeclient.h"
13 #include "media/blink/mock_weburlloader.h"
14 #include "media/blink/test_response_generator.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"
20 using ::testing::Assign
;
21 using ::testing::DoAll
;
22 using ::testing::Invoke
;
23 using ::testing::InvokeWithoutArgs
;
24 using ::testing::InSequence
;
25 using ::testing::NiceMock
;
26 using ::testing::StrictMock
;
28 using blink::WebLocalFrame
;
29 using blink::WebString
;
30 using blink::WebURLLoader
;
31 using blink::WebURLResponse
;
36 class MockBufferedDataSourceHost
: public BufferedDataSourceHost
{
38 MockBufferedDataSourceHost() {}
39 virtual ~MockBufferedDataSourceHost() {}
41 MOCK_METHOD1(SetTotalBytes
, void(int64 total_bytes
));
42 MOCK_METHOD2(AddBufferedByteRange
, void(int64 start
, int64 end
));
45 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost
);
48 // Overrides CreateResourceLoader() to permit injecting a MockWebURLLoader.
49 // Also keeps track of whether said MockWebURLLoader is actively loading.
50 class MockBufferedDataSource
: public BufferedDataSource
{
52 MockBufferedDataSource(
54 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
56 BufferedDataSourceHost
* host
)
57 : BufferedDataSource(url
,
58 BufferedResourceLoader::kUnspecified
,
61 new media::MediaLog(),
63 base::Bind(&MockBufferedDataSource::set_downloading
,
64 base::Unretained(this))),
67 virtual ~MockBufferedDataSource() {}
69 MOCK_METHOD2(CreateResourceLoader
, BufferedResourceLoader
*(int64
, int64
));
70 BufferedResourceLoader
* CreateMockResourceLoader(int64 first_byte_position
,
71 int64 last_byte_position
) {
72 CHECK(!loading_
) << "Previous resource load wasn't cancelled";
74 BufferedResourceLoader
* loader
=
75 BufferedDataSource::CreateResourceLoader(first_byte_position
,
78 // Keep track of active loading state via loadAsynchronously() and cancel().
79 NiceMock
<MockWebURLLoader
>* url_loader
= new NiceMock
<MockWebURLLoader
>();
80 ON_CALL(*url_loader
, loadAsynchronously(_
, _
))
81 .WillByDefault(Assign(&loading_
, true));
82 ON_CALL(*url_loader
, cancel())
83 .WillByDefault(Assign(&loading_
, false));
85 // |test_loader_| will be used when Start() is called.
86 loader
->test_loader_
= scoped_ptr
<WebURLLoader
>(url_loader
);
90 bool loading() { return loading_
; }
91 void set_loading(bool loading
) { loading_
= loading
; }
92 bool downloading() { return downloading_
; }
93 void set_downloading(bool downloading
) { downloading_
= downloading
; }
96 // Whether the resource is downloading or deferred.
99 // Whether the resource load has starting loading but yet to been cancelled.
102 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource
);
105 static const int64 kFileSize
= 5000000;
106 static const int64 kFarReadPosition
= 4000000;
107 static const int kDataSize
= 1024;
109 static const char kHttpUrl
[] = "http://localhost/foo.webm";
110 static const char kFileUrl
[] = "file:///tmp/bar.webm";
112 class BufferedDataSourceTest
: public testing::Test
{
114 BufferedDataSourceTest()
115 : view_(WebView::create(NULL
)),
116 frame_(WebLocalFrame::create(&client_
)),
117 preload_(BufferedDataSource::AUTO
) {
118 view_
->setMainFrame(frame_
);
121 virtual ~BufferedDataSourceTest() {
126 MOCK_METHOD1(OnInitialize
, void(bool));
128 void Initialize(const char* url
, bool expected
) {
131 new MockBufferedDataSource(gurl
,
132 message_loop_
.message_loop_proxy(),
133 view_
->mainFrame()->toWebLocalFrame(),
135 data_source_
->SetPreload(preload_
);
137 response_generator_
.reset(new TestResponseGenerator(gurl
, kFileSize
));
138 ExpectCreateResourceLoader();
139 EXPECT_CALL(*this, OnInitialize(expected
));
140 data_source_
->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize
,
141 base::Unretained(this)));
142 message_loop_
.RunUntilIdle();
144 bool is_http
= gurl
.SchemeIsHTTPOrHTTPS();
145 EXPECT_EQ(data_source_
->downloading(), is_http
);
148 // Helper to initialize tests with a valid 200 response.
149 void InitializeWith200Response() {
150 Initialize(kHttpUrl
, true);
152 EXPECT_CALL(host_
, SetTotalBytes(response_generator_
->content_length()));
153 Respond(response_generator_
->Generate200());
156 // Helper to initialize tests with a valid 206 response.
157 void InitializeWith206Response() {
158 Initialize(kHttpUrl
, true);
160 EXPECT_CALL(host_
, SetTotalBytes(response_generator_
->content_length()));
161 Respond(response_generator_
->Generate206(0));
164 // Helper to initialize tests with a valid file:// response.
165 void InitializeWithFileResponse() {
166 Initialize(kFileUrl
, true);
168 EXPECT_CALL(host_
, SetTotalBytes(kFileSize
));
169 EXPECT_CALL(host_
, AddBufferedByteRange(0, kFileSize
));
170 Respond(response_generator_
->GenerateFileResponse(0));
173 // Stops any active loaders and shuts down the data source.
175 // This typically happens when the page is closed and for our purposes is
176 // appropriate to do when tearing down a test.
178 if (data_source_
->loading()) {
179 loader()->didFail(url_loader(), response_generator_
->GenerateError());
180 message_loop_
.RunUntilIdle();
183 data_source_
->Stop();
184 message_loop_
.RunUntilIdle();
187 void ExpectCreateResourceLoader() {
188 EXPECT_CALL(*data_source_
, CreateResourceLoader(_
, _
))
189 .WillOnce(Invoke(data_source_
.get(),
190 &MockBufferedDataSource::CreateMockResourceLoader
));
191 message_loop_
.RunUntilIdle();
194 void Respond(const WebURLResponse
& response
) {
195 loader()->didReceiveResponse(url_loader(), response
);
196 message_loop_
.RunUntilIdle();
199 void ReceiveData(int size
) {
200 scoped_ptr
<char[]> data(new char[size
]);
201 memset(data
.get(), 0xA5, size
); // Arbitrary non-zero value.
203 loader()->didReceiveData(url_loader(), data
.get(), size
, size
);
204 message_loop_
.RunUntilIdle();
207 void FinishLoading() {
208 data_source_
->set_loading(false);
209 loader()->didFinishLoading(url_loader(), 0, -1);
210 message_loop_
.RunUntilIdle();
213 MOCK_METHOD1(ReadCallback
, void(int size
));
215 void ReadAt(int64 position
) {
216 data_source_
->Read(position
, kDataSize
, buffer_
,
217 base::Bind(&BufferedDataSourceTest::ReadCallback
,
218 base::Unretained(this)));
219 message_loop_
.RunUntilIdle();
222 // Accessors for private variables on |data_source_|.
223 BufferedResourceLoader
* loader() {
224 return data_source_
->loader_
.get();
226 WebURLLoader
* url_loader() {
227 return loader()->active_loader_
->loader_
.get();
230 BufferedDataSource::Preload
preload() { return data_source_
->preload_
; }
231 void set_preload(BufferedDataSource::Preload preload
) { preload_
= preload
; }
232 BufferedResourceLoader::DeferStrategy
defer_strategy() {
233 return loader()->defer_strategy_
;
235 int data_source_bitrate() { return data_source_
->bitrate_
; }
236 int data_source_playback_rate() { return data_source_
->playback_rate_
; }
237 int loader_bitrate() { return loader()->bitrate_
; }
238 int loader_playback_rate() { return loader()->playback_rate_
; }
239 bool is_local_source() { return data_source_
->assume_fully_buffered(); }
240 void set_might_be_reused_from_cache_in_future(bool value
) {
241 loader()->might_be_reused_from_cache_in_future_
= value
;
244 scoped_ptr
<MockBufferedDataSource
> data_source_
;
246 scoped_ptr
<TestResponseGenerator
> response_generator_
;
247 MockWebFrameClient client_
;
249 WebLocalFrame
* frame_
;
251 StrictMock
<MockBufferedDataSourceHost
> host_
;
252 base::MessageLoop message_loop_
;
255 // Used for calling BufferedDataSource::Read().
256 uint8 buffer_
[kDataSize
];
258 BufferedDataSource::Preload preload_
;
260 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest
);
263 TEST_F(BufferedDataSourceTest
, Range_Supported
) {
264 InitializeWith206Response();
266 EXPECT_TRUE(data_source_
->loading());
267 EXPECT_FALSE(data_source_
->IsStreaming());
271 TEST_F(BufferedDataSourceTest
, Range_InstanceSizeUnknown
) {
272 Initialize(kHttpUrl
, true);
274 Respond(response_generator_
->Generate206(
275 0, TestResponseGenerator::kNoContentRangeInstanceSize
));
277 EXPECT_TRUE(data_source_
->loading());
278 EXPECT_TRUE(data_source_
->IsStreaming());
282 TEST_F(BufferedDataSourceTest
, Range_NotFound
) {
283 Initialize(kHttpUrl
, false);
284 Respond(response_generator_
->Generate404());
286 EXPECT_FALSE(data_source_
->loading());
290 TEST_F(BufferedDataSourceTest
, Range_NotSupported
) {
291 InitializeWith200Response();
293 EXPECT_TRUE(data_source_
->loading());
294 EXPECT_TRUE(data_source_
->IsStreaming());
298 // Special carve-out for Apache versions that choose to return a 200 for
299 // Range:0- ("because it's more efficient" than a 206)
300 TEST_F(BufferedDataSourceTest
, Range_SupportedButReturned200
) {
301 Initialize(kHttpUrl
, true);
302 EXPECT_CALL(host_
, SetTotalBytes(response_generator_
->content_length()));
303 WebURLResponse response
= response_generator_
->Generate200();
304 response
.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
305 WebString::fromUTF8("bytes"));
308 EXPECT_TRUE(data_source_
->loading());
309 EXPECT_FALSE(data_source_
->IsStreaming());
313 TEST_F(BufferedDataSourceTest
, Range_MissingContentRange
) {
314 Initialize(kHttpUrl
, false);
315 Respond(response_generator_
->Generate206(
316 0, TestResponseGenerator::kNoContentRange
));
318 EXPECT_FALSE(data_source_
->loading());
322 TEST_F(BufferedDataSourceTest
, Range_MissingContentLength
) {
323 Initialize(kHttpUrl
, true);
325 // It'll manage without a Content-Length response.
326 EXPECT_CALL(host_
, SetTotalBytes(response_generator_
->content_length()));
327 Respond(response_generator_
->Generate206(
328 0, TestResponseGenerator::kNoContentLength
));
330 EXPECT_TRUE(data_source_
->loading());
331 EXPECT_FALSE(data_source_
->IsStreaming());
335 TEST_F(BufferedDataSourceTest
, Range_WrongContentRange
) {
336 Initialize(kHttpUrl
, false);
338 // Now it's done and will fail.
339 Respond(response_generator_
->Generate206(1337));
341 EXPECT_FALSE(data_source_
->loading());
345 // Test the case where the initial response from the server indicates that
346 // Range requests are supported, but a later request prove otherwise.
347 TEST_F(BufferedDataSourceTest
, Range_ServerLied
) {
348 InitializeWith206Response();
350 // Read causing a new request to be made -- we'll expect it to error.
351 ExpectCreateResourceLoader();
352 ReadAt(kFarReadPosition
);
354 // Return a 200 in response to a range request.
355 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
356 Respond(response_generator_
->Generate200());
358 EXPECT_FALSE(data_source_
->loading());
362 TEST_F(BufferedDataSourceTest
, Http_AbortWhileReading
) {
363 InitializeWith206Response();
365 // Make sure there's a pending read -- we'll expect it to error.
369 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
370 data_source_
->Abort();
371 message_loop_
.RunUntilIdle();
373 EXPECT_FALSE(data_source_
->loading());
377 TEST_F(BufferedDataSourceTest
, File_AbortWhileReading
) {
378 InitializeWithFileResponse();
380 // Make sure there's a pending read -- we'll expect it to error.
384 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
385 data_source_
->Abort();
386 message_loop_
.RunUntilIdle();
388 EXPECT_FALSE(data_source_
->loading());
392 TEST_F(BufferedDataSourceTest
, Http_Retry
) {
393 InitializeWith206Response();
395 // Read to advance our position.
396 EXPECT_CALL(*this, ReadCallback(kDataSize
));
397 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
- 1));
399 ReceiveData(kDataSize
);
401 // Issue a pending read but terminate the connection to force a retry.
403 ExpectCreateResourceLoader();
405 Respond(response_generator_
->Generate206(kDataSize
));
407 // Complete the read.
408 EXPECT_CALL(*this, ReadCallback(kDataSize
));
409 EXPECT_CALL(host_
, AddBufferedByteRange(kDataSize
, (kDataSize
* 2) - 1));
410 ReceiveData(kDataSize
);
412 EXPECT_TRUE(data_source_
->loading());
416 TEST_F(BufferedDataSourceTest
, Http_RetryOnError
) {
417 InitializeWith206Response();
419 // Read to advance our position.
420 EXPECT_CALL(*this, ReadCallback(kDataSize
));
421 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
- 1));
423 ReceiveData(kDataSize
);
425 // Issue a pending read but trigger an error to force a retry.
426 EXPECT_CALL(*this, ReadCallback(kDataSize
));
427 EXPECT_CALL(host_
, AddBufferedByteRange(kDataSize
, (kDataSize
* 2) - 1));
429 base::RunLoop run_loop
;
430 EXPECT_CALL(*data_source_
, CreateResourceLoader(_
, _
))
432 DoAll(InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
),
433 Invoke(data_source_
.get(),
434 &MockBufferedDataSource::CreateMockResourceLoader
)));
435 loader()->didFail(url_loader(), response_generator_
->GenerateError());
437 Respond(response_generator_
->Generate206(kDataSize
));
438 ReceiveData(kDataSize
);
440 EXPECT_FALSE(data_source_
->loading());
444 TEST_F(BufferedDataSourceTest
, File_Retry
) {
445 InitializeWithFileResponse();
447 // Read to advance our position.
448 EXPECT_CALL(*this, ReadCallback(kDataSize
));
450 ReceiveData(kDataSize
);
452 // Issue a pending read but terminate the connection to force a retry.
454 ExpectCreateResourceLoader();
456 Respond(response_generator_
->GenerateFileResponse(kDataSize
));
458 // Complete the read.
459 EXPECT_CALL(*this, ReadCallback(kDataSize
));
460 ReceiveData(kDataSize
);
462 EXPECT_TRUE(data_source_
->loading());
466 TEST_F(BufferedDataSourceTest
, Http_TooManyRetries
) {
467 InitializeWith206Response();
469 // Make sure there's a pending read -- we'll expect it to error.
472 // It'll try three times.
473 ExpectCreateResourceLoader();
475 Respond(response_generator_
->Generate206(0));
477 ExpectCreateResourceLoader();
479 Respond(response_generator_
->Generate206(0));
481 ExpectCreateResourceLoader();
483 Respond(response_generator_
->Generate206(0));
485 // It'll error after this.
486 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
489 EXPECT_FALSE(data_source_
->loading());
493 TEST_F(BufferedDataSourceTest
, File_TooManyRetries
) {
494 InitializeWithFileResponse();
496 // Make sure there's a pending read -- we'll expect it to error.
499 // It'll try three times.
500 ExpectCreateResourceLoader();
502 Respond(response_generator_
->GenerateFileResponse(0));
504 ExpectCreateResourceLoader();
506 Respond(response_generator_
->GenerateFileResponse(0));
508 ExpectCreateResourceLoader();
510 Respond(response_generator_
->GenerateFileResponse(0));
512 // It'll error after this.
513 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
516 EXPECT_FALSE(data_source_
->loading());
520 TEST_F(BufferedDataSourceTest
, File_InstanceSizeUnknown
) {
521 Initialize(kFileUrl
, false);
522 EXPECT_FALSE(data_source_
->downloading());
524 Respond(response_generator_
->GenerateFileResponse(-1));
526 EXPECT_FALSE(data_source_
->loading());
530 TEST_F(BufferedDataSourceTest
, File_Successful
) {
531 InitializeWithFileResponse();
533 EXPECT_TRUE(data_source_
->loading());
534 EXPECT_FALSE(data_source_
->IsStreaming());
538 TEST_F(BufferedDataSourceTest
, StopDuringRead
) {
539 InitializeWith206Response();
542 data_source_
->Read(0, arraysize(buffer
), buffer
, base::Bind(
543 &BufferedDataSourceTest::ReadCallback
, base::Unretained(this)));
545 // The outstanding read should fail before the stop callback runs.
548 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
549 data_source_
->Stop();
551 message_loop_
.RunUntilIdle();
554 TEST_F(BufferedDataSourceTest
, DefaultValues
) {
555 InitializeWith206Response();
557 // Ensure we have sane values for default loading scenario.
558 EXPECT_EQ(BufferedDataSource::AUTO
, preload());
559 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
561 EXPECT_EQ(0, data_source_bitrate());
562 EXPECT_EQ(0.0f
, data_source_playback_rate());
563 EXPECT_EQ(0, loader_bitrate());
564 EXPECT_EQ(0.0f
, loader_playback_rate());
566 EXPECT_TRUE(data_source_
->loading());
570 TEST_F(BufferedDataSourceTest
, SetBitrate
) {
571 InitializeWith206Response();
573 data_source_
->SetBitrate(1234);
574 message_loop_
.RunUntilIdle();
575 EXPECT_EQ(1234, data_source_bitrate());
576 EXPECT_EQ(1234, loader_bitrate());
578 // Read so far ahead to cause the loader to get recreated.
579 BufferedResourceLoader
* old_loader
= loader();
580 ExpectCreateResourceLoader();
581 ReadAt(kFarReadPosition
);
582 Respond(response_generator_
->Generate206(kFarReadPosition
));
584 // Verify loader changed but still has same bitrate.
585 EXPECT_NE(old_loader
, loader());
586 EXPECT_EQ(1234, loader_bitrate());
588 EXPECT_TRUE(data_source_
->loading());
589 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
593 TEST_F(BufferedDataSourceTest
, MediaPlaybackRateChanged
) {
594 InitializeWith206Response();
596 data_source_
->MediaPlaybackRateChanged(2.0f
);
597 message_loop_
.RunUntilIdle();
598 EXPECT_EQ(2.0f
, data_source_playback_rate());
599 EXPECT_EQ(2.0f
, loader_playback_rate());
601 // Read so far ahead to cause the loader to get recreated.
602 BufferedResourceLoader
* old_loader
= loader();
603 ExpectCreateResourceLoader();
604 ReadAt(kFarReadPosition
);
605 Respond(response_generator_
->Generate206(kFarReadPosition
));
607 // Verify loader changed but still has same playback rate.
608 EXPECT_NE(old_loader
, loader());
610 EXPECT_TRUE(data_source_
->loading());
611 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError
));
615 TEST_F(BufferedDataSourceTest
, Http_Read
) {
616 InitializeWith206Response();
620 // Receive first half of the read.
621 EXPECT_CALL(host_
, AddBufferedByteRange(0, (kDataSize
/ 2) - 1));
622 ReceiveData(kDataSize
/ 2);
624 // Receive last half of the read.
625 EXPECT_CALL(*this, ReadCallback(kDataSize
));
626 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
- 1));
627 ReceiveData(kDataSize
/ 2);
629 EXPECT_TRUE(data_source_
->downloading());
633 TEST_F(BufferedDataSourceTest
, Http_Read_Seek
) {
634 InitializeWith206Response();
636 // Read a bit from the beginning.
638 EXPECT_CALL(*this, ReadCallback(kDataSize
));
639 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
- 1));
640 ReceiveData(kDataSize
);
642 // Simulate a seek by reading a bit beyond kDataSize.
643 ReadAt(kDataSize
* 2);
645 // We receive data leading up to but not including our read.
646 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
* 2 - 1));
647 ReceiveData(kDataSize
);
649 // We now receive the rest of the data for our read.
650 EXPECT_CALL(*this, ReadCallback(kDataSize
));
651 EXPECT_CALL(host_
, AddBufferedByteRange(0, kDataSize
* 3 - 1));
652 ReceiveData(kDataSize
);
654 EXPECT_TRUE(data_source_
->downloading());
658 TEST_F(BufferedDataSourceTest
, File_Read
) {
659 InitializeWithFileResponse();
663 // Receive first half of the read but no buffering update.
664 ReceiveData(kDataSize
/ 2);
666 // Receive last half of the read but no buffering update.
667 EXPECT_CALL(*this, ReadCallback(kDataSize
));
668 ReceiveData(kDataSize
/ 2);
673 TEST_F(BufferedDataSourceTest
, Http_FinishLoading
) {
674 InitializeWith206Response();
676 EXPECT_TRUE(data_source_
->downloading());
678 EXPECT_FALSE(data_source_
->downloading());
683 TEST_F(BufferedDataSourceTest
, File_FinishLoading
) {
684 InitializeWithFileResponse();
686 EXPECT_FALSE(data_source_
->downloading());
688 EXPECT_FALSE(data_source_
->downloading());
693 TEST_F(BufferedDataSourceTest
, LocalResource_DeferStrategy
) {
694 InitializeWithFileResponse();
696 EXPECT_EQ(BufferedDataSource::AUTO
, preload());
697 EXPECT_TRUE(is_local_source());
698 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
700 data_source_
->MediaIsPlaying();
701 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
703 data_source_
->MediaIsPaused();
704 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
709 TEST_F(BufferedDataSourceTest
, LocalResource_PreloadMetadata_DeferStrategy
) {
710 set_preload(BufferedDataSource::METADATA
);
711 InitializeWithFileResponse();
713 EXPECT_EQ(BufferedDataSource::METADATA
, preload());
714 EXPECT_TRUE(is_local_source());
715 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer
, defer_strategy());
717 data_source_
->MediaIsPlaying();
718 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
720 data_source_
->MediaIsPaused();
721 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
726 TEST_F(BufferedDataSourceTest
, ExternalResource_Reponse200_DeferStrategy
) {
727 InitializeWith200Response();
729 EXPECT_EQ(BufferedDataSource::AUTO
, preload());
730 EXPECT_FALSE(is_local_source());
731 EXPECT_FALSE(loader()->range_supported());
732 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
734 data_source_
->MediaIsPlaying();
735 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
737 data_source_
->MediaIsPaused();
738 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
743 TEST_F(BufferedDataSourceTest
,
744 ExternalResource_Response200_PreloadMetadata_DeferStrategy
) {
745 set_preload(BufferedDataSource::METADATA
);
746 InitializeWith200Response();
748 EXPECT_EQ(BufferedDataSource::METADATA
, preload());
749 EXPECT_FALSE(is_local_source());
750 EXPECT_FALSE(loader()->range_supported());
751 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer
, defer_strategy());
753 data_source_
->MediaIsPlaying();
754 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
756 data_source_
->MediaIsPaused();
757 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
762 TEST_F(BufferedDataSourceTest
, ExternalResource_Reponse206_DeferStrategy
) {
763 InitializeWith206Response();
765 EXPECT_EQ(BufferedDataSource::AUTO
, preload());
766 EXPECT_FALSE(is_local_source());
767 EXPECT_TRUE(loader()->range_supported());
768 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
770 data_source_
->MediaIsPlaying();
771 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
772 set_might_be_reused_from_cache_in_future(true);
773 data_source_
->MediaIsPaused();
774 EXPECT_EQ(BufferedResourceLoader::kNeverDefer
, defer_strategy());
776 data_source_
->MediaIsPlaying();
777 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
778 set_might_be_reused_from_cache_in_future(false);
779 data_source_
->MediaIsPaused();
780 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
785 TEST_F(BufferedDataSourceTest
,
786 ExternalResource_Response206_PreloadMetadata_DeferStrategy
) {
787 set_preload(BufferedDataSource::METADATA
);
788 InitializeWith206Response();
790 EXPECT_EQ(BufferedDataSource::METADATA
, preload());
791 EXPECT_FALSE(is_local_source());
792 EXPECT_TRUE(loader()->range_supported());
793 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer
, defer_strategy());
795 data_source_
->MediaIsPlaying();
796 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
797 set_might_be_reused_from_cache_in_future(true);
798 data_source_
->MediaIsPaused();
799 EXPECT_EQ(BufferedResourceLoader::kNeverDefer
, defer_strategy());
801 data_source_
->MediaIsPlaying();
802 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());
803 set_might_be_reused_from_cache_in_future(false);
804 data_source_
->MediaIsPaused();
805 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer
, defer_strategy());