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