Chromium Code Reviews| 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, | |
| 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)); | |
| 55 }; | 68 }; |
| 56 | 69 |
| 57 ACTION_P2(SendPendingBytes, socket, pending_bytes) { | 70 ACTION_P2(SendPendingBytes, socket, pending_bytes) { |
| 58 socket->Send(&pending_bytes, sizeof(pending_bytes)); | 71 socket->Send(&pending_bytes, sizeof(pending_bytes)); |
| 59 } | 72 } |
| 60 | 73 |
| 61 // Used to terminate a loop from a different thread than the loop belongs to. | 74 // Used to terminate a loop from a different thread than the loop belongs to. |
| 62 // |loop| should be a MessageLoopProxy. | 75 // |loop| should be a MessageLoopProxy. |
| 63 ACTION_P(QuitLoop, loop) { | 76 ACTION_P(QuitLoop, loop) { |
| 64 loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 77 loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 65 } | 78 } |
| 66 | 79 |
| 67 } // namespace. | 80 } // namespace. |
| 68 | 81 |
| 69 class AudioOutputDeviceTest | 82 class AudioOutputDeviceTest |
| 70 : public testing::Test, | 83 : public testing::Test, |
| 71 public testing::WithParamInterface<bool> { | 84 public testing::WithParamInterface<bool> { |
| 72 public: | 85 public: |
| 73 AudioOutputDeviceTest(); | 86 AudioOutputDeviceTest(); |
| 74 ~AudioOutputDeviceTest(); | 87 ~AudioOutputDeviceTest(); |
| 75 | 88 |
| 76 void StartAudioDevice(); | 89 void StartAudioDevice(); |
| 77 void CreateStream(); | 90 void CreateStream(); |
| 78 void ExpectRenderCallback(); | 91 void ExpectRenderCallback(); |
| 79 void WaitUntilRenderCallback(); | 92 void WaitUntilRenderCallback(); |
| 80 void StopAudioDevice(); | 93 void StopAudioDevice(); |
| 94 void SwitchOutputDevice(); | |
| 81 | 95 |
| 82 protected: | 96 protected: |
| 83 // Used to clean up TLS pointers that the test(s) will initialize. | 97 // Used to clean up TLS pointers that the test(s) will initialize. |
| 84 // Must remain the first member of this class. | 98 // Must remain the first member of this class. |
| 85 base::ShadowingAtExitManager at_exit_manager_; | 99 base::ShadowingAtExitManager at_exit_manager_; |
| 86 base::MessageLoopForIO io_loop_; | 100 base::MessageLoopForIO io_loop_; |
| 87 AudioParameters default_audio_parameters_; | 101 AudioParameters default_audio_parameters_; |
| 88 StrictMock<MockRenderCallback> callback_; | 102 StrictMock<MockRenderCallback> callback_; |
| 89 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ | 103 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
| 90 scoped_refptr<AudioOutputDevice> audio_device_; | 104 scoped_refptr<AudioOutputDevice> audio_device_; |
| 105 MockSwitchOutputDeviceCallback switch_output_device_callback_; | |
| 91 | 106 |
| 92 private: | 107 private: |
| 93 int CalculateMemorySize(); | 108 int CalculateMemorySize(); |
| 109 void SwitchOutputDeviceCallback(SwitchOutputDeviceResult result); | |
| 94 | 110 |
| 95 SharedMemory shared_memory_; | 111 SharedMemory shared_memory_; |
| 96 CancelableSyncSocket browser_socket_; | 112 CancelableSyncSocket browser_socket_; |
| 97 CancelableSyncSocket renderer_socket_; | 113 CancelableSyncSocket renderer_socket_; |
| 98 | 114 |
| 99 DISALLOW_COPY_AND_ASSIGN(AudioOutputDeviceTest); | 115 DISALLOW_COPY_AND_ASSIGN(AudioOutputDeviceTest); |
| 100 }; | 116 }; |
| 101 | 117 |
| 102 int AudioOutputDeviceTest::CalculateMemorySize() { | 118 int AudioOutputDeviceTest::CalculateMemorySize() { |
| 103 // Calculate output memory size. | 119 // Calculate output memory size. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 } | 205 } |
| 190 | 206 |
| 191 void AudioOutputDeviceTest::StopAudioDevice() { | 207 void AudioOutputDeviceTest::StopAudioDevice() { |
| 192 audio_device_->Stop(); | 208 audio_device_->Stop(); |
| 193 | 209 |
| 194 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 210 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 195 | 211 |
| 196 io_loop_.RunUntilIdle(); | 212 io_loop_.RunUntilIdle(); |
| 197 } | 213 } |
| 198 | 214 |
| 215 void AudioOutputDeviceTest::SwitchOutputDevice() { | |
| 216 const GURL kSecurityOrigin("http://localhost"); | |
|
tommi (sloooow) - chröme
2015/06/13 06:16:04
this should just be security_origin.
the style gu
Guido Urdaneta
2015/06/13 10:45:50
Done.
| |
| 217 const std::string kDeviceId = ""; | |
|
tommi (sloooow) - chröme
2015/06/13 06:16:04
device_id
(and don't think that initialization to
Guido Urdaneta
2015/06/13 10:45:50
Done.
| |
| 218 const int kRequestId = 1; | |
|
tommi (sloooow) - chröme
2015/06/13 06:16:04
request_id
Guido Urdaneta
2015/06/13 10:45:50
Done.
| |
| 219 | |
| 220 // Switch the output device and check that the IPC message is sent | |
| 221 EXPECT_CALL(*audio_output_ipc_, | |
| 222 SwitchOutputDevice(kDeviceId, kSecurityOrigin, kRequestId)); | |
| 223 audio_device_->SwitchOutputDevice( | |
| 224 kDeviceId, kSecurityOrigin, | |
| 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(kRequestId, | |
| 233 SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); | |
| 234 io_loop_.RunUntilIdle(); | |
| 235 } | |
| 236 | |
| 199 TEST_P(AudioOutputDeviceTest, Initialize) { | 237 TEST_P(AudioOutputDeviceTest, Initialize) { |
| 200 // Tests that the object can be constructed, initialized and destructed | 238 // Tests that the object can be constructed, initialized and destructed |
| 201 // without having ever been started/stopped. | 239 // without having ever been started/stopped. |
| 202 } | 240 } |
| 203 | 241 |
| 204 // Calls Start() followed by an immediate Stop() and check for the basic message | 242 // Calls Start() followed by an immediate Stop() and check for the basic message |
| 205 // filter messages being sent in that case. | 243 // filter messages being sent in that case. |
| 206 TEST_P(AudioOutputDeviceTest, StartStop) { | 244 TEST_P(AudioOutputDeviceTest, StartStop) { |
| 207 StartAudioDevice(); | 245 StartAudioDevice(); |
| 208 StopAudioDevice(); | 246 StopAudioDevice(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 232 | 270 |
| 233 // Full test with output only. | 271 // Full test with output only. |
| 234 TEST_P(AudioOutputDeviceTest, CreateStream) { | 272 TEST_P(AudioOutputDeviceTest, CreateStream) { |
| 235 StartAudioDevice(); | 273 StartAudioDevice(); |
| 236 ExpectRenderCallback(); | 274 ExpectRenderCallback(); |
| 237 CreateStream(); | 275 CreateStream(); |
| 238 WaitUntilRenderCallback(); | 276 WaitUntilRenderCallback(); |
| 239 StopAudioDevice(); | 277 StopAudioDevice(); |
| 240 } | 278 } |
| 241 | 279 |
| 280 // Switch the output device | |
| 281 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) { | |
| 282 StartAudioDevice(); | |
| 283 SwitchOutputDevice(); | |
| 284 StopAudioDevice(); | |
| 285 } | |
| 286 | |
| 242 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 287 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
| 243 | 288 |
| 244 } // namespace media. | 289 } // namespace media. |
| OLD | NEW |