diff options
Diffstat (limited to 'webkit/glue/media/buffered_resource_loader_unittest.cc')
-rw-r--r-- | webkit/glue/media/buffered_resource_loader_unittest.cc | 216 |
1 files changed, 114 insertions, 102 deletions
diff --git a/webkit/glue/media/buffered_resource_loader_unittest.cc b/webkit/glue/media/buffered_resource_loader_unittest.cc index aef44ca..03fd360 100644 --- a/webkit/glue/media/buffered_resource_loader_unittest.cc +++ b/webkit/glue/media/buffered_resource_loader_unittest.cc @@ -103,7 +103,15 @@ class BufferedResourceLoaderTest : public testing::Test { } void FullResponse(int64 instance_size) { - EXPECT_CALL(*this, StartCallback(net::OK)); + FullResponse(instance_size, net::OK); + } + + void FullResponse(int64 instance_size, int status) { + EXPECT_CALL(*this, StartCallback(status)); + if (status != net::OK) { + EXPECT_CALL(*url_loader_, cancel()) + .WillOnce(RequestCanceled(loader_)); + } WebURLResponse response(gurl_); response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), @@ -112,15 +120,23 @@ class BufferedResourceLoaderTest : public testing::Test { response.setExpectedContentLength(instance_size); response.setHTTPStatusCode(kHttpOK); loader_->didReceiveResponse(url_loader_, response); - EXPECT_EQ(instance_size, loader_->content_length()); - EXPECT_EQ(instance_size, loader_->instance_size()); - EXPECT_FALSE(loader_->partial_response()); + + if (status == net::OK) { + EXPECT_EQ(instance_size, loader_->content_length()); + EXPECT_EQ(instance_size, loader_->instance_size()); + } + + EXPECT_FALSE(loader_->range_supported()); } void PartialResponse(int64 first_position, int64 last_position, int64 instance_size) { + PartialResponse(first_position, last_position, instance_size, false, true); + } + + void PartialResponse(int64 first_position, int64 last_position, + int64 instance_size, bool chunked, bool accept_ranges) { EXPECT_CALL(*this, StartCallback(net::OK)); - int64 content_length = last_position - first_position + 1; WebURLResponse response(gurl_); response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), @@ -129,12 +145,36 @@ class BufferedResourceLoaderTest : public testing::Test { first_position, last_position, instance_size))); + + // HTTP 1.1 doesn't permit Content-Length with Transfer-Encoding: chunked. + int64 content_length = -1; + if (chunked) { + response.setHTTPHeaderField(WebString::fromUTF8("Transfer-Encoding"), + WebString::fromUTF8("chunked")); + } else { + content_length = last_position - first_position + 1; + } response.setExpectedContentLength(content_length); + + // A server isn't required to return Accept-Ranges even though it might. + if (accept_ranges) { + response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), + WebString::fromUTF8("bytes")); + } + response.setHTTPStatusCode(kHttpPartialContent); loader_->didReceiveResponse(url_loader_, response); + + // XXX: what's the difference between these two? For example in the chunked + // range request case, Content-Length is unspecified (because it's chunked) + // but Content-Range: a-b/c can be returned, where c == Content-Length + // + // Can we eliminate one? EXPECT_EQ(content_length, loader_->content_length()); EXPECT_EQ(instance_size, loader_->instance_size()); - EXPECT_TRUE(loader_->partial_response()); + + // A valid partial response should always result in this being true. + EXPECT_TRUE(loader_->range_supported()); } void Redirect(const char* url) { @@ -160,7 +200,9 @@ class BufferedResourceLoaderTest : public testing::Test { EXPECT_CALL(*this, NetworkCallback()) .RetiresOnSaturation(); loader_->didReceiveData(url_loader_, - reinterpret_cast<char*>(data_ + position), size); + reinterpret_cast<char*>(data_ + position), + size, + size); } // Helper method to read from |loader_|. @@ -174,18 +216,8 @@ class BufferedResourceLoaderTest : public testing::Test { EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); } - // Helper method to disallow deferring in |loader_|. - void DisallowLoaderDefer() { - if (loader_->deferred_) { - EXPECT_CALL(*url_loader_, setDefersLoading(false)); - EXPECT_CALL(*this, NetworkCallback()); - } - loader_->SetAllowDefer(false); - } - - // Helper method to allow deferring in |loader_|. - void AllowLoaderDefer() { - loader_->SetAllowDefer(true); + void ConfirmLoaderDeferredState(bool expectedVal) { + EXPECT_EQ(loader_->deferred_, expectedVal); } MOCK_METHOD1(StartCallback, void(int error)); @@ -232,8 +264,7 @@ TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { Initialize(kHttpUrl, 100, -1); Start(); - FullResponse(1024); - StopWhenLoad(); + FullResponse(1024, net::ERR_INVALID_RESPONSE); } // Tests that a 200 response is received. @@ -252,6 +283,27 @@ TEST_F(BufferedResourceLoaderTest, PartialResponse) { StopWhenLoad(); } +TEST_F(BufferedResourceLoaderTest, PartialResponse_Chunked) { + Initialize(kHttpUrl, 100, 200); + Start(); + PartialResponse(100, 200, 1024, true, true); + StopWhenLoad(); +} + +TEST_F(BufferedResourceLoaderTest, PartialResponse_NoAcceptRanges) { + Initialize(kHttpUrl, 100, 200); + Start(); + PartialResponse(100, 200, 1024, false, false); + StopWhenLoad(); +} + +TEST_F(BufferedResourceLoaderTest, PartialResponse_ChunkedNoAcceptRanges) { + Initialize(kHttpUrl, 100, 200); + Start(); + PartialResponse(100, 200, 1024, true, false); + StopWhenLoad(); +} + // Tests that an invalid partial response is received. TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { Initialize(kHttpUrl, 0, 10); @@ -273,6 +325,7 @@ TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { // Tests the logic of sliding window for data buffering and reading. TEST_F(BufferedResourceLoaderTest, BufferAndRead) { Initialize(kHttpUrl, 10, 29); + loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); Start(); PartialResponse(10, 29, 30); @@ -323,6 +376,7 @@ TEST_F(BufferedResourceLoaderTest, BufferAndRead) { TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { Initialize(kHttpUrl, 10, 0x00FFFFFF); + loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); Start(); PartialResponse(10, 0x00FFFFFF, 0x01000000); @@ -366,128 +420,86 @@ TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { loader_->didFail(url_loader_, error); } -// Tests the logic of caching data to disk when media is paused. -TEST_F(BufferedResourceLoaderTest, AllowDefer_NoDataReceived) { - Initialize(kHttpUrl, 10, 99); - SetLoaderBuffer(10, 20); - Start(); - PartialResponse(10, 99, 100); - - // Start in undeferred state, then disallow defer, then allow defer - // without receiving data in between. - DisallowLoaderDefer(); - AllowLoaderDefer(); - StopWhenLoad(); -} - -TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadSameWindow) { +// Tests the data buffering logic of NeverDefer strategy. +TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) { Initialize(kHttpUrl, 10, 99); SetLoaderBuffer(10, 20); + loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer); Start(); PartialResponse(10, 99, 100); uint8 buffer[10]; - // Start in undeferred state, disallow defer, receive data but don't shift - // buffer window, then allow defer and read. - DisallowLoaderDefer(); - WriteLoader(10, 10); - AllowLoaderDefer(); - - EXPECT_CALL(*this, ReadCallback(10)); - ReadLoader(10, 10, buffer); - VerifyBuffer(buffer, 10, 10); - StopWhenLoad(); -} - -TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadPastWindow) { - Initialize(kHttpUrl, 10, 99); - SetLoaderBuffer(10, 20); - Start(); - PartialResponse(10, 99, 100); - - uint8 buffer[10]; - - // Not deferred, disallow defer, received data and shift buffer window, - // allow defer, then read in area outside of buffer window. - DisallowLoaderDefer(); + // Read past the buffer size; should not defer regardless. WriteLoader(10, 10); WriteLoader(20, 50); - AllowLoaderDefer(); + ConfirmLoaderDeferredState(false); + // Should move past window. EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); ReadLoader(10, 10, buffer); + StopWhenLoad(); } -TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredNoDataReceived) { +// Tests the data buffering logic of ReadThenDefer strategy. +TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) { Initialize(kHttpUrl, 10, 99); SetLoaderBuffer(10, 20); + loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); Start(); PartialResponse(10, 99, 100); uint8 buffer[10]; - // Start in deferred state, then disallow defer, receive no data, and - // allow defer and read. - EXPECT_CALL(*url_loader_, setDefersLoading(true)); + // Make an outstanding read request. + // We should disable deferring after the read request, so expect + // a network event. EXPECT_CALL(*this, NetworkCallback()); - WriteLoader(10, 40); - - DisallowLoaderDefer(); - AllowLoaderDefer(); - - EXPECT_CALL(*this, ReadCallback(10)); - ReadLoader(20, 10, buffer); - VerifyBuffer(buffer, 20, 10); - StopWhenLoad(); -} - -TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadSameWindow) { - Initialize(kHttpUrl, 10, 99); - SetLoaderBuffer(10, 20); - Start(); - PartialResponse(10, 99, 100); + ReadLoader(10, 10, buffer); - uint8 buffer[10]; + // Receive almost enough data to cover, shouldn't defer. + WriteLoader(10, 9); + ConfirmLoaderDeferredState(false); - // 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)); + // As soon as we have received enough data to fulfill the read, defer. EXPECT_CALL(*this, NetworkCallback()); - WriteLoader(10, 30); + EXPECT_CALL(*this, ReadCallback(10)); + WriteLoader(19, 1); - DisallowLoaderDefer(); - WriteLoader(40, 5); - AllowLoaderDefer(); + ConfirmLoaderDeferredState(true); + VerifyBuffer(buffer, 10, 10); - EXPECT_CALL(*this, ReadCallback(10)); - ReadLoader(20, 10, buffer); - VerifyBuffer(buffer, 20, 10); StopWhenLoad(); } -TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) { +// Tests the data buffering logic of ThresholdDefer strategy. +TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) { Initialize(kHttpUrl, 10, 99); SetLoaderBuffer(10, 20); + loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); Start(); PartialResponse(10, 99, 100); uint8 buffer[10]; - // 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)); + WriteLoader(10, 5); + // Haven't reached threshold, don't defer. + ConfirmLoaderDeferredState(false); + + // We're at the threshold now, let's defer. EXPECT_CALL(*this, NetworkCallback()); - WriteLoader(10, 40); + WriteLoader(15, 5); + ConfirmLoaderDeferredState(true); - DisallowLoaderDefer(); - WriteLoader(50, 20); - WriteLoader(70, 40); - AllowLoaderDefer(); + // Now we've read over half of the buffer, disable deferring. + EXPECT_CALL(*this, ReadCallback(6)); + EXPECT_CALL(*this, NetworkCallback()); + ReadLoader(10, 6, buffer); + + ConfirmLoaderDeferredState(false); + VerifyBuffer(buffer, 10, 6); - EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); - ReadLoader(20, 5, buffer); StopWhenLoad(); } |