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