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

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

Issue 8667002: Split a portion of BufferedResourceLoader into a separate class ActiveLoader. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src
Patch Set: fixes Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « webkit/media/buffered_resource_loader.cc ('k') | webkit/media/webkit_media.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind.h" 8 #include "base/bind.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 static const int kDataSize = 1024; 46 static const int kDataSize = 1024;
47 static const int kHttpOK = 200; 47 static const int kHttpOK = 200;
48 static const int kHttpPartialContent = 206; 48 static const int kHttpPartialContent = 206;
49 49
50 enum NetworkState { 50 enum NetworkState {
51 NONE, 51 NONE,
52 LOADED, 52 LOADED,
53 LOADING 53 LOADING
54 }; 54 };
55 55
56 // Submit a request completed event to the resource loader due to request
57 // being canceled. Pretending the event is from external.
58 ACTION_P(RequestCanceled, loader) {
59 WebURLError error;
60 error.reason = net::ERR_ABORTED;
61 error.domain = WebString::fromUTF8(net::kErrorDomain);
62 loader->didFail(NULL, error);
63 }
64
65 // Predicate that tests that request disallows compressed data. 56 // Predicate that tests that request disallows compressed data.
66 static bool CorrectAcceptEncoding(const WebKit::WebURLRequest &request) { 57 static bool CorrectAcceptEncoding(const WebKit::WebURLRequest &request) {
67 std::string value = request.httpHeaderField( 58 std::string value = request.httpHeaderField(
68 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding)).utf8(); 59 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding)).utf8();
69 return (value.find("identity;q=1") != std::string::npos) && 60 return (value.find("identity;q=1") != std::string::npos) &&
70 (value.find("*;q=0") != std::string::npos); 61 (value.find("*;q=0") != std::string::npos);
71 } 62 }
72 63
73 class BufferedResourceLoaderTest : public testing::Test { 64 class BufferedResourceLoaderTest : public testing::Test {
74 public: 65 public:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 base::Unretained(this)), 104 base::Unretained(this)),
114 view_->mainFrame()); 105 view_->mainFrame());
115 } 106 }
116 107
117 void FullResponse(int64 instance_size) { 108 void FullResponse(int64 instance_size) {
118 FullResponse(instance_size, net::OK); 109 FullResponse(instance_size, net::OK);
119 } 110 }
120 111
121 void FullResponse(int64 instance_size, int status) { 112 void FullResponse(int64 instance_size, int status) {
122 EXPECT_CALL(*this, StartCallback(status)); 113 EXPECT_CALL(*this, StartCallback(status));
123 if (status != net::OK) {
124 EXPECT_CALL(*url_loader_, cancel())
125 .WillOnce(RequestCanceled(loader_));
126 }
127 114
128 WebURLResponse response(gurl_); 115 WebURLResponse response(gurl_);
129 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), 116 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
130 WebString::fromUTF8(base::StringPrintf("%" 117 WebString::fromUTF8(base::StringPrintf("%"
131 PRId64, instance_size))); 118 PRId64, instance_size)));
132 response.setExpectedContentLength(instance_size); 119 response.setExpectedContentLength(instance_size);
133 response.setHTTPStatusCode(kHttpOK); 120 response.setHTTPStatusCode(kHttpOK);
134 loader_->didReceiveResponse(url_loader_, response); 121 loader_->didReceiveResponse(url_loader_, response);
135 122
136 if (status == net::OK) { 123 if (status == net::OK) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 WebKit::WebURLRequest newRequest(redirectUrl); 181 WebKit::WebURLRequest newRequest(redirectUrl);
195 WebKit::WebURLResponse redirectResponse(gurl_); 182 WebKit::WebURLResponse redirectResponse(gurl_);
196 183
197 loader_->willSendRequest(url_loader_, newRequest, redirectResponse); 184 loader_->willSendRequest(url_loader_, newRequest, redirectResponse);
198 185
199 MessageLoop::current()->RunAllPending(); 186 MessageLoop::current()->RunAllPending();
200 } 187 }
201 188
202 void StopWhenLoad() { 189 void StopWhenLoad() {
203 InSequence s; 190 InSequence s;
204 EXPECT_CALL(*url_loader_, cancel()) 191 EXPECT_CALL(*url_loader_, cancel());
205 .WillOnce(RequestCanceled(loader_));
206 loader_->Stop(); 192 loader_->Stop();
207 loader_ = NULL; 193 loader_ = NULL;
208 } 194 }
209 195
210 // Helper method to write to |loader_| from |data_|. 196 // Helper method to write to |loader_| from |data_|.
211 void WriteLoader(int position, int size) { 197 void WriteLoader(int position, int size) {
212 EXPECT_CALL(*this, NetworkCallback()) 198 EXPECT_CALL(*this, NetworkCallback())
213 .RetiresOnSaturation(); 199 .RetiresOnSaturation();
214 loader_->didReceiveData(url_loader_, 200 loader_->didReceiveData(url_loader_,
215 reinterpret_cast<char*>(data_ + position), 201 reinterpret_cast<char*>(data_ + position),
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 void ConfirmLoaderBufferBackwardCapacity(size_t expected_backward_capacity) { 253 void ConfirmLoaderBufferBackwardCapacity(size_t expected_backward_capacity) {
268 EXPECT_EQ(loader_->buffer_->backward_capacity(), 254 EXPECT_EQ(loader_->buffer_->backward_capacity(),
269 expected_backward_capacity); 255 expected_backward_capacity);
270 } 256 }
271 257
272 void ConfirmLoaderBufferForwardCapacity(size_t expected_forward_capacity) { 258 void ConfirmLoaderBufferForwardCapacity(size_t expected_forward_capacity) {
273 EXPECT_EQ(loader_->buffer_->forward_capacity(), expected_forward_capacity); 259 EXPECT_EQ(loader_->buffer_->forward_capacity(), expected_forward_capacity);
274 } 260 }
275 261
276 void ConfirmLoaderDeferredState(bool expectedVal) { 262 void ConfirmLoaderDeferredState(bool expectedVal) {
277 EXPECT_EQ(loader_->deferred_, expectedVal); 263 EXPECT_EQ(loader_->active_loader_->deferred(), expectedVal);
278 } 264 }
279 265
280 // Makes sure the |loader_| buffer window is in a reasonable range. 266 // Makes sure the |loader_| buffer window is in a reasonable range.
281 void CheckBufferWindowBounds() { 267 void CheckBufferWindowBounds() {
282 // Corresponds to value defined in buffered_resource_loader.cc. 268 // Corresponds to value defined in buffered_resource_loader.cc.
283 static const size_t kMinBufferCapacity = 2 * 1024 * 1024; 269 static const size_t kMinBufferCapacity = 2 * 1024 * 1024;
284 EXPECT_GE(loader_->buffer_->forward_capacity(), kMinBufferCapacity); 270 EXPECT_GE(loader_->buffer_->forward_capacity(), kMinBufferCapacity);
285 EXPECT_GE(loader_->buffer_->backward_capacity(), kMinBufferCapacity); 271 EXPECT_GE(loader_->buffer_->backward_capacity(), kMinBufferCapacity);
286 272
287 // Corresponds to value defined in buffered_resource_loader.cc. 273 // Corresponds to value defined in buffered_resource_loader.cc.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 Start(); 306 Start();
321 StopWhenLoad(); 307 StopWhenLoad();
322 } 308 }
323 309
324 // Tests that a bad HTTP response is recived, e.g. file not found. 310 // Tests that a bad HTTP response is recived, e.g. file not found.
325 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { 311 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) {
326 Initialize(kHttpUrl, -1, -1); 312 Initialize(kHttpUrl, -1, -1);
327 Start(); 313 Start();
328 314
329 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); 315 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED));
330 EXPECT_CALL(*url_loader_, cancel())
331 .WillOnce(RequestCanceled(loader_));
332 316
333 WebURLResponse response(gurl_); 317 WebURLResponse response(gurl_);
334 response.setHTTPStatusCode(404); 318 response.setHTTPStatusCode(404);
335 response.setHTTPStatusText("Not Found\n"); 319 response.setHTTPStatusText("Not Found\n");
336 loader_->didReceiveResponse(url_loader_, response); 320 loader_->didReceiveResponse(url_loader_, response);
321 StopWhenLoad();
337 } 322 }
338 323
339 // Tests that partial content is requested but not fulfilled. 324 // Tests that partial content is requested but not fulfilled.
340 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { 325 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) {
341 Initialize(kHttpUrl, 100, -1); 326 Initialize(kHttpUrl, 100, -1);
342 Start(); 327 Start();
343 FullResponse(1024, net::ERR_INVALID_RESPONSE); 328 FullResponse(1024, net::ERR_INVALID_RESPONSE);
329 StopWhenLoad();
344 } 330 }
345 331
346 // Tests that a 200 response is received. 332 // Tests that a 200 response is received.
347 TEST_F(BufferedResourceLoaderTest, FullResponse) { 333 TEST_F(BufferedResourceLoaderTest, FullResponse) {
348 Initialize(kHttpUrl, -1, -1); 334 Initialize(kHttpUrl, -1, -1);
349 Start(); 335 Start();
350 FullResponse(1024); 336 FullResponse(1024);
351 StopWhenLoad(); 337 StopWhenLoad();
352 } 338 }
353 339
(...skipping 25 matching lines...) Expand all
379 PartialResponse(100, 200, 1024, true, false); 365 PartialResponse(100, 200, 1024, true, false);
380 StopWhenLoad(); 366 StopWhenLoad();
381 } 367 }
382 368
383 // Tests that an invalid partial response is received. 369 // Tests that an invalid partial response is received.
384 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { 370 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) {
385 Initialize(kHttpUrl, 0, 10); 371 Initialize(kHttpUrl, 0, 10);
386 Start(); 372 Start();
387 373
388 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); 374 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE));
389 EXPECT_CALL(*url_loader_, cancel())
390 .WillOnce(RequestCanceled(loader_));
391 375
392 WebURLResponse response(gurl_); 376 WebURLResponse response(gurl_);
393 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), 377 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
394 WebString::fromUTF8(base::StringPrintf("bytes " 378 WebString::fromUTF8(base::StringPrintf("bytes "
395 "%d-%d/%d", 1, 10, 1024))); 379 "%d-%d/%d", 1, 10, 1024)));
396 response.setExpectedContentLength(10); 380 response.setExpectedContentLength(10);
397 response.setHTTPStatusCode(kHttpPartialContent); 381 response.setHTTPStatusCode(kHttpPartialContent);
398 loader_->didReceiveResponse(url_loader_, response); 382 loader_->didReceiveResponse(url_loader_, response);
383 StopWhenLoad();
399 } 384 }
400 385
401 // Tests the logic of sliding window for data buffering and reading. 386 // Tests the logic of sliding window for data buffering and reading.
402 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { 387 TEST_F(BufferedResourceLoaderTest, BufferAndRead) {
403 Initialize(kHttpUrl, 10, 29); 388 Initialize(kHttpUrl, 10, 29);
404 loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer); 389 loader_->UpdateDeferStrategy(BufferedResourceLoader::kThresholdDefer);
405 Start(); 390 Start();
406 PartialResponse(10, 29, 30); 391 PartialResponse(10, 29, 30);
407 392
408 uint8 buffer[10]; 393 uint8 buffer[10];
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 ReadLoader(10, 10, buffer); 505 ReadLoader(10, 10, buffer);
521 506
522 // Writing to loader will fulfill the read request. 507 // Writing to loader will fulfill the read request.
523 EXPECT_CALL(*this, ReadCallback(10)); 508 EXPECT_CALL(*this, ReadCallback(10));
524 WriteLoader(10, 20); 509 WriteLoader(10, 20);
525 VerifyBuffer(buffer, 10, 10); 510 VerifyBuffer(buffer, 10, 10);
526 511
527 // The following call cannot be fulfilled now. 512 // The following call cannot be fulfilled now.
528 ReadLoader(25, 10, buffer); 513 ReadLoader(25, 10, buffer);
529 514
515 EXPECT_CALL(*this, NetworkCallback());
530 EXPECT_CALL(*this, ReadCallback(5)); 516 EXPECT_CALL(*this, ReadCallback(5));
531 EXPECT_CALL(*this, NetworkCallback());
532 loader_->didFinishLoading(url_loader_, 0); 517 loader_->didFinishLoading(url_loader_, 0);
533 } 518 }
534 519
535 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { 520 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) {
536 Initialize(kHttpUrl, 10, 29); 521 Initialize(kHttpUrl, 10, 29);
537 Start(); 522 Start();
538 PartialResponse(10, 29, 30); 523 PartialResponse(10, 29, 30);
539 524
540 uint8 buffer[10]; 525 uint8 buffer[10];
541 InSequence s; 526 InSequence s;
542 527
543 ReadLoader(10, 10, buffer); 528 ReadLoader(10, 10, buffer);
529 EXPECT_CALL(*this, NetworkCallback());
544 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); 530 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED));
545 EXPECT_CALL(*this, NetworkCallback());
546 WebURLError error; 531 WebURLError error;
547 error.reason = net::ERR_FAILED; 532 error.reason = net::ERR_FAILED;
548 loader_->didFail(url_loader_, error); 533 loader_->didFail(url_loader_, error);
549 } 534 }
550 535
551 // Tests the data buffering logic of NeverDefer strategy. 536 // Tests the data buffering logic of NeverDefer strategy.
552 TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) { 537 TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) {
553 Initialize(kHttpUrl, 10, 99); 538 Initialize(kHttpUrl, 10, 99);
554 SetLoaderBuffer(10, 20); 539 SetLoaderBuffer(10, 20);
555 loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer); 540 loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer);
(...skipping 18 matching lines...) Expand all
574 TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) { 559 TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) {
575 Initialize(kHttpUrl, 10, 99); 560 Initialize(kHttpUrl, 10, 99);
576 SetLoaderBuffer(10, 20); 561 SetLoaderBuffer(10, 20);
577 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer); 562 loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer);
578 Start(); 563 Start();
579 PartialResponse(10, 99, 100); 564 PartialResponse(10, 99, 100);
580 565
581 uint8 buffer[10]; 566 uint8 buffer[10];
582 567
583 // Make an outstanding read request. 568 // Make an outstanding read request.
584 // We should disable deferring after the read request, so expect
585 // a network event.
586 EXPECT_CALL(*this, NetworkCallback());
587 ReadLoader(10, 10, buffer); 569 ReadLoader(10, 10, buffer);
588 570
589 // Receive almost enough data to cover, shouldn't defer. 571 // Receive almost enough data to cover, shouldn't defer.
590 WriteLoader(10, 9); 572 WriteLoader(10, 9);
591 ConfirmLoaderDeferredState(false); 573 ConfirmLoaderDeferredState(false);
592 574
593 // As soon as we have received enough data to fulfill the read, defer. 575 // As soon as we have received enough data to fulfill the read, defer.
594 EXPECT_CALL(*this, NetworkCallback()); 576 EXPECT_CALL(*this, NetworkCallback());
595 EXPECT_CALL(*this, ReadCallback(10)); 577 EXPECT_CALL(*this, ReadCallback(10));
596 WriteLoader(19, 1); 578 WriteLoader(19, 1);
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 1084
1103 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) { 1085 TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) {
1104 Initialize(kHttpUrl, -1, -1); 1086 Initialize(kHttpUrl, -1, -1);
1105 Start(); 1087 Start();
1106 loader_->SetPlaybackRate(100); 1088 loader_->SetPlaybackRate(100);
1107 CheckBufferWindowBounds(); 1089 CheckBufferWindowBounds();
1108 StopWhenLoad(); 1090 StopWhenLoad();
1109 } 1091 }
1110 1092
1111 } // namespace webkit_media 1093 } // namespace webkit_media
OLDNEW
« no previous file with comments | « webkit/media/buffered_resource_loader.cc ('k') | webkit/media/webkit_media.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698