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
|
// Copyright (c) 2010 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 <algorithm>
#include "base/callback.h"
#include "base/format_macros.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "media/base/filters.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_filters.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "webkit/glue/media/buffered_data_source.h"
#include "webkit/glue/media/mock_media_resource_loader_bridge_factory.h"
#include "webkit/glue/mock_resource_loader_bridge.h"
using ::testing::_;
using ::testing::Assign;
using ::testing::DeleteArg;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::SetArgumentPointee;
using ::testing::StrictMock;
using ::testing::NiceMock;
using ::testing::WithArgs;
namespace {
const char* kHttpUrl = "http://test";
const char* kFileUrl = "file://test";
const int kDataSize = 1024;
enum NetworkState {
NONE,
LOADED,
LOADING
};
} // namespace
namespace webkit_glue {
// Submit a request completed event to the resource loader due to request
// being canceled. Pretending the event is from external.
ACTION_P(RequestCanceled, loader) {
URLRequestStatus status;
status.set_status(URLRequestStatus::CANCELED);
status.set_os_error(net::ERR_ABORTED);
loader->OnCompletedRequest(status, "", base::Time());
}
class BufferedResourceLoaderTest : public testing::Test {
public:
BufferedResourceLoaderTest() {
bridge_.reset(new StrictMock<MockResourceLoaderBridge>());
for (int i = 0; i < kDataSize; ++i)
data_[i] = i;
}
~BufferedResourceLoaderTest() {
if (bridge_.get())
EXPECT_CALL(*bridge_, OnDestroy());
EXPECT_CALL(bridge_factory_, OnDestroy());
}
void Initialize(const char* url, int first_position, int last_position) {
gurl_ = GURL(url);
first_position_ = first_position;
last_position_ = last_position;
loader_ = new BufferedResourceLoader(&bridge_factory_, gurl_,
first_position_, last_position_);
EXPECT_EQ(gurl_.spec(), loader_->GetURLForDebugging().spec());
}
void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) {
loader_->buffer_.reset(
new media::SeekableBuffer(backward_capacity, forward_capacity));
}
void Start() {
InSequence s;
EXPECT_CALL(bridge_factory_,
CreateBridge(gurl_, _, first_position_, last_position_))
.WillOnce(Return(bridge_.get()));
EXPECT_CALL(*bridge_, Start(loader_.get()));
loader_->Start(
NewCallback(this, &BufferedResourceLoaderTest::StartCallback),
NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback));
}
void FullResponse(int64 instance_size) {
EXPECT_CALL(*this, StartCallback(net::OK));
ResourceResponseInfo info;
std::string header = base::StringPrintf("HTTP/1.1 200 OK\n"
"Content-Length: %" PRId64,
instance_size);
replace(header.begin(), header.end(), '\n', '\0');
info.headers = new net::HttpResponseHeaders(header);
info.content_length = instance_size;
loader_->OnReceivedResponse(info, false);
EXPECT_EQ(instance_size, loader_->content_length());
EXPECT_EQ(instance_size, loader_->instance_size());
EXPECT_FALSE(loader_->partial_response());
}
void PartialResponse(int64 first_position, int64 last_position,
int64 instance_size) {
EXPECT_CALL(*this, StartCallback(net::OK));
int64 content_length = last_position - first_position + 1;
ResourceResponseInfo info;
std::string header = base::StringPrintf("HTTP/1.1 206 Partial Content\n"
"Content-Range: bytes "
"%" PRId64 "-%" PRId64 "/%" PRId64,
first_position,
last_position,
instance_size);
replace(header.begin(), header.end(), '\n', '\0');
info.headers = new net::HttpResponseHeaders(header);
info.content_length = content_length;
loader_->OnReceivedResponse(info, false);
EXPECT_EQ(content_length, loader_->content_length());
EXPECT_EQ(instance_size, loader_->instance_size());
EXPECT_TRUE(loader_->partial_response());
}
void StopWhenLoad() {
InSequence s;
EXPECT_CALL(*bridge_, Cancel())
.WillOnce(RequestCanceled(loader_));
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
loader_->Stop();
}
void ReleaseBridge() {
ignore_result(bridge_.release());
}
// Helper method to write to |loader_| from |data_|.
void WriteLoader(int position, int size) {
EXPECT_CALL(*this, NetworkCallback())
.RetiresOnSaturation();
loader_->OnReceivedData(reinterpret_cast<char*>(data_ + position), size);
}
// Helper method to read from |loader_|.
void ReadLoader(int64 position, int size, uint8* buffer) {
loader_->Read(position, size, buffer,
NewCallback(this, &BufferedResourceLoaderTest::ReadCallback));
}
// Verifis that data in buffer[0...size] is equal to data_[pos...pos+size].
void VerifyBuffer(uint8* buffer, int pos, int size) {
EXPECT_EQ(0, memcmp(buffer, data_ + pos, size));
}
// Helper method to disallow deferring in |loader_|.
void DisallowLoaderDefer() {
if (loader_->deferred_) {
EXPECT_CALL(*bridge_, SetDefersLoading(false));
EXPECT_CALL(*this, NetworkCallback());
}
loader_->SetAllowDefer(false);
}
// Helper method to allow deferring in |loader_|.
void AllowLoaderDefer() {
loader_->SetAllowDefer(true);
}
MOCK_METHOD1(StartCallback, void(int error));
MOCK_METHOD1(ReadCallback, void(int error));
MOCK_METHOD0(NetworkCallback, void());
protected:
GURL gurl_;
int64 first_position_;
int64 last_position_;
scoped_refptr<BufferedResourceLoader> loader_;
StrictMock<MockMediaResourceLoaderBridgeFactory> bridge_factory_;
scoped_ptr<StrictMock<MockResourceLoaderBridge> > bridge_;
uint8 data_[kDataSize];
private:
DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest);
};
TEST_F(BufferedResourceLoaderTest, StartStop) {
Initialize(kHttpUrl, -1, -1);
Start();
StopWhenLoad();
}
// Tests that HTTP header is missing in the response.
TEST_F(BufferedResourceLoaderTest, MissingHttpHeader) {
Initialize(kHttpUrl, -1, -1);
Start();
EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE));
EXPECT_CALL(*bridge_, Cancel())
.WillOnce(RequestCanceled(loader_));
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
ResourceResponseInfo info;
loader_->OnReceivedResponse(info, false);
}
// Tests that a bad HTTP response is recived, e.g. file not found.
TEST_F(BufferedResourceLoaderTest, BadHttpResponse) {
Initialize(kHttpUrl, -1, -1);
Start();
EXPECT_CALL(*this, StartCallback(net::ERR_FAILED));
EXPECT_CALL(*bridge_, Cancel())
.WillOnce(RequestCanceled(loader_));
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
ResourceResponseInfo info;
info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Not Found\n");
loader_->OnReceivedResponse(info, false);
}
// Tests that partial content is requested but not fulfilled.
TEST_F(BufferedResourceLoaderTest, NotPartialResponse) {
Initialize(kHttpUrl, 100, -1);
Start();
FullResponse(1024);
StopWhenLoad();
}
// Tests that a 200 response is received.
TEST_F(BufferedResourceLoaderTest, FullResponse) {
Initialize(kHttpUrl, -1, -1);
Start();
FullResponse(1024);
StopWhenLoad();
}
// Tests that a partial content response is received.
TEST_F(BufferedResourceLoaderTest, PartialResponse) {
Initialize(kHttpUrl, 100, 200);
Start();
PartialResponse(100, 200, 1024);
StopWhenLoad();
}
// Tests that an invalid partial response is received.
TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) {
Initialize(kHttpUrl, 0, 10);
Start();
EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE));
EXPECT_CALL(*bridge_, Cancel())
.WillOnce(RequestCanceled(loader_));
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
ResourceResponseInfo info;
std::string header = base::StringPrintf("HTTP/1.1 206 Partial Content\n"
"Content-Range: bytes %d-%d/%d",
1, 10, 1024);
replace(header.begin(), header.end(), '\n', '\0');
info.headers = new net::HttpResponseHeaders(header);
info.content_length = 10;
loader_->OnReceivedResponse(info, false);
}
// Tests the logic of sliding window for data buffering and reading.
TEST_F(BufferedResourceLoaderTest, BufferAndRead) {
Initialize(kHttpUrl, 10, 29);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
// Writes 10 bytes and read them back.
WriteLoader(10, 10);
EXPECT_CALL(*this, ReadCallback(10));
ReadLoader(10, 10, buffer);
VerifyBuffer(buffer, 10, 10);
// Writes 10 bytes and read 2 times.
WriteLoader(20, 10);
EXPECT_CALL(*this, ReadCallback(5));
ReadLoader(20, 5, buffer);
VerifyBuffer(buffer, 20, 5);
EXPECT_CALL(*this, ReadCallback(5));
ReadLoader(25, 5, buffer);
VerifyBuffer(buffer, 25, 5);
// Read backward within buffer.
EXPECT_CALL(*this, ReadCallback(10));
ReadLoader(10, 10, buffer);
VerifyBuffer(buffer, 10, 10);
// Read backward outside buffer.
EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
ReadLoader(9, 10, buffer);
// Response has completed.
EXPECT_CALL(*this, NetworkCallback());
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
URLRequestStatus status;
status.set_status(URLRequestStatus::SUCCESS);
loader_->OnCompletedRequest(status, "", base::Time());
// Try to read 10 from position 25 will just return with 5 bytes.
EXPECT_CALL(*this, ReadCallback(5));
ReadLoader(25, 10, buffer);
VerifyBuffer(buffer, 25, 5);
// Try to read outside buffered range after request has completed.
EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
ReadLoader(5, 10, buffer);
// Try to read beyond the instance size.
EXPECT_CALL(*this, ReadCallback(0));
ReadLoader(30, 10, buffer);
}
TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) {
Initialize(kHttpUrl, 10, 0x00FFFFFF);
Start();
PartialResponse(10, 0x00FFFFFF, 0x01000000);
uint8 buffer[10];
InSequence s;
// Read very far aheard will get a cache miss.
EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
ReadLoader(0x00FFFFFF, 1, buffer);
// The following call will not call ReadCallback() because it is waiting for
// data to arrive.
ReadLoader(10, 10, buffer);
// Writing to loader will fulfill the read request.
EXPECT_CALL(*this, ReadCallback(10));
WriteLoader(10, 20);
VerifyBuffer(buffer, 10, 10);
// The following call cannot be fulfilled now.
ReadLoader(25, 10, buffer);
EXPECT_CALL(*this, ReadCallback(5));
EXPECT_CALL(*this, NetworkCallback());
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
URLRequestStatus status;
status.set_status(URLRequestStatus::SUCCESS);
loader_->OnCompletedRequest(status, "", base::Time());
}
TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) {
Initialize(kHttpUrl, 10, 29);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
ReadLoader(10, 10, buffer);
EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED));
EXPECT_CALL(*this, NetworkCallback());
EXPECT_CALL(*bridge_, OnDestroy())
.WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
URLRequestStatus status;
status.set_status(URLRequestStatus::FAILED);
loader_->OnCompletedRequest(status, "", base::Time());
}
// Tests the logic of caching data to disk when media is paused.
TEST_F(BufferedResourceLoaderTest, AllowDefer_NoDataReceived) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
// Start in undeferred state, then disallow defer, then allow defer
// without receiving data in between.
DisallowLoaderDefer();
AllowLoaderDefer();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadSameWindow) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
// Start in undeferred state, disallow defer, receive data but don't shift
// buffer window, then allow defer and read.
DisallowLoaderDefer();
WriteLoader(10, 10);
AllowLoaderDefer();
EXPECT_CALL(*this, ReadCallback(10));
ReadLoader(10, 10, buffer);
VerifyBuffer(buffer, 10, 10);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadPastWindow) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
// Not deferred, disallow defer, received data and shift buffer window,
// allow defer, then read in area outside of buffer window.
DisallowLoaderDefer();
WriteLoader(10, 10);
WriteLoader(20, 50);
AllowLoaderDefer();
EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
ReadLoader(10, 10, buffer);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredNoDataReceived) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
// Start in deferred state, then disallow defer, receive no data, and
// allow defer and read.
EXPECT_CALL(*bridge_, SetDefersLoading(true));
EXPECT_CALL(*this, NetworkCallback());
WriteLoader(10, 40);
DisallowLoaderDefer();
AllowLoaderDefer();
EXPECT_CALL(*this, ReadCallback(10));
ReadLoader(20, 10, buffer);
VerifyBuffer(buffer, 20, 10);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadSameWindow) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
// Start in deferred state, disallow defer, receive data and shift buffer
// window, allow defer, and read in a place that's still in the window.
EXPECT_CALL(*bridge_, SetDefersLoading(true));
EXPECT_CALL(*this, NetworkCallback());
WriteLoader(10, 30);
DisallowLoaderDefer();
WriteLoader(40, 5);
AllowLoaderDefer();
EXPECT_CALL(*this, ReadCallback(10));
ReadLoader(20, 10, buffer);
VerifyBuffer(buffer, 20, 10);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
// Start in deferred state, disallow defer, receive data and shift buffer
// window, allow defer, and read outside of the buffer window.
EXPECT_CALL(*bridge_, SetDefersLoading(true));
EXPECT_CALL(*this, NetworkCallback());
WriteLoader(10, 40);
DisallowLoaderDefer();
WriteLoader(50, 20);
WriteLoader(70, 40);
AllowLoaderDefer();
EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS));
ReadLoader(20, 5, buffer);
StopWhenLoad();
}
// TODO(hclam): add unit test for defer loading.
class MockBufferedResourceLoader : public BufferedResourceLoader {
public:
MockBufferedResourceLoader() : BufferedResourceLoader(NULL, GURL(), 0, 0) {
}
MOCK_METHOD2(Start, void(net::CompletionCallback* read_callback,
NetworkEventCallback* network_callback));
MOCK_METHOD0(Stop, void());
MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer,
net::CompletionCallback* callback));
MOCK_METHOD0(content_length, int64());
MOCK_METHOD0(instance_size, int64());
MOCK_METHOD0(partial_response, bool());
MOCK_METHOD0(network_activity, bool());
MOCK_METHOD0(GetBufferedFirstBytePosition, int64());
MOCK_METHOD0(GetBufferedLastBytePosition, int64());
protected:
~MockBufferedResourceLoader() {}
DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader);
};
// A mock BufferedDataSource to inject mock BufferedResourceLoader through
// CreateResourceLoader() method.
class MockBufferedDataSource : public BufferedDataSource {
public:
// Static methods for creating this class.
static media::FilterFactory* CreateFactory(
MessageLoop* message_loop,
MediaResourceLoaderBridgeFactory* bridge_factory) {
return new media::FilterFactoryImpl2<
MockBufferedDataSource,
MessageLoop*,
MediaResourceLoaderBridgeFactory*>(message_loop,
bridge_factory);
}
virtual base::TimeDelta GetTimeoutMilliseconds() {
// It is 100 ms because we don't want the test to run too long.
return base::TimeDelta::FromMilliseconds(100);
}
MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(
int64 first_position, int64 last_position));
protected:
MockBufferedDataSource(
MessageLoop* message_loop, MediaResourceLoaderBridgeFactory* factory)
: BufferedDataSource(message_loop, factory) {
}
private:
friend class media::FilterFactoryImpl2<
MockBufferedDataSource,
MessageLoop*,
MediaResourceLoaderBridgeFactory*>;
DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource);
};
class BufferedDataSourceTest : public testing::Test {
public:
BufferedDataSourceTest() {
message_loop_ = MessageLoop::current();
bridge_factory_.reset(
new StrictMock<MockMediaResourceLoaderBridgeFactory>());
factory_ = MockBufferedDataSource::CreateFactory(message_loop_,
bridge_factory_.get());
// Prepare test data.
for (size_t i = 0; i < sizeof(data_); ++i) {
data_[i] = i;
}
}
virtual ~BufferedDataSourceTest() {
if (data_source_) {
// Release the bridge factory because we don't own it.
// Expects bridge factory to be destroyed along with data source.
EXPECT_CALL(*bridge_factory_, OnDestroy())
.WillOnce(Invoke(this,
&BufferedDataSourceTest::ReleaseBridgeFactory));
}
}
void ExpectCreateAndStartResourceLoader(int start_error) {
EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
.WillOnce(Return(loader_.get()));
EXPECT_CALL(*loader_, Start(NotNull(), NotNull()))
.WillOnce(
DoAll(Assign(&error_, start_error),
Invoke(this,
&BufferedDataSourceTest::InvokeStartCallback)));
}
void InitializeDataSource(const char* url, int error,
bool partial_response, int64 instance_size,
NetworkState networkState) {
// Saves the url first.
gurl_ = GURL(url);
media::MediaFormat url_format;
url_format.SetAsString(media::MediaFormat::kMimeType,
media::mime_type::kURL);
url_format.SetAsString(media::MediaFormat::kURL, url);
data_source_ = factory_->Create<MockBufferedDataSource>(url_format);
CHECK(data_source_);
// There is no need to provide a message loop to data source.
data_source_->set_host(&host_);
scoped_refptr<NiceMock<MockBufferedResourceLoader> > first_loader =
new NiceMock<MockBufferedResourceLoader>();
// Creates the mock loader to be injected.
loader_ = first_loader;
bool initialized_ok = (error == net::OK);
bool loaded = networkState == LOADED;
{
InSequence s;
ExpectCreateAndStartResourceLoader(error);
// In the case of an invalid partial response we expect a second loader
// to be created.
if (partial_response && (error == net::ERR_INVALID_RESPONSE)) {
// Verify that the initial loader is stopped.
EXPECT_CALL(*loader_, Stop());
// Replace loader_ with a new instance.
loader_ = new NiceMock<MockBufferedResourceLoader>();
// Create and start Make sure Start() is called the new loader.
ExpectCreateAndStartResourceLoader(net::OK);
// Update initialization variable since we know the second loader will
// return OK.
initialized_ok = true;
}
}
StrictMock<media::MockFilterCallback> callback;
EXPECT_CALL(*loader_, instance_size())
.WillRepeatedly(Return(instance_size));
EXPECT_CALL(*loader_, partial_response())
.WillRepeatedly(Return(partial_response));
if (initialized_ok) {
// Expected loaded or not.
EXPECT_CALL(host_, SetLoaded(loaded));
// TODO(hclam): The condition for streaming needs to be adjusted.
if (instance_size != -1 && (loaded || partial_response)) {
EXPECT_CALL(host_, SetTotalBytes(instance_size));
if (loaded)
EXPECT_CALL(host_, SetBufferedBytes(instance_size));
else
EXPECT_CALL(host_, SetBufferedBytes(0));
} else {
EXPECT_CALL(host_, SetStreaming(true));
}
EXPECT_CALL(callback, OnFilterCallback());
EXPECT_CALL(callback, OnCallbackDestroyed());
} else {
EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK));
EXPECT_CALL(*loader_, Stop());
EXPECT_CALL(callback, OnFilterCallback());
EXPECT_CALL(callback, OnCallbackDestroyed());
}
// Actual initialization of the data source.
data_source_->Initialize(url, callback.NewCallback());
message_loop_->RunAllPending();
if (initialized_ok) {
// Verify the size of the data source.
int64 size;
if (instance_size != -1 && (loaded || partial_response)) {
EXPECT_TRUE(data_source_->GetSize(&size));
EXPECT_EQ(instance_size, size);
} else {
EXPECT_TRUE(data_source_->IsStreaming());
}
}
}
void StopDataSource() {
if (loader_) {
InSequence s;
EXPECT_CALL(*loader_, Stop());
}
StrictMock<media::MockFilterCallback> callback;
EXPECT_CALL(callback, OnFilterCallback());
EXPECT_CALL(callback, OnCallbackDestroyed());
data_source_->Stop(callback.NewCallback());
message_loop_->RunAllPending();
}
void ReleaseBridgeFactory() {
ignore_result(bridge_factory_.release());
}
void InvokeStartCallback(
net::CompletionCallback* callback,
BufferedResourceLoader::NetworkEventCallback* network_callback) {
callback->RunWithParams(Tuple1<int>(error_));
delete callback;
// TODO(hclam): Save this callback.
delete network_callback;
}
void InvokeReadCallback(int64 position, int size, uint8* buffer,
net::CompletionCallback* callback) {
if (error_ > 0)
memcpy(buffer, data_ + static_cast<int>(position), error_);
callback->RunWithParams(Tuple1<int>(error_));
delete callback;
}
void ReadDataSourceHit(int64 position, int size, int read_size) {
EXPECT_TRUE(loader_);
InSequence s;
// Expect the read is delegated to the resource loader.
EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, read_size),
Invoke(this,
&BufferedDataSourceTest::InvokeReadCallback)));
// The read has succeeded, so read callback will be called.
EXPECT_CALL(*this, ReadCallback(read_size));
data_source_->Read(
position, size, buffer_,
NewCallback(this, &BufferedDataSourceTest::ReadCallback));
message_loop_->RunAllPending();
// Make sure data is correct.
EXPECT_EQ(0,
memcmp(buffer_, data_ + static_cast<int>(position), read_size));
}
void ReadDataSourceMiss(int64 position, int size) {
EXPECT_TRUE(loader_);
// 1. Reply with a cache miss for the read.
{
InSequence s;
EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS),
Invoke(this,
&BufferedDataSourceTest::InvokeReadCallback)));
EXPECT_CALL(*loader_, Stop());
}
// 2. Then the current loader will be stop and destroyed.
NiceMock<MockBufferedResourceLoader> *new_loader =
new NiceMock<MockBufferedResourceLoader>();
EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1))
.WillOnce(Return(new_loader));
// 3. Then the new loader will be started.
EXPECT_CALL(*new_loader, Start(NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, net::OK),
Invoke(this,
&BufferedDataSourceTest::InvokeStartCallback)));
EXPECT_CALL(*new_loader, partial_response())
.WillRepeatedly(Return(loader_->partial_response()));
// 4. Then again a read request is made to the new loader.
EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, size),
Invoke(this,
&BufferedDataSourceTest::InvokeReadCallback)));
EXPECT_CALL(*this, ReadCallback(size));
data_source_->Read(
position, size, buffer_,
NewCallback(this, &BufferedDataSourceTest::ReadCallback));
message_loop_->RunAllPending();
// Make sure data is correct.
EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size));
loader_ = new_loader;
}
void ReadDataSourceFailed(int64 position, int size, int error) {
EXPECT_TRUE(loader_);
// 1. Expect the read is delegated to the resource loader.
EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, error),
Invoke(this,
&BufferedDataSourceTest::InvokeReadCallback)));
// 2. Host will then receive an error.
EXPECT_CALL(*loader_, Stop());
// 3. The read has failed, so read callback will be called.
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
data_source_->Read(
position, size, buffer_,
NewCallback(this, &BufferedDataSourceTest::ReadCallback));
message_loop_->RunAllPending();
}
void ReadDataSourceTimesOut(int64 position, int size) {
// 1. Drop the request and let it times out.
{
InSequence s;
EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
.WillOnce(DeleteArg<3>());
EXPECT_CALL(*loader_, Stop());
}
// 2. Then the current loader will be stop and destroyed.
NiceMock<MockBufferedResourceLoader> *new_loader =
new NiceMock<MockBufferedResourceLoader>();
EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1))
.WillOnce(Return(new_loader));
// 3. Then the new loader will be started and respond to queries about
// whether this is a partial response using the value of the previous
// loader.
EXPECT_CALL(*new_loader, Start(NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, net::OK),
Invoke(this,
&BufferedDataSourceTest::InvokeStartCallback)));
EXPECT_CALL(*new_loader, partial_response())
.WillRepeatedly(Return(loader_->partial_response()));
// 4. Then again a read request is made to the new loader.
EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
.WillOnce(DoAll(Assign(&error_, size),
Invoke(this,
&BufferedDataSourceTest::InvokeReadCallback),
InvokeWithoutArgs(message_loop_,
&MessageLoop::Quit)));
EXPECT_CALL(*this, ReadCallback(size));
data_source_->Read(
position, size, buffer_,
NewCallback(this, &BufferedDataSourceTest::ReadCallback));
// This blocks the current thread until the watch task is executed and
// triggers a read callback to quit this message loop.
message_loop_->Run();
// Make sure data is correct.
EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size));
loader_ = new_loader;
}
MOCK_METHOD1(ReadCallback, void(size_t size));
scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> >
bridge_factory_;
scoped_refptr<NiceMock<MockBufferedResourceLoader> > loader_;
scoped_refptr<MockBufferedDataSource> data_source_;
scoped_refptr<media::FilterFactory> factory_;
StrictMock<media::MockFilterHost> host_;
GURL gurl_;
MessageLoop* message_loop_;
int error_;
uint8 buffer_[1024];
uint8 data_[1024];
private:
DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
};
TEST_F(BufferedDataSourceTest, InitializationSuccess) {
InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, InitiailizationFailed) {
InitializeDataSource(kHttpUrl, net::ERR_FILE_NOT_FOUND, false, 0, NONE);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, MissingContentLength) {
InitializeDataSource(kHttpUrl, net::OK, true, -1, LOADING);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, RangeRequestNotSupported) {
InitializeDataSource(kHttpUrl, net::OK, false, 1024, LOADING);
StopDataSource();
}
// Test the case where we get a 206 response, but no Content-Range header.
TEST_F(BufferedDataSourceTest, MissingContentRange) {
InitializeDataSource(kHttpUrl, net::ERR_INVALID_RESPONSE, true, 1024,
LOADING);
StopDataSource();
}
TEST_F(BufferedDataSourceTest,
MissingContentLengthAndRangeRequestNotSupported) {
InitializeDataSource(kHttpUrl, net::OK, false, -1, LOADING);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, ReadCacheHit) {
InitializeDataSource(kHttpUrl, net::OK, true, 25, LOADING);
// Performs read with cache hit.
ReadDataSourceHit(10, 10, 10);
// Performs read with cache hit but partially filled.
ReadDataSourceHit(20, 10, 5);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, ReadCacheMiss) {
InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
ReadDataSourceMiss(1000, 10);
ReadDataSourceMiss(20, 10);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, ReadFailed) {
InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
ReadDataSourceHit(10, 10, 10);
ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, ReadTimesOut) {
InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
ReadDataSourceTimesOut(20, 10);
StopDataSource();
}
TEST_F(BufferedDataSourceTest, FileHasLoadedState) {
InitializeDataSource(kFileUrl, net::OK, true, 1024, LOADED);
ReadDataSourceTimesOut(20, 10);
StopDataSource();
}
} // namespace webkit_glue
|