Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / media / blink / buffered_data_source_unittest.cc
blobeed8674dc14bea5a80cd1a3e7adf7bb832590c77
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.
5 #include "base/bind.h"
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"
19 using ::testing::_;
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;
32 using blink::WebView;
34 namespace media {
36 class MockBufferedDataSourceHost : public BufferedDataSourceHost {
37 public:
38 MockBufferedDataSourceHost() {}
39 virtual ~MockBufferedDataSourceHost() {}
41 MOCK_METHOD1(SetTotalBytes, void(int64 total_bytes));
42 MOCK_METHOD2(AddBufferedByteRange, void(int64 start, int64 end));
44 private:
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 {
51 public:
52 MockBufferedDataSource(
53 const GURL& url,
54 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
55 WebLocalFrame* frame,
56 BufferedDataSourceHost* host)
57 : BufferedDataSource(url,
58 BufferedResourceLoader::kUnspecified,
59 task_runner,
60 frame,
61 new media::MediaLog(),
62 host,
63 base::Bind(&MockBufferedDataSource::set_downloading,
64 base::Unretained(this))),
65 downloading_(false),
66 loading_(false) {}
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,
76 last_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);
87 return 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; }
95 private:
96 // Whether the resource is downloading or deferred.
97 bool downloading_;
99 // Whether the resource load has starting loading but yet to been cancelled.
100 bool loading_;
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 {
113 public:
114 BufferedDataSourceTest()
115 : view_(WebView::create(NULL)),
116 frame_(WebLocalFrame::create(&client_)),
117 preload_(BufferedDataSource::AUTO) {
118 view_->setMainFrame(frame_);
121 virtual ~BufferedDataSourceTest() {
122 view_->close();
123 frame_->close();
126 MOCK_METHOD1(OnInitialize, void(bool));
128 void Initialize(const char* url, bool expected) {
129 GURL gurl(url);
130 data_source_.reset(
131 new MockBufferedDataSource(gurl,
132 message_loop_.message_loop_proxy(),
133 view_->mainFrame()->toWebLocalFrame(),
134 &host_));
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.
177 void Stop() {
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 ActiveLoader* active_loader() { return loader()->active_loader_.get(); }
227 WebURLLoader* url_loader() {
228 return loader()->active_loader_->loader_.get();
231 BufferedDataSource::Preload preload() { return data_source_->preload_; }
232 void set_preload(BufferedDataSource::Preload preload) { preload_ = preload; }
233 BufferedResourceLoader::DeferStrategy defer_strategy() {
234 return loader()->defer_strategy_;
236 int data_source_bitrate() { return data_source_->bitrate_; }
237 int data_source_playback_rate() { return data_source_->playback_rate_; }
238 int loader_bitrate() { return loader()->bitrate_; }
239 int loader_playback_rate() { return loader()->playback_rate_; }
240 bool is_local_source() { return data_source_->assume_fully_buffered(); }
241 void set_might_be_reused_from_cache_in_future(bool value) {
242 loader()->might_be_reused_from_cache_in_future_ = value;
245 scoped_ptr<MockBufferedDataSource> data_source_;
247 scoped_ptr<TestResponseGenerator> response_generator_;
248 MockWebFrameClient client_;
249 WebView* view_;
250 WebLocalFrame* frame_;
252 StrictMock<MockBufferedDataSourceHost> host_;
253 base::MessageLoop message_loop_;
255 private:
256 // Used for calling BufferedDataSource::Read().
257 uint8 buffer_[kDataSize];
259 BufferedDataSource::Preload preload_;
261 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
264 TEST_F(BufferedDataSourceTest, Range_Supported) {
265 InitializeWith206Response();
267 EXPECT_TRUE(data_source_->loading());
268 EXPECT_FALSE(data_source_->IsStreaming());
269 Stop();
272 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) {
273 Initialize(kHttpUrl, true);
275 Respond(response_generator_->Generate206(
276 0, TestResponseGenerator::kNoContentRangeInstanceSize));
278 EXPECT_TRUE(data_source_->loading());
279 EXPECT_TRUE(data_source_->IsStreaming());
280 Stop();
283 TEST_F(BufferedDataSourceTest, Range_NotFound) {
284 Initialize(kHttpUrl, false);
285 Respond(response_generator_->Generate404());
287 EXPECT_FALSE(data_source_->loading());
288 Stop();
291 TEST_F(BufferedDataSourceTest, Range_NotSupported) {
292 InitializeWith200Response();
294 EXPECT_TRUE(data_source_->loading());
295 EXPECT_TRUE(data_source_->IsStreaming());
296 Stop();
299 // Special carve-out for Apache versions that choose to return a 200 for
300 // Range:0- ("because it's more efficient" than a 206)
301 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) {
302 Initialize(kHttpUrl, true);
303 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
304 WebURLResponse response = response_generator_->Generate200();
305 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
306 WebString::fromUTF8("bytes"));
307 Respond(response);
309 EXPECT_TRUE(data_source_->loading());
310 EXPECT_FALSE(data_source_->IsStreaming());
311 Stop();
314 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) {
315 Initialize(kHttpUrl, false);
316 Respond(response_generator_->Generate206(
317 0, TestResponseGenerator::kNoContentRange));
319 EXPECT_FALSE(data_source_->loading());
320 Stop();
323 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) {
324 Initialize(kHttpUrl, true);
326 // It'll manage without a Content-Length response.
327 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
328 Respond(response_generator_->Generate206(
329 0, TestResponseGenerator::kNoContentLength));
331 EXPECT_TRUE(data_source_->loading());
332 EXPECT_FALSE(data_source_->IsStreaming());
333 Stop();
336 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) {
337 Initialize(kHttpUrl, false);
339 // Now it's done and will fail.
340 Respond(response_generator_->Generate206(1337));
342 EXPECT_FALSE(data_source_->loading());
343 Stop();
346 // Test the case where the initial response from the server indicates that
347 // Range requests are supported, but a later request prove otherwise.
348 TEST_F(BufferedDataSourceTest, Range_ServerLied) {
349 InitializeWith206Response();
351 // Read causing a new request to be made -- we'll expect it to error.
352 ExpectCreateResourceLoader();
353 ReadAt(kFarReadPosition);
355 // Return a 200 in response to a range request.
356 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
357 Respond(response_generator_->Generate200());
359 EXPECT_FALSE(data_source_->loading());
360 Stop();
363 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) {
364 InitializeWith206Response();
366 // Make sure there's a pending read -- we'll expect it to error.
367 ReadAt(0);
369 // Abort!!!
370 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
371 data_source_->Abort();
372 message_loop_.RunUntilIdle();
374 EXPECT_FALSE(data_source_->loading());
375 Stop();
378 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) {
379 InitializeWithFileResponse();
381 // Make sure there's a pending read -- we'll expect it to error.
382 ReadAt(0);
384 // Abort!!!
385 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
386 data_source_->Abort();
387 message_loop_.RunUntilIdle();
389 EXPECT_FALSE(data_source_->loading());
390 Stop();
393 TEST_F(BufferedDataSourceTest, Http_Retry) {
394 InitializeWith206Response();
396 // Read to advance our position.
397 EXPECT_CALL(*this, ReadCallback(kDataSize));
398 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
399 ReadAt(0);
400 ReceiveData(kDataSize);
402 // Issue a pending read but terminate the connection to force a retry.
403 ReadAt(kDataSize);
404 ExpectCreateResourceLoader();
405 FinishLoading();
406 Respond(response_generator_->Generate206(kDataSize));
408 // Complete the read.
409 EXPECT_CALL(*this, ReadCallback(kDataSize));
410 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
411 ReceiveData(kDataSize);
413 EXPECT_TRUE(data_source_->loading());
414 Stop();
417 TEST_F(BufferedDataSourceTest, Http_RetryOnError) {
418 InitializeWith206Response();
420 // Read to advance our position.
421 EXPECT_CALL(*this, ReadCallback(kDataSize));
422 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
423 ReadAt(0);
424 ReceiveData(kDataSize);
426 // Issue a pending read but trigger an error to force a retry.
427 EXPECT_CALL(*this, ReadCallback(kDataSize));
428 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
429 ReadAt(kDataSize);
430 base::RunLoop run_loop;
431 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
432 .WillOnce(
433 DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
434 Invoke(data_source_.get(),
435 &MockBufferedDataSource::CreateMockResourceLoader)));
436 loader()->didFail(url_loader(), response_generator_->GenerateError());
437 run_loop.Run();
438 Respond(response_generator_->Generate206(kDataSize));
439 ReceiveData(kDataSize);
440 FinishLoading();
441 EXPECT_FALSE(data_source_->loading());
442 Stop();
445 TEST_F(BufferedDataSourceTest, File_Retry) {
446 InitializeWithFileResponse();
448 // Read to advance our position.
449 EXPECT_CALL(*this, ReadCallback(kDataSize));
450 ReadAt(0);
451 ReceiveData(kDataSize);
453 // Issue a pending read but terminate the connection to force a retry.
454 ReadAt(kDataSize);
455 ExpectCreateResourceLoader();
456 FinishLoading();
457 Respond(response_generator_->GenerateFileResponse(kDataSize));
459 // Complete the read.
460 EXPECT_CALL(*this, ReadCallback(kDataSize));
461 ReceiveData(kDataSize);
463 EXPECT_TRUE(data_source_->loading());
464 Stop();
467 TEST_F(BufferedDataSourceTest, Http_TooManyRetries) {
468 InitializeWith206Response();
470 // Make sure there's a pending read -- we'll expect it to error.
471 ReadAt(0);
473 // It'll try three times.
474 ExpectCreateResourceLoader();
475 FinishLoading();
476 Respond(response_generator_->Generate206(0));
478 ExpectCreateResourceLoader();
479 FinishLoading();
480 Respond(response_generator_->Generate206(0));
482 ExpectCreateResourceLoader();
483 FinishLoading();
484 Respond(response_generator_->Generate206(0));
486 // It'll error after this.
487 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
488 FinishLoading();
490 EXPECT_FALSE(data_source_->loading());
491 Stop();
494 TEST_F(BufferedDataSourceTest, File_TooManyRetries) {
495 InitializeWithFileResponse();
497 // Make sure there's a pending read -- we'll expect it to error.
498 ReadAt(0);
500 // It'll try three times.
501 ExpectCreateResourceLoader();
502 FinishLoading();
503 Respond(response_generator_->GenerateFileResponse(0));
505 ExpectCreateResourceLoader();
506 FinishLoading();
507 Respond(response_generator_->GenerateFileResponse(0));
509 ExpectCreateResourceLoader();
510 FinishLoading();
511 Respond(response_generator_->GenerateFileResponse(0));
513 // It'll error after this.
514 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
515 FinishLoading();
517 EXPECT_FALSE(data_source_->loading());
518 Stop();
521 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) {
522 Initialize(kFileUrl, false);
523 EXPECT_FALSE(data_source_->downloading());
525 Respond(response_generator_->GenerateFileResponse(-1));
527 EXPECT_FALSE(data_source_->loading());
528 Stop();
531 TEST_F(BufferedDataSourceTest, File_Successful) {
532 InitializeWithFileResponse();
534 EXPECT_TRUE(data_source_->loading());
535 EXPECT_FALSE(data_source_->IsStreaming());
536 Stop();
539 TEST_F(BufferedDataSourceTest, StopDuringRead) {
540 InitializeWith206Response();
542 uint8 buffer[256];
543 data_source_->Read(0, arraysize(buffer), buffer, base::Bind(
544 &BufferedDataSourceTest::ReadCallback, base::Unretained(this)));
546 // The outstanding read should fail before the stop callback runs.
548 InSequence s;
549 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
550 data_source_->Stop();
552 message_loop_.RunUntilIdle();
555 TEST_F(BufferedDataSourceTest, DefaultValues) {
556 InitializeWith206Response();
558 // Ensure we have sane values for default loading scenario.
559 EXPECT_EQ(BufferedDataSource::AUTO, preload());
560 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
562 EXPECT_EQ(0, data_source_bitrate());
563 EXPECT_EQ(0.0f, data_source_playback_rate());
564 EXPECT_EQ(0, loader_bitrate());
565 EXPECT_EQ(0.0f, loader_playback_rate());
567 EXPECT_TRUE(data_source_->loading());
568 Stop();
571 TEST_F(BufferedDataSourceTest, SetBitrate) {
572 InitializeWith206Response();
574 data_source_->SetBitrate(1234);
575 message_loop_.RunUntilIdle();
576 EXPECT_EQ(1234, data_source_bitrate());
577 EXPECT_EQ(1234, loader_bitrate());
579 // Read so far ahead to cause the loader to get recreated.
580 BufferedResourceLoader* old_loader = loader();
581 ExpectCreateResourceLoader();
582 ReadAt(kFarReadPosition);
583 Respond(response_generator_->Generate206(kFarReadPosition));
585 // Verify loader changed but still has same bitrate.
586 EXPECT_NE(old_loader, loader());
587 EXPECT_EQ(1234, loader_bitrate());
589 EXPECT_TRUE(data_source_->loading());
590 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
591 Stop();
594 TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) {
595 InitializeWith206Response();
597 data_source_->MediaPlaybackRateChanged(2.0f);
598 message_loop_.RunUntilIdle();
599 EXPECT_EQ(2.0f, data_source_playback_rate());
600 EXPECT_EQ(2.0f, loader_playback_rate());
602 // Read so far ahead to cause the loader to get recreated.
603 BufferedResourceLoader* old_loader = loader();
604 ExpectCreateResourceLoader();
605 ReadAt(kFarReadPosition);
606 Respond(response_generator_->Generate206(kFarReadPosition));
608 // Verify loader changed but still has same playback rate.
609 EXPECT_NE(old_loader, loader());
611 EXPECT_TRUE(data_source_->loading());
612 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
613 Stop();
616 TEST_F(BufferedDataSourceTest, Http_Read) {
617 InitializeWith206Response();
619 ReadAt(0);
621 // Receive first half of the read.
622 EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1));
623 ReceiveData(kDataSize / 2);
625 // Receive last half of the read.
626 EXPECT_CALL(*this, ReadCallback(kDataSize));
627 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
628 ReceiveData(kDataSize / 2);
630 EXPECT_TRUE(data_source_->downloading());
631 Stop();
634 TEST_F(BufferedDataSourceTest, Http_Read_Seek) {
635 InitializeWith206Response();
637 // Read a bit from the beginning.
638 ReadAt(0);
639 EXPECT_CALL(*this, ReadCallback(kDataSize));
640 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
641 ReceiveData(kDataSize);
643 // Simulate a seek by reading a bit beyond kDataSize.
644 ReadAt(kDataSize * 2);
646 // We receive data leading up to but not including our read.
647 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1));
648 ReceiveData(kDataSize);
650 // We now receive the rest of the data for our read.
651 EXPECT_CALL(*this, ReadCallback(kDataSize));
652 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1));
653 ReceiveData(kDataSize);
655 EXPECT_TRUE(data_source_->downloading());
656 Stop();
659 TEST_F(BufferedDataSourceTest, File_Read) {
660 InitializeWithFileResponse();
662 ReadAt(0);
664 // Receive first half of the read but no buffering update.
665 ReceiveData(kDataSize / 2);
667 // Receive last half of the read but no buffering update.
668 EXPECT_CALL(*this, ReadCallback(kDataSize));
669 ReceiveData(kDataSize / 2);
671 Stop();
674 TEST_F(BufferedDataSourceTest, Http_FinishLoading) {
675 InitializeWith206Response();
677 EXPECT_TRUE(data_source_->downloading());
678 FinishLoading();
679 EXPECT_FALSE(data_source_->downloading());
681 Stop();
684 TEST_F(BufferedDataSourceTest, File_FinishLoading) {
685 InitializeWithFileResponse();
687 EXPECT_FALSE(data_source_->downloading());
688 FinishLoading();
689 EXPECT_FALSE(data_source_->downloading());
691 Stop();
694 TEST_F(BufferedDataSourceTest, LocalResource_DeferStrategy) {
695 InitializeWithFileResponse();
697 EXPECT_EQ(BufferedDataSource::AUTO, preload());
698 EXPECT_TRUE(is_local_source());
699 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
701 data_source_->MediaIsPlaying();
702 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
704 data_source_->MediaIsPaused();
705 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
707 Stop();
710 TEST_F(BufferedDataSourceTest, LocalResource_PreloadMetadata_DeferStrategy) {
711 set_preload(BufferedDataSource::METADATA);
712 InitializeWithFileResponse();
714 EXPECT_EQ(BufferedDataSource::METADATA, preload());
715 EXPECT_TRUE(is_local_source());
716 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
718 data_source_->MediaIsPlaying();
719 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
721 data_source_->MediaIsPaused();
722 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
724 Stop();
727 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse200_DeferStrategy) {
728 InitializeWith200Response();
730 EXPECT_EQ(BufferedDataSource::AUTO, preload());
731 EXPECT_FALSE(is_local_source());
732 EXPECT_FALSE(loader()->range_supported());
733 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
735 data_source_->MediaIsPlaying();
736 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
738 data_source_->MediaIsPaused();
739 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
741 Stop();
744 TEST_F(BufferedDataSourceTest,
745 ExternalResource_Response200_PreloadMetadata_DeferStrategy) {
746 set_preload(BufferedDataSource::METADATA);
747 InitializeWith200Response();
749 EXPECT_EQ(BufferedDataSource::METADATA, preload());
750 EXPECT_FALSE(is_local_source());
751 EXPECT_FALSE(loader()->range_supported());
752 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
754 data_source_->MediaIsPlaying();
755 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
757 data_source_->MediaIsPaused();
758 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
760 Stop();
763 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse206_DeferStrategy) {
764 InitializeWith206Response();
766 EXPECT_EQ(BufferedDataSource::AUTO, preload());
767 EXPECT_FALSE(is_local_source());
768 EXPECT_TRUE(loader()->range_supported());
769 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
771 data_source_->MediaIsPlaying();
772 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
773 set_might_be_reused_from_cache_in_future(true);
774 data_source_->MediaIsPaused();
775 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
777 data_source_->MediaIsPlaying();
778 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
779 set_might_be_reused_from_cache_in_future(false);
780 data_source_->MediaIsPaused();
781 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
783 Stop();
786 TEST_F(BufferedDataSourceTest,
787 ExternalResource_Response206_PreloadMetadata_DeferStrategy) {
788 set_preload(BufferedDataSource::METADATA);
789 InitializeWith206Response();
791 EXPECT_EQ(BufferedDataSource::METADATA, preload());
792 EXPECT_FALSE(is_local_source());
793 EXPECT_TRUE(loader()->range_supported());
794 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
796 data_source_->MediaIsPlaying();
797 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
798 set_might_be_reused_from_cache_in_future(true);
799 data_source_->MediaIsPaused();
800 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
802 data_source_->MediaIsPlaying();
803 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
804 set_might_be_reused_from_cache_in_future(false);
805 data_source_->MediaIsPaused();
806 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
808 Stop();
811 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_VerifyDefer) {
812 set_preload(BufferedDataSource::METADATA);
813 InitializeWith206Response();
815 EXPECT_EQ(BufferedDataSource::METADATA, preload());
816 EXPECT_FALSE(is_local_source());
817 EXPECT_TRUE(loader()->range_supported());
818 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
820 // Read a bit from the beginning.
821 ReadAt(0);
822 EXPECT_CALL(*this, ReadCallback(kDataSize));
823 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
824 ReceiveData(kDataSize);
826 ASSERT_TRUE(active_loader());
827 EXPECT_TRUE(active_loader()->deferred());
830 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_CancelAfterDefer) {
831 set_preload(BufferedDataSource::METADATA);
832 InitializeWith206Response();
834 EXPECT_EQ(BufferedDataSource::METADATA, preload());
835 EXPECT_FALSE(is_local_source());
836 EXPECT_TRUE(loader()->range_supported());
837 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
839 data_source_->OnBufferingHaveEnough();
841 ASSERT_TRUE(active_loader());
843 // Read a bit from the beginning.
844 ReadAt(0);
845 EXPECT_CALL(*this, ReadCallback(kDataSize));
846 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
847 ReceiveData(kDataSize);
849 EXPECT_FALSE(active_loader());
852 } // namespace media