Use md5_check in proguard.py to detect when it's not really necessary
[chromium-blink-merge.git] / media / blink / buffered_data_source_unittest.cc
blob878d5f7ea007d663509dca2b131239f303645ad0
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";
111 static const char kHttpDifferentPathUrl[] = "http://localhost/bar.webm";
112 static const char kHttpDifferentOriginUrl[] = "http://127.0.0.1/foo.webm";
114 class BufferedDataSourceTest : public testing::Test {
115 public:
116 BufferedDataSourceTest()
117 : view_(WebView::create(NULL)),
118 frame_(
119 WebLocalFrame::create(blink::WebTreeScopeType::Document, &client_)),
120 preload_(BufferedDataSource::AUTO) {
121 view_->setMainFrame(frame_);
124 virtual ~BufferedDataSourceTest() {
125 view_->close();
126 frame_->close();
129 MOCK_METHOD1(OnInitialize, void(bool));
131 void Initialize(const char* url, bool expected) {
132 GURL gurl(url);
133 data_source_.reset(
134 new MockBufferedDataSource(gurl,
135 message_loop_.task_runner(),
136 view_->mainFrame()->toWebLocalFrame(),
137 &host_));
138 data_source_->SetPreload(preload_);
140 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize));
141 ExpectCreateResourceLoader();
142 EXPECT_CALL(*this, OnInitialize(expected));
143 data_source_->Initialize(base::Bind(&BufferedDataSourceTest::OnInitialize,
144 base::Unretained(this)));
145 message_loop_.RunUntilIdle();
147 bool is_http = gurl.SchemeIsHTTPOrHTTPS();
148 EXPECT_EQ(data_source_->downloading(), is_http);
151 // Helper to initialize tests with a valid 200 response.
152 void InitializeWith200Response() {
153 Initialize(kHttpUrl, true);
155 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
156 Respond(response_generator_->Generate200());
159 // Helper to initialize tests with a valid 206 response.
160 void InitializeWith206Response() {
161 Initialize(kHttpUrl, true);
163 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
164 Respond(response_generator_->Generate206(0));
167 // Helper to initialize tests with a valid file:// response.
168 void InitializeWithFileResponse() {
169 Initialize(kFileUrl, true);
171 EXPECT_CALL(host_, SetTotalBytes(kFileSize));
172 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize));
173 Respond(response_generator_->GenerateFileResponse(0));
176 // Stops any active loaders and shuts down the data source.
178 // This typically happens when the page is closed and for our purposes is
179 // appropriate to do when tearing down a test.
180 void Stop() {
181 if (data_source_->loading()) {
182 loader()->didFail(url_loader(), response_generator_->GenerateError());
183 message_loop_.RunUntilIdle();
186 data_source_->Stop();
187 message_loop_.RunUntilIdle();
190 void ExpectCreateResourceLoader() {
191 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
192 .WillOnce(Invoke(data_source_.get(),
193 &MockBufferedDataSource::CreateMockResourceLoader));
194 message_loop_.RunUntilIdle();
197 void Respond(const WebURLResponse& response) {
198 loader()->didReceiveResponse(url_loader(), response);
199 message_loop_.RunUntilIdle();
202 void ReceiveData(int size) {
203 scoped_ptr<char[]> data(new char[size]);
204 memset(data.get(), 0xA5, size); // Arbitrary non-zero value.
206 loader()->didReceiveData(url_loader(), data.get(), size, size);
207 message_loop_.RunUntilIdle();
210 void FinishLoading() {
211 data_source_->set_loading(false);
212 loader()->didFinishLoading(url_loader(), 0, -1);
213 message_loop_.RunUntilIdle();
216 MOCK_METHOD1(ReadCallback, void(int size));
218 void ReadAt(int64 position) {
219 data_source_->Read(position, kDataSize, buffer_,
220 base::Bind(&BufferedDataSourceTest::ReadCallback,
221 base::Unretained(this)));
222 message_loop_.RunUntilIdle();
225 void ExecuteMixedResponseSuccessTest(const WebURLResponse& response1,
226 const WebURLResponse& response2) {
227 EXPECT_CALL(host_, SetTotalBytes(kFileSize));
228 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, kDataSize * 2 - 1));
229 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
230 EXPECT_CALL(*this, ReadCallback(kDataSize)).Times(2);
232 Respond(response1);
233 ReadAt(0);
234 ReceiveData(kDataSize);
235 EXPECT_TRUE(data_source_->loading());
237 ExpectCreateResourceLoader();
238 FinishLoading();
239 ReadAt(kDataSize);
240 Respond(response2);
241 ReceiveData(kDataSize);
242 FinishLoading();
243 Stop();
246 void ExecuteMixedResponseFailureTest(const WebURLResponse& response1,
247 const WebURLResponse& response2) {
248 EXPECT_CALL(host_, SetTotalBytes(kFileSize));
249 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
250 EXPECT_CALL(*this, ReadCallback(kDataSize));
251 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
253 Respond(response1);
254 ReadAt(0);
255 ReceiveData(kDataSize);
256 EXPECT_TRUE(data_source_->loading());
258 ExpectCreateResourceLoader();
259 FinishLoading();
260 ReadAt(kDataSize);
261 Respond(response2);
262 Stop();
265 // Accessors for private variables on |data_source_|.
266 BufferedResourceLoader* loader() {
267 return data_source_->loader_.get();
269 ActiveLoader* active_loader() { return loader()->active_loader_.get(); }
270 WebURLLoader* url_loader() {
271 return loader()->active_loader_->loader_.get();
274 BufferedDataSource::Preload preload() { return data_source_->preload_; }
275 void set_preload(BufferedDataSource::Preload preload) { preload_ = preload; }
276 BufferedResourceLoader::DeferStrategy defer_strategy() {
277 return loader()->defer_strategy_;
279 int data_source_bitrate() { return data_source_->bitrate_; }
280 double data_source_playback_rate() { return data_source_->playback_rate_; }
281 int loader_bitrate() { return loader()->bitrate_; }
282 double loader_playback_rate() { return loader()->playback_rate_; }
283 bool is_local_source() { return data_source_->assume_fully_buffered(); }
284 void set_might_be_reused_from_cache_in_future(bool value) {
285 loader()->might_be_reused_from_cache_in_future_ = value;
288 scoped_ptr<MockBufferedDataSource> data_source_;
290 scoped_ptr<TestResponseGenerator> response_generator_;
291 MockWebFrameClient client_;
292 WebView* view_;
293 WebLocalFrame* frame_;
295 StrictMock<MockBufferedDataSourceHost> host_;
296 base::MessageLoop message_loop_;
298 private:
299 // Used for calling BufferedDataSource::Read().
300 uint8 buffer_[kDataSize];
302 BufferedDataSource::Preload preload_;
304 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
307 TEST_F(BufferedDataSourceTest, Range_Supported) {
308 InitializeWith206Response();
310 EXPECT_TRUE(data_source_->loading());
311 EXPECT_FALSE(data_source_->IsStreaming());
312 Stop();
315 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) {
316 Initialize(kHttpUrl, true);
318 Respond(response_generator_->Generate206(
319 0, TestResponseGenerator::kNoContentRangeInstanceSize));
321 EXPECT_TRUE(data_source_->loading());
322 EXPECT_TRUE(data_source_->IsStreaming());
323 Stop();
326 TEST_F(BufferedDataSourceTest, Range_NotFound) {
327 Initialize(kHttpUrl, false);
328 Respond(response_generator_->Generate404());
330 EXPECT_FALSE(data_source_->loading());
331 Stop();
334 TEST_F(BufferedDataSourceTest, Range_NotSupported) {
335 InitializeWith200Response();
337 EXPECT_TRUE(data_source_->loading());
338 EXPECT_TRUE(data_source_->IsStreaming());
339 Stop();
342 // Special carve-out for Apache versions that choose to return a 200 for
343 // Range:0- ("because it's more efficient" than a 206)
344 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) {
345 Initialize(kHttpUrl, true);
346 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
347 WebURLResponse response = response_generator_->Generate200();
348 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
349 WebString::fromUTF8("bytes"));
350 Respond(response);
352 EXPECT_TRUE(data_source_->loading());
353 EXPECT_FALSE(data_source_->IsStreaming());
354 Stop();
357 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) {
358 Initialize(kHttpUrl, false);
359 Respond(response_generator_->Generate206(
360 0, TestResponseGenerator::kNoContentRange));
362 EXPECT_FALSE(data_source_->loading());
363 Stop();
366 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) {
367 Initialize(kHttpUrl, true);
369 // It'll manage without a Content-Length response.
370 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
371 Respond(response_generator_->Generate206(
372 0, TestResponseGenerator::kNoContentLength));
374 EXPECT_TRUE(data_source_->loading());
375 EXPECT_FALSE(data_source_->IsStreaming());
376 Stop();
379 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) {
380 Initialize(kHttpUrl, false);
382 // Now it's done and will fail.
383 Respond(response_generator_->Generate206(1337));
385 EXPECT_FALSE(data_source_->loading());
386 Stop();
389 // Test the case where the initial response from the server indicates that
390 // Range requests are supported, but a later request prove otherwise.
391 TEST_F(BufferedDataSourceTest, Range_ServerLied) {
392 InitializeWith206Response();
394 // Read causing a new request to be made -- we'll expect it to error.
395 ExpectCreateResourceLoader();
396 ReadAt(kFarReadPosition);
398 // Return a 200 in response to a range request.
399 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
400 Respond(response_generator_->Generate200());
402 EXPECT_FALSE(data_source_->loading());
403 Stop();
406 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) {
407 InitializeWith206Response();
409 // Make sure there's a pending read -- we'll expect it to error.
410 ReadAt(0);
412 // Abort!!!
413 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
414 data_source_->Abort();
415 message_loop_.RunUntilIdle();
417 EXPECT_FALSE(data_source_->loading());
418 Stop();
421 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) {
422 InitializeWithFileResponse();
424 // Make sure there's a pending read -- we'll expect it to error.
425 ReadAt(0);
427 // Abort!!!
428 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
429 data_source_->Abort();
430 message_loop_.RunUntilIdle();
432 EXPECT_FALSE(data_source_->loading());
433 Stop();
436 TEST_F(BufferedDataSourceTest, Http_Retry) {
437 InitializeWith206Response();
439 // Read to advance our position.
440 EXPECT_CALL(*this, ReadCallback(kDataSize));
441 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
442 ReadAt(0);
443 ReceiveData(kDataSize);
445 // Issue a pending read but terminate the connection to force a retry.
446 ReadAt(kDataSize);
447 ExpectCreateResourceLoader();
448 FinishLoading();
449 Respond(response_generator_->Generate206(kDataSize));
451 // Complete the read.
452 EXPECT_CALL(*this, ReadCallback(kDataSize));
453 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
454 ReceiveData(kDataSize);
456 EXPECT_TRUE(data_source_->loading());
457 Stop();
460 TEST_F(BufferedDataSourceTest, Http_RetryOnError) {
461 InitializeWith206Response();
463 // Read to advance our position.
464 EXPECT_CALL(*this, ReadCallback(kDataSize));
465 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
466 ReadAt(0);
467 ReceiveData(kDataSize);
469 // Issue a pending read but trigger an error to force a retry.
470 EXPECT_CALL(*this, ReadCallback(kDataSize));
471 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
472 ReadAt(kDataSize);
473 base::RunLoop run_loop;
474 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
475 .WillOnce(
476 DoAll(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit),
477 Invoke(data_source_.get(),
478 &MockBufferedDataSource::CreateMockResourceLoader)));
479 loader()->didFail(url_loader(), response_generator_->GenerateError());
480 run_loop.Run();
481 Respond(response_generator_->Generate206(kDataSize));
482 ReceiveData(kDataSize);
483 FinishLoading();
484 EXPECT_FALSE(data_source_->loading());
485 Stop();
488 TEST_F(BufferedDataSourceTest, Http_PartialResponse) {
489 Initialize(kHttpUrl, true);
490 WebURLResponse response1 =
491 response_generator_->GeneratePartial206(0, kDataSize - 1);
492 WebURLResponse response2 =
493 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
494 // The origin URL of response1 and response2 are same. So no error should
495 // occur.
496 ExecuteMixedResponseSuccessTest(response1, response2);
499 TEST_F(BufferedDataSourceTest,
500 Http_MixedResponse_RedirectedToDifferentPathResponse) {
501 Initialize(kHttpUrl, true);
502 WebURLResponse response1 =
503 response_generator_->GeneratePartial206(0, kDataSize - 1);
504 WebURLResponse response2 =
505 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
506 response2.setURL(GURL(kHttpDifferentPathUrl));
507 // The origin URL of response1 and response2 are same. So no error should
508 // occur.
509 ExecuteMixedResponseSuccessTest(response1, response2);
512 TEST_F(BufferedDataSourceTest,
513 Http_MixedResponse_RedirectedToDifferentOriginResponse) {
514 Initialize(kHttpUrl, true);
515 WebURLResponse response1 =
516 response_generator_->GeneratePartial206(0, kDataSize - 1);
517 WebURLResponse response2 =
518 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
519 response2.setURL(GURL(kHttpDifferentOriginUrl));
520 // The origin URL of response1 and response2 are different. So an error should
521 // occur.
522 ExecuteMixedResponseFailureTest(response1, response2);
525 TEST_F(BufferedDataSourceTest,
526 Http_MixedResponse_ServiceWorkerGeneratedResponseAndNormalResponse) {
527 Initialize(kHttpUrl, true);
528 WebURLResponse response1 =
529 response_generator_->GeneratePartial206(0, kDataSize - 1);
530 response1.setWasFetchedViaServiceWorker(true);
531 WebURLResponse response2 =
532 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
533 // response1 is generated in a Service Worker but response2 is from a native
534 // server. So an error should occur.
535 ExecuteMixedResponseFailureTest(response1, response2);
538 TEST_F(BufferedDataSourceTest,
539 Http_MixedResponse_ServiceWorkerProxiedAndSameURLResponse) {
540 Initialize(kHttpUrl, true);
541 WebURLResponse response1 =
542 response_generator_->GeneratePartial206(0, kDataSize - 1);
543 response1.setWasFetchedViaServiceWorker(true);
544 response1.setOriginalURLViaServiceWorker(GURL(kHttpUrl));
545 WebURLResponse response2 =
546 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
547 // The origin URL of response1 and response2 are same. So no error should
548 // occur.
549 ExecuteMixedResponseSuccessTest(response1, response2);
552 TEST_F(BufferedDataSourceTest,
553 Http_MixedResponse_ServiceWorkerProxiedAndDifferentPathResponse) {
554 Initialize(kHttpUrl, true);
555 WebURLResponse response1 =
556 response_generator_->GeneratePartial206(0, kDataSize - 1);
557 response1.setWasFetchedViaServiceWorker(true);
558 response1.setOriginalURLViaServiceWorker(GURL(kHttpDifferentPathUrl));
559 WebURLResponse response2 =
560 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
561 // The origin URL of response1 and response2 are same. So no error should
562 // occur.
563 ExecuteMixedResponseSuccessTest(response1, response2);
566 TEST_F(BufferedDataSourceTest,
567 Http_MixedResponse_ServiceWorkerProxiedAndDifferentOriginResponse) {
568 Initialize(kHttpUrl, true);
569 WebURLResponse response1 =
570 response_generator_->GeneratePartial206(0, kDataSize - 1);
571 response1.setWasFetchedViaServiceWorker(true);
572 response1.setOriginalURLViaServiceWorker(GURL(kHttpDifferentOriginUrl));
573 WebURLResponse response2 =
574 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1);
575 // The origin URL of response1 and response2 are different. So an error should
576 // occur.
577 ExecuteMixedResponseFailureTest(response1, response2);
580 TEST_F(BufferedDataSourceTest, File_Retry) {
581 InitializeWithFileResponse();
583 // Read to advance our position.
584 EXPECT_CALL(*this, ReadCallback(kDataSize));
585 ReadAt(0);
586 ReceiveData(kDataSize);
588 // Issue a pending read but terminate the connection to force a retry.
589 ReadAt(kDataSize);
590 ExpectCreateResourceLoader();
591 FinishLoading();
592 Respond(response_generator_->GenerateFileResponse(kDataSize));
594 // Complete the read.
595 EXPECT_CALL(*this, ReadCallback(kDataSize));
596 ReceiveData(kDataSize);
598 EXPECT_TRUE(data_source_->loading());
599 Stop();
602 TEST_F(BufferedDataSourceTest, Http_TooManyRetries) {
603 InitializeWith206Response();
605 // Make sure there's a pending read -- we'll expect it to error.
606 ReadAt(0);
608 // It'll try three times.
609 ExpectCreateResourceLoader();
610 FinishLoading();
611 Respond(response_generator_->Generate206(0));
613 ExpectCreateResourceLoader();
614 FinishLoading();
615 Respond(response_generator_->Generate206(0));
617 ExpectCreateResourceLoader();
618 FinishLoading();
619 Respond(response_generator_->Generate206(0));
621 // It'll error after this.
622 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
623 FinishLoading();
625 EXPECT_FALSE(data_source_->loading());
626 Stop();
629 TEST_F(BufferedDataSourceTest, File_TooManyRetries) {
630 InitializeWithFileResponse();
632 // Make sure there's a pending read -- we'll expect it to error.
633 ReadAt(0);
635 // It'll try three times.
636 ExpectCreateResourceLoader();
637 FinishLoading();
638 Respond(response_generator_->GenerateFileResponse(0));
640 ExpectCreateResourceLoader();
641 FinishLoading();
642 Respond(response_generator_->GenerateFileResponse(0));
644 ExpectCreateResourceLoader();
645 FinishLoading();
646 Respond(response_generator_->GenerateFileResponse(0));
648 // It'll error after this.
649 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
650 FinishLoading();
652 EXPECT_FALSE(data_source_->loading());
653 Stop();
656 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) {
657 Initialize(kFileUrl, false);
658 EXPECT_FALSE(data_source_->downloading());
660 Respond(response_generator_->GenerateFileResponse(-1));
662 EXPECT_FALSE(data_source_->loading());
663 Stop();
666 TEST_F(BufferedDataSourceTest, File_Successful) {
667 InitializeWithFileResponse();
669 EXPECT_TRUE(data_source_->loading());
670 EXPECT_FALSE(data_source_->IsStreaming());
671 Stop();
674 TEST_F(BufferedDataSourceTest, StopDuringRead) {
675 InitializeWith206Response();
677 uint8 buffer[256];
678 data_source_->Read(0, arraysize(buffer), buffer, base::Bind(
679 &BufferedDataSourceTest::ReadCallback, base::Unretained(this)));
681 // The outstanding read should fail before the stop callback runs.
683 InSequence s;
684 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
685 data_source_->Stop();
687 message_loop_.RunUntilIdle();
690 TEST_F(BufferedDataSourceTest, DefaultValues) {
691 InitializeWith206Response();
693 // Ensure we have sane values for default loading scenario.
694 EXPECT_EQ(BufferedDataSource::AUTO, preload());
695 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
697 EXPECT_EQ(0, data_source_bitrate());
698 EXPECT_EQ(0.0, data_source_playback_rate());
699 EXPECT_EQ(0, loader_bitrate());
700 EXPECT_EQ(0.0, loader_playback_rate());
702 EXPECT_TRUE(data_source_->loading());
703 Stop();
706 TEST_F(BufferedDataSourceTest, SetBitrate) {
707 InitializeWith206Response();
709 data_source_->SetBitrate(1234);
710 message_loop_.RunUntilIdle();
711 EXPECT_EQ(1234, data_source_bitrate());
712 EXPECT_EQ(1234, loader_bitrate());
714 // Read so far ahead to cause the loader to get recreated.
715 BufferedResourceLoader* old_loader = loader();
716 ExpectCreateResourceLoader();
717 ReadAt(kFarReadPosition);
718 Respond(response_generator_->Generate206(kFarReadPosition));
720 // Verify loader changed but still has same bitrate.
721 EXPECT_NE(old_loader, loader());
722 EXPECT_EQ(1234, loader_bitrate());
724 EXPECT_TRUE(data_source_->loading());
725 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
726 Stop();
729 TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) {
730 InitializeWith206Response();
732 data_source_->MediaPlaybackRateChanged(2.0);
733 message_loop_.RunUntilIdle();
734 EXPECT_EQ(2.0, data_source_playback_rate());
735 EXPECT_EQ(2.0, loader_playback_rate());
737 // Read so far ahead to cause the loader to get recreated.
738 BufferedResourceLoader* old_loader = loader();
739 ExpectCreateResourceLoader();
740 ReadAt(kFarReadPosition);
741 Respond(response_generator_->Generate206(kFarReadPosition));
743 // Verify loader changed but still has same playback rate.
744 EXPECT_NE(old_loader, loader());
746 EXPECT_TRUE(data_source_->loading());
747 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
748 Stop();
751 TEST_F(BufferedDataSourceTest, Http_Read) {
752 InitializeWith206Response();
754 ReadAt(0);
756 // Receive first half of the read.
757 EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1));
758 ReceiveData(kDataSize / 2);
760 // Receive last half of the read.
761 EXPECT_CALL(*this, ReadCallback(kDataSize));
762 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
763 ReceiveData(kDataSize / 2);
765 EXPECT_TRUE(data_source_->downloading());
766 Stop();
769 TEST_F(BufferedDataSourceTest, Http_Read_Seek) {
770 InitializeWith206Response();
772 // Read a bit from the beginning.
773 ReadAt(0);
774 EXPECT_CALL(*this, ReadCallback(kDataSize));
775 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
776 ReceiveData(kDataSize);
778 // Simulate a seek by reading a bit beyond kDataSize.
779 ReadAt(kDataSize * 2);
781 // We receive data leading up to but not including our read.
782 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1));
783 ReceiveData(kDataSize);
785 // We now receive the rest of the data for our read.
786 EXPECT_CALL(*this, ReadCallback(kDataSize));
787 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1));
788 ReceiveData(kDataSize);
790 EXPECT_TRUE(data_source_->downloading());
791 Stop();
794 TEST_F(BufferedDataSourceTest, File_Read) {
795 InitializeWithFileResponse();
797 ReadAt(0);
799 // Receive first half of the read but no buffering update.
800 ReceiveData(kDataSize / 2);
802 // Receive last half of the read but no buffering update.
803 EXPECT_CALL(*this, ReadCallback(kDataSize));
804 ReceiveData(kDataSize / 2);
806 Stop();
809 TEST_F(BufferedDataSourceTest, Http_FinishLoading) {
810 InitializeWith206Response();
812 EXPECT_TRUE(data_source_->downloading());
813 FinishLoading();
814 EXPECT_FALSE(data_source_->downloading());
816 Stop();
819 TEST_F(BufferedDataSourceTest, File_FinishLoading) {
820 InitializeWithFileResponse();
822 EXPECT_FALSE(data_source_->downloading());
823 FinishLoading();
824 EXPECT_FALSE(data_source_->downloading());
826 Stop();
829 TEST_F(BufferedDataSourceTest, LocalResource_DeferStrategy) {
830 InitializeWithFileResponse();
832 EXPECT_EQ(BufferedDataSource::AUTO, preload());
833 EXPECT_TRUE(is_local_source());
834 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
836 data_source_->MediaIsPlaying();
837 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
839 data_source_->MediaIsPaused();
840 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
842 Stop();
845 TEST_F(BufferedDataSourceTest, LocalResource_PreloadMetadata_DeferStrategy) {
846 set_preload(BufferedDataSource::METADATA);
847 InitializeWithFileResponse();
849 EXPECT_EQ(BufferedDataSource::METADATA, preload());
850 EXPECT_TRUE(is_local_source());
851 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
853 data_source_->MediaIsPlaying();
854 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
856 data_source_->MediaIsPaused();
857 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
859 Stop();
862 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse200_DeferStrategy) {
863 InitializeWith200Response();
865 EXPECT_EQ(BufferedDataSource::AUTO, preload());
866 EXPECT_FALSE(is_local_source());
867 EXPECT_FALSE(loader()->range_supported());
868 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
870 data_source_->MediaIsPlaying();
871 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
873 data_source_->MediaIsPaused();
874 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
876 Stop();
879 TEST_F(BufferedDataSourceTest,
880 ExternalResource_Response200_PreloadMetadata_DeferStrategy) {
881 set_preload(BufferedDataSource::METADATA);
882 InitializeWith200Response();
884 EXPECT_EQ(BufferedDataSource::METADATA, preload());
885 EXPECT_FALSE(is_local_source());
886 EXPECT_FALSE(loader()->range_supported());
887 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
889 data_source_->MediaIsPlaying();
890 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
892 data_source_->MediaIsPaused();
893 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
895 Stop();
898 TEST_F(BufferedDataSourceTest, ExternalResource_Reponse206_DeferStrategy) {
899 InitializeWith206Response();
901 EXPECT_EQ(BufferedDataSource::AUTO, preload());
902 EXPECT_FALSE(is_local_source());
903 EXPECT_TRUE(loader()->range_supported());
904 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
906 data_source_->MediaIsPlaying();
907 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
908 set_might_be_reused_from_cache_in_future(true);
909 data_source_->MediaIsPaused();
910 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
912 data_source_->MediaIsPlaying();
913 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
914 set_might_be_reused_from_cache_in_future(false);
915 data_source_->MediaIsPaused();
916 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
918 Stop();
921 TEST_F(BufferedDataSourceTest,
922 ExternalResource_Response206_PreloadMetadata_DeferStrategy) {
923 set_preload(BufferedDataSource::METADATA);
924 InitializeWith206Response();
926 EXPECT_EQ(BufferedDataSource::METADATA, preload());
927 EXPECT_FALSE(is_local_source());
928 EXPECT_TRUE(loader()->range_supported());
929 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
931 data_source_->MediaIsPlaying();
932 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
933 set_might_be_reused_from_cache_in_future(true);
934 data_source_->MediaIsPaused();
935 EXPECT_EQ(BufferedResourceLoader::kNeverDefer, defer_strategy());
937 data_source_->MediaIsPlaying();
938 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
939 set_might_be_reused_from_cache_in_future(false);
940 data_source_->MediaIsPaused();
941 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
943 Stop();
946 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_VerifyDefer) {
947 set_preload(BufferedDataSource::METADATA);
948 InitializeWith206Response();
950 EXPECT_EQ(BufferedDataSource::METADATA, preload());
951 EXPECT_FALSE(is_local_source());
952 EXPECT_TRUE(loader()->range_supported());
953 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
955 // Read a bit from the beginning.
956 ReadAt(0);
957 EXPECT_CALL(*this, ReadCallback(kDataSize));
958 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
959 ReceiveData(kDataSize);
961 ASSERT_TRUE(active_loader());
962 EXPECT_TRUE(active_loader()->deferred());
965 TEST_F(BufferedDataSourceTest, ExternalResource_Response206_CancelAfterDefer) {
966 set_preload(BufferedDataSource::METADATA);
967 InitializeWith206Response();
969 EXPECT_EQ(BufferedDataSource::METADATA, preload());
970 EXPECT_FALSE(is_local_source());
971 EXPECT_TRUE(loader()->range_supported());
972 EXPECT_EQ(BufferedResourceLoader::kReadThenDefer, defer_strategy());
974 data_source_->OnBufferingHaveEnough();
976 ASSERT_TRUE(active_loader());
978 // Read a bit from the beginning.
979 ReadAt(0);
980 EXPECT_CALL(*this, ReadCallback(kDataSize));
981 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
982 ReceiveData(kDataSize);
984 EXPECT_FALSE(active_loader());
987 } // namespace media