OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "media/base/media_log.h" | 10 #include "media/base/media_log.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/http/http_request_headers.h" | 12 #include "net/http/http_request_headers.h" |
13 #include "net/http/http_util.h" | 13 #include "net/http/http_util.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
20 #include "webkit/glue/media/buffered_resource_loader.h" | 20 #include "webkit/glue/media/buffered_resource_loader.h" |
21 #include "webkit/mocks/mock_webframeclient.h" | 21 #include "webkit/mocks/mock_webframeclient.h" |
22 #include "webkit/mocks/mock_weburlloader.h" | 22 #include "webkit/mocks/mock_weburlloader.h" |
23 | 23 |
24 using ::testing::_; | |
25 using ::testing::Assign; | |
26 using ::testing::AtLeast; | |
27 using ::testing::DeleteArg; | |
28 using ::testing::DoAll; | |
29 using ::testing::InSequence; | 24 using ::testing::InSequence; |
30 using ::testing::Invoke; | |
31 using ::testing::InvokeWithoutArgs; | |
32 using ::testing::NotNull; | |
33 using ::testing::Return; | 25 using ::testing::Return; |
34 using ::testing::ReturnRef; | |
35 using ::testing::SetArgumentPointee; | |
36 using ::testing::StrictMock; | |
37 using ::testing::Truly; | 26 using ::testing::Truly; |
38 using ::testing::NiceMock; | 27 using ::testing::NiceMock; |
39 using ::testing::WithArgs; | |
40 | 28 |
41 using WebKit::WebString; | 29 using WebKit::WebString; |
42 using WebKit::WebURLError; | 30 using WebKit::WebURLError; |
43 using WebKit::WebURLResponse; | 31 using WebKit::WebURLResponse; |
44 using WebKit::WebView; | 32 using WebKit::WebView; |
45 | 33 |
46 namespace webkit_glue { | 34 namespace webkit_glue { |
47 | 35 |
48 static const char* kHttpUrl = "http://test"; | 36 static const char* kHttpUrl = "http://test"; |
49 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing"; | 37 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing"; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 virtual ~BufferedResourceLoaderTest() { | 80 virtual ~BufferedResourceLoaderTest() { |
93 view_->close(); | 81 view_->close(); |
94 } | 82 } |
95 | 83 |
96 void Initialize(const char* url, int first_position, int last_position) { | 84 void Initialize(const char* url, int first_position, int last_position) { |
97 gurl_ = GURL(url); | 85 gurl_ = GURL(url); |
98 first_position_ = first_position; | 86 first_position_ = first_position; |
99 last_position_ = last_position; | 87 last_position_ = last_position; |
100 | 88 |
101 url_loader_ = new NiceMock<MockWebURLLoader>(); | 89 url_loader_ = new NiceMock<MockWebURLLoader>(); |
102 loader_ = new BufferedResourceLoader(gurl_, | 90 loader_ = new BufferedResourceLoader( |
103 first_position_, last_position_, | 91 gurl_, first_position_, last_position_, |
104 new media::MediaLog()); | 92 BufferedResourceLoader::kThresholdDefer, 0, 0, |
| 93 new media::MediaLog()); |
105 loader_->SetURLLoaderForTest(url_loader_); | 94 loader_->SetURLLoaderForTest(url_loader_); |
106 } | 95 } |
107 | 96 |
108 void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) { | 97 void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) { |
109 loader_->buffer_.reset( | 98 loader_->buffer_.reset( |
110 new media::SeekableBuffer(backward_capacity, forward_capacity)); | 99 new media::SeekableBuffer(backward_capacity, forward_capacity)); |
111 } | 100 } |
112 | 101 |
113 void Start() { | 102 void Start() { |
114 InSequence s; | 103 InSequence s; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 void ReadLoader(int64 position, int size, uint8* buffer) { | 216 void ReadLoader(int64 position, int size, uint8* buffer) { |
228 loader_->Read(position, size, buffer, | 217 loader_->Read(position, size, buffer, |
229 NewCallback(this, &BufferedResourceLoaderTest::ReadCallback)); | 218 NewCallback(this, &BufferedResourceLoaderTest::ReadCallback)); |
230 } | 219 } |
231 | 220 |
232 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. | 221 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. |
233 void VerifyBuffer(uint8* buffer, int pos, int size) { | 222 void VerifyBuffer(uint8* buffer, int pos, int size) { |
234 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); | 223 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); |
235 } | 224 } |
236 | 225 |
| 226 void ConfirmLoaderBufferBackwardCapacity(size_t expected_backward_capacity) { |
| 227 EXPECT_EQ(loader_->buffer_->backward_capacity(), |
| 228 expected_backward_capacity); |
| 229 } |
| 230 |
237 void ConfirmLoaderBufferForwardCapacity(size_t expected_forward_capacity) { | 231 void ConfirmLoaderBufferForwardCapacity(size_t expected_forward_capacity) { |
238 EXPECT_EQ(loader_->buffer_->forward_capacity(), expected_forward_capacity); | 232 EXPECT_EQ(loader_->buffer_->forward_capacity(), expected_forward_capacity); |
239 } | 233 } |
240 | 234 |
241 void ConfirmLoaderDeferredState(bool expectedVal) { | 235 void ConfirmLoaderDeferredState(bool expectedVal) { |
242 EXPECT_EQ(loader_->deferred_, expectedVal); | 236 EXPECT_EQ(loader_->deferred_, expectedVal); |
243 } | 237 } |
244 | 238 |
| 239 // Makes sure the |loader_| buffer window is in a reasonable range. |
| 240 void CheckBufferWindowBounds() { |
| 241 // Corresponds to value defined in buffered_resource_loader.cc. |
| 242 static const size_t kMinBufferCapacity = 2 * 1024 * 1024; |
| 243 EXPECT_GE(loader_->buffer_->forward_capacity(), kMinBufferCapacity); |
| 244 EXPECT_GE(loader_->buffer_->backward_capacity(), kMinBufferCapacity); |
| 245 |
| 246 // Corresponds to value defined in buffered_resource_loader.cc. |
| 247 static const size_t kMaxBufferCapacity = 20 * 1024 * 1024; |
| 248 EXPECT_LE(loader_->buffer_->forward_capacity(), kMaxBufferCapacity); |
| 249 EXPECT_LE(loader_->buffer_->backward_capacity(), kMaxBufferCapacity); |
| 250 } |
| 251 |
245 MOCK_METHOD1(StartCallback, void(int error)); | 252 MOCK_METHOD1(StartCallback, void(int error)); |
246 MOCK_METHOD1(ReadCallback, void(int error)); | 253 MOCK_METHOD1(ReadCallback, void(int error)); |
247 MOCK_METHOD0(NetworkCallback, void()); | 254 MOCK_METHOD0(NetworkCallback, void()); |
248 | 255 |
249 protected: | 256 protected: |
250 GURL gurl_; | 257 GURL gurl_; |
251 int64 first_position_; | 258 int64 first_position_; |
252 int64 last_position_; | 259 int64 last_position_; |
253 | 260 |
254 scoped_refptr<BufferedResourceLoader> loader_; | 261 scoped_refptr<BufferedResourceLoader> loader_; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 ReadLoader(30, 10, buffer); | 405 ReadLoader(30, 10, buffer); |
399 } | 406 } |
400 | 407 |
401 // Tests the logic of expanding the data buffer for large reads. | 408 // Tests the logic of expanding the data buffer for large reads. |
402 TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) { | 409 TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) { |
403 Initialize(kHttpUrl, 10, 0x014FFFFFF); | 410 Initialize(kHttpUrl, 10, 0x014FFFFFF); |
404 SetLoaderBuffer(10, 20); | 411 SetLoaderBuffer(10, 20); |
405 Start(); | 412 Start(); |
406 PartialResponse(10, 0x014FFFFFF, 0x01500000); | 413 PartialResponse(10, 0x014FFFFFF, 0x01500000); |
407 | 414 |
| 415 // Don't test for network callbacks (covered by *Strategy tests). |
| 416 EXPECT_CALL(*this, NetworkCallback()) |
| 417 .WillRepeatedly(Return()); |
| 418 |
408 uint8 buffer[20]; | 419 uint8 buffer[20]; |
409 InSequence s; | 420 InSequence s; |
410 | 421 |
411 // Write more than forward capacity and read it back. Ensure forward capacity | 422 // Write more than forward capacity and read it back. Ensure forward capacity |
412 // gets reset. | 423 // gets reset. |
413 EXPECT_CALL(*this, NetworkCallback()); | |
414 WriteLoader(10, 20); | 424 WriteLoader(10, 20); |
415 EXPECT_CALL(*this, ReadCallback(20)); | 425 EXPECT_CALL(*this, ReadCallback(20)); |
416 ReadLoader(10, 20, buffer); | 426 ReadLoader(10, 20, buffer); |
417 | 427 |
418 VerifyBuffer(buffer, 10, 20); | 428 VerifyBuffer(buffer, 10, 20); |
419 ConfirmLoaderBufferForwardCapacity(10); | 429 ConfirmLoaderBufferForwardCapacity(10); |
420 | 430 |
421 // Make and outstanding read request larger than forward capacity. Ensure | 431 // Make and outstanding read request larger than forward capacity. Ensure |
422 // forward capacity gets extended. | 432 // forward capacity gets extended. |
423 EXPECT_CALL(*this, NetworkCallback()); | |
424 ReadLoader(30, 20, buffer); | 433 ReadLoader(30, 20, buffer); |
425 | 434 |
426 ConfirmLoaderBufferForwardCapacity(20); | 435 ConfirmLoaderBufferForwardCapacity(20); |
427 | 436 |
428 // Fulfill outstanding request. Ensure forward capacity gets reset. | 437 // Fulfill outstanding request. Ensure forward capacity gets reset. |
429 EXPECT_CALL(*this, ReadCallback(20)); | 438 EXPECT_CALL(*this, ReadCallback(20)); |
430 EXPECT_CALL(*this, NetworkCallback()); | |
431 WriteLoader(30, 20); | 439 WriteLoader(30, 20); |
432 | 440 |
433 VerifyBuffer(buffer, 30, 20); | 441 VerifyBuffer(buffer, 30, 20); |
434 ConfirmLoaderBufferForwardCapacity(10); | 442 ConfirmLoaderBufferForwardCapacity(10); |
435 | 443 |
436 // Try to read further ahead than kForwardWaitThreshold allows. Ensure | 444 // Try to read further ahead than kForwardWaitThreshold allows. Ensure |
437 // forward capacity is not changed. | 445 // forward capacity is not changed. |
438 EXPECT_CALL(*this, NetworkCallback()); | |
439 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 446 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
440 ReadLoader(0x00300000, 1, buffer); | 447 ReadLoader(0x00300000, 1, buffer); |
441 | 448 |
442 ConfirmLoaderBufferForwardCapacity(10); | 449 ConfirmLoaderBufferForwardCapacity(10); |
443 | 450 |
444 // Try to read more than maximum forward capacity. Ensure forward capacity is | 451 // Try to read more than maximum forward capacity. Ensure forward capacity is |
445 // not changed. | 452 // not changed. |
446 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); | 453 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); |
447 ReadLoader(30, 0x01400001, buffer); | 454 ReadLoader(30, 0x01400001, buffer); |
448 | 455 |
449 ConfirmLoaderBufferForwardCapacity(10); | 456 ConfirmLoaderBufferForwardCapacity(10); |
450 | 457 |
451 StopWhenLoad(); | 458 StopWhenLoad(); |
452 } | 459 } |
453 | 460 |
454 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { | 461 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { |
455 Initialize(kHttpUrl, 10, 0x00FFFFFF); | 462 Initialize(kHttpUrl, 10, 0x00FFFFFF); |
456 loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); | |
457 Start(); | 463 Start(); |
458 PartialResponse(10, 0x00FFFFFF, 0x01000000); | 464 PartialResponse(10, 0x00FFFFFF, 0x01000000); |
459 | 465 |
460 uint8 buffer[10]; | 466 uint8 buffer[10]; |
461 InSequence s; | 467 InSequence s; |
462 | 468 |
463 // Read very far ahead will get a cache miss. | 469 // Read very far ahead will get a cache miss. |
464 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | 470 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
465 ReadLoader(0x00FFFFFF, 1, buffer); | 471 ReadLoader(0x00FFFFFF, 1, buffer); |
466 | 472 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 | 653 |
648 // Test redirect to the same domain and then to a different domain. | 654 // Test redirect to the same domain and then to a different domain. |
649 Initialize(kHttpUrl, -1, -1); | 655 Initialize(kHttpUrl, -1, -1); |
650 Start(); | 656 Start(); |
651 Redirect(kHttpRedirectToSameDomainUrl1); | 657 Redirect(kHttpRedirectToSameDomainUrl1); |
652 Redirect(kHttpRedirectToDifferentDomainUrl1); | 658 Redirect(kHttpRedirectToDifferentDomainUrl1); |
653 EXPECT_FALSE(loader_->HasSingleOrigin()); | 659 EXPECT_FALSE(loader_->HasSingleOrigin()); |
654 StopWhenLoad(); | 660 StopWhenLoad(); |
655 } | 661 } |
656 | 662 |
657 // TODO(hclam): add unit test for defer loading. | 663 TEST_F(BufferedResourceLoaderTest, BufferWindow_Default) { |
| 664 Initialize(kHttpUrl, -1, -1); |
| 665 Start(); |
| 666 |
| 667 // Test ensures that default construction of a BufferedResourceLoader has sane |
| 668 // values. |
| 669 // |
| 670 // Please do not change these values in order to make a test pass! Instead, |
| 671 // start a conversation on what the default buffer window capacities should |
| 672 // be. |
| 673 ConfirmLoaderBufferBackwardCapacity(2 * 1024 * 1024); |
| 674 ConfirmLoaderBufferForwardCapacity(2 * 1024 * 1024); |
| 675 |
| 676 StopWhenLoad(); |
| 677 } |
| 678 |
| 679 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_Unknown) { |
| 680 Initialize(kHttpUrl, -1, -1); |
| 681 Start(); |
| 682 loader_->SetBitrate(0); |
| 683 CheckBufferWindowBounds(); |
| 684 StopWhenLoad(); |
| 685 } |
| 686 |
| 687 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_BelowLowerBound) { |
| 688 Initialize(kHttpUrl, -1, -1); |
| 689 Start(); |
| 690 loader_->SetBitrate(1024 * 8); // 1 Kbps. |
| 691 CheckBufferWindowBounds(); |
| 692 StopWhenLoad(); |
| 693 } |
| 694 |
| 695 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_WithinBounds) { |
| 696 Initialize(kHttpUrl, -1, -1); |
| 697 Start(); |
| 698 loader_->SetBitrate(2 * 1024 * 1024 * 8); // 2 Mbps. |
| 699 CheckBufferWindowBounds(); |
| 700 StopWhenLoad(); |
| 701 } |
| 702 |
| 703 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_AboveUpperBound) { |
| 704 Initialize(kHttpUrl, -1, -1); |
| 705 Start(); |
| 706 loader_->SetBitrate(100 * 1024 * 1024 * 8); // 100 Mbps. |
| 707 CheckBufferWindowBounds(); |
| 708 StopWhenLoad(); |
| 709 } |
| 710 |
| 711 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Negative) { |
| 712 Initialize(kHttpUrl, -1, -1); |
| 713 Start(); |
| 714 loader_->SetPlaybackRate(-10); |
| 715 CheckBufferWindowBounds(); |
| 716 StopWhenLoad(); |
| 717 } |
| 718 |
| 719 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Zero) { |
| 720 Initialize(kHttpUrl, -1, -1); |
| 721 Start(); |
| 722 loader_->SetPlaybackRate(0); |
| 723 CheckBufferWindowBounds(); |
| 724 StopWhenLoad(); |
| 725 } |
| 726 |
| 727 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_BelowLowerBound) { |
| 728 Initialize(kHttpUrl, -1, -1); |
| 729 Start(); |
| 730 loader_->SetPlaybackRate(0.1f); |
| 731 CheckBufferWindowBounds(); |
| 732 StopWhenLoad(); |
| 733 } |
| 734 |
| 735 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_WithinBounds) { |
| 736 Initialize(kHttpUrl, -1, -1); |
| 737 Start(); |
| 738 loader_->SetPlaybackRate(10); |
| 739 CheckBufferWindowBounds(); |
| 740 StopWhenLoad(); |
| 741 } |
| 742 |
| 743 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) { |
| 744 Initialize(kHttpUrl, -1, -1); |
| 745 Start(); |
| 746 loader_->SetPlaybackRate(100); |
| 747 CheckBufferWindowBounds(); |
| 748 StopWhenLoad(); |
| 749 } |
658 | 750 |
659 } // namespace webkit_glue | 751 } // namespace webkit_glue |
OLD | NEW |