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 "content/public/common/url_constants.h"
8 #include "content/renderer/media/buffered_data_source.h"
9 #include "content/renderer/media/test_response_generator.h"
10 #include "content/test/mock_webframeclient.h"
11 #include "content/test/mock_weburlloader.h"
12 #include "media/base/media_log.h"
13 #include "media/base/mock_filters.h"
14 #include "media/base/test_helpers.h"
15 #include "third_party/WebKit/public/platform/WebURLResponse.h"
16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
17 #include "third_party/WebKit/public/web/WebView.h"
20 using ::testing::Assign;
21 using ::testing::Invoke;
22 using ::testing::InSequence;
23 using ::testing::NiceMock;
24 using ::testing::StrictMock;
26 using blink::WebLocalFrame;
27 using blink::WebString;
28 using blink::WebURLLoader;
29 using blink::WebURLResponse;
34 class MockBufferedDataSourceHost : public BufferedDataSourceHost {
36 MockBufferedDataSourceHost() {}
37 virtual ~MockBufferedDataSourceHost() {}
39 MOCK_METHOD1(SetTotalBytes, void(int64 total_bytes));
40 MOCK_METHOD2(AddBufferedByteRange, void(int64 start, int64 end));
43 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost);
46 // Overrides CreateResourceLoader() to permit injecting a MockWebURLLoader.
47 // Also keeps track of whether said MockWebURLLoader is actively loading.
48 class MockBufferedDataSource : public BufferedDataSource {
50 MockBufferedDataSource(
51 const scoped_refptr<base::MessageLoopProxy>& message_loop,
53 BufferedDataSourceHost* host)
54 : BufferedDataSource(message_loop, frame, new media::MediaLog(), host,
55 base::Bind(&MockBufferedDataSource::set_downloading,
56 base::Unretained(this))),
60 virtual ~MockBufferedDataSource() {}
62 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(int64, int64));
63 BufferedResourceLoader* CreateMockResourceLoader(int64 first_byte_position,
64 int64 last_byte_position) {
65 CHECK(!loading_) << "Previous resource load wasn't cancelled";
67 BufferedResourceLoader* loader =
68 BufferedDataSource::CreateResourceLoader(first_byte_position,
71 // Keep track of active loading state via loadAsynchronously() and cancel().
72 NiceMock<MockWebURLLoader>* url_loader = new NiceMock<MockWebURLLoader>();
73 ON_CALL(*url_loader, loadAsynchronously(_, _))
74 .WillByDefault(Assign(&loading_, true));
75 ON_CALL(*url_loader, cancel())
76 .WillByDefault(Assign(&loading_, false));
78 // |test_loader_| will be used when Start() is called.
79 loader->test_loader_ = scoped_ptr<WebURLLoader>(url_loader);
83 bool loading() { return loading_; }
84 void set_loading(bool loading) { loading_ = loading; }
85 bool downloading() { return downloading_; }
86 void set_downloading(bool downloading) { downloading_ = downloading; }
89 // Whether the resource is downloading or deferred.
92 // Whether the resource load has starting loading but yet to been cancelled.
95 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource);
98 static const int64 kFileSize = 5000000;
99 static const int64 kFarReadPosition = 4000000;
100 static const int kDataSize = 1024;
102 static const char kHttpUrl[] = "http://localhost/foo.webm";
103 static const char kFileUrl[] = "file:///tmp/bar.webm";
105 class BufferedDataSourceTest : public testing::Test {
107 BufferedDataSourceTest()
108 : view_(WebView::create(NULL)), frame_(WebLocalFrame::create(&client_)) {
109 view_->setMainFrame(frame_);
112 new MockBufferedDataSource(message_loop_.message_loop_proxy(),
113 view_->mainFrame()->toWebLocalFrame(),
117 virtual ~BufferedDataSourceTest() {
122 MOCK_METHOD1(OnInitialize, void(bool));
124 void Initialize(const char* url, bool expected) {
126 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize));
128 ExpectCreateResourceLoader();
129 EXPECT_CALL(*this, OnInitialize(expected));
130 data_source_->Initialize(
131 gurl, BufferedResourceLoader::kUnspecified, base::Bind(
132 &BufferedDataSourceTest::OnInitialize, base::Unretained(this)));
133 message_loop_.RunUntilIdle();
136 gurl.SchemeIs(url::kHttpScheme) || gurl.SchemeIs(url::kHttpsScheme);
137 EXPECT_EQ(data_source_->downloading(), is_http);
140 // Helper to initialize tests with a valid 206 response.
141 void InitializeWith206Response() {
142 Initialize(kHttpUrl, true);
144 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
145 Respond(response_generator_->Generate206(0));
148 // Helper to initialize tests with a valid file:// response.
149 void InitializeWithFileResponse() {
150 Initialize(kFileUrl, true);
152 EXPECT_CALL(host_, SetTotalBytes(kFileSize));
153 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize));
154 Respond(response_generator_->GenerateFileResponse(0));
157 // Stops any active loaders and shuts down the data source.
159 // This typically happens when the page is closed and for our purposes is
160 // appropriate to do when tearing down a test.
162 if (data_source_->loading()) {
163 loader()->didFail(url_loader(), response_generator_->GenerateError());
164 message_loop_.RunUntilIdle();
167 data_source_->Stop(media::NewExpectedClosure());
168 message_loop_.RunUntilIdle();
171 void ExpectCreateResourceLoader() {
172 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
173 .WillOnce(Invoke(data_source_.get(),
174 &MockBufferedDataSource::CreateMockResourceLoader));
175 message_loop_.RunUntilIdle();
178 void Respond(const WebURLResponse& response) {
179 loader()->didReceiveResponse(url_loader(), response);
180 message_loop_.RunUntilIdle();
183 void ReceiveData(int size) {
184 scoped_ptr<char[]> data(new char[size]);
185 memset(data.get(), 0xA5, size); // Arbitrary non-zero value.
187 loader()->didReceiveData(url_loader(), data.get(), size, size);
188 message_loop_.RunUntilIdle();
191 void FinishLoading() {
192 data_source_->set_loading(false);
193 loader()->didFinishLoading(url_loader(), 0, -1);
194 message_loop_.RunUntilIdle();
197 MOCK_METHOD1(ReadCallback, void(int size));
199 void ReadAt(int64 position) {
200 data_source_->Read(position, kDataSize, buffer_,
201 base::Bind(&BufferedDataSourceTest::ReadCallback,
202 base::Unretained(this)));
203 message_loop_.RunUntilIdle();
206 // Accessors for private variables on |data_source_|.
207 BufferedResourceLoader* loader() {
208 return data_source_->loader_.get();
210 WebURLLoader* url_loader() {
211 return loader()->active_loader_->loader_.get();
214 Preload preload() { return data_source_->preload_; }
215 BufferedResourceLoader::DeferStrategy defer_strategy() {
216 return loader()->defer_strategy_;
218 int data_source_bitrate() { return data_source_->bitrate_; }
219 int data_source_playback_rate() { return data_source_->playback_rate_; }
220 int loader_bitrate() { return loader()->bitrate_; }
221 int loader_playback_rate() { return loader()->playback_rate_; }
223 scoped_ptr<MockBufferedDataSource> data_source_;
225 scoped_ptr<TestResponseGenerator> response_generator_;
226 MockWebFrameClient client_;
228 WebLocalFrame* frame_;
230 StrictMock<MockBufferedDataSourceHost> host_;
231 base::MessageLoop message_loop_;
234 // Used for calling BufferedDataSource::Read().
235 uint8 buffer_[kDataSize];
237 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
240 TEST_F(BufferedDataSourceTest, Range_Supported) {
241 Initialize(kHttpUrl, true);
243 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
244 Respond(response_generator_->Generate206(0));
246 EXPECT_TRUE(data_source_->loading());
247 EXPECT_FALSE(data_source_->IsStreaming());
251 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) {
252 Initialize(kHttpUrl, true);
254 Respond(response_generator_->Generate206(
255 0, TestResponseGenerator::kNoContentRangeInstanceSize));
257 EXPECT_TRUE(data_source_->loading());
258 EXPECT_TRUE(data_source_->IsStreaming());
262 TEST_F(BufferedDataSourceTest, Range_NotFound) {
263 Initialize(kHttpUrl, false);
264 Respond(response_generator_->Generate404());
266 EXPECT_FALSE(data_source_->loading());
270 TEST_F(BufferedDataSourceTest, Range_NotSupported) {
271 Initialize(kHttpUrl, true);
272 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
273 Respond(response_generator_->Generate200());
275 EXPECT_TRUE(data_source_->loading());
276 EXPECT_TRUE(data_source_->IsStreaming());
280 // Special carve-out for Apache versions that choose to return a 200 for
281 // Range:0- ("because it's more efficient" than a 206)
282 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) {
283 Initialize(kHttpUrl, true);
284 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
285 WebURLResponse response = response_generator_->Generate200();
286 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
287 WebString::fromUTF8("bytes"));
290 EXPECT_TRUE(data_source_->loading());
291 EXPECT_FALSE(data_source_->IsStreaming());
295 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) {
296 Initialize(kHttpUrl, false);
297 Respond(response_generator_->Generate206(
298 0, TestResponseGenerator::kNoContentRange));
300 EXPECT_FALSE(data_source_->loading());
304 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) {
305 Initialize(kHttpUrl, true);
307 // It'll manage without a Content-Length response.
308 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
309 Respond(response_generator_->Generate206(
310 0, TestResponseGenerator::kNoContentLength));
312 EXPECT_TRUE(data_source_->loading());
313 EXPECT_FALSE(data_source_->IsStreaming());
317 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) {
318 Initialize(kHttpUrl, false);
320 // Now it's done and will fail.
321 Respond(response_generator_->Generate206(1337));
323 EXPECT_FALSE(data_source_->loading());
327 // Test the case where the initial response from the server indicates that
328 // Range requests are supported, but a later request prove otherwise.
329 TEST_F(BufferedDataSourceTest, Range_ServerLied) {
330 InitializeWith206Response();
332 // Read causing a new request to be made -- we'll expect it to error.
333 ExpectCreateResourceLoader();
334 ReadAt(kFarReadPosition);
336 // Return a 200 in response to a range request.
337 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
338 Respond(response_generator_->Generate200());
340 EXPECT_FALSE(data_source_->loading());
344 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) {
345 InitializeWith206Response();
347 // Make sure there's a pending read -- we'll expect it to error.
351 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
352 data_source_->Abort();
353 message_loop_.RunUntilIdle();
355 EXPECT_FALSE(data_source_->loading());
359 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) {
360 InitializeWithFileResponse();
362 // Make sure there's a pending read -- we'll expect it to error.
366 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
367 data_source_->Abort();
368 message_loop_.RunUntilIdle();
370 EXPECT_FALSE(data_source_->loading());
374 TEST_F(BufferedDataSourceTest, Http_Retry) {
375 InitializeWith206Response();
377 // Read to advance our position.
378 EXPECT_CALL(*this, ReadCallback(kDataSize));
379 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
381 ReceiveData(kDataSize);
383 // Issue a pending read but terminate the connection to force a retry.
385 ExpectCreateResourceLoader();
387 Respond(response_generator_->Generate206(kDataSize));
389 // Complete the read.
390 EXPECT_CALL(*this, ReadCallback(kDataSize));
391 EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
392 ReceiveData(kDataSize);
394 EXPECT_TRUE(data_source_->loading());
398 TEST_F(BufferedDataSourceTest, File_Retry) {
399 InitializeWithFileResponse();
401 // Read to advance our position.
402 EXPECT_CALL(*this, ReadCallback(kDataSize));
404 ReceiveData(kDataSize);
406 // Issue a pending read but terminate the connection to force a retry.
408 ExpectCreateResourceLoader();
410 Respond(response_generator_->GenerateFileResponse(kDataSize));
412 // Complete the read.
413 EXPECT_CALL(*this, ReadCallback(kDataSize));
414 ReceiveData(kDataSize);
416 EXPECT_TRUE(data_source_->loading());
420 TEST_F(BufferedDataSourceTest, Http_TooManyRetries) {
421 InitializeWith206Response();
423 // Make sure there's a pending read -- we'll expect it to error.
426 // It'll try three times.
427 ExpectCreateResourceLoader();
429 Respond(response_generator_->Generate206(0));
431 ExpectCreateResourceLoader();
433 Respond(response_generator_->Generate206(0));
435 ExpectCreateResourceLoader();
437 Respond(response_generator_->Generate206(0));
439 // It'll error after this.
440 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
443 EXPECT_FALSE(data_source_->loading());
447 TEST_F(BufferedDataSourceTest, File_TooManyRetries) {
448 InitializeWithFileResponse();
450 // Make sure there's a pending read -- we'll expect it to error.
453 // It'll try three times.
454 ExpectCreateResourceLoader();
456 Respond(response_generator_->GenerateFileResponse(0));
458 ExpectCreateResourceLoader();
460 Respond(response_generator_->GenerateFileResponse(0));
462 ExpectCreateResourceLoader();
464 Respond(response_generator_->GenerateFileResponse(0));
466 // It'll error after this.
467 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
470 EXPECT_FALSE(data_source_->loading());
474 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) {
475 Initialize(kFileUrl, false);
476 EXPECT_FALSE(data_source_->downloading());
478 Respond(response_generator_->GenerateFileResponse(-1));
480 EXPECT_FALSE(data_source_->loading());
484 TEST_F(BufferedDataSourceTest, File_Successful) {
485 InitializeWithFileResponse();
487 EXPECT_TRUE(data_source_->loading());
488 EXPECT_FALSE(data_source_->IsStreaming());
492 static void SetTrue(bool* value) {
496 // This test makes sure that Stop() does not require a task to run on
497 // |message_loop_| before it calls its callback. This prevents accidental
498 // introduction of a pipeline teardown deadlock. The pipeline owner blocks
499 // the render message loop while waiting for Stop() to complete. Since this
500 // object runs on the render message loop, Stop() will not complete if it
501 // requires a task to run on the the message loop that is being blocked.
502 TEST_F(BufferedDataSourceTest, StopDoesNotUseMessageLoopForCallback) {
503 InitializeWith206Response();
505 // Stop() the data source, using a callback that lets us verify that it was
506 // called before Stop() returns. This is to make sure that the callback does
507 // not require |message_loop_| to execute tasks before being called.
508 bool stop_done_called = false;
509 EXPECT_TRUE(data_source_->loading());
510 data_source_->Stop(base::Bind(&SetTrue, &stop_done_called));
512 // Verify that the callback was called inside the Stop() call.
513 EXPECT_TRUE(stop_done_called);
514 message_loop_.RunUntilIdle();
517 TEST_F(BufferedDataSourceTest, StopDuringRead) {
518 InitializeWith206Response();
521 data_source_->Read(0, arraysize(buffer), buffer, base::Bind(
522 &BufferedDataSourceTest::ReadCallback, base::Unretained(this)));
524 // The outstanding read should fail before the stop callback runs.
527 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
528 data_source_->Stop(media::NewExpectedClosure());
530 message_loop_.RunUntilIdle();
533 TEST_F(BufferedDataSourceTest, DefaultValues) {
534 InitializeWith206Response();
536 // Ensure we have sane values for default loading scenario.
537 EXPECT_EQ(AUTO, preload());
538 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
540 EXPECT_EQ(0, data_source_bitrate());
541 EXPECT_EQ(0.0f, data_source_playback_rate());
542 EXPECT_EQ(0, loader_bitrate());
543 EXPECT_EQ(0.0f, loader_playback_rate());
545 EXPECT_TRUE(data_source_->loading());
549 TEST_F(BufferedDataSourceTest, SetBitrate) {
550 InitializeWith206Response();
552 data_source_->SetBitrate(1234);
553 message_loop_.RunUntilIdle();
554 EXPECT_EQ(1234, data_source_bitrate());
555 EXPECT_EQ(1234, loader_bitrate());
557 // Read so far ahead to cause the loader to get recreated.
558 BufferedResourceLoader* old_loader = loader();
559 ExpectCreateResourceLoader();
560 ReadAt(kFarReadPosition);
561 Respond(response_generator_->Generate206(kFarReadPosition));
563 // Verify loader changed but still has same bitrate.
564 EXPECT_NE(old_loader, loader());
565 EXPECT_EQ(1234, loader_bitrate());
567 EXPECT_TRUE(data_source_->loading());
568 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
572 TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) {
573 InitializeWith206Response();
575 data_source_->MediaPlaybackRateChanged(2.0f);
576 message_loop_.RunUntilIdle();
577 EXPECT_EQ(2.0f, data_source_playback_rate());
578 EXPECT_EQ(2.0f, loader_playback_rate());
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 playback rate.
587 EXPECT_NE(old_loader, loader());
589 EXPECT_TRUE(data_source_->loading());
590 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
594 TEST_F(BufferedDataSourceTest, Http_Read) {
595 InitializeWith206Response();
599 // Receive first half of the read.
600 EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1));
601 ReceiveData(kDataSize / 2);
603 // Receive last half of the read.
604 EXPECT_CALL(*this, ReadCallback(kDataSize));
605 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
606 ReceiveData(kDataSize / 2);
608 EXPECT_TRUE(data_source_->downloading());
612 TEST_F(BufferedDataSourceTest, Http_Read_Seek) {
613 InitializeWith206Response();
615 // Read a bit from the beginning.
617 EXPECT_CALL(*this, ReadCallback(kDataSize));
618 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
619 ReceiveData(kDataSize);
621 // Simulate a seek by reading a bit beyond kDataSize.
622 ReadAt(kDataSize * 2);
624 // We receive data leading up to but not including our read.
625 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1));
626 ReceiveData(kDataSize);
628 // We now receive the rest of the data for our read.
629 EXPECT_CALL(*this, ReadCallback(kDataSize));
630 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1));
631 ReceiveData(kDataSize);
633 EXPECT_TRUE(data_source_->downloading());
637 TEST_F(BufferedDataSourceTest, File_Read) {
638 InitializeWithFileResponse();
642 // Receive first half of the read but no buffering update.
643 ReceiveData(kDataSize / 2);
645 // Receive last half of the read but no buffering update.
646 EXPECT_CALL(*this, ReadCallback(kDataSize));
647 ReceiveData(kDataSize / 2);
652 TEST_F(BufferedDataSourceTest, Http_FinishLoading) {
653 InitializeWith206Response();
655 EXPECT_TRUE(data_source_->downloading());
657 EXPECT_FALSE(data_source_->downloading());
662 TEST_F(BufferedDataSourceTest, File_FinishLoading) {
663 InitializeWithFileResponse();
665 EXPECT_FALSE(data_source_->downloading());
667 EXPECT_FALSE(data_source_->downloading());
672 } // namespace content