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

Side by Side Diff: pdf/document_loader_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698