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

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

Issue 6815012: Only make Range requests when the desired range doesn't cover the whole file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix CR nits and added unit test for lying server case. Created 9 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « webkit/glue/media/buffered_data_source.cc ('k') | webkit/glue/media/buffered_resource_loader.h » ('j') | 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/test/test_timeouts.h" 7 #include "base/test/test_timeouts.h"
8 #include "media/base/mock_callback.h" 8 #include "media/base/mock_callback.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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 MOCK_METHOD3(Start, void(net::CompletionCallback* read_callback, 71 MOCK_METHOD3(Start, void(net::CompletionCallback* read_callback,
72 NetworkEventCallback* network_callback, 72 NetworkEventCallback* network_callback,
73 WebFrame* frame)); 73 WebFrame* frame));
74 MOCK_METHOD0(Stop, void()); 74 MOCK_METHOD0(Stop, void());
75 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, 75 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer,
76 net::CompletionCallback* callback)); 76 net::CompletionCallback* callback));
77 MOCK_METHOD0(content_length, int64()); 77 MOCK_METHOD0(content_length, int64());
78 MOCK_METHOD0(instance_size, int64()); 78 MOCK_METHOD0(instance_size, int64());
79 MOCK_METHOD0(partial_response, bool()); 79 MOCK_METHOD0(range_supported, bool());
80 MOCK_METHOD0(network_activity, bool()); 80 MOCK_METHOD0(network_activity, bool());
81 MOCK_METHOD0(url, const GURL&()); 81 MOCK_METHOD0(url, const GURL&());
82 MOCK_METHOD0(GetBufferedFirstBytePosition, int64()); 82 MOCK_METHOD0(GetBufferedFirstBytePosition, int64());
83 MOCK_METHOD0(GetBufferedLastBytePosition, int64()); 83 MOCK_METHOD0(GetBufferedLastBytePosition, int64());
84 84
85 protected: 85 protected:
86 ~MockBufferedResourceLoader() {} 86 ~MockBufferedResourceLoader() {}
87 87
88 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); 88 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader);
89 }; 89 };
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 } 157 }
158 } 158 }
159 159
160 // Attach a static function that deletes the memory referred by the 160 // Attach a static function that deletes the memory referred by the
161 // "callback" parameter. 161 // "callback" parameter.
162 ON_CALL(*loader_, Read(_, _, _ , _)) 162 ON_CALL(*loader_, Read(_, _, _ , _))
163 .WillByDefault(DeleteArg<3>()); 163 .WillByDefault(DeleteArg<3>());
164 164
165 ON_CALL(*loader_, instance_size()) 165 ON_CALL(*loader_, instance_size())
166 .WillByDefault(Return(instance_size)); 166 .WillByDefault(Return(instance_size));
167 ON_CALL(*loader_, partial_response()) 167
168 // range_supported() return true if we expect to get a partial response.
169 ON_CALL(*loader_, range_supported())
168 .WillByDefault(Return(partial_response)); 170 .WillByDefault(Return(partial_response));
171
169 ON_CALL(*loader_, url()) 172 ON_CALL(*loader_, url())
170 .WillByDefault(ReturnRef(gurl_)); 173 .WillByDefault(ReturnRef(gurl_));
171 media::PipelineStatus expected_init_status = media::PIPELINE_OK; 174 media::PipelineStatus expected_init_status = media::PIPELINE_OK;
172 if (initialized_ok) { 175 if (initialized_ok) {
173 // Expected loaded or not. 176 // Expected loaded or not.
174 EXPECT_CALL(host_, SetLoaded(loaded)); 177 EXPECT_CALL(host_, SetLoaded(loaded));
175 178
176 // TODO(hclam): The condition for streaming needs to be adjusted. 179 // TODO(hclam): The condition for streaming needs to be adjusted.
177 if (instance_size != -1 && (loaded || partial_response)) { 180 if (instance_size != -1 && (loaded || partial_response)) {
178 EXPECT_CALL(host_, SetTotalBytes(instance_size)); 181 EXPECT_CALL(host_, SetTotalBytes(instance_size));
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 EXPECT_CALL(*this, ReadCallback(_)); 273 EXPECT_CALL(*this, ReadCallback(_));
271 EXPECT_CALL(*loader_, Stop()); 274 EXPECT_CALL(*loader_, Stop());
272 data_source_->Abort(); 275 data_source_->Abort();
273 message_loop_->RunAllPending(); 276 message_loop_->RunAllPending();
274 277
275 // The loader has now been stopped. Set this to null so that when the 278 // The loader has now been stopped. Set this to null so that when the
276 // DataSource is stopped, it does not expect a call to stop the loader. 279 // DataSource is stopped, it does not expect a call to stop the loader.
277 loader_ = NULL; 280 loader_ = NULL;
278 } 281 }
279 282
280 void ReadDataSourceMiss(int64 position, int size) { 283 void ReadDataSourceMiss(int64 position, int size, int start_error) {
281 EXPECT_TRUE(loader_); 284 EXPECT_TRUE(loader_);
282 285
283 // 1. Reply with a cache miss for the read. 286 // 1. Reply with a cache miss for the read.
284 { 287 {
285 InSequence s; 288 InSequence s;
286 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 289 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
287 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS), 290 .WillOnce(DoAll(Assign(&error_, net::ERR_CACHE_MISS),
288 Invoke(this, 291 Invoke(this,
289 &BufferedDataSourceTest::InvokeReadCallback))); 292 &BufferedDataSourceTest::InvokeReadCallback)));
290 EXPECT_CALL(*loader_, Stop()); 293 EXPECT_CALL(*loader_, Stop());
291 } 294 }
292 295
293 // 2. Then the current loader will be stop and destroyed. 296 // 2. Then the current loader will be stop and destroyed.
294 NiceMock<MockBufferedResourceLoader> *new_loader = 297 NiceMock<MockBufferedResourceLoader> *new_loader =
295 new NiceMock<MockBufferedResourceLoader>(); 298 new NiceMock<MockBufferedResourceLoader>();
296 EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1)) 299 EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1))
297 .WillOnce(Return(new_loader)); 300 .WillOnce(Return(new_loader));
298 301
299 // 3. Then the new loader will be started. 302 // 3. Then the new loader will be started.
300 EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull())) 303 EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull()))
301 .WillOnce(DoAll(Assign(&error_, net::OK), 304 .WillOnce(DoAll(Assign(&error_, start_error),
302 Invoke(this, 305 Invoke(this,
303 &BufferedDataSourceTest::InvokeStartCallback))); 306 &BufferedDataSourceTest::InvokeStartCallback)));
304 EXPECT_CALL(*new_loader, partial_response())
305 .WillRepeatedly(Return(loader_->partial_response()));
306 307
307 // 4. Then again a read request is made to the new loader. 308 if (start_error == net::OK) {
308 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) 309 EXPECT_CALL(*new_loader, range_supported())
309 .WillOnce(DoAll(Assign(&error_, size), 310 .WillRepeatedly(Return(loader_->range_supported()));
310 Invoke(this,
311 &BufferedDataSourceTest::InvokeReadCallback)));
312 311
313 EXPECT_CALL(*this, ReadCallback(size)); 312 // 4a. Then again a read request is made to the new loader.
313 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
314 .WillOnce(DoAll(Assign(&error_, size),
315 Invoke(this,
316 &BufferedDataSourceTest::InvokeReadCallback)));
317
318 EXPECT_CALL(*this, ReadCallback(size));
319 } else {
320 // 4b. The read callback is called with an error because Start() on the
321 // new loader returned an error.
322 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
323 }
314 324
315 data_source_->Read( 325 data_source_->Read(
316 position, size, buffer_, 326 position, size, buffer_,
317 NewCallback(this, &BufferedDataSourceTest::ReadCallback)); 327 NewCallback(this, &BufferedDataSourceTest::ReadCallback));
318 message_loop_->RunAllPending(); 328 message_loop_->RunAllPending();
319 329
320 // Make sure data is correct. 330 // Make sure data is correct.
321 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size)); 331 if (start_error == net::OK)
332 EXPECT_EQ(0, memcmp(buffer_, data_ + static_cast<int>(position), size));
322 333
323 loader_ = new_loader; 334 loader_ = new_loader;
324 } 335 }
325 336
326 void ReadDataSourceFailed(int64 position, int size, int error) { 337 void ReadDataSourceFailed(int64 position, int size, int error) {
327 EXPECT_TRUE(loader_); 338 EXPECT_TRUE(loader_);
328 339
329 // 1. Expect the read is delegated to the resource loader. 340 // 1. Expect the read is delegated to the resource loader.
330 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull())) 341 EXPECT_CALL(*loader_, Read(position, size, NotNull(), NotNull()))
331 .WillOnce(DoAll(Assign(&error_, error), 342 .WillOnce(DoAll(Assign(&error_, error),
(...skipping 28 matching lines...) Expand all
360 EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1)) 371 EXPECT_CALL(*data_source_, CreateResourceLoader(position, -1))
361 .WillOnce(Return(new_loader)); 372 .WillOnce(Return(new_loader));
362 373
363 // 3. Then the new loader will be started and respond to queries about 374 // 3. Then the new loader will be started and respond to queries about
364 // whether this is a partial response using the value of the previous 375 // whether this is a partial response using the value of the previous
365 // loader. 376 // loader.
366 EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull())) 377 EXPECT_CALL(*new_loader, Start(NotNull(), NotNull(), NotNull()))
367 .WillOnce(DoAll(Assign(&error_, net::OK), 378 .WillOnce(DoAll(Assign(&error_, net::OK),
368 Invoke(this, 379 Invoke(this,
369 &BufferedDataSourceTest::InvokeStartCallback))); 380 &BufferedDataSourceTest::InvokeStartCallback)));
370 EXPECT_CALL(*new_loader, partial_response()) 381 EXPECT_CALL(*new_loader, range_supported())
371 .WillRepeatedly(Return(loader_->partial_response())); 382 .WillRepeatedly(Return(loader_->range_supported()));
372 383
373 // 4. Then again a read request is made to the new loader. 384 // 4. Then again a read request is made to the new loader.
374 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull())) 385 EXPECT_CALL(*new_loader, Read(position, size, NotNull(), NotNull()))
375 .WillOnce(DoAll(Assign(&error_, size), 386 .WillOnce(DoAll(Assign(&error_, size),
376 Invoke(this, 387 Invoke(this,
377 &BufferedDataSourceTest::InvokeReadCallback), 388 &BufferedDataSourceTest::InvokeReadCallback),
378 InvokeWithoutArgs(message_loop_, 389 InvokeWithoutArgs(message_loop_,
379 &MessageLoop::Quit))); 390 &MessageLoop::Quit)));
380 391
381 EXPECT_CALL(*this, ReadCallback(size)); 392 EXPECT_CALL(*this, ReadCallback(size));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 ReadDataSourceHit(10, 10, 10); 463 ReadDataSourceHit(10, 10, 10);
453 464
454 // Performs read with cache hit but partially filled. 465 // Performs read with cache hit but partially filled.
455 ReadDataSourceHit(20, 10, 5); 466 ReadDataSourceHit(20, 10, 5);
456 467
457 StopDataSource(); 468 StopDataSource();
458 } 469 }
459 470
460 TEST_F(BufferedDataSourceTest, ReadCacheMiss) { 471 TEST_F(BufferedDataSourceTest, ReadCacheMiss) {
461 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); 472 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
462 ReadDataSourceMiss(1000, 10); 473 ReadDataSourceMiss(1000, 10, net::OK);
463 ReadDataSourceMiss(20, 10); 474 ReadDataSourceMiss(20, 10, net::OK);
464 StopDataSource(); 475 StopDataSource();
465 } 476 }
466 477
478 // Test the case where the initial response from the server indicates that
479 // Range requests are supported, but a later request prove otherwise.
480 TEST_F(BufferedDataSourceTest, ServerLiesAboutRangeSupport) {
481 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
482 ReadDataSourceHit(10, 10, 10);
483 ReadDataSourceMiss(1000, 10, net::ERR_INVALID_RESPONSE);
484 StopDataSource();
485 }
486
467 TEST_F(BufferedDataSourceTest, ReadHang) { 487 TEST_F(BufferedDataSourceTest, ReadHang) {
468 InitializeDataSource(kHttpUrl, net::OK, true, 25, LOADING); 488 InitializeDataSource(kHttpUrl, net::OK, true, 25, LOADING);
469 ReadDataSourceHang(10, 10); 489 ReadDataSourceHang(10, 10);
470 StopDataSource(); 490 StopDataSource();
471 } 491 }
472 492
473 TEST_F(BufferedDataSourceTest, ReadFailed) { 493 TEST_F(BufferedDataSourceTest, ReadFailed) {
474 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING); 494 InitializeDataSource(kHttpUrl, net::OK, true, 1024, LOADING);
475 ReadDataSourceHit(10, 10, 10); 495 ReadDataSourceHit(10, 10, 10);
476 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET); 496 ReadDataSourceFailed(10, 10, net::ERR_CONNECTION_RESET);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 data_source_->Stop(media::NewExpectedCallback()); 569 data_source_->Stop(media::NewExpectedCallback());
550 570
551 // Allow cleanup task to run. 571 // Allow cleanup task to run.
552 message_loop_->RunAllPending(); 572 message_loop_->RunAllPending();
553 573
554 // Verify that Read() was not called on the loader. 574 // Verify that Read() was not called on the loader.
555 EXPECT_FALSE(read_called); 575 EXPECT_FALSE(read_called);
556 } 576 }
557 577
558 } // namespace webkit_glue 578 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/glue/media/buffered_data_source.cc ('k') | webkit/glue/media/buffered_resource_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698