Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "media/base/filters.h" | 8 #include "media/base/filters.h" |
| 9 #include "media/base/mock_filter_host.h" | 9 #include "media/base/mock_filter_host.h" |
| 10 #include "media/base/mock_filters.h" | 10 #include "media/base/mock_filters.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 EXPECT_CALL(*this, StartCallback(net::OK)); | 90 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 91 ResourceLoaderBridge::ResponseInfo info; | 91 ResourceLoaderBridge::ResponseInfo info; |
| 92 std::string header = StringPrintf("HTTP/1.1 200 OK\n" | 92 std::string header = StringPrintf("HTTP/1.1 200 OK\n" |
| 93 "Content-Length: %lld", instance_size); | 93 "Content-Length: %lld", instance_size); |
| 94 replace(header.begin(), header.end(), '\n', '\0'); | 94 replace(header.begin(), header.end(), '\n', '\0'); |
| 95 info.headers = new net::HttpResponseHeaders(header); | 95 info.headers = new net::HttpResponseHeaders(header); |
| 96 info.content_length = instance_size; | 96 info.content_length = instance_size; |
| 97 loader_->OnReceivedResponse(info, false); | 97 loader_->OnReceivedResponse(info, false); |
| 98 EXPECT_EQ(instance_size, loader_->content_length()); | 98 EXPECT_EQ(instance_size, loader_->content_length()); |
| 99 EXPECT_EQ(instance_size, loader_->instance_size()); | 99 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 100 EXPECT_FALSE(loader_->partial_response()); | |
| 100 } | 101 } |
| 101 | 102 |
| 102 void PartialResponse(int64 instance_size) { | 103 void PartialResponse(int64 first_position, int64 last_position, |
| 104 int64 instance_size) { | |
| 103 EXPECT_CALL(*this, StartCallback(net::OK)); | 105 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 104 int64 content_length = last_position_ - first_position_ + 1; | 106 int64 content_length = last_position - first_position + 1; |
| 105 ResourceLoaderBridge::ResponseInfo info; | 107 ResourceLoaderBridge::ResponseInfo info; |
| 106 std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" | 108 std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" |
| 107 "Content-Range: bytes %lld-%lld/%lld", | 109 "Content-Range: bytes %lld-%lld/%lld", |
| 108 first_position_, | 110 first_position, |
| 109 last_position_, | 111 last_position, |
| 110 instance_size); | 112 instance_size); |
| 111 replace(header.begin(), header.end(), '\n', '\0'); | 113 replace(header.begin(), header.end(), '\n', '\0'); |
| 112 info.headers = new net::HttpResponseHeaders(header); | 114 info.headers = new net::HttpResponseHeaders(header); |
| 113 info.content_length = content_length; | 115 info.content_length = content_length; |
| 114 loader_->OnReceivedResponse(info, false); | 116 loader_->OnReceivedResponse(info, false); |
| 115 EXPECT_EQ(content_length, loader_->content_length()); | 117 EXPECT_EQ(content_length, loader_->content_length()); |
| 116 EXPECT_EQ(instance_size, loader_->instance_size()); | 118 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 119 EXPECT_TRUE(loader_->partial_response()); | |
| 117 } | 120 } |
| 118 | 121 |
| 119 void StopWhenLoad() { | 122 void StopWhenLoad() { |
| 120 InSequence s; | 123 InSequence s; |
| 121 EXPECT_CALL(*bridge_, Cancel()) | 124 EXPECT_CALL(*bridge_, Cancel()) |
| 122 .WillOnce(RequestCanceled(loader_)); | 125 .WillOnce(RequestCanceled(loader_)); |
| 123 EXPECT_CALL(*bridge_, OnDestroy()) | 126 EXPECT_CALL(*bridge_, OnDestroy()) |
| 124 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 127 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 125 loader_->Stop(); | 128 loader_->Stop(); |
| 126 } | 129 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 Initialize(kHttpUrl, -1, -1); | 192 Initialize(kHttpUrl, -1, -1); |
| 190 Start(); | 193 Start(); |
| 191 | 194 |
| 192 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); | 195 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); |
| 193 EXPECT_CALL(*bridge_, Cancel()) | 196 EXPECT_CALL(*bridge_, Cancel()) |
| 194 .WillOnce(RequestCanceled(loader_)); | 197 .WillOnce(RequestCanceled(loader_)); |
| 195 EXPECT_CALL(*bridge_, OnDestroy()) | 198 EXPECT_CALL(*bridge_, OnDestroy()) |
| 196 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 199 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 197 | 200 |
| 198 ResourceLoaderBridge::ResponseInfo info; | 201 ResourceLoaderBridge::ResponseInfo info; |
| 199 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n"); | 202 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Not Found\n"); |
| 200 loader_->OnReceivedResponse(info, false); | 203 loader_->OnReceivedResponse(info, false); |
| 201 } | 204 } |
| 202 | 205 |
| 203 // Tests that partial content is requested but not fulfilled. | 206 // Tests that partial content is requested but not fulfilled. |
| 204 TEST_F(BufferedResourceLoaderTest, NotPartialRange) { | 207 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { |
| 205 Initialize(kHttpUrl, 100, -1); | 208 Initialize(kHttpUrl, 100, -1); |
| 206 Start(); | 209 Start(); |
| 207 | 210 FullResponse(1024); |
| 208 EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 211 StopWhenLoad(); |
| 209 EXPECT_CALL(*bridge_, Cancel()) | |
| 210 .WillOnce(RequestCanceled(loader_)); | |
| 211 EXPECT_CALL(*bridge_, OnDestroy()) | |
| 212 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | |
| 213 | |
| 214 ResourceLoaderBridge::ResponseInfo info; | |
| 215 info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n"); | |
| 216 loader_->OnReceivedResponse(info, false); | |
| 217 } | 212 } |
| 218 | 213 |
| 219 // Tests that a 200 response is received. | 214 // Tests that a 200 response is received. |
| 220 TEST_F(BufferedResourceLoaderTest, FullResponse) { | 215 TEST_F(BufferedResourceLoaderTest, FullResponse) { |
| 221 Initialize(kHttpUrl, -1, -1); | 216 Initialize(kHttpUrl, -1, -1); |
| 222 Start(); | 217 Start(); |
| 223 FullResponse(1024); | 218 FullResponse(1024); |
| 224 StopWhenLoad(); | 219 StopWhenLoad(); |
| 225 } | 220 } |
| 226 | 221 |
| 227 // Tests that a partial content response is received. | 222 // Tests that a partial content response is received. |
| 228 TEST_F(BufferedResourceLoaderTest, PartialResponse) { | 223 TEST_F(BufferedResourceLoaderTest, PartialResponse) { |
| 229 Initialize(kHttpUrl, 100, 200); | 224 Initialize(kHttpUrl, 100, 200); |
| 230 Start(); | 225 Start(); |
| 231 PartialResponse(1024); | 226 PartialResponse(100, 200, 1024); |
| 232 StopWhenLoad(); | 227 StopWhenLoad(); |
| 233 } | 228 } |
| 234 | 229 |
| 230 // Tests that an invalid partial response is received. | |
| 231 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { | |
| 232 Initialize(kHttpUrl, 0, 10); | |
| 233 Start(); | |
| 234 | |
| 235 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); | |
| 236 EXPECT_CALL(*bridge_, Cancel()) | |
| 237 .WillOnce(RequestCanceled(loader_)); | |
| 238 EXPECT_CALL(*bridge_, OnDestroy()) | |
| 239 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | |
| 240 | |
| 241 ResourceLoaderBridge::ResponseInfo info; | |
| 242 std::string header = StringPrintf("HTTP/1.1 206 Partial Content\n" | |
| 243 "Content-Range: bytes %d-%d/%d", | |
| 244 1, 10, 1024); | |
| 245 replace(header.begin(), header.end(), '\n', '\0'); | |
| 246 info.headers = new net::HttpResponseHeaders(header); | |
| 247 info.content_length = 10; | |
| 248 loader_->OnReceivedResponse(info, false); | |
| 249 } | |
| 250 | |
| 235 // Tests the logic of sliding window for data buffering and reading. | 251 // Tests the logic of sliding window for data buffering and reading. |
| 236 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { | 252 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { |
| 237 Initialize(kHttpUrl, 10, 29); | 253 Initialize(kHttpUrl, 10, 29); |
| 238 Start(); | 254 Start(); |
| 239 PartialResponse(30); | 255 PartialResponse(10, 29, 30); |
| 240 | 256 |
| 241 uint8 buffer[10]; | 257 uint8 buffer[10]; |
| 242 InSequence s; | 258 InSequence s; |
| 243 | 259 |
| 244 // Writes 10 bytes and read them back. | 260 // Writes 10 bytes and read them back. |
| 245 WriteLoader(10, 10); | 261 WriteLoader(10, 10); |
| 246 EXPECT_CALL(*this, ReadCallback(10)); | 262 EXPECT_CALL(*this, ReadCallback(10)); |
| 247 ReadLoader(10, 10, buffer); | 263 ReadLoader(10, 10, buffer); |
| 248 VerifyBuffer(buffer, 10, 10); | 264 VerifyBuffer(buffer, 10, 10); |
| 249 | 265 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 ReadLoader(5, 10, buffer); | 298 ReadLoader(5, 10, buffer); |
| 283 | 299 |
| 284 // Try to read beyond the instance size. | 300 // Try to read beyond the instance size. |
| 285 EXPECT_CALL(*this, ReadCallback(0)); | 301 EXPECT_CALL(*this, ReadCallback(0)); |
| 286 ReadLoader(30, 10, buffer); | 302 ReadLoader(30, 10, buffer); |
| 287 } | 303 } |
| 288 | 304 |
| 289 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { | 305 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { |
| 290 Initialize(kHttpUrl, 10, 0x00FFFFFF); | 306 Initialize(kHttpUrl, 10, 0x00FFFFFF); |
| 291 Start(); | 307 Start(); |
| 292 PartialResponse(0x01000000); | 308 PartialResponse(10, 0x00FFFFFF, 0x01000000); |
| 293 | 309 |
| 294 uint8 buffer[10]; | 310 uint8 buffer[10]; |
| 295 InSequence s; | 311 InSequence s; |
| 296 | 312 |
| 297 // Read very far aheard will get a cache miss. | 313 // Read very far aheard will get a cache miss. |
| 298 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 314 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 299 ReadLoader(0x00FFFFFF, 1, buffer); | 315 ReadLoader(0x00FFFFFF, 1, buffer); |
| 300 | 316 |
| 301 // The following call will not call ReadCallback() because it is waiting for | 317 // The following call will not call ReadCallback() because it is waiting for |
| 302 // data to arrive. | 318 // data to arrive. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 314 EXPECT_CALL(*bridge_, OnDestroy()) | 330 EXPECT_CALL(*bridge_, OnDestroy()) |
| 315 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 331 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 316 URLRequestStatus status; | 332 URLRequestStatus status; |
| 317 status.set_status(URLRequestStatus::SUCCESS); | 333 status.set_status(URLRequestStatus::SUCCESS); |
| 318 loader_->OnCompletedRequest(status, ""); | 334 loader_->OnCompletedRequest(status, ""); |
| 319 } | 335 } |
| 320 | 336 |
| 321 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { | 337 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { |
| 322 Initialize(kHttpUrl, 10, 29); | 338 Initialize(kHttpUrl, 10, 29); |
| 323 Start(); | 339 Start(); |
| 324 PartialResponse(30); | 340 PartialResponse(10, 29, 30); |
| 325 | 341 |
| 326 uint8 buffer[10]; | 342 uint8 buffer[10]; |
| 327 InSequence s; | 343 InSequence s; |
| 328 | 344 |
| 329 ReadLoader(10, 10, buffer); | 345 ReadLoader(10, 10, buffer); |
| 330 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); | 346 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); |
| 331 EXPECT_CALL(*bridge_, OnDestroy()) | 347 EXPECT_CALL(*bridge_, OnDestroy()) |
| 332 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); | 348 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); |
| 333 URLRequestStatus status; | 349 URLRequestStatus status; |
| 334 status.set_status(URLRequestStatus::FAILED); | 350 status.set_status(URLRequestStatus::FAILED); |
| 335 loader_->OnCompletedRequest(status, ""); | 351 loader_->OnCompletedRequest(status, ""); |
| 336 } | 352 } |
| 337 | 353 |
| 338 // TODO(hclam): add unit test for defer loading. | 354 // TODO(hclam): add unit test for defer loading. |
| 339 | 355 |
| 340 class MockBufferedResourceLoader : public BufferedResourceLoader { | 356 class MockBufferedResourceLoader : public BufferedResourceLoader { |
| 341 public: | 357 public: |
| 342 MockBufferedResourceLoader() : BufferedResourceLoader() { | 358 MockBufferedResourceLoader() : BufferedResourceLoader() { |
| 343 } | 359 } |
| 344 | 360 |
| 345 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback)); | 361 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback)); |
| 346 MOCK_METHOD0(Stop, void()); | 362 MOCK_METHOD0(Stop, void()); |
| 347 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, | 363 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, |
| 348 net::CompletionCallback* callback)); | 364 net::CompletionCallback* callback)); |
| 349 MOCK_METHOD0(content_length, int64()); | 365 MOCK_METHOD0(content_length, int64()); |
| 350 MOCK_METHOD0(instance_size, int64()); | 366 MOCK_METHOD0(instance_size, int64()); |
| 367 MOCK_METHOD0(partial_response, bool()); | |
| 351 | 368 |
| 352 private: | 369 private: |
| 353 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); | 370 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); |
| 354 }; | 371 }; |
| 355 | 372 |
| 356 // A mock BufferedDataSource to inject mock BufferedResourceLoader through | 373 // A mock BufferedDataSource to inject mock BufferedResourceLoader through |
| 357 // CreateLoader() method. | 374 // CreateLoader() method. |
| 358 class MockBufferedDataSource : public BufferedDataSource { | 375 class MockBufferedDataSource : public BufferedDataSource { |
| 359 public: | 376 public: |
| 360 // Static methods for creating this class. | 377 // Static methods for creating this class. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 413 // Expects bridge factory to be destroyed along with data source. | 430 // Expects bridge factory to be destroyed along with data source. |
| 414 EXPECT_CALL(*bridge_factory_, OnDestroy()) | 431 EXPECT_CALL(*bridge_factory_, OnDestroy()) |
| 415 .WillOnce(Invoke(this, | 432 .WillOnce(Invoke(this, |
| 416 &BufferedDataSourceTest::ReleaseBridgeFactory)); | 433 &BufferedDataSourceTest::ReleaseBridgeFactory)); |
| 417 } | 434 } |
| 418 | 435 |
| 419 // We don't own the message loop so release it. | 436 // We don't own the message loop so release it. |
| 420 message_loop_.release(); | 437 message_loop_.release(); |
| 421 } | 438 } |
| 422 | 439 |
| 423 void InitializeDataSource(const char* url, int error, int probe_error, | 440 void InitializeDataSource(const char* url, int error, |
| 424 int64 instance_size, NetworkState networkState) { | 441 bool partial_response, int64 instance_size, |
| 442 NetworkState networkState) { | |
| 425 // Saves the url first. | 443 // Saves the url first. |
| 426 gurl_ = GURL(url); | 444 gurl_ = GURL(url); |
| 427 | 445 |
| 428 media::MediaFormat url_format; | 446 media::MediaFormat url_format; |
| 429 url_format.SetAsString(media::MediaFormat::kMimeType, | 447 url_format.SetAsString(media::MediaFormat::kMimeType, |
| 430 media::mime_type::kURL); | 448 media::mime_type::kURL); |
| 431 url_format.SetAsString(media::MediaFormat::kURL, url); | 449 url_format.SetAsString(media::MediaFormat::kURL, url); |
| 432 data_source_ = factory_->Create<MockBufferedDataSource>(url_format); | 450 data_source_ = factory_->Create<MockBufferedDataSource>(url_format); |
| 433 CHECK(data_source_); | 451 CHECK(data_source_); |
| 434 | 452 |
| 435 // There is no need to provide a message loop to data source. | 453 // There is no need to provide a message loop to data source. |
| 436 data_source_->set_host(&host_); | 454 data_source_->set_host(&host_); |
| 437 | 455 |
| 438 // Creates the first mock loader to be injected. | 456 // Creates the mock loader to be injected. |
| 439 loader_ = new StrictMock<MockBufferedResourceLoader>(); | 457 loader_ = new StrictMock<MockBufferedResourceLoader>(); |
| 440 probe_loader_ = new StrictMock<MockBufferedResourceLoader>(); | |
| 441 | 458 |
| 459 // Expected loaded or not. | |
| 442 if (networkState == LOADED) { | 460 if (networkState == LOADED) { |
| 443 EXPECT_CALL(host_, SetLoaded(true)); | 461 EXPECT_CALL(host_, SetLoaded(true)); |
| 444 } else if (networkState == LOADING) { | 462 } else if (networkState == LOADING) { |
| 445 EXPECT_CALL(host_, SetLoaded(false)); | 463 EXPECT_CALL(host_, SetLoaded(false)); |
| 446 } | 464 } |
| 447 | 465 |
| 448 // TODO(ajwong): This mock is too strict. We do not need to guarantee a | 466 { |
| 449 // full sequencing each of these expectations. | 467 InSequence s; |
| 450 InSequence s; | 468 |
| 469 // There is one resource loader that request the first 1KB. | |
| 470 EXPECT_CALL(*data_source_, CreateLoader(0, 1024)) | |
|
scherkus (not reviewing)
2009/09/29 23:10:34
you can also use the constant here etc...
| |
| 471 .WillOnce(Return(loader_.get())); | |
| 472 | |
| 473 // The initial response loader will be started. | |
| 474 EXPECT_CALL(*loader_, Start(NotNull())) | |
| 475 .WillOnce( | |
| 476 DoAll(Assign(&error_, error), | |
| 477 Invoke(this, | |
| 478 &BufferedDataSourceTest::InvokeStartCallback))); | |
| 479 } | |
| 480 | |
| 451 StrictMock<media::MockFilterCallback> callback; | 481 StrictMock<media::MockFilterCallback> callback; |
| 452 | |
| 453 // There is one resource loader with full range will be created. | |
| 454 EXPECT_CALL(*data_source_, CreateLoader(-1, -1)) | |
| 455 .WillOnce(Return(loader_.get())); | |
| 456 | |
| 457 // Then another resource loader with a small partial range is created. | |
| 458 EXPECT_CALL(*data_source_, CreateLoader(1, 1)) | |
| 459 .WillOnce(Return(probe_loader_.get())); | |
| 460 | |
| 461 // The initial response loader will be started. | |
| 462 EXPECT_CALL(*loader_, Start(NotNull())) | |
| 463 .WillOnce(DoAll(Assign(&error_, error), | |
| 464 Invoke(this, | |
| 465 &BufferedDataSourceTest::InvokeStartCallback))); | |
| 466 | |
| 467 if (error == net::OK) { | 482 if (error == net::OK) { |
| 468 EXPECT_CALL(*loader_, instance_size()) | 483 EXPECT_CALL(*loader_, instance_size()) |
| 469 .WillOnce(Return(instance_size)); | 484 .WillRepeatedly(Return(instance_size)); |
| 470 if (instance_size != -1) { | 485 EXPECT_CALL(*loader_, partial_response()) |
| 486 .WillRepeatedly(Return(partial_response)); | |
| 487 | |
| 488 // TODO(hclam): The condition for streaming needs to be adjusted. | |
| 489 if (instance_size != -1 && partial_response) { | |
| 471 EXPECT_CALL(host_, SetTotalBytes(instance_size)); | 490 EXPECT_CALL(host_, SetTotalBytes(instance_size)); |
| 472 EXPECT_CALL(host_, SetBufferedBytes(instance_size)); | 491 EXPECT_CALL(host_, SetBufferedBytes(instance_size)); |
| 473 } else { | 492 } else { |
| 474 EXPECT_CALL(host_, SetStreaming(true)); | 493 EXPECT_CALL(host_, SetStreaming(true)); |
| 475 } | 494 } |
| 476 | 495 |
| 477 // Then the probe resource loader will start. | |
| 478 EXPECT_CALL(*probe_loader_, Start(NotNull())) | |
| 479 .WillOnce(DoAll(Assign(&error_, probe_error), | |
| 480 Invoke(this, | |
| 481 &BufferedDataSourceTest::InvokeStartCallback))); | |
| 482 if (probe_error != net::OK) | |
| 483 EXPECT_CALL(host_, SetStreaming(true)); | |
| 484 EXPECT_CALL(*probe_loader_, Stop()); | |
| 485 EXPECT_CALL(callback, OnFilterCallback()); | 496 EXPECT_CALL(callback, OnFilterCallback()); |
| 486 EXPECT_CALL(callback, OnCallbackDestroyed()); | 497 EXPECT_CALL(callback, OnCallbackDestroyed()); |
| 487 } else { | 498 } else { |
| 488 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); | 499 EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK)); |
| 489 EXPECT_CALL(*loader_, Stop()); | 500 EXPECT_CALL(*loader_, Stop()); |
| 490 EXPECT_CALL(*probe_loader_, Stop()); | |
| 491 EXPECT_CALL(callback, OnFilterCallback()); | 501 EXPECT_CALL(callback, OnFilterCallback()); |
| 492 EXPECT_CALL(callback, OnCallbackDestroyed()); | 502 EXPECT_CALL(callback, OnCallbackDestroyed()); |
| 493 | |
| 494 // This expectation looks a little strange, but this is actually what | |
| 495 // will happen since we are not running a message loop. So simply | |
| 496 // delete the callback. | |
| 497 EXPECT_CALL(*probe_loader_, Start(NotNull())) | |
| 498 .WillOnce(DeleteArg<0>()); | |
| 499 } | 503 } |
| 500 | 504 |
| 505 // Actual initialization of the data source. | |
| 501 data_source_->Initialize(url, callback.NewCallback()); | 506 data_source_->Initialize(url, callback.NewCallback()); |
| 502 message_loop_->RunAllPending(); | 507 message_loop_->RunAllPending(); |
| 503 | 508 |
| 504 if (error == net::OK) { | 509 if (error == net::OK) { |
| 505 // Verify the size of the data source. | 510 // Verify the size of the data source. |
| 506 int64 size; | 511 int64 size; |
| 507 if (instance_size != -1) { | 512 if (instance_size != -1 && partial_response) { |
| 508 EXPECT_TRUE(data_source_->GetSize(&size)); | 513 EXPECT_TRUE(data_source_->GetSize(&size)); |
| 509 EXPECT_EQ(instance_size, size); | 514 EXPECT_EQ(instance_size, size); |
| 510 | |
| 511 if (probe_error == net::OK) { | |
| 512 EXPECT_FALSE(data_source_->IsStreaming()); | |
| 513 } | |
| 514 } else { | 515 } else { |
| 515 EXPECT_FALSE(data_source_->GetSize(&size)); | |
| 516 EXPECT_EQ(0, size); | |
| 517 EXPECT_TRUE(data_source_->IsStreaming()); | |
| 518 } | |
| 519 | |
| 520 // Verify the data source is streamed if the probe has received an error. | |
| 521 if (probe_error != net::OK) { | |
| 522 EXPECT_TRUE(data_source_->IsStreaming()); | 516 EXPECT_TRUE(data_source_->IsStreaming()); |
| 523 } | 517 } |
| 524 } | 518 } |
| 525 } | 519 } |
| 526 | 520 |
| 527 void StopDataSource() { | 521 void StopDataSource() { |
| 528 if (loader_) { | 522 if (loader_) { |
| 529 InSequence s; | 523 InSequence s; |
| 530 EXPECT_CALL(*loader_, Stop()); | 524 EXPECT_CALL(*loader_, Stop()); |
| 531 EXPECT_CALL(*probe_loader_, Stop()); | |
| 532 } | 525 } |
| 533 | 526 |
| 534 data_source_->Stop(); | 527 data_source_->Stop(); |
| 535 message_loop_->RunAllPending(); | 528 message_loop_->RunAllPending(); |
| 536 } | 529 } |
| 537 | 530 |
| 538 void ReleaseBridgeFactory() { | 531 void ReleaseBridgeFactory() { |
| 539 bridge_factory_.release(); | 532 bridge_factory_.release(); |
| 540 } | 533 } |
| 541 | 534 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 571 message_loop_->RunAllPending(); | 564 message_loop_->RunAllPending(); |
| 572 | 565 |
| 573 // Make sure data is correct. | 566 // Make sure data is correct. |
| 574 EXPECT_EQ(0, | 567 EXPECT_EQ(0, |
| 575 memcmp(buffer_, data_ + static_cast<int>(position), read_size)); | 568 memcmp(buffer_, data_ + static_cast<int>(position), read_size)); |
| 576 } | 569 } |
| 577 | 570 |
| 578 void ReadDataSourceMiss(int64 position, int size) { | 571 void ReadDataSourceMiss(int64 position, int size) { |
| 579 EXPECT_TRUE(loader_); | 572 EXPECT_TRUE(loader_); |
| 580 | 573 |
| 581 InSequence s; | |
| 582 // 1. Reply with a cache miss for the read. | 574 // 1. Reply with a cache miss for the read. |
| 583 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) | 575 { |
| 584 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), | 576 InSequence s; |
| 585 Invoke(this, | 577 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) |
| 586 &BufferedDataSourceTest::InvokeReadCallback))); | 578 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), |
| 579 Invoke(this, | |
| 580 &BufferedDataSourceTest::InvokeReadCallback))); | |
| 581 EXPECT_CALL(*loader_, Stop()); | |
| 582 } | |
| 587 | 583 |
| 588 // 2. Then the current loader will be stop and destroyed. | 584 // 2. Then the current loader will be stop and destroyed. |
| 589 StrictMock<MockBufferedResourceLoader> *new_loader = | 585 StrictMock<MockBufferedResourceLoader> *new_loader = |
| 590 new StrictMock<MockBufferedResourceLoader>(); | 586 new StrictMock<MockBufferedResourceLoader>(); |
| 591 EXPECT_CALL(*loader_, Stop()); | |
| 592 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) | 587 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) |
| 593 .WillOnce(Return(new_loader)); | 588 .WillOnce(Return(new_loader)); |
| 594 | 589 |
| 595 // 3. Then the new loader will be started. | 590 // 3. Then the new loader will be started. |
| 596 EXPECT_CALL(*new_loader, Start(NotNull())) | 591 EXPECT_CALL(*new_loader, Start(NotNull())) |
| 597 .WillOnce(DoAll(Assign(&error_, net::OK), | 592 .WillOnce(DoAll(Assign(&error_, net::OK), |
| 598 Invoke(this, | 593 Invoke(this, |
| 599 &BufferedDataSourceTest::InvokeStartCallback))); | 594 &BufferedDataSourceTest::InvokeStartCallback))); |
| 595 EXPECT_CALL(*new_loader, partial_response()) | |
| 596 .WillRepeatedly(Return(loader_->partial_response())); | |
| 600 | 597 |
| 601 // 4. Then again a read request is made to the new loader. | 598 // 4. Then again a read request is made to the new loader. |
| 602 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) | 599 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) |
| 603 .WillOnce(DoAll(Assign(&error_, size), | 600 .WillOnce(DoAll(Assign(&error_, size), |
| 604 Invoke(this, | 601 Invoke(this, |
| 605 &BufferedDataSourceTest::InvokeReadCallback))); | 602 &BufferedDataSourceTest::InvokeReadCallback))); |
| 606 | 603 |
| 607 EXPECT_CALL(*this, ReadCallback(size)); | 604 EXPECT_CALL(*this, ReadCallback(size)); |
| 608 | 605 |
| 609 data_source_->Read( | 606 data_source_->Read( |
| 610 position, size, buffer_, | 607 position, size, buffer_, |
| 611 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); | 608 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); |
| 612 message_loop_->RunAllPending(); | 609 message_loop_->RunAllPending(); |
| 613 | 610 |
| 614 // Make sure data is correct. | 611 // Make sure data is correct. |
| 615 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); | 612 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); |
| 616 | 613 |
| 617 loader_ = new_loader; | 614 loader_ = new_loader; |
| 618 } | 615 } |
| 619 | 616 |
| 620 void ReadDataSourceFailed(int64 position, int size, int error) { | 617 void ReadDataSourceFailed(int64 position, int size, int error) { |
| 621 EXPECT_TRUE(loader_); | 618 EXPECT_TRUE(loader_); |
| 622 | 619 |
| 623 InSequence s; | |
| 624 // 1. Expect the read is delegated to the resource loader. | 620 // 1. Expect the read is delegated to the resource loader. |
| 625 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) | 621 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) |
| 626 .WillOnce(DoAll(Assign(&error_, error), | 622 .WillOnce(DoAll(Assign(&error_, error), |
| 627 Invoke(this, | 623 Invoke(this, |
| 628 &BufferedDataSourceTest::InvokeReadCallback))); | 624 &BufferedDataSourceTest::InvokeReadCallback))); |
| 629 | 625 |
| 630 // 2. Host will then receive an error. | 626 // 2. Host will then receive an error. |
| 631 EXPECT_CALL(*loader_, Stop()); | 627 EXPECT_CALL(*loader_, Stop()); |
| 632 | 628 |
| 633 // 3. The read has failed, so read callback will be called. | 629 // 3. The read has failed, so read callback will be called. |
| 634 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 630 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 635 | 631 |
| 636 data_source_->Read( | 632 data_source_->Read( |
| 637 position, size, buffer_, | 633 position, size, buffer_, |
| 638 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); | 634 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); |
| 639 | 635 |
| 640 message_loop_->RunAllPending(); | 636 message_loop_->RunAllPending(); |
| 641 } | 637 } |
| 642 | 638 |
| 643 void ReadDataSourceTimesOut(int64 position, int size) { | 639 void ReadDataSourceTimesOut(int64 position, int size) { |
| 644 InSequence s; | |
| 645 // 1. Drop the request and let it times out. | 640 // 1. Drop the request and let it times out. |
| 646 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) | 641 { |
| 647 .WillOnce(DeleteArg<3>()); | 642 InSequence s; |
| 643 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) | |
| 644 .WillOnce(DeleteArg<3>()); | |
| 645 EXPECT_CALL(*loader_, Stop()); | |
| 646 } | |
| 648 | 647 |
| 649 // 2. Then the current loader will be stop and destroyed. | 648 // 2. Then the current loader will be stop and destroyed. |
| 650 StrictMock<MockBufferedResourceLoader> *new_loader = | 649 StrictMock<MockBufferedResourceLoader> *new_loader = |
| 651 new StrictMock<MockBufferedResourceLoader>(); | 650 new StrictMock<MockBufferedResourceLoader>(); |
| 652 EXPECT_CALL(*loader_, Stop()); | |
| 653 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) | 651 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) |
| 654 .WillOnce(Return(new_loader)); | 652 .WillOnce(Return(new_loader)); |
| 655 | 653 |
| 656 // 3. Then the new loader will be started. | 654 // 3. Then the new loader will be started and respond to queries about |
| 655 // whether this is a partial response using the value of the previous | |
| 656 // loader. | |
| 657 EXPECT_CALL(*new_loader, Start(NotNull())) | 657 EXPECT_CALL(*new_loader, Start(NotNull())) |
| 658 .WillOnce(DoAll(Assign(&error_, net::OK), | 658 .WillOnce(DoAll(Assign(&error_, net::OK), |
| 659 Invoke(this, | 659 Invoke(this, |
| 660 &BufferedDataSourceTest::InvokeStartCallback))); | 660 &BufferedDataSourceTest::InvokeStartCallback))); |
| 661 EXPECT_CALL(*new_loader, partial_response()) | |
| 662 .WillRepeatedly(Return(loader_->partial_response())); | |
| 661 | 663 |
| 662 // 4. Then again a read request is made to the new loader. | 664 // 4. Then again a read request is made to the new loader. |
| 663 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) | 665 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) |
| 664 .WillOnce(DoAll(Assign(&error_, size), | 666 .WillOnce(DoAll(Assign(&error_, size), |
| 665 Invoke(this, | 667 Invoke(this, |
| 666 &BufferedDataSourceTest::InvokeReadCallback), | 668 &BufferedDataSourceTest::InvokeReadCallback), |
| 667 InvokeWithoutArgs(message_loop_.get(), | 669 InvokeWithoutArgs(message_loop_.get(), |
| 668 &MessageLoop::Quit))); | 670 &MessageLoop::Quit))); |
| 669 | 671 |
| 670 EXPECT_CALL(*this, ReadCallback(size)); | 672 EXPECT_CALL(*this, ReadCallback(size)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 681 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); | 683 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); |
| 682 | 684 |
| 683 loader_ = new_loader; | 685 loader_ = new_loader; |
| 684 } | 686 } |
| 685 | 687 |
| 686 MOCK_METHOD1(ReadCallback, void(size_t size)); | 688 MOCK_METHOD1(ReadCallback, void(size_t size)); |
| 687 | 689 |
| 688 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > | 690 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > |
| 689 bridge_factory_; | 691 bridge_factory_; |
| 690 scoped_refptr<StrictMock<MockBufferedResourceLoader> > loader_; | 692 scoped_refptr<StrictMock<MockBufferedResourceLoader> > loader_; |
| 691 scoped_refptr<StrictMock<MockBufferedResourceLoader> > probe_loader_; | |
| 692 scoped_refptr<MockBufferedDataSource > data_source_; | 693 scoped_refptr<MockBufferedDataSource > data_source_; |
| 693 scoped_refptr<media::FilterFactory> factory_; | 694 scoped_refptr<media::FilterFactory> factory_; |
| 694 | 695 |
| 695 StrictMock<media::MockFilterHost> host_; | 696 StrictMock<media::MockFilterHost> host_; |
| 696 GURL gurl_; | 697 GURL gurl_; |
| 697 scoped_ptr<MessageLoop> message_loop_; | 698 scoped_ptr<MessageLoop> message_loop_; |
| 698 | 699 |
| 699 int error_; | 700 int error_; |
| 700 uint8 buffer_[1024]; | 701 uint8 buffer_[1024]; |
| 701 uint8 data_[1024]; | 702 uint8 data_[1024]; |
| 702 | 703 |
| 703 private: | 704 private: |
| 704 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); | 705 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); |
| 705 }; | 706 }; |
| 706 | 707 |
| 707 TEST_F(BufferedDataSourceTest, InitializationSuccess) { | 708 TEST_F(BufferedDataSourceTest, InitializationSuccess) { |
| 708 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); | 709 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); |
| 709 StopDataSource(); | 710 StopDataSource(); |
| 710 } | 711 } |
| 711 | 712 |
| 712 TEST_F(BufferedDataSourceTest, InitiailizationFailed) { | 713 TEST_F(BufferedDataSourceTest, InitiailizationFailed) { |
| 713 InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, | 714 InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, false, 0, NONE); |
| 714 net::ERR_FILE_NOT_FOUND, 0, NONE); | |
| 715 StopDataSource(); | 715 StopDataSource(); |
| 716 } | 716 } |
| 717 | 717 |
| 718 TEST_F(BufferedDataSourceTest, MissingContentLength) { | 718 TEST_F(BufferedDataSourceTest, MissingContentLength) { |
| 719 InitializeDataSource(kHttpUrl, net::OK, net::OK, -1, LOADING); | 719 InitializeDataSource(kHttpUrl, net::OK, true, -1, LOADING); |
| 720 StopDataSource(); | 720 StopDataSource(); |
| 721 } | 721 } |
| 722 | 722 |
| 723 TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) { | 723 TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) { |
| 724 InitializeDataSource(kHttpUrl, net::OK, | 724 InitializeDataSource(kHttpUrl, net::OK, false, 1024, LOADING); |
| 725 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, 1024, LOADING); | |
| 726 StopDataSource(); | 725 StopDataSource(); |
| 727 } | 726 } |
| 728 | 727 |
| 729 TEST_F(BufferedDataSourceTest, | 728 TEST_F(BufferedDataSourceTest, |
| 730 MissingContentLengthAndRangeRequestNotSupported) { | 729 MissingContentLengthAndRangeRequestNotSupported) { |
| 731 InitializeDataSource(kHttpUrl, net::OK, | 730 InitializeDataSource(kHttpUrl, net::OK, false, -1, LOADING); |
| 732 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, -1, LOADING); | |
| 733 StopDataSource(); | 731 StopDataSource(); |
| 734 } | 732 } |
| 735 | 733 |
| 736 TEST_F(BufferedDataSourceTest, ReadCacheHit) { | 734 TEST_F(BufferedDataSourceTest, ReadCacheHit) { |
| 737 InitializeDataSource(kHttpUrl, net::OK, net::OK, 25, LOADING); | 735 InitializeDataSource(kHttpUrl, net::OK, true, 25, LOADING); |
| 738 | 736 |
| 739 // Performs read with cache hit. | 737 // Performs read with cache hit. |
| 740 ReadDataSourceHit(10, 10, 10); | 738 ReadDataSourceHit(10, 10, 10); |
| 741 | 739 |
| 742 // Performs read with cache hit but partially filled. | 740 // Performs read with cache hit but partially filled. |
| 743 ReadDataSourceHit(20, 10, 5); | 741 ReadDataSourceHit(20, 10, 5); |
| 744 | 742 |
| 745 StopDataSource(); | 743 StopDataSource(); |
| 746 } | 744 } |
| 747 | 745 |
| 748 TEST_F(BufferedDataSourceTest, ReadCacheMiss) { | 746 TEST_F(BufferedDataSourceTest, ReadCacheMiss) { |
| 749 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); | 747 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); |
| 750 ReadDataSourceMiss(1000, 10); | 748 ReadDataSourceMiss(1000, 10); |
| 751 ReadDataSourceMiss(20, 10); | 749 ReadDataSourceMiss(20, 10); |
| 752 StopDataSource(); | 750 StopDataSource(); |
| 753 } | 751 } |
| 754 | 752 |
| 755 TEST_F(BufferedDataSourceTest, ReadFailed) { | 753 TEST_F(BufferedDataSourceTest, ReadFailed) { |
| 756 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); | 754 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); |
| 757 ReadDataSourceHit(10, 10, 10); | 755 ReadDataSourceHit(10, 10, 10); |
| 758 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); | 756 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); |
| 759 StopDataSource(); | 757 StopDataSource(); |
| 760 } | 758 } |
| 761 | 759 |
| 762 TEST_F(BufferedDataSourceTest, ReadTimesOut) { | 760 TEST_F(BufferedDataSourceTest, ReadTimesOut) { |
| 763 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024, LOADING); | 761 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); |
| 764 ReadDataSourceTimesOut(20, 10); | 762 ReadDataSourceTimesOut(20, 10); |
| 765 StopDataSource(); | 763 StopDataSource(); |
| 766 } | 764 } |
| 767 | 765 |
| 768 TEST_F(BufferedDataSourceTest, FileHasLoadedState) { | 766 TEST_F(BufferedDataSourceTest, FileHasLoadedState) { |
| 769 InitializeDataSource(kFileUrl, net::OK, net::OK, 1024, LOADED); | 767 InitializeDataSource(kFileUrl, net::OK, true, 1024, LOADED); |
| 770 ReadDataSourceTimesOut(20, 10); | 768 ReadDataSourceTimesOut(20, 10); |
| 771 StopDataSource(); | 769 StopDataSource(); |
| 772 } | 770 } |
| 773 | 771 |
| 774 } // namespace webkit_glue | 772 } // namespace webkit_glue |
| OLD | NEW |