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