diff options
Diffstat (limited to 'webkit/glue/media/buffered_data_source_unittest.cc')
-rw-r--r-- | webkit/glue/media/buffered_data_source_unittest.cc | 190 |
1 files changed, 94 insertions, 96 deletions
diff --git a/webkit/glue/media/buffered_data_source_unittest.cc b/webkit/glue/media/buffered_data_source_unittest.cc index 003313c..8c72120 100644 --- a/webkit/glue/media/buffered_data_source_unittest.cc +++ b/webkit/glue/media/buffered_data_source_unittest.cc @@ -97,16 +97,18 @@ class BufferedResourceLoaderTest : public testing::Test { loader_->OnReceivedResponse(info, false); EXPECT_EQ(instance_size, loader_->content_length()); EXPECT_EQ(instance_size, loader_->instance_size()); + EXPECT_FALSE(loader_->partial_response()); } - void PartialResponse(int64 instance_size) { + void PartialResponse(int64 first_position, int64 last_position, + int64 instance_size) { EXPECT_CALL(*this, StartCallback(net::OK)); - int64 content_length = last_position_ - first_position_ + 1; + int64 content_length = last_position - first_position + 1; ResourceLoaderBridge::ResponseInfo info; std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" "Content-Range: bytes %lld-%lld/%lld", - first_position_, - last_position_, + first_position, + last_position, instance_size); replace(header.begin(), header.end(), '\n', '\0'); info.headers = new net::HttpResponseHeaders(header); @@ -114,6 +116,7 @@ class BufferedResourceLoaderTest : public testing::Test { loader_->OnReceivedResponse(info, false); EXPECT_EQ(content_length, loader_->content_length()); EXPECT_EQ(instance_size, loader_->instance_size()); + EXPECT_TRUE(loader_->partial_response()); } void StopWhenLoad() { @@ -196,24 +199,16 @@ TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); ResourceLoaderBridge::ResponseInfo info; - info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n"); + 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. -TEST_F(BufferedResourceLoaderTest, NotPartialRange) { +TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { Initialize(kHttpUrl, 100, -1); Start(); - - EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); - EXPECT_CALL(*bridge_, Cancel()) - .WillOnce(RequestCanceled(loader_)); - EXPECT_CALL(*bridge_, OnDestroy()) - .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); - - ResourceLoaderBridge::ResponseInfo info; - info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n"); - loader_->OnReceivedResponse(info, false); + FullResponse(1024); + StopWhenLoad(); } // Tests that a 200 response is received. @@ -228,15 +223,36 @@ TEST_F(BufferedResourceLoaderTest, FullResponse) { TEST_F(BufferedResourceLoaderTest, PartialResponse) { Initialize(kHttpUrl, 100, 200); Start(); - PartialResponse(1024); + PartialResponse(100, 200, 1024); StopWhenLoad(); } +// Tests that an invalid partial response is received. +TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { + Initialize(kHttpUrl, 0, 10); + 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)); + + ResourceLoaderBridge::ResponseInfo info; + std::string header = 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. TEST_F(BufferedResourceLoaderTest, BufferAndRead) { Initialize(kHttpUrl, 10, 29); Start(); - PartialResponse(30); + PartialResponse(10, 29, 30); uint8 buffer[10]; InSequence s; @@ -289,7 +305,7 @@ TEST_F(BufferedResourceLoaderTest, BufferAndRead) { TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { Initialize(kHttpUrl, 10, 0x00FFFFFF); Start(); - PartialResponse(0x01000000); + PartialResponse(10, 0x00FFFFFF, 0x01000000); uint8 buffer[10]; InSequence s; @@ -321,7 +337,7 @@ TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { Initialize(kHttpUrl, 10, 29); Start(); - PartialResponse(30); + PartialResponse(10, 29, 30); uint8 buffer[10]; InSequence s; @@ -348,6 +364,7 @@ class MockBufferedResourceLoader : public BufferedResourceLoader { net::CompletionCallback* callback)); MOCK_METHOD0(content_length, int64()); MOCK_METHOD0(instance_size, int64()); + MOCK_METHOD0(partial_response, bool()); private: DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); @@ -420,8 +437,9 @@ class BufferedDataSourceTest : public testing::Test { message_loop_.release(); } - void InitializeDataSource(const char* url, int error, int probe_error, - int64 instance_size, NetworkState networkState) { + void InitializeDataSource(const char* url, int error, + bool partial_response, int64 instance_size, + NetworkState networkState) { // Saves the url first. gurl_ = GURL(url); @@ -435,90 +453,66 @@ class BufferedDataSourceTest : public testing::Test { // There is no need to provide a message loop to data source. data_source_->set_host(&host_); - // Creates the first mock loader to be injected. + // Creates the mock loader to be injected. loader_ = new StrictMock<MockBufferedResourceLoader>(); - probe_loader_ = new StrictMock<MockBufferedResourceLoader>(); + // Expected loaded or not. if (networkState == LOADED) { EXPECT_CALL(host_, SetLoaded(true)); } else if (networkState == LOADING) { EXPECT_CALL(host_, SetLoaded(false)); } - // TODO(ajwong): This mock is too strict. We do not need to guarantee a - // full sequencing each of these expectations. - InSequence s; - StrictMock<media::MockFilterCallback> callback; - - // There is one resource loader with full range will be created. - EXPECT_CALL(*data_source_, CreateLoader(-1, -1)) - .WillOnce(Return(loader_.get())); + { + InSequence s; - // Then another resource loader with a small partial range is created. - EXPECT_CALL(*data_source_, CreateLoader(1, 1)) - .WillOnce(Return(probe_loader_.get())); + // There is one resource loader that request the first 1KB. + EXPECT_CALL(*data_source_, CreateLoader(0, 1024)) + .WillOnce(Return(loader_.get())); - // The initial response loader will be started. - EXPECT_CALL(*loader_, Start(NotNull())) - .WillOnce(DoAll(Assign(&error_, error), - Invoke(this, - &BufferedDataSourceTest::InvokeStartCallback))); + // The initial response loader will be started. + EXPECT_CALL(*loader_, Start(NotNull())) + .WillOnce( + DoAll(Assign(&error_, error), + Invoke(this, + &BufferedDataSourceTest::InvokeStartCallback))); + } + StrictMock<media::MockFilterCallback> callback; if (error == net::OK) { EXPECT_CALL(*loader_, instance_size()) - .WillOnce(Return(instance_size)); - if (instance_size != -1) { + .WillRepeatedly(Return(instance_size)); + EXPECT_CALL(*loader_, partial_response()) + .WillRepeatedly(Return(partial_response)); + + // TODO(hclam): The condition for streaming needs to be adjusted. + if (instance_size != -1 && partial_response) { EXPECT_CALL(host_, SetTotalBytes(instance_size)); EXPECT_CALL(host_, SetBufferedBytes(instance_size)); } else { EXPECT_CALL(host_, SetStreaming(true)); } - // Then the probe resource loader will start. - EXPECT_CALL(*probe_loader_, Start(NotNull())) - .WillOnce(DoAll(Assign(&error_, probe_error), - Invoke(this, - &BufferedDataSourceTest::InvokeStartCallback))); - if (probe_error != net::OK) - EXPECT_CALL(host_, SetStreaming(true)); - EXPECT_CALL(*probe_loader_, Stop()); EXPECT_CALL(callback, OnFilterCallback()); EXPECT_CALL(callback, OnCallbackDestroyed()); } else { EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); EXPECT_CALL(*loader_, Stop()); - EXPECT_CALL(*probe_loader_, Stop()); EXPECT_CALL(callback, OnFilterCallback()); EXPECT_CALL(callback, OnCallbackDestroyed()); - - // This expectation looks a little strange, but this is actually what - // will happen since we are not running a message loop. So simply - // delete the callback. - EXPECT_CALL(*probe_loader_, Start(NotNull())) - .WillOnce(DeleteArg<0>()); } + // Actual initialization of the data source. data_source_->Initialize(url, callback.NewCallback()); message_loop_->RunAllPending(); if (error == net::OK) { // Verify the size of the data source. int64 size; - if (instance_size != -1) { + if (instance_size != -1 && partial_response) { EXPECT_TRUE(data_source_->GetSize(&size)); EXPECT_EQ(instance_size, size); - - if (probe_error == net::OK) { - EXPECT_FALSE(data_source_->IsStreaming()); - } } else { - EXPECT_FALSE(data_source_->GetSize(&size)); - EXPECT_EQ(0, size); - EXPECT_TRUE(data_source_->IsStreaming()); - } - - // Verify the data source is streamed if the probe has received an error. - if (probe_error != net::OK) { EXPECT_TRUE(data_source_->IsStreaming()); } } @@ -528,7 +522,6 @@ class BufferedDataSourceTest : public testing::Test { if (loader_) { InSequence s; EXPECT_CALL(*loader_, Stop()); - EXPECT_CALL(*probe_loader_, Stop()); } data_source_->Stop(); @@ -578,17 +571,19 @@ class BufferedDataSourceTest : public testing::Test { void ReadDataSourceMiss(int64 position, int size) { EXPECT_TRUE(loader_); - InSequence s; // 1. Reply with a cache miss for the read. - EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) - .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), - Invoke(this, - &BufferedDataSourceTest::InvokeReadCallback))); + { + InSequence s; + EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) + .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), + Invoke(this, + &BufferedDataSourceTest::InvokeReadCallback))); + EXPECT_CALL(*loader_, Stop()); + } // 2. Then the current loader will be stop and destroyed. StrictMock<MockBufferedResourceLoader> *new_loader = new StrictMock<MockBufferedResourceLoader>(); - EXPECT_CALL(*loader_, Stop()); EXPECT_CALL(*data_source_, CreateLoader(position, -1)) .WillOnce(Return(new_loader)); @@ -597,6 +592,8 @@ class BufferedDataSourceTest : public testing::Test { .WillOnce(DoAll(Assign(&error_, net::OK), Invoke(this, &BufferedDataSourceTest::InvokeStartCallback))); + EXPECT_CALL(*new_loader, partial_response()) + .WillRepeatedly(Return(loader_->partial_response())); // 4. Then again a read request is made to the new loader. EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) @@ -620,7 +617,6 @@ class BufferedDataSourceTest : public testing::Test { void ReadDataSourceFailed(int64 position, int size, int error) { EXPECT_TRUE(loader_); - InSequence s; // 1. Expect the read is delegated to the resource loader. EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) .WillOnce(DoAll(Assign(&error_, error), @@ -641,23 +637,29 @@ class BufferedDataSourceTest : public testing::Test { } void ReadDataSourceTimesOut(int64 position, int size) { - InSequence s; // 1. Drop the request and let it times out. - EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) - .WillOnce(DeleteArg<3>()); + { + InSequence s; + EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) + .WillOnce(DeleteArg<3>()); + EXPECT_CALL(*loader_, Stop()); + } // 2. Then the current loader will be stop and destroyed. StrictMock<MockBufferedResourceLoader> *new_loader = new StrictMock<MockBufferedResourceLoader>(); - EXPECT_CALL(*loader_, Stop()); EXPECT_CALL(*data_source_, CreateLoader(position, -1)) .WillOnce(Return(new_loader)); - // 3. Then the new loader will be started. + // 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())) .WillOnce(DoAll(Assign(&error_, net::OK), Invoke(this, &BufferedDataSourceTest::InvokeStartCallback))); + EXPECT_CALL(*new_loader, partial_response()) + .WillRepeatedly(Return(loader_->partial_response())); // 4. Then again a read request is made to the new loader. EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) @@ -688,7 +690,6 @@ class BufferedDataSourceTest : public testing::Test { scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > bridge_factory_; scoped_refptr<StrictMock<MockBufferedResourceLoader> > loader_; - scoped_refptr<StrictMock<MockBufferedResourceLoader> > probe_loader_; scoped_refptr<MockBufferedDataSource > data_source_; scoped_refptr<media::FilterFactory> factory_; @@ -705,36 +706,33 @@ class BufferedDataSourceTest : public testing::Test { }; TEST_F(BufferedDataSourceTest, InitializationSuccess) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); StopDataSource(); } TEST_F(BufferedDataSourceTest, InitiailizationFailed) { - InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, - net::ERR_FILE_NOT_FOUND, 0, NONE); + InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, false, 0, NONE); StopDataSource(); } TEST_F(BufferedDataSourceTest, MissingContentLength) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, -1, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, -1, LOADING); StopDataSource(); } TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) { - InitializeDataSource(kHttpUrl, net::OK, - net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, 1024, LOADING); + InitializeDataSource(kHttpUrl, net::OK, false, 1024, LOADING); StopDataSource(); } TEST_F(BufferedDataSourceTest, MissingContentLengthAndRangeRequestNotSupported) { - InitializeDataSource(kHttpUrl, net::OK, - net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, -1, LOADING); + InitializeDataSource(kHttpUrl, net::OK, false, -1, LOADING); StopDataSource(); } TEST_F(BufferedDataSourceTest, ReadCacheHit) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, 25, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, 25, LOADING); // Performs read with cache hit. ReadDataSourceHit(10, 10, 10); @@ -746,27 +744,27 @@ TEST_F(BufferedDataSourceTest, ReadCacheHit) { } TEST_F(BufferedDataSourceTest, ReadCacheMiss) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); ReadDataSourceMiss(1000, 10); ReadDataSourceMiss(20, 10); StopDataSource(); } TEST_F(BufferedDataSourceTest, ReadFailed) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); ReadDataSourceHit(10, 10, 10); ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); StopDataSource(); } TEST_F(BufferedDataSourceTest, ReadTimesOut) { - InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); + InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); ReadDataSourceTimesOut(20, 10); StopDataSource(); } TEST_F(BufferedDataSourceTest, FileHasLoadedState) { - InitializeDataSource(kFileUrl, net::OK, net::OK, 1024, LOADED); + InitializeDataSource(kFileUrl, net::OK, true, 1024, LOADED); ReadDataSourceTimesOut(20, 10); StopDataSource(); } |