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

Side by Side Diff: pdf/document_loader_unittest.cc

Issue 2349753003: Improve linearized pdf load/show time. (Closed)
Patch Set: fix review issues. 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 } // namespace
Lei Zhang 2016/10/25 18:24:52 nit: blank line above this please.
snake 2016/10/25 19:16:27 Done.
252
253 class DocumentLoaderTest : public ::testing::Test {
254 public:
255 DocumentLoaderTest() {}
256 ~DocumentLoaderTest() override {}
257 void SetUp() override {}
258 void TearDown() override {}
259 };
260
261 TEST_F(DocumentLoaderTest, PartialLoadingEnabled) {
262 TestClient client;
263 client.SetCanUsePartialLoading();
264 DocumentLoader loader(&client);
265 loader.Init(client.CreateFullPageLoader(), "http://url.com");
266 loader.RequestData(1000000, 1);
267 EXPECT_FALSE(loader.is_partial_loader_active());
268 // Always send initial data from FullPageLoader.
269 client.full_page_loader_data()->CallReadCallback(
270 DocumentLoader::kDefaultRequestSize);
271 EXPECT_TRUE(loader.is_partial_loader_active());
272 }
273
274 TEST_F(DocumentLoaderTest, PartialLoadingDisabledOnSmallFiles) {
275 TestClient client;
276 client.SetCanUsePartialLoading();
277 client.full_page_loader_data()->set_content_length(
278 DocumentLoader::kDefaultRequestSize * 2);
279 DocumentLoader loader(&client);
280 loader.Init(client.CreateFullPageLoader(), "http://url.com");
281 loader.RequestData(1000000, 1);
282 EXPECT_FALSE(loader.is_partial_loader_active());
283 // Always send initial data from FullPageLoader.
284 client.full_page_loader_data()->CallReadCallback(
285 DocumentLoader::kDefaultRequestSize);
286 EXPECT_FALSE(loader.is_partial_loader_active());
287 }
288
289 TEST_F(DocumentLoaderTest, PartialLoadingDisabledIfContentEncoded) {
290 TestClient client;
291 client.SetCanUsePartialLoading();
292 client.full_page_loader_data()->set_content_encoded(true);
293 DocumentLoader loader(&client);
294 loader.Init(client.CreateFullPageLoader(), "http://url.com");
295 loader.RequestData(1000000, 1);
296 EXPECT_FALSE(loader.is_partial_loader_active());
297 // Always send initial data from FullPageLoader.
298 client.full_page_loader_data()->CallReadCallback(
299 DocumentLoader::kDefaultRequestSize);
300 EXPECT_FALSE(loader.is_partial_loader_active());
301 }
302
303 TEST_F(DocumentLoaderTest, PartialLoadingDisabledNoAcceptRangeBytes) {
304 TestClient client;
305 client.SetCanUsePartialLoading();
306 client.full_page_loader_data()->set_accept_ranges_bytes(false);
307 DocumentLoader loader(&client);
308 loader.Init(client.CreateFullPageLoader(), "http://url.com");
309 loader.RequestData(1000000, 1);
310 EXPECT_FALSE(loader.is_partial_loader_active());
311 // Always send initial data from FullPageLoader.
312 client.full_page_loader_data()->CallReadCallback(
313 DocumentLoader::kDefaultRequestSize);
314 EXPECT_FALSE(loader.is_partial_loader_active());
315 }
316
317 TEST_F(DocumentLoaderTest, PartialLoadingReallyDisabledRequestFromBegin) {
318 TestClient client;
319 DocumentLoader loader(&client);
320 client.SetCanUsePartialLoading();
321 loader.SetPartialLoadingEnabled(false);
322 loader.Init(client.CreateFullPageLoader(), "http://url.com");
323 // We should not start partial loading if requested data is beside full page
324 // loading position.
325 loader.RequestData(DocumentLoader::kDefaultRequestSize, 1);
326 EXPECT_FALSE(loader.is_partial_loader_active());
327 // Always send initial data from FullPageLoader.
328 client.full_page_loader_data()->CallReadCallback(
329 DocumentLoader::kDefaultRequestSize);
330 EXPECT_FALSE(loader.is_partial_loader_active());
331 }
332
333 TEST_F(DocumentLoaderTest, PartialLoadingReallyDisabledRequestFromMiddle) {
334 TestClient client;
335 client.SetCanUsePartialLoading();
336 DocumentLoader loader(&client);
337 loader.SetPartialLoadingEnabled(false);
338 loader.Init(client.CreateFullPageLoader(), "http://url.com");
339 loader.RequestData(1000000, 1);
340 EXPECT_FALSE(loader.is_partial_loader_active());
341 // Always send initial data from FullPageLoader.
342 client.full_page_loader_data()->CallReadCallback(
343 DocumentLoader::kDefaultRequestSize);
344 EXPECT_FALSE(loader.is_partial_loader_active());
345 }
346
347 TEST_F(DocumentLoaderTest, PartialLoadingSimple) {
348 TestClient client;
349 client.SetCanUsePartialLoading();
350
351 DocumentLoader loader(&client);
352 loader.Init(client.CreateFullPageLoader(), "http://url.com");
353
354 // While we have no requests, we should not start partial loading.
355 EXPECT_FALSE(loader.is_partial_loader_active());
356
357 loader.RequestData(5000000, 1);
358
359 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
360 EXPECT_FALSE(loader.is_partial_loader_active());
361
362 // Always send initial data from FullPageLoader.
363 client.full_page_loader_data()->CallReadCallback(
364 DocumentLoader::kDefaultRequestSize);
365
366 // Partial loader should request headers.
367 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
368 EXPECT_TRUE(loader.is_partial_loader_active());
369 // Loader should be stopped.
370 EXPECT_TRUE(client.full_page_loader_data()->closed());
371
372 EXPECT_EQ("{4980736,10485760}",
373 client.partial_loader_data()->open_byte_range().ToString());
374 }
375
376 TEST_F(DocumentLoaderTest, PartialLoadingBackOrder) {
377 TestClient client;
378 client.SetCanUsePartialLoading();
379
380 DocumentLoader loader(&client);
381 loader.Init(client.CreateFullPageLoader(), "http://url.com");
382
383 // While we have no requests, we should not start partial loading.
384 EXPECT_FALSE(loader.is_partial_loader_active());
385
386 loader.RequestData(client.full_page_loader_data()->content_length() - 1, 1);
387
388 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
389 EXPECT_FALSE(loader.is_partial_loader_active());
390
391 // Always send initial data from FullPageLoader.
392 client.full_page_loader_data()->CallReadCallback(
393 DocumentLoader::kDefaultRequestSize);
394
395 // Partial loader should request headers.
396 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
397 EXPECT_TRUE(loader.is_partial_loader_active());
398 // Loader should be stopped.
399 EXPECT_TRUE(client.full_page_loader_data()->closed());
400
401 // Requested range should be enlarged.
402 EXPECT_GT(client.partial_loader_data()->open_byte_range().length(), 1u);
403 EXPECT_EQ("{9830400,10485760}",
404 client.partial_loader_data()->open_byte_range().ToString());
405 }
406
407 TEST_F(DocumentLoaderTest, CompleteWithoutPartial) {
408 TestClient client;
409 client.SetCanUsePartialLoading();
410 DocumentLoader loader(&client);
411 loader.Init(client.CreateFullPageLoader(), "http://url.com");
412 EXPECT_FALSE(client.full_page_loader_data()->closed());
413 while (client.full_page_loader_data()->IsWaitRead()) {
414 client.full_page_loader_data()->CallReadCallback(1000);
415 }
416 EXPECT_TRUE(loader.IsDocumentComplete());
417 EXPECT_TRUE(client.full_page_loader_data()->closed());
418 }
419
420 TEST_F(DocumentLoaderTest, ErrorDownloadFullDocument) {
421 TestClient client;
422 client.SetCanUsePartialLoading();
423 DocumentLoader loader(&client);
424 loader.Init(client.CreateFullPageLoader(), "http://url.com");
425 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead());
426 EXPECT_FALSE(client.full_page_loader_data()->closed());
427 client.full_page_loader_data()->CallReadCallback(-3);
428 EXPECT_TRUE(client.full_page_loader_data()->closed());
429 EXPECT_FALSE(loader.IsDocumentComplete());
430 }
431
432 TEST_F(DocumentLoaderTest, CompleteNoContentLength) {
433 TestClient client;
434 DocumentLoader loader(&client);
435 loader.Init(client.CreateFullPageLoader(), "http://url.com");
436 EXPECT_FALSE(client.full_page_loader_data()->closed());
437 for (int i = 0; i < 10; ++i) {
438 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead());
439 client.full_page_loader_data()->CallReadCallback(1000);
440 }
441 EXPECT_TRUE(client.full_page_loader_data()->IsWaitRead());
442 client.full_page_loader_data()->CallReadCallback(0);
443 EXPECT_EQ(10000ul, loader.GetDocumentSize());
444 EXPECT_TRUE(loader.IsDocumentComplete());
445 EXPECT_TRUE(client.full_page_loader_data()->closed());
446 }
447
448 TEST_F(DocumentLoaderTest, CompleteWithPartial) {
449 TestClient client;
450 client.SetCanUsePartialLoading();
451 client.full_page_loader_data()->set_content_length(
452 DocumentLoader::kDefaultRequestSize * 20);
453 DocumentLoader loader(&client);
454 loader.Init(client.CreateFullPageLoader(), "http://url.com");
455 loader.RequestData(19 * DocumentLoader::kDefaultRequestSize,
456 DocumentLoader::kDefaultRequestSize);
457 EXPECT_FALSE(client.full_page_loader_data()->closed());
458 EXPECT_FALSE(client.partial_loader_data()->IsWaitRead());
459 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
460
461 // Always send initial data from FullPageLoader.
462 client.full_page_loader_data()->CallReadCallback(
463 DocumentLoader::kDefaultRequestSize);
464 EXPECT_TRUE(client.full_page_loader_data()->closed());
465 EXPECT_FALSE(client.partial_loader_data()->closed());
466
467 client.SendAllPartialData();
468 // Now we should send other document data.
469 client.SendAllPartialData();
470 EXPECT_TRUE(client.full_page_loader_data()->closed());
471 EXPECT_TRUE(client.partial_loader_data()->closed());
472 }
473
474 TEST_F(DocumentLoaderTest, PartialRequestLastChunk) {
475 const uint32_t kLastChunkSize = 300;
476 TestClient client;
477 client.SetCanUsePartialLoading();
478 client.full_page_loader_data()->set_content_length(
479 DocumentLoader::kDefaultRequestSize * 20 + kLastChunkSize);
480 DocumentLoader loader(&client);
481 loader.Init(client.CreateFullPageLoader(), "http://url.com");
482 loader.RequestData(20 * DocumentLoader::kDefaultRequestSize, 1);
483
484 // Always send initial data from FullPageLoader.
485 client.full_page_loader_data()->CallReadCallback(
486 DocumentLoader::kDefaultRequestSize);
487
488 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
489 EXPECT_EQ(
490 static_cast<int>(client.partial_loader_data()->open_byte_range().end()),
491 client.full_page_loader_data()->content_length());
492 client.partial_loader_data()->set_byte_range(
493 client.partial_loader_data()->open_byte_range());
494 client.partial_loader_data()->CallOpenCallback(0);
495 uint32_t data_length = client.partial_loader_data()->byte_range().length();
496 while (data_length > DocumentLoader::kDefaultRequestSize) {
497 client.partial_loader_data()->CallReadCallback(
498 DocumentLoader::kDefaultRequestSize);
499 data_length -= DocumentLoader::kDefaultRequestSize;
500 }
501 EXPECT_EQ(kLastChunkSize, data_length);
502 client.partial_loader_data()->CallReadCallback(kLastChunkSize);
503 EXPECT_TRUE(loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize * 20,
504 kLastChunkSize));
505 }
506
507 TEST_F(DocumentLoaderTest, DocumentSize) {
508 TestClient client;
509 client.SetCanUsePartialLoading();
510 client.full_page_loader_data()->set_content_length(123456789);
511 DocumentLoader loader(&client);
512 loader.Init(client.CreateFullPageLoader(), "http://url.com");
513 EXPECT_EQ(static_cast<int>(loader.GetDocumentSize()),
514 client.full_page_loader_data()->content_length());
515 }
516
517 TEST_F(DocumentLoaderTest, DocumentSizeNoContentLength) {
518 TestClient client;
519 DocumentLoader loader(&client);
520 loader.Init(client.CreateFullPageLoader(), "http://url.com");
521 EXPECT_EQ(0ul, loader.GetDocumentSize());
522 client.full_page_loader_data()->CallReadCallback(
523 DocumentLoader::kDefaultRequestSize);
524 client.full_page_loader_data()->CallReadCallback(1000);
525 client.full_page_loader_data()->CallReadCallback(500);
526 client.full_page_loader_data()->CallReadCallback(0);
527 EXPECT_EQ(DocumentLoader::kDefaultRequestSize + 1000ul + 500ul,
528 loader.GetDocumentSize());
529 EXPECT_TRUE(loader.IsDocumentComplete());
530 }
531
532 TEST_F(DocumentLoaderTest, ClearPendingRequests) {
533 TestClient client;
534 client.SetCanUsePartialLoading();
535 client.full_page_loader_data()->set_content_length(
536 DocumentLoader::kDefaultRequestSize * 100 + 58383);
537 DocumentLoader loader(&client);
538 loader.Init(client.CreateFullPageLoader(), "http://url.com");
539 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10);
540 loader.ClearPendingRequests();
541 loader.RequestData(15 * DocumentLoader::kDefaultRequestSize + 200, 20);
542 // pending requests are accumulating, and will be processed after initial data
543 // load.
544 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
545
546 // Send initial data from FullPageLoader.
547 client.full_page_loader_data()->CallReadCallback(
548 DocumentLoader::kDefaultRequestSize);
549
550 {
551 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
552 const gfx::Range range_requested(15 * DocumentLoader::kDefaultRequestSize,
553 16 * DocumentLoader::kDefaultRequestSize);
554 EXPECT_EQ(range_requested.start(),
555 client.partial_loader_data()->open_byte_range().start());
556 EXPECT_LE(range_requested.end(),
557 client.partial_loader_data()->open_byte_range().end());
558 client.partial_loader_data()->set_byte_range(
559 client.partial_loader_data()->open_byte_range());
560 }
561 // clear requests before Open callback.
562 loader.ClearPendingRequests();
563 // Current request should continue loading.
564 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
565 client.partial_loader_data()->CallOpenCallback(0);
566 client.partial_loader_data()->CallReadCallback(
567 DocumentLoader::kDefaultRequestSize);
568 EXPECT_FALSE(client.partial_loader_data()->closed());
569 // Current request should continue loading, because no other request queued.
570
571 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 20);
572 // Requests queue is processed only on receiving data.
573 client.partial_loader_data()->CallReadCallback(
574 DocumentLoader::kDefaultRequestSize);
575 // New request within close distance from the one currently loading. Loading
576 // isn't restarted.
577 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
578
579 loader.ClearPendingRequests();
580 // request again two.
581 loader.RequestData(60 * DocumentLoader::kDefaultRequestSize + 100, 10);
582 loader.RequestData(35 * DocumentLoader::kDefaultRequestSize + 200, 20);
583 // Requests queue is processed only on receiving data.
584 client.partial_loader_data()->CallReadCallback(
585 DocumentLoader::kDefaultRequestSize);
586 {
587 // new requset not with in close distance from current loading.
588 // Loading should be restarted.
589 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
590 // The first requested chunk should be processed.
591 const gfx::Range range_requested(35 * DocumentLoader::kDefaultRequestSize,
592 36 * DocumentLoader::kDefaultRequestSize);
593 EXPECT_EQ(range_requested.start(),
594 client.partial_loader_data()->open_byte_range().start());
595 EXPECT_LE(range_requested.end(),
596 client.partial_loader_data()->open_byte_range().end());
597 client.partial_loader_data()->set_byte_range(
598 client.partial_loader_data()->open_byte_range());
599 }
600 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
601 client.partial_loader_data()->CallOpenCallback(0);
602 // Override pending requests.
603 loader.ClearPendingRequests();
604 loader.RequestData(70 * DocumentLoader::kDefaultRequestSize + 100, 10);
605
606 // Requests queue is processed only on receiving data.
607 client.partial_loader_data()->CallReadCallback(
608 DocumentLoader::kDefaultRequestSize);
609 {
610 // New requset not with in close distance from current loading.
611 // Loading should be restarted .
612 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
613 // The first requested chunk should be processed.
614 const gfx::Range range_requested(70 * DocumentLoader::kDefaultRequestSize,
615 71 * DocumentLoader::kDefaultRequestSize);
616 EXPECT_EQ(range_requested.start(),
617 client.partial_loader_data()->open_byte_range().start());
618 EXPECT_LE(range_requested.end(),
619 client.partial_loader_data()->open_byte_range().end());
620 client.partial_loader_data()->set_byte_range(
621 client.partial_loader_data()->open_byte_range());
622 }
623 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
624 }
625
626 TEST_F(DocumentLoaderTest, GetBlock) {
627 char buffer[DocumentLoader::kDefaultRequestSize];
628 TestClient client;
629 client.SetCanUsePartialLoading();
630 client.full_page_loader_data()->set_content_length(
631 DocumentLoader::kDefaultRequestSize * 20 + 58383);
632 DocumentLoader loader(&client);
633 loader.Init(client.CreateFullPageLoader(), "http://url.com");
634 EXPECT_FALSE(loader.GetBlock(0, 1000, buffer));
635 client.full_page_loader_data()->CallReadCallback(
636 DocumentLoader::kDefaultRequestSize);
637 EXPECT_TRUE(loader.GetBlock(0, 1000, buffer));
638 EXPECT_FALSE(
639 loader.GetBlock(DocumentLoader::kDefaultRequestSize, 1500, buffer));
640 client.full_page_loader_data()->CallReadCallback(
641 DocumentLoader::kDefaultRequestSize);
642 EXPECT_TRUE(
643 loader.GetBlock(DocumentLoader::kDefaultRequestSize, 1500, buffer));
644
645 EXPECT_FALSE(
646 loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, buffer));
647 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10);
648 EXPECT_FALSE(
649 loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, buffer));
650
651 // Requests queue is processed only on receiving data.
652 client.full_page_loader_data()->CallReadCallback(
653 DocumentLoader::kDefaultRequestSize);
654
655 client.SendAllPartialData();
656 EXPECT_TRUE(
657 loader.GetBlock(17 * DocumentLoader::kDefaultRequestSize, 3000, buffer));
658 }
659
660 TEST_F(DocumentLoaderTest, IsDataAvailable) {
661 TestClient client;
662 client.SetCanUsePartialLoading();
663 client.full_page_loader_data()->set_content_length(
664 DocumentLoader::kDefaultRequestSize * 20 + 58383);
665 DocumentLoader loader(&client);
666 loader.Init(client.CreateFullPageLoader(), "http://url.com");
667 EXPECT_FALSE(loader.IsDataAvailable(0, 1000));
668 client.full_page_loader_data()->CallReadCallback(
669 DocumentLoader::kDefaultRequestSize);
670 EXPECT_TRUE(loader.IsDataAvailable(0, 1000));
671 EXPECT_FALSE(
672 loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize, 1500));
673 client.full_page_loader_data()->CallReadCallback(
674 DocumentLoader::kDefaultRequestSize);
675 EXPECT_TRUE(
676 loader.IsDataAvailable(DocumentLoader::kDefaultRequestSize, 1500));
677
678 EXPECT_FALSE(
679 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000));
680 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 100, 10);
681 EXPECT_FALSE(
682 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000));
683
684 // Requests queue is processed only on receiving data.
685 client.full_page_loader_data()->CallReadCallback(
686 DocumentLoader::kDefaultRequestSize);
687
688 client.SendAllPartialData();
689 EXPECT_TRUE(
690 loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize, 3000));
691 }
692
693 TEST_F(DocumentLoaderTest, RequestData) {
694 TestClient client;
695 client.SetCanUsePartialLoading();
696 client.full_page_loader_data()->set_content_length(
697 DocumentLoader::kDefaultRequestSize * 100 + 58383);
698 DocumentLoader loader(&client);
699 loader.Init(client.CreateFullPageLoader(), "http://url.com");
700 loader.RequestData(37 * DocumentLoader::kDefaultRequestSize + 200, 10);
701 loader.RequestData(25 * DocumentLoader::kDefaultRequestSize + 600, 100);
702 loader.RequestData(13 * DocumentLoader::kDefaultRequestSize + 900, 500);
703
704 // Send initial data from FullPageLoader.
705 client.full_page_loader_data()->CallReadCallback(
706 DocumentLoader::kDefaultRequestSize);
707
708 {
709 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
710 const gfx::Range range_requested(13 * DocumentLoader::kDefaultRequestSize,
711 14 * DocumentLoader::kDefaultRequestSize);
712 EXPECT_EQ(range_requested.start(),
713 client.partial_loader_data()->open_byte_range().start());
714 EXPECT_LE(range_requested.end(),
715 client.partial_loader_data()->open_byte_range().end());
716 client.partial_loader_data()->set_byte_range(
717 client.partial_loader_data()->open_byte_range());
718 }
719 client.partial_loader_data()->CallOpenCallback(0);
720 // Override pending requests.
721 loader.ClearPendingRequests();
722 loader.RequestData(38 * DocumentLoader::kDefaultRequestSize + 200, 10);
723 loader.RequestData(26 * DocumentLoader::kDefaultRequestSize + 600, 100);
724 // Requests queue is processed only on receiving data.
725 client.partial_loader_data()->CallReadCallback(
726 DocumentLoader::kDefaultRequestSize);
727 {
728 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
729 const gfx::Range range_requested(26 * DocumentLoader::kDefaultRequestSize,
730 27 * DocumentLoader::kDefaultRequestSize);
731 EXPECT_EQ(range_requested.start(),
732 client.partial_loader_data()->open_byte_range().start());
733 EXPECT_LE(range_requested.end(),
734 client.partial_loader_data()->open_byte_range().end());
735 client.partial_loader_data()->set_byte_range(
736 client.partial_loader_data()->open_byte_range());
737 }
738 client.partial_loader_data()->CallOpenCallback(0);
739 // Override pending requests.
740 loader.ClearPendingRequests();
741 loader.RequestData(39 * DocumentLoader::kDefaultRequestSize + 200, 10);
742 // Requests queue is processed only on receiving data.
743 client.partial_loader_data()->CallReadCallback(
744 DocumentLoader::kDefaultRequestSize);
745 {
746 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
747 const gfx::Range range_requested(39 * DocumentLoader::kDefaultRequestSize,
748 40 * DocumentLoader::kDefaultRequestSize);
749 EXPECT_EQ(range_requested.start(),
750 client.partial_loader_data()->open_byte_range().start());
751 EXPECT_LE(range_requested.end(),
752 client.partial_loader_data()->open_byte_range().end());
753 client.partial_loader_data()->set_byte_range(
754 client.partial_loader_data()->open_byte_range());
755 }
756 // Fill all gaps.
757 while (!loader.IsDocumentComplete()) {
758 client.SendAllPartialData();
759 }
760 EXPECT_TRUE(client.partial_loader_data()->closed());
761 }
762
763 TEST_F(DocumentLoaderTest, DoNotLoadAvailablePartialData) {
764 TestClient client;
765 client.SetCanUsePartialLoading();
766 client.full_page_loader_data()->set_content_length(
767 DocumentLoader::kDefaultRequestSize * 20 + 58383);
768 DocumentLoader loader(&client);
769 loader.Init(client.CreateFullPageLoader(), "http://url.com");
770 // Send initial data from FullPageLoader.
771 client.full_page_loader_data()->CallReadCallback(
772 DocumentLoader::kDefaultRequestSize);
773
774 // Send more data from FullPageLoader.
775 client.full_page_loader_data()->CallReadCallback(
776 DocumentLoader::kDefaultRequestSize);
777
778 loader.RequestData(2 * DocumentLoader::kDefaultRequestSize + 200, 10);
779
780 // Send more data from FullPageLoader.
781 client.full_page_loader_data()->CallReadCallback(
782 DocumentLoader::kDefaultRequestSize);
783
784 // Partial loading should not have started for already available data.
785 EXPECT_TRUE(client.partial_loader_data()->closed());
786 }
787
788 TEST_F(DocumentLoaderTest, DoNotLoadDataAfterComplete) {
789 TestClient client;
790 client.SetCanUsePartialLoading();
791 client.full_page_loader_data()->set_content_length(
792 DocumentLoader::kDefaultRequestSize * 20);
793 DocumentLoader loader(&client);
794 loader.Init(client.CreateFullPageLoader(), "http://url.com");
795
796 for (int i = 0; i < 20; ++i) {
797 client.full_page_loader_data()->CallReadCallback(
798 DocumentLoader::kDefaultRequestSize);
799 }
800
801 EXPECT_TRUE(loader.IsDocumentComplete());
802
803 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
804
805 EXPECT_TRUE(client.partial_loader_data()->closed());
806 EXPECT_TRUE(client.full_page_loader_data()->closed());
807 }
808
809 TEST_F(DocumentLoaderTest, DoNotLoadPartialDataAboveDocumentSize) {
810 TestClient client;
811 client.SetCanUsePartialLoading();
812 client.full_page_loader_data()->set_content_length(
813 DocumentLoader::kDefaultRequestSize * 20);
814 DocumentLoader loader(&client);
815 loader.Init(client.CreateFullPageLoader(), "http://url.com");
816
817 loader.RequestData(20 * DocumentLoader::kDefaultRequestSize + 200, 10);
818
819 // Send initial data from FullPageLoader.
820 client.full_page_loader_data()->CallReadCallback(
821 DocumentLoader::kDefaultRequestSize);
822
823 EXPECT_TRUE(client.partial_loader_data()->closed());
824 }
825
826 TEST_F(DocumentLoaderTest, MergePendingRequests) {
827 TestClient client;
828 client.SetCanUsePartialLoading();
829 client.full_page_loader_data()->set_content_length(
830 DocumentLoader::kDefaultRequestSize * 50 + 58383);
831 DocumentLoader loader(&client);
832 loader.Init(client.CreateFullPageLoader(), "http://url.com");
833 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
834 loader.RequestData(16 * DocumentLoader::kDefaultRequestSize + 600, 100);
835
836 // Send initial data from FullPageLoader.
837 client.full_page_loader_data()->CallReadCallback(
838 DocumentLoader::kDefaultRequestSize);
839
840 const gfx::Range range_requested(16 * DocumentLoader::kDefaultRequestSize,
841 18 * DocumentLoader::kDefaultRequestSize);
842 EXPECT_EQ(range_requested.start(),
843 client.partial_loader_data()->open_byte_range().start());
844 EXPECT_LE(range_requested.end(),
845 client.partial_loader_data()->open_byte_range().end());
846
847 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
848
849 // Fill all gaps.
850 while (!loader.IsDocumentComplete()) {
851 client.SendAllPartialData();
852 }
853 EXPECT_TRUE(client.partial_loader_data()->closed());
854 }
855
856 TEST_F(DocumentLoaderTest, PartialStopOnStatusCodeError) {
857 TestClient client;
858 client.SetCanUsePartialLoading();
859 client.full_page_loader_data()->set_content_length(
860 DocumentLoader::kDefaultRequestSize * 20);
861 DocumentLoader loader(&client);
862 loader.Init(client.CreateFullPageLoader(), "http://url.com");
863
864 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
865
866 // Send initial data from FullPageLoader.
867 client.full_page_loader_data()->CallReadCallback(
868 DocumentLoader::kDefaultRequestSize);
869
870 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
871 client.partial_loader_data()->set_status_code(404);
872 client.partial_loader_data()->CallOpenCallback(0);
873 EXPECT_TRUE(client.partial_loader_data()->closed());
874 }
875
876 TEST_F(DocumentLoaderTest,
877 PartialAsFullDocumentLoadingRangeRequestNoRangeField) {
878 TestClient client;
879 client.SetCanUsePartialLoading();
880 client.full_page_loader_data()->set_content_length(
881 DocumentLoader::kDefaultRequestSize * 20);
882 DocumentLoader loader(&client);
883 loader.Init(client.CreateFullPageLoader(), "http://url.com");
884
885 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
886
887 // Send initial data from FullPageLoader.
888 client.full_page_loader_data()->CallReadCallback(
889 DocumentLoader::kDefaultRequestSize);
890
891 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
892 client.partial_loader_data()->set_byte_range(gfx::Range::InvalidRange());
893 client.partial_loader_data()->CallOpenCallback(0);
894 EXPECT_FALSE(client.partial_loader_data()->closed());
895 // Partial loader is used to load the whole page, like full page loader.
896 EXPECT_FALSE(loader.is_partial_loader_active());
897 }
898
899 TEST_F(DocumentLoaderTest, PartialMultiPart) {
900 TestClient client;
901 client.SetCanUsePartialLoading();
902 client.full_page_loader_data()->set_content_length(
903 DocumentLoader::kDefaultRequestSize * 20);
904 DocumentLoader loader(&client);
905 loader.Init(client.CreateFullPageLoader(), "http://url.com");
906
907 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
908
909 // Send initial data from FullPageLoader.
910 client.full_page_loader_data()->CallReadCallback(
911 DocumentLoader::kDefaultRequestSize);
912
913 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
914 client.partial_loader_data()->set_is_multipart(true);
915 client.partial_loader_data()->CallOpenCallback(0);
916 client.partial_loader_data()->set_byte_range(
917 gfx::Range(17 * DocumentLoader::kDefaultRequestSize,
918 18 * DocumentLoader::kDefaultRequestSize));
919 client.partial_loader_data()->CallReadCallback(
920 DocumentLoader::kDefaultRequestSize);
921 EXPECT_TRUE(loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize,
922 DocumentLoader::kDefaultRequestSize));
923 }
924
925 TEST_F(DocumentLoaderTest, PartialMultiPartRangeError) {
926 TestClient client;
927 client.SetCanUsePartialLoading();
928 client.full_page_loader_data()->set_content_length(
929 DocumentLoader::kDefaultRequestSize * 20);
930 DocumentLoader loader(&client);
931 loader.Init(client.CreateFullPageLoader(), "http://url.com");
932
933 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
934
935 // Send initial data from FullPageLoader.
936 client.full_page_loader_data()->CallReadCallback(
937 DocumentLoader::kDefaultRequestSize);
938
939 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
940 client.partial_loader_data()->set_is_multipart(true);
941 client.partial_loader_data()->CallOpenCallback(0);
942 client.partial_loader_data()->set_byte_range(gfx::Range::InvalidRange());
943 client.partial_loader_data()->CallReadCallback(
944 DocumentLoader::kDefaultRequestSize);
945 EXPECT_FALSE(loader.IsDataAvailable(17 * DocumentLoader::kDefaultRequestSize,
946 DocumentLoader::kDefaultRequestSize));
947 EXPECT_TRUE(client.partial_loader_data()->closed());
948 }
949
950 TEST_F(DocumentLoaderTest, PartialConnectionErrorOnOpen) {
951 TestClient client;
952 client.SetCanUsePartialLoading();
953 client.full_page_loader_data()->set_content_length(
954 DocumentLoader::kDefaultRequestSize * 20);
955 DocumentLoader loader(&client);
956 loader.Init(client.CreateFullPageLoader(), "http://url.com");
957
958 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
959
960 // Send initial data from FullPageLoader.
961 client.full_page_loader_data()->CallReadCallback(
962 DocumentLoader::kDefaultRequestSize);
963
964 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
965 client.partial_loader_data()->CallOpenCallback(-3);
966 EXPECT_TRUE(client.partial_loader_data()->closed());
967
968 // Partial loading should not restart after any error.
969 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 10);
970
971 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
972 EXPECT_TRUE(client.partial_loader_data()->closed());
973 }
974
975 TEST_F(DocumentLoaderTest, PartialConnectionErrorOnRead) {
976 TestClient client;
977 client.SetCanUsePartialLoading();
978 client.full_page_loader_data()->set_content_length(
979 DocumentLoader::kDefaultRequestSize * 20);
980 DocumentLoader loader(&client);
981 loader.Init(client.CreateFullPageLoader(), "http://url.com");
982
983 loader.RequestData(17 * DocumentLoader::kDefaultRequestSize + 200, 10);
984
985 // Send initial data from FullPageLoader.
986 client.full_page_loader_data()->CallReadCallback(
987 DocumentLoader::kDefaultRequestSize);
988
989 EXPECT_TRUE(client.partial_loader_data()->IsWaitOpen());
990 client.partial_loader_data()->set_byte_range(
991 gfx::Range(17 * DocumentLoader::kDefaultRequestSize,
992 18 * DocumentLoader::kDefaultRequestSize));
993 client.partial_loader_data()->CallOpenCallback(0);
994 EXPECT_TRUE(client.partial_loader_data()->IsWaitRead());
995 client.partial_loader_data()->CallReadCallback(-3);
996 EXPECT_TRUE(client.partial_loader_data()->closed());
997
998 // Partial loading should not restart after any error.
999 loader.RequestData(18 * DocumentLoader::kDefaultRequestSize + 200, 10);
1000
1001 EXPECT_FALSE(client.partial_loader_data()->IsWaitOpen());
1002 EXPECT_TRUE(client.partial_loader_data()->closed());
1003 }
1004
1005 TEST_F(DocumentLoaderTest, GetProgress) {
1006 TestClient client;
1007 client.SetCanUsePartialLoading();
1008 client.full_page_loader_data()->set_content_length(
1009 DocumentLoader::kDefaultRequestSize * 20);
1010 DocumentLoader loader(&client);
1011 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1012 EXPECT_EQ(0., loader.GetProgress());
1013
1014 for (int i = 0; i < 20; ++i) {
1015 EXPECT_EQ(i * 100 / 20, static_cast<int>(loader.GetProgress() * 100));
1016 client.full_page_loader_data()->CallReadCallback(
1017 DocumentLoader::kDefaultRequestSize);
1018 }
1019 EXPECT_EQ(1., loader.GetProgress());
1020 }
1021
1022 TEST_F(DocumentLoaderTest, GetProgressNoContentLength) {
1023 TestClient client;
1024 DocumentLoader loader(&client);
1025 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1026 EXPECT_EQ(-1., loader.GetProgress());
1027
1028 for (int i = 0; i < 20; ++i) {
1029 EXPECT_EQ(-1., loader.GetProgress());
1030 client.full_page_loader_data()->CallReadCallback(
1031 DocumentLoader::kDefaultRequestSize);
1032 }
1033 client.full_page_loader_data()->CallReadCallback(0);
1034 EXPECT_EQ(1., loader.GetProgress());
1035 }
1036
1037 TEST_F(DocumentLoaderTest, ClientCompleteCallbacks) {
1038 MockClient client;
1039 client.SetCanUsePartialLoading();
1040 client.full_page_loader_data()->set_content_length(
1041 DocumentLoader::kDefaultRequestSize * 20);
1042 DocumentLoader loader(&client);
1043 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1044
1045 EXPECT_CALL(client, OnDocumentComplete()).Times(0);
1046 for (int i = 0; i < 19; ++i) {
1047 client.full_page_loader_data()->CallReadCallback(
1048 DocumentLoader::kDefaultRequestSize);
1049 }
1050 Mock::VerifyAndClear(&client);
1051
1052 EXPECT_CALL(client, OnDocumentComplete()).Times(1);
1053 client.full_page_loader_data()->CallReadCallback(
1054 DocumentLoader::kDefaultRequestSize);
1055 Mock::VerifyAndClear(&client);
1056 }
1057
1058 TEST_F(DocumentLoaderTest, ClientCompleteCallbacksNoContentLength) {
1059 MockClient client;
1060 DocumentLoader loader(&client);
1061 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1062
1063 EXPECT_CALL(client, OnDocumentCanceled()).Times(0);
1064 EXPECT_CALL(client, OnDocumentComplete()).Times(0);
1065 for (int i = 0; i < 20; ++i) {
1066 client.full_page_loader_data()->CallReadCallback(
1067 DocumentLoader::kDefaultRequestSize);
1068 }
1069 Mock::VerifyAndClear(&client);
1070
1071 EXPECT_CALL(client, OnDocumentCanceled()).Times(0);
1072 EXPECT_CALL(client, OnDocumentComplete()).Times(1);
1073 client.full_page_loader_data()->CallReadCallback(0);
1074 Mock::VerifyAndClear(&client);
1075 }
1076
1077 TEST_F(DocumentLoaderTest, ClientCancelCallback) {
1078 MockClient client;
1079 client.SetCanUsePartialLoading();
1080 client.full_page_loader_data()->set_content_length(
1081 DocumentLoader::kDefaultRequestSize * 20);
1082 DocumentLoader loader(&client);
1083 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1084
1085 EXPECT_CALL(client, OnDocumentCanceled()).Times(0);
1086 EXPECT_CALL(client, OnDocumentComplete()).Times(0);
1087 for (int i = 0; i < 10; ++i) {
1088 client.full_page_loader_data()->CallReadCallback(
1089 DocumentLoader::kDefaultRequestSize);
1090 }
1091 Mock::VerifyAndClear(&client);
1092
1093 EXPECT_CALL(client, OnDocumentComplete()).Times(0);
1094 EXPECT_CALL(client, OnDocumentCanceled()).Times(1);
1095 client.full_page_loader_data()->CallReadCallback(-3);
1096 Mock::VerifyAndClear(&client);
1097 }
1098
1099 TEST_F(DocumentLoaderTest, NewDataAvailable) {
1100 MockClient client;
1101 client.SetCanUsePartialLoading();
1102 client.full_page_loader_data()->set_content_length(
1103 DocumentLoader::kDefaultRequestSize * 20);
1104 DocumentLoader loader(&client);
1105 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1106
1107 EXPECT_CALL(client, OnNewDataAvailable()).Times(1);
1108 client.full_page_loader_data()->CallReadCallback(
1109 DocumentLoader::kDefaultRequestSize);
1110 Mock::VerifyAndClear(&client);
1111
1112 EXPECT_CALL(client, OnNewDataAvailable()).Times(0);
1113 client.full_page_loader_data()->CallReadCallback(
1114 DocumentLoader::kDefaultRequestSize - 100);
1115 Mock::VerifyAndClear(&client);
1116
1117 EXPECT_CALL(client, OnNewDataAvailable()).Times(1);
1118 client.full_page_loader_data()->CallReadCallback(100);
1119 Mock::VerifyAndClear(&client);
1120 }
1121
1122 TEST_F(DocumentLoaderTest, ClientPendingRequestCompleteFullLoader) {
1123 MockClient client;
1124 client.SetCanUsePartialLoading();
1125 DocumentLoader loader(&client);
1126 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1127
1128 loader.RequestData(1000, 4000);
1129
1130 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1);
1131 client.full_page_loader_data()->CallReadCallback(
1132 DocumentLoader::kDefaultRequestSize);
1133 Mock::VerifyAndClear(&client);
1134 }
1135
1136 TEST_F(DocumentLoaderTest, ClientPendingRequestCompletePartialLoader) {
1137 MockClient client;
1138 client.SetCanUsePartialLoading();
1139 DocumentLoader loader(&client);
1140 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1141
1142 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1);
1143 loader.RequestData(15 * DocumentLoader::kDefaultRequestSize + 4000, 4000);
1144
1145 // Always send initial data from FullPageLoader.
1146 client.full_page_loader_data()->CallReadCallback(
1147 DocumentLoader::kDefaultRequestSize);
1148
1149 client.SendAllPartialData();
1150 Mock::VerifyAndClear(&client);
1151 }
1152
1153 TEST_F(DocumentLoaderTest, ClientPendingRequestCompletePartialAndFullLoader) {
1154 MockClient client;
1155 client.SetCanUsePartialLoading();
1156 DocumentLoader loader(&client);
1157 loader.Init(client.CreateFullPageLoader(), "http://url.com");
1158
1159 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1);
1160 loader.RequestData(16 * DocumentLoader::kDefaultRequestSize + 4000, 4000);
1161 loader.RequestData(4 * DocumentLoader::kDefaultRequestSize + 4000, 4000);
1162
1163 for (int i = 0; i < 5; ++i) {
1164 client.full_page_loader_data()->CallReadCallback(
1165 DocumentLoader::kDefaultRequestSize);
1166 }
1167
1168 Mock::VerifyAndClear(&client);
1169
1170 EXPECT_CALL(client, OnPendingRequestComplete()).Times(1);
1171 client.SendAllPartialData();
1172 Mock::VerifyAndClear(&client);
1173 }
1174
1175 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698