diff options
author | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-03 01:49:16 +0000 |
---|---|---|
committer | scherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-03 01:49:16 +0000 |
commit | 8f2bf64884944e8b770852240e34e79f117acbb6 (patch) | |
tree | e7235339bc556dc5f933b75578cb12ca8f2ce905 /webkit/glue/media/buffered_data_source_unittest.cc | |
parent | ebc34fab52eabbb680eee966bb10e8f8a8cbd845 (diff) | |
download | chromium_src-8f2bf64884944e8b770852240e34e79f117acbb6.zip chromium_src-8f2bf64884944e8b770852240e34e79f117acbb6.tar.gz chromium_src-8f2bf64884944e8b770852240e34e79f117acbb6.tar.bz2 |
Revert 68094 - Refactoring BufferedDataSource to work with WebURLLoader instead of a MediaResourceLoaderBridge.
One thing to notice is that both buffered_data_source_unittest and simple_data_source_unittest need to have a way to inject a MockWebURLLoader into the BufferedResourceLoader and the SimpleDataSource. In order to make sure a new one is not created during a Start(), I introduced the function SetURLLoaderForTest and keep_test_loader flag.
Patch by annacc@chromium.org:
http://codereview.chromium.org/3863002/
BUG=16751
TEST=src/xcodebuild/Debug/test_shell_tests --gtest_filter=Buffered*
src/xcodebuild/Debug/test_shell_tests --gtest_filter=Simple*
src/webkit/tools/layout_tests/run_webkit_tests.sh --debug media
webkit/tools/layout_tests/run_webkit_tests.sh --debug http/tests/media
TBR=scherkus@chromium.org
Review URL: http://codereview.chromium.org/5619002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68126 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/media/buffered_data_source_unittest.cc')
-rw-r--r-- | webkit/glue/media/buffered_data_source_unittest.cc | 227 |
1 files changed, 130 insertions, 97 deletions
diff --git a/webkit/glue/media/buffered_data_source_unittest.cc b/webkit/glue/media/buffered_data_source_unittest.cc index 5fad3a4..81103b2 100644 --- a/webkit/glue/media/buffered_data_source_unittest.cc +++ b/webkit/glue/media/buffered_data_source_unittest.cc @@ -13,21 +13,13 @@ #include "media/base/mock_filter_host.h" #include "media/base/mock_filters.h" #include "net/base/net_errors.h" -#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" -#include "third_party/WebKit/WebKit/chromium/public/WebFrameClient.h" -#include "third_party/WebKit/WebKit/chromium/public/WebString.h" -#include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" -#include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" -#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" -#include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" -#include "third_party/WebKit/WebKit/chromium/public/WebView.h" +#include "net/http/http_response_headers.h" #include "webkit/glue/media/buffered_data_source.h" -#include "webkit/glue/mock_webframe.h" -#include "webkit/glue/mock_weburlloader_impl.h" +#include "webkit/glue/media/mock_media_resource_loader_bridge_factory.h" +#include "webkit/glue/mock_resource_loader_bridge.h" using ::testing::_; using ::testing::Assign; -using ::testing::AtLeast; using ::testing::DeleteArg; using ::testing::DoAll; using ::testing::InSequence; @@ -41,22 +33,11 @@ using ::testing::StrictMock; using ::testing::NiceMock; using ::testing::WithArgs; -using WebKit::WebURLError; -using WebKit::WebFrame; -using WebKit::WebFrameClient; -using WebKit::WebString; -using WebKit::WebURLLoader; -using WebKit::WebURLRequest; -using WebKit::WebURLResponse; -using WebKit::WebView; - namespace { const char* kHttpUrl = "http://test"; const char* kFileUrl = "file://test"; const int kDataSize = 1024; -const int kHttpOK = 200; -const int kHttpPartialContent = 206; enum NetworkState { NONE, @@ -71,23 +52,25 @@ namespace webkit_glue { // Submit a request completed event to the resource loader due to request // being canceled. Pretending the event is from external. ACTION_P(RequestCanceled, loader) { - WebURLError error; - error.reason = net::ERR_ABORTED; - error.domain = WebString::fromUTF8(net::kErrorDomain); - loader->didFail(NULL, error); + URLRequestStatus status; + status.set_status(URLRequestStatus::CANCELED); + status.set_os_error(net::ERR_ABORTED); + loader->OnCompletedRequest(status, "", base::Time()); } class BufferedResourceLoaderTest : public testing::Test { public: BufferedResourceLoaderTest() { - url_loader_ = new NiceMock<MockWebURLLoader>(); + bridge_.reset(new StrictMock<MockResourceLoaderBridge>()); for (int i = 0; i < kDataSize; ++i) data_[i] = i; } - virtual ~BufferedResourceLoaderTest() { - ignore_result(frame_.release()); + ~BufferedResourceLoaderTest() { + if (bridge_.get()) + EXPECT_CALL(*bridge_, OnDestroy()); + EXPECT_CALL(bridge_factory_, OnDestroy()); } void Initialize(const char* url, int first_position, int last_position) { @@ -95,11 +78,8 @@ class BufferedResourceLoaderTest : public testing::Test { first_position_ = first_position; last_position_ = last_position; - frame_.reset(new NiceMock<MockWebFrame>()); - - loader_ = new BufferedResourceLoader(gurl_, + loader_ = new BufferedResourceLoader(&bridge_factory_, gurl_, first_position_, last_position_); - loader_->SetURLLoaderForTest(url_loader_); } void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) { @@ -109,23 +89,25 @@ class BufferedResourceLoaderTest : public testing::Test { void Start() { InSequence s; - EXPECT_CALL(*url_loader_, loadAsynchronously(_, loader_.get())); + EXPECT_CALL(bridge_factory_, + CreateBridge(gurl_, _, first_position_, last_position_)) + .WillOnce(Return(bridge_.get())); + EXPECT_CALL(*bridge_, Start(loader_.get())); loader_->Start( NewCallback(this, &BufferedResourceLoaderTest::StartCallback), - NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback), - frame_.get()); + NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback)); } void FullResponse(int64 instance_size) { EXPECT_CALL(*this, StartCallback(net::OK)); - - WebURLResponse response(gurl_); - response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), - WebString::fromUTF8(base::StringPrintf("%" - PRId64, instance_size))); - response.setExpectedContentLength(instance_size); - response.setHTTPStatusCode(kHttpOK); - loader_->didReceiveResponse(url_loader_, response); + ResourceResponseInfo info; + std::string header = base::StringPrintf("HTTP/1.1 200 OK\n" + "Content-Length: %" PRId64, + instance_size); + replace(header.begin(), header.end(), '\n', '\0'); + info.headers = new net::HttpResponseHeaders(header); + info.content_length = instance_size; + loader_->OnReceivedResponse(info, false); EXPECT_EQ(instance_size, loader_->content_length()); EXPECT_EQ(instance_size, loader_->instance_size()); EXPECT_FALSE(loader_->partial_response()); @@ -135,17 +117,17 @@ class BufferedResourceLoaderTest : public testing::Test { int64 instance_size) { EXPECT_CALL(*this, StartCallback(net::OK)); int64 content_length = last_position - first_position + 1; - - WebURLResponse response(gurl_); - response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), - WebString::fromUTF8(base::StringPrintf("bytes " + ResourceResponseInfo info; + std::string header = base::StringPrintf("HTTP/1.1 206 Partial Content\n" + "Content-Range: bytes " "%" PRId64 "-%" PRId64 "/%" PRId64, first_position, last_position, - instance_size))); - response.setExpectedContentLength(content_length); - response.setHTTPStatusCode(kHttpPartialContent); - loader_->didReceiveResponse(url_loader_, response); + instance_size); + replace(header.begin(), header.end(), '\n', '\0'); + info.headers = new net::HttpResponseHeaders(header); + info.content_length = content_length; + loader_->OnReceivedResponse(info, false); EXPECT_EQ(content_length, loader_->content_length()); EXPECT_EQ(instance_size, loader_->instance_size()); EXPECT_TRUE(loader_->partial_response()); @@ -153,17 +135,22 @@ class BufferedResourceLoaderTest : public testing::Test { void StopWhenLoad() { InSequence s; - EXPECT_CALL(*url_loader_, cancel()) + EXPECT_CALL(*bridge_, Cancel()) .WillOnce(RequestCanceled(loader_)); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); loader_->Stop(); } + void ReleaseBridge() { + ignore_result(bridge_.release()); + } + // Helper method to write to |loader_| from |data_|. void WriteLoader(int position, int size) { EXPECT_CALL(*this, NetworkCallback()) .RetiresOnSaturation(); - loader_->didReceiveData(url_loader_, - reinterpret_cast<char*>(data_ + position), size); + loader_->OnReceivedData(reinterpret_cast<char*>(data_ + position), size); } // Helper method to read from |loader_|. @@ -180,7 +167,7 @@ class BufferedResourceLoaderTest : public testing::Test { // Helper method to disallow deferring in |loader_|. void DisallowLoaderDefer() { if (loader_->deferred_) { - EXPECT_CALL(*url_loader_, setDefersLoading(false)); + EXPECT_CALL(*bridge_, SetDefersLoading(false)); EXPECT_CALL(*this, NetworkCallback()); } loader_->SetAllowDefer(false); @@ -201,8 +188,8 @@ class BufferedResourceLoaderTest : public testing::Test { int64 last_position_; scoped_refptr<BufferedResourceLoader> loader_; - NiceMock<MockWebURLLoader>* url_loader_; - scoped_ptr<NiceMock<MockWebFrame> > frame_; + StrictMock<MockMediaResourceLoaderBridgeFactory> bridge_factory_; + scoped_ptr<StrictMock<MockResourceLoaderBridge> > bridge_; uint8 data_[kDataSize]; @@ -216,19 +203,35 @@ TEST_F(BufferedResourceLoaderTest, StartStop) { StopWhenLoad(); } +// Tests that HTTP header is missing in the response. +TEST_F(BufferedResourceLoaderTest, MissingHttpHeader) { + Initialize(kHttpUrl, -1, -1); + Start(); + + EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); + EXPECT_CALL(*bridge_, Cancel()) + .WillOnce(RequestCanceled(loader_)); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); + + ResourceResponseInfo info; + loader_->OnReceivedResponse(info, false); +} + // Tests that a bad HTTP response is recived, e.g. file not found. TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { Initialize(kHttpUrl, -1, -1); Start(); EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); - EXPECT_CALL(*url_loader_, cancel()) + EXPECT_CALL(*bridge_, Cancel()) .WillOnce(RequestCanceled(loader_)); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); - WebURLResponse response(gurl_); - response.setHTTPStatusCode(404); - response.setHTTPStatusText("Not Found\n"); - loader_->didReceiveResponse(url_loader_, response); + ResourceResponseInfo info; + info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Not Found\n"); + loader_->OnReceivedResponse(info, false); } // Tests that partial content is requested but not fulfilled. @@ -261,16 +264,19 @@ TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { Start(); EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); - EXPECT_CALL(*url_loader_, cancel()) + EXPECT_CALL(*bridge_, Cancel()) .WillOnce(RequestCanceled(loader_)); - - WebURLResponse response(gurl_); - response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), - WebString::fromUTF8(base::StringPrintf("bytes " - "%d-%d/%d", 1, 10, 1024))); - response.setExpectedContentLength(10); - response.setHTTPStatusCode(kHttpPartialContent); - loader_->didReceiveResponse(url_loader_, response); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); + + ResourceResponseInfo info; + std::string header = base::StringPrintf("HTTP/1.1 206 Partial Content\n" + "Content-Range: bytes %d-%d/%d", + 1, 10, 1024); + replace(header.begin(), header.end(), '\n', '\0'); + info.headers = new net::HttpResponseHeaders(header); + info.content_length = 10; + loader_->OnReceivedResponse(info, false); } // Tests the logic of sliding window for data buffering and reading. @@ -308,7 +314,11 @@ TEST_F(BufferedResourceLoaderTest, BufferAndRead) { // Response has completed. EXPECT_CALL(*this, NetworkCallback()); - loader_->didFinishLoading(url_loader_, 0); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); + URLRequestStatus status; + status.set_status(URLRequestStatus::SUCCESS); + loader_->OnCompletedRequest(status, "", base::Time()); // Try to read 10 from position 25 will just return with 5 bytes. EXPECT_CALL(*this, ReadCallback(5)); @@ -350,7 +360,11 @@ TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { EXPECT_CALL(*this, ReadCallback(5)); EXPECT_CALL(*this, NetworkCallback()); - loader_->didFinishLoading(url_loader_, 0); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); + URLRequestStatus status; + status.set_status(URLRequestStatus::SUCCESS); + loader_->OnCompletedRequest(status, "", base::Time()); } TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { @@ -364,9 +378,11 @@ TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { ReadLoader(10, 10, buffer); EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); EXPECT_CALL(*this, NetworkCallback()); - WebURLError error; - error.reason = net::ERR_FAILED; - loader_->didFail(url_loader_, error); + EXPECT_CALL(*bridge_, OnDestroy()) + .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); + URLRequestStatus status; + status.set_status(URLRequestStatus::FAILED); + loader_->OnCompletedRequest(status, "", base::Time()); } // Tests the logic of caching data to disk when media is paused. @@ -433,7 +449,7 @@ TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredNoDataReceived) { // Start in deferred state, then disallow defer, receive no data, and // allow defer and read. - EXPECT_CALL(*url_loader_, setDefersLoading(true)); + EXPECT_CALL(*bridge_, SetDefersLoading(true)); EXPECT_CALL(*this, NetworkCallback()); WriteLoader(10, 40); @@ -456,7 +472,7 @@ TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadSameWindow) { // Start in deferred state, disallow defer, receive data and shift buffer // window, allow defer, and read in a place that's still in the window. - EXPECT_CALL(*url_loader_, setDefersLoading(true)); + EXPECT_CALL(*bridge_, SetDefersLoading(true)); EXPECT_CALL(*this, NetworkCallback()); WriteLoader(10, 30); @@ -480,7 +496,7 @@ TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) { // Start in deferred state, disallow defer, receive data and shift buffer // window, allow defer, and read outside of the buffer window. - EXPECT_CALL(*url_loader_, setDefersLoading(true)); + EXPECT_CALL(*bridge_, SetDefersLoading(true)); EXPECT_CALL(*this, NetworkCallback()); WriteLoader(10, 40); @@ -493,16 +509,16 @@ TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) { ReadLoader(20, 5, buffer); StopWhenLoad(); } + // TODO(hclam): add unit test for defer loading. class MockBufferedResourceLoader : public BufferedResourceLoader { public: - MockBufferedResourceLoader() : BufferedResourceLoader(GURL(), 0, 0) { + MockBufferedResourceLoader() : BufferedResourceLoader(NULL, GURL(), 0, 0) { } - MOCK_METHOD3(Start, void(net::CompletionCallback* read_callback, - NetworkEventCallback* network_callback, - WebFrame* frame)); + MOCK_METHOD2(Start, void(net::CompletionCallback* read_callback, + NetworkEventCallback* network_callback)); MOCK_METHOD0(Stop, void()); MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, net::CompletionCallback* callback)); @@ -525,8 +541,8 @@ class MockBufferedResourceLoader : public BufferedResourceLoader { class MockBufferedDataSource : public BufferedDataSource { public: MockBufferedDataSource( - MessageLoop* message_loop, WebFrame* frame) - : BufferedDataSource(message_loop, frame) { + MessageLoop* message_loop, MediaResourceLoaderBridgeFactory* factory) + : BufferedDataSource(message_loop, factory) { } virtual base::TimeDelta GetTimeoutMilliseconds() { @@ -545,6 +561,8 @@ class BufferedDataSourceTest : public testing::Test { public: BufferedDataSourceTest() { message_loop_ = MessageLoop::current(); + bridge_factory_.reset( + new StrictMock<MockMediaResourceLoaderBridgeFactory>()); // Prepare test data. for (size_t i = 0; i < sizeof(data_); ++i) { @@ -553,14 +571,20 @@ class BufferedDataSourceTest : public testing::Test { } virtual ~BufferedDataSourceTest() { - ignore_result(frame_.release()); + if (data_source_) { + // Release the bridge factory because we don't own it. + // Expects bridge factory to be destroyed along with data source. + EXPECT_CALL(*bridge_factory_, OnDestroy()) + .WillOnce(Invoke(this, + &BufferedDataSourceTest::ReleaseBridgeFactory)); + } } void ExpectCreateAndStartResourceLoader(int start_error) { EXPECT_CALL(*data_source_, CreateResourceLoader(_, _)) .WillOnce(Return(loader_.get())); - EXPECT_CALL(*loader_, Start(NotNull(), NotNull(), NotNull())) + EXPECT_CALL(*loader_, Start(NotNull(), NotNull())) .WillOnce( DoAll(Assign(&error_, start_error), Invoke(this, @@ -573,10 +597,15 @@ class BufferedDataSourceTest : public testing::Test { // Saves the url first. gurl_ = GURL(url); - frame_.reset(new NiceMock<MockWebFrame>()); - + media::MediaFormat url_format; + url_format.SetAsString(media::MediaFormat::kMimeType, + media::mime_type::kURL); + url_format.SetAsString(media::MediaFormat::kURL, url); data_source_ = new MockBufferedDataSource(MessageLoop::current(), - frame_.get()); + bridge_factory_.get()); + CHECK(data_source_); + + // There is no need to provide a message loop to data source. data_source_->set_host(&host_); scoped_refptr<NiceMock<MockBufferedResourceLoader> > first_loader( @@ -602,7 +631,7 @@ class BufferedDataSourceTest : public testing::Test { // Replace loader_ with a new instance. loader_ = new NiceMock<MockBufferedResourceLoader>(); - // Create and start. Make sure Start() is called on the new loader. + // Create and start Make sure Start() is called the new loader. ExpectCreateAndStartResourceLoader(net::OK); // Update initialization variable since we know the second loader will @@ -676,10 +705,13 @@ class BufferedDataSourceTest : public testing::Test { message_loop_->RunAllPending(); } + void ReleaseBridgeFactory() { + ignore_result(bridge_factory_.release()); + } + void InvokeStartCallback( net::CompletionCallback* callback, - BufferedResourceLoader::NetworkEventCallback* network_callback, - WebFrame* frame) { + BufferedResourceLoader::NetworkEventCallback* network_callback) { callback->RunWithParams(Tuple1<int>(error_)); delete callback; // TODO(hclam): Save this callback. @@ -758,7 +790,7 @@ class BufferedDataSourceTest : public testing::Test { .WillOnce(Return(new_loader)); // 3. Then the new loader will be started. - EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull())) + EXPECT_CALL(*new_loader, Start(NotNull(), NotNull())) .WillOnce(DoAll(Assign(&error_, net::OK), Invoke(this, &BufferedDataSourceTest::InvokeStartCallback))); @@ -824,7 +856,7 @@ class BufferedDataSourceTest : public testing::Test { // 3. Then the new loader will be started and respond to queries about // whether this is a partial response using the value of the previous // loader. - EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull())) + EXPECT_CALL(*new_loader, Start(NotNull(), NotNull())) .WillOnce(DoAll(Assign(&error_, net::OK), Invoke(this, &BufferedDataSourceTest::InvokeStartCallback))); @@ -857,9 +889,10 @@ class BufferedDataSourceTest : public testing::Test { MOCK_METHOD1(ReadCallback, void(size_t size)); + scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > + bridge_factory_; scoped_refptr<NiceMock<MockBufferedResourceLoader> > loader_; scoped_refptr<MockBufferedDataSource> data_source_; - scoped_ptr<NiceMock<MockWebFrame> > frame_; StrictMock<media::MockFilterHost> host_; GURL gurl_; |