| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 11 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 12 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| 13 #include "base/sync_socket.h" | 11 #include "base/sync_socket.h" |
| 14 #include "base/task_runner.h" | |
| 15 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "media/audio/audio_output_device.h" | 13 #include "media/audio/audio_output_device.h" |
| 18 #include "media/audio/sample_rates.h" | 14 #include "media/audio/sample_rates.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gmock_mutant.h" | 16 #include "testing/gmock_mutant.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 18 |
| 23 using base::CancelableSyncSocket; | 19 using base::CancelableSyncSocket; |
| 24 using base::SharedMemory; | 20 using base::SharedMemory; |
| 25 using base::SyncSocket; | 21 using base::SyncSocket; |
| 26 using testing::_; | 22 using testing::_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 49 MockAudioOutputIPC() {} | 45 MockAudioOutputIPC() {} |
| 50 virtual ~MockAudioOutputIPC() {} | 46 virtual ~MockAudioOutputIPC() {} |
| 51 | 47 |
| 52 MOCK_METHOD3(CreateStream, void(AudioOutputIPCDelegate* delegate, | 48 MOCK_METHOD3(CreateStream, void(AudioOutputIPCDelegate* delegate, |
| 53 const AudioParameters& params, | 49 const AudioParameters& params, |
| 54 int session_id)); | 50 int session_id)); |
| 55 MOCK_METHOD0(PlayStream, void()); | 51 MOCK_METHOD0(PlayStream, void()); |
| 56 MOCK_METHOD0(PauseStream, void()); | 52 MOCK_METHOD0(PauseStream, void()); |
| 57 MOCK_METHOD0(CloseStream, void()); | 53 MOCK_METHOD0(CloseStream, void()); |
| 58 MOCK_METHOD1(SetVolume, void(double volume)); | 54 MOCK_METHOD1(SetVolume, void(double volume)); |
| 59 MOCK_METHOD3(SwitchOutputDevice, | |
| 60 void(const std::string& device_id, | |
| 61 const GURL& security_origin, | |
| 62 int request_id)); | |
| 63 }; | |
| 64 | |
| 65 class MockSwitchOutputDeviceCallback { | |
| 66 public: | |
| 67 MOCK_METHOD1(Callback, void(media::SwitchOutputDeviceResult result)); | |
| 68 }; | 55 }; |
| 69 | 56 |
| 70 ACTION_P2(SendPendingBytes, socket, pending_bytes) { | 57 ACTION_P2(SendPendingBytes, socket, pending_bytes) { |
| 71 socket->Send(&pending_bytes, sizeof(pending_bytes)); | 58 socket->Send(&pending_bytes, sizeof(pending_bytes)); |
| 72 } | 59 } |
| 73 | 60 |
| 74 // Used to terminate a loop from a different thread than the loop belongs to. | 61 // Used to terminate a loop from a different thread than the loop belongs to. |
| 75 // |task_runner| should be a SingleThreadTaskRunner. | 62 // |task_runner| should be a SingleThreadTaskRunner. |
| 76 ACTION_P(QuitLoop, task_runner) { | 63 ACTION_P(QuitLoop, task_runner) { |
| 77 task_runner->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 64 task_runner->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 78 } | 65 } |
| 79 | 66 |
| 80 } // namespace. | 67 } // namespace. |
| 81 | 68 |
| 82 class AudioOutputDeviceTest | 69 class AudioOutputDeviceTest |
| 83 : public testing::Test, | 70 : public testing::Test, |
| 84 public testing::WithParamInterface<bool> { | 71 public testing::WithParamInterface<bool> { |
| 85 public: | 72 public: |
| 86 AudioOutputDeviceTest(); | 73 AudioOutputDeviceTest(); |
| 87 ~AudioOutputDeviceTest(); | 74 ~AudioOutputDeviceTest(); |
| 88 | 75 |
| 89 void StartAudioDevice(); | 76 void StartAudioDevice(); |
| 90 void CreateStream(); | 77 void CreateStream(); |
| 91 void ExpectRenderCallback(); | 78 void ExpectRenderCallback(); |
| 92 void WaitUntilRenderCallback(); | 79 void WaitUntilRenderCallback(); |
| 93 void StopAudioDevice(); | 80 void StopAudioDevice(); |
| 94 void SwitchOutputDevice(); | |
| 95 | 81 |
| 96 protected: | 82 protected: |
| 97 // Used to clean up TLS pointers that the test(s) will initialize. | 83 // Used to clean up TLS pointers that the test(s) will initialize. |
| 98 // Must remain the first member of this class. | 84 // Must remain the first member of this class. |
| 99 base::ShadowingAtExitManager at_exit_manager_; | 85 base::ShadowingAtExitManager at_exit_manager_; |
| 100 base::MessageLoopForIO io_loop_; | 86 base::MessageLoopForIO io_loop_; |
| 101 AudioParameters default_audio_parameters_; | 87 AudioParameters default_audio_parameters_; |
| 102 StrictMock<MockRenderCallback> callback_; | 88 StrictMock<MockRenderCallback> callback_; |
| 103 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ | 89 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
| 104 scoped_refptr<AudioOutputDevice> audio_device_; | 90 scoped_refptr<AudioOutputDevice> audio_device_; |
| 105 MockSwitchOutputDeviceCallback switch_output_device_callback_; | |
| 106 | 91 |
| 107 private: | 92 private: |
| 108 int CalculateMemorySize(); | 93 int CalculateMemorySize(); |
| 109 void SwitchOutputDeviceCallback(SwitchOutputDeviceResult result); | |
| 110 | 94 |
| 111 SharedMemory shared_memory_; | 95 SharedMemory shared_memory_; |
| 112 CancelableSyncSocket browser_socket_; | 96 CancelableSyncSocket browser_socket_; |
| 113 CancelableSyncSocket renderer_socket_; | 97 CancelableSyncSocket renderer_socket_; |
| 114 | 98 |
| 115 DISALLOW_COPY_AND_ASSIGN(AudioOutputDeviceTest); | 99 DISALLOW_COPY_AND_ASSIGN(AudioOutputDeviceTest); |
| 116 }; | 100 }; |
| 117 | 101 |
| 118 int AudioOutputDeviceTest::CalculateMemorySize() { | 102 int AudioOutputDeviceTest::CalculateMemorySize() { |
| 119 // Calculate output memory size. | 103 // Calculate output memory size. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 189 } |
| 206 | 190 |
| 207 void AudioOutputDeviceTest::StopAudioDevice() { | 191 void AudioOutputDeviceTest::StopAudioDevice() { |
| 208 audio_device_->Stop(); | 192 audio_device_->Stop(); |
| 209 | 193 |
| 210 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 194 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 211 | 195 |
| 212 io_loop_.RunUntilIdle(); | 196 io_loop_.RunUntilIdle(); |
| 213 } | 197 } |
| 214 | 198 |
| 215 void AudioOutputDeviceTest::SwitchOutputDevice() { | |
| 216 const GURL security_origin("http://localhost"); | |
| 217 const std::string device_id; | |
| 218 const int request_id = 1; | |
| 219 | |
| 220 // Switch the output device and check that the IPC message is sent | |
| 221 EXPECT_CALL(*audio_output_ipc_, | |
| 222 SwitchOutputDevice(device_id, security_origin, request_id)); | |
| 223 audio_device_->SwitchOutputDevice( | |
| 224 device_id, security_origin, | |
| 225 base::Bind(&MockSwitchOutputDeviceCallback::Callback, | |
| 226 base::Unretained(&switch_output_device_callback_))); | |
| 227 io_loop_.RunUntilIdle(); | |
| 228 | |
| 229 // Simulate the reception of a successful response from the browser | |
| 230 EXPECT_CALL(switch_output_device_callback_, | |
| 231 Callback(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); | |
| 232 audio_device_->OnOutputDeviceSwitched(request_id, | |
| 233 SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); | |
| 234 io_loop_.RunUntilIdle(); | |
| 235 } | |
| 236 | |
| 237 TEST_P(AudioOutputDeviceTest, Initialize) { | 199 TEST_P(AudioOutputDeviceTest, Initialize) { |
| 238 // Tests that the object can be constructed, initialized and destructed | 200 // Tests that the object can be constructed, initialized and destructed |
| 239 // without having ever been started/stopped. | 201 // without having ever been started/stopped. |
| 240 } | 202 } |
| 241 | 203 |
| 242 // Calls Start() followed by an immediate Stop() and check for the basic message | 204 // Calls Start() followed by an immediate Stop() and check for the basic message |
| 243 // filter messages being sent in that case. | 205 // filter messages being sent in that case. |
| 244 TEST_P(AudioOutputDeviceTest, StartStop) { | 206 TEST_P(AudioOutputDeviceTest, StartStop) { |
| 245 StartAudioDevice(); | 207 StartAudioDevice(); |
| 246 StopAudioDevice(); | 208 StopAudioDevice(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 270 | 232 |
| 271 // Full test with output only. | 233 // Full test with output only. |
| 272 TEST_P(AudioOutputDeviceTest, CreateStream) { | 234 TEST_P(AudioOutputDeviceTest, CreateStream) { |
| 273 StartAudioDevice(); | 235 StartAudioDevice(); |
| 274 ExpectRenderCallback(); | 236 ExpectRenderCallback(); |
| 275 CreateStream(); | 237 CreateStream(); |
| 276 WaitUntilRenderCallback(); | 238 WaitUntilRenderCallback(); |
| 277 StopAudioDevice(); | 239 StopAudioDevice(); |
| 278 } | 240 } |
| 279 | 241 |
| 280 // Switch the output device | |
| 281 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) { | |
| 282 StartAudioDevice(); | |
| 283 SwitchOutputDevice(); | |
| 284 StopAudioDevice(); | |
| 285 } | |
| 286 | |
| 287 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 242 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
| 288 | 243 |
| 289 } // namespace media. | 244 } // namespace media. |
| OLD | NEW |