Files.app: Remove wait for startup animation.
[chromium-blink-merge.git] / media / blink / buffered_data_source_unittest.cc
blobb2716ac5940a569b83678a439f5f96b192182812
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_(
117 WebLocalFrame::create(blink::WebTreeScopeType::Document, &client_)),
118 preload_(BufferedDataSource::AUTO) {
119 view_->setMainFrame(frame_);
122 virtual ~BufferedDataSourceTest() {
123 view_->close();
124 frame_->close();
127 MOCK_METHOD1(OnInitialize, void(bool));
129 void Initialize(const char* url, bool expected) {
130 GURL gurl(url);
131 data_source_.reset(
132 new MockBufferedDataSource(gurl,
133 message_loop_.task_runner(),
134 view_->mainFrame()->toWebLocalFrame(),
135 &host_));
136 data_source_->SetPreload(preload_);
138 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize));
139 ExpectCreateResourceLoader();
140 EXPECT_CALL(*this, OnInitialize(expected));
141 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize,
142 base::Unretained(this)));
143 message_loop_.RunUntilIdle();
145 bool is_http = gurl.SchemeIsHTTPOrHTTPS();
146 EXPECT_EQ(data_source_->downloading(), is_http);
149 // Helper to initialize tests with a valid 200 response.
150 void InitializeWith200Response() {
151 Initialize(kHttpUrl, true);
153 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
154 Respond(response_generator_->Generate200());
157 // Helper to initialize tests with a valid 206 response.
158 void InitializeWith206Response() {
159 Initialize(kHttpUrl, true);
161 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
162 Respond(response_generator_->Generate206(0));
165 // Helper to initialize tests with a valid file:// response.
166 void InitializeWithFileResponse() {
167 Initialize(kFileUrl, true);
169 EXPECT_CALL(host_, SetTotalBytes(kFileSize));
170 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize));
171 Respond(response_generator_->GenerateFileResponse(0));
174 // Stops any active loaders and shuts down the data source.
176 // This typically happens when the page is closed and for our purposes is
177 // appropriate to do when tearing down a test.
178 void Stop() {
179 if (data_source_->loading()) {
180 loader()->didFail(url_loader(), response_generator_->GenerateError());
181 message_loop_.RunUntilIdle();
184 data_source_->Stop();
185 message_loop_.RunUntilIdle();
188 void ExpectCreateResourceLoader() {
189 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
190 .WillOnce(Invoke(data_source_.get(),
191 &MockBufferedDataSource::CreateMockResourceLoader));
192 message_loop_.RunUntilIdle();
195 void Respond(const WebURLResponse& response) {
196 loader()->didReceiveResponse(url_loader(), response);
197 message_loop_.RunUntilIdle();
200 void ReceiveData(int size) {
201 scoped_ptr<char[]> data(new char[size]);
202 memset(data.get(), 0xA5, size); // Arbitrary non-zero value.
204 loader()->didReceiveData(url_loader(), data.get(), size, size);
205 message_loop_.RunUntilIdle();
208 void FinishLoading() {
209 data_source_->set_loading(false);
210 loader()->didFinishLoading(url_loader(), 0, -1);
211 message_loop_.RunUntilIdle();
214 MOCK_METHOD1(ReadCallback, void(int size));
216 void ReadAt(int64 position) {
217 data_source_->Read(position, kDataSize, buffer_,
218 base::Bind(&BufferedDataSourceTest::ReadCallback,
219 base::Unretained(this)));
220 message_loop_.RunUntilIdle();
223 // Accessors for private variables on |data_source_|.
224 BufferedResourceLoader* loader() {
225 return data_source_->loader_.get();
227 ActiveLoader* active_loader() { return loader()->active_loader_.get(); }
228 WebURLLoader* url_loader() {
229 return loader()->active_loader_->loader_.get();
232 BufferedDataSource::Preload preload() { return data_source_->preload_; }
233 void set_preload(BufferedDataSource::Preload preload) { preload_ = preload; }
234 BufferedResourceLoader::DeferStrategy defer_strategy() {
235 return loader()->defer_strategy_;
237 int data_source_bitrate() { return data_source_->bitrate_; }
238 double data_source_playback_rate() { return data_source_->playback_rate_; }
239 int loader_bitrate() { return loader()->bitrate_; }
240 double loader_playback_rate() { return loader()->playback_rate_; }
241 bool is_local_source() { return data_source_->assume_fully_buffered(); }
242 void set_might_be_reused_from_cache_in_future(bool value) {
243 loader()->might_be_reused_from_cache_in_future_ = value;
246 scoped_ptr<MockBufferedDataSource> data_source_;
248 scoped_ptr<TestResponseGenerator> response_generator_;
249 MockWebFrameClient client_;
250 WebView* view_;
251 WebLocalFrame* frame_;
253 StrictMock<MockBufferedDataSourceHost> host_;
254 base::MessageLoop message_loop_;
256 private:
257 // Used for calling BufferedDataSource::Read().
258 uint8 buffer_[kDataSize];
260 BufferedDataSource::Preload preload_;
262 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
265 TEST_F(BufferedDataSourceTest, Range_Supported) {
266 InitializeWith206Response();
268 EXPECT_TRUE(data_source_->loading());
269 EXPECT_FALSE(data_source_->IsStreaming());
270 Stop();
273 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) {
274 Initialize(kHttpUrl, true);
276 Respond(response_generator_->Generate206(
277 0, TestResponseGenerator::kNoContentRangeInstanceSize));
279 EXPECT_TRUE(data_source_->loading());
280 EXPECT_TRUE(data_source_->IsStreaming());
281 Stop();
284 TEST_F(BufferedDataSourceTest, Range_NotFound) {
285 Initialize(kHttpUrl, false);
286 Respond(response_generator_->Generate404());
288 EXPECT_FALSE(data_source_->loading());
289 Stop();
292 TEST_F(BufferedDataSourceTest, Range_NotSupported) {
293 InitializeWith200Response();
295 EXPECT_TRUE(data_source_->loading());
296 EXPECT_TRUE(data_source_->IsStreaming());
297 Stop();
300 // Special carve-out for Apache versions that choose to return a 200 for
301 // Range:0- ("because it's more efficient" than a 206)
302 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) {
303 Initialize(kHttpUrl, true);
304 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
305 WebURLResponse response = response_generator_->Generate200();
306 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
307 WebString::fromUTF8("bytes"));
308 Respond(response);
310 EXPECT_TRUE(data_source_->loading());
311 EXPECT_FALSE(data_source_->IsStreaming());
312 Stop();
315 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) {
316 Initialize(kHttpUrl, false);
317 Respond(response_generator_->Generate206(
318 0, TestResponseGenerator::kNoContentRange));
320 EXPECT_FALSE(data_source_->loading());
321 Stop();
324 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) {
325 Initialize(kHttpUrl, true);
327 // It'll manage without a Content-Length response.
328 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
329 Respond(response_generator_->Generate206(
330 0, TestResponseGenerator::kNoContentLength));
332 EXPECT_TRUE(data_source_->loading());
333 EXPECT_FALSE(data_source_->IsStreaming());
334 Stop();
337 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) {
338 Initialize(kHttpUrl, false);
340 // Now it's done and will fail.
341 Respond(response_generator_->Generate206(1337));
343 EXPECT_FALSE(data_source_->loading());
344 Stop();
347 // Test the case where the initial response from the server indicates that
348 // Range requests are supported, but a later request prove otherwise.
349 TEST_F(BufferedDataSourceTest, Range_ServerLied) {
350 InitializeWith206Response();
352 // Read causing a new request to be made -- we'll expect it to error.
353 ExpectCreateResourceLoader();
354 ReadAt(kFarReadPosition);
356 // Return a 200 in response to a range request.
357 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
358 Respond(response_generator_->Generate200());
360 EXPECT_FALSE(data_source_->loading());
361 Stop();
364 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) {
365 InitializeWith206Response();
367 // Make sure there's a pending read -- we'll expect it to error.
368 ReadAt(0);
370 // Abort!!!
371 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
372 data_source_->Abort();
373 message_loop_.RunUntilIdle();
375 EXPECT_FALSE(data_source_->loading());
376 Stop();
379 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) {
380 InitializeWithFileResponse();
382 // Make sure there's a pending read -- we'll expect it to error.
383 ReadAt(0);
385 // Abort!!!
386 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
387 data_source_->Abort();
388 message_loop_.RunUntilIdle();
390 EXPECT_FALSE(data_source_->loading());
391 Stop();
394 TEST_F(BufferedDataSourceTest, Http_Retry) {
395 InitializeWith206Response();
397 // Read to advance our position.
398 EXPECT_CALL(*this, ReadCallback(kDataSize));
399 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
400 ReadAt(0);
401 ReceiveData(kDataSize);
403 // Issue a pending read but terminate the connection to force a retry.
404 ReadAt(kDataSize);
405 ExpectCreateResourceLoader();
406 FinishLoading();
407 Respond(response_generator_->Generate206(kDataSize));
409 // Complete the read.
410 EXPECT_CALL(*this, ReadCallback(kDataSize));
411 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
412 ReceiveData(kDataSize);
414 EXPECT_TRUE(data_source_->loading());
415 Stop();
418 TEST_F(BufferedDataSourceTest, Http_RetryOnError) {
419 InitializeWith206Response();
421 // Read to advance our position.
422 EXPECT_CALL(*this, ReadCallback(kDataSize));
423 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
424 ReadAt(0);
425 ReceiveData(kDataSize);
427 // Issue a pending read but trigger an error to force a retry.
428 EXPECT_CALL(*this, ReadCallback(kDataSize));
429 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
430 ReadAt(kDataSize);
431 base::RunLoop run_loop;
432 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
433 .WillOnce(
434 DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
435 Invoke(data_source_.get(),
436 &MockBufferedDataSource::CreateMockResourceLoader)));
437 loader()->didFail(url_loader(), response_generator_->GenerateError());
438 run_loop.Run();
439 Respond(response_generator_->Generate206(kDataSize));
440 ReceiveData(kDataSize);
441 FinishLoading();
442 EXPECT_FALSE(data_source_->loading());
443 Stop();
446 TEST_F(BufferedDataSourceTest, File_Retry) {
447 InitializeWithFileResponse();
449 // Read to advance our position.
450 EXPECT_CALL(*this, ReadCallback(kDataSize));
451 ReadAt(0);
452 ReceiveData(kDataSize);
454 // Issue a pending read but terminate the connection to force a retry.
455 ReadAt(kDataSize);
456 ExpectCreateResourceLoader();
457 FinishLoading();
458 Respond(response_generator_->GenerateFileResponse(kDataSize));
460 // Complete the read.
461 EXPECT_CALL(*this, ReadCallback(kDataSize));
462 ReceiveData(kDataSize);
464 EXPECT_TRUE(data_source_->loading());
465 Stop();
468 TEST_F(BufferedDataSourceTest, Http_TooManyRetries) {
469 InitializeWith206Response();
471 // Make sure there's a pending read -- we'll expect it to error.
472 ReadAt(0);
474 // It'll try three times.
475 ExpectCreateResourceLoader();
476 FinishLoading();
477 Respond(response_generator_->Generate206(0));
479 ExpectCreateResourceLoader();
480 FinishLoading();
481 Respond(response_generator_->Generate206(0));
483 ExpectCreateResourceLoader();
484 FinishLoading();
485 Respond(response_generator_->Generate206(0));
487 // It'll error after this.
488 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
489 FinishLoading();
491 EXPECT_FALSE(data_source_->loading());
492 Stop();
495 TEST_F(BufferedDataSourceTest, File_TooManyRetries) {
496 InitializeWithFileResponse();
498 // Make sure there's a pending read -- we'll expect it to error.
499 ReadAt(0);
501 // It'll try three times.
502 ExpectCreateResourceLoader();
503 FinishLoading();
504 Respond(response_generator_->GenerateFileResponse(0));
506 ExpectCreateResourceLoader();
507 FinishLoading();
508 Respond(response_generator_->GenerateFileResponse(0));
510 ExpectCreateResourceLoader();
511 FinishLoading();
512 Respond(response_generator_->GenerateFileResponse(0));
514 // It'll error after this.
515 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
516 FinishLoading();
518 EXPECT_FALSE(data_source_->loading());
519 Stop();
522 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) {
523 Initialize(kFileUrl, false);
524 EXPECT_FALSE(data_source_->downloading());
526 Respond(response_generator_->GenerateFileResponse(-1));
528 EXPECT_FALSE(data_source_->loading());
529 Stop();
532 TEST_F(BufferedDataSourceTest, File_Successful) {
533 InitializeWithFileResponse();
535 EXPECT_TRUE(data_source_->loading());
536 EXPECT_FALSE(data_source_->IsStreaming());
537 Stop();
540 TEST_F(BufferedDataSourceTest, StopDuringRead) {
541 InitializeWith206Response();
543 uint8 buffer[256];
544 data_source_->Read(0, arraysize(buffer), buffer, base::Bind(
545 &BufferedDataSourceTest::ReadCallback, base::Unretained(this)));
547 // The outstanding read should fail before the stop callback runs.
549 InSequence s;
550 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
551 data_source_->Stop();
553 message_loop_.RunUntilIdle();
556 TEST_F(BufferedDataSourceTest, DefaultValues) {
557 InitializeWith206Response();
559 // Ensure we have sane values for default loading scenario.
560 EXPECT_EQ(BufferedDataSource::AUTO, preload());
561 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
563 EXPECT_EQ(0, data_source_bitrate());
564 EXPECT_EQ(0.0, data_source_playback_rate());
565 EXPECT_EQ(0, loader_bitrate());
566 EXPECT_EQ(0.0, loader_playback_rate());
568 EXPECT_TRUE(data_source_->loading());
569 Stop();
572 TEST_F(BufferedDataSourceTest, SetBitrate) {
573 InitializeWith206Response();
575 data_source_->SetBitrate(1234);
576 message_loop_.RunUntilIdle();
577 EXPECT_EQ(1234, data_source_bitrate());
578 EXPECT_EQ(1234, loader_bitrate());
580 // Read so far ahead to cause the loader to get recreated.
581 BufferedResourceLoader* old_loader = loader();
582 ExpectCreateResourceLoader();
583 ReadAt(kFarReadPosition);
584 Respond(response_generator_->Generate206(kFarReadPosition));
586 // Verify loader changed but still has same bitrate.
587 EXPECT_NE(old_loader, loader());
588 EXPECT_EQ(1234, loader_bitrate());
590 EXPECT_TRUE(data_source_->loading());
591 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
592 Stop();
595 TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) {
596 InitializeWith206Response();
598 data_source_->MediaPlaybackRateChanged(2.0);
599 message_loop_.RunUntilIdle();
600 EXPECT_EQ(2.0, data_source_playback_rate());
601 EXPECT_EQ(2.0, loader_playback_rate());
603 // Read so far ahead to cause the loader to get recreated.
604 BufferedResourceLoader* old_loader = loader();
605 ExpectCreateResourceLoader();
606 ReadAt(kFarReadPosition);
607 Respond(response_generator_->Generate206(kFarReadPosition));
609 // Verify loader changed but still has same playback rate.
610 EXPECT_NE(old_loader, loader());
612 EXPECT_TRUE(data_source_->loading());
613 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
614 Stop();
617 TEST_F(BufferedDataSourceTest, Http_Read) {
618 InitializeWith206Response();
620 ReadAt(0);
622 // Receive first half of the read.
623 EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1));
624 ReceiveData(kDataSize / 2);
626 // Receive last half of the read.
627 EXPECT_CALL(*this, ReadCallback(kDataSize));
628 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
629 ReceiveData(kDataSize / 2);
631 EXPECT_TRUE(data_source_->downloading());
632 Stop();
635 TEST_F(BufferedDataSourceTest, Http_Read_Seek) {
636 InitializeWith206Response();
638 // Read a bit from the beginning.
639 ReadAt(0);
640 EXPECT_CALL(*this, ReadCallback(kDataSize));
641 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
642 ReceiveData(kDataSize);
644 // Simulate a seek by reading a bit beyond kDataSize.
645 ReadAt(kDataSize * 2);
647 // We receive data leading up to but not including our read.
648 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1));
649 ReceiveData(kDataSize);
651 // We now receive the rest of the data for our read.
652 EXPECT_CALL(*this, ReadCallback(kDataSize));
653 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1));
654 ReceiveData(kDataSize);
656 EXPECT_TRUE(data_source_->downloading());
657 Stop();
660 TEST_F(BufferedDataSourceTest, File_Read) {
661 InitializeWithFileResponse();
663 ReadAt(0);
665 // Receive first half of the read but no buffering update.
666 ReceiveData(kDataSize / 2);
668 // Receive last half of the read but no buffering update.
669 EXPECT_CALL(*this, ReadCallback(kDataSize));
670 ReceiveData(kDataSize / 2);
672 Stop();
675 TEST_F(BufferedDataSourceTest, Http_FinishLoading) {
676 InitializeWith206Response();
678 EXPECT_TRUE(data_source_->downloading());
679 FinishLoading();
680 EXPECT_FALSE(data_source_->downloading());
682 Stop();
685 TEST_F(BufferedDataSourceTest, File_FinishLoading) {
686 InitializeWithFileResponse();
688 EXPECT_FALSE(data_source_->downloading());
689 FinishLoading();
690 EXPECT_FALSE(data_source_->downloading());
692 Stop();
695 TEST_F(BufferedDataSourceTest, LocalResource_DeferStrategy) {
696 InitializeWithFileResponse();
698 EXPECT_EQ(BufferedDataSource::AUTO, preload());
699 EXPECT_TRUE(is_local_source());
700 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
702 data_source_->MediaIsPlaying();
703 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
705 data_source_->MediaIsPaused();
706 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
708 Stop();
711 TEST_F(BufferedDataSourceTest, LocalResource_PreloadMetadata_DeferStrategy) {
712 set_preload(BufferedDataSource::METADATA);
713 InitializeWithFileResponse();
715 EXPECT_EQ(BufferedDataSource::METADATA, preload());
716 EXPECT_TRUE(is_local_source());
717 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
719 data_source_->MediaIsPlaying();
720 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
722 data_source_->MediaIsPaused();
723 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
725 Stop();
728 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse200_DeferStrategy) {
729 InitializeWith200Response();
731 EXPECT_EQ(BufferedDataSource::AUTO, preload());
732 EXPECT_FALSE(is_local_source());
733 EXPECT_FALSE(loader()->range_supported());
734 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
736 data_source_->MediaIsPlaying();
737 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
739 data_source_->MediaIsPaused();
740 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
742 Stop();
745 TEST_F(BufferedDataSourceTest,
746 ExternalResource_Response200_PreloadMetadata_DeferStrategy) {
747 set_preload(BufferedDataSource::METADATA);
748 InitializeWith200Response();
750 EXPECT_EQ(BufferedDataSource::METADATA, preload());
751 EXPECT_FALSE(is_local_source());
752 EXPECT_FALSE(loader()->range_supported());
753 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
755 data_source_->MediaIsPlaying();
756 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
758 data_source_->MediaIsPaused();
759 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
761 Stop();
764 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse206_DeferStrategy) {
765 InitializeWith206Response();
767 EXPECT_EQ(BufferedDataSource::AUTO, preload());
768 EXPECT_FALSE(is_local_source());
769 EXPECT_TRUE(loader()->range_supported());
770 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
772 data_source_->MediaIsPlaying();
773 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
774 set_might_be_reused_from_cache_in_future(true);
775 data_source_->MediaIsPaused();
776 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
778 data_source_->MediaIsPlaying();
779 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
780 set_might_be_reused_from_cache_in_future(false);
781 data_source_->MediaIsPaused();
782 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
784 Stop();
787 TEST_F(BufferedDataSourceTest,
788 ExternalResource_Response206_PreloadMetadata_DeferStrategy) {
789 set_preload(BufferedDataSource::METADATA);
790 InitializeWith206Response();
792 EXPECT_EQ(BufferedDataSource::METADATA, preload());
793 EXPECT_FALSE(is_local_source());
794 EXPECT_TRUE(loader()->range_supported());
795 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
797 data_source_->MediaIsPlaying();
798 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
799 set_might_be_reused_from_cache_in_future(true);
800 data_source_->MediaIsPaused();
801 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
803 data_source_->MediaIsPlaying();
804 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
805 set_might_be_reused_from_cache_in_future(false);
806 data_source_->MediaIsPaused();
807 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
809 Stop();
812 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_VerifyDefer) {
813 set_preload(BufferedDataSource::METADATA);
814 InitializeWith206Response();
816 EXPECT_EQ(BufferedDataSource::METADATA, preload());
817 EXPECT_FALSE(is_local_source());
818 EXPECT_TRUE(loader()->range_supported());
819 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
821 // Read a bit from the beginning.
822 ReadAt(0);
823 EXPECT_CALL(*this, ReadCallback(kDataSize));
824 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
825 ReceiveData(kDataSize);
827 ASSERT_TRUE(active_loader());
828 EXPECT_TRUE(active_loader()->deferred());
831 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_CancelAfterDefer) {
832 set_preload(BufferedDataSource::METADATA);
833 InitializeWith206Response();
835 EXPECT_EQ(BufferedDataSource::METADATA, preload());
836 EXPECT_FALSE(is_local_source());
837 EXPECT_TRUE(loader()->range_supported());
838 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
840 data_source_->OnBufferingHaveEnough();
842 ASSERT_TRUE(active_loader());
844 // Read a bit from the beginning.
845 ReadAt(0);
846 EXPECT_CALL(*this, ReadCallback(kDataSize));
847 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
848 ReceiveData(kDataSize);
850 EXPECT_FALSE(active_loader());
853 } // namespace media