OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "media/base/media_log.h" | 7 #include "media/base/media_log.h" |
8 #include "media/base/mock_callback.h" | 8 #include "media/base/mock_callback.h" |
9 #include "media/base/mock_data_source_host.h" | 9 #include "media/base/mock_data_source_host.h" |
10 #include "media/base/mock_filters.h" | 10 #include "media/base/mock_filters.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 void Initialize(const char* url, bool expected) { | 109 void Initialize(const char* url, bool expected) { |
110 GURL gurl(url); | 110 GURL gurl(url); |
111 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 111 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
112 | 112 |
113 ExpectCreateResourceLoader(); | 113 ExpectCreateResourceLoader(); |
114 EXPECT_CALL(*this, OnInitialize(expected)); | 114 EXPECT_CALL(*this, OnInitialize(expected)); |
115 data_source_->Initialize( | 115 data_source_->Initialize( |
116 gurl, BufferedResourceLoader::kUnspecified, base::Bind( | 116 gurl, BufferedResourceLoader::kUnspecified, base::Bind( |
117 &BufferedDataSourceTest::OnInitialize, base::Unretained(this))); | 117 &BufferedDataSourceTest::OnInitialize, base::Unretained(this))); |
118 message_loop_.RunAllPending(); | 118 message_loop_.RunUntilIdle(); |
119 | 119 |
120 bool is_http = gurl.SchemeIs(kHttpScheme) || gurl.SchemeIs(kHttpsScheme); | 120 bool is_http = gurl.SchemeIs(kHttpScheme) || gurl.SchemeIs(kHttpsScheme); |
121 EXPECT_EQ(data_source_->downloading(), is_http); | 121 EXPECT_EQ(data_source_->downloading(), is_http); |
122 } | 122 } |
123 | 123 |
124 // Helper to initialize tests with a valid 206 response. | 124 // Helper to initialize tests with a valid 206 response. |
125 void InitializeWith206Response() { | 125 void InitializeWith206Response() { |
126 Initialize(kHttpUrl, true); | 126 Initialize(kHttpUrl, true); |
127 | 127 |
128 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 128 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
129 Respond(response_generator_->Generate206(0)); | 129 Respond(response_generator_->Generate206(0)); |
130 } | 130 } |
131 | 131 |
132 // Helper to initialize tests with a valid file:// response. | 132 // Helper to initialize tests with a valid file:// response. |
133 void InitializeWithFileResponse() { | 133 void InitializeWithFileResponse() { |
134 Initialize(kFileUrl, true); | 134 Initialize(kFileUrl, true); |
135 | 135 |
136 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); | 136 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); |
137 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize)); | 137 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize)); |
138 Respond(response_generator_->GenerateFileResponse(0)); | 138 Respond(response_generator_->GenerateFileResponse(0)); |
139 } | 139 } |
140 | 140 |
141 // Stops any active loaders and shuts down the data source. | 141 // Stops any active loaders and shuts down the data source. |
142 // | 142 // |
143 // This typically happens when the page is closed and for our purposes is | 143 // This typically happens when the page is closed and for our purposes is |
144 // appropriate to do when tearing down a test. | 144 // appropriate to do when tearing down a test. |
145 void Stop() { | 145 void Stop() { |
146 if (data_source_->loading()) { | 146 if (data_source_->loading()) { |
147 loader()->didFail(url_loader(), response_generator_->GenerateError()); | 147 loader()->didFail(url_loader(), response_generator_->GenerateError()); |
148 message_loop_.RunAllPending(); | 148 message_loop_.RunUntilIdle(); |
149 } | 149 } |
150 | 150 |
151 data_source_->Stop(media::NewExpectedClosure()); | 151 data_source_->Stop(media::NewExpectedClosure()); |
152 message_loop_.RunAllPending(); | 152 message_loop_.RunUntilIdle(); |
153 } | 153 } |
154 | 154 |
155 void ExpectCreateResourceLoader() { | 155 void ExpectCreateResourceLoader() { |
156 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _)) | 156 EXPECT_CALL(*data_source_, CreateResourceLoader(_, _)) |
157 .WillOnce(Invoke(data_source_.get(), | 157 .WillOnce(Invoke(data_source_.get(), |
158 &MockBufferedDataSource::CreateMockResourceLoader)); | 158 &MockBufferedDataSource::CreateMockResourceLoader)); |
159 message_loop_.RunAllPending(); | 159 message_loop_.RunUntilIdle(); |
160 } | 160 } |
161 | 161 |
162 void Respond(const WebURLResponse& response) { | 162 void Respond(const WebURLResponse& response) { |
163 loader()->didReceiveResponse(url_loader(), response); | 163 loader()->didReceiveResponse(url_loader(), response); |
164 message_loop_.RunAllPending(); | 164 message_loop_.RunUntilIdle(); |
165 } | 165 } |
166 | 166 |
167 void ReceiveData(int size) { | 167 void ReceiveData(int size) { |
168 scoped_array<char> data(new char[size]); | 168 scoped_array<char> data(new char[size]); |
169 memset(data.get(), 0xA5, size); // Arbitrary non-zero value. | 169 memset(data.get(), 0xA5, size); // Arbitrary non-zero value. |
170 | 170 |
171 loader()->didReceiveData(url_loader(), data.get(), size, size); | 171 loader()->didReceiveData(url_loader(), data.get(), size, size); |
172 message_loop_.RunAllPending(); | 172 message_loop_.RunUntilIdle(); |
173 } | 173 } |
174 | 174 |
175 void FinishLoading() { | 175 void FinishLoading() { |
176 data_source_->set_loading(false); | 176 data_source_->set_loading(false); |
177 loader()->didFinishLoading(url_loader(), 0); | 177 loader()->didFinishLoading(url_loader(), 0); |
178 message_loop_.RunAllPending(); | 178 message_loop_.RunUntilIdle(); |
179 } | 179 } |
180 | 180 |
181 MOCK_METHOD1(ReadCallback, void(int size)); | 181 MOCK_METHOD1(ReadCallback, void(int size)); |
182 | 182 |
183 void ReadAt(int64 position) { | 183 void ReadAt(int64 position) { |
184 data_source_->Read(position, kDataSize, buffer_, | 184 data_source_->Read(position, kDataSize, buffer_, |
185 base::Bind(&BufferedDataSourceTest::ReadCallback, | 185 base::Bind(&BufferedDataSourceTest::ReadCallback, |
186 base::Unretained(this))); | 186 base::Unretained(this))); |
187 message_loop_.RunAllPending(); | 187 message_loop_.RunUntilIdle(); |
188 } | 188 } |
189 | 189 |
190 // Accessors for private variables on |data_source_|. | 190 // Accessors for private variables on |data_source_|. |
191 BufferedResourceLoader* loader() { | 191 BufferedResourceLoader* loader() { |
192 return data_source_->loader_.get(); | 192 return data_source_->loader_.get(); |
193 } | 193 } |
194 WebURLLoader* url_loader() { | 194 WebURLLoader* url_loader() { |
195 return loader()->active_loader_->loader_.get(); | 195 return loader()->active_loader_->loader_.get(); |
196 } | 196 } |
197 | 197 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 326 |
327 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) { | 327 TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) { |
328 InitializeWith206Response(); | 328 InitializeWith206Response(); |
329 | 329 |
330 // Make sure there's a pending read -- we'll expect it to error. | 330 // Make sure there's a pending read -- we'll expect it to error. |
331 ReadAt(0); | 331 ReadAt(0); |
332 | 332 |
333 // Abort!!! | 333 // Abort!!! |
334 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 334 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
335 data_source_->Abort(); | 335 data_source_->Abort(); |
336 message_loop_.RunAllPending(); | 336 message_loop_.RunUntilIdle(); |
337 | 337 |
338 EXPECT_FALSE(data_source_->loading()); | 338 EXPECT_FALSE(data_source_->loading()); |
339 Stop(); | 339 Stop(); |
340 } | 340 } |
341 | 341 |
342 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) { | 342 TEST_F(BufferedDataSourceTest, File_AbortWhileReading) { |
343 InitializeWithFileResponse(); | 343 InitializeWithFileResponse(); |
344 | 344 |
345 // Make sure there's a pending read -- we'll expect it to error. | 345 // Make sure there's a pending read -- we'll expect it to error. |
346 ReadAt(0); | 346 ReadAt(0); |
347 | 347 |
348 // Abort!!! | 348 // Abort!!! |
349 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 349 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
350 data_source_->Abort(); | 350 data_source_->Abort(); |
351 message_loop_.RunAllPending(); | 351 message_loop_.RunUntilIdle(); |
352 | 352 |
353 EXPECT_FALSE(data_source_->loading()); | 353 EXPECT_FALSE(data_source_->loading()); |
354 Stop(); | 354 Stop(); |
355 } | 355 } |
356 | 356 |
357 TEST_F(BufferedDataSourceTest, Http_Retry) { | 357 TEST_F(BufferedDataSourceTest, Http_Retry) { |
358 InitializeWith206Response(); | 358 InitializeWith206Response(); |
359 | 359 |
360 // Read to advance our position. | 360 // Read to advance our position. |
361 EXPECT_CALL(*this, ReadCallback(kDataSize)); | 361 EXPECT_CALL(*this, ReadCallback(kDataSize)); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 | 487 |
488 // Stop() the data source, using a callback that lets us verify that it was | 488 // Stop() the data source, using a callback that lets us verify that it was |
489 // called before Stop() returns. This is to make sure that the callback does | 489 // called before Stop() returns. This is to make sure that the callback does |
490 // not require |message_loop_| to execute tasks before being called. | 490 // not require |message_loop_| to execute tasks before being called. |
491 bool stop_done_called = false; | 491 bool stop_done_called = false; |
492 EXPECT_TRUE(data_source_->loading()); | 492 EXPECT_TRUE(data_source_->loading()); |
493 data_source_->Stop(base::Bind(&SetTrue, &stop_done_called)); | 493 data_source_->Stop(base::Bind(&SetTrue, &stop_done_called)); |
494 | 494 |
495 // Verify that the callback was called inside the Stop() call. | 495 // Verify that the callback was called inside the Stop() call. |
496 EXPECT_TRUE(stop_done_called); | 496 EXPECT_TRUE(stop_done_called); |
497 message_loop_.RunAllPending(); | 497 message_loop_.RunUntilIdle(); |
498 } | 498 } |
499 | 499 |
500 TEST_F(BufferedDataSourceTest, DefaultValues) { | 500 TEST_F(BufferedDataSourceTest, DefaultValues) { |
501 InitializeWith206Response(); | 501 InitializeWith206Response(); |
502 | 502 |
503 // Ensure we have sane values for default loading scenario. | 503 // Ensure we have sane values for default loading scenario. |
504 EXPECT_EQ(AUTO, preload()); | 504 EXPECT_EQ(AUTO, preload()); |
505 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); | 505 EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy()); |
506 | 506 |
507 EXPECT_EQ(0, data_source_bitrate()); | 507 EXPECT_EQ(0, data_source_bitrate()); |
508 EXPECT_EQ(0.0f, data_source_playback_rate()); | 508 EXPECT_EQ(0.0f, data_source_playback_rate()); |
509 EXPECT_EQ(0, loader_bitrate()); | 509 EXPECT_EQ(0, loader_bitrate()); |
510 EXPECT_EQ(0.0f, loader_playback_rate()); | 510 EXPECT_EQ(0.0f, loader_playback_rate()); |
511 | 511 |
512 EXPECT_TRUE(data_source_->loading()); | 512 EXPECT_TRUE(data_source_->loading()); |
513 Stop(); | 513 Stop(); |
514 } | 514 } |
515 | 515 |
516 TEST_F(BufferedDataSourceTest, SetBitrate) { | 516 TEST_F(BufferedDataSourceTest, SetBitrate) { |
517 InitializeWith206Response(); | 517 InitializeWith206Response(); |
518 | 518 |
519 data_source_->SetBitrate(1234); | 519 data_source_->SetBitrate(1234); |
520 message_loop_.RunAllPending(); | 520 message_loop_.RunUntilIdle(); |
521 EXPECT_EQ(1234, data_source_bitrate()); | 521 EXPECT_EQ(1234, data_source_bitrate()); |
522 EXPECT_EQ(1234, loader_bitrate()); | 522 EXPECT_EQ(1234, loader_bitrate()); |
523 | 523 |
524 // Read so far ahead to cause the loader to get recreated. | 524 // Read so far ahead to cause the loader to get recreated. |
525 BufferedResourceLoader* old_loader = loader(); | 525 BufferedResourceLoader* old_loader = loader(); |
526 ExpectCreateResourceLoader(); | 526 ExpectCreateResourceLoader(); |
527 ReadAt(kFarReadPosition); | 527 ReadAt(kFarReadPosition); |
528 Respond(response_generator_->Generate206(kFarReadPosition)); | 528 Respond(response_generator_->Generate206(kFarReadPosition)); |
529 | 529 |
530 // Verify loader changed but still has same bitrate. | 530 // Verify loader changed but still has same bitrate. |
531 EXPECT_NE(old_loader, loader()); | 531 EXPECT_NE(old_loader, loader()); |
532 EXPECT_EQ(1234, loader_bitrate()); | 532 EXPECT_EQ(1234, loader_bitrate()); |
533 | 533 |
534 EXPECT_TRUE(data_source_->loading()); | 534 EXPECT_TRUE(data_source_->loading()); |
535 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 535 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
536 Stop(); | 536 Stop(); |
537 } | 537 } |
538 | 538 |
539 TEST_F(BufferedDataSourceTest, SetPlaybackRate) { | 539 TEST_F(BufferedDataSourceTest, SetPlaybackRate) { |
540 InitializeWith206Response(); | 540 InitializeWith206Response(); |
541 | 541 |
542 data_source_->SetPlaybackRate(2.0f); | 542 data_source_->SetPlaybackRate(2.0f); |
543 message_loop_.RunAllPending(); | 543 message_loop_.RunUntilIdle(); |
544 EXPECT_EQ(2.0f, data_source_playback_rate()); | 544 EXPECT_EQ(2.0f, data_source_playback_rate()); |
545 EXPECT_EQ(2.0f, loader_playback_rate()); | 545 EXPECT_EQ(2.0f, loader_playback_rate()); |
546 | 546 |
547 // Read so far ahead to cause the loader to get recreated. | 547 // Read so far ahead to cause the loader to get recreated. |
548 BufferedResourceLoader* old_loader = loader(); | 548 BufferedResourceLoader* old_loader = loader(); |
549 ExpectCreateResourceLoader(); | 549 ExpectCreateResourceLoader(); |
550 ReadAt(kFarReadPosition); | 550 ReadAt(kFarReadPosition); |
551 Respond(response_generator_->Generate206(kFarReadPosition)); | 551 Respond(response_generator_->Generate206(kFarReadPosition)); |
552 | 552 |
553 // Verify loader changed but still has same playback rate. | 553 // Verify loader changed but still has same playback rate. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 InitializeWithFileResponse(); | 605 InitializeWithFileResponse(); |
606 | 606 |
607 EXPECT_FALSE(data_source_->downloading()); | 607 EXPECT_FALSE(data_source_->downloading()); |
608 FinishLoading(); | 608 FinishLoading(); |
609 EXPECT_FALSE(data_source_->downloading()); | 609 EXPECT_FALSE(data_source_->downloading()); |
610 | 610 |
611 Stop(); | 611 Stop(); |
612 } | 612 } |
613 | 613 |
614 } // namespace webkit_media | 614 } // namespace webkit_media |
OLD | NEW |