| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "media/base/filters.h" | 8 #include "media/base/filters.h" |
| 9 #include "media/base/mock_filter_host.h" | 9 #include "media/base/mock_filter_host.h" |
| 10 #include "media/base/mock_filters.h" | 10 #include "media/base/mock_filters.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 13 #include "webkit/glue/media/buffered_data_source.h" | 13 #include "webkit/glue/media/buffered_data_source.h" |
| 14 #include "webkit/glue/media/mock_media_resource_loader_bridge_factory.h" | 14 #include "webkit/glue/media/mock_media_resource_loader_bridge_factory.h" |
| 15 #include "webkit/glue/mock_resource_loader_bridge.h" | 15 #include "webkit/glue/mock_resource_loader_bridge.h" |
| 16 | 16 |
| 17 using ::testing::_; | 17 using ::testing::_; |
| 18 using ::testing::Assign; | 18 using ::testing::Assign; |
| 19 using ::testing::DeleteArg; |
| 19 using ::testing::DoAll; | 20 using ::testing::DoAll; |
| 20 using ::testing::InSequence; | 21 using ::testing::InSequence; |
| 21 using ::testing::Invoke; | 22 using ::testing::Invoke; |
| 22 using ::testing::NotNull; | 23 using ::testing::NotNull; |
| 23 using ::testing::Return; | 24 using ::testing::Return; |
| 24 using ::testing::SetArgumentPointee; | 25 using ::testing::SetArgumentPointee; |
| 25 using ::testing::StrictMock; | 26 using ::testing::StrictMock; |
| 26 using ::testing::WithArgs; | 27 using ::testing::WithArgs; |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 void Start() { | 63 void Start() { |
| 63 InSequence s; | 64 InSequence s; |
| 64 EXPECT_CALL(bridge_factory_, | 65 EXPECT_CALL(bridge_factory_, |
| 65 CreateBridge(gurl_, _, first_position_, last_position_)) | 66 CreateBridge(gurl_, _, first_position_, last_position_)) |
| 66 .WillOnce(Return(bridge_.get())); | 67 .WillOnce(Return(bridge_.get())); |
| 67 EXPECT_CALL(*bridge_, Start(loader_.get())); | 68 EXPECT_CALL(*bridge_, Start(loader_.get())); |
| 68 loader_->Start(NewCallback(this, | 69 loader_->Start(NewCallback(this, |
| 69 &BufferedResourceLoaderTest::StartCallback)); | 70 &BufferedResourceLoaderTest::StartCallback)); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void FullResponse(int64 content_length) { | 73 void FullResponse(int64 instance_size) { |
| 73 EXPECT_CALL(*this, StartCallback(net::OK)); | 74 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 74 ResourceLoaderBridge::ResponseInfo info; | 75 ResourceLoaderBridge::ResponseInfo info; |
| 75 std::string header = StringPrintf("HTTP/1.1 200 OK\n" | 76 std::string header = StringPrintf("HTTP/1.1 200 OK\n" |
| 76 "Content-Length: %lld", content_length); | 77 "Content-Length: %lld", instance_size); |
| 78 replace(header.begin(), header.end(), '\n', '\0'); |
| 79 info.headers = new net::HttpResponseHeaders(header); |
| 80 info.content_length = instance_size; |
| 81 loader_->OnReceivedResponse(info, false); |
| 82 EXPECT_EQ(instance_size, loader_->content_length()); |
| 83 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 84 } |
| 85 |
| 86 void PartialResponse(int64 instance_size) { |
| 87 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 88 int64 content_length = last_position_ - first_position_ + 1; |
| 89 ResourceLoaderBridge::ResponseInfo info; |
| 90 std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" |
| 91 "Content-Range: bytes %lld-%lld/%lld", |
| 92 first_position_, |
| 93 last_position_, |
| 94 instance_size); |
| 77 replace(header.begin(), header.end(), '\n', '\0'); | 95 replace(header.begin(), header.end(), '\n', '\0'); |
| 78 info.headers = new net::HttpResponseHeaders(header); | 96 info.headers = new net::HttpResponseHeaders(header); |
| 79 info.content_length = content_length; | 97 info.content_length = content_length; |
| 80 loader_->OnReceivedResponse(info, false); | 98 loader_->OnReceivedResponse(info, false); |
| 81 EXPECT_EQ(content_length, loader_->content_length()); | 99 EXPECT_EQ(content_length, loader_->content_length()); |
| 82 } | 100 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 83 | |
| 84 void PartialResponse(int64 content_length) { | |
| 85 EXPECT_CALL(*this, StartCallback(net::OK)); | |
| 86 ResourceLoaderBridge::ResponseInfo info; | |
| 87 std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" | |
| 88 "Content-Range: bytes %lld-%lld/%lld", | |
| 89 first_position_, | |
| 90 last_position_, | |
| 91 content_length); | |
| 92 replace(header.begin(), header.end(), '\n', '\0'); | |
| 93 info.headers = new net::HttpResponseHeaders(header); | |
| 94 info.content_length = content_length; | |
| 95 loader_->OnReceivedResponse(info, false); | |
| 96 // TODO(hclam): Right now BufferedResourceLoader doesn't care about the | |
| 97 // partial range replied by the server. Do the check here. | |
| 98 } | 101 } |
| 99 | 102 |
| 100 void StopWhenLoad() { | 103 void StopWhenLoad() { |
| 101 InSequence s; | 104 InSequence s; |
| 102 EXPECT_CALL(*bridge_, Cancel()); | 105 EXPECT_CALL(*bridge_, Cancel()); |
| 103 EXPECT_CALL(*bridge_, OnDestroy()) | 106 EXPECT_CALL(*bridge_, OnDestroy()) |
| 104 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 107 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 105 loader_->Stop(); | 108 loader_->Stop(); |
| 106 } | 109 } |
| 107 | 110 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 ResourceLoaderBridge::ResponseInfo info; | 179 ResourceLoaderBridge::ResponseInfo info; |
| 177 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n"); | 180 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n"); |
| 178 loader_->OnReceivedResponse(info, false); | 181 loader_->OnReceivedResponse(info, false); |
| 179 } | 182 } |
| 180 | 183 |
| 181 // Tests that partial content is requested but not fulfilled. | 184 // Tests that partial content is requested but not fulfilled. |
| 182 TEST_F(BufferedResourceLoaderTest, NotPartialRange) { | 185 TEST_F(BufferedResourceLoaderTest, NotPartialRange) { |
| 183 Initialize(kHttpUrl, 100, -1); | 186 Initialize(kHttpUrl, 100, -1); |
| 184 Start(); | 187 Start(); |
| 185 | 188 |
| 186 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); | 189 EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 187 EXPECT_CALL(*bridge_, Cancel()); | 190 EXPECT_CALL(*bridge_, Cancel()); |
| 188 EXPECT_CALL(*bridge_, OnDestroy()) | 191 EXPECT_CALL(*bridge_, OnDestroy()) |
| 189 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 192 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 190 | 193 |
| 191 ResourceLoaderBridge::ResponseInfo info; | 194 ResourceLoaderBridge::ResponseInfo info; |
| 192 info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n"); | 195 info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n"); |
| 193 loader_->OnReceivedResponse(info, false); | 196 loader_->OnReceivedResponse(info, false); |
| 194 } | 197 } |
| 195 | 198 |
| 196 // Tests that a 200 response is received. | 199 // Tests that a 200 response is received. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 VerifyBuffer(buffer, 20, 5); | 234 VerifyBuffer(buffer, 20, 5); |
| 232 EXPECT_CALL(*this, ReadCallback(5)); | 235 EXPECT_CALL(*this, ReadCallback(5)); |
| 233 ReadLoader(25, 5, buffer); | 236 ReadLoader(25, 5, buffer); |
| 234 VerifyBuffer(buffer, 25, 5); | 237 VerifyBuffer(buffer, 25, 5); |
| 235 | 238 |
| 236 // Read backward within buffer. | 239 // Read backward within buffer. |
| 237 EXPECT_CALL(*this, ReadCallback(10)); | 240 EXPECT_CALL(*this, ReadCallback(10)); |
| 238 ReadLoader(10, 10, buffer); | 241 ReadLoader(10, 10, buffer); |
| 239 VerifyBuffer(buffer, 10, 10); | 242 VerifyBuffer(buffer, 10, 10); |
| 240 | 243 |
| 241 // Read backwith outside buffer. | 244 // Read backward outside buffer. |
| 242 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 245 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 243 ReadLoader(9, 10, buffer); | 246 ReadLoader(9, 10, buffer); |
| 244 | 247 |
| 245 // Response has completed. | 248 // Response has completed. |
| 246 EXPECT_CALL(*bridge_, OnDestroy()) | 249 EXPECT_CALL(*bridge_, OnDestroy()) |
| 247 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 250 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 248 URLRequestStatus status; | 251 URLRequestStatus status; |
| 249 status.set_status(URLRequestStatus::SUCCESS); | 252 status.set_status(URLRequestStatus::SUCCESS); |
| 250 loader_->OnCompletedRequest(status, ""); | 253 loader_->OnCompletedRequest(status, ""); |
| 251 | 254 |
| 252 // Try to read 10 from position 25 will just return with 5 bytes. | 255 // Try to read 10 from position 25 will just return with 5 bytes. |
| 253 EXPECT_CALL(*this, ReadCallback(5)); | 256 EXPECT_CALL(*this, ReadCallback(5)); |
| 254 ReadLoader(25, 10, buffer); | 257 ReadLoader(25, 10, buffer); |
| 255 VerifyBuffer(buffer, 25, 5); | 258 VerifyBuffer(buffer, 25, 5); |
| 256 | 259 |
| 257 // Try to read outside buffered range after request has completed. | 260 // Try to read outside buffered range after request has completed. |
| 258 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 261 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 259 ReadLoader(5, 10, buffer); | 262 ReadLoader(5, 10, buffer); |
| 260 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 263 |
| 264 // Try to read beyond the instance size. |
| 265 EXPECT_CALL(*this, ReadCallback(0)); |
| 261 ReadLoader(30, 10, buffer); | 266 ReadLoader(30, 10, buffer); |
| 262 } | 267 } |
| 263 | 268 |
| 264 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { | 269 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { |
| 265 Initialize(kHttpUrl, 10, 0x00FFFFFF); | 270 Initialize(kHttpUrl, 10, 0x00FFFFFF); |
| 266 Start(); | 271 Start(); |
| 267 PartialResponse(0x01000000); | 272 PartialResponse(0x01000000); |
| 268 | 273 |
| 269 uint8 buffer[10]; | 274 uint8 buffer[10]; |
| 270 InSequence s; | 275 InSequence s; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 324 |
| 320 ~MockBufferedResourceLoader() { | 325 ~MockBufferedResourceLoader() { |
| 321 OnDestroy(); | 326 OnDestroy(); |
| 322 } | 327 } |
| 323 | 328 |
| 324 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback)); | 329 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback)); |
| 325 MOCK_METHOD0(Stop, void()); | 330 MOCK_METHOD0(Stop, void()); |
| 326 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, | 331 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, |
| 327 net::CompletionCallback* callback)); | 332 net::CompletionCallback* callback)); |
| 328 MOCK_METHOD0(content_length, int64()); | 333 MOCK_METHOD0(content_length, int64()); |
| 334 MOCK_METHOD0(instance_size, int64()); |
| 329 MOCK_METHOD0(OnDestroy, void()); | 335 MOCK_METHOD0(OnDestroy, void()); |
| 330 | 336 |
| 331 private: | 337 private: |
| 332 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); | 338 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); |
| 333 }; | 339 }; |
| 334 | 340 |
| 335 // A mock BufferedDataSource to inject mock BufferedResourceLoader through | 341 // A mock BufferedDataSource to inject mock BufferedResourceLoader through |
| 336 // CreateLoader() method. | 342 // CreateLoader() method. |
| 337 class MockBufferedDataSource : public BufferedDataSource { | 343 class MockBufferedDataSource : public BufferedDataSource { |
| 338 public: | 344 public: |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 // Expects bridge factory to be destroyed along with data source. | 393 // Expects bridge factory to be destroyed along with data source. |
| 388 EXPECT_CALL(*bridge_factory_, OnDestroy()) | 394 EXPECT_CALL(*bridge_factory_, OnDestroy()) |
| 389 .WillOnce(Invoke(this, | 395 .WillOnce(Invoke(this, |
| 390 &BufferedDataSourceTest::ReleaseBridgeFactory)); | 396 &BufferedDataSourceTest::ReleaseBridgeFactory)); |
| 391 } | 397 } |
| 392 | 398 |
| 393 // We don't own the message loop so release it. | 399 // We don't own the message loop so release it. |
| 394 message_loop_.release(); | 400 message_loop_.release(); |
| 395 } | 401 } |
| 396 | 402 |
| 397 void InitializeDataSource(const char* url, int error, int64 content_length) { | 403 void InitializeDataSource(const char* url, int error, int probe_error, |
| 404 int64 instance_size) { |
| 398 // Saves the url first. | 405 // Saves the url first. |
| 399 gurl_ = GURL(url); | 406 gurl_ = GURL(url); |
| 400 | 407 |
| 401 media::MediaFormat url_format; | 408 media::MediaFormat url_format; |
| 402 url_format.SetAsString(media::MediaFormat::kMimeType, | 409 url_format.SetAsString(media::MediaFormat::kMimeType, |
| 403 media::mime_type::kURL); | 410 media::mime_type::kURL); |
| 404 url_format.SetAsString(media::MediaFormat::kURL, url); | 411 url_format.SetAsString(media::MediaFormat::kURL, url); |
| 405 data_source_ = factory_->Create<MockBufferedDataSource>(url_format); | 412 data_source_ = factory_->Create<MockBufferedDataSource>(url_format); |
| 406 CHECK(data_source_); | 413 CHECK(data_source_); |
| 407 | 414 |
| 408 // There is no need to provide a message loop to data source. | 415 // There is no need to provide a message loop to data source. |
| 409 data_source_->set_host(&host_); | 416 data_source_->set_host(&host_); |
| 410 | 417 |
| 411 // Creates the first mock loader to be injected. | 418 // Creates the first mock loader to be injected. |
| 412 loader_.reset(new StrictMock<MockBufferedResourceLoader>()); | 419 loader_.reset(new StrictMock<MockBufferedResourceLoader>()); |
| 420 probe_loader_.reset(new StrictMock<MockBufferedResourceLoader>()); |
| 413 | 421 |
| 414 InSequence s; | 422 InSequence s; |
| 415 StrictMock<media::MockFilterCallback> callback; | 423 StrictMock<media::MockFilterCallback> callback; |
| 424 |
| 425 // There is one resource loader with full range will be created. |
| 416 EXPECT_CALL(*data_source_, CreateLoader(-1, -1)) | 426 EXPECT_CALL(*data_source_, CreateLoader(-1, -1)) |
| 417 .WillOnce(Return(loader_.get())); | 427 .WillOnce(Return(loader_.get())); |
| 428 |
| 429 // Then another resource loader with a small partial range is created. |
| 430 EXPECT_CALL(*data_source_, CreateLoader(1, 1)) |
| 431 .WillOnce(Return(probe_loader_.get())); |
| 432 |
| 433 // The initial response loader will be started. |
| 418 EXPECT_CALL(*loader_, Start(NotNull())) | 434 EXPECT_CALL(*loader_, Start(NotNull())) |
| 419 .WillOnce(DoAll(Assign(&error_, error), | 435 .WillOnce(DoAll(Assign(&error_, error), |
| 420 Invoke(this, | 436 Invoke(this, |
| 421 &BufferedDataSourceTest::InvokeStartCallback))); | 437 &BufferedDataSourceTest::InvokeStartCallback))); |
| 422 if (error != net::OK) { | 438 if (error == net::OK) { |
| 439 EXPECT_CALL(*loader_, instance_size()) |
| 440 .WillOnce(Return(instance_size)); |
| 441 if (instance_size != -1) { |
| 442 EXPECT_CALL(host_, SetTotalBytes(instance_size)); |
| 443 EXPECT_CALL(host_, SetBufferedBytes(instance_size)); |
| 444 } else { |
| 445 EXPECT_CALL(host_, SetStreaming(true)); |
| 446 } |
| 447 |
| 448 // Then the probe resource loader will start. |
| 449 EXPECT_CALL(*probe_loader_, Start(NotNull())) |
| 450 .WillOnce(DoAll(Assign(&error_, probe_error), |
| 451 Invoke(this, |
| 452 &BufferedDataSourceTest::InvokeStartCallback))); |
| 453 if (probe_error != net::OK) |
| 454 EXPECT_CALL(host_, SetStreaming(true)); |
| 455 EXPECT_CALL(*probe_loader_, Stop()); |
| 456 EXPECT_CALL(callback, OnFilterCallback()); |
| 457 EXPECT_CALL(callback, OnCallbackDestroyed()); |
| 458 } else { |
| 423 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); | 459 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); |
| 424 EXPECT_CALL(*loader_, Stop()); | 460 EXPECT_CALL(*loader_, Stop()); |
| 425 } else { | 461 EXPECT_CALL(*probe_loader_, Stop()); |
| 426 EXPECT_CALL(*loader_, content_length()) | 462 EXPECT_CALL(callback, OnFilterCallback()); |
| 427 .WillOnce(Return(content_length)); | 463 EXPECT_CALL(callback, OnCallbackDestroyed()); |
| 428 EXPECT_CALL(host_, SetTotalBytes(content_length)); | 464 |
| 429 EXPECT_CALL(host_, SetBufferedBytes(content_length)); | 465 // This expectation looks a little strange, but this is actually what |
| 466 // will happen since we are not running a message loop. So simply |
| 467 // delete the callback. |
| 468 EXPECT_CALL(*probe_loader_, Start(NotNull())) |
| 469 .WillOnce(DeleteArg<0>()); |
| 430 } | 470 } |
| 431 EXPECT_CALL(callback, OnFilterCallback()); | |
| 432 EXPECT_CALL(callback, OnCallbackDestroyed()); | |
| 433 | 471 |
| 434 data_source_->Initialize(url, callback.NewCallback()); | 472 data_source_->Initialize(url, callback.NewCallback()); |
| 435 message_loop_->RunAllPending(); | 473 message_loop_->RunAllPending(); |
| 436 | 474 |
| 437 if (error == net::OK) { | 475 if (error == net::OK) { |
| 476 // Verify the size of the data source. |
| 438 int64 size; | 477 int64 size; |
| 439 EXPECT_TRUE(data_source_->GetSize(&size)); | 478 if (instance_size != -1) { |
| 440 EXPECT_EQ(content_length, size); | 479 EXPECT_TRUE(data_source_->GetSize(&size)); |
| 480 EXPECT_EQ(instance_size, size); |
| 481 |
| 482 if (probe_error == net::OK) { |
| 483 EXPECT_FALSE(data_source_->IsStreaming()); |
| 484 } |
| 485 } else { |
| 486 EXPECT_FALSE(data_source_->GetSize(&size)); |
| 487 EXPECT_EQ(0, size); |
| 488 EXPECT_TRUE(data_source_->IsStreaming()); |
| 489 } |
| 490 |
| 491 // Verify the data source is streamed if the probe has received an error. |
| 492 if (probe_error != net::OK) { |
| 493 EXPECT_TRUE(data_source_->IsStreaming()); |
| 494 } |
| 441 } | 495 } |
| 442 } | 496 } |
| 443 | 497 |
| 444 void StopDataSource() { | 498 void StopDataSource() { |
| 445 if (loader_.get()) { | 499 if (loader_.get()) { |
| 446 InSequence s; | 500 InSequence s; |
| 447 EXPECT_CALL(*loader_, Stop()); | 501 EXPECT_CALL(*loader_, Stop()); |
| 448 EXPECT_CALL(*loader_, OnDestroy()) | 502 EXPECT_CALL(*probe_loader_, Stop()); |
| 449 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader)); | |
| 450 } | 503 } |
| 451 | 504 |
| 505 EXPECT_CALL(*loader_, OnDestroy()) |
| 506 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader)); |
| 507 EXPECT_CALL(*probe_loader_, OnDestroy()) |
| 508 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseProbeLoader)); |
| 509 |
| 452 data_source_->Stop(); | 510 data_source_->Stop(); |
| 453 message_loop_->RunAllPending(); | 511 message_loop_->RunAllPending(); |
| 454 } | 512 } |
| 455 | 513 |
| 456 void ReleaseBridgeFactory() { | 514 void ReleaseBridgeFactory() { |
| 457 bridge_factory_.release(); | 515 bridge_factory_.release(); |
| 458 } | 516 } |
| 459 | 517 |
| 460 void ReleaseLoader() { | 518 void ReleaseLoader() { |
| 461 loader_.release(); | 519 loader_.release(); |
| 462 } | 520 } |
| 463 | 521 |
| 522 void ReleaseProbeLoader() { |
| 523 probe_loader_.release(); |
| 524 } |
| 525 |
| 464 void InvokeStartCallback(net::CompletionCallback* callback) { | 526 void InvokeStartCallback(net::CompletionCallback* callback) { |
| 465 callback->RunWithParams(Tuple1<int>(error_)); | 527 callback->RunWithParams(Tuple1<int>(error_)); |
| 466 delete callback; | 528 delete callback; |
| 467 } | 529 } |
| 468 | 530 |
| 469 void InvokeReadCallback(int64 position, int size, uint8* buffer, | 531 void InvokeReadCallback(int64 position, int size, uint8* buffer, |
| 470 net::CompletionCallback* callback) { | 532 net::CompletionCallback* callback) { |
| 471 if (error_ > 0) | 533 if (error_ > 0) |
| 472 memcpy(buffer, data_ + static_cast<int>(position), error_); | 534 memcpy(buffer, data_ + static_cast<int>(position), error_); |
| 473 callback->RunWithParams(Tuple1<int>(error_)); | 535 callback->RunWithParams(Tuple1<int>(error_)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 void ReadDataSourceFailed(int64 position, int size, int error) { | 607 void ReadDataSourceFailed(int64 position, int size, int error) { |
| 546 EXPECT_TRUE(loader_.get() != NULL); | 608 EXPECT_TRUE(loader_.get() != NULL); |
| 547 | 609 |
| 548 InSequence s; | 610 InSequence s; |
| 549 // 1. Expect the read is delegated to the resource loader. | 611 // 1. Expect the read is delegated to the resource loader. |
| 550 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) | 612 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) |
| 551 .WillOnce(DoAll(Assign(&error_, error), | 613 .WillOnce(DoAll(Assign(&error_, error), |
| 552 Invoke(this, | 614 Invoke(this, |
| 553 &BufferedDataSourceTest::InvokeReadCallback))); | 615 &BufferedDataSourceTest::InvokeReadCallback))); |
| 554 | 616 |
| 555 // 2. The read has failed, so read callback will be called. | 617 // 2. Host will then receive an error. |
| 618 EXPECT_CALL(*loader_, Stop()); |
| 619 |
| 620 // 3. The read has failed, so read callback will be called. |
| 556 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 621 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 557 | 622 |
| 558 // 3. Host will then receive an error. | |
| 559 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); | |
| 560 | |
| 561 // 4. The the loader is destroyed. | |
| 562 EXPECT_CALL(*loader_, Stop()); | |
| 563 | |
| 564 data_source_->Read( | 623 data_source_->Read( |
| 565 position, size, buffer_, | 624 position, size, buffer_, |
| 566 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); | 625 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); |
| 567 | 626 |
| 568 message_loop_->RunAllPending(); | 627 message_loop_->RunAllPending(); |
| 569 } | 628 } |
| 570 | 629 |
| 571 MOCK_METHOD1(ReadCallback, void(size_t size)); | 630 MOCK_METHOD1(ReadCallback, void(size_t size)); |
| 572 | 631 |
| 573 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > | 632 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > |
| 574 bridge_factory_; | 633 bridge_factory_; |
| 575 scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_; | 634 scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_; |
| 635 scoped_ptr<StrictMock<MockBufferedResourceLoader> > probe_loader_; |
| 576 scoped_refptr<MockBufferedDataSource > data_source_; | 636 scoped_refptr<MockBufferedDataSource > data_source_; |
| 577 scoped_refptr<media::FilterFactory> factory_; | 637 scoped_refptr<media::FilterFactory> factory_; |
| 578 | 638 |
| 579 StrictMock<media::MockFilterHost> host_; | 639 StrictMock<media::MockFilterHost> host_; |
| 580 GURL gurl_; | 640 GURL gurl_; |
| 581 scoped_ptr<MessageLoop> message_loop_; | 641 scoped_ptr<MessageLoop> message_loop_; |
| 582 | 642 |
| 583 int error_; | 643 int error_; |
| 584 uint8 buffer_[1024]; | 644 uint8 buffer_[1024]; |
| 585 uint8 data_[1024]; | 645 uint8 data_[1024]; |
| 586 | 646 |
| 587 private: | 647 private: |
| 588 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); | 648 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); |
| 589 }; | 649 }; |
| 590 | 650 |
| 591 TEST_F(BufferedDataSourceTest, InitializationSuccess) { | 651 TEST_F(BufferedDataSourceTest, InitializationSuccess) { |
| 592 InitializeDataSource(kHttpUrl, net::OK, 1024); | 652 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024); |
| 593 StopDataSource(); | 653 StopDataSource(); |
| 594 } | 654 } |
| 595 | 655 |
| 596 TEST_F(BufferedDataSourceTest, InitiailizationFailed) { | 656 TEST_F(BufferedDataSourceTest, InitiailizationFailed) { |
| 597 InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, 0); | 657 InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, |
| 658 net::ERR_FILE_NOT_FOUND, 0); |
| 659 StopDataSource(); |
| 660 } |
| 661 |
| 662 TEST_F(BufferedDataSourceTest, MissingContentLength) { |
| 663 InitializeDataSource(kHttpUrl, net::OK, net::OK, -1); |
| 664 StopDataSource(); |
| 665 } |
| 666 |
| 667 TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) { |
| 668 InitializeDataSource(kHttpUrl, net::OK, |
| 669 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, 1024); |
| 670 StopDataSource(); |
| 671 } |
| 672 |
| 673 TEST_F(BufferedDataSourceTest, |
| 674 MissingContentLengthAndRangeRequestNotSupported) { |
| 675 InitializeDataSource(kHttpUrl, net::OK, |
| 676 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, -1); |
| 598 StopDataSource(); | 677 StopDataSource(); |
| 599 } | 678 } |
| 600 | 679 |
| 601 TEST_F(BufferedDataSourceTest, ReadCacheHit) { | 680 TEST_F(BufferedDataSourceTest, ReadCacheHit) { |
| 602 InitializeDataSource(kHttpUrl, net::OK, 25); | 681 InitializeDataSource(kHttpUrl, net::OK, net::OK, 25); |
| 603 | 682 |
| 604 // Performs read with cache hit. | 683 // Performs read with cache hit. |
| 605 ReadDataSourceHit(10, 10, 10); | 684 ReadDataSourceHit(10, 10, 10); |
| 606 | 685 |
| 607 // Performs read with cache hit but partially filled. | 686 // Performs read with cache hit but partially filled. |
| 608 ReadDataSourceHit(20, 10, 5); | 687 ReadDataSourceHit(20, 10, 5); |
| 609 | 688 |
| 610 StopDataSource(); | 689 StopDataSource(); |
| 611 } | 690 } |
| 612 | 691 |
| 613 TEST_F(BufferedDataSourceTest, ReadCacheMiss) { | 692 TEST_F(BufferedDataSourceTest, ReadCacheMiss) { |
| 614 InitializeDataSource(kHttpUrl, net::OK, 1024); | 693 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024); |
| 615 ReadDataSourceMiss(1000, 10); | 694 ReadDataSourceMiss(1000, 10); |
| 616 ReadDataSourceMiss(20, 10); | 695 ReadDataSourceMiss(20, 10); |
| 617 StopDataSource(); | 696 StopDataSource(); |
| 618 } | 697 } |
| 619 | 698 |
| 620 TEST_F(BufferedDataSourceTest, ReadFailed) { | 699 TEST_F(BufferedDataSourceTest, ReadFailed) { |
| 621 InitializeDataSource(kHttpUrl, net::OK, 1024); | 700 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024); |
| 622 ReadDataSourceHit(10, 10, 10); | 701 ReadDataSourceHit(10, 10, 10); |
| 623 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); | 702 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); |
| 624 StopDataSource(); | 703 StopDataSource(); |
| 625 } | 704 } |
| 626 | 705 |
| 627 } // namespace webkit_glue | 706 } // namespace webkit_glue |
| OLD | NEW |