OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "pdf/document_loader.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "pdf/url_loader_wrapper.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/gfx/range/range.h" |
| 15 |
| 16 using ::testing::_; |
| 17 using ::testing::Mock; |
| 18 using ::testing::Sequence; |
| 19 using ::testing::NiceMock; |
| 20 using ::testing::Return; |
| 21 |
| 22 namespace chrome_pdf { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class TestURLLoader : public URLLoaderWrapper { |
| 27 public: |
| 28 class LoaderData { |
| 29 public: |
| 30 LoaderData() {} |
| 31 ~LoaderData() { |
| 32 // We should call callbacks to prevent memory leaks. |
| 33 // The callbacks don't do anything, because the objects that created the |
| 34 // callbacks have been destroyed. |
| 35 if (IsWaitRead()) |
| 36 CallReadCallback(-1); |
| 37 if (IsWaitOpen()) |
| 38 CallOpenCallback(-1); |
| 39 } |
| 40 |
| 41 int content_length() const { return content_length_; } |
| 42 void set_content_length(int content_length) { |
| 43 content_length_ = content_length; |
| 44 } |
| 45 bool accept_ranges_bytes() const { return accept_ranges_bytes_; } |
| 46 void set_accept_ranges_bytes(bool accept_ranges_bytes) { |
| 47 accept_ranges_bytes_ = accept_ranges_bytes; |
| 48 } |
| 49 bool content_encoded() const { return content_encoded_; } |
| 50 void set_content_encoded(bool content_encoded) { |
| 51 content_encoded_ = content_encoded; |
| 52 } |
| 53 const std::string& content_type() const { return content_type_; } |
| 54 void set_content_type(const std::string& content_type) { |
| 55 content_type_ = content_type; |
| 56 } |
| 57 const std::string& content_disposition() const { |
| 58 return content_disposition_; |
| 59 } |
| 60 void set_content_disposition(const std::string& content_disposition) { |
| 61 content_disposition_ = content_disposition; |
| 62 } |
| 63 const std::string& multipart_boundary() const { |
| 64 return multipart_boundary_; |
| 65 } |
| 66 void set_multipart_boundary(const std::string& multipart_boundary) { |
| 67 multipart_boundary_ = multipart_boundary; |
| 68 } |
| 69 const gfx::Range& byte_range() const { return byte_range_; } |
| 70 void set_byte_range(const gfx::Range& byte_range) { |
| 71 byte_range_ = byte_range; |
| 72 } |
| 73 bool is_multipart() const { return is_multipart_; } |
| 74 void set_is_multipart(bool is_multipart) { is_multipart_ = is_multipart; } |
| 75 int status_code() const { return status_code_; } |
| 76 void set_status_code(int status_code) { status_code_ = status_code; } |
| 77 bool closed() const { return closed_; } |
| 78 void set_closed(bool closed) { closed_ = closed; } |
| 79 const gfx::Range& open_byte_range() const { return open_byte_range_; } |
| 80 void set_open_byte_range(const gfx::Range& open_byte_range) { |
| 81 open_byte_range_ = open_byte_range; |
| 82 } |
| 83 |
| 84 bool IsWaitRead() const { return !did_read_callback_.IsOptional(); } |
| 85 bool IsWaitOpen() const { return !did_open_callback_.IsOptional(); } |
| 86 char* buffer() const { return buffer_; } |
| 87 int buffer_size() const { return buffer_size_; } |
| 88 |
| 89 void SetReadCallback(const pp::CompletionCallback& read_callback, |
| 90 char* buffer, |
| 91 int buffer_size) { |
| 92 did_read_callback_ = read_callback; |
| 93 buffer_ = buffer; |
| 94 buffer_size_ = buffer_size; |
| 95 } |
| 96 |
| 97 void SetOpenCallback(const pp::CompletionCallback& open_callback, |
| 98 gfx::Range req_byte_range) { |
| 99 did_open_callback_ = open_callback; |
| 100 set_open_byte_range(req_byte_range); |
| 101 } |
| 102 |
| 103 void CallOpenCallback(int result) { |
| 104 DCHECK(IsWaitOpen()); |
| 105 did_open_callback_.RunAndClear(result); |
| 106 } |
| 107 |
| 108 void CallReadCallback(int result) { |
| 109 DCHECK(IsWaitRead()); |
| 110 did_read_callback_.RunAndClear(result); |
| 111 } |
| 112 |
| 113 private: |
| 114 pp::CompletionCallback did_open_callback_; |
| 115 pp::CompletionCallback did_read_callback_; |
| 116 char* buffer_ = nullptr; |
| 117 int buffer_size_ = 0; |
| 118 |
| 119 int content_length_ = -1; |
| 120 bool accept_ranges_bytes_ = false; |
| 121 bool content_encoded_ = false; |
| 122 std::string content_type_; |
| 123 std::string content_disposition_; |
| 124 std::string multipart_boundary_; |
| 125 gfx::Range byte_range_ = gfx::Range::InvalidRange(); |
| 126 bool is_multipart_ = false; |
| 127 int status_code_ = 0; |
| 128 bool closed_ = true; |
| 129 gfx::Range open_byte_range_ = gfx::Range::InvalidRange(); |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(LoaderData); |
| 132 }; |
| 133 |
| 134 explicit TestURLLoader(LoaderData* data) : data_(data) { |
| 135 data_->set_closed(false); |
| 136 } |
| 137 |
| 138 ~TestURLLoader() override { Close(); } |
| 139 |
| 140 int GetContentLength() const override { return data_->content_length(); } |
| 141 |
| 142 bool IsAcceptRangesBytes() const override { |
| 143 return data_->accept_ranges_bytes(); |
| 144 } |
| 145 |
| 146 bool IsContentEncoded() const override { return data_->content_encoded(); } |
| 147 |
| 148 std::string GetContentType() const override { return data_->content_type(); } |
| 149 |
| 150 std::string GetContentDisposition() const override { |
| 151 return data_->content_disposition(); |
| 152 } |
| 153 |
| 154 int GetStatusCode() const override { return data_->status_code(); } |
| 155 |
| 156 bool IsMultipart() const override { return data_->is_multipart(); } |
| 157 |
| 158 bool GetByteRange(int* start, int* end) const override { |
| 159 *start = data_->byte_range().start(); |
| 160 *end = data_->byte_range().end(); |
| 161 return data_->byte_range().IsValid(); |
| 162 } |
| 163 |
| 164 void Close() override { data_->set_closed(true); } |
| 165 |
| 166 void OpenRange(const std::string& url, |
| 167 const std::string& referrer_url, |
| 168 uint32_t position, |
| 169 uint32_t size, |
| 170 const pp::CompletionCallback& cc) override { |
| 171 data_->SetOpenCallback(cc, gfx::Range(position, position + size)); |
| 172 } |
| 173 |
| 174 void ReadResponseBody(char* buffer, |
| 175 int buffer_size, |
| 176 const pp::CompletionCallback& cc) override { |
| 177 data_->SetReadCallback(cc, buffer, buffer_size); |
| 178 } |
| 179 |
| 180 bool GetDownloadProgress(int64_t* bytes_received, |
| 181 int64_t* total_bytes_to_be_received) const override { |
| 182 return false; |
| 183 } |
| 184 |
| 185 private: |
| 186 LoaderData* data_; |
| 187 |
| 188 DISALLOW_COPY_AND_ASSIGN(TestURLLoader); |
| 189 }; |
| 190 |
| 191 class TestClient : public DocumentLoader::Client { |
| 192 public: |
| 193 TestClient() { full_page_loader_data()->set_content_type("application/pdf"); } |
| 194 ~TestClient() override {} |
| 195 |
| 196 // DocumentLoader::Client overrides: |
| 197 pp::Instance* GetPluginInstance() override { return nullptr; } |
| 198 std::unique_ptr<URLLoaderWrapper> CreateURLLoader() override { |
| 199 return std::unique_ptr<URLLoaderWrapper>( |
| 200 new TestURLLoader(partial_loader_data())); |
| 201 } |
| 202 void OnPendingRequestComplete() override {} |
| 203 void OnNewDataAvailable() override {} |
| 204 void OnDocumentComplete() override {} |
| 205 void OnDocumentCanceled() override {} |
| 206 void CancelBrowserDownload() override {} |
| 207 |
| 208 std::unique_ptr<URLLoaderWrapper> CreateFullPageLoader() { |
| 209 return std::unique_ptr<URLLoaderWrapper>( |
| 210 new TestURLLoader(full_page_loader_data())); |
| 211 } |
| 212 |
| 213 TestURLLoader::LoaderData* full_page_loader_data() { |
| 214 return &full_page_loader_data_; |
| 215 } |
| 216 TestURLLoader::LoaderData* partial_loader_data() { |
| 217 return &partial_loader_data_; |
| 218 } |
| 219 |
| 220 void SetCanUsePartialLoading() { |
| 221 full_page_loader_data()->set_content_length(10 * 1024 * 1024); |
| 222 full_page_loader_data()->set_content_encoded(false); |
| 223 full_page_loader_data()->set_accept_ranges_bytes(true); |
| 224 } |
| 225 |
| 226 void SendAllPartialData() { |
| 227 partial_loader_data_.set_byte_range(partial_loader_data_.open_byte_range()); |
| 228 partial_loader_data_.CallOpenCallback(0); |
| 229 uint32_t length = partial_loader_data_.byte_range().length(); |
| 230 while (length > 0) { |
| 231 const uint32_t max_part_len = DocumentLoader::kDefaultRequestSize; |
| 232 const uint32_t part_len = std::min(length, max_part_len); |
| 233 partial_loader_data_.CallReadCallback(part_len); |
| 234 length -= part_len; |
| 235 } |
| 236 if (partial_loader_data_.IsWaitRead()) { |
| 237 partial_loader_data_.CallReadCallback(0); |
| 238 } |
| 239 } |
| 240 |
| 241 private: |
| 242 TestURLLoader::LoaderData full_page_loader_data_; |
| 243 TestURLLoader::LoaderData partial_loader_data_; |
| 244 |
| 245 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 246 }; |
| 247 |
| 248 class MockClient : public TestClient { |
| 249 public: |
| 250 MockClient() {} |
| 251 |
| 252 MOCK_METHOD0(OnPendingRequestComplete, void()); |
| 253 MOCK_METHOD0(OnNewDataAvailable, void()); |
| 254 MOCK_METHOD0(OnDocumentComplete, void()); |
| 255 MOCK_METHOD0(OnDocumentCanceled, void()); |
| 256 |
| 257 private: |
| 258 DISALLOW_COPY_AND_ASSIGN(MockClient); |
| 259 }; |
| 260 |
| 261 } // namespace |
| 262 |
| 263 using DocumentLoaderTest = ::testing::Test; |
| 264 |
| 265 TEST_F(DocumentLoaderTest, PartialLoadingEnabled) { |
| 266 TestClient client; |
| 267 client.SetCanUsePartialLoading(); |
| 268 DocumentLoader loader(&client); |
| 269 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 270 loader.RequestData(1000000, 1); |
| 271 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 272 // Always send initial data from FullPageLoader. |
| 273 client.full_page_loader_data()->CallReadCallback( |
| 274 DocumentLoader::kDefaultRequestSize); |
| 275 EXPECT_TRUE(loader.is_partial_loader_active()); |
| 276 } |
| 277 |
| 278 TEST_F(DocumentLoaderTest, PartialLoadingDisabledOnSmallFiles) { |
| 279 TestClient client; |
| 280 client.SetCanUsePartialLoading(); |
| 281 client.full_page_loader_data()->set_content_length( |
| 282 DocumentLoader::kDefaultRequestSize * 2); |
| 283 DocumentLoader loader(&client); |
| 284 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 285 loader.RequestData(1000000, 1); |
| 286 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 287 // Always send initial data from FullPageLoader. |
| 288 client.full_page_loader_data()->CallReadCallback( |
| 289 DocumentLoader::kDefaultRequestSize); |
| 290 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 291 } |
| 292 |
| 293 TEST_F(DocumentLoaderTest, PartialLoadingDisabledIfContentEncoded) { |
| 294 TestClient client; |
| 295 client.SetCanUsePartialLoading(); |
| 296 client.full_page_loader_data()->set_content_encoded(true); |
| 297 DocumentLoader loader(&client); |
| 298 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 299 loader.RequestData(1000000, 1); |
| 300 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 301 // Always send initial data from FullPageLoader. |
| 302 client.full_page_loader_data()->CallReadCallback( |
| 303 DocumentLoader::kDefaultRequestSize); |
| 304 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 305 } |
| 306 |
| 307 TEST_F(DocumentLoaderTest, PartialLoadingDisabledNoAcceptRangeBytes) { |
| 308 TestClient client; |
| 309 client.SetCanUsePartialLoading(); |
| 310 client.full_page_loader_data()->set_accept_ranges_bytes(false); |
| 311 DocumentLoader loader(&client); |
| 312 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 313 loader.RequestData(1000000, 1); |
| 314 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 315 // Always send initial data from FullPageLoader. |
| 316 client.full_page_loader_data()->CallReadCallback( |
| 317 DocumentLoader::kDefaultRequestSize); |
| 318 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 319 } |
| 320 |
| 321 TEST_F(DocumentLoaderTest, PartialLoadingReallyDisabledRequestFromBegin) { |
| 322 TestClient client; |
| 323 DocumentLoader loader(&client); |
| 324 client.SetCanUsePartialLoading(); |
| 325 loader.SetPartialLoadingEnabled(false); |
| 326 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 327 // We should not start partial loading if requested data is beside full page |
| 328 // loading position. |
| 329 loader.RequestData(DocumentLoader::kDefaultRequestSize, 1); |
| 330 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 331 // Always send initial data from FullPageLoader. |
| 332 client.full_page_loader_data()->CallReadCallback( |
| 333 DocumentLoader::kDefaultRequestSize); |
| 334 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 335 } |
| 336 |
| 337 TEST_F(DocumentLoaderTest, PartialLoadingReallyDisabledRequestFromMiddle) { |
| 338 TestClient client; |
| 339 client.SetCanUsePartialLoading(); |
| 340 DocumentLoader loader(&client); |
| 341 loader.SetPartialLoadingEnabled(false); |
| 342 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 343 loader.RequestData(1000000, 1); |
| 344 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 345 // Always send initial data from FullPageLoader. |
| 346 client.full_page_loader_data()->CallReadCallback( |
| 347 DocumentLoader::kDefaultRequestSize); |
| 348 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 349 } |
| 350 |
| 351 TEST_F(DocumentLoaderTest, PartialLoadingSimple) { |
| 352 TestClient client; |
| 353 client.SetCanUsePartialLoading(); |
| 354 |
| 355 DocumentLoader loader(&client); |
| 356 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 357 |
| 358 // While we have no requests, we should not start partial loading. |
| 359 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 360 |
| 361 loader.RequestData(5000000, 1); |
| 362 |
| 363 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 364 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 365 |
| 366 // Always send initial data from FullPageLoader. |
| 367 client.full_page_loader_data()->CallReadCallback( |
| 368 DocumentLoader::kDefaultRequestSize); |
| 369 |
| 370 // Partial loader should request headers. |
| 371 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 372 EXPECT_TRUE(loader.is_partial_loader_active()); |
| 373 // Loader should be stopped. |
| 374 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 375 |
| 376 EXPECT_EQ("{4980736,10485760}", |
| 377 client.partial_loader_data()->open_byte_range().ToString()); |
| 378 } |
| 379 |
| 380 TEST_F(DocumentLoaderTest, PartialLoadingBackOrder) { |
| 381 TestClient client; |
| 382 client.SetCanUsePartialLoading(); |
| 383 |
| 384 DocumentLoader loader(&client); |
| 385 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 386 |
| 387 // While we have no requests, we should not start partial loading. |
| 388 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 389 |
| 390 loader.RequestData(client.full_page_loader_data()->content_length() - 1, 1); |
| 391 |
| 392 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 393 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 394 |
| 395 // Always send initial data from FullPageLoader. |
| 396 client.full_page_loader_data()->CallReadCallback( |
| 397 DocumentLoader::kDefaultRequestSize); |
| 398 |
| 399 // Partial loader should request headers. |
| 400 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 401 EXPECT_TRUE(loader.is_partial_loader_active()); |
| 402 // Loader should be stopped. |
| 403 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 404 |
| 405 // Requested range should be enlarged. |
| 406 EXPECT_GT(client.partial_loader_data()->open_byte_range().length(), 1u); |
| 407 EXPECT_EQ("{9830400,10485760}", |
| 408 client.partial_loader_data()->open_byte_range().ToString()); |
| 409 } |
| 410 |
| 411 TEST_F(DocumentLoaderTest, CompleteWithoutPartial) { |
| 412 TestClient client; |
| 413 client.SetCanUsePartialLoading(); |
| 414 DocumentLoader loader(&client); |
| 415 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 416 EXPECT_FALSE(client.full_page_loader_data()->closed()); |
| 417 while (client.full_page_loader_data()->IsWaitRead()) { |
| 418 client.full_page_loader_data()->CallReadCallback(1000); |
| 419 } |
| 420 EXPECT_TRUE(loader.IsDocumentComplete()); |
| 421 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 422 } |
| 423 |
| 424 TEST_F(DocumentLoaderTest, ErrorDownloadFullDocument) { |
| 425 TestClient client; |
| 426 client.SetCanUsePartialLoading(); |
| 427 DocumentLoader loader(&client); |
| 428 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 429 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead()); |
| 430 EXPECT_FALSE(client.full_page_loader_data()->closed()); |
| 431 client.full_page_loader_data()->CallReadCallback(-3); |
| 432 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 433 EXPECT_FALSE(loader.IsDocumentComplete()); |
| 434 } |
| 435 |
| 436 TEST_F(DocumentLoaderTest, CompleteNoContentLength) { |
| 437 TestClient client; |
| 438 DocumentLoader loader(&client); |
| 439 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 440 EXPECT_FALSE(client.full_page_loader_data()->closed()); |
| 441 for (int i = 0; i < 10; ++i) { |
| 442 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead()); |
| 443 client.full_page_loader_data()->CallReadCallback(1000); |
| 444 } |
| 445 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead()); |
| 446 client.full_page_loader_data()->CallReadCallback(0); |
| 447 EXPECT_EQ(10000ul, loader.GetDocumentSize()); |
| 448 EXPECT_TRUE(loader.IsDocumentComplete()); |
| 449 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 450 } |
| 451 |
| 452 TEST_F(DocumentLoaderTest, CompleteWithPartial) { |
| 453 TestClient client; |
| 454 client.SetCanUsePartialLoading(); |
| 455 client.full_page_loader_data()->set_content_length( |
| 456 DocumentLoader::kDefaultRequestSize * 20); |
| 457 DocumentLoader loader(&client); |
| 458 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 459 loader.RequestData(19 * DocumentLoader::kDefaultRequestSize, |
| 460 DocumentLoader::kDefaultRequestSize); |
| 461 EXPECT_FALSE(client.full_page_loader_data()->closed()); |
| 462 EXPECT_FALSE(client.partial_loader_data()->IsWaitRead()); |
| 463 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 464 |
| 465 // Always send initial data from FullPageLoader. |
| 466 client.full_page_loader_data()->CallReadCallback( |
| 467 DocumentLoader::kDefaultRequestSize); |
| 468 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 469 EXPECT_FALSE(client.partial_loader_data()->closed()); |
| 470 |
| 471 client.SendAllPartialData(); |
| 472 // Now we should send other document data. |
| 473 client.SendAllPartialData(); |
| 474 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 475 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 476 } |
| 477 |
| 478 TEST_F(DocumentLoaderTest, PartialRequestLastChunk) { |
| 479 const uint32_t kLastChunkSize = 300; |
| 480 TestClient client; |
| 481 client.SetCanUsePartialLoading(); |
| 482 client.full_page_loader_data()->set_content_length( |
| 483 DocumentLoader::kDefaultRequestSize * 20 + kLastChunkSize); |
| 484 DocumentLoader loader(&client); |
| 485 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 486 loader.RequestData(20 * DocumentLoader::kDefaultRequestSize, 1); |
| 487 |
| 488 // Always send initial data from FullPageLoader. |
| 489 client.full_page_loader_data()->CallReadCallback( |
| 490 DocumentLoader::kDefaultRequestSize); |
| 491 |
| 492 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 493 EXPECT_EQ( |
| 494 static_cast<int>(client.partial_loader_data()->open_byte_range().end()), |
| 495 client.full_page_loader_data()->content_length()); |
| 496 client.partial_loader_data()->set_byte_range( |
| 497 client.partial_loader_data()->open_byte_range()); |
| 498 client.partial_loader_data()->CallOpenCallback(0); |
| 499 uint32_t data_length = client.partial_loader_data()->byte_range().length(); |
| 500 while (data_length > DocumentLoader::kDefaultRequestSize) { |
| 501 client.partial_loader_data()->CallReadCallback( |
| 502 DocumentLoader::kDefaultRequestSize); |
| 503 data_length -= DocumentLoader::kDefaultRequestSize; |
| 504 } |
| 505 EXPECT_EQ(kLastChunkSize, data_length); |
| 506 client.partial_loader_data()->CallReadCallback(kLastChunkSize); |
| 507 EXPECT_TRUE(loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize * 20, |
| 508 kLastChunkSize)); |
| 509 } |
| 510 |
| 511 TEST_F(DocumentLoaderTest, DocumentSize) { |
| 512 TestClient client; |
| 513 client.SetCanUsePartialLoading(); |
| 514 client.full_page_loader_data()->set_content_length(123456789); |
| 515 DocumentLoader loader(&client); |
| 516 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 517 EXPECT_EQ(static_cast<int>(loader.GetDocumentSize()), |
| 518 client.full_page_loader_data()->content_length()); |
| 519 } |
| 520 |
| 521 TEST_F(DocumentLoaderTest, DocumentSizeNoContentLength) { |
| 522 TestClient client; |
| 523 DocumentLoader loader(&client); |
| 524 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 525 EXPECT_EQ(0ul, loader.GetDocumentSize()); |
| 526 client.full_page_loader_data()->CallReadCallback( |
| 527 DocumentLoader::kDefaultRequestSize); |
| 528 client.full_page_loader_data()->CallReadCallback(1000); |
| 529 client.full_page_loader_data()->CallReadCallback(500); |
| 530 client.full_page_loader_data()->CallReadCallback(0); |
| 531 EXPECT_EQ(DocumentLoader::kDefaultRequestSize + 1000ul + 500ul, |
| 532 loader.GetDocumentSize()); |
| 533 EXPECT_TRUE(loader.IsDocumentComplete()); |
| 534 } |
| 535 |
| 536 TEST_F(DocumentLoaderTest, ClearPendingRequests) { |
| 537 TestClient client; |
| 538 client.SetCanUsePartialLoading(); |
| 539 client.full_page_loader_data()->set_content_length( |
| 540 DocumentLoader::kDefaultRequestSize * 100 + 58383); |
| 541 DocumentLoader loader(&client); |
| 542 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 543 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10); |
| 544 loader.ClearPendingRequests(); |
| 545 loader.RequestData(15 * DocumentLoader::kDefaultRequestSize + 200, 20); |
| 546 // pending requests are accumulating, and will be processed after initial data |
| 547 // load. |
| 548 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 549 |
| 550 // Send initial data from FullPageLoader. |
| 551 client.full_page_loader_data()->CallReadCallback( |
| 552 DocumentLoader::kDefaultRequestSize); |
| 553 |
| 554 { |
| 555 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 556 const gfx::Range range_requested(15 * DocumentLoader::kDefaultRequestSize, |
| 557 16 * DocumentLoader::kDefaultRequestSize); |
| 558 EXPECT_EQ(range_requested.start(), |
| 559 client.partial_loader_data()->open_byte_range().start()); |
| 560 EXPECT_LE(range_requested.end(), |
| 561 client.partial_loader_data()->open_byte_range().end()); |
| 562 client.partial_loader_data()->set_byte_range( |
| 563 client.partial_loader_data()->open_byte_range()); |
| 564 } |
| 565 // clear requests before Open callback. |
| 566 loader.ClearPendingRequests(); |
| 567 // Current request should continue loading. |
| 568 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 569 client.partial_loader_data()->CallOpenCallback(0); |
| 570 client.partial_loader_data()->CallReadCallback( |
| 571 DocumentLoader::kDefaultRequestSize); |
| 572 EXPECT_FALSE(client.partial_loader_data()->closed()); |
| 573 // Current request should continue loading, because no other request queued. |
| 574 |
| 575 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 20); |
| 576 // Requests queue is processed only on receiving data. |
| 577 client.partial_loader_data()->CallReadCallback( |
| 578 DocumentLoader::kDefaultRequestSize); |
| 579 // New request within close distance from the one currently loading. Loading |
| 580 // isn't restarted. |
| 581 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 582 |
| 583 loader.ClearPendingRequests(); |
| 584 // request again two. |
| 585 loader.RequestData(60 * DocumentLoader::kDefaultRequestSize + 100, 10); |
| 586 loader.RequestData(35 * DocumentLoader::kDefaultRequestSize + 200, 20); |
| 587 // Requests queue is processed only on receiving data. |
| 588 client.partial_loader_data()->CallReadCallback( |
| 589 DocumentLoader::kDefaultRequestSize); |
| 590 { |
| 591 // new requset not with in close distance from current loading. |
| 592 // Loading should be restarted. |
| 593 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 594 // The first requested chunk should be processed. |
| 595 const gfx::Range range_requested(35 * DocumentLoader::kDefaultRequestSize, |
| 596 36 * DocumentLoader::kDefaultRequestSize); |
| 597 EXPECT_EQ(range_requested.start(), |
| 598 client.partial_loader_data()->open_byte_range().start()); |
| 599 EXPECT_LE(range_requested.end(), |
| 600 client.partial_loader_data()->open_byte_range().end()); |
| 601 client.partial_loader_data()->set_byte_range( |
| 602 client.partial_loader_data()->open_byte_range()); |
| 603 } |
| 604 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 605 client.partial_loader_data()->CallOpenCallback(0); |
| 606 // Override pending requests. |
| 607 loader.ClearPendingRequests(); |
| 608 loader.RequestData(70 * DocumentLoader::kDefaultRequestSize + 100, 10); |
| 609 |
| 610 // Requests queue is processed only on receiving data. |
| 611 client.partial_loader_data()->CallReadCallback( |
| 612 DocumentLoader::kDefaultRequestSize); |
| 613 { |
| 614 // New requset not with in close distance from current loading. |
| 615 // Loading should be restarted . |
| 616 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 617 // The first requested chunk should be processed. |
| 618 const gfx::Range range_requested(70 * DocumentLoader::kDefaultRequestSize, |
| 619 71 * DocumentLoader::kDefaultRequestSize); |
| 620 EXPECT_EQ(range_requested.start(), |
| 621 client.partial_loader_data()->open_byte_range().start()); |
| 622 EXPECT_LE(range_requested.end(), |
| 623 client.partial_loader_data()->open_byte_range().end()); |
| 624 client.partial_loader_data()->set_byte_range( |
| 625 client.partial_loader_data()->open_byte_range()); |
| 626 } |
| 627 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 628 } |
| 629 |
| 630 TEST_F(DocumentLoaderTest, GetBlock) { |
| 631 std::vector<char> buffer(DocumentLoader::kDefaultRequestSize); |
| 632 TestClient client; |
| 633 client.SetCanUsePartialLoading(); |
| 634 client.full_page_loader_data()->set_content_length( |
| 635 DocumentLoader::kDefaultRequestSize * 20 + 58383); |
| 636 DocumentLoader loader(&client); |
| 637 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 638 EXPECT_FALSE(loader.GetBlock(0, 1000, buffer.data())); |
| 639 client.full_page_loader_data()->CallReadCallback( |
| 640 DocumentLoader::kDefaultRequestSize); |
| 641 EXPECT_TRUE(loader.GetBlock(0, 1000, buffer.data())); |
| 642 EXPECT_FALSE(loader.GetBlock(DocumentLoader::kDefaultRequestSize, 1500, |
| 643 buffer.data())); |
| 644 client.full_page_loader_data()->CallReadCallback( |
| 645 DocumentLoader::kDefaultRequestSize); |
| 646 EXPECT_TRUE(loader.GetBlock(DocumentLoader::kDefaultRequestSize, 1500, |
| 647 buffer.data())); |
| 648 |
| 649 EXPECT_FALSE(loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, |
| 650 buffer.data())); |
| 651 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10); |
| 652 EXPECT_FALSE(loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, |
| 653 buffer.data())); |
| 654 |
| 655 // Requests queue is processed only on receiving data. |
| 656 client.full_page_loader_data()->CallReadCallback( |
| 657 DocumentLoader::kDefaultRequestSize); |
| 658 |
| 659 client.SendAllPartialData(); |
| 660 EXPECT_TRUE(loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, |
| 661 buffer.data())); |
| 662 } |
| 663 |
| 664 TEST_F(DocumentLoaderTest, IsDataAvailable) { |
| 665 TestClient client; |
| 666 client.SetCanUsePartialLoading(); |
| 667 client.full_page_loader_data()->set_content_length( |
| 668 DocumentLoader::kDefaultRequestSize * 20 + 58383); |
| 669 DocumentLoader loader(&client); |
| 670 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 671 EXPECT_FALSE(loader.IsDataAvailable(0, 1000)); |
| 672 client.full_page_loader_data()->CallReadCallback( |
| 673 DocumentLoader::kDefaultRequestSize); |
| 674 EXPECT_TRUE(loader.IsDataAvailable(0, 1000)); |
| 675 EXPECT_FALSE( |
| 676 loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize, 1500)); |
| 677 client.full_page_loader_data()->CallReadCallback( |
| 678 DocumentLoader::kDefaultRequestSize); |
| 679 EXPECT_TRUE( |
| 680 loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize, 1500)); |
| 681 |
| 682 EXPECT_FALSE( |
| 683 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000)); |
| 684 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10); |
| 685 EXPECT_FALSE( |
| 686 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000)); |
| 687 |
| 688 // Requests queue is processed only on receiving data. |
| 689 client.full_page_loader_data()->CallReadCallback( |
| 690 DocumentLoader::kDefaultRequestSize); |
| 691 |
| 692 client.SendAllPartialData(); |
| 693 EXPECT_TRUE( |
| 694 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000)); |
| 695 } |
| 696 |
| 697 TEST_F(DocumentLoaderTest, RequestData) { |
| 698 TestClient client; |
| 699 client.SetCanUsePartialLoading(); |
| 700 client.full_page_loader_data()->set_content_length( |
| 701 DocumentLoader::kDefaultRequestSize * 100 + 58383); |
| 702 DocumentLoader loader(&client); |
| 703 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 704 loader.RequestData(37 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 705 loader.RequestData(25 * DocumentLoader::kDefaultRequestSize + 600, 100); |
| 706 loader.RequestData(13 * DocumentLoader::kDefaultRequestSize + 900, 500); |
| 707 |
| 708 // Send initial data from FullPageLoader. |
| 709 client.full_page_loader_data()->CallReadCallback( |
| 710 DocumentLoader::kDefaultRequestSize); |
| 711 |
| 712 { |
| 713 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 714 const gfx::Range range_requested(13 * DocumentLoader::kDefaultRequestSize, |
| 715 14 * DocumentLoader::kDefaultRequestSize); |
| 716 EXPECT_EQ(range_requested.start(), |
| 717 client.partial_loader_data()->open_byte_range().start()); |
| 718 EXPECT_LE(range_requested.end(), |
| 719 client.partial_loader_data()->open_byte_range().end()); |
| 720 client.partial_loader_data()->set_byte_range( |
| 721 client.partial_loader_data()->open_byte_range()); |
| 722 } |
| 723 client.partial_loader_data()->CallOpenCallback(0); |
| 724 // Override pending requests. |
| 725 loader.ClearPendingRequests(); |
| 726 loader.RequestData(38 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 727 loader.RequestData(26 * DocumentLoader::kDefaultRequestSize + 600, 100); |
| 728 // Requests queue is processed only on receiving data. |
| 729 client.partial_loader_data()->CallReadCallback( |
| 730 DocumentLoader::kDefaultRequestSize); |
| 731 { |
| 732 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 733 const gfx::Range range_requested(26 * DocumentLoader::kDefaultRequestSize, |
| 734 27 * DocumentLoader::kDefaultRequestSize); |
| 735 EXPECT_EQ(range_requested.start(), |
| 736 client.partial_loader_data()->open_byte_range().start()); |
| 737 EXPECT_LE(range_requested.end(), |
| 738 client.partial_loader_data()->open_byte_range().end()); |
| 739 client.partial_loader_data()->set_byte_range( |
| 740 client.partial_loader_data()->open_byte_range()); |
| 741 } |
| 742 client.partial_loader_data()->CallOpenCallback(0); |
| 743 // Override pending requests. |
| 744 loader.ClearPendingRequests(); |
| 745 loader.RequestData(39 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 746 // Requests queue is processed only on receiving data. |
| 747 client.partial_loader_data()->CallReadCallback( |
| 748 DocumentLoader::kDefaultRequestSize); |
| 749 { |
| 750 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 751 const gfx::Range range_requested(39 * DocumentLoader::kDefaultRequestSize, |
| 752 40 * DocumentLoader::kDefaultRequestSize); |
| 753 EXPECT_EQ(range_requested.start(), |
| 754 client.partial_loader_data()->open_byte_range().start()); |
| 755 EXPECT_LE(range_requested.end(), |
| 756 client.partial_loader_data()->open_byte_range().end()); |
| 757 client.partial_loader_data()->set_byte_range( |
| 758 client.partial_loader_data()->open_byte_range()); |
| 759 } |
| 760 // Fill all gaps. |
| 761 while (!loader.IsDocumentComplete()) { |
| 762 client.SendAllPartialData(); |
| 763 } |
| 764 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 765 } |
| 766 |
| 767 TEST_F(DocumentLoaderTest, DoNotLoadAvailablePartialData) { |
| 768 TestClient client; |
| 769 client.SetCanUsePartialLoading(); |
| 770 client.full_page_loader_data()->set_content_length( |
| 771 DocumentLoader::kDefaultRequestSize * 20 + 58383); |
| 772 DocumentLoader loader(&client); |
| 773 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 774 // Send initial data from FullPageLoader. |
| 775 client.full_page_loader_data()->CallReadCallback( |
| 776 DocumentLoader::kDefaultRequestSize); |
| 777 |
| 778 // Send more data from FullPageLoader. |
| 779 client.full_page_loader_data()->CallReadCallback( |
| 780 DocumentLoader::kDefaultRequestSize); |
| 781 |
| 782 loader.RequestData(2 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 783 |
| 784 // Send more data from FullPageLoader. |
| 785 client.full_page_loader_data()->CallReadCallback( |
| 786 DocumentLoader::kDefaultRequestSize); |
| 787 |
| 788 // Partial loading should not have started for already available data. |
| 789 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 790 } |
| 791 |
| 792 TEST_F(DocumentLoaderTest, DoNotLoadDataAfterComplete) { |
| 793 TestClient client; |
| 794 client.SetCanUsePartialLoading(); |
| 795 client.full_page_loader_data()->set_content_length( |
| 796 DocumentLoader::kDefaultRequestSize * 20); |
| 797 DocumentLoader loader(&client); |
| 798 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 799 |
| 800 for (int i = 0; i < 20; ++i) { |
| 801 client.full_page_loader_data()->CallReadCallback( |
| 802 DocumentLoader::kDefaultRequestSize); |
| 803 } |
| 804 |
| 805 EXPECT_TRUE(loader.IsDocumentComplete()); |
| 806 |
| 807 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 808 |
| 809 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 810 EXPECT_TRUE(client.full_page_loader_data()->closed()); |
| 811 } |
| 812 |
| 813 TEST_F(DocumentLoaderTest, DoNotLoadPartialDataAboveDocumentSize) { |
| 814 TestClient client; |
| 815 client.SetCanUsePartialLoading(); |
| 816 client.full_page_loader_data()->set_content_length( |
| 817 DocumentLoader::kDefaultRequestSize * 20); |
| 818 DocumentLoader loader(&client); |
| 819 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 820 |
| 821 loader.RequestData(20 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 822 |
| 823 // Send initial data from FullPageLoader. |
| 824 client.full_page_loader_data()->CallReadCallback( |
| 825 DocumentLoader::kDefaultRequestSize); |
| 826 |
| 827 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 828 } |
| 829 |
| 830 TEST_F(DocumentLoaderTest, MergePendingRequests) { |
| 831 TestClient client; |
| 832 client.SetCanUsePartialLoading(); |
| 833 client.full_page_loader_data()->set_content_length( |
| 834 DocumentLoader::kDefaultRequestSize * 50 + 58383); |
| 835 DocumentLoader loader(&client); |
| 836 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 837 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 838 loader.RequestData(16 * DocumentLoader::kDefaultRequestSize + 600, 100); |
| 839 |
| 840 // Send initial data from FullPageLoader. |
| 841 client.full_page_loader_data()->CallReadCallback( |
| 842 DocumentLoader::kDefaultRequestSize); |
| 843 |
| 844 const gfx::Range range_requested(16 * DocumentLoader::kDefaultRequestSize, |
| 845 18 * DocumentLoader::kDefaultRequestSize); |
| 846 EXPECT_EQ(range_requested.start(), |
| 847 client.partial_loader_data()->open_byte_range().start()); |
| 848 EXPECT_LE(range_requested.end(), |
| 849 client.partial_loader_data()->open_byte_range().end()); |
| 850 |
| 851 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 852 |
| 853 // Fill all gaps. |
| 854 while (!loader.IsDocumentComplete()) { |
| 855 client.SendAllPartialData(); |
| 856 } |
| 857 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 858 } |
| 859 |
| 860 TEST_F(DocumentLoaderTest, PartialStopOnStatusCodeError) { |
| 861 TestClient client; |
| 862 client.SetCanUsePartialLoading(); |
| 863 client.full_page_loader_data()->set_content_length( |
| 864 DocumentLoader::kDefaultRequestSize * 20); |
| 865 DocumentLoader loader(&client); |
| 866 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 867 |
| 868 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 869 |
| 870 // Send initial data from FullPageLoader. |
| 871 client.full_page_loader_data()->CallReadCallback( |
| 872 DocumentLoader::kDefaultRequestSize); |
| 873 |
| 874 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 875 client.partial_loader_data()->set_status_code(404); |
| 876 client.partial_loader_data()->CallOpenCallback(0); |
| 877 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 878 } |
| 879 |
| 880 TEST_F(DocumentLoaderTest, |
| 881 PartialAsFullDocumentLoadingRangeRequestNoRangeField) { |
| 882 TestClient client; |
| 883 client.SetCanUsePartialLoading(); |
| 884 client.full_page_loader_data()->set_content_length( |
| 885 DocumentLoader::kDefaultRequestSize * 20); |
| 886 DocumentLoader loader(&client); |
| 887 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 888 |
| 889 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 890 |
| 891 // Send initial data from FullPageLoader. |
| 892 client.full_page_loader_data()->CallReadCallback( |
| 893 DocumentLoader::kDefaultRequestSize); |
| 894 |
| 895 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 896 client.partial_loader_data()->set_byte_range(gfx::Range::InvalidRange()); |
| 897 client.partial_loader_data()->CallOpenCallback(0); |
| 898 EXPECT_FALSE(client.partial_loader_data()->closed()); |
| 899 // Partial loader is used to load the whole page, like full page loader. |
| 900 EXPECT_FALSE(loader.is_partial_loader_active()); |
| 901 } |
| 902 |
| 903 TEST_F(DocumentLoaderTest, PartialMultiPart) { |
| 904 TestClient client; |
| 905 client.SetCanUsePartialLoading(); |
| 906 client.full_page_loader_data()->set_content_length( |
| 907 DocumentLoader::kDefaultRequestSize * 20); |
| 908 DocumentLoader loader(&client); |
| 909 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 910 |
| 911 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 912 |
| 913 // Send initial data from FullPageLoader. |
| 914 client.full_page_loader_data()->CallReadCallback( |
| 915 DocumentLoader::kDefaultRequestSize); |
| 916 |
| 917 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 918 client.partial_loader_data()->set_is_multipart(true); |
| 919 client.partial_loader_data()->CallOpenCallback(0); |
| 920 client.partial_loader_data()->set_byte_range( |
| 921 gfx::Range(17 * DocumentLoader::kDefaultRequestSize, |
| 922 18 * DocumentLoader::kDefaultRequestSize)); |
| 923 client.partial_loader_data()->CallReadCallback( |
| 924 DocumentLoader::kDefaultRequestSize); |
| 925 EXPECT_TRUE(loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, |
| 926 DocumentLoader::kDefaultRequestSize)); |
| 927 } |
| 928 |
| 929 TEST_F(DocumentLoaderTest, PartialMultiPartRangeError) { |
| 930 TestClient client; |
| 931 client.SetCanUsePartialLoading(); |
| 932 client.full_page_loader_data()->set_content_length( |
| 933 DocumentLoader::kDefaultRequestSize * 20); |
| 934 DocumentLoader loader(&client); |
| 935 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 936 |
| 937 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 938 |
| 939 // Send initial data from FullPageLoader. |
| 940 client.full_page_loader_data()->CallReadCallback( |
| 941 DocumentLoader::kDefaultRequestSize); |
| 942 |
| 943 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 944 client.partial_loader_data()->set_is_multipart(true); |
| 945 client.partial_loader_data()->CallOpenCallback(0); |
| 946 client.partial_loader_data()->set_byte_range(gfx::Range::InvalidRange()); |
| 947 client.partial_loader_data()->CallReadCallback( |
| 948 DocumentLoader::kDefaultRequestSize); |
| 949 EXPECT_FALSE(loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, |
| 950 DocumentLoader::kDefaultRequestSize)); |
| 951 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 952 } |
| 953 |
| 954 TEST_F(DocumentLoaderTest, PartialConnectionErrorOnOpen) { |
| 955 TestClient client; |
| 956 client.SetCanUsePartialLoading(); |
| 957 client.full_page_loader_data()->set_content_length( |
| 958 DocumentLoader::kDefaultRequestSize * 20); |
| 959 DocumentLoader loader(&client); |
| 960 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 961 |
| 962 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 963 |
| 964 // Send initial data from FullPageLoader. |
| 965 client.full_page_loader_data()->CallReadCallback( |
| 966 DocumentLoader::kDefaultRequestSize); |
| 967 |
| 968 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 969 client.partial_loader_data()->CallOpenCallback(-3); |
| 970 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 971 |
| 972 // Partial loading should not restart after any error. |
| 973 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 974 |
| 975 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 976 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 977 } |
| 978 |
| 979 TEST_F(DocumentLoaderTest, PartialConnectionErrorOnRead) { |
| 980 TestClient client; |
| 981 client.SetCanUsePartialLoading(); |
| 982 client.full_page_loader_data()->set_content_length( |
| 983 DocumentLoader::kDefaultRequestSize * 20); |
| 984 DocumentLoader loader(&client); |
| 985 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 986 |
| 987 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 988 |
| 989 // Send initial data from FullPageLoader. |
| 990 client.full_page_loader_data()->CallReadCallback( |
| 991 DocumentLoader::kDefaultRequestSize); |
| 992 |
| 993 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen()); |
| 994 client.partial_loader_data()->set_byte_range( |
| 995 gfx::Range(17 * DocumentLoader::kDefaultRequestSize, |
| 996 18 * DocumentLoader::kDefaultRequestSize)); |
| 997 client.partial_loader_data()->CallOpenCallback(0); |
| 998 EXPECT_TRUE(client.partial_loader_data()->IsWaitRead()); |
| 999 client.partial_loader_data()->CallReadCallback(-3); |
| 1000 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 1001 |
| 1002 // Partial loading should not restart after any error. |
| 1003 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 10); |
| 1004 |
| 1005 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen()); |
| 1006 EXPECT_TRUE(client.partial_loader_data()->closed()); |
| 1007 } |
| 1008 |
| 1009 TEST_F(DocumentLoaderTest, GetProgress) { |
| 1010 TestClient client; |
| 1011 client.SetCanUsePartialLoading(); |
| 1012 client.full_page_loader_data()->set_content_length( |
| 1013 DocumentLoader::kDefaultRequestSize * 20); |
| 1014 DocumentLoader loader(&client); |
| 1015 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1016 EXPECT_EQ(0., loader.GetProgress()); |
| 1017 |
| 1018 for (int i = 0; i < 20; ++i) { |
| 1019 EXPECT_EQ(i * 100 / 20, static_cast<int>(loader.GetProgress() * 100)); |
| 1020 client.full_page_loader_data()->CallReadCallback( |
| 1021 DocumentLoader::kDefaultRequestSize); |
| 1022 } |
| 1023 EXPECT_EQ(1., loader.GetProgress()); |
| 1024 } |
| 1025 |
| 1026 TEST_F(DocumentLoaderTest, GetProgressNoContentLength) { |
| 1027 TestClient client; |
| 1028 DocumentLoader loader(&client); |
| 1029 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1030 EXPECT_EQ(-1., loader.GetProgress()); |
| 1031 |
| 1032 for (int i = 0; i < 20; ++i) { |
| 1033 EXPECT_EQ(-1., loader.GetProgress()); |
| 1034 client.full_page_loader_data()->CallReadCallback( |
| 1035 DocumentLoader::kDefaultRequestSize); |
| 1036 } |
| 1037 client.full_page_loader_data()->CallReadCallback(0); |
| 1038 EXPECT_EQ(1., loader.GetProgress()); |
| 1039 } |
| 1040 |
| 1041 TEST_F(DocumentLoaderTest, ClientCompleteCallbacks) { |
| 1042 MockClient client; |
| 1043 client.SetCanUsePartialLoading(); |
| 1044 client.full_page_loader_data()->set_content_length( |
| 1045 DocumentLoader::kDefaultRequestSize * 20); |
| 1046 DocumentLoader loader(&client); |
| 1047 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1048 |
| 1049 EXPECT_CALL(client, OnDocumentComplete()).Times(0); |
| 1050 for (int i = 0; i < 19; ++i) { |
| 1051 client.full_page_loader_data()->CallReadCallback( |
| 1052 DocumentLoader::kDefaultRequestSize); |
| 1053 } |
| 1054 Mock::VerifyAndClear(&client); |
| 1055 |
| 1056 EXPECT_CALL(client, OnDocumentComplete()).Times(1); |
| 1057 client.full_page_loader_data()->CallReadCallback( |
| 1058 DocumentLoader::kDefaultRequestSize); |
| 1059 Mock::VerifyAndClear(&client); |
| 1060 } |
| 1061 |
| 1062 TEST_F(DocumentLoaderTest, ClientCompleteCallbacksNoContentLength) { |
| 1063 MockClient client; |
| 1064 DocumentLoader loader(&client); |
| 1065 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1066 |
| 1067 EXPECT_CALL(client, OnDocumentCanceled()).Times(0); |
| 1068 EXPECT_CALL(client, OnDocumentComplete()).Times(0); |
| 1069 for (int i = 0; i < 20; ++i) { |
| 1070 client.full_page_loader_data()->CallReadCallback( |
| 1071 DocumentLoader::kDefaultRequestSize); |
| 1072 } |
| 1073 Mock::VerifyAndClear(&client); |
| 1074 |
| 1075 EXPECT_CALL(client, OnDocumentCanceled()).Times(0); |
| 1076 EXPECT_CALL(client, OnDocumentComplete()).Times(1); |
| 1077 client.full_page_loader_data()->CallReadCallback(0); |
| 1078 Mock::VerifyAndClear(&client); |
| 1079 } |
| 1080 |
| 1081 TEST_F(DocumentLoaderTest, ClientCancelCallback) { |
| 1082 MockClient client; |
| 1083 client.SetCanUsePartialLoading(); |
| 1084 client.full_page_loader_data()->set_content_length( |
| 1085 DocumentLoader::kDefaultRequestSize * 20); |
| 1086 DocumentLoader loader(&client); |
| 1087 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1088 |
| 1089 EXPECT_CALL(client, OnDocumentCanceled()).Times(0); |
| 1090 EXPECT_CALL(client, OnDocumentComplete()).Times(0); |
| 1091 for (int i = 0; i < 10; ++i) { |
| 1092 client.full_page_loader_data()->CallReadCallback( |
| 1093 DocumentLoader::kDefaultRequestSize); |
| 1094 } |
| 1095 Mock::VerifyAndClear(&client); |
| 1096 |
| 1097 EXPECT_CALL(client, OnDocumentComplete()).Times(0); |
| 1098 EXPECT_CALL(client, OnDocumentCanceled()).Times(1); |
| 1099 client.full_page_loader_data()->CallReadCallback(-3); |
| 1100 Mock::VerifyAndClear(&client); |
| 1101 } |
| 1102 |
| 1103 TEST_F(DocumentLoaderTest, NewDataAvailable) { |
| 1104 MockClient client; |
| 1105 client.SetCanUsePartialLoading(); |
| 1106 client.full_page_loader_data()->set_content_length( |
| 1107 DocumentLoader::kDefaultRequestSize * 20); |
| 1108 DocumentLoader loader(&client); |
| 1109 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1110 |
| 1111 EXPECT_CALL(client, OnNewDataAvailable()).Times(1); |
| 1112 client.full_page_loader_data()->CallReadCallback( |
| 1113 DocumentLoader::kDefaultRequestSize); |
| 1114 Mock::VerifyAndClear(&client); |
| 1115 |
| 1116 EXPECT_CALL(client, OnNewDataAvailable()).Times(0); |
| 1117 client.full_page_loader_data()->CallReadCallback( |
| 1118 DocumentLoader::kDefaultRequestSize - 100); |
| 1119 Mock::VerifyAndClear(&client); |
| 1120 |
| 1121 EXPECT_CALL(client, OnNewDataAvailable()).Times(1); |
| 1122 client.full_page_loader_data()->CallReadCallback(100); |
| 1123 Mock::VerifyAndClear(&client); |
| 1124 } |
| 1125 |
| 1126 TEST_F(DocumentLoaderTest, ClientPendingRequestCompleteFullLoader) { |
| 1127 MockClient client; |
| 1128 client.SetCanUsePartialLoading(); |
| 1129 DocumentLoader loader(&client); |
| 1130 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1131 |
| 1132 loader.RequestData(1000, 4000); |
| 1133 |
| 1134 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1); |
| 1135 client.full_page_loader_data()->CallReadCallback( |
| 1136 DocumentLoader::kDefaultRequestSize); |
| 1137 Mock::VerifyAndClear(&client); |
| 1138 } |
| 1139 |
| 1140 TEST_F(DocumentLoaderTest, ClientPendingRequestCompletePartialLoader) { |
| 1141 MockClient client; |
| 1142 client.SetCanUsePartialLoading(); |
| 1143 DocumentLoader loader(&client); |
| 1144 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1145 |
| 1146 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1); |
| 1147 loader.RequestData(15 * DocumentLoader::kDefaultRequestSize + 4000, 4000); |
| 1148 |
| 1149 // Always send initial data from FullPageLoader. |
| 1150 client.full_page_loader_data()->CallReadCallback( |
| 1151 DocumentLoader::kDefaultRequestSize); |
| 1152 |
| 1153 client.SendAllPartialData(); |
| 1154 Mock::VerifyAndClear(&client); |
| 1155 } |
| 1156 |
| 1157 TEST_F(DocumentLoaderTest, ClientPendingRequestCompletePartialAndFullLoader) { |
| 1158 MockClient client; |
| 1159 client.SetCanUsePartialLoading(); |
| 1160 DocumentLoader loader(&client); |
| 1161 loader.Init(client.CreateFullPageLoader(), "http://url.com"); |
| 1162 |
| 1163 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1); |
| 1164 loader.RequestData(16 * DocumentLoader::kDefaultRequestSize + 4000, 4000); |
| 1165 loader.RequestData(4 * DocumentLoader::kDefaultRequestSize + 4000, 4000); |
| 1166 |
| 1167 for (int i = 0; i < 5; ++i) { |
| 1168 client.full_page_loader_data()->CallReadCallback( |
| 1169 DocumentLoader::kDefaultRequestSize); |
| 1170 } |
| 1171 |
| 1172 Mock::VerifyAndClear(&client); |
| 1173 |
| 1174 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1); |
| 1175 client.SendAllPartialData(); |
| 1176 Mock::VerifyAndClear(&client); |
| 1177 } |
| 1178 |
| 1179 } // namespace chrome_pdf |
OLD | NEW |