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