| 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; |
| 23 using ::testing::InvokeWithoutArgs; |
| 22 using ::testing::NotNull; | 24 using ::testing::NotNull; |
| 23 using ::testing::Return; | 25 using ::testing::Return; |
| 24 using ::testing::SetArgumentPointee; | 26 using ::testing::SetArgumentPointee; |
| 25 using ::testing::StrictMock; | 27 using ::testing::StrictMock; |
| 26 using ::testing::WithArgs; | 28 using ::testing::WithArgs; |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 const char* kHttpUrl = "http://test"; | 32 const char* kHttpUrl = "http://test"; |
| 31 const int kDataSize = 1024; | 33 const int kDataSize = 1024; |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 static media::FilterFactory* CreateFactory( | 342 static media::FilterFactory* CreateFactory( |
| 341 MessageLoop* message_loop, | 343 MessageLoop* message_loop, |
| 342 MediaResourceLoaderBridgeFactory* bridge_factory) { | 344 MediaResourceLoaderBridgeFactory* bridge_factory) { |
| 343 return new media::FilterFactoryImpl2< | 345 return new media::FilterFactoryImpl2< |
| 344 MockBufferedDataSource, | 346 MockBufferedDataSource, |
| 345 MessageLoop*, | 347 MessageLoop*, |
| 346 MediaResourceLoaderBridgeFactory*>(message_loop, | 348 MediaResourceLoaderBridgeFactory*>(message_loop, |
| 347 bridge_factory); | 349 bridge_factory); |
| 348 } | 350 } |
| 349 | 351 |
| 352 virtual base::TimeDelta GetTimeoutMilliseconds() { |
| 353 // It is 100 ms because we don't want the test to run too long. |
| 354 return base::TimeDelta::FromMilliseconds(100); |
| 355 } |
| 356 |
| 350 MOCK_METHOD2(CreateLoader, BufferedResourceLoader*(int64 first_position, | 357 MOCK_METHOD2(CreateLoader, BufferedResourceLoader*(int64 first_position, |
| 351 int64 last_position)); | 358 int64 last_position)); |
| 352 | 359 |
| 353 protected: | 360 protected: |
| 354 MockBufferedDataSource( | 361 MockBufferedDataSource( |
| 355 MessageLoop* message_loop, | 362 MessageLoop* message_loop, |
| 356 MediaResourceLoaderBridgeFactory* factory) | 363 MediaResourceLoaderBridgeFactory* factory) |
| 357 : BufferedDataSource(message_loop, factory) { | 364 : BufferedDataSource(message_loop, factory) { |
| 358 } | 365 } |
| 359 | 366 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 377 bridge_factory_.get()); | 384 bridge_factory_.get()); |
| 378 | 385 |
| 379 // Prepare test data. | 386 // Prepare test data. |
| 380 for (size_t i = 0; i < sizeof(data_); ++i) { | 387 for (size_t i = 0; i < sizeof(data_); ++i) { |
| 381 data_[i] = i; | 388 data_[i] = i; |
| 382 } | 389 } |
| 383 } | 390 } |
| 384 | 391 |
| 385 ~BufferedDataSourceTest() { | 392 ~BufferedDataSourceTest() { |
| 386 if (data_source_) { | 393 if (data_source_) { |
| 394 // Release the bridge factory because we don't own it. |
| 387 // Expects bridge factory to be destroyed along with data source. | 395 // Expects bridge factory to be destroyed along with data source. |
| 388 EXPECT_CALL(*bridge_factory_, OnDestroy()) | 396 EXPECT_CALL(*bridge_factory_, OnDestroy()) |
| 389 .WillOnce(Invoke(this, | 397 .WillOnce(Invoke(this, |
| 390 &BufferedDataSourceTest::ReleaseBridgeFactory)); | 398 &BufferedDataSourceTest::ReleaseBridgeFactory)); |
| 391 } | 399 } |
| 392 | 400 |
| 393 // We don't own the message loop so release it. | 401 // We don't own the message loop so release it. |
| 394 message_loop_.release(); | 402 message_loop_.release(); |
| 395 } | 403 } |
| 396 | 404 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 // 4. The the loader is destroyed. | 569 // 4. The the loader is destroyed. |
| 562 EXPECT_CALL(*loader_, Stop()); | 570 EXPECT_CALL(*loader_, Stop()); |
| 563 | 571 |
| 564 data_source_->Read( | 572 data_source_->Read( |
| 565 position, size, buffer_, | 573 position, size, buffer_, |
| 566 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); | 574 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); |
| 567 | 575 |
| 568 message_loop_->RunAllPending(); | 576 message_loop_->RunAllPending(); |
| 569 } | 577 } |
| 570 | 578 |
| 579 void ReadDataSourceTimesOut(int64 position, int size) { |
| 580 InSequence s; |
| 581 // 1. Drop the request and let it times out. |
| 582 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) |
| 583 .WillOnce(DeleteArg<3>()); |
| 584 |
| 585 // 2. Then the current loader will be stop and destroyed. |
| 586 StrictMock<MockBufferedResourceLoader> *new_loader = |
| 587 new StrictMock<MockBufferedResourceLoader>(); |
| 588 EXPECT_CALL(*loader_, Stop()); |
| 589 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) |
| 590 .WillOnce(Return(new_loader)); |
| 591 EXPECT_CALL(*loader_, OnDestroy()) |
| 592 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader)); |
| 593 |
| 594 // 3. Then the new loader will be started. |
| 595 EXPECT_CALL(*new_loader, Start(NotNull())) |
| 596 .WillOnce(DoAll(Assign(&error_, net::OK), |
| 597 Invoke(this, |
| 598 &BufferedDataSourceTest::InvokeStartCallback))); |
| 599 |
| 600 // 4. Then again a read request is made to the new loader. |
| 601 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) |
| 602 .WillOnce(DoAll(Assign(&error_, size), |
| 603 Invoke(this, |
| 604 &BufferedDataSourceTest::InvokeReadCallback), |
| 605 InvokeWithoutArgs(message_loop_.get(), |
| 606 &MessageLoop::Quit))); |
| 607 |
| 608 EXPECT_CALL(*this, ReadCallback(size)); |
| 609 |
| 610 data_source_->Read( |
| 611 position, size, buffer_, |
| 612 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); |
| 613 |
| 614 // This blocks the current thread until the watch task is executed and |
| 615 // triggers a read callback to quit this message loop. |
| 616 message_loop_->Run(); |
| 617 |
| 618 // Make sure data is correct. |
| 619 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); |
| 620 |
| 621 EXPECT_TRUE(loader_.get() == NULL); |
| 622 loader_.reset(new_loader); |
| 623 } |
| 624 |
| 571 MOCK_METHOD1(ReadCallback, void(size_t size)); | 625 MOCK_METHOD1(ReadCallback, void(size_t size)); |
| 572 | 626 |
| 573 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > | 627 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > |
| 574 bridge_factory_; | 628 bridge_factory_; |
| 575 scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_; | 629 scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_; |
| 576 scoped_refptr<MockBufferedDataSource > data_source_; | 630 scoped_refptr<MockBufferedDataSource > data_source_; |
| 577 scoped_refptr<media::FilterFactory> factory_; | 631 scoped_refptr<media::FilterFactory> factory_; |
| 578 | 632 |
| 579 StrictMock<media::MockFilterHost> host_; | 633 StrictMock<media::MockFilterHost> host_; |
| 580 GURL gurl_; | 634 GURL gurl_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 StopDataSource(); | 671 StopDataSource(); |
| 618 } | 672 } |
| 619 | 673 |
| 620 TEST_F(BufferedDataSourceTest, ReadFailed) { | 674 TEST_F(BufferedDataSourceTest, ReadFailed) { |
| 621 InitializeDataSource(kHttpUrl, net::OK, 1024); | 675 InitializeDataSource(kHttpUrl, net::OK, 1024); |
| 622 ReadDataSourceHit(10, 10, 10); | 676 ReadDataSourceHit(10, 10, 10); |
| 623 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); | 677 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); |
| 624 StopDataSource(); | 678 StopDataSource(); |
| 625 } | 679 } |
| 626 | 680 |
| 681 TEST_F(BufferedDataSourceTest, ReadTimesOut) { |
| 682 InitializeDataSource(kHttpUrl, net::OK, 1024); |
| 683 ReadDataSourceTimesOut(20, 10); |
| 684 StopDataSource(); |
| 685 } |
| 686 |
| 627 } // namespace webkit_glue | 687 } // namespace webkit_glue |
| OLD | NEW |