| Index: webkit/glue/media/buffered_data_source_unittest.cc
|
| ===================================================================
|
| --- webkit/glue/media/buffered_data_source_unittest.cc (revision 21893)
|
| +++ webkit/glue/media/buffered_data_source_unittest.cc (working copy)
|
| @@ -16,6 +16,7 @@
|
|
|
| using ::testing::_;
|
| using ::testing::Assign;
|
| +using ::testing::DeleteArg;
|
| using ::testing::DoAll;
|
| using ::testing::InSequence;
|
| using ::testing::Invoke;
|
| @@ -69,32 +70,34 @@
|
| &BufferedResourceLoaderTest::StartCallback));
|
| }
|
|
|
| - void FullResponse(int64 content_length) {
|
| + void FullResponse(int64 instance_size) {
|
| EXPECT_CALL(*this, StartCallback(net::OK));
|
| ResourceLoaderBridge::ResponseInfo info;
|
| std::string header = StringPrintf("HTTP/1.1 200 OK\n"
|
| - "Content-Length: %lld", content_length);
|
| + "Content-Length: %lld", instance_size);
|
| replace(header.begin(), header.end(), '\n', '\0');
|
| info.headers = new net::HttpResponseHeaders(header);
|
| - info.content_length = content_length;
|
| + info.content_length = instance_size;
|
| loader_->OnReceivedResponse(info, false);
|
| - EXPECT_EQ(content_length, loader_->content_length());
|
| + EXPECT_EQ(instance_size, loader_->content_length());
|
| + EXPECT_EQ(instance_size, loader_->instance_size());
|
| }
|
|
|
| - void PartialResponse(int64 content_length) {
|
| + void PartialResponse(int64 instance_size) {
|
| EXPECT_CALL(*this, StartCallback(net::OK));
|
| + 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_,
|
| - content_length);
|
| + instance_size);
|
| replace(header.begin(), header.end(), '\n', '\0');
|
| info.headers = new net::HttpResponseHeaders(header);
|
| info.content_length = content_length;
|
| loader_->OnReceivedResponse(info, false);
|
| - // TODO(hclam): Right now BufferedResourceLoader doesn't care about the
|
| - // partial range replied by the server. Do the check here.
|
| + EXPECT_EQ(content_length, loader_->content_length());
|
| + EXPECT_EQ(instance_size, loader_->instance_size());
|
| }
|
|
|
| void StopWhenLoad() {
|
| @@ -183,7 +186,7 @@
|
| Initialize(kHttpUrl, 100, -1);
|
| Start();
|
|
|
| - EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE));
|
| + EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
| EXPECT_CALL(*bridge_, Cancel());
|
| EXPECT_CALL(*bridge_, OnDestroy())
|
| .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
|
| @@ -238,7 +241,7 @@
|
| ReadLoader(10, 10, buffer);
|
| VerifyBuffer(buffer, 10, 10);
|
|
|
| - // Read backwith outside buffer.
|
| + // Read backward outside buffer.
|
| EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
|
| ReadLoader(9, 10, buffer);
|
|
|
| @@ -257,7 +260,9 @@
|
| // Try to read outside buffered range after request has completed.
|
| EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
|
| ReadLoader(5, 10, buffer);
|
| - EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
|
| +
|
| + // Try to read beyond the instance size.
|
| + EXPECT_CALL(*this, ReadCallback(0));
|
| ReadLoader(30, 10, buffer);
|
| }
|
|
|
| @@ -326,6 +331,7 @@
|
| MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer,
|
| net::CompletionCallback* callback));
|
| MOCK_METHOD0(content_length, int64());
|
| + MOCK_METHOD0(instance_size, int64());
|
| MOCK_METHOD0(OnDestroy, void());
|
|
|
| private:
|
| @@ -394,7 +400,8 @@
|
| message_loop_.release();
|
| }
|
|
|
| - void InitializeDataSource(const char* url, int error, int64 content_length) {
|
| + void InitializeDataSource(const char* url, int error, int probe_error,
|
| + int64 instance_size) {
|
| // Saves the url first.
|
| gurl_ = GURL(url);
|
|
|
| @@ -410,34 +417,81 @@
|
|
|
| // Creates the first mock loader to be injected.
|
| loader_.reset(new StrictMock<MockBufferedResourceLoader>());
|
| + probe_loader_.reset(new StrictMock<MockBufferedResourceLoader>());
|
|
|
| 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) {
|
| + if (error == net::OK) {
|
| + EXPECT_CALL(*loader_, instance_size())
|
| + .WillOnce(Return(instance_size));
|
| + if (instance_size != -1) {
|
| + 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());
|
| - } else {
|
| - EXPECT_CALL(*loader_, content_length())
|
| - .WillOnce(Return(content_length));
|
| - EXPECT_CALL(host_, SetTotalBytes(content_length));
|
| - EXPECT_CALL(host_, SetBufferedBytes(content_length));
|
| + 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>());
|
| }
|
| - EXPECT_CALL(callback, OnFilterCallback());
|
| - EXPECT_CALL(callback, OnCallbackDestroyed());
|
|
|
| data_source_->Initialize(url, callback.NewCallback());
|
| message_loop_->RunAllPending();
|
|
|
| if (error == net::OK) {
|
| + // Verify the size of the data source.
|
| int64 size;
|
| - EXPECT_TRUE(data_source_->GetSize(&size));
|
| - EXPECT_EQ(content_length, size);
|
| + if (instance_size != -1) {
|
| + 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());
|
| + }
|
| }
|
| }
|
|
|
| @@ -445,10 +499,14 @@
|
| if (loader_.get()) {
|
| InSequence s;
|
| EXPECT_CALL(*loader_, Stop());
|
| - EXPECT_CALL(*loader_, OnDestroy())
|
| - .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader));
|
| + EXPECT_CALL(*probe_loader_, Stop());
|
| }
|
|
|
| + EXPECT_CALL(*loader_, OnDestroy())
|
| + .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader));
|
| + EXPECT_CALL(*probe_loader_, OnDestroy())
|
| + .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseProbeLoader));
|
| +
|
| data_source_->Stop();
|
| message_loop_->RunAllPending();
|
| }
|
| @@ -461,6 +519,10 @@
|
| loader_.release();
|
| }
|
|
|
| + void ReleaseProbeLoader() {
|
| + probe_loader_.release();
|
| + }
|
| +
|
| void InvokeStartCallback(net::CompletionCallback* callback) {
|
| callback->RunWithParams(Tuple1<int>(error_));
|
| delete callback;
|
| @@ -552,15 +614,12 @@
|
| Invoke(this,
|
| &BufferedDataSourceTest::InvokeReadCallback)));
|
|
|
| - // 2. The read has failed, so read callback will be called.
|
| + // 2. Host will then receive an error.
|
| + EXPECT_CALL(*loader_, Stop());
|
| +
|
| + // 3. The read has failed, so read callback will be called.
|
| EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
|
|
|
| - // 3. Host will then receive an error.
|
| - EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK));
|
| -
|
| - // 4. The the loader is destroyed.
|
| - EXPECT_CALL(*loader_, Stop());
|
| -
|
| data_source_->Read(
|
| position, size, buffer_,
|
| NewCallback(this, &BufferedDataSourceTest::ReadCallback));
|
| @@ -573,6 +632,7 @@
|
| scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> >
|
| bridge_factory_;
|
| scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_;
|
| + scoped_ptr<StrictMock<MockBufferedResourceLoader> > probe_loader_;
|
| scoped_refptr<MockBufferedDataSource > data_source_;
|
| scoped_refptr<media::FilterFactory> factory_;
|
|
|
| @@ -589,17 +649,36 @@
|
| };
|
|
|
| TEST_F(BufferedDataSourceTest, InitializationSuccess) {
|
| - InitializeDataSource(kHttpUrl, net::OK, 1024);
|
| + InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024);
|
| StopDataSource();
|
| }
|
|
|
| TEST_F(BufferedDataSourceTest, InitiailizationFailed) {
|
| - InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, 0);
|
| + InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND,
|
| + net::ERR_FILE_NOT_FOUND, 0);
|
| StopDataSource();
|
| }
|
|
|
| +TEST_F(BufferedDataSourceTest, MissingContentLength) {
|
| + InitializeDataSource(kHttpUrl, net::OK, net::OK, -1);
|
| + StopDataSource();
|
| +}
|
| +
|
| +TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) {
|
| + InitializeDataSource(kHttpUrl, net::OK,
|
| + net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, 1024);
|
| + StopDataSource();
|
| +}
|
| +
|
| +TEST_F(BufferedDataSourceTest,
|
| + MissingContentLengthAndRangeRequestNotSupported) {
|
| + InitializeDataSource(kHttpUrl, net::OK,
|
| + net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, -1);
|
| + StopDataSource();
|
| +}
|
| +
|
| TEST_F(BufferedDataSourceTest, ReadCacheHit) {
|
| - InitializeDataSource(kHttpUrl, net::OK, 25);
|
| + InitializeDataSource(kHttpUrl, net::OK, net::OK, 25);
|
|
|
| // Performs read with cache hit.
|
| ReadDataSourceHit(10, 10, 10);
|
| @@ -611,14 +690,14 @@
|
| }
|
|
|
| TEST_F(BufferedDataSourceTest, ReadCacheMiss) {
|
| - InitializeDataSource(kHttpUrl, net::OK, 1024);
|
| + InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024);
|
| ReadDataSourceMiss(1000, 10);
|
| ReadDataSourceMiss(20, 10);
|
| StopDataSource();
|
| }
|
|
|
| TEST_F(BufferedDataSourceTest, ReadFailed) {
|
| - InitializeDataSource(kHttpUrl, net::OK, 1024);
|
| + InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024);
|
| ReadDataSourceHit(10, 10, 10);
|
| ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET);
|
| StopDataSource();
|
|
|