OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 <algorithm> |
| 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "net/base/net_errors.h" |
| 10 #include "net/http/http_util.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebFrameClient.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" |
| 16 #include "webkit/glue/media/buffered_resource_loader.h" |
| 17 #include "webkit/glue/mock_webframe.h" |
| 18 #include "webkit/glue/mock_weburlloader_impl.h" |
| 19 |
| 20 using ::testing::_; |
| 21 using ::testing::Assign; |
| 22 using ::testing::AtLeast; |
| 23 using ::testing::DeleteArg; |
| 24 using ::testing::DoAll; |
| 25 using ::testing::InSequence; |
| 26 using ::testing::Invoke; |
| 27 using ::testing::InvokeWithoutArgs; |
| 28 using ::testing::NotNull; |
| 29 using ::testing::Return; |
| 30 using ::testing::ReturnRef; |
| 31 using ::testing::SetArgumentPointee; |
| 32 using ::testing::StrictMock; |
| 33 using ::testing::NiceMock; |
| 34 using ::testing::WithArgs; |
| 35 |
| 36 using WebKit::WebURLError; |
| 37 using WebKit::WebFrameClient; |
| 38 using WebKit::WebURLResponse; |
| 39 using WebKit::WebView; |
| 40 |
| 41 namespace { |
| 42 |
| 43 const char* kHttpUrl = "http://test"; |
| 44 const int kDataSize = 1024; |
| 45 |
| 46 enum NetworkState { |
| 47 NONE, |
| 48 LOADED, |
| 49 LOADING |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 namespace webkit_glue { |
| 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 class BufferedResourceLoaderTest : public testing::Test { |
| 66 public: |
| 67 BufferedResourceLoaderTest() { |
| 68 url_loader_ = new NiceMock<MockWebURLLoader>(); |
| 69 |
| 70 for (int i = 0; i < kDataSize; ++i) |
| 71 data_[i] = i; |
| 72 } |
| 73 |
| 74 virtual ~BufferedResourceLoaderTest() { |
| 75 ignore_result(frame_.release()); |
| 76 } |
| 77 |
| 78 void Initialize(const char* url, int first_position, int last_position) { |
| 79 gurl_ = GURL(url); |
| 80 first_position_ = first_position; |
| 81 last_position_ = last_position; |
| 82 |
| 83 frame_.reset(new NiceMock<MockWebFrame>()); |
| 84 |
| 85 loader_ = new BufferedResourceLoader(gurl_, |
| 86 first_position_, last_position_); |
| 87 loader_->SetURLLoaderForTest(url_loader_); |
| 88 } |
| 89 |
| 90 void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) { |
| 91 loader_->buffer_.reset( |
| 92 new media::SeekableBuffer(backward_capacity, forward_capacity)); |
| 93 } |
| 94 |
| 95 void Start() { |
| 96 InSequence s; |
| 97 EXPECT_CALL(*url_loader_, loadAsynchronously(_, loader_.get())); |
| 98 loader_->Start( |
| 99 NewCallback(this, &BufferedResourceLoaderTest::StartCallback), |
| 100 NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback), |
| 101 frame_.get()); |
| 102 } |
| 103 |
| 104 void FullResponse(int64 instance_size) { |
| 105 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 106 |
| 107 WebURLResponse response(gurl_); |
| 108 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), |
| 109 WebString::fromUTF8(base::StringPrintf("%" |
| 110 PRId64, instance_size))); |
| 111 response.setExpectedContentLength(instance_size); |
| 112 response.setHTTPStatusCode(net::kHttpOK); |
| 113 loader_->didReceiveResponse(url_loader_, response); |
| 114 EXPECT_EQ(instance_size, loader_->content_length()); |
| 115 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 116 EXPECT_FALSE(loader_->partial_response()); |
| 117 } |
| 118 |
| 119 void PartialResponse(int64 first_position, int64 last_position, |
| 120 int64 instance_size) { |
| 121 EXPECT_CALL(*this, StartCallback(net::OK)); |
| 122 int64 content_length = last_position - first_position + 1; |
| 123 |
| 124 WebURLResponse response(gurl_); |
| 125 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), |
| 126 WebString::fromUTF8(base::StringPrintf("bytes " |
| 127 "%" PRId64 "-%" PRId64 "/%" PRId64, |
| 128 first_position, |
| 129 last_position, |
| 130 instance_size))); |
| 131 response.setExpectedContentLength(content_length); |
| 132 response.setHTTPStatusCode(net::kHttpPartialContent); |
| 133 loader_->didReceiveResponse(url_loader_, response); |
| 134 EXPECT_EQ(content_length, loader_->content_length()); |
| 135 EXPECT_EQ(instance_size, loader_->instance_size()); |
| 136 EXPECT_TRUE(loader_->partial_response()); |
| 137 } |
| 138 |
| 139 void StopWhenLoad() { |
| 140 InSequence s; |
| 141 EXPECT_CALL(*url_loader_, cancel()) |
| 142 .WillOnce(RequestCanceled(loader_)); |
| 143 loader_->Stop(); |
| 144 } |
| 145 |
| 146 // Helper method to write to |loader_| from |data_|. |
| 147 void WriteLoader(int position, int size) { |
| 148 EXPECT_CALL(*this, NetworkCallback()) |
| 149 .RetiresOnSaturation(); |
| 150 loader_->didReceiveData(url_loader_, |
| 151 reinterpret_cast<char*>(data_ + position), size); |
| 152 } |
| 153 |
| 154 // Helper method to read from |loader_|. |
| 155 void ReadLoader(int64 position, int size, uint8* buffer) { |
| 156 loader_->Read(position, size, buffer, |
| 157 NewCallback(this, &BufferedResourceLoaderTest::ReadCallback)); |
| 158 } |
| 159 |
| 160 // Verifis that data in buffer[0...size] is equal to data_[pos...pos+size]. |
| 161 void VerifyBuffer(uint8* buffer, int pos, int size) { |
| 162 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); |
| 163 } |
| 164 |
| 165 // Helper method to disallow deferring in |loader_|. |
| 166 void DisallowLoaderDefer() { |
| 167 if (loader_->deferred_) { |
| 168 EXPECT_CALL(*url_loader_, setDefersLoading(false)); |
| 169 EXPECT_CALL(*this, NetworkCallback()); |
| 170 } |
| 171 loader_->SetAllowDefer(false); |
| 172 } |
| 173 |
| 174 // Helper method to allow deferring in |loader_|. |
| 175 void AllowLoaderDefer() { |
| 176 loader_->SetAllowDefer(true); |
| 177 } |
| 178 |
| 179 MOCK_METHOD1(StartCallback, void(int error)); |
| 180 MOCK_METHOD1(ReadCallback, void(int error)); |
| 181 MOCK_METHOD0(NetworkCallback, void()); |
| 182 |
| 183 protected: |
| 184 GURL gurl_; |
| 185 int64 first_position_; |
| 186 int64 last_position_; |
| 187 |
| 188 scoped_refptr<BufferedResourceLoader> loader_; |
| 189 NiceMock<MockWebURLLoader>* url_loader_; |
| 190 scoped_ptr<NiceMock<MockWebFrame> > frame_; |
| 191 |
| 192 uint8 data_[kDataSize]; |
| 193 |
| 194 private: |
| 195 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest); |
| 196 }; |
| 197 |
| 198 TEST_F(BufferedResourceLoaderTest, StartStop) { |
| 199 Initialize(kHttpUrl, -1, -1); |
| 200 Start(); |
| 201 StopWhenLoad(); |
| 202 } |
| 203 |
| 204 // Tests that a bad HTTP response is recived, e.g. file not found. |
| 205 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { |
| 206 Initialize(kHttpUrl, -1, -1); |
| 207 Start(); |
| 208 |
| 209 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); |
| 210 EXPECT_CALL(*url_loader_, cancel()) |
| 211 .WillOnce(RequestCanceled(loader_)); |
| 212 |
| 213 WebURLResponse response(gurl_); |
| 214 response.setHTTPStatusCode(404); |
| 215 response.setHTTPStatusText("Not Found\n"); |
| 216 loader_->didReceiveResponse(url_loader_, response); |
| 217 } |
| 218 |
| 219 // Tests that partial content is requested but not fulfilled. |
| 220 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { |
| 221 Initialize(kHttpUrl, 100, -1); |
| 222 Start(); |
| 223 FullResponse(1024); |
| 224 StopWhenLoad(); |
| 225 } |
| 226 |
| 227 // Tests that a 200 response is received. |
| 228 TEST_F(BufferedResourceLoaderTest, FullResponse) { |
| 229 Initialize(kHttpUrl, -1, -1); |
| 230 Start(); |
| 231 FullResponse(1024); |
| 232 StopWhenLoad(); |
| 233 } |
| 234 |
| 235 // Tests that a partial content response is received. |
| 236 TEST_F(BufferedResourceLoaderTest, PartialResponse) { |
| 237 Initialize(kHttpUrl, 100, 200); |
| 238 Start(); |
| 239 PartialResponse(100, 200, 1024); |
| 240 StopWhenLoad(); |
| 241 } |
| 242 |
| 243 // Tests that an invalid partial response is received. |
| 244 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { |
| 245 Initialize(kHttpUrl, 0, 10); |
| 246 Start(); |
| 247 |
| 248 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); |
| 249 EXPECT_CALL(*url_loader_, cancel()) |
| 250 .WillOnce(RequestCanceled(loader_)); |
| 251 |
| 252 WebURLResponse response(gurl_); |
| 253 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), |
| 254 WebString::fromUTF8(base::StringPrintf("bytes " |
| 255 "%d-%d/%d", 1, 10, 1024))); |
| 256 response.setExpectedContentLength(10); |
| 257 response.setHTTPStatusCode(net::kHttpPartialContent); |
| 258 loader_->didReceiveResponse(url_loader_, response); |
| 259 } |
| 260 |
| 261 // Tests the logic of sliding window for data buffering and reading. |
| 262 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { |
| 263 Initialize(kHttpUrl, 10, 29); |
| 264 Start(); |
| 265 PartialResponse(10, 29, 30); |
| 266 |
| 267 uint8 buffer[10]; |
| 268 InSequence s; |
| 269 |
| 270 // Writes 10 bytes and read them back. |
| 271 WriteLoader(10, 10); |
| 272 EXPECT_CALL(*this, ReadCallback(10)); |
| 273 ReadLoader(10, 10, buffer); |
| 274 VerifyBuffer(buffer, 10, 10); |
| 275 |
| 276 // Writes 10 bytes and read 2 times. |
| 277 WriteLoader(20, 10); |
| 278 EXPECT_CALL(*this, ReadCallback(5)); |
| 279 ReadLoader(20, 5, buffer); |
| 280 VerifyBuffer(buffer, 20, 5); |
| 281 EXPECT_CALL(*this, ReadCallback(5)); |
| 282 ReadLoader(25, 5, buffer); |
| 283 VerifyBuffer(buffer, 25, 5); |
| 284 |
| 285 // Read backward within buffer. |
| 286 EXPECT_CALL(*this, ReadCallback(10)); |
| 287 ReadLoader(10, 10, buffer); |
| 288 VerifyBuffer(buffer, 10, 10); |
| 289 |
| 290 // Read backward outside buffer. |
| 291 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 292 ReadLoader(9, 10, buffer); |
| 293 |
| 294 // Response has completed. |
| 295 EXPECT_CALL(*this, NetworkCallback()); |
| 296 loader_->didFinishLoading(url_loader_, 0); |
| 297 |
| 298 // Try to read 10 from position 25 will just return with 5 bytes. |
| 299 EXPECT_CALL(*this, ReadCallback(5)); |
| 300 ReadLoader(25, 10, buffer); |
| 301 VerifyBuffer(buffer, 25, 5); |
| 302 |
| 303 // Try to read outside buffered range after request has completed. |
| 304 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 305 ReadLoader(5, 10, buffer); |
| 306 |
| 307 // Try to read beyond the instance size. |
| 308 EXPECT_CALL(*this, ReadCallback(0)); |
| 309 ReadLoader(30, 10, buffer); |
| 310 } |
| 311 |
| 312 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { |
| 313 Initialize(kHttpUrl, 10, 0x00FFFFFF); |
| 314 Start(); |
| 315 PartialResponse(10, 0x00FFFFFF, 0x01000000); |
| 316 |
| 317 uint8 buffer[10]; |
| 318 InSequence s; |
| 319 |
| 320 // Read very far aheard will get a cache miss. |
| 321 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 322 ReadLoader(0x00FFFFFF, 1, buffer); |
| 323 |
| 324 // The following call will not call ReadCallback() because it is waiting for |
| 325 // data to arrive. |
| 326 ReadLoader(10, 10, buffer); |
| 327 |
| 328 // Writing to loader will fulfill the read request. |
| 329 EXPECT_CALL(*this, ReadCallback(10)); |
| 330 WriteLoader(10, 20); |
| 331 VerifyBuffer(buffer, 10, 10); |
| 332 |
| 333 // The following call cannot be fulfilled now. |
| 334 ReadLoader(25, 10, buffer); |
| 335 |
| 336 EXPECT_CALL(*this, ReadCallback(5)); |
| 337 EXPECT_CALL(*this, NetworkCallback()); |
| 338 loader_->didFinishLoading(url_loader_, 0); |
| 339 } |
| 340 |
| 341 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { |
| 342 Initialize(kHttpUrl, 10, 29); |
| 343 Start(); |
| 344 PartialResponse(10, 29, 30); |
| 345 |
| 346 uint8 buffer[10]; |
| 347 InSequence s; |
| 348 |
| 349 ReadLoader(10, 10, buffer); |
| 350 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); |
| 351 EXPECT_CALL(*this, NetworkCallback()); |
| 352 WebURLError error; |
| 353 error.reason = net::ERR_FAILED; |
| 354 loader_->didFail(url_loader_, error); |
| 355 } |
| 356 |
| 357 // Tests the logic of caching data to disk when media is paused. |
| 358 TEST_F(BufferedResourceLoaderTest, AllowDefer_NoDataReceived) { |
| 359 Initialize(kHttpUrl, 10, 99); |
| 360 SetLoaderBuffer(10, 20); |
| 361 Start(); |
| 362 PartialResponse(10, 99, 100); |
| 363 |
| 364 // Start in undeferred state, then disallow defer, then allow defer |
| 365 // without receiving data in between. |
| 366 DisallowLoaderDefer(); |
| 367 AllowLoaderDefer(); |
| 368 StopWhenLoad(); |
| 369 } |
| 370 |
| 371 TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadSameWindow) { |
| 372 Initialize(kHttpUrl, 10, 99); |
| 373 SetLoaderBuffer(10, 20); |
| 374 Start(); |
| 375 PartialResponse(10, 99, 100); |
| 376 |
| 377 uint8 buffer[10]; |
| 378 |
| 379 // Start in undeferred state, disallow defer, receive data but don't shift |
| 380 // buffer window, then allow defer and read. |
| 381 DisallowLoaderDefer(); |
| 382 WriteLoader(10, 10); |
| 383 AllowLoaderDefer(); |
| 384 |
| 385 EXPECT_CALL(*this, ReadCallback(10)); |
| 386 ReadLoader(10, 10, buffer); |
| 387 VerifyBuffer(buffer, 10, 10); |
| 388 StopWhenLoad(); |
| 389 } |
| 390 |
| 391 TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadPastWindow) { |
| 392 Initialize(kHttpUrl, 10, 99); |
| 393 SetLoaderBuffer(10, 20); |
| 394 Start(); |
| 395 PartialResponse(10, 99, 100); |
| 396 |
| 397 uint8 buffer[10]; |
| 398 |
| 399 // Not deferred, disallow defer, received data and shift buffer window, |
| 400 // allow defer, then read in area outside of buffer window. |
| 401 DisallowLoaderDefer(); |
| 402 WriteLoader(10, 10); |
| 403 WriteLoader(20, 50); |
| 404 AllowLoaderDefer(); |
| 405 |
| 406 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 407 ReadLoader(10, 10, buffer); |
| 408 StopWhenLoad(); |
| 409 } |
| 410 |
| 411 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredNoDataReceived) { |
| 412 Initialize(kHttpUrl, 10, 99); |
| 413 SetLoaderBuffer(10, 20); |
| 414 Start(); |
| 415 PartialResponse(10, 99, 100); |
| 416 |
| 417 uint8 buffer[10]; |
| 418 |
| 419 // Start in deferred state, then disallow defer, receive no data, and |
| 420 // allow defer and read. |
| 421 EXPECT_CALL(*url_loader_, setDefersLoading(true)); |
| 422 EXPECT_CALL(*this, NetworkCallback()); |
| 423 WriteLoader(10, 40); |
| 424 |
| 425 DisallowLoaderDefer(); |
| 426 AllowLoaderDefer(); |
| 427 |
| 428 EXPECT_CALL(*this, ReadCallback(10)); |
| 429 ReadLoader(20, 10, buffer); |
| 430 VerifyBuffer(buffer, 20, 10); |
| 431 StopWhenLoad(); |
| 432 } |
| 433 |
| 434 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadSameWindow) { |
| 435 Initialize(kHttpUrl, 10, 99); |
| 436 SetLoaderBuffer(10, 20); |
| 437 Start(); |
| 438 PartialResponse(10, 99, 100); |
| 439 |
| 440 uint8 buffer[10]; |
| 441 |
| 442 // Start in deferred state, disallow defer, receive data and shift buffer |
| 443 // window, allow defer, and read in a place that's still in the window. |
| 444 EXPECT_CALL(*url_loader_, setDefersLoading(true)); |
| 445 EXPECT_CALL(*this, NetworkCallback()); |
| 446 WriteLoader(10, 30); |
| 447 |
| 448 DisallowLoaderDefer(); |
| 449 WriteLoader(40, 5); |
| 450 AllowLoaderDefer(); |
| 451 |
| 452 EXPECT_CALL(*this, ReadCallback(10)); |
| 453 ReadLoader(20, 10, buffer); |
| 454 VerifyBuffer(buffer, 20, 10); |
| 455 StopWhenLoad(); |
| 456 } |
| 457 |
| 458 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) { |
| 459 Initialize(kHttpUrl, 10, 99); |
| 460 SetLoaderBuffer(10, 20); |
| 461 Start(); |
| 462 PartialResponse(10, 99, 100); |
| 463 |
| 464 uint8 buffer[10]; |
| 465 |
| 466 // Start in deferred state, disallow defer, receive data and shift buffer |
| 467 // window, allow defer, and read outside of the buffer window. |
| 468 EXPECT_CALL(*url_loader_, setDefersLoading(true)); |
| 469 EXPECT_CALL(*this, NetworkCallback()); |
| 470 WriteLoader(10, 40); |
| 471 |
| 472 DisallowLoaderDefer(); |
| 473 WriteLoader(50, 20); |
| 474 WriteLoader(70, 40); |
| 475 AllowLoaderDefer(); |
| 476 |
| 477 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); |
| 478 ReadLoader(20, 5, buffer); |
| 479 StopWhenLoad(); |
| 480 } |
| 481 // TODO(hclam): add unit test for defer loading. |
| 482 |
| 483 } // namespace webkit_glue |
| 484 |
OLD | NEW |