| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/sync_socket.h" | 17 #include "base/sync_socket.h" |
| 18 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 19 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
| 20 #include "base/threading/thread.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "media/audio/audio_output_device.h" | 22 #include "media/audio/audio_output_device.h" |
| 22 #include "media/audio/sample_rates.h" | 23 #include "media/audio/sample_rates.h" |
| 24 #include "media/base/test_helpers.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gmock_mutant.h" | 26 #include "testing/gmock_mutant.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 28 |
| 27 using base::CancelableSyncSocket; | 29 using base::CancelableSyncSocket; |
| 28 using base::SharedMemory; | 30 using base::SharedMemory; |
| 29 using base::SyncSocket; | 31 using base::SyncSocket; |
| 30 using testing::_; | 32 using testing::_; |
| 31 using testing::DoAll; | 33 using testing::DoAll; |
| 32 using testing::Invoke; | 34 using testing::Invoke; |
| 33 using testing::Return; | 35 using testing::Return; |
| 34 using testing::WithArgs; | 36 using testing::WithArgs; |
| 35 using testing::StrictMock; | 37 using testing::StrictMock; |
| 36 using testing::Values; | 38 using testing::Values; |
| 37 | 39 |
| 38 namespace media { | 40 namespace media { |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 const char kDefaultDeviceId[] = ""; | 44 const char kDefaultDeviceId[] = ""; |
| 43 const char kNonDefaultDeviceId[] = "valid-nondefault-device-id"; | 45 const char kNonDefaultDeviceId[] = "valid-nondefault-device-id"; |
| 44 const char kUnauthorizedDeviceId[] = "unauthorized-device-id"; | 46 const char kUnauthorizedDeviceId[] = "unauthorized-device-id"; |
| 47 const int kAuthTimeoutForTesting = 500; |
| 45 | 48 |
| 46 class MockRenderCallback : public AudioRendererSink::RenderCallback { | 49 class MockRenderCallback : public AudioRendererSink::RenderCallback { |
| 47 public: | 50 public: |
| 48 MockRenderCallback() {} | 51 MockRenderCallback() {} |
| 49 virtual ~MockRenderCallback() {} | 52 virtual ~MockRenderCallback() {} |
| 50 | 53 |
| 51 MOCK_METHOD3(Render, | 54 MOCK_METHOD3(Render, |
| 52 int(AudioBus* dest, | 55 int(AudioBus* dest, |
| 53 uint32_t frames_delayed, | 56 uint32_t frames_delayed, |
| 54 uint32_t frames_skipped)); | 57 uint32_t frames_skipped)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 public: | 95 public: |
| 93 AudioOutputDeviceTest(); | 96 AudioOutputDeviceTest(); |
| 94 ~AudioOutputDeviceTest(); | 97 ~AudioOutputDeviceTest(); |
| 95 | 98 |
| 96 void ReceiveAuthorization(OutputDeviceStatus device_status); | 99 void ReceiveAuthorization(OutputDeviceStatus device_status); |
| 97 void StartAudioDevice(); | 100 void StartAudioDevice(); |
| 98 void CreateStream(); | 101 void CreateStream(); |
| 99 void ExpectRenderCallback(); | 102 void ExpectRenderCallback(); |
| 100 void WaitUntilRenderCallback(); | 103 void WaitUntilRenderCallback(); |
| 101 void StopAudioDevice(); | 104 void StopAudioDevice(); |
| 105 void CreateDevice(const std::string& device_id); |
| 102 void SetDevice(const std::string& device_id); | 106 void SetDevice(const std::string& device_id); |
| 107 void CheckDeviceStatus(OutputDeviceStatus device_status); |
| 103 | 108 |
| 104 protected: | 109 protected: |
| 105 // Used to clean up TLS pointers that the test(s) will initialize. | 110 // Used to clean up TLS pointers that the test(s) will initialize. |
| 106 // Must remain the first member of this class. | 111 // Must remain the first member of this class. |
| 107 base::ShadowingAtExitManager at_exit_manager_; | 112 base::ShadowingAtExitManager at_exit_manager_; |
| 108 base::MessageLoopForIO io_loop_; | 113 base::MessageLoopForIO io_loop_; |
| 109 AudioParameters default_audio_parameters_; | 114 AudioParameters default_audio_parameters_; |
| 110 StrictMock<MockRenderCallback> callback_; | 115 StrictMock<MockRenderCallback> callback_; |
| 111 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ | 116 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
| 112 scoped_refptr<AudioOutputDevice> audio_device_; | 117 scoped_refptr<AudioOutputDevice> audio_device_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 132 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { | 137 : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { |
| 133 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, | 138 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, |
| 134 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); | 139 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); |
| 135 SetDevice(kDefaultDeviceId); | 140 SetDevice(kDefaultDeviceId); |
| 136 } | 141 } |
| 137 | 142 |
| 138 AudioOutputDeviceTest::~AudioOutputDeviceTest() { | 143 AudioOutputDeviceTest::~AudioOutputDeviceTest() { |
| 139 audio_device_ = NULL; | 144 audio_device_ = NULL; |
| 140 } | 145 } |
| 141 | 146 |
| 147 void AudioOutputDeviceTest::CreateDevice(const std::string& device_id) { |
| 148 audio_output_ipc_ = new MockAudioOutputIPC(); |
| 149 audio_device_ = new AudioOutputDevice( |
| 150 base::WrapUnique(audio_output_ipc_), io_loop_.task_runner(), 0, device_id, |
| 151 url::Origin(), base::TimeDelta::FromMilliseconds(kAuthTimeoutForTesting)); |
| 152 } |
| 153 |
| 142 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { | 154 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { |
| 143 audio_output_ipc_ = new MockAudioOutputIPC(); | 155 CreateDevice(device_id); |
| 144 audio_device_ = new AudioOutputDevice(base::WrapUnique(audio_output_ipc_), | |
| 145 io_loop_.task_runner(), 0, device_id, | |
| 146 url::Origin()); | |
| 147 EXPECT_CALL(*audio_output_ipc_, | 156 EXPECT_CALL(*audio_output_ipc_, |
| 148 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); | 157 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); |
| 149 audio_device_->RequestDeviceAuthorization(); | 158 audio_device_->RequestDeviceAuthorization(); |
| 150 io_loop_.RunUntilIdle(); | 159 io_loop_.RunUntilIdle(); |
| 151 | 160 |
| 152 // Simulate response from browser | 161 // Simulate response from browser |
| 153 OutputDeviceStatus device_status = | 162 OutputDeviceStatus device_status = |
| 154 (device_id == kUnauthorizedDeviceId) | 163 (device_id == kUnauthorizedDeviceId) |
| 155 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 164 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
| 156 : OUTPUT_DEVICE_STATUS_OK; | 165 : OUTPUT_DEVICE_STATUS_OK; |
| 157 ReceiveAuthorization(device_status); | 166 ReceiveAuthorization(device_status); |
| 158 | 167 |
| 159 audio_device_->Initialize(default_audio_parameters_, | 168 audio_device_->Initialize(default_audio_parameters_, |
| 160 &callback_); | 169 &callback_); |
| 161 } | 170 } |
| 162 | 171 |
| 172 void AudioOutputDeviceTest::CheckDeviceStatus(OutputDeviceStatus status) { |
| 173 DCHECK(!io_loop_.task_runner()->BelongsToCurrentThread()); |
| 174 EXPECT_EQ(status, audio_device_->GetOutputDeviceInfo().device_status()); |
| 175 } |
| 176 |
| 163 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { | 177 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { |
| 164 device_status_ = status; | 178 device_status_ = status; |
| 165 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) | 179 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) |
| 166 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 180 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 167 | 181 |
| 168 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, | 182 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, |
| 169 kDefaultDeviceId); | 183 kDefaultDeviceId); |
| 170 io_loop_.RunUntilIdle(); | 184 io_loop_.RunUntilIdle(); |
| 171 } | 185 } |
| 172 | 186 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 325 |
| 312 StopAudioDevice(); | 326 StopAudioDevice(); |
| 313 } | 327 } |
| 314 | 328 |
| 315 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { | 329 TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { |
| 316 SetDevice(kUnauthorizedDeviceId); | 330 SetDevice(kUnauthorizedDeviceId); |
| 317 StartAudioDevice(); | 331 StartAudioDevice(); |
| 318 StopAudioDevice(); | 332 StopAudioDevice(); |
| 319 } | 333 } |
| 320 | 334 |
| 335 TEST_P(AudioOutputDeviceTest, AuthorizationTimedOut) { |
| 336 base::Thread thread("DeviceInfo"); |
| 337 thread.Start(); |
| 338 |
| 339 CreateDevice(kNonDefaultDeviceId); |
| 340 EXPECT_CALL(*audio_output_ipc_, |
| 341 RequestDeviceAuthorization(audio_device_.get(), 0, |
| 342 kNonDefaultDeviceId, _)); |
| 343 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 344 |
| 345 // Request authorization; no reply from the browser. |
| 346 audio_device_->RequestDeviceAuthorization(); |
| 347 |
| 348 media::WaitableMessageLoopEvent event; |
| 349 |
| 350 // Request device info on another thread. |
| 351 thread.task_runner()->PostTaskAndReply( |
| 352 FROM_HERE, |
| 353 base::Bind(&AudioOutputDeviceTest::CheckDeviceStatus, |
| 354 base::Unretained(this), OUTPUT_DEVICE_STATUS_ERROR_INTERNAL), |
| 355 event.GetClosure()); |
| 356 |
| 357 io_loop_.RunUntilIdle(); |
| 358 |
| 359 // Runs the loop and waits for |thread| to call event's closure. |
| 360 event.RunAndWait(); |
| 361 |
| 362 audio_device_->Stop(); |
| 363 io_loop_.RunUntilIdle(); |
| 364 } |
| 365 |
| 321 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 366 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
| 322 | 367 |
| 323 } // namespace media. | 368 } // namespace media. |
| OLD | NEW |