| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <array> | 5 #include <array> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "content/renderer/media/audio_renderer_sink_cache_impl.h" | 13 #include "content/renderer/media/audio_renderer_sink_cache_impl.h" |
| 14 #include "media/audio/audio_device_description.h" | 14 #include "media/audio/audio_device_description.h" |
| 15 #include "media/base/audio_parameters.h" | 15 #include "media/base/audio_parameters.h" |
| 16 #include "media/base/mock_audio_renderer_sink.h" | 16 #include "media/base/mock_audio_renderer_sink.h" |
| 17 #include "media/base/test_helpers.h" | 17 #include "media/base/test_helpers.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 const char* const kDefaultDeviceId = | 25 const char* const kDefaultDeviceId = |
| 26 media::AudioDeviceDescription::kDefaultDeviceId; | 26 media::AudioDeviceDescription::kDefaultDeviceId; |
| 27 const char kAnotherDeviceId[] = "another-device-id"; | 27 const char kAnotherDeviceId[] = "another-device-id"; |
| 28 const char kUnhealthyDeviceId[] = "i-am-sick"; |
| 28 const int kRenderFrameId = 124; | 29 const int kRenderFrameId = 124; |
| 29 const int kDeleteTimeoutMs = 500; | 30 const int kDeleteTimeoutMs = 500; |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 class AudioRendererSinkCacheTest : public testing::Test { | 33 class AudioRendererSinkCacheTest : public testing::Test { |
| 33 public: | 34 public: |
| 34 AudioRendererSinkCacheTest() | 35 AudioRendererSinkCacheTest() |
| 35 : cache_(new AudioRendererSinkCacheImpl( | 36 : cache_(new AudioRendererSinkCacheImpl( |
| 36 message_loop_.task_runner(), | 37 message_loop_.task_runner(), |
| 37 base::Bind(&AudioRendererSinkCacheTest::CreateSink, | 38 base::Bind(&AudioRendererSinkCacheTest::CreateSink, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 65 int sink_count() { | 66 int sink_count() { |
| 66 DCHECK(message_loop_.task_runner()->BelongsToCurrentThread()); | 67 DCHECK(message_loop_.task_runner()->BelongsToCurrentThread()); |
| 67 return cache_->GetCacheSizeForTesting(); | 68 return cache_->GetCacheSizeForTesting(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 scoped_refptr<media::AudioRendererSink> CreateSink( | 71 scoped_refptr<media::AudioRendererSink> CreateSink( |
| 71 int render_frame_id, | 72 int render_frame_id, |
| 72 int session_id, | 73 int session_id, |
| 73 const std::string& device_id, | 74 const std::string& device_id, |
| 74 const url::Origin& security_origin) { | 75 const url::Origin& security_origin) { |
| 75 return new media::MockAudioRendererSink(device_id, | 76 return new media::MockAudioRendererSink( |
| 76 media::OUTPUT_DEVICE_STATUS_OK); | 77 device_id, (device_id == kUnhealthyDeviceId) |
| 78 ? media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL |
| 79 : media::OUTPUT_DEVICE_STATUS_OK); |
| 77 } | 80 } |
| 78 | 81 |
| 79 void ExpectToStop(media::AudioRendererSink* sink) { | 82 void ExpectToStop(media::AudioRendererSink* sink) { |
| 80 // Sink must be stoped before deletion. | 83 // Sink must be stoped before deletion. |
| 81 EXPECT_CALL(*static_cast<media::MockAudioRendererSink*>(sink), Stop()) | 84 EXPECT_CALL(*static_cast<media::MockAudioRendererSink*>(sink), Stop()) |
| 82 .Times(1); | 85 .Times(1); |
| 83 } | 86 } |
| 84 | 87 |
| 85 void ExpectNotToStop(media::AudioRendererSink* sink) { | 88 void ExpectNotToStop(media::AudioRendererSink* sink) { |
| 86 // The sink must be stoped before deletion. | 89 // The sink must be stoped before deletion. |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 EXPECT_EQ(kDefaultDeviceId, sink->GetOutputDeviceInfo().device_id()); | 246 EXPECT_EQ(kDefaultDeviceId, sink->GetOutputDeviceInfo().device_id()); |
| 244 EXPECT_EQ(1, sink_count()); | 247 EXPECT_EQ(1, sink_count()); |
| 245 | 248 |
| 246 // Wait more to hit garbage collection timeout. | 249 // Wait more to hit garbage collection timeout. |
| 247 WaitOnAnotherThread(thread, kDeleteTimeoutMs); | 250 WaitOnAnotherThread(thread, kDeleteTimeoutMs); |
| 248 | 251 |
| 249 // The sink is still in place. | 252 // The sink is still in place. |
| 250 EXPECT_EQ(1, sink_count()); | 253 EXPECT_EQ(1, sink_count()); |
| 251 } | 254 } |
| 252 | 255 |
| 256 // Verify that the sink created with GetSinkInfo() is not cached if it is |
| 257 // unhealthy. |
| 258 TEST_F(AudioRendererSinkCacheTest, UnhealthySinkIsNotCached) { |
| 259 EXPECT_EQ(0, sink_count()); |
| 260 media::OutputDeviceInfo device_info = |
| 261 cache_->GetSinkInfo(kRenderFrameId, 0, kUnhealthyDeviceId, url::Origin()); |
| 262 EXPECT_EQ(0, sink_count()); |
| 263 scoped_refptr<media::AudioRendererSink> sink = |
| 264 cache_->GetSink(kRenderFrameId, kUnhealthyDeviceId, url::Origin()).get(); |
| 265 EXPECT_EQ(0, sink_count()); |
| 266 } |
| 267 |
| 253 // Verify that cache works fine if a sink scheduled for delettion is aquired and | 268 // Verify that cache works fine if a sink scheduled for delettion is aquired and |
| 254 // released before deletion timeout elapses. | 269 // released before deletion timeout elapses. |
| 255 // The test produces one "Uninteresting mock" warning for | 270 // The test produces one "Uninteresting mock" warning for |
| 256 // MockAudioRendererSink::Stop(). | 271 // MockAudioRendererSink::Stop(). |
| 257 TEST_F(AudioRendererSinkCacheTest, ReleaseSinkBeforeScheduledDeletion) { | 272 TEST_F(AudioRendererSinkCacheTest, ReleaseSinkBeforeScheduledDeletion) { |
| 258 EXPECT_EQ(0, sink_count()); | 273 EXPECT_EQ(0, sink_count()); |
| 259 | 274 |
| 260 media::OutputDeviceInfo device_info = | 275 media::OutputDeviceInfo device_info = |
| 261 cache_->GetSinkInfo(kRenderFrameId, 0, kDefaultDeviceId, url::Origin()); | 276 cache_->GetSinkInfo(kRenderFrameId, 0, kDefaultDeviceId, url::Origin()); |
| 262 EXPECT_EQ(1, sink_count()); // This sink is scheduled for deletion now. | 277 EXPECT_EQ(1, sink_count()); // This sink is scheduled for deletion now. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // Wait for completion of all the tasks posted to at least one thread. | 381 // Wait for completion of all the tasks posted to at least one thread. |
| 367 media::WaitableMessageLoopEvent loop_event( | 382 media::WaitableMessageLoopEvent loop_event( |
| 368 TestTimeouts::action_max_timeout()); | 383 TestTimeouts::action_max_timeout()); |
| 369 threads[kThreadCount - 1]->task_runner()->PostTaskAndReply( | 384 threads[kThreadCount - 1]->task_runner()->PostTaskAndReply( |
| 370 FROM_HERE, base::Bind(&base::DoNothing), loop_event.GetClosure()); | 385 FROM_HERE, base::Bind(&base::DoNothing), loop_event.GetClosure()); |
| 371 // Runs the loop and waits for the thread to call event's closure. | 386 // Runs the loop and waits for the thread to call event's closure. |
| 372 loop_event.RunAndWait(); | 387 loop_event.RunAndWait(); |
| 373 } | 388 } |
| 374 | 389 |
| 375 } // namespace content | 390 } // namespace content |
| OLD | NEW |