diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-30 17:49:41 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-30 17:49:41 +0000 |
commit | 3979b7df049bbcf8197cfe0849c1f526795b7c56 (patch) | |
tree | 80735491fbc9220a449133ce4064bfa645f128c5 /webkit/glue/media/buffered_data_source_unittest.cc | |
parent | 09642233be151b5dc640043e594b043850cbac69 (diff) | |
download | chromium_src-3979b7df049bbcf8197cfe0849c1f526795b7c56.zip chromium_src-3979b7df049bbcf8197cfe0849c1f526795b7c56.tar.gz chromium_src-3979b7df049bbcf8197cfe0849c1f526795b7c56.tar.bz2 |
Enable <video> using cache
Retrying the following two patches:
http://codereview.chromium.org/253001/show
http://codereview.chromium.org/248012/show
TBR=scherkus
Review URL: http://codereview.chromium.org/242073
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27632 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 | 196 |
1 files changed, 94 insertions, 102 deletions
diff --git a/webkit/glue/media/buffered_data_source_unittest.cc b/webkit/glue/media/buffered_data_source_unittest.cc index 003313c..c05c9b9 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,60 @@ 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>(); - if (networkState == LOADED) { - EXPECT_CALL(host_, SetLoaded(true)); - } else if (networkState == LOADING) { - EXPECT_CALL(host_, SetLoaded(false)); + bool loaded = networkState == LOADED; + { + InSequence s; + EXPECT_CALL(*data_source_, CreateLoader(_, _)) + .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))); } - // 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())); - - // Then another resource loader with a small partial range is created. - EXPECT_CALL(*data_source_, CreateLoader(1, 1)) - .WillOnce(Return(probe_loader_.get())); - - // The initial response loader will be started. - EXPECT_CALL(*loader_, Start(NotNull())) - .WillOnce(DoAll(Assign(&error_, error), - Invoke(this, - &BufferedDataSourceTest::InvokeStartCallback))); - if (error == net::OK) { + // Expected loaded or not. + EXPECT_CALL(host_, SetLoaded(loaded)); 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 && (loaded || 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 && (loaded || 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 +516,6 @@ class BufferedDataSourceTest : public testing::Test { if (loader_) { InSequence s; EXPECT_CALL(*loader_, Stop()); - EXPECT_CALL(*probe_loader_, Stop()); } data_source_->Stop(); @@ -578,17 +565,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 +586,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 +611,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 +631,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 +684,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 +700,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 +738,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(); } |