| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "media/base/media_log.h" | 7 #include "media/base/media_log.h" |
| 8 #include "media/base/mock_callback.h" | 8 #include "media/base/mock_callback.h" |
| 9 #include "media/base/mock_data_source_host.h" | 9 #include "media/base/mock_data_source_host.h" |
| 10 #include "media/base/mock_filters.h" | 10 #include "media/base/mock_filters.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 data_source_ = new MockBufferedDataSource(&message_loop_, | 98 data_source_ = new MockBufferedDataSource(&message_loop_, |
| 99 view_->mainFrame()); | 99 view_->mainFrame()); |
| 100 data_source_->set_host(&host_); | 100 data_source_->set_host(&host_); |
| 101 } | 101 } |
| 102 | 102 |
| 103 virtual ~BufferedDataSourceTest() { | 103 virtual ~BufferedDataSourceTest() { |
| 104 view_->close(); | 104 view_->close(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void Initialize(const char* url, media::PipelineStatus expected) { | 107 MOCK_METHOD1(OnInitialize, void(bool)); |
| 108 |
| 109 void Initialize(const char* url, bool expected) { |
| 108 GURL gurl(url); | 110 GURL gurl(url); |
| 109 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 111 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
| 110 | 112 |
| 111 ExpectCreateResourceLoader(); | 113 ExpectCreateResourceLoader(); |
| 112 data_source_->Initialize(gurl, | 114 EXPECT_CALL(*this, OnInitialize(expected)); |
| 113 BufferedResourceLoader::kUnspecified, | 115 data_source_->Initialize( |
| 114 media::NewExpectedStatusCB(expected)); | 116 gurl, BufferedResourceLoader::kUnspecified, base::Bind( |
| 117 &BufferedDataSourceTest::OnInitialize, base::Unretained(this))); |
| 115 message_loop_.RunAllPending(); | 118 message_loop_.RunAllPending(); |
| 116 | 119 |
| 117 bool is_http = gurl.SchemeIs(kHttpScheme) || gurl.SchemeIs(kHttpsScheme); | 120 bool is_http = gurl.SchemeIs(kHttpScheme) || gurl.SchemeIs(kHttpsScheme); |
| 118 EXPECT_EQ(data_source_->downloading(), is_http); | 121 EXPECT_EQ(data_source_->downloading(), is_http); |
| 119 } | 122 } |
| 120 | 123 |
| 121 // Helper to initialize tests with a valid 206 response. | 124 // Helper to initialize tests with a valid 206 response. |
| 122 void InitializeWith206Response() { | 125 void InitializeWith206Response() { |
| 123 Initialize(kHttpUrl, media::PIPELINE_OK); | 126 Initialize(kHttpUrl, true); |
| 124 | 127 |
| 125 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 128 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 126 Respond(response_generator_->Generate206(0)); | 129 Respond(response_generator_->Generate206(0)); |
| 127 } | 130 } |
| 128 | 131 |
| 129 // Helper to initialize tests with a valid file:// response. | 132 // Helper to initialize tests with a valid file:// response. |
| 130 void InitializeWithFileResponse() { | 133 void InitializeWithFileResponse() { |
| 131 Initialize(kFileUrl, media::PIPELINE_OK); | 134 Initialize(kFileUrl, true); |
| 132 | 135 |
| 133 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); | 136 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); |
| 134 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize)); | 137 EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize)); |
| 135 Respond(response_generator_->GenerateFileResponse(0)); | 138 Respond(response_generator_->GenerateFileResponse(0)); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // Stops any active loaders and shuts down the data source. | 141 // Stops any active loaders and shuts down the data source. |
| 139 // | 142 // |
| 140 // This typically happens when the page is closed and for our purposes is | 143 // This typically happens when the page is closed and for our purposes is |
| 141 // appropriate to do when tearing down a test. | 144 // appropriate to do when tearing down a test. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 MessageLoop message_loop_; | 214 MessageLoop message_loop_; |
| 212 | 215 |
| 213 private: | 216 private: |
| 214 // Used for calling BufferedDataSource::Read(). | 217 // Used for calling BufferedDataSource::Read(). |
| 215 uint8 buffer_[kDataSize]; | 218 uint8 buffer_[kDataSize]; |
| 216 | 219 |
| 217 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); | 220 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest); |
| 218 }; | 221 }; |
| 219 | 222 |
| 220 TEST_F(BufferedDataSourceTest, Range_Supported) { | 223 TEST_F(BufferedDataSourceTest, Range_Supported) { |
| 221 Initialize(kHttpUrl, media::PIPELINE_OK); | 224 Initialize(kHttpUrl, true); |
| 222 | 225 |
| 223 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 226 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 224 Respond(response_generator_->Generate206(0)); | 227 Respond(response_generator_->Generate206(0)); |
| 225 | 228 |
| 226 EXPECT_TRUE(data_source_->loading()); | 229 EXPECT_TRUE(data_source_->loading()); |
| 227 EXPECT_FALSE(data_source_->IsStreaming()); | 230 EXPECT_FALSE(data_source_->IsStreaming()); |
| 228 Stop(); | 231 Stop(); |
| 229 } | 232 } |
| 230 | 233 |
| 231 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) { | 234 TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) { |
| 232 Initialize(kHttpUrl, media::PIPELINE_OK); | 235 Initialize(kHttpUrl, true); |
| 233 | 236 |
| 234 Respond(response_generator_->Generate206( | 237 Respond(response_generator_->Generate206( |
| 235 0, TestResponseGenerator::kNoContentRangeInstanceSize)); | 238 0, TestResponseGenerator::kNoContentRangeInstanceSize)); |
| 236 | 239 |
| 237 EXPECT_TRUE(data_source_->loading()); | 240 EXPECT_TRUE(data_source_->loading()); |
| 238 EXPECT_TRUE(data_source_->IsStreaming()); | 241 EXPECT_TRUE(data_source_->IsStreaming()); |
| 239 Stop(); | 242 Stop(); |
| 240 } | 243 } |
| 241 | 244 |
| 242 TEST_F(BufferedDataSourceTest, Range_NotFound) { | 245 TEST_F(BufferedDataSourceTest, Range_NotFound) { |
| 243 Initialize(kHttpUrl, media::PIPELINE_ERROR_NETWORK); | 246 Initialize(kHttpUrl, false); |
| 244 Respond(response_generator_->Generate404()); | 247 Respond(response_generator_->Generate404()); |
| 245 | 248 |
| 246 EXPECT_FALSE(data_source_->loading()); | 249 EXPECT_FALSE(data_source_->loading()); |
| 247 Stop(); | 250 Stop(); |
| 248 } | 251 } |
| 249 | 252 |
| 250 TEST_F(BufferedDataSourceTest, Range_NotSupported) { | 253 TEST_F(BufferedDataSourceTest, Range_NotSupported) { |
| 251 Initialize(kHttpUrl, media::PIPELINE_OK); | 254 Initialize(kHttpUrl, true); |
| 252 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 255 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 253 Respond(response_generator_->Generate200()); | 256 Respond(response_generator_->Generate200()); |
| 254 | 257 |
| 255 EXPECT_TRUE(data_source_->loading()); | 258 EXPECT_TRUE(data_source_->loading()); |
| 256 EXPECT_TRUE(data_source_->IsStreaming()); | 259 EXPECT_TRUE(data_source_->IsStreaming()); |
| 257 Stop(); | 260 Stop(); |
| 258 } | 261 } |
| 259 | 262 |
| 260 // Special carve-out for Apache versions that choose to return a 200 for | 263 // Special carve-out for Apache versions that choose to return a 200 for |
| 261 // Range:0- ("because it's more efficient" than a 206) | 264 // Range:0- ("because it's more efficient" than a 206) |
| 262 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) { | 265 TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) { |
| 263 Initialize(kHttpUrl, media::PIPELINE_OK); | 266 Initialize(kHttpUrl, true); |
| 264 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 267 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 265 WebURLResponse response = response_generator_->Generate200(); | 268 WebURLResponse response = response_generator_->Generate200(); |
| 266 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), | 269 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), |
| 267 WebString::fromUTF8("bytes")); | 270 WebString::fromUTF8("bytes")); |
| 268 Respond(response); | 271 Respond(response); |
| 269 | 272 |
| 270 EXPECT_TRUE(data_source_->loading()); | 273 EXPECT_TRUE(data_source_->loading()); |
| 271 EXPECT_FALSE(data_source_->IsStreaming()); | 274 EXPECT_FALSE(data_source_->IsStreaming()); |
| 272 Stop(); | 275 Stop(); |
| 273 } | 276 } |
| 274 | 277 |
| 275 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) { | 278 TEST_F(BufferedDataSourceTest, Range_MissingContentRange) { |
| 276 Initialize(kHttpUrl, media::PIPELINE_ERROR_NETWORK); | 279 Initialize(kHttpUrl, false); |
| 277 Respond(response_generator_->Generate206( | 280 Respond(response_generator_->Generate206( |
| 278 0, TestResponseGenerator::kNoContentRange)); | 281 0, TestResponseGenerator::kNoContentRange)); |
| 279 | 282 |
| 280 EXPECT_FALSE(data_source_->loading()); | 283 EXPECT_FALSE(data_source_->loading()); |
| 281 Stop(); | 284 Stop(); |
| 282 } | 285 } |
| 283 | 286 |
| 284 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) { | 287 TEST_F(BufferedDataSourceTest, Range_MissingContentLength) { |
| 285 Initialize(kHttpUrl, media::PIPELINE_OK); | 288 Initialize(kHttpUrl, true); |
| 286 | 289 |
| 287 // It'll manage without a Content-Length response. | 290 // It'll manage without a Content-Length response. |
| 288 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 291 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 289 Respond(response_generator_->Generate206( | 292 Respond(response_generator_->Generate206( |
| 290 0, TestResponseGenerator::kNoContentLength)); | 293 0, TestResponseGenerator::kNoContentLength)); |
| 291 | 294 |
| 292 EXPECT_TRUE(data_source_->loading()); | 295 EXPECT_TRUE(data_source_->loading()); |
| 293 EXPECT_FALSE(data_source_->IsStreaming()); | 296 EXPECT_FALSE(data_source_->IsStreaming()); |
| 294 Stop(); | 297 Stop(); |
| 295 } | 298 } |
| 296 | 299 |
| 297 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) { | 300 TEST_F(BufferedDataSourceTest, Range_WrongContentRange) { |
| 298 Initialize(kHttpUrl, media::PIPELINE_ERROR_NETWORK); | 301 Initialize(kHttpUrl, false); |
| 299 | 302 |
| 300 // Now it's done and will fail. | 303 // Now it's done and will fail. |
| 301 Respond(response_generator_->Generate206(1337)); | 304 Respond(response_generator_->Generate206(1337)); |
| 302 | 305 |
| 303 EXPECT_FALSE(data_source_->loading()); | 306 EXPECT_FALSE(data_source_->loading()); |
| 304 Stop(); | 307 Stop(); |
| 305 } | 308 } |
| 306 | 309 |
| 307 // Test the case where the initial response from the server indicates that | 310 // Test the case where the initial response from the server indicates that |
| 308 // Range requests are supported, but a later request prove otherwise. | 311 // Range requests are supported, but a later request prove otherwise. |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 448 |
| 446 // It'll error after this. | 449 // It'll error after this. |
| 447 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 450 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); |
| 448 FinishLoading(); | 451 FinishLoading(); |
| 449 | 452 |
| 450 EXPECT_FALSE(data_source_->loading()); | 453 EXPECT_FALSE(data_source_->loading()); |
| 451 Stop(); | 454 Stop(); |
| 452 } | 455 } |
| 453 | 456 |
| 454 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) { | 457 TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) { |
| 455 Initialize(kFileUrl, media::PIPELINE_ERROR_NETWORK); | 458 Initialize(kFileUrl, false); |
| 456 EXPECT_FALSE(data_source_->downloading()); | 459 EXPECT_FALSE(data_source_->downloading()); |
| 457 | 460 |
| 458 Respond(response_generator_->GenerateFileResponse(-1)); | 461 Respond(response_generator_->GenerateFileResponse(-1)); |
| 459 | 462 |
| 460 EXPECT_FALSE(data_source_->loading()); | 463 EXPECT_FALSE(data_source_->loading()); |
| 461 Stop(); | 464 Stop(); |
| 462 } | 465 } |
| 463 | 466 |
| 464 TEST_F(BufferedDataSourceTest, File_Successful) { | 467 TEST_F(BufferedDataSourceTest, File_Successful) { |
| 465 InitializeWithFileResponse(); | 468 InitializeWithFileResponse(); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 ReceiveData(kDataSize / 2); | 585 ReceiveData(kDataSize / 2); |
| 583 | 586 |
| 584 // Receive last half of the read but no buffering update. | 587 // Receive last half of the read but no buffering update. |
| 585 EXPECT_CALL(*this, ReadCallback(kDataSize)); | 588 EXPECT_CALL(*this, ReadCallback(kDataSize)); |
| 586 ReceiveData(kDataSize / 2); | 589 ReceiveData(kDataSize / 2); |
| 587 | 590 |
| 588 Stop(); | 591 Stop(); |
| 589 } | 592 } |
| 590 | 593 |
| 591 } // namespace webkit_media | 594 } // namespace webkit_media |
| OLD | NEW |