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