Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: media/blink/buffered_resource_loader_unittest.cc

Issue 2272163002: Delete now deprecated BufferedDataSource and friends. (Closed)
Patch Set: Delete BufferedResourceLoader too. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/blink/buffered_resource_loader.cc ('k') | media/blink/multibuffer_data_source.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <stdint.h>
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/format_macros.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "media/base/media_log.h"
17 #include "media/base/seekable_buffer.h"
18 #include "media/blink/buffered_resource_loader.h"
19 #include "media/blink/mock_webframeclient.h"
20 #include "media/blink/mock_weburlloader.h"
21 #include "net/base/net_errors.h"
22 #include "net/http/http_request_headers.h"
23 #include "net/http/http_util.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/platform/WebURLError.h"
26 #include "third_party/WebKit/public/platform/WebURLRequest.h"
27 #include "third_party/WebKit/public/platform/WebURLResponse.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebView.h"
30
31 using ::testing::_;
32 using ::testing::InSequence;
33 using ::testing::Return;
34 using ::testing::Truly;
35 using ::testing::NiceMock;
36
37 using blink::WebLocalFrame;
38 using blink::WebString;
39 using blink::WebURLError;
40 using blink::WebURLResponse;
41 using blink::WebView;
42
43 namespace media {
44
45 static const char* kHttpUrl = "http://test";
46 static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
47 static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
48 static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
49
50 static const int kDataSize = 1024;
51 static const int kHttpOK = 200;
52 static const int kHttpPartialContent = 206;
53
54 enum NetworkState {
55 NONE,
56 LOADED,
57 LOADING
58 };
59
60 // Predicate that tests that request disallows compressed data.
61 static bool CorrectAcceptEncoding(const blink::WebURLRequest &request) {
62 std::string value = request.httpHeaderField(
63 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding)).utf8();
64 return (value.find("identity;q=1") != std::string::npos) &&
65 (value.find("*;q=0") != std::string::npos);
66 }
67
68 class BufferedResourceLoaderTest : public testing::Test {
69 public:
70 BufferedResourceLoaderTest()
71 : view_(WebView::create(nullptr, blink::WebPageVisibilityStateVisible)),
72 frame_(WebLocalFrame::create(blink::WebTreeScopeType::Document,
73 &client_)) {
74 view_->setMainFrame(frame_);
75
76 for (int i = 0; i < kDataSize; ++i) {
77 data_[i] = i;
78 }
79 }
80
81 virtual ~BufferedResourceLoaderTest() {
82 view_->close();
83 frame_->close();
84 }
85
86 void Initialize(const char* url, int first_position, int last_position) {
87 gurl_ = GURL(url);
88 first_position_ = first_position;
89 last_position_ = last_position;
90
91 loader_.reset(new BufferedResourceLoader(
92 gurl_, BufferedResourceLoader::kUnspecified,
93 first_position_, last_position_,
94 BufferedResourceLoader::kCapacityDefer, 0, 0,
95 new MediaLog()));
96
97 // |test_loader_| will be used when Start() is called.
98 url_loader_ = new NiceMock<MockWebURLLoader>();
99 loader_->test_loader_ = std::unique_ptr<blink::WebURLLoader>(url_loader_);
100 }
101
102 void SetLoaderBuffer(int forward_capacity, int backward_capacity) {
103 loader_->buffer_.set_forward_capacity(forward_capacity);
104 loader_->buffer_.set_backward_capacity(backward_capacity);
105 loader_->buffer_.Clear();
106 }
107
108 void Start() {
109 InSequence s;
110 EXPECT_CALL(*url_loader_, loadAsynchronously(Truly(CorrectAcceptEncoding),
111 loader_.get()));
112
113 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
114 loader_->Start(
115 base::Bind(&BufferedResourceLoaderTest::StartCallback,
116 base::Unretained(this)),
117 base::Bind(&BufferedResourceLoaderTest::LoadingCallback,
118 base::Unretained(this)),
119 base::Bind(&BufferedResourceLoaderTest::ProgressCallback,
120 base::Unretained(this)),
121 view_->mainFrame());
122 }
123
124 void FullResponse(int64_t instance_size) {
125 FullResponse(instance_size, BufferedResourceLoader::kOk);
126 }
127
128 void FullResponse(int64_t instance_size,
129 BufferedResourceLoader::Status status) {
130 EXPECT_CALL(*this, StartCallback(status));
131
132 WebURLResponse response(gurl_);
133 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
134 WebString::fromUTF8(base::StringPrintf("%"
135 PRId64, instance_size)));
136 response.setExpectedContentLength(instance_size);
137 response.setHTTPStatusCode(kHttpOK);
138 loader_->didReceiveResponse(url_loader_, response);
139
140 if (status == BufferedResourceLoader::kOk) {
141 EXPECT_EQ(instance_size, loader_->content_length());
142 EXPECT_EQ(instance_size, loader_->instance_size());
143 }
144
145 EXPECT_FALSE(loader_->range_supported());
146 }
147
148 void PartialResponse(int64_t first_position,
149 int64_t last_position,
150 int64_t instance_size) {
151 PartialResponse(first_position, last_position, instance_size, false, true);
152 }
153
154 void PartialResponse(int64_t first_position,
155 int64_t last_position,
156 int64_t instance_size,
157 bool chunked,
158 bool accept_ranges) {
159 EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kOk));
160
161 WebURLResponse response(gurl_);
162 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
163 WebString::fromUTF8(base::StringPrintf("bytes "
164 "%" PRId64 "-%" PRId64 "/%" PRId64,
165 first_position,
166 last_position,
167 instance_size)));
168
169 // HTTP 1.1 doesn't permit Content-Length with Transfer-Encoding: chunked.
170 int64_t content_length = -1;
171 if (chunked) {
172 response.setHTTPHeaderField(WebString::fromUTF8("Transfer-Encoding"),
173 WebString::fromUTF8("chunked"));
174 } else {
175 content_length = last_position - first_position + 1;
176 }
177 response.setExpectedContentLength(content_length);
178
179 // A server isn't required to return Accept-Ranges even though it might.
180 if (accept_ranges) {
181 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
182 WebString::fromUTF8("bytes"));
183 }
184
185 response.setHTTPStatusCode(kHttpPartialContent);
186 loader_->didReceiveResponse(url_loader_, response);
187
188 // XXX: what's the difference between these two? For example in the chunked
189 // range request case, Content-Length is unspecified (because it's chunked)
190 // but Content-Range: a-b/c can be returned, where c == Content-Length
191 //
192 // Can we eliminate one?
193 EXPECT_EQ(content_length, loader_->content_length());
194 EXPECT_EQ(instance_size, loader_->instance_size());
195
196 // A valid partial response should always result in this being true.
197 EXPECT_TRUE(loader_->range_supported());
198 }
199
200 void Redirect(const char* url) {
201 GURL redirectUrl(url);
202 blink::WebURLRequest newRequest(redirectUrl);
203 blink::WebURLResponse redirectResponse(gurl_);
204
205 loader_->willFollowRedirect(url_loader_, newRequest, redirectResponse, 0);
206
207 base::RunLoop().RunUntilIdle();
208 }
209
210 void StopWhenLoad() {
211 InSequence s;
212 EXPECT_CALL(*url_loader_, cancel());
213 loader_->Stop();
214 loader_.reset();
215 }
216
217 // Helper method to write to |loader_| from |data_|.
218 void WriteLoader(int position, int size) {
219 EXPECT_CALL(*this, ProgressCallback(position + size - 1));
220 loader_->didReceiveData(url_loader_,
221 reinterpret_cast<char*>(data_ + position), size,
222 size, size);
223 }
224
225 void WriteData(int size) {
226 EXPECT_CALL(*this, ProgressCallback(_));
227
228 std::unique_ptr<char[]> data(new char[size]);
229 loader_->didReceiveData(url_loader_, data.get(), size, size, size);
230 }
231
232 void WriteUntilThreshold() {
233 int buffered = loader_->buffer_.forward_bytes();
234 int capacity = loader_->buffer_.forward_capacity();
235 CHECK_LT(buffered, capacity);
236
237 EXPECT_CALL(*this, LoadingCallback(
238 BufferedResourceLoader::kLoadingDeferred));
239 WriteData(capacity - buffered);
240 }
241
242 // Helper method to read from |loader_|.
243 void ReadLoader(int64_t position, int size, uint8_t* buffer) {
244 loader_->Read(position, size, buffer,
245 base::Bind(&BufferedResourceLoaderTest::ReadCallback,
246 base::Unretained(this)));
247 }
248
249 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size].
250 void VerifyBuffer(uint8_t* buffer, int pos, int size) {
251 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size));
252 }
253
254 void ConfirmLoaderOffsets(int64_t expected_offset,
255 int expected_first_offset,
256 int expected_last_offset) {
257 EXPECT_EQ(loader_->offset_, expected_offset);
258 EXPECT_EQ(loader_->first_offset_, expected_first_offset);
259 EXPECT_EQ(loader_->last_offset_, expected_last_offset);
260 }
261
262 void ConfirmBufferState(int backward_bytes,
263 int backward_capacity,
264 int forward_bytes,
265 int forward_capacity) {
266 EXPECT_EQ(backward_bytes, loader_->buffer_.backward_bytes());
267 EXPECT_EQ(backward_capacity, loader_->buffer_.backward_capacity());
268 EXPECT_EQ(forward_bytes, loader_->buffer_.forward_bytes());
269 EXPECT_EQ(forward_capacity, loader_->buffer_.forward_capacity());
270 EXPECT_EQ(backward_bytes + forward_bytes, loader_->GetMemoryUsage());
271 }
272
273 void ConfirmLoaderBufferBackwardCapacity(int expected_backward_capacity) {
274 EXPECT_EQ(loader_->buffer_.backward_capacity(),
275 expected_backward_capacity);
276 }
277
278 void ConfirmLoaderBufferForwardCapacity(int expected_forward_capacity) {
279 EXPECT_EQ(loader_->buffer_.forward_capacity(), expected_forward_capacity);
280 }
281
282 // Makes sure the |loader_| buffer window is in a reasonable range.
283 void CheckBufferWindowBounds() {
284 // Corresponds to value defined in buffered_resource_loader.cc.
285 static const int kMinBufferCapacity = 2 * 1024 * 1024;
286 EXPECT_GE(loader_->buffer_.forward_capacity(), kMinBufferCapacity);
287 EXPECT_GE(loader_->buffer_.backward_capacity(), kMinBufferCapacity);
288
289 // Corresponds to value defined in buffered_resource_loader.cc.
290 static const int kMaxBufferCapacity = 20 * 1024 * 1024;
291 EXPECT_LE(loader_->buffer_.forward_capacity(), kMaxBufferCapacity);
292 EXPECT_LE(loader_->buffer_.backward_capacity(), kMaxBufferCapacity);
293 }
294
295 bool HasActiveLoader() { return !!loader_->active_loader_; }
296
297 MOCK_METHOD1(StartCallback, void(BufferedResourceLoader::Status));
298 MOCK_METHOD2(ReadCallback, void(BufferedResourceLoader::Status, int));
299 MOCK_METHOD1(LoadingCallback, void(BufferedResourceLoader::LoadingState));
300 MOCK_METHOD1(ProgressCallback, void(int64_t));
301
302 protected:
303 GURL gurl_;
304 int64_t first_position_;
305 int64_t last_position_;
306
307 std::unique_ptr<BufferedResourceLoader> loader_;
308 NiceMock<MockWebURLLoader>* url_loader_;
309
310 MockWebFrameClient client_;
311 WebView* view_;
312 WebLocalFrame* frame_;
313
314 base::MessageLoop message_loop_;
315
316 uint8_t data_[kDataSize];
317
318 private:
319 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest);
320 };
321
322 TEST_F(BufferedResourceLoaderTest, StartStop) {
323 Initialize(kHttpUrl, -1, -1);
324 Start();
325 StopWhenLoad();
326 }
327
328 // Tests that a bad HTTP response is recived, e.g. file not found.
329 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) {
330 Initialize(kHttpUrl, -1, -1);
331 Start();
332
333 EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kFailed));
334
335 WebURLResponse response(gurl_);
336 response.setHTTPStatusCode(404);
337 response.setHTTPStatusText("Not Found\n");
338 loader_->didReceiveResponse(url_loader_, response);
339 StopWhenLoad();
340 }
341
342 // Tests that partial content is requested but not fulfilled.
343 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) {
344 Initialize(kHttpUrl, 100, -1);
345 Start();
346 FullResponse(1024, BufferedResourceLoader::kFailed);
347 StopWhenLoad();
348 }
349
350 // Tests that a 200 response is received.
351 TEST_F(BufferedResourceLoaderTest, FullResponse) {
352 Initialize(kHttpUrl, -1, -1);
353 Start();
354 FullResponse(1024);
355 StopWhenLoad();
356 }
357
358 // Tests that a partial content response is received.
359 TEST_F(BufferedResourceLoaderTest, PartialResponse) {
360 Initialize(kHttpUrl, 100, 200);
361 Start();
362 PartialResponse(100, 200, 1024);
363 StopWhenLoad();
364 }
365
366 TEST_F(BufferedResourceLoaderTest, PartialResponse_Chunked) {
367 Initialize(kHttpUrl, 100, 200);
368 Start();
369 PartialResponse(100, 200, 1024, true, true);
370 StopWhenLoad();
371 }
372
373 TEST_F(BufferedResourceLoaderTest, PartialResponse_NoAcceptRanges) {
374 Initialize(kHttpUrl, 100, 200);
375 Start();
376 PartialResponse(100, 200, 1024, false, false);
377 StopWhenLoad();
378 }
379
380 TEST_F(BufferedResourceLoaderTest, PartialResponse_ChunkedNoAcceptRanges) {
381 Initialize(kHttpUrl, 100, 200);
382 Start();
383 PartialResponse(100, 200, 1024, true, false);
384 StopWhenLoad();
385 }
386
387 // Tests that an invalid partial response is received.
388 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) {
389 Initialize(kHttpUrl, 0, 10);
390 Start();
391
392 EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kFailed));
393
394 WebURLResponse response(gurl_);
395 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
396 WebString::fromUTF8(base::StringPrintf("bytes "
397 "%d-%d/%d", 1, 10, 1024)));
398 response.setExpectedContentLength(10);
399 response.setHTTPStatusCode(kHttpPartialContent);
400 loader_->didReceiveResponse(url_loader_, response);
401 StopWhenLoad();
402 }
403
404 // Tests the logic of sliding window for data buffering and reading.
405 TEST_F(BufferedResourceLoaderTest, BufferAndRead) {
406 Initialize(kHttpUrl, 10, 29);
407 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer);
408 Start();
409 PartialResponse(10, 29, 30);
410
411 uint8_t buffer[10];
412 InSequence s;
413
414 // Writes 10 bytes and read them back.
415 WriteLoader(10, 10);
416 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
417 ReadLoader(10, 10, buffer);
418 VerifyBuffer(buffer, 10, 10);
419
420 // Writes 10 bytes and read 2 times.
421 WriteLoader(20, 10);
422 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
423 ReadLoader(20, 5, buffer);
424 VerifyBuffer(buffer, 20, 5);
425 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
426 ReadLoader(25, 5, buffer);
427 VerifyBuffer(buffer, 25, 5);
428
429 // Read backward within buffer.
430 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
431 ReadLoader(10, 10, buffer);
432 VerifyBuffer(buffer, 10, 10);
433
434 // Read backward outside buffer.
435 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
436 ReadLoader(9, 10, buffer);
437
438 // Response has completed.
439 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished));
440 loader_->didFinishLoading(url_loader_, 0, -1);
441
442 // Try to read 10 from position 25 will just return with 5 bytes.
443 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
444 ReadLoader(25, 10, buffer);
445 VerifyBuffer(buffer, 25, 5);
446
447 // Try to read outside buffered range after request has completed.
448 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
449 ReadLoader(5, 10, buffer);
450
451 // Try to read beyond the instance size.
452 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 0));
453 ReadLoader(30, 10, buffer);
454 }
455
456 // Tests the logic of expanding the data buffer for large reads.
457 TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) {
458 Initialize(kHttpUrl, 10, 0x014FFFFFF);
459 SetLoaderBuffer(10, 20);
460 Start();
461 PartialResponse(10, 0x014FFFFFF, 0x015000000);
462
463 uint8_t buffer[20];
464 InSequence s;
465
466 // Write more than forward capacity and read it back. Ensure forward capacity
467 // gets reset after reading.
468 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
469 WriteLoader(10, 20);
470
471 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20));
472 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
473 ReadLoader(10, 20, buffer);
474
475 VerifyBuffer(buffer, 10, 20);
476 ConfirmLoaderBufferForwardCapacity(10);
477
478 // Make and outstanding read request larger than forward capacity. Ensure
479 // forward capacity gets extended.
480 ReadLoader(30, 20, buffer);
481 ConfirmLoaderBufferForwardCapacity(20);
482
483 // Fulfill outstanding request. Ensure forward capacity gets reset.
484 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20));
485 WriteLoader(30, 20);
486
487 VerifyBuffer(buffer, 30, 20);
488 ConfirmLoaderBufferForwardCapacity(10);
489
490 // Try to read further ahead than kForwardWaitThreshold allows. Ensure
491 // forward capacity is not changed.
492 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
493 ReadLoader(0x00300000, 1, buffer);
494
495 ConfirmLoaderBufferForwardCapacity(10);
496
497 // Try to read more than maximum forward capacity. Ensure forward capacity is
498 // not changed.
499 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
500 ReadLoader(30, 0x01400001, buffer);
501
502 ConfirmLoaderBufferForwardCapacity(10);
503
504 StopWhenLoad();
505 }
506
507 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) {
508 Initialize(kHttpUrl, 10, 0x00FFFFFF);
509 Start();
510 PartialResponse(10, 0x00FFFFFF, 0x01000000);
511
512 uint8_t buffer[10];
513 InSequence s;
514
515 // Read very far ahead will get a cache miss.
516 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
517 ReadLoader(0x00FFFFFF, 1, buffer);
518
519 // The following call will not call ReadCallback() because it is waiting for
520 // data to arrive.
521 ReadLoader(10, 10, buffer);
522
523 // Writing to loader will fulfill the read request.
524 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
525 WriteLoader(10, 20);
526 VerifyBuffer(buffer, 10, 10);
527
528 // The following call cannot be fulfilled now.
529 ReadLoader(25, 10, buffer);
530
531 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished));
532 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
533 loader_->didFinishLoading(url_loader_, 0, -1);
534 }
535
536 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) {
537 Initialize(kHttpUrl, 10, 29);
538 Start();
539 PartialResponse(10, 29, 30);
540
541 uint8_t buffer[10];
542 InSequence s;
543
544 // We should convert any error we receive to BufferedResourceLoader::kFailed.
545 ReadLoader(10, 10, buffer);
546 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
547 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
548 WebURLError error;
549 error.reason = net::ERR_TIMED_OUT;
550 error.isCancellation = false;
551 loader_->didFail(url_loader_, error);
552 }
553
554 TEST_F(BufferedResourceLoaderTest, RequestFailedWithNoPendingReads) {
555 Initialize(kHttpUrl, 10, 29);
556 Start();
557 PartialResponse(10, 29, 30);
558
559 uint8_t buffer[10];
560 InSequence s;
561
562 // Write enough data so that a read would technically complete had the request
563 // not failed.
564 WriteLoader(10, 20);
565
566 // Fail without a pending read.
567 WebURLError error;
568 error.reason = net::ERR_TIMED_OUT;
569 error.isCancellation = false;
570 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
571 loader_->didFail(url_loader_, error);
572
573 // Now we should immediately fail any read even if we have data buffered.
574 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
575 ReadLoader(10, 10, buffer);
576 }
577
578 TEST_F(BufferedResourceLoaderTest, RequestCancelledWhenRead) {
579 Initialize(kHttpUrl, 10, 29);
580 Start();
581 PartialResponse(10, 29, 30);
582
583 uint8_t buffer[10];
584 InSequence s;
585
586 // We should convert any error we receive to BufferedResourceLoader::kFailed.
587 ReadLoader(10, 10, buffer);
588 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
589 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
590 WebURLError error;
591 error.reason = 0;
592 error.isCancellation = true;
593 loader_->didFail(url_loader_, error);
594 }
595
596 // Tests the data buffering logic of NeverDefer strategy.
597 TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) {
598 Initialize(kHttpUrl, 10, 99);
599 SetLoaderBuffer(10, 20);
600 loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer);
601 Start();
602 PartialResponse(10, 99, 100);
603
604 uint8_t buffer[10];
605
606 // Read past the buffer size; should not defer regardless.
607 WriteLoader(10, 10);
608 WriteLoader(20, 50);
609
610 // Should move past window.
611 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
612 ReadLoader(10, 10, buffer);
613
614 StopWhenLoad();
615 }
616
617 // Tests the data buffering logic of ReadThenDefer strategy.
618 TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) {
619 Initialize(kHttpUrl, 10, 99);
620 SetLoaderBuffer(10, 20);
621 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer);
622 Start();
623 PartialResponse(10, 99, 100);
624
625 uint8_t buffer[10];
626
627 // Make an outstanding read request.
628 ReadLoader(10, 10, buffer);
629
630 // Receive almost enough data to cover, shouldn't defer.
631 WriteLoader(10, 9);
632
633 // As soon as we have received enough data to fulfill the read, defer.
634 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
635 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
636 WriteLoader(19, 1);
637
638 VerifyBuffer(buffer, 10, 10);
639
640 // Read again which should disable deferring since there should be nothing
641 // left in our internal buffer.
642 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
643 ReadLoader(20, 10, buffer);
644
645 // Over-fulfill requested bytes, then deferring should be enabled again.
646 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
647 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
648 WriteLoader(20, 40);
649
650 VerifyBuffer(buffer, 20, 10);
651
652 // Read far ahead, which should disable deferring. In this case we still have
653 // bytes in our internal buffer.
654 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
655 ReadLoader(80, 10, buffer);
656
657 // Fulfill requested bytes, then deferring should be enabled again.
658 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
659 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
660 WriteLoader(60, 40);
661
662 VerifyBuffer(buffer, 80, 10);
663
664 StopWhenLoad();
665 }
666
667 // Tests the data buffering logic of kCapacityDefer strategy.
668 TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) {
669 Initialize(kHttpUrl, 10, 99);
670 SetLoaderBuffer(10, 20);
671 Start();
672 PartialResponse(10, 99, 100);
673
674 uint8_t buffer[10];
675 InSequence s;
676
677 // Write half of capacity: keep not deferring.
678 WriteData(5);
679
680 // Write rest of space until capacity: start deferring.
681 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
682 WriteData(5);
683
684 // Read a byte from the buffer: stop deferring.
685 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1));
686 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
687 ReadLoader(10, 1, buffer);
688
689 // Write a byte to hit capacity: start deferring.
690 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
691 WriteData(6);
692
693 StopWhenLoad();
694 }
695
696 TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) {
697 Initialize(kHttpUrl, 10, 99);
698 SetLoaderBuffer(10, 10);
699 Start();
700 PartialResponse(10, 99, 100);
701
702 uint8_t buffer[256];
703 InSequence s;
704
705 // PRECONDITION
706 WriteUntilThreshold();
707 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1));
708 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
709 ReadLoader(10, 1, buffer);
710 ConfirmBufferState(1, 10, 9, 10);
711 ConfirmLoaderOffsets(11, 0, 0);
712
713 // *** TRICKY BUSINESS, PT. I ***
714 // Read past buffered: stop deferring.
715 //
716 // In order for the read to complete we must:
717 // 1) Stop deferring to receive more data.
718 //
719 // BEFORE
720 // offset=11 [xxxxxxxxx_]
721 // ^ ^^^ requested 4 bytes @ offset 20
722 // AFTER
723 // offset=24 [__________]
724 //
725 ReadLoader(20, 4, buffer);
726
727 // Write a little, make sure we didn't start deferring.
728 WriteData(2);
729
730 // Write the rest, read should complete.
731 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4));
732 WriteData(2);
733
734 // POSTCONDITION
735 ConfirmBufferState(4, 10, 0, 10);
736 ConfirmLoaderOffsets(24, 0, 0);
737
738 StopWhenLoad();
739 }
740
741 TEST_F(BufferedResourceLoaderTest, Tricky_ReadBackwardsPastBuffered) {
742 Initialize(kHttpUrl, 10, 99);
743 SetLoaderBuffer(10, 10);
744 Start();
745 PartialResponse(10, 99, 100);
746
747 uint8_t buffer[256];
748 InSequence s;
749
750 // PRECONDITION
751 WriteUntilThreshold();
752 ConfirmBufferState(0, 10, 10, 10);
753 ConfirmLoaderOffsets(10, 0, 0);
754
755 // *** TRICKY BUSINESS, PT. II ***
756 // Read backwards a little too much: cache miss.
757 //
758 // BEFORE
759 // offset=10 [__________|xxxxxxxxxx]
760 // ^ ^^^ requested 10 bytes @ offset 9
761 // AFTER
762 // offset=10 [__________|xxxxxxxxxx] !!! cache miss !!!
763 //
764 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
765 ReadLoader(9, 4, buffer);
766
767 // POSTCONDITION
768 ConfirmBufferState(0, 10, 10, 10);
769 ConfirmLoaderOffsets(10, 0, 0);
770
771 StopWhenLoad();
772 }
773
774 TEST_F(BufferedResourceLoaderTest, Tricky_SmallReadWithinThreshold) {
775 Initialize(kHttpUrl, 10, 99);
776 SetLoaderBuffer(10, 10);
777 Start();
778 PartialResponse(10, 99, 100);
779
780 uint8_t buffer[256];
781 InSequence s;
782
783 // PRECONDITION
784 WriteUntilThreshold();
785 ConfirmBufferState(0, 10, 10, 10);
786 ConfirmLoaderOffsets(10, 0, 0);
787
788 // *** TRICKY BUSINESS, PT. III ***
789 // Read past forward capacity but within capacity: stop deferring.
790 //
791 // In order for the read to complete we must:
792 // 1) Adjust offset forward to create capacity.
793 // 2) Stop deferring to receive more data.
794 //
795 // BEFORE
796 // offset=10 [xxxxxxxxxx]
797 // ^^^^ requested 4 bytes @ offset 24
798 // ADJUSTED OFFSET
799 // offset=20 [__________]
800 // ^^^^ requested 4 bytes @ offset 24
801 // AFTER
802 // offset=28 [__________]
803 //
804 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
805 ReadLoader(24, 4, buffer);
806 ConfirmLoaderOffsets(20, 4, 8);
807
808 // Write a little, make sure we didn't start deferring.
809 WriteData(4);
810
811 // Write the rest, read should complete.
812 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4));
813 WriteData(4);
814
815 // POSTCONDITION
816 ConfirmBufferState(8, 10, 0, 10);
817 ConfirmLoaderOffsets(28, 0, 0);
818
819 StopWhenLoad();
820 }
821
822 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadWithinThreshold) {
823 Initialize(kHttpUrl, 10, 99);
824 SetLoaderBuffer(10, 10);
825 Start();
826 PartialResponse(10, 99, 100);
827
828 uint8_t buffer[256];
829 InSequence s;
830
831 // PRECONDITION
832 WriteUntilThreshold();
833 ConfirmBufferState(0, 10, 10, 10);
834 ConfirmLoaderOffsets(10, 0, 0);
835
836 // *** TRICKY BUSINESS, PT. IV ***
837 // Read a large amount past forward capacity but within
838 // capacity: stop deferring.
839 //
840 // In order for the read to complete we must:
841 // 1) Adjust offset forward to create capacity.
842 // 2) Expand capacity to make sure we don't defer as data arrives.
843 // 3) Stop deferring to receive more data.
844 //
845 // BEFORE
846 // offset=10 [xxxxxxxxxx]
847 // ^^^^^^^^^^^^ requested 12 bytes @ offset 24
848 // ADJUSTED OFFSET
849 // offset=20 [__________]
850 // ^^^^^^ ^^^^^^ requested 12 bytes @ offset 24
851 // ADJUSTED CAPACITY
852 // offset=20 [________________]
853 // ^^^^^^^^^^^^ requested 12 bytes @ offset 24
854 // AFTER
855 // offset=36 [__________]
856 //
857 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
858 ReadLoader(24, 12, buffer);
859 ConfirmLoaderOffsets(20, 4, 16);
860 ConfirmBufferState(10, 10, 0, 16);
861
862 // Write a little, make sure we didn't start deferring.
863 WriteData(10);
864
865 // Write the rest, read should complete and capacity should go back to normal.
866 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 12));
867 WriteData(6);
868 ConfirmLoaderBufferForwardCapacity(10);
869
870 // POSTCONDITION
871 ConfirmBufferState(6, 10, 0, 10);
872 ConfirmLoaderOffsets(36, 0, 0);
873
874 StopWhenLoad();
875 }
876
877 TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadBackwards) {
878 Initialize(kHttpUrl, 10, 99);
879 SetLoaderBuffer(10, 10);
880 Start();
881 PartialResponse(10, 99, 100);
882
883 uint8_t buffer[256];
884 InSequence s;
885
886 // PRECONDITION
887 WriteUntilThreshold();
888 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
889 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
890 ReadLoader(10, 10, buffer);
891 WriteUntilThreshold();
892 ConfirmBufferState(10, 10, 10, 10);
893 ConfirmLoaderOffsets(20, 0, 0);
894
895 // *** TRICKY BUSINESS, PT. V ***
896 // Read a large amount that involves backwards data: stop deferring.
897 //
898 // In order for the read to complete we must:
899 // 1) Adjust offset *backwards* to create capacity.
900 // 2) Expand capacity to make sure we don't defer as data arrives.
901 // 3) Stop deferring to receive more data.
902 //
903 // BEFORE
904 // offset=20 [xxxxxxxxxx|xxxxxxxxxx]
905 // ^^^^ ^^^^^^^^^^ ^^^^ requested 18 bytes @ offset 16
906 // ADJUSTED OFFSET
907 // offset=16 [____xxxxxx|xxxxxxxxxx]xxxx
908 // ^^^^^^^^^^ ^^^^^^^^ requested 18 bytes @ offset 16
909 // ADJUSTED CAPACITY
910 // offset=16 [____xxxxxx|xxxxxxxxxxxxxx____]
911 // ^^^^^^^^^^^^^^^^^^ requested 18 bytes @ offset 16
912 // AFTER
913 // offset=34 [xxxxxxxxxx|__________]
914 //
915 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
916 ReadLoader(16, 18, buffer);
917 ConfirmLoaderOffsets(16, 0, 18);
918 ConfirmBufferState(6, 10, 14, 18);
919
920 // Write a little, make sure we didn't start deferring.
921 WriteData(2);
922
923 // Write the rest, read should complete and capacity should go back to normal.
924 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 18));
925 WriteData(2);
926 ConfirmLoaderBufferForwardCapacity(10);
927
928 // POSTCONDITION
929 ConfirmBufferState(4, 10, 0, 10);
930 ConfirmLoaderOffsets(34, 0, 0);
931
932 StopWhenLoad();
933 }
934
935 TEST_F(BufferedResourceLoaderTest, Tricky_ReadPastThreshold) {
936 const int kSize = 5 * 1024 * 1024;
937 const int kThreshold = 2 * 1024 * 1024;
938
939 Initialize(kHttpUrl, 10, kSize);
940 SetLoaderBuffer(10, 10);
941 Start();
942 PartialResponse(10, kSize - 1, kSize);
943
944 uint8_t buffer[256];
945 InSequence s;
946
947 // PRECONDITION
948 WriteUntilThreshold();
949 ConfirmBufferState(0, 10, 10, 10);
950 ConfirmLoaderOffsets(10, 0, 0);
951
952 // *** TRICKY BUSINESS, PT. VI ***
953 // Read past the forward wait threshold: cache miss.
954 //
955 // BEFORE
956 // offset=10 [xxxxxxxxxx] ...
957 // ^^^^ requested 10 bytes @ threshold
958 // AFTER
959 // offset=10 [xxxxxxxxxx] !!! cache miss !!!
960 //
961 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
962 ReadLoader(kThreshold + 20, 10, buffer);
963
964 // POSTCONDITION
965 ConfirmBufferState(0, 10, 10, 10);
966 ConfirmLoaderOffsets(10, 0, 0);
967
968 StopWhenLoad();
969 }
970
971 TEST_F(BufferedResourceLoaderTest, HasSingleOrigin) {
972 // Make sure no redirect case works as expected.
973 Initialize(kHttpUrl, -1, -1);
974 Start();
975 FullResponse(1024);
976 EXPECT_TRUE(loader_->HasSingleOrigin());
977 StopWhenLoad();
978
979 // Test redirect to the same domain.
980 Initialize(kHttpUrl, -1, -1);
981 Start();
982 Redirect(kHttpRedirectToSameDomainUrl1);
983 FullResponse(1024);
984 EXPECT_TRUE(loader_->HasSingleOrigin());
985 StopWhenLoad();
986
987 // Test redirect twice to the same domain.
988 Initialize(kHttpUrl, -1, -1);
989 Start();
990 Redirect(kHttpRedirectToSameDomainUrl1);
991 Redirect(kHttpRedirectToSameDomainUrl2);
992 FullResponse(1024);
993 EXPECT_TRUE(loader_->HasSingleOrigin());
994 StopWhenLoad();
995
996 // Test redirect to a different domain.
997 Initialize(kHttpUrl, -1, -1);
998 Start();
999 Redirect(kHttpRedirectToDifferentDomainUrl1);
1000 FullResponse(1024);
1001 EXPECT_FALSE(loader_->HasSingleOrigin());
1002 StopWhenLoad();
1003
1004 // Test redirect to the same domain and then to a different domain.
1005 Initialize(kHttpUrl, -1, -1);
1006 Start();
1007 Redirect(kHttpRedirectToSameDomainUrl1);
1008 Redirect(kHttpRedirectToDifferentDomainUrl1);
1009 FullResponse(1024);
1010 EXPECT_FALSE(loader_->HasSingleOrigin());
1011 StopWhenLoad();
1012 }
1013
1014 TEST_F(BufferedResourceLoaderTest, BufferWindow_Default) {
1015 Initialize(kHttpUrl, -1, -1);
1016 Start();
1017
1018 // Test ensures that default construction of a BufferedResourceLoader has sane
1019 // values.
1020 //
1021 // Please do not change these values in order to make a test pass! Instead,
1022 // start a conversation on what the default buffer window capacities should
1023 // be.
1024 ConfirmLoaderBufferBackwardCapacity(2 * 1024 * 1024);
1025 ConfirmLoaderBufferForwardCapacity(2 * 1024 * 1024);
1026
1027 StopWhenLoad();
1028 }
1029
1030 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_Unknown) {
1031 Initialize(kHttpUrl, -1, -1);
1032 Start();
1033 loader_->SetBitrate(0);
1034 CheckBufferWindowBounds();
1035 StopWhenLoad();
1036 }
1037
1038 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_BelowLowerBound) {
1039 Initialize(kHttpUrl, -1, -1);
1040 Start();
1041 loader_->SetBitrate(1024 * 8); // 1 Kbps.
1042 CheckBufferWindowBounds();
1043 StopWhenLoad();
1044 }
1045
1046 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_WithinBounds) {
1047 Initialize(kHttpUrl, -1, -1);
1048 Start();
1049 loader_->SetBitrate(2 * 1024 * 1024 * 8); // 2 Mbps.
1050 CheckBufferWindowBounds();
1051 StopWhenLoad();
1052 }
1053
1054 TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_AboveUpperBound) {
1055 Initialize(kHttpUrl, -1, -1);
1056 Start();
1057 loader_->SetBitrate(100 * 1024 * 1024 * 8); // 100 Mbps.
1058 CheckBufferWindowBounds();
1059 StopWhenLoad();
1060 }
1061
1062 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Negative) {
1063 Initialize(kHttpUrl, -1, -1);
1064 Start();
1065 loader_->SetPlaybackRate(-10);
1066 CheckBufferWindowBounds();
1067 StopWhenLoad();
1068 }
1069
1070 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Zero) {
1071 Initialize(kHttpUrl, -1, -1);
1072 Start();
1073 loader_->SetPlaybackRate(0);
1074 CheckBufferWindowBounds();
1075 StopWhenLoad();
1076 }
1077
1078 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_BelowLowerBound) {
1079 Initialize(kHttpUrl, -1, -1);
1080 Start();
1081 loader_->SetPlaybackRate(0.1);
1082 CheckBufferWindowBounds();
1083 StopWhenLoad();
1084 }
1085
1086 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_WithinBounds) {
1087 Initialize(kHttpUrl, -1, -1);
1088 Start();
1089 loader_->SetPlaybackRate(10);
1090 CheckBufferWindowBounds();
1091 StopWhenLoad();
1092 }
1093
1094 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) {
1095 Initialize(kHttpUrl, -1, -1);
1096 Start();
1097 loader_->SetPlaybackRate(100);
1098 CheckBufferWindowBounds();
1099 StopWhenLoad();
1100 }
1101
1102 static void ExpectContentRange(const std::string& str,
1103 bool expect_success,
1104 int64_t expected_first,
1105 int64_t expected_last,
1106 int64_t expected_size) {
1107 int64_t first, last, size;
1108 ASSERT_EQ(expect_success, BufferedResourceLoader::ParseContentRange(
1109 str, &first, &last, &size)) << str;
1110 if (!expect_success)
1111 return;
1112 EXPECT_EQ(first, expected_first);
1113 EXPECT_EQ(last, expected_last);
1114 EXPECT_EQ(size, expected_size);
1115 }
1116
1117 static void ExpectContentRangeFailure(const std::string& str) {
1118 ExpectContentRange(str, false, 0, 0, 0);
1119 }
1120
1121 static void ExpectContentRangeSuccess(const std::string& str,
1122 int64_t expected_first,
1123 int64_t expected_last,
1124 int64_t expected_size) {
1125 ExpectContentRange(str, true, expected_first, expected_last, expected_size);
1126 }
1127
1128 TEST(BufferedResourceLoaderStandaloneTest, ParseContentRange) {
1129 ExpectContentRangeFailure("cytes 0-499/500");
1130 ExpectContentRangeFailure("bytes 0499/500");
1131 ExpectContentRangeFailure("bytes 0-499500");
1132 ExpectContentRangeFailure("bytes 0-499/500-blorg");
1133 ExpectContentRangeFailure("bytes 0-499/500-1");
1134 ExpectContentRangeFailure("bytes 0-499/400");
1135 ExpectContentRangeFailure("bytes 0-/400");
1136 ExpectContentRangeFailure("bytes -300/400");
1137 ExpectContentRangeFailure("bytes 20-10/400");
1138
1139 ExpectContentRangeSuccess("bytes 0-499/500", 0, 499, 500);
1140 ExpectContentRangeSuccess("bytes 0-0/500", 0, 0, 500);
1141 ExpectContentRangeSuccess("bytes 10-11/50", 10, 11, 50);
1142 ExpectContentRangeSuccess("bytes 10-11/*", 10, 11,
1143 kPositionNotSpecified);
1144 }
1145
1146 // Tests the data buffering logic of ReadThenDefer strategy.
1147 TEST_F(BufferedResourceLoaderTest, CancelAfterDeferral) {
1148 Initialize(kHttpUrl, 10, 99);
1149 SetLoaderBuffer(10, 20);
1150 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer);
1151 loader_->CancelUponDeferral();
1152 Start();
1153 PartialResponse(10, 99, 100);
1154
1155 uint8_t buffer[10];
1156
1157 // Make an outstanding read request.
1158 ReadLoader(10, 10, buffer);
1159
1160 // Receive almost enough data to cover, shouldn't defer.
1161 WriteLoader(10, 9);
1162 EXPECT_TRUE(HasActiveLoader());
1163
1164 // As soon as we have received enough data to fulfill the read, defer.
1165 EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
1166 EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
1167 WriteLoader(19, 1);
1168 VerifyBuffer(buffer, 10, 10);
1169 EXPECT_FALSE(HasActiveLoader());
1170 }
1171
1172 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/buffered_resource_loader.cc ('k') | media/blink/multibuffer_data_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698