| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 GURL gurl(url); | 244 GURL gurl(url); |
| 245 data_source_.reset(new MockMultibufferDataSource( | 245 data_source_.reset(new MockMultibufferDataSource( |
| 246 gurl, cors_mode, message_loop_.task_runner(), url_index_, | 246 gurl, cors_mode, message_loop_.task_runner(), url_index_, |
| 247 view_->mainFrame()->toWebLocalFrame(), &host_)); | 247 view_->mainFrame()->toWebLocalFrame(), &host_)); |
| 248 data_source_->SetPreload(preload_); | 248 data_source_->SetPreload(preload_); |
| 249 | 249 |
| 250 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 250 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
| 251 EXPECT_CALL(*this, OnInitialize(expected)); | 251 EXPECT_CALL(*this, OnInitialize(expected)); |
| 252 data_source_->Initialize(base::Bind( | 252 data_source_->Initialize(base::Bind( |
| 253 &MultibufferDataSourceTest::OnInitialize, base::Unretained(this))); | 253 &MultibufferDataSourceTest::OnInitialize, base::Unretained(this))); |
| 254 message_loop_.RunUntilIdle(); | 254 base::RunLoop().RunUntilIdle(); |
| 255 | 255 |
| 256 // Not really loading until after OnInitialize is called. | 256 // Not really loading until after OnInitialize is called. |
| 257 EXPECT_EQ(data_source_->downloading(), false); | 257 EXPECT_EQ(data_source_->downloading(), false); |
| 258 } | 258 } |
| 259 | 259 |
| 260 void Initialize(const char* url, bool expected) { | 260 void Initialize(const char* url, bool expected) { |
| 261 InitializeWithCORS(url, expected, UrlData::CORS_UNSPECIFIED); | 261 InitializeWithCORS(url, expected, UrlData::CORS_UNSPECIFIED); |
| 262 } | 262 } |
| 263 | 263 |
| 264 // Helper to initialize tests with a valid 200 response. | 264 // Helper to initialize tests with a valid 200 response. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 294 } | 294 } |
| 295 | 295 |
| 296 // Stops any active loaders and shuts down the data source. | 296 // Stops any active loaders and shuts down the data source. |
| 297 // | 297 // |
| 298 // This typically happens when the page is closed and for our purposes is | 298 // This typically happens when the page is closed and for our purposes is |
| 299 // appropriate to do when tearing down a test. | 299 // appropriate to do when tearing down a test. |
| 300 void Stop() { | 300 void Stop() { |
| 301 if (loading()) { | 301 if (loading()) { |
| 302 data_provider()->didFail(url_loader(), | 302 data_provider()->didFail(url_loader(), |
| 303 response_generator_->GenerateError()); | 303 response_generator_->GenerateError()); |
| 304 message_loop_.RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
| 305 } | 305 } |
| 306 | 306 |
| 307 data_source_->Stop(); | 307 data_source_->Stop(); |
| 308 message_loop_.RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
| 309 } | 309 } |
| 310 | 310 |
| 311 void Respond(const WebURLResponse& response) { | 311 void Respond(const WebURLResponse& response) { |
| 312 EXPECT_TRUE(url_loader()); | 312 EXPECT_TRUE(url_loader()); |
| 313 if (!active_loader()) | 313 if (!active_loader()) |
| 314 return; | 314 return; |
| 315 data_provider()->didReceiveResponse(url_loader(), response); | 315 data_provider()->didReceiveResponse(url_loader(), response); |
| 316 message_loop_.RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void ReceiveData(int size) { | 319 void ReceiveData(int size) { |
| 320 EXPECT_TRUE(url_loader()); | 320 EXPECT_TRUE(url_loader()); |
| 321 if (!url_loader()) | 321 if (!url_loader()) |
| 322 return; | 322 return; |
| 323 std::unique_ptr<char[]> data(new char[size]); | 323 std::unique_ptr<char[]> data(new char[size]); |
| 324 memset(data.get(), 0xA5, size); // Arbitrary non-zero value. | 324 memset(data.get(), 0xA5, size); // Arbitrary non-zero value. |
| 325 | 325 |
| 326 data_provider()->didReceiveData(url_loader(), data.get(), size, size); | 326 data_provider()->didReceiveData(url_loader(), data.get(), size, size); |
| 327 message_loop_.RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 void FinishLoading() { | 330 void FinishLoading() { |
| 331 EXPECT_TRUE(url_loader()); | 331 EXPECT_TRUE(url_loader()); |
| 332 if (!url_loader()) | 332 if (!url_loader()) |
| 333 return; | 333 return; |
| 334 data_provider()->didFinishLoading(url_loader(), 0, -1); | 334 data_provider()->didFinishLoading(url_loader(), 0, -1); |
| 335 message_loop_.RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
| 336 } | 336 } |
| 337 | 337 |
| 338 void FailLoading() { | 338 void FailLoading() { |
| 339 data_provider()->didFail(url_loader(), | 339 data_provider()->didFail(url_loader(), |
| 340 response_generator_->GenerateError()); | 340 response_generator_->GenerateError()); |
| 341 message_loop_.RunUntilIdle(); | 341 base::RunLoop().RunUntilIdle(); |
| 342 } | 342 } |
| 343 | 343 |
| 344 void Restart() { | 344 void Restart() { |
| 345 EXPECT_TRUE(data_provider()); | 345 EXPECT_TRUE(data_provider()); |
| 346 EXPECT_FALSE(active_loader_allownull()); | 346 EXPECT_FALSE(active_loader_allownull()); |
| 347 if (!data_provider()) | 347 if (!data_provider()) |
| 348 return; | 348 return; |
| 349 data_provider()->Start(); | 349 data_provider()->Start(); |
| 350 } | 350 } |
| 351 | 351 |
| 352 MOCK_METHOD1(ReadCallback, void(int size)); | 352 MOCK_METHOD1(ReadCallback, void(int size)); |
| 353 | 353 |
| 354 void ReadAt(int64_t position, int64_t howmuch = kDataSize) { | 354 void ReadAt(int64_t position, int64_t howmuch = kDataSize) { |
| 355 data_source_->Read(position, howmuch, buffer_, | 355 data_source_->Read(position, howmuch, buffer_, |
| 356 base::Bind(&MultibufferDataSourceTest::ReadCallback, | 356 base::Bind(&MultibufferDataSourceTest::ReadCallback, |
| 357 base::Unretained(this))); | 357 base::Unretained(this))); |
| 358 message_loop_.RunUntilIdle(); | 358 base::RunLoop().RunUntilIdle(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 void ExecuteMixedResponseSuccessTest(const WebURLResponse& response1, | 361 void ExecuteMixedResponseSuccessTest(const WebURLResponse& response1, |
| 362 const WebURLResponse& response2) { | 362 const WebURLResponse& response2) { |
| 363 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); | 363 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); |
| 364 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); | 364 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); |
| 365 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); | 365 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); |
| 366 EXPECT_CALL(*this, ReadCallback(kDataSize)).Times(2); | 366 EXPECT_CALL(*this, ReadCallback(kDataSize)).Times(2); |
| 367 | 367 |
| 368 Respond(response1); | 368 Respond(response1); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 | 606 |
| 607 TEST_F(MultibufferDataSourceTest, Http_AbortWhileReading) { | 607 TEST_F(MultibufferDataSourceTest, Http_AbortWhileReading) { |
| 608 InitializeWith206Response(); | 608 InitializeWith206Response(); |
| 609 | 609 |
| 610 // Make sure there's a pending read -- we'll expect it to error. | 610 // Make sure there's a pending read -- we'll expect it to error. |
| 611 ReadAt(kFileSize); | 611 ReadAt(kFileSize); |
| 612 | 612 |
| 613 // Abort!!! | 613 // Abort!!! |
| 614 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 614 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 615 data_source_->Abort(); | 615 data_source_->Abort(); |
| 616 message_loop_.RunUntilIdle(); | 616 base::RunLoop().RunUntilIdle(); |
| 617 | 617 |
| 618 EXPECT_FALSE(loading()); | 618 EXPECT_FALSE(loading()); |
| 619 Stop(); | 619 Stop(); |
| 620 } | 620 } |
| 621 | 621 |
| 622 TEST_F(MultibufferDataSourceTest, File_AbortWhileReading) { | 622 TEST_F(MultibufferDataSourceTest, File_AbortWhileReading) { |
| 623 InitializeWithFileResponse(); | 623 InitializeWithFileResponse(); |
| 624 | 624 |
| 625 // Make sure there's a pending read -- we'll expect it to error. | 625 // Make sure there's a pending read -- we'll expect it to error. |
| 626 ReadAt(kFileSize); | 626 ReadAt(kFileSize); |
| 627 | 627 |
| 628 // Abort!!! | 628 // Abort!!! |
| 629 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 629 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 630 data_source_->Abort(); | 630 data_source_->Abort(); |
| 631 message_loop_.RunUntilIdle(); | 631 base::RunLoop().RunUntilIdle(); |
| 632 | 632 |
| 633 EXPECT_FALSE(loading()); | 633 EXPECT_FALSE(loading()); |
| 634 Stop(); | 634 Stop(); |
| 635 } | 635 } |
| 636 | 636 |
| 637 TEST_F(MultibufferDataSourceTest, Http_Retry) { | 637 TEST_F(MultibufferDataSourceTest, Http_Retry) { |
| 638 InitializeWith206Response(); | 638 InitializeWith206Response(); |
| 639 | 639 |
| 640 // Read to advance our position. | 640 // Read to advance our position. |
| 641 EXPECT_CALL(*this, ReadCallback(kDataSize)); | 641 EXPECT_CALL(*this, ReadCallback(kDataSize)); |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 906 data_source_->Read(0, arraysize(buffer), buffer, | 906 data_source_->Read(0, arraysize(buffer), buffer, |
| 907 base::Bind(&MultibufferDataSourceTest::ReadCallback, | 907 base::Bind(&MultibufferDataSourceTest::ReadCallback, |
| 908 base::Unretained(this))); | 908 base::Unretained(this))); |
| 909 | 909 |
| 910 // The outstanding read should fail before the stop callback runs. | 910 // The outstanding read should fail before the stop callback runs. |
| 911 { | 911 { |
| 912 InSequence s; | 912 InSequence s; |
| 913 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 913 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 914 data_source_->Stop(); | 914 data_source_->Stop(); |
| 915 } | 915 } |
| 916 message_loop_.RunUntilIdle(); | 916 base::RunLoop().RunUntilIdle(); |
| 917 } | 917 } |
| 918 | 918 |
| 919 TEST_F(MultibufferDataSourceTest, DefaultValues) { | 919 TEST_F(MultibufferDataSourceTest, DefaultValues) { |
| 920 InitializeWith206Response(); | 920 InitializeWith206Response(); |
| 921 | 921 |
| 922 // Ensure we have sane values for default loading scenario. | 922 // Ensure we have sane values for default loading scenario. |
| 923 EXPECT_EQ(MultibufferDataSource::AUTO, preload()); | 923 EXPECT_EQ(MultibufferDataSource::AUTO, preload()); |
| 924 EXPECT_EQ(2 << 20, preload_low()); | 924 EXPECT_EQ(2 << 20, preload_low()); |
| 925 EXPECT_EQ(3 << 20, preload_high()); | 925 EXPECT_EQ(3 << 20, preload_high()); |
| 926 | 926 |
| 927 EXPECT_EQ(0, data_source_bitrate()); | 927 EXPECT_EQ(0, data_source_bitrate()); |
| 928 EXPECT_EQ(0.0, data_source_playback_rate()); | 928 EXPECT_EQ(0.0, data_source_playback_rate()); |
| 929 | 929 |
| 930 EXPECT_TRUE(loading()); | 930 EXPECT_TRUE(loading()); |
| 931 Stop(); | 931 Stop(); |
| 932 } | 932 } |
| 933 | 933 |
| 934 TEST_F(MultibufferDataSourceTest, SetBitrate) { | 934 TEST_F(MultibufferDataSourceTest, SetBitrate) { |
| 935 InitializeWith206Response(); | 935 InitializeWith206Response(); |
| 936 | 936 |
| 937 data_source_->SetBitrate(1234); | 937 data_source_->SetBitrate(1234); |
| 938 message_loop_.RunUntilIdle(); | 938 base::RunLoop().RunUntilIdle(); |
| 939 EXPECT_EQ(1234, data_source_bitrate()); | 939 EXPECT_EQ(1234, data_source_bitrate()); |
| 940 | 940 |
| 941 // Read so far ahead to cause the loader to get recreated. | 941 // Read so far ahead to cause the loader to get recreated. |
| 942 TestMultiBufferDataProvider* old_loader = data_provider(); | 942 TestMultiBufferDataProvider* old_loader = data_provider(); |
| 943 ReadAt(kFarReadPosition); | 943 ReadAt(kFarReadPosition); |
| 944 Respond(response_generator_->Generate206(kFarReadPosition)); | 944 Respond(response_generator_->Generate206(kFarReadPosition)); |
| 945 | 945 |
| 946 // Verify loader changed but still has same bitrate. | 946 // Verify loader changed but still has same bitrate. |
| 947 EXPECT_NE(old_loader, data_provider()); | 947 EXPECT_NE(old_loader, data_provider()); |
| 948 | 948 |
| 949 EXPECT_TRUE(loading()); | 949 EXPECT_TRUE(loading()); |
| 950 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 950 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 951 Stop(); | 951 Stop(); |
| 952 } | 952 } |
| 953 | 953 |
| 954 TEST_F(MultibufferDataSourceTest, MediaPlaybackRateChanged) { | 954 TEST_F(MultibufferDataSourceTest, MediaPlaybackRateChanged) { |
| 955 InitializeWith206Response(); | 955 InitializeWith206Response(); |
| 956 | 956 |
| 957 data_source_->MediaPlaybackRateChanged(2.0); | 957 data_source_->MediaPlaybackRateChanged(2.0); |
| 958 message_loop_.RunUntilIdle(); | 958 base::RunLoop().RunUntilIdle(); |
| 959 EXPECT_EQ(2.0, data_source_playback_rate()); | 959 EXPECT_EQ(2.0, data_source_playback_rate()); |
| 960 | 960 |
| 961 // Read so far ahead to cause the loader to get recreated. | 961 // Read so far ahead to cause the loader to get recreated. |
| 962 TestMultiBufferDataProvider* old_loader = data_provider(); | 962 TestMultiBufferDataProvider* old_loader = data_provider(); |
| 963 ReadAt(kFarReadPosition); | 963 ReadAt(kFarReadPosition); |
| 964 Respond(response_generator_->Generate206(kFarReadPosition)); | 964 Respond(response_generator_->Generate206(kFarReadPosition)); |
| 965 | 965 |
| 966 // Verify loader changed but still has same playback rate. | 966 // Verify loader changed but still has same playback rate. |
| 967 EXPECT_NE(old_loader, data_provider()); | 967 EXPECT_NE(old_loader, data_provider()); |
| 968 | 968 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 GURL(kHttpUrl), UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), | 1010 GURL(kHttpUrl), UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), |
| 1011 url_index_, view_->mainFrame()->toWebLocalFrame(), &host2); | 1011 url_index_, view_->mainFrame()->toWebLocalFrame(), &host2); |
| 1012 source2.SetPreload(preload_); | 1012 source2.SetPreload(preload_); |
| 1013 | 1013 |
| 1014 EXPECT_CALL(*this, OnInitialize(true)); | 1014 EXPECT_CALL(*this, OnInitialize(true)); |
| 1015 | 1015 |
| 1016 // This call would not be expected if we were not sharing data. | 1016 // This call would not be expected if we were not sharing data. |
| 1017 EXPECT_CALL(host2, SetTotalBytes(response_generator_->content_length())); | 1017 EXPECT_CALL(host2, SetTotalBytes(response_generator_->content_length())); |
| 1018 source2.Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, | 1018 source2.Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, |
| 1019 base::Unretained(this))); | 1019 base::Unretained(this))); |
| 1020 message_loop_.RunUntilIdle(); | 1020 base::RunLoop().RunUntilIdle(); |
| 1021 | 1021 |
| 1022 // Always loading after initialize. | 1022 // Always loading after initialize. |
| 1023 EXPECT_EQ(source2.downloading(), true); | 1023 EXPECT_EQ(source2.downloading(), true); |
| 1024 | 1024 |
| 1025 Stop(); | 1025 Stop(); |
| 1026 } | 1026 } |
| 1027 | 1027 |
| 1028 TEST_F(MultibufferDataSourceTest, Http_Read_Seek) { | 1028 TEST_F(MultibufferDataSourceTest, Http_Read_Seek) { |
| 1029 InitializeWith206Response(); | 1029 InitializeWith206Response(); |
| 1030 | 1030 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1302 GURL gurl(kHttpUrl); | 1302 GURL gurl(kHttpUrl); |
| 1303 data_source_.reset(new MockMultibufferDataSource( | 1303 data_source_.reset(new MockMultibufferDataSource( |
| 1304 gurl, UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), url_index_, | 1304 gurl, UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), url_index_, |
| 1305 view_->mainFrame()->toWebLocalFrame(), &host_)); | 1305 view_->mainFrame()->toWebLocalFrame(), &host_)); |
| 1306 data_source_->SetPreload(preload_); | 1306 data_source_->SetPreload(preload_); |
| 1307 | 1307 |
| 1308 response_generator_.reset(new TestResponseGenerator(gurl, kDataSize + 1)); | 1308 response_generator_.reset(new TestResponseGenerator(gurl, kDataSize + 1)); |
| 1309 EXPECT_CALL(*this, OnInitialize(true)); | 1309 EXPECT_CALL(*this, OnInitialize(true)); |
| 1310 data_source_->Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, | 1310 data_source_->Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, |
| 1311 base::Unretained(this))); | 1311 base::Unretained(this))); |
| 1312 message_loop_.RunUntilIdle(); | 1312 base::RunLoop().RunUntilIdle(); |
| 1313 | 1313 |
| 1314 // Not really loading until after OnInitialize is called. | 1314 // Not really loading until after OnInitialize is called. |
| 1315 EXPECT_EQ(data_source_->downloading(), false); | 1315 EXPECT_EQ(data_source_->downloading(), false); |
| 1316 | 1316 |
| 1317 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 1317 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 1318 Respond(response_generator_->Generate206(0)); | 1318 Respond(response_generator_->Generate206(0)); |
| 1319 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); | 1319 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); |
| 1320 ReceiveData(kDataSize); | 1320 ReceiveData(kDataSize); |
| 1321 | 1321 |
| 1322 // Read a bit from the beginning. | 1322 // Read a bit from the beginning. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1419 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); | 1419 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); |
| 1420 FinishLoading(); | 1420 FinishLoading(); |
| 1421 | 1421 |
| 1422 // Done loading, now we should know the length. | 1422 // Done loading, now we should know the length. |
| 1423 EXPECT_TRUE(data_source_->GetSize(&len)); | 1423 EXPECT_TRUE(data_source_->GetSize(&len)); |
| 1424 EXPECT_EQ(kDataSize, len); | 1424 EXPECT_EQ(kDataSize, len); |
| 1425 Stop(); | 1425 Stop(); |
| 1426 } | 1426 } |
| 1427 | 1427 |
| 1428 } // namespace media | 1428 } // namespace media |
| OLD | NEW |