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

Side by Side Diff: webkit/glue/media/buffered_data_source_unittest.cc

Issue 164361: Refcounting BufferedResourceLoader (Closed)
Patch Set: done Created 11 years, 4 months 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
« no previous file with comments | « webkit/glue/media/buffered_data_source.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "media/base/filters.h" 8 #include "media/base/filters.h"
9 #include "media/base/mock_filter_host.h" 9 #include "media/base/mock_filter_host.h"
10 #include "media/base/mock_filters.h" 10 #include "media/base/mock_filters.h"
(...skipping 18 matching lines...) Expand all
29 29
30 namespace { 30 namespace {
31 31
32 const char* kHttpUrl = "http://test"; 32 const char* kHttpUrl = "http://test";
33 const int kDataSize = 1024; 33 const int kDataSize = 1024;
34 34
35 } // namespace 35 } // namespace
36 36
37 namespace webkit_glue { 37 namespace webkit_glue {
38 38
39 // Submit a request completed event to the resource loader due to request
40 // being canceled. Pretending the event is from external.
41 ACTION_P(RequestCanceled, loader) {
42 URLRequestStatus status;
43 status.set_status(URLRequestStatus::CANCELED);
44 loader->OnCompletedRequest(status, "");
45 }
46
39 class BufferedResourceLoaderTest : public testing::Test { 47 class BufferedResourceLoaderTest : public testing::Test {
40 public: 48 public:
41 BufferedResourceLoaderTest() { 49 BufferedResourceLoaderTest() {
42 bridge_.reset(new StrictMock<MockResourceLoaderBridge>()); 50 bridge_.reset(new StrictMock<MockResourceLoaderBridge>());
43 51
44 for (int i = 0; i < kDataSize; ++i) 52 for (int i = 0; i < kDataSize; ++i)
45 data_[i] = i; 53 data_[i] = i;
46 } 54 }
47 55
48 ~BufferedResourceLoaderTest() { 56 ~BufferedResourceLoaderTest() {
49 if (bridge_.get()) 57 if (bridge_.get())
50 EXPECT_CALL(*bridge_, OnDestroy()); 58 EXPECT_CALL(*bridge_, OnDestroy());
51 EXPECT_CALL(bridge_factory_, OnDestroy()); 59 EXPECT_CALL(bridge_factory_, OnDestroy());
52 } 60 }
53 61
54 void Initialize(const char* url, int first_position, int last_position) { 62 void Initialize(const char* url, int first_position, int last_position) {
55 gurl_ = GURL(url); 63 gurl_ = GURL(url);
56 first_position_ = first_position; 64 first_position_ = first_position;
57 last_position_ = last_position; 65 last_position_ = last_position;
58 66
59 loader_.reset(new BufferedResourceLoader(&bridge_factory_, gurl_, 67 loader_ = new BufferedResourceLoader(&bridge_factory_, gurl_,
60 first_position_, last_position_)); 68 first_position_, last_position_);
61 EXPECT_EQ(gurl_.spec(), loader_->GetURLForDebugging()); 69 EXPECT_EQ(gurl_.spec(), loader_->GetURLForDebugging());
62 } 70 }
63 71
64 void Start() { 72 void Start() {
65 InSequence s; 73 InSequence s;
66 EXPECT_CALL(bridge_factory_, 74 EXPECT_CALL(bridge_factory_,
67 CreateBridge(gurl_, _, first_position_, last_position_)) 75 CreateBridge(gurl_, _, first_position_, last_position_))
68 .WillOnce(Return(bridge_.get())); 76 .WillOnce(Return(bridge_.get()));
69 EXPECT_CALL(*bridge_, Start(loader_.get())); 77 EXPECT_CALL(*bridge_, Start(loader_.get()));
70 loader_->Start(NewCallback(this, 78 loader_->Start(NewCallback(this,
(...skipping 25 matching lines...) Expand all
96 replace(header.begin(), header.end(), '\n', '\0'); 104 replace(header.begin(), header.end(), '\n', '\0');
97 info.headers = new net::HttpResponseHeaders(header); 105 info.headers = new net::HttpResponseHeaders(header);
98 info.content_length = content_length; 106 info.content_length = content_length;
99 loader_->OnReceivedResponse(info, false); 107 loader_->OnReceivedResponse(info, false);
100 EXPECT_EQ(content_length, loader_->content_length()); 108 EXPECT_EQ(content_length, loader_->content_length());
101 EXPECT_EQ(instance_size, loader_->instance_size()); 109 EXPECT_EQ(instance_size, loader_->instance_size());
102 } 110 }
103 111
104 void StopWhenLoad() { 112 void StopWhenLoad() {
105 InSequence s; 113 InSequence s;
106 EXPECT_CALL(*bridge_, Cancel()); 114 EXPECT_CALL(*bridge_, Cancel())
115 .WillOnce(RequestCanceled(loader_));
107 EXPECT_CALL(*bridge_, OnDestroy()) 116 EXPECT_CALL(*bridge_, OnDestroy())
108 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 117 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
109 loader_->Stop(); 118 loader_->Stop();
110 } 119 }
111 120
112 void ReleaseBridge() { 121 void ReleaseBridge() {
113 bridge_.release(); 122 bridge_.release();
114 } 123 }
115 124
116 // Helper method to write to |loader_| from |data_|. 125 // Helper method to write to |loader_| from |data_|.
117 void WriteLoader(int position, int size) { 126 void WriteLoader(int position, int size) {
118 loader_->OnReceivedData(reinterpret_cast<char*>(data_ + position), size); 127 loader_->OnReceivedData(reinterpret_cast<char*>(data_ + position), size);
(...skipping 11 matching lines...) Expand all
130 } 139 }
131 140
132 MOCK_METHOD1(StartCallback, void(int error)); 141 MOCK_METHOD1(StartCallback, void(int error));
133 MOCK_METHOD1(ReadCallback, void(int error)); 142 MOCK_METHOD1(ReadCallback, void(int error));
134 143
135 protected: 144 protected:
136 GURL gurl_; 145 GURL gurl_;
137 int64 first_position_; 146 int64 first_position_;
138 int64 last_position_; 147 int64 last_position_;
139 148
140 scoped_ptr<BufferedResourceLoader> loader_; 149 scoped_refptr<BufferedResourceLoader> loader_;
141 StrictMock<MockMediaResourceLoaderBridgeFactory> bridge_factory_; 150 StrictMock<MockMediaResourceLoaderBridgeFactory> bridge_factory_;
142 scoped_ptr<StrictMock<MockResourceLoaderBridge> > bridge_; 151 scoped_ptr<StrictMock<MockResourceLoaderBridge> > bridge_;
143 152
144 uint8 data_[kDataSize]; 153 uint8 data_[kDataSize];
145 154
146 private: 155 private:
147 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest); 156 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest);
148 }; 157 };
149 158
150 TEST_F(BufferedResourceLoaderTest, StartStop) { 159 TEST_F(BufferedResourceLoaderTest, StartStop) {
151 Initialize(kHttpUrl, -1, -1); 160 Initialize(kHttpUrl, -1, -1);
152 Start(); 161 Start();
153 StopWhenLoad(); 162 StopWhenLoad();
154 } 163 }
155 164
156 // Tests that HTTP header is missing in the response. 165 // Tests that HTTP header is missing in the response.
157 TEST_F(BufferedResourceLoaderTest, MissingHttpHeader) { 166 TEST_F(BufferedResourceLoaderTest, MissingHttpHeader) {
158 Initialize(kHttpUrl, -1, -1); 167 Initialize(kHttpUrl, -1, -1);
159 Start(); 168 Start();
160 169
161 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); 170 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE));
162 EXPECT_CALL(*bridge_, Cancel()); 171 EXPECT_CALL(*bridge_, Cancel())
172 .WillOnce(RequestCanceled(loader_));
163 EXPECT_CALL(*bridge_, OnDestroy()) 173 EXPECT_CALL(*bridge_, OnDestroy())
164 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 174 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
165 175
166 ResourceLoaderBridge::ResponseInfo info; 176 ResourceLoaderBridge::ResponseInfo info;
167 loader_->OnReceivedResponse(info, false); 177 loader_->OnReceivedResponse(info, false);
168 } 178 }
169 179
170 // Tests that a bad HTTP response is recived, e.g. file not found. 180 // Tests that a bad HTTP response is recived, e.g. file not found.
171 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { 181 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) {
172 Initialize(kHttpUrl, -1, -1); 182 Initialize(kHttpUrl, -1, -1);
173 Start(); 183 Start();
174 184
175 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); 185 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED));
176 EXPECT_CALL(*bridge_, Cancel()); 186 EXPECT_CALL(*bridge_, Cancel())
187 .WillOnce(RequestCanceled(loader_));
177 EXPECT_CALL(*bridge_, OnDestroy()) 188 EXPECT_CALL(*bridge_, OnDestroy())
178 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 189 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
179 190
180 ResourceLoaderBridge::ResponseInfo info; 191 ResourceLoaderBridge::ResponseInfo info;
181 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n"); 192 info.headers = new net::HttpResponseHeaders("HTTP/1.1 404 Bot Found\n");
182 loader_->OnReceivedResponse(info, false); 193 loader_->OnReceivedResponse(info, false);
183 } 194 }
184 195
185 // Tests that partial content is requested but not fulfilled. 196 // Tests that partial content is requested but not fulfilled.
186 TEST_F(BufferedResourceLoaderTest, NotPartialRange) { 197 TEST_F(BufferedResourceLoaderTest, NotPartialRange) {
187 Initialize(kHttpUrl, 100, -1); 198 Initialize(kHttpUrl, 100, -1);
188 Start(); 199 Start();
189 200
190 EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 201 EXPECT_CALL(*this, StartCallback(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
191 EXPECT_CALL(*bridge_, Cancel()); 202 EXPECT_CALL(*bridge_, Cancel())
203 .WillOnce(RequestCanceled(loader_));
192 EXPECT_CALL(*bridge_, OnDestroy()) 204 EXPECT_CALL(*bridge_, OnDestroy())
193 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 205 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
194 206
195 ResourceLoaderBridge::ResponseInfo info; 207 ResourceLoaderBridge::ResponseInfo info;
196 info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n"); 208 info.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK\n");
197 loader_->OnReceivedResponse(info, false); 209 loader_->OnReceivedResponse(info, false);
198 } 210 }
199 211
200 // Tests that a 200 response is received. 212 // Tests that a 200 response is received.
201 TEST_F(BufferedResourceLoaderTest, FullResponse) { 213 TEST_F(BufferedResourceLoaderTest, FullResponse) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 ReadLoader(10, 10, buffer); 296 ReadLoader(10, 10, buffer);
285 297
286 // Writing to loader will fulfill the read request. 298 // Writing to loader will fulfill the read request.
287 EXPECT_CALL(*this, ReadCallback(10)); 299 EXPECT_CALL(*this, ReadCallback(10));
288 WriteLoader(10, 20); 300 WriteLoader(10, 20);
289 VerifyBuffer(buffer, 10, 10); 301 VerifyBuffer(buffer, 10, 10);
290 302
291 // The following call cannot be fulfilled now. 303 // The following call cannot be fulfilled now.
292 ReadLoader(25, 10, buffer); 304 ReadLoader(25, 10, buffer);
293 305
306 EXPECT_CALL(*this, ReadCallback(5));
294 EXPECT_CALL(*bridge_, OnDestroy()) 307 EXPECT_CALL(*bridge_, OnDestroy())
295 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 308 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
296 EXPECT_CALL(*this, ReadCallback(5));
297 URLRequestStatus status; 309 URLRequestStatus status;
298 status.set_status(URLRequestStatus::SUCCESS); 310 status.set_status(URLRequestStatus::SUCCESS);
299 loader_->OnCompletedRequest(status, ""); 311 loader_->OnCompletedRequest(status, "");
300 } 312 }
301 313
302 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { 314 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) {
303 Initialize(kHttpUrl, 10, 29); 315 Initialize(kHttpUrl, 10, 29);
304 Start(); 316 Start();
305 PartialResponse(30); 317 PartialResponse(30);
306 318
307 uint8 buffer[10]; 319 uint8 buffer[10];
308 InSequence s; 320 InSequence s;
309 321
310 ReadLoader(10, 10, buffer); 322 ReadLoader(10, 10, buffer);
323 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED));
311 EXPECT_CALL(*bridge_, OnDestroy()) 324 EXPECT_CALL(*bridge_, OnDestroy())
312 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge)); 325 .WillOnce(Invoke(this, &BufferedResourceLoaderTest::ReleaseBridge));
313 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED));
314 URLRequestStatus status; 326 URLRequestStatus status;
315 status.set_status(URLRequestStatus::FAILED); 327 status.set_status(URLRequestStatus::FAILED);
316 loader_->OnCompletedRequest(status, ""); 328 loader_->OnCompletedRequest(status, "");
317 } 329 }
318 330
319 // TODO(hclam): add unit test for defer loading. 331 // TODO(hclam): add unit test for defer loading.
320 332
321 class MockBufferedResourceLoader : public BufferedResourceLoader { 333 class MockBufferedResourceLoader : public BufferedResourceLoader {
322 public: 334 public:
323 MockBufferedResourceLoader() : BufferedResourceLoader() { 335 MockBufferedResourceLoader() : BufferedResourceLoader() {
324 } 336 }
325 337
326 ~MockBufferedResourceLoader() {
327 OnDestroy();
328 }
329
330 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback)); 338 MOCK_METHOD1(Start, void(net::CompletionCallback* read_callback));
331 MOCK_METHOD0(Stop, void()); 339 MOCK_METHOD0(Stop, void());
332 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, 340 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer,
333 net::CompletionCallback* callback)); 341 net::CompletionCallback* callback));
334 MOCK_METHOD0(content_length, int64()); 342 MOCK_METHOD0(content_length, int64());
335 MOCK_METHOD0(instance_size, int64()); 343 MOCK_METHOD0(instance_size, int64());
336 MOCK_METHOD0(OnDestroy, void());
337 344
338 private: 345 private:
339 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); 346 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader);
340 }; 347 };
341 348
342 // A mock BufferedDataSource to inject mock BufferedResourceLoader through 349 // A mock BufferedDataSource to inject mock BufferedResourceLoader through
343 // CreateLoader() method. 350 // CreateLoader() method.
344 class MockBufferedDataSource : public BufferedDataSource { 351 class MockBufferedDataSource : public BufferedDataSource {
345 public: 352 public:
346 // Static methods for creating this class. 353 // Static methods for creating this class.
(...skipping 30 matching lines...) Expand all
377 384
378 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); 385 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource);
379 }; 386 };
380 387
381 class BufferedDataSourceTest : public testing::Test { 388 class BufferedDataSourceTest : public testing::Test {
382 public: 389 public:
383 BufferedDataSourceTest() { 390 BufferedDataSourceTest() {
384 message_loop_.reset(MessageLoop::current()); 391 message_loop_.reset(MessageLoop::current());
385 bridge_factory_.reset( 392 bridge_factory_.reset(
386 new StrictMock<MockMediaResourceLoaderBridgeFactory>()); 393 new StrictMock<MockMediaResourceLoaderBridgeFactory>());
387 ReleaseLoader();
388 factory_ = MockBufferedDataSource::CreateFactory(message_loop_.get(), 394 factory_ = MockBufferedDataSource::CreateFactory(message_loop_.get(),
389 bridge_factory_.get()); 395 bridge_factory_.get());
390 396
391 // Prepare test data. 397 // Prepare test data.
392 for (size_t i = 0; i < sizeof(data_); ++i) { 398 for (size_t i = 0; i < sizeof(data_); ++i) {
393 data_[i] = i; 399 data_[i] = i;
394 } 400 }
395 } 401 }
396 402
397 ~BufferedDataSourceTest() { 403 ~BufferedDataSourceTest() {
(...skipping 18 matching lines...) Expand all
416 url_format.SetAsString(media::MediaFormat::kMimeType, 422 url_format.SetAsString(media::MediaFormat::kMimeType,
417 media::mime_type::kURL); 423 media::mime_type::kURL);
418 url_format.SetAsString(media::MediaFormat::kURL, url); 424 url_format.SetAsString(media::MediaFormat::kURL, url);
419 data_source_ = factory_->Create<MockBufferedDataSource>(url_format); 425 data_source_ = factory_->Create<MockBufferedDataSource>(url_format);
420 CHECK(data_source_); 426 CHECK(data_source_);
421 427
422 // There is no need to provide a message loop to data source. 428 // There is no need to provide a message loop to data source.
423 data_source_->set_host(&host_); 429 data_source_->set_host(&host_);
424 430
425 // Creates the first mock loader to be injected. 431 // Creates the first mock loader to be injected.
426 loader_.reset(new StrictMock<MockBufferedResourceLoader>()); 432 loader_ = new StrictMock<MockBufferedResourceLoader>();
427 probe_loader_.reset(new StrictMock<MockBufferedResourceLoader>()); 433 probe_loader_ = new StrictMock<MockBufferedResourceLoader>();
428 434
429 InSequence s; 435 InSequence s;
430 StrictMock<media::MockFilterCallback> callback; 436 StrictMock<media::MockFilterCallback> callback;
431 437
432 // There is one resource loader with full range will be created. 438 // There is one resource loader with full range will be created.
433 EXPECT_CALL(*data_source_, CreateLoader(-1, -1)) 439 EXPECT_CALL(*data_source_, CreateLoader(-1, -1))
434 .WillOnce(Return(loader_.get())); 440 .WillOnce(Return(loader_.get()));
435 441
436 // Then another resource loader with a small partial range is created. 442 // Then another resource loader with a small partial range is created.
437 EXPECT_CALL(*data_source_, CreateLoader(1, 1)) 443 EXPECT_CALL(*data_source_, CreateLoader(1, 1))
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 } 502 }
497 503
498 // Verify the data source is streamed if the probe has received an error. 504 // Verify the data source is streamed if the probe has received an error.
499 if (probe_error != net::OK) { 505 if (probe_error != net::OK) {
500 EXPECT_TRUE(data_source_->IsStreaming()); 506 EXPECT_TRUE(data_source_->IsStreaming());
501 } 507 }
502 } 508 }
503 } 509 }
504 510
505 void StopDataSource() { 511 void StopDataSource() {
506 if (loader_.get()) { 512 if (loader_) {
507 InSequence s; 513 InSequence s;
508 EXPECT_CALL(*loader_, Stop()); 514 EXPECT_CALL(*loader_, Stop());
509 EXPECT_CALL(*probe_loader_, Stop()); 515 EXPECT_CALL(*probe_loader_, Stop());
510 } 516 }
511 517
512 EXPECT_CALL(*loader_, OnDestroy())
513 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader));
514 EXPECT_CALL(*probe_loader_, OnDestroy())
515 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseProbeLoader));
516
517 data_source_->Stop(); 518 data_source_->Stop();
518 message_loop_->RunAllPending(); 519 message_loop_->RunAllPending();
519 } 520 }
520 521
521 void ReleaseBridgeFactory() { 522 void ReleaseBridgeFactory() {
522 bridge_factory_.release(); 523 bridge_factory_.release();
523 } 524 }
524 525
525 void ReleaseLoader() {
526 loader_.release();
527 }
528
529 void ReleaseProbeLoader() {
530 probe_loader_.release();
531 }
532
533 void InvokeStartCallback(net::CompletionCallback* callback) { 526 void InvokeStartCallback(net::CompletionCallback* callback) {
534 callback->RunWithParams(Tuple1<int>(error_)); 527 callback->RunWithParams(Tuple1<int>(error_));
535 delete callback; 528 delete callback;
536 } 529 }
537 530
538 void InvokeReadCallback(int64 position, int size, uint8* buffer, 531 void InvokeReadCallback(int64 position, int size, uint8* buffer,
539 net::CompletionCallback* callback) { 532 net::CompletionCallback* callback) {
540 if (error_ > 0) 533 if (error_ > 0)
541 memcpy(buffer, data_ + static_cast<int>(position), error_); 534 memcpy(buffer, data_ + static_cast<int>(position), error_);
542 callback->RunWithParams(Tuple1<int>(error_)); 535 callback->RunWithParams(Tuple1<int>(error_));
543 delete callback; 536 delete callback;
544 } 537 }
545 538
546 void ReadDataSourceHit(int64 position, int size, int read_size) { 539 void ReadDataSourceHit(int64 position, int size, int read_size) {
547 EXPECT_TRUE(loader_.get() != NULL); 540 EXPECT_TRUE(loader_);
548 541
549 InSequence s; 542 InSequence s;
550 // Expect the read is delegated to the resource loader. 543 // Expect the read is delegated to the resource loader.
551 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 544 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
552 .WillOnce(DoAll(Assign(&error_, read_size), 545 .WillOnce(DoAll(Assign(&error_, read_size),
553 Invoke(this, 546 Invoke(this,
554 &BufferedDataSourceTest::InvokeReadCallback))); 547 &BufferedDataSourceTest::InvokeReadCallback)));
555 548
556 // The read has succeeded, so read callback will be called. 549 // The read has succeeded, so read callback will be called.
557 EXPECT_CALL(*this, ReadCallback(read_size)); 550 EXPECT_CALL(*this, ReadCallback(read_size));
558 551
559 data_source_->Read( 552 data_source_->Read(
560 position, size, buffer_, 553 position, size, buffer_,
561 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); 554 NewCallback(this, &BufferedDataSourceTest::ReadCallback));
562 message_loop_->RunAllPending(); 555 message_loop_->RunAllPending();
563 556
564 // Make sure data is correct. 557 // Make sure data is correct.
565 EXPECT_EQ(0, 558 EXPECT_EQ(0,
566 memcmp(buffer_, data_ + static_cast<int>(position), read_size)); 559 memcmp(buffer_, data_ + static_cast<int>(position), read_size));
567 } 560 }
568 561
569 void ReadDataSourceMiss(int64 position, int size) { 562 void ReadDataSourceMiss(int64 position, int size) {
570 EXPECT_TRUE(loader_.get() != NULL); 563 EXPECT_TRUE(loader_);
571 564
572 InSequence s; 565 InSequence s;
573 // 1. Reply with a cache miss for the read. 566 // 1. Reply with a cache miss for the read.
574 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 567 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
575 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), 568 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS),
576 Invoke(this, 569 Invoke(this,
577 &BufferedDataSourceTest::InvokeReadCallback))); 570 &BufferedDataSourceTest::InvokeReadCallback)));
578 571
579 // 2. Then the current loader will be stop and destroyed. 572 // 2. Then the current loader will be stop and destroyed.
580 StrictMock<MockBufferedResourceLoader> *new_loader = 573 StrictMock<MockBufferedResourceLoader> *new_loader =
581 new StrictMock<MockBufferedResourceLoader>(); 574 new StrictMock<MockBufferedResourceLoader>();
582 EXPECT_CALL(*loader_, Stop()); 575 EXPECT_CALL(*loader_, Stop());
583 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) 576 EXPECT_CALL(*data_source_, CreateLoader(position, -1))
584 .WillOnce(Return(new_loader)); 577 .WillOnce(Return(new_loader));
585 EXPECT_CALL(*loader_, OnDestroy())
586 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader));
587 578
588 // 3. Then the new loader will be started. 579 // 3. Then the new loader will be started.
589 EXPECT_CALL(*new_loader, Start(NotNull())) 580 EXPECT_CALL(*new_loader, Start(NotNull()))
590 .WillOnce(DoAll(Assign(&error_, net::OK), 581 .WillOnce(DoAll(Assign(&error_, net::OK),
591 Invoke(this, 582 Invoke(this,
592 &BufferedDataSourceTest::InvokeStartCallback))); 583 &BufferedDataSourceTest::InvokeStartCallback)));
593 584
594 // 4. Then again a read request is made to the new loader. 585 // 4. Then again a read request is made to the new loader.
595 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) 586 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
596 .WillOnce(DoAll(Assign(&error_, size), 587 .WillOnce(DoAll(Assign(&error_, size),
597 Invoke(this, 588 Invoke(this,
598 &BufferedDataSourceTest::InvokeReadCallback))); 589 &BufferedDataSourceTest::InvokeReadCallback)));
599 590
600 EXPECT_CALL(*this, ReadCallback(size)); 591 EXPECT_CALL(*this, ReadCallback(size));
601 592
602 data_source_->Read( 593 data_source_->Read(
603 position, size, buffer_, 594 position, size, buffer_,
604 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); 595 NewCallback(this, &BufferedDataSourceTest::ReadCallback));
605 message_loop_->RunAllPending(); 596 message_loop_->RunAllPending();
606 597
607 // Make sure data is correct. 598 // Make sure data is correct.
608 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); 599 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size));
609 600
610 EXPECT_TRUE(loader_.get() == NULL); 601 loader_ = new_loader;
611 loader_.reset(new_loader);
612 } 602 }
613 603
614 void ReadDataSourceFailed(int64 position, int size, int error) { 604 void ReadDataSourceFailed(int64 position, int size, int error) {
615 EXPECT_TRUE(loader_.get() != NULL); 605 EXPECT_TRUE(loader_);
616 606
617 InSequence s; 607 InSequence s;
618 // 1. Expect the read is delegated to the resource loader. 608 // 1. Expect the read is delegated to the resource loader.
619 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 609 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
620 .WillOnce(DoAll(Assign(&error_, error), 610 .WillOnce(DoAll(Assign(&error_, error),
621 Invoke(this, 611 Invoke(this,
622 &BufferedDataSourceTest::InvokeReadCallback))); 612 &BufferedDataSourceTest::InvokeReadCallback)));
623 613
624 // 2. Host will then receive an error. 614 // 2. Host will then receive an error.
625 EXPECT_CALL(*loader_, Stop()); 615 EXPECT_CALL(*loader_, Stop());
(...skipping 13 matching lines...) Expand all
639 // 1. Drop the request and let it times out. 629 // 1. Drop the request and let it times out.
640 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 630 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
641 .WillOnce(DeleteArg<3>()); 631 .WillOnce(DeleteArg<3>());
642 632
643 // 2. Then the current loader will be stop and destroyed. 633 // 2. Then the current loader will be stop and destroyed.
644 StrictMock<MockBufferedResourceLoader> *new_loader = 634 StrictMock<MockBufferedResourceLoader> *new_loader =
645 new StrictMock<MockBufferedResourceLoader>(); 635 new StrictMock<MockBufferedResourceLoader>();
646 EXPECT_CALL(*loader_, Stop()); 636 EXPECT_CALL(*loader_, Stop());
647 EXPECT_CALL(*data_source_, CreateLoader(position, -1)) 637 EXPECT_CALL(*data_source_, CreateLoader(position, -1))
648 .WillOnce(Return(new_loader)); 638 .WillOnce(Return(new_loader));
649 EXPECT_CALL(*loader_, OnDestroy())
650 .WillOnce(Invoke(this, &BufferedDataSourceTest::ReleaseLoader));
651 639
652 // 3. Then the new loader will be started. 640 // 3. Then the new loader will be started.
653 EXPECT_CALL(*new_loader, Start(NotNull())) 641 EXPECT_CALL(*new_loader, Start(NotNull()))
654 .WillOnce(DoAll(Assign(&error_, net::OK), 642 .WillOnce(DoAll(Assign(&error_, net::OK),
655 Invoke(this, 643 Invoke(this,
656 &BufferedDataSourceTest::InvokeStartCallback))); 644 &BufferedDataSourceTest::InvokeStartCallback)));
657 645
658 // 4. Then again a read request is made to the new loader. 646 // 4. Then again a read request is made to the new loader.
659 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) 647 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
660 .WillOnce(DoAll(Assign(&error_, size), 648 .WillOnce(DoAll(Assign(&error_, size),
661 Invoke(this, 649 Invoke(this,
662 &BufferedDataSourceTest::InvokeReadCallback), 650 &BufferedDataSourceTest::InvokeReadCallback),
663 InvokeWithoutArgs(message_loop_.get(), 651 InvokeWithoutArgs(message_loop_.get(),
664 &MessageLoop::Quit))); 652 &MessageLoop::Quit)));
665 653
666 EXPECT_CALL(*this, ReadCallback(size)); 654 EXPECT_CALL(*this, ReadCallback(size));
667 655
668 data_source_->Read( 656 data_source_->Read(
669 position, size, buffer_, 657 position, size, buffer_,
670 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); 658 NewCallback(this, &BufferedDataSourceTest::ReadCallback));
671 659
672 // This blocks the current thread until the watch task is executed and 660 // This blocks the current thread until the watch task is executed and
673 // triggers a read callback to quit this message loop. 661 // triggers a read callback to quit this message loop.
674 message_loop_->Run(); 662 message_loop_->Run();
675 663
676 // Make sure data is correct. 664 // Make sure data is correct.
677 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); 665 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size));
678 666
679 EXPECT_TRUE(loader_.get() == NULL); 667 loader_ = new_loader;
680 loader_.reset(new_loader);
681 } 668 }
682 669
683 MOCK_METHOD1(ReadCallback, void(size_t size)); 670 MOCK_METHOD1(ReadCallback, void(size_t size));
684 671
685 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> > 672 scoped_ptr<StrictMock<MockMediaResourceLoaderBridgeFactory> >
686 bridge_factory_; 673 bridge_factory_;
687 scoped_ptr<StrictMock<MockBufferedResourceLoader> > loader_; 674 scoped_refptr<StrictMock<MockBufferedResourceLoader> > loader_;
688 scoped_ptr<StrictMock<MockBufferedResourceLoader> > probe_loader_; 675 scoped_refptr<StrictMock<MockBufferedResourceLoader> > probe_loader_;
689 scoped_refptr<MockBufferedDataSource > data_source_; 676 scoped_refptr<MockBufferedDataSource > data_source_;
690 scoped_refptr<media::FilterFactory> factory_; 677 scoped_refptr<media::FilterFactory> factory_;
691 678
692 StrictMock<media::MockFilterHost> host_; 679 StrictMock<media::MockFilterHost> host_;
693 GURL gurl_; 680 GURL gurl_;
694 scoped_ptr<MessageLoop> message_loop_; 681 scoped_ptr<MessageLoop> message_loop_;
695 682
696 int error_; 683 int error_;
697 uint8 buffer_[1024]; 684 uint8 buffer_[1024];
698 uint8 data_[1024]; 685 uint8 data_[1024];
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 StopDataSource(); 743 StopDataSource();
757 } 744 }
758 745
759 TEST_F(BufferedDataSourceTest, ReadTimesOut) { 746 TEST_F(BufferedDataSourceTest, ReadTimesOut) {
760 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024); 747 InitializeDataSource(kHttpUrl, net::OK, net::OK, 1024);
761 ReadDataSourceTimesOut(20, 10); 748 ReadDataSourceTimesOut(20, 10);
762 StopDataSource(); 749 StopDataSource();
763 } 750 }
764 751
765 } // namespace webkit_glue 752 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/glue/media/buffered_data_source.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698