OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bind.h" | 8 #include "base/bind.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 loader_->Start( | 110 loader_->Start( |
111 base::Bind(&BufferedResourceLoaderTest::StartCallback, | 111 base::Bind(&BufferedResourceLoaderTest::StartCallback, |
112 base::Unretained(this)), | 112 base::Unretained(this)), |
113 base::Bind(&BufferedResourceLoaderTest::LoadingCallback, | 113 base::Bind(&BufferedResourceLoaderTest::LoadingCallback, |
114 base::Unretained(this)), | 114 base::Unretained(this)), |
115 base::Bind(&BufferedResourceLoaderTest::ProgressCallback, | 115 base::Bind(&BufferedResourceLoaderTest::ProgressCallback, |
116 base::Unretained(this)), | 116 base::Unretained(this)), |
117 view_->mainFrame()); | 117 view_->mainFrame()); |
118 } | 118 } |
119 | 119 |
120 void FullResponse(int64 instance_size) { | 120 void FullResponse(int64_t instance_size) { |
121 FullResponse(instance_size, BufferedResourceLoader::kOk); | 121 FullResponse(instance_size, BufferedResourceLoader::kOk); |
122 } | 122 } |
123 | 123 |
124 void FullResponse(int64 instance_size, | 124 void FullResponse(int64_t instance_size, |
125 BufferedResourceLoader::Status status) { | 125 BufferedResourceLoader::Status status) { |
126 EXPECT_CALL(*this, StartCallback(status)); | 126 EXPECT_CALL(*this, StartCallback(status)); |
127 | 127 |
128 WebURLResponse response(gurl_); | 128 WebURLResponse response(gurl_); |
129 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), | 129 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), |
130 WebString::fromUTF8(base::StringPrintf("%" | 130 WebString::fromUTF8(base::StringPrintf("%" |
131 PRId64, instance_size))); | 131 PRId64, instance_size))); |
132 response.setExpectedContentLength(instance_size); | 132 response.setExpectedContentLength(instance_size); |
133 response.setHTTPStatusCode(kHttpOK); | 133 response.setHTTPStatusCode(kHttpOK); |
134 loader_->didReceiveResponse(url_loader_, response); | 134 loader_->didReceiveResponse(url_loader_, response); |
135 | 135 |
136 if (status == BufferedResourceLoader::kOk) { | 136 if (status == BufferedResourceLoader::kOk) { |
137 EXPECT_EQ(instance_size, loader_->content_length()); | 137 EXPECT_EQ(instance_size, loader_->content_length()); |
138 EXPECT_EQ(instance_size, loader_->instance_size()); | 138 EXPECT_EQ(instance_size, loader_->instance_size()); |
139 } | 139 } |
140 | 140 |
141 EXPECT_FALSE(loader_->range_supported()); | 141 EXPECT_FALSE(loader_->range_supported()); |
142 } | 142 } |
143 | 143 |
144 void PartialResponse(int64 first_position, int64 last_position, | 144 void PartialResponse(int64_t first_position, |
145 int64 instance_size) { | 145 int64_t last_position, |
| 146 int64_t instance_size) { |
146 PartialResponse(first_position, last_position, instance_size, false, true); | 147 PartialResponse(first_position, last_position, instance_size, false, true); |
147 } | 148 } |
148 | 149 |
149 void PartialResponse(int64 first_position, int64 last_position, | 150 void PartialResponse(int64_t first_position, |
150 int64 instance_size, bool chunked, bool accept_ranges) { | 151 int64_t last_position, |
| 152 int64_t instance_size, |
| 153 bool chunked, |
| 154 bool accept_ranges) { |
151 EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kOk)); | 155 EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kOk)); |
152 | 156 |
153 WebURLResponse response(gurl_); | 157 WebURLResponse response(gurl_); |
154 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), | 158 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), |
155 WebString::fromUTF8(base::StringPrintf("bytes " | 159 WebString::fromUTF8(base::StringPrintf("bytes " |
156 "%" PRId64 "-%" PRId64 "/%" PRId64, | 160 "%" PRId64 "-%" PRId64 "/%" PRId64, |
157 first_position, | 161 first_position, |
158 last_position, | 162 last_position, |
159 instance_size))); | 163 instance_size))); |
160 | 164 |
161 // HTTP 1.1 doesn't permit Content-Length with Transfer-Encoding: chunked. | 165 // HTTP 1.1 doesn't permit Content-Length with Transfer-Encoding: chunked. |
162 int64 content_length = -1; | 166 int64_t content_length = -1; |
163 if (chunked) { | 167 if (chunked) { |
164 response.setHTTPHeaderField(WebString::fromUTF8("Transfer-Encoding"), | 168 response.setHTTPHeaderField(WebString::fromUTF8("Transfer-Encoding"), |
165 WebString::fromUTF8("chunked")); | 169 WebString::fromUTF8("chunked")); |
166 } else { | 170 } else { |
167 content_length = last_position - first_position + 1; | 171 content_length = last_position - first_position + 1; |
168 } | 172 } |
169 response.setExpectedContentLength(content_length); | 173 response.setExpectedContentLength(content_length); |
170 | 174 |
171 // A server isn't required to return Accept-Ranges even though it might. | 175 // A server isn't required to return Accept-Ranges even though it might. |
172 if (accept_ranges) { | 176 if (accept_ranges) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 int buffered = loader_->buffer_.forward_bytes(); | 230 int buffered = loader_->buffer_.forward_bytes(); |
227 int capacity = loader_->buffer_.forward_capacity(); | 231 int capacity = loader_->buffer_.forward_capacity(); |
228 CHECK_LT(buffered, capacity); | 232 CHECK_LT(buffered, capacity); |
229 | 233 |
230 EXPECT_CALL(*this, LoadingCallback( | 234 EXPECT_CALL(*this, LoadingCallback( |
231 BufferedResourceLoader::kLoadingDeferred)); | 235 BufferedResourceLoader::kLoadingDeferred)); |
232 WriteData(capacity - buffered); | 236 WriteData(capacity - buffered); |
233 } | 237 } |
234 | 238 |
235 // Helper method to read from |loader_|. | 239 // Helper method to read from |loader_|. |
236 void ReadLoader(int64 position, int size, uint8* buffer) { | 240 void ReadLoader(int64_t position, int size, uint8_t* buffer) { |
237 loader_->Read(position, size, buffer, | 241 loader_->Read(position, size, buffer, |
238 base::Bind(&BufferedResourceLoaderTest::ReadCallback, | 242 base::Bind(&BufferedResourceLoaderTest::ReadCallback, |
239 base::Unretained(this))); | 243 base::Unretained(this))); |
240 } | 244 } |
241 | 245 |
242 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. | 246 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. |
243 void VerifyBuffer(uint8* buffer, int pos, int size) { | 247 void VerifyBuffer(uint8_t* buffer, int pos, int size) { |
244 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); | 248 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); |
245 } | 249 } |
246 | 250 |
247 void ConfirmLoaderOffsets(int64 expected_offset, | 251 void ConfirmLoaderOffsets(int64_t expected_offset, |
248 int expected_first_offset, | 252 int expected_first_offset, |
249 int expected_last_offset) { | 253 int expected_last_offset) { |
250 EXPECT_EQ(loader_->offset_, expected_offset); | 254 EXPECT_EQ(loader_->offset_, expected_offset); |
251 EXPECT_EQ(loader_->first_offset_, expected_first_offset); | 255 EXPECT_EQ(loader_->first_offset_, expected_first_offset); |
252 EXPECT_EQ(loader_->last_offset_, expected_last_offset); | 256 EXPECT_EQ(loader_->last_offset_, expected_last_offset); |
253 } | 257 } |
254 | 258 |
255 void ConfirmBufferState(int backward_bytes, | 259 void ConfirmBufferState(int backward_bytes, |
256 int backward_capacity, | 260 int backward_capacity, |
257 int forward_bytes, | 261 int forward_bytes, |
(...skipping 25 matching lines...) Expand all Loading... |
283 static const int kMaxBufferCapacity = 20 * 1024 * 1024; | 287 static const int kMaxBufferCapacity = 20 * 1024 * 1024; |
284 EXPECT_LE(loader_->buffer_.forward_capacity(), kMaxBufferCapacity); | 288 EXPECT_LE(loader_->buffer_.forward_capacity(), kMaxBufferCapacity); |
285 EXPECT_LE(loader_->buffer_.backward_capacity(), kMaxBufferCapacity); | 289 EXPECT_LE(loader_->buffer_.backward_capacity(), kMaxBufferCapacity); |
286 } | 290 } |
287 | 291 |
288 bool HasActiveLoader() { return loader_->active_loader_; } | 292 bool HasActiveLoader() { return loader_->active_loader_; } |
289 | 293 |
290 MOCK_METHOD1(StartCallback, void(BufferedResourceLoader::Status)); | 294 MOCK_METHOD1(StartCallback, void(BufferedResourceLoader::Status)); |
291 MOCK_METHOD2(ReadCallback, void(BufferedResourceLoader::Status, int)); | 295 MOCK_METHOD2(ReadCallback, void(BufferedResourceLoader::Status, int)); |
292 MOCK_METHOD1(LoadingCallback, void(BufferedResourceLoader::LoadingState)); | 296 MOCK_METHOD1(LoadingCallback, void(BufferedResourceLoader::LoadingState)); |
293 MOCK_METHOD1(ProgressCallback, void(int64)); | 297 MOCK_METHOD1(ProgressCallback, void(int64_t)); |
294 | 298 |
295 protected: | 299 protected: |
296 GURL gurl_; | 300 GURL gurl_; |
297 int64 first_position_; | 301 int64_t first_position_; |
298 int64 last_position_; | 302 int64_t last_position_; |
299 | 303 |
300 scoped_ptr<BufferedResourceLoader> loader_; | 304 scoped_ptr<BufferedResourceLoader> loader_; |
301 NiceMock<MockWebURLLoader>* url_loader_; | 305 NiceMock<MockWebURLLoader>* url_loader_; |
302 | 306 |
303 MockWebFrameClient client_; | 307 MockWebFrameClient client_; |
304 WebView* view_; | 308 WebView* view_; |
305 WebLocalFrame* frame_; | 309 WebLocalFrame* frame_; |
306 | 310 |
307 base::MessageLoop message_loop_; | 311 base::MessageLoop message_loop_; |
308 | 312 |
309 uint8 data_[kDataSize]; | 313 uint8_t data_[kDataSize]; |
310 | 314 |
311 private: | 315 private: |
312 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest); | 316 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest); |
313 }; | 317 }; |
314 | 318 |
315 TEST_F(BufferedResourceLoaderTest, StartStop) { | 319 TEST_F(BufferedResourceLoaderTest, StartStop) { |
316 Initialize(kHttpUrl, -1, -1); | 320 Initialize(kHttpUrl, -1, -1); |
317 Start(); | 321 Start(); |
318 StopWhenLoad(); | 322 StopWhenLoad(); |
319 } | 323 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 StopWhenLoad(); | 398 StopWhenLoad(); |
395 } | 399 } |
396 | 400 |
397 // Tests the logic of sliding window for data buffering and reading. | 401 // Tests the logic of sliding window for data buffering and reading. |
398 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { | 402 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { |
399 Initialize(kHttpUrl, 10, 29); | 403 Initialize(kHttpUrl, 10, 29); |
400 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); | 404 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); |
401 Start(); | 405 Start(); |
402 PartialResponse(10, 29, 30); | 406 PartialResponse(10, 29, 30); |
403 | 407 |
404 uint8 buffer[10]; | 408 uint8_t buffer[10]; |
405 InSequence s; | 409 InSequence s; |
406 | 410 |
407 // Writes 10 bytes and read them back. | 411 // Writes 10 bytes and read them back. |
408 WriteLoader(10, 10); | 412 WriteLoader(10, 10); |
409 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 413 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
410 ReadLoader(10, 10, buffer); | 414 ReadLoader(10, 10, buffer); |
411 VerifyBuffer(buffer, 10, 10); | 415 VerifyBuffer(buffer, 10, 10); |
412 | 416 |
413 // Writes 10 bytes and read 2 times. | 417 // Writes 10 bytes and read 2 times. |
414 WriteLoader(20, 10); | 418 WriteLoader(20, 10); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 ReadLoader(30, 10, buffer); | 450 ReadLoader(30, 10, buffer); |
447 } | 451 } |
448 | 452 |
449 // Tests the logic of expanding the data buffer for large reads. | 453 // Tests the logic of expanding the data buffer for large reads. |
450 TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) { | 454 TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) { |
451 Initialize(kHttpUrl, 10, 0x014FFFFFF); | 455 Initialize(kHttpUrl, 10, 0x014FFFFFF); |
452 SetLoaderBuffer(10, 20); | 456 SetLoaderBuffer(10, 20); |
453 Start(); | 457 Start(); |
454 PartialResponse(10, 0x014FFFFFF, 0x015000000); | 458 PartialResponse(10, 0x014FFFFFF, 0x015000000); |
455 | 459 |
456 uint8 buffer[20]; | 460 uint8_t buffer[20]; |
457 InSequence s; | 461 InSequence s; |
458 | 462 |
459 // Write more than forward capacity and read it back. Ensure forward capacity | 463 // Write more than forward capacity and read it back. Ensure forward capacity |
460 // gets reset after reading. | 464 // gets reset after reading. |
461 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 465 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
462 WriteLoader(10, 20); | 466 WriteLoader(10, 20); |
463 | 467 |
464 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20)); | 468 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20)); |
465 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | 469 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
466 ReadLoader(10, 20, buffer); | 470 ReadLoader(10, 20, buffer); |
(...skipping 28 matching lines...) Expand all Loading... |
495 ConfirmLoaderBufferForwardCapacity(10); | 499 ConfirmLoaderBufferForwardCapacity(10); |
496 | 500 |
497 StopWhenLoad(); | 501 StopWhenLoad(); |
498 } | 502 } |
499 | 503 |
500 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { | 504 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { |
501 Initialize(kHttpUrl, 10, 0x00FFFFFF); | 505 Initialize(kHttpUrl, 10, 0x00FFFFFF); |
502 Start(); | 506 Start(); |
503 PartialResponse(10, 0x00FFFFFF, 0x01000000); | 507 PartialResponse(10, 0x00FFFFFF, 0x01000000); |
504 | 508 |
505 uint8 buffer[10]; | 509 uint8_t buffer[10]; |
506 InSequence s; | 510 InSequence s; |
507 | 511 |
508 // Read very far ahead will get a cache miss. | 512 // Read very far ahead will get a cache miss. |
509 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0)); | 513 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0)); |
510 ReadLoader(0x00FFFFFF, 1, buffer); | 514 ReadLoader(0x00FFFFFF, 1, buffer); |
511 | 515 |
512 // The following call will not call ReadCallback() because it is waiting for | 516 // The following call will not call ReadCallback() because it is waiting for |
513 // data to arrive. | 517 // data to arrive. |
514 ReadLoader(10, 10, buffer); | 518 ReadLoader(10, 10, buffer); |
515 | 519 |
516 // Writing to loader will fulfill the read request. | 520 // Writing to loader will fulfill the read request. |
517 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 521 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
518 WriteLoader(10, 20); | 522 WriteLoader(10, 20); |
519 VerifyBuffer(buffer, 10, 10); | 523 VerifyBuffer(buffer, 10, 10); |
520 | 524 |
521 // The following call cannot be fulfilled now. | 525 // The following call cannot be fulfilled now. |
522 ReadLoader(25, 10, buffer); | 526 ReadLoader(25, 10, buffer); |
523 | 527 |
524 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished)); | 528 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished)); |
525 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5)); | 529 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5)); |
526 loader_->didFinishLoading(url_loader_, 0, -1); | 530 loader_->didFinishLoading(url_loader_, 0, -1); |
527 } | 531 } |
528 | 532 |
529 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { | 533 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { |
530 Initialize(kHttpUrl, 10, 29); | 534 Initialize(kHttpUrl, 10, 29); |
531 Start(); | 535 Start(); |
532 PartialResponse(10, 29, 30); | 536 PartialResponse(10, 29, 30); |
533 | 537 |
534 uint8 buffer[10]; | 538 uint8_t buffer[10]; |
535 InSequence s; | 539 InSequence s; |
536 | 540 |
537 // We should convert any error we receive to BufferedResourceLoader::kFailed. | 541 // We should convert any error we receive to BufferedResourceLoader::kFailed. |
538 ReadLoader(10, 10, buffer); | 542 ReadLoader(10, 10, buffer); |
539 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); | 543 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); |
540 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); | 544 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); |
541 WebURLError error; | 545 WebURLError error; |
542 error.reason = net::ERR_TIMED_OUT; | 546 error.reason = net::ERR_TIMED_OUT; |
543 error.isCancellation = false; | 547 error.isCancellation = false; |
544 loader_->didFail(url_loader_, error); | 548 loader_->didFail(url_loader_, error); |
545 } | 549 } |
546 | 550 |
547 TEST_F(BufferedResourceLoaderTest, RequestFailedWithNoPendingReads) { | 551 TEST_F(BufferedResourceLoaderTest, RequestFailedWithNoPendingReads) { |
548 Initialize(kHttpUrl, 10, 29); | 552 Initialize(kHttpUrl, 10, 29); |
549 Start(); | 553 Start(); |
550 PartialResponse(10, 29, 30); | 554 PartialResponse(10, 29, 30); |
551 | 555 |
552 uint8 buffer[10]; | 556 uint8_t buffer[10]; |
553 InSequence s; | 557 InSequence s; |
554 | 558 |
555 // Write enough data so that a read would technically complete had the request | 559 // Write enough data so that a read would technically complete had the request |
556 // not failed. | 560 // not failed. |
557 WriteLoader(10, 20); | 561 WriteLoader(10, 20); |
558 | 562 |
559 // Fail without a pending read. | 563 // Fail without a pending read. |
560 WebURLError error; | 564 WebURLError error; |
561 error.reason = net::ERR_TIMED_OUT; | 565 error.reason = net::ERR_TIMED_OUT; |
562 error.isCancellation = false; | 566 error.isCancellation = false; |
563 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); | 567 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); |
564 loader_->didFail(url_loader_, error); | 568 loader_->didFail(url_loader_, error); |
565 | 569 |
566 // Now we should immediately fail any read even if we have data buffered. | 570 // Now we should immediately fail any read even if we have data buffered. |
567 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); | 571 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); |
568 ReadLoader(10, 10, buffer); | 572 ReadLoader(10, 10, buffer); |
569 } | 573 } |
570 | 574 |
571 TEST_F(BufferedResourceLoaderTest, RequestCancelledWhenRead) { | 575 TEST_F(BufferedResourceLoaderTest, RequestCancelledWhenRead) { |
572 Initialize(kHttpUrl, 10, 29); | 576 Initialize(kHttpUrl, 10, 29); |
573 Start(); | 577 Start(); |
574 PartialResponse(10, 29, 30); | 578 PartialResponse(10, 29, 30); |
575 | 579 |
576 uint8 buffer[10]; | 580 uint8_t buffer[10]; |
577 InSequence s; | 581 InSequence s; |
578 | 582 |
579 // We should convert any error we receive to BufferedResourceLoader::kFailed. | 583 // We should convert any error we receive to BufferedResourceLoader::kFailed. |
580 ReadLoader(10, 10, buffer); | 584 ReadLoader(10, 10, buffer); |
581 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); | 585 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed)); |
582 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); | 586 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0)); |
583 WebURLError error; | 587 WebURLError error; |
584 error.reason = 0; | 588 error.reason = 0; |
585 error.isCancellation = true; | 589 error.isCancellation = true; |
586 loader_->didFail(url_loader_, error); | 590 loader_->didFail(url_loader_, error); |
587 } | 591 } |
588 | 592 |
589 // Tests the data buffering logic of NeverDefer strategy. | 593 // Tests the data buffering logic of NeverDefer strategy. |
590 TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) { | 594 TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) { |
591 Initialize(kHttpUrl, 10, 99); | 595 Initialize(kHttpUrl, 10, 99); |
592 SetLoaderBuffer(10, 20); | 596 SetLoaderBuffer(10, 20); |
593 loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer); | 597 loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer); |
594 Start(); | 598 Start(); |
595 PartialResponse(10, 99, 100); | 599 PartialResponse(10, 99, 100); |
596 | 600 |
597 uint8 buffer[10]; | 601 uint8_t buffer[10]; |
598 | 602 |
599 // Read past the buffer size; should not defer regardless. | 603 // Read past the buffer size; should not defer regardless. |
600 WriteLoader(10, 10); | 604 WriteLoader(10, 10); |
601 WriteLoader(20, 50); | 605 WriteLoader(20, 50); |
602 | 606 |
603 // Should move past window. | 607 // Should move past window. |
604 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0)); | 608 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0)); |
605 ReadLoader(10, 10, buffer); | 609 ReadLoader(10, 10, buffer); |
606 | 610 |
607 StopWhenLoad(); | 611 StopWhenLoad(); |
608 } | 612 } |
609 | 613 |
610 // Tests the data buffering logic of ReadThenDefer strategy. | 614 // Tests the data buffering logic of ReadThenDefer strategy. |
611 TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) { | 615 TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) { |
612 Initialize(kHttpUrl, 10, 99); | 616 Initialize(kHttpUrl, 10, 99); |
613 SetLoaderBuffer(10, 20); | 617 SetLoaderBuffer(10, 20); |
614 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); | 618 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); |
615 Start(); | 619 Start(); |
616 PartialResponse(10, 99, 100); | 620 PartialResponse(10, 99, 100); |
617 | 621 |
618 uint8 buffer[10]; | 622 uint8_t buffer[10]; |
619 | 623 |
620 // Make an outstanding read request. | 624 // Make an outstanding read request. |
621 ReadLoader(10, 10, buffer); | 625 ReadLoader(10, 10, buffer); |
622 | 626 |
623 // Receive almost enough data to cover, shouldn't defer. | 627 // Receive almost enough data to cover, shouldn't defer. |
624 WriteLoader(10, 9); | 628 WriteLoader(10, 9); |
625 | 629 |
626 // As soon as we have received enough data to fulfill the read, defer. | 630 // As soon as we have received enough data to fulfill the read, defer. |
627 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 631 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
628 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 632 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
(...skipping 28 matching lines...) Expand all Loading... |
657 StopWhenLoad(); | 661 StopWhenLoad(); |
658 } | 662 } |
659 | 663 |
660 // Tests the data buffering logic of kCapacityDefer strategy. | 664 // Tests the data buffering logic of kCapacityDefer strategy. |
661 TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) { | 665 TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) { |
662 Initialize(kHttpUrl, 10, 99); | 666 Initialize(kHttpUrl, 10, 99); |
663 SetLoaderBuffer(10, 20); | 667 SetLoaderBuffer(10, 20); |
664 Start(); | 668 Start(); |
665 PartialResponse(10, 99, 100); | 669 PartialResponse(10, 99, 100); |
666 | 670 |
667 uint8 buffer[10]; | 671 uint8_t buffer[10]; |
668 InSequence s; | 672 InSequence s; |
669 | 673 |
670 // Write half of capacity: keep not deferring. | 674 // Write half of capacity: keep not deferring. |
671 WriteData(5); | 675 WriteData(5); |
672 | 676 |
673 // Write rest of space until capacity: start deferring. | 677 // Write rest of space until capacity: start deferring. |
674 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 678 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
675 WriteData(5); | 679 WriteData(5); |
676 | 680 |
677 // Read a byte from the buffer: stop deferring. | 681 // Read a byte from the buffer: stop deferring. |
678 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); | 682 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); |
679 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | 683 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
680 ReadLoader(10, 1, buffer); | 684 ReadLoader(10, 1, buffer); |
681 | 685 |
682 // Write a byte to hit capacity: start deferring. | 686 // Write a byte to hit capacity: start deferring. |
683 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 687 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
684 WriteData(6); | 688 WriteData(6); |
685 | 689 |
686 StopWhenLoad(); | 690 StopWhenLoad(); |
687 } | 691 } |
688 | 692 |
689 TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) { | 693 TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) { |
690 Initialize(kHttpUrl, 10, 99); | 694 Initialize(kHttpUrl, 10, 99); |
691 SetLoaderBuffer(10, 10); | 695 SetLoaderBuffer(10, 10); |
692 Start(); | 696 Start(); |
693 PartialResponse(10, 99, 100); | 697 PartialResponse(10, 99, 100); |
694 | 698 |
695 uint8 buffer[256]; | 699 uint8_t buffer[256]; |
696 InSequence s; | 700 InSequence s; |
697 | 701 |
698 // PRECONDITION | 702 // PRECONDITION |
699 WriteUntilThreshold(); | 703 WriteUntilThreshold(); |
700 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); | 704 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1)); |
701 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | 705 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
702 ReadLoader(10, 1, buffer); | 706 ReadLoader(10, 1, buffer); |
703 ConfirmBufferState(1, 10, 9, 10); | 707 ConfirmBufferState(1, 10, 9, 10); |
704 ConfirmLoaderOffsets(11, 0, 0); | 708 ConfirmLoaderOffsets(11, 0, 0); |
705 | 709 |
(...skipping 24 matching lines...) Expand all Loading... |
730 | 734 |
731 StopWhenLoad(); | 735 StopWhenLoad(); |
732 } | 736 } |
733 | 737 |
734 TEST_F(BufferedResourceLoaderTest, Tricky_ReadBackwardsPastBuffered) { | 738 TEST_F(BufferedResourceLoaderTest, Tricky_ReadBackwardsPastBuffered) { |
735 Initialize(kHttpUrl, 10, 99); | 739 Initialize(kHttpUrl, 10, 99); |
736 SetLoaderBuffer(10, 10); | 740 SetLoaderBuffer(10, 10); |
737 Start(); | 741 Start(); |
738 PartialResponse(10, 99, 100); | 742 PartialResponse(10, 99, 100); |
739 | 743 |
740 uint8 buffer[256]; | 744 uint8_t buffer[256]; |
741 InSequence s; | 745 InSequence s; |
742 | 746 |
743 // PRECONDITION | 747 // PRECONDITION |
744 WriteUntilThreshold(); | 748 WriteUntilThreshold(); |
745 ConfirmBufferState(0, 10, 10, 10); | 749 ConfirmBufferState(0, 10, 10, 10); |
746 ConfirmLoaderOffsets(10, 0, 0); | 750 ConfirmLoaderOffsets(10, 0, 0); |
747 | 751 |
748 // *** TRICKY BUSINESS, PT. II *** | 752 // *** TRICKY BUSINESS, PT. II *** |
749 // Read backwards a little too much: cache miss. | 753 // Read backwards a little too much: cache miss. |
750 // | 754 // |
(...skipping 12 matching lines...) Expand all Loading... |
763 | 767 |
764 StopWhenLoad(); | 768 StopWhenLoad(); |
765 } | 769 } |
766 | 770 |
767 TEST_F(BufferedResourceLoaderTest, Tricky_SmallReadWithinThreshold) { | 771 TEST_F(BufferedResourceLoaderTest, Tricky_SmallReadWithinThreshold) { |
768 Initialize(kHttpUrl, 10, 99); | 772 Initialize(kHttpUrl, 10, 99); |
769 SetLoaderBuffer(10, 10); | 773 SetLoaderBuffer(10, 10); |
770 Start(); | 774 Start(); |
771 PartialResponse(10, 99, 100); | 775 PartialResponse(10, 99, 100); |
772 | 776 |
773 uint8 buffer[256]; | 777 uint8_t buffer[256]; |
774 InSequence s; | 778 InSequence s; |
775 | 779 |
776 // PRECONDITION | 780 // PRECONDITION |
777 WriteUntilThreshold(); | 781 WriteUntilThreshold(); |
778 ConfirmBufferState(0, 10, 10, 10); | 782 ConfirmBufferState(0, 10, 10, 10); |
779 ConfirmLoaderOffsets(10, 0, 0); | 783 ConfirmLoaderOffsets(10, 0, 0); |
780 | 784 |
781 // *** TRICKY BUSINESS, PT. III *** | 785 // *** TRICKY BUSINESS, PT. III *** |
782 // Read past forward capacity but within capacity: stop deferring. | 786 // Read past forward capacity but within capacity: stop deferring. |
783 // | 787 // |
(...skipping 27 matching lines...) Expand all Loading... |
811 | 815 |
812 StopWhenLoad(); | 816 StopWhenLoad(); |
813 } | 817 } |
814 | 818 |
815 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadWithinThreshold) { | 819 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadWithinThreshold) { |
816 Initialize(kHttpUrl, 10, 99); | 820 Initialize(kHttpUrl, 10, 99); |
817 SetLoaderBuffer(10, 10); | 821 SetLoaderBuffer(10, 10); |
818 Start(); | 822 Start(); |
819 PartialResponse(10, 99, 100); | 823 PartialResponse(10, 99, 100); |
820 | 824 |
821 uint8 buffer[256]; | 825 uint8_t buffer[256]; |
822 InSequence s; | 826 InSequence s; |
823 | 827 |
824 // PRECONDITION | 828 // PRECONDITION |
825 WriteUntilThreshold(); | 829 WriteUntilThreshold(); |
826 ConfirmBufferState(0, 10, 10, 10); | 830 ConfirmBufferState(0, 10, 10, 10); |
827 ConfirmLoaderOffsets(10, 0, 0); | 831 ConfirmLoaderOffsets(10, 0, 0); |
828 | 832 |
829 // *** TRICKY BUSINESS, PT. IV *** | 833 // *** TRICKY BUSINESS, PT. IV *** |
830 // Read a large amount past forward capacity but within | 834 // Read a large amount past forward capacity but within |
831 // capacity: stop deferring. | 835 // capacity: stop deferring. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 | 870 |
867 StopWhenLoad(); | 871 StopWhenLoad(); |
868 } | 872 } |
869 | 873 |
870 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadBackwards) { | 874 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadBackwards) { |
871 Initialize(kHttpUrl, 10, 99); | 875 Initialize(kHttpUrl, 10, 99); |
872 SetLoaderBuffer(10, 10); | 876 SetLoaderBuffer(10, 10); |
873 Start(); | 877 Start(); |
874 PartialResponse(10, 99, 100); | 878 PartialResponse(10, 99, 100); |
875 | 879 |
876 uint8 buffer[256]; | 880 uint8_t buffer[256]; |
877 InSequence s; | 881 InSequence s; |
878 | 882 |
879 // PRECONDITION | 883 // PRECONDITION |
880 WriteUntilThreshold(); | 884 WriteUntilThreshold(); |
881 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 885 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
882 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); | 886 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading)); |
883 ReadLoader(10, 10, buffer); | 887 ReadLoader(10, 10, buffer); |
884 WriteUntilThreshold(); | 888 WriteUntilThreshold(); |
885 ConfirmBufferState(10, 10, 10, 10); | 889 ConfirmBufferState(10, 10, 10, 10); |
886 ConfirmLoaderOffsets(20, 0, 0); | 890 ConfirmLoaderOffsets(20, 0, 0); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
927 | 931 |
928 TEST_F(BufferedResourceLoaderTest, Tricky_ReadPastThreshold) { | 932 TEST_F(BufferedResourceLoaderTest, Tricky_ReadPastThreshold) { |
929 const int kSize = 5 * 1024 * 1024; | 933 const int kSize = 5 * 1024 * 1024; |
930 const int kThreshold = 2 * 1024 * 1024; | 934 const int kThreshold = 2 * 1024 * 1024; |
931 | 935 |
932 Initialize(kHttpUrl, 10, kSize); | 936 Initialize(kHttpUrl, 10, kSize); |
933 SetLoaderBuffer(10, 10); | 937 SetLoaderBuffer(10, 10); |
934 Start(); | 938 Start(); |
935 PartialResponse(10, kSize - 1, kSize); | 939 PartialResponse(10, kSize - 1, kSize); |
936 | 940 |
937 uint8 buffer[256]; | 941 uint8_t buffer[256]; |
938 InSequence s; | 942 InSequence s; |
939 | 943 |
940 // PRECONDITION | 944 // PRECONDITION |
941 WriteUntilThreshold(); | 945 WriteUntilThreshold(); |
942 ConfirmBufferState(0, 10, 10, 10); | 946 ConfirmBufferState(0, 10, 10, 10); |
943 ConfirmLoaderOffsets(10, 0, 0); | 947 ConfirmLoaderOffsets(10, 0, 0); |
944 | 948 |
945 // *** TRICKY BUSINESS, PT. VI *** | 949 // *** TRICKY BUSINESS, PT. VI *** |
946 // Read past the forward wait threshold: cache miss. | 950 // Read past the forward wait threshold: cache miss. |
947 // | 951 // |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1085 } | 1089 } |
1086 | 1090 |
1087 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) { | 1091 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) { |
1088 Initialize(kHttpUrl, -1, -1); | 1092 Initialize(kHttpUrl, -1, -1); |
1089 Start(); | 1093 Start(); |
1090 loader_->SetPlaybackRate(100); | 1094 loader_->SetPlaybackRate(100); |
1091 CheckBufferWindowBounds(); | 1095 CheckBufferWindowBounds(); |
1092 StopWhenLoad(); | 1096 StopWhenLoad(); |
1093 } | 1097 } |
1094 | 1098 |
1095 static void ExpectContentRange( | 1099 static void ExpectContentRange(const std::string& str, |
1096 const std::string& str, bool expect_success, | 1100 bool expect_success, |
1097 int64 expected_first, int64 expected_last, int64 expected_size) { | 1101 int64_t expected_first, |
1098 int64 first, last, size; | 1102 int64_t expected_last, |
| 1103 int64_t expected_size) { |
| 1104 int64_t first, last, size; |
1099 ASSERT_EQ(expect_success, BufferedResourceLoader::ParseContentRange( | 1105 ASSERT_EQ(expect_success, BufferedResourceLoader::ParseContentRange( |
1100 str, &first, &last, &size)) << str; | 1106 str, &first, &last, &size)) << str; |
1101 if (!expect_success) | 1107 if (!expect_success) |
1102 return; | 1108 return; |
1103 EXPECT_EQ(first, expected_first); | 1109 EXPECT_EQ(first, expected_first); |
1104 EXPECT_EQ(last, expected_last); | 1110 EXPECT_EQ(last, expected_last); |
1105 EXPECT_EQ(size, expected_size); | 1111 EXPECT_EQ(size, expected_size); |
1106 } | 1112 } |
1107 | 1113 |
1108 static void ExpectContentRangeFailure(const std::string& str) { | 1114 static void ExpectContentRangeFailure(const std::string& str) { |
1109 ExpectContentRange(str, false, 0, 0, 0); | 1115 ExpectContentRange(str, false, 0, 0, 0); |
1110 } | 1116 } |
1111 | 1117 |
1112 static void ExpectContentRangeSuccess( | 1118 static void ExpectContentRangeSuccess(const std::string& str, |
1113 const std::string& str, | 1119 int64_t expected_first, |
1114 int64 expected_first, int64 expected_last, int64 expected_size) { | 1120 int64_t expected_last, |
| 1121 int64_t expected_size) { |
1115 ExpectContentRange(str, true, expected_first, expected_last, expected_size); | 1122 ExpectContentRange(str, true, expected_first, expected_last, expected_size); |
1116 } | 1123 } |
1117 | 1124 |
1118 TEST(BufferedResourceLoaderStandaloneTest, ParseContentRange) { | 1125 TEST(BufferedResourceLoaderStandaloneTest, ParseContentRange) { |
1119 ExpectContentRangeFailure("cytes 0-499/500"); | 1126 ExpectContentRangeFailure("cytes 0-499/500"); |
1120 ExpectContentRangeFailure("bytes 0499/500"); | 1127 ExpectContentRangeFailure("bytes 0499/500"); |
1121 ExpectContentRangeFailure("bytes 0-499500"); | 1128 ExpectContentRangeFailure("bytes 0-499500"); |
1122 ExpectContentRangeFailure("bytes 0-499/500-blorg"); | 1129 ExpectContentRangeFailure("bytes 0-499/500-blorg"); |
1123 ExpectContentRangeFailure("bytes 0-499/500-1"); | 1130 ExpectContentRangeFailure("bytes 0-499/500-1"); |
1124 ExpectContentRangeFailure("bytes 0-499/400"); | 1131 ExpectContentRangeFailure("bytes 0-499/400"); |
(...skipping 10 matching lines...) Expand all Loading... |
1135 | 1142 |
1136 // Tests the data buffering logic of ReadThenDefer strategy. | 1143 // Tests the data buffering logic of ReadThenDefer strategy. |
1137 TEST_F(BufferedResourceLoaderTest, CancelAfterDeferral) { | 1144 TEST_F(BufferedResourceLoaderTest, CancelAfterDeferral) { |
1138 Initialize(kHttpUrl, 10, 99); | 1145 Initialize(kHttpUrl, 10, 99); |
1139 SetLoaderBuffer(10, 20); | 1146 SetLoaderBuffer(10, 20); |
1140 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); | 1147 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); |
1141 loader_->CancelUponDeferral(); | 1148 loader_->CancelUponDeferral(); |
1142 Start(); | 1149 Start(); |
1143 PartialResponse(10, 99, 100); | 1150 PartialResponse(10, 99, 100); |
1144 | 1151 |
1145 uint8 buffer[10]; | 1152 uint8_t buffer[10]; |
1146 | 1153 |
1147 // Make an outstanding read request. | 1154 // Make an outstanding read request. |
1148 ReadLoader(10, 10, buffer); | 1155 ReadLoader(10, 10, buffer); |
1149 | 1156 |
1150 // Receive almost enough data to cover, shouldn't defer. | 1157 // Receive almost enough data to cover, shouldn't defer. |
1151 WriteLoader(10, 9); | 1158 WriteLoader(10, 9); |
1152 EXPECT_TRUE(HasActiveLoader()); | 1159 EXPECT_TRUE(HasActiveLoader()); |
1153 | 1160 |
1154 // As soon as we have received enough data to fulfill the read, defer. | 1161 // As soon as we have received enough data to fulfill the read, defer. |
1155 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); | 1162 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred)); |
1156 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); | 1163 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10)); |
1157 WriteLoader(19, 1); | 1164 WriteLoader(19, 1); |
1158 VerifyBuffer(buffer, 10, 10); | 1165 VerifyBuffer(buffer, 10, 10); |
1159 EXPECT_FALSE(HasActiveLoader()); | 1166 EXPECT_FALSE(HasActiveLoader()); |
1160 } | 1167 } |
1161 | 1168 |
1162 } // namespace media | 1169 } // namespace media |
OLD | NEW |