| 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" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 MOCK_METHOD2(Render, int(AudioBus* dest, int audio_delay_milliseconds)); | 43 MOCK_METHOD2(Render, int(AudioBus* dest, int audio_delay_milliseconds)); |
| 44 MOCK_METHOD0(OnRenderError, void()); | 44 MOCK_METHOD0(OnRenderError, void()); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 class MockAudioOutputIPC : public AudioOutputIPC { | 47 class MockAudioOutputIPC : public AudioOutputIPC { |
| 48 public: | 48 public: |
| 49 MockAudioOutputIPC() {} | 49 MockAudioOutputIPC() {} |
| 50 virtual ~MockAudioOutputIPC() {} | 50 virtual ~MockAudioOutputIPC() {} |
| 51 | 51 |
| 52 MOCK_METHOD3(CreateStream, void(AudioOutputIPCDelegate* delegate, | 52 MOCK_METHOD4(RequestDeviceAuthorization, |
| 53 const AudioParameters& params, | 53 void(AudioOutputIPCDelegate* delegate, |
| 54 int session_id)); | 54 int session_id, |
| 55 const std::string& device_id, |
| 56 const GURL& security_origin)); |
| 57 MOCK_METHOD2(CreateStream, |
| 58 void(AudioOutputIPCDelegate* delegate, |
| 59 const AudioParameters& params)); |
| 55 MOCK_METHOD0(PlayStream, void()); | 60 MOCK_METHOD0(PlayStream, void()); |
| 56 MOCK_METHOD0(PauseStream, void()); | 61 MOCK_METHOD0(PauseStream, void()); |
| 57 MOCK_METHOD0(CloseStream, void()); | 62 MOCK_METHOD0(CloseStream, void()); |
| 58 MOCK_METHOD1(SetVolume, void(double volume)); | 63 MOCK_METHOD1(SetVolume, void(double volume)); |
| 59 MOCK_METHOD3(SwitchOutputDevice, | 64 MOCK_METHOD2(SwitchOutputDevice, |
| 60 void(const std::string& device_id, | 65 void(const std::string& device_id, const GURL& security_origin)); |
| 61 const GURL& security_origin, | |
| 62 int request_id)); | |
| 63 }; | 66 }; |
| 64 | 67 |
| 65 class MockSwitchOutputDeviceCallback { | 68 class MockSwitchOutputDeviceCallback { |
| 66 public: | 69 public: |
| 67 MOCK_METHOD1(Callback, void(media::SwitchOutputDeviceResult result)); | 70 MOCK_METHOD1(Callback, void(media::SwitchOutputDeviceResult result)); |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 ACTION_P2(SendPendingBytes, socket, pending_bytes) { | 73 ACTION_P2(SendPendingBytes, socket, pending_bytes) { |
| 71 socket->Send(&pending_bytes, sizeof(pending_bytes)); | 74 socket->Send(&pending_bytes, sizeof(pending_bytes)); |
| 72 } | 75 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 125 |
| 123 AudioOutputDeviceTest::AudioOutputDeviceTest() { | 126 AudioOutputDeviceTest::AudioOutputDeviceTest() { |
| 124 default_audio_parameters_.Reset( | 127 default_audio_parameters_.Reset( |
| 125 AudioParameters::AUDIO_PCM_LINEAR, | 128 AudioParameters::AUDIO_PCM_LINEAR, |
| 126 CHANNEL_LAYOUT_STEREO, 2, 48000, 16, 1024); | 129 CHANNEL_LAYOUT_STEREO, 2, 48000, 16, 1024); |
| 127 | 130 |
| 128 audio_output_ipc_ = new MockAudioOutputIPC(); | 131 audio_output_ipc_ = new MockAudioOutputIPC(); |
| 129 audio_device_ = new AudioOutputDevice( | 132 audio_device_ = new AudioOutputDevice( |
| 130 scoped_ptr<AudioOutputIPC>(audio_output_ipc_), | 133 scoped_ptr<AudioOutputIPC>(audio_output_ipc_), |
| 131 io_loop_.task_runner()); | 134 io_loop_.task_runner()); |
| 135 audio_device_->RequestDeviceAuthorization(); |
| 136 EXPECT_CALL(*audio_output_ipc_, |
| 137 RequestDeviceAuthorization(audio_device_.get(), 0, std::string(), |
| 138 GURL::EmptyGURL())); |
| 139 |
| 140 // Simulate response from browser |
| 141 audio_device_->OnDeviceAuthorized(true, default_audio_parameters_); |
| 132 | 142 |
| 133 audio_device_->Initialize(default_audio_parameters_, | 143 audio_device_->Initialize(default_audio_parameters_, |
| 134 &callback_); | 144 &callback_); |
| 135 | 145 |
| 136 io_loop_.RunUntilIdle(); | 146 io_loop_.RunUntilIdle(); |
| 137 } | 147 } |
| 138 | 148 |
| 139 AudioOutputDeviceTest::~AudioOutputDeviceTest() { | 149 AudioOutputDeviceTest::~AudioOutputDeviceTest() { |
| 140 audio_device_ = NULL; | 150 audio_device_ = NULL; |
| 141 } | 151 } |
| 142 | 152 |
| 143 void AudioOutputDeviceTest::StartAudioDevice() { | 153 void AudioOutputDeviceTest::StartAudioDevice() { |
| 144 audio_device_->Start(); | 154 audio_device_->Start(); |
| 145 | 155 |
| 146 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _, 0)); | 156 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _)); |
| 147 | 157 |
| 148 io_loop_.RunUntilIdle(); | 158 io_loop_.RunUntilIdle(); |
| 149 } | 159 } |
| 150 | 160 |
| 151 void AudioOutputDeviceTest::CreateStream() { | 161 void AudioOutputDeviceTest::CreateStream() { |
| 152 const int kMemorySize = CalculateMemorySize(); | 162 const int kMemorySize = CalculateMemorySize(); |
| 153 | 163 |
| 154 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); | 164 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); |
| 155 memset(shared_memory_.memory(), 0xff, kMemorySize); | 165 memset(shared_memory_.memory(), 0xff, kMemorySize); |
| 156 | 166 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 audio_device_->Stop(); | 218 audio_device_->Stop(); |
| 209 | 219 |
| 210 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 220 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 211 | 221 |
| 212 io_loop_.RunUntilIdle(); | 222 io_loop_.RunUntilIdle(); |
| 213 } | 223 } |
| 214 | 224 |
| 215 void AudioOutputDeviceTest::SwitchOutputDevice() { | 225 void AudioOutputDeviceTest::SwitchOutputDevice() { |
| 216 const GURL security_origin("http://localhost"); | 226 const GURL security_origin("http://localhost"); |
| 217 const std::string device_id; | 227 const std::string device_id; |
| 218 const int request_id = 1; | |
| 219 | 228 |
| 220 // Switch the output device and check that the IPC message is sent | 229 // Switch the output device and check that the IPC message is sent |
| 221 EXPECT_CALL(*audio_output_ipc_, | 230 EXPECT_CALL(*audio_output_ipc_, |
| 222 SwitchOutputDevice(device_id, security_origin, request_id)); | 231 SwitchOutputDevice(device_id, security_origin)); |
| 223 audio_device_->SwitchOutputDevice( | 232 audio_device_->SwitchOutputDevice( |
| 224 device_id, security_origin, | 233 device_id, security_origin, |
| 225 base::Bind(&MockSwitchOutputDeviceCallback::Callback, | 234 base::Bind(&MockSwitchOutputDeviceCallback::Callback, |
| 226 base::Unretained(&switch_output_device_callback_))); | 235 base::Unretained(&switch_output_device_callback_))); |
| 227 io_loop_.RunUntilIdle(); | 236 io_loop_.RunUntilIdle(); |
| 228 | 237 |
| 229 // Simulate the reception of a successful response from the browser | 238 // Simulate the reception of a successful response from the browser |
| 230 EXPECT_CALL(switch_output_device_callback_, | 239 EXPECT_CALL(switch_output_device_callback_, |
| 231 Callback(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); | 240 Callback(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); |
| 232 audio_device_->OnOutputDeviceSwitched(request_id, | 241 audio_device_->OnOutputDeviceSwitched(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS, |
| 233 SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); | 242 default_audio_parameters_); |
| 234 io_loop_.RunUntilIdle(); | 243 io_loop_.RunUntilIdle(); |
| 235 } | 244 } |
| 236 | 245 |
| 237 TEST_P(AudioOutputDeviceTest, Initialize) { | 246 TEST_P(AudioOutputDeviceTest, Initialize) { |
| 238 // Tests that the object can be constructed, initialized and destructed | 247 // Tests that the object can be constructed, initialized and destructed |
| 239 // without having ever been started/stopped. | 248 // without having ever been started. |
| 249 StopAudioDevice(); |
| 240 } | 250 } |
| 241 | 251 |
| 242 // Calls Start() followed by an immediate Stop() and check for the basic message | 252 // Calls Start() followed by an immediate Stop() and check for the basic message |
| 243 // filter messages being sent in that case. | 253 // filter messages being sent in that case. |
| 244 TEST_P(AudioOutputDeviceTest, StartStop) { | 254 TEST_P(AudioOutputDeviceTest, StartStop) { |
| 245 StartAudioDevice(); | 255 StartAudioDevice(); |
| 246 StopAudioDevice(); | 256 StopAudioDevice(); |
| 247 } | 257 } |
| 248 | 258 |
| 249 // AudioOutputDevice supports multiple start/stop sequences. | 259 // AudioOutputDevice supports multiple start/stop sequences. |
| 250 TEST_P(AudioOutputDeviceTest, StartStopStartStop) { | 260 TEST_P(AudioOutputDeviceTest, StartStopStartStop) { |
| 251 StartAudioDevice(); | 261 StartAudioDevice(); |
| 252 StopAudioDevice(); | 262 StopAudioDevice(); |
| 263 |
| 264 // Expect extra authorization request after stopping and restarting |
| 265 EXPECT_CALL(*audio_output_ipc_, |
| 266 RequestDeviceAuthorization(audio_device_.get(), 0, "", |
| 267 GURL::EmptyGURL())); |
| 253 StartAudioDevice(); | 268 StartAudioDevice(); |
| 254 StopAudioDevice(); | 269 StopAudioDevice(); |
| 255 } | 270 } |
| 256 | 271 |
| 257 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread() | 272 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread() |
| 258 // on the IO loop. | 273 // on the IO loop. |
| 259 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { | 274 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { |
| 260 StartAudioDevice(); | 275 StartAudioDevice(); |
| 261 | 276 |
| 262 // Call Stop() but don't run the IO loop yet. | 277 // Call Stop() but don't run the IO loop yet. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 280 // Switch the output device | 295 // Switch the output device |
| 281 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) { | 296 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) { |
| 282 StartAudioDevice(); | 297 StartAudioDevice(); |
| 283 SwitchOutputDevice(); | 298 SwitchOutputDevice(); |
| 284 StopAudioDevice(); | 299 StopAudioDevice(); |
| 285 } | 300 } |
| 286 | 301 |
| 287 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 302 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
| 288 | 303 |
| 289 } // namespace media. | 304 } // namespace media. |
| OLD | NEW |