| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #if defined(OS_ANDROID) | 47 #if defined(OS_ANDROID) |
| 48 #define MAYBE_AudioInputDeviceManagerTest DISABLED_AudioInputDeviceManagerTest | 48 #define MAYBE_AudioInputDeviceManagerTest DISABLED_AudioInputDeviceManagerTest |
| 49 #else | 49 #else |
| 50 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest | 50 #define MAYBE_AudioInputDeviceManagerTest AudioInputDeviceManagerTest |
| 51 #endif | 51 #endif |
| 52 | 52 |
| 53 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { | 53 class MAYBE_AudioInputDeviceManagerTest : public testing::Test { |
| 54 public: | 54 public: |
| 55 MAYBE_AudioInputDeviceManagerTest() {} | 55 MAYBE_AudioInputDeviceManagerTest() {} |
| 56 | 56 |
| 57 // Returns true iff machine has an audio input device. | |
| 58 bool CanRunAudioInputDeviceTests() { | |
| 59 return audio_manager_->HasAudioInputDevices(); | |
| 60 } | |
| 61 | |
| 62 protected: | 57 protected: |
| 63 virtual void SetUp() OVERRIDE { | 58 virtual void SetUp() OVERRIDE { |
| 64 // The test must run on Browser::IO. | 59 // The test must run on Browser::IO. |
| 65 message_loop_.reset(new base::MessageLoopForIO); | 60 message_loop_.reset(new base::MessageLoopForIO); |
| 66 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 61 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 67 message_loop_.get())); | 62 message_loop_.get())); |
| 68 audio_manager_.reset(media::AudioManager::CreateForTesting()); | 63 audio_manager_.reset(media::AudioManager::CreateForTesting()); |
| 69 // Wait for audio thread initialization to complete. Otherwise the | 64 // Wait for audio thread initialization to complete. Otherwise the |
| 70 // enumeration type may not have been set yet. | 65 // enumeration type may not have been set yet. |
| 71 base::WaitableEvent event(false, false); | 66 base::WaitableEvent event(false, false); |
| 72 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( | 67 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( |
| 73 &base::WaitableEvent::Signal, base::Unretained(&event))); | 68 &base::WaitableEvent::Signal, base::Unretained(&event))); |
| 74 event.Wait(); | 69 event.Wait(); |
| 75 manager_ = new AudioInputDeviceManager(audio_manager_.get()); | 70 manager_ = new AudioInputDeviceManager(audio_manager_.get()); |
| 71 manager_->UseFakeDevice(); |
| 76 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); | 72 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
| 77 manager_->Register(audio_input_listener_.get(), | 73 manager_->Register(audio_input_listener_.get(), |
| 78 message_loop_->message_loop_proxy().get()); | 74 message_loop_->message_loop_proxy().get()); |
| 79 | 75 |
| 80 // Gets the enumerated device list from the AudioInputDeviceManager. | 76 // Gets the enumerated device list from the AudioInputDeviceManager. |
| 81 manager_->EnumerateDevices(MEDIA_DEVICE_AUDIO_CAPTURE); | 77 manager_->EnumerateDevices(MEDIA_DEVICE_AUDIO_CAPTURE); |
| 82 EXPECT_CALL(*audio_input_listener_, | 78 EXPECT_CALL(*audio_input_listener_, |
| 83 DevicesEnumerated(MEDIA_DEVICE_AUDIO_CAPTURE, _)) | 79 DevicesEnumerated(MEDIA_DEVICE_AUDIO_CAPTURE, _)) |
| 84 .Times(1) | 80 .Times(1) |
| 85 .WillOnce(SaveArg<1>(&devices_)); | 81 .WillOnce(SaveArg<1>(&devices_)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 99 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 95 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
| 100 scoped_ptr<media::AudioManager> audio_manager_; | 96 scoped_ptr<media::AudioManager> audio_manager_; |
| 101 StreamDeviceInfoArray devices_; | 97 StreamDeviceInfoArray devices_; |
| 102 | 98 |
| 103 private: | 99 private: |
| 104 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); | 100 DISALLOW_COPY_AND_ASSIGN(MAYBE_AudioInputDeviceManagerTest); |
| 105 }; | 101 }; |
| 106 | 102 |
| 107 // Opens and closes the devices. | 103 // Opens and closes the devices. |
| 108 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 104 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
| 109 if (!CanRunAudioInputDeviceTests()) | |
| 110 return; | |
| 111 | 105 |
| 112 ASSERT_FALSE(devices_.empty()); | 106 ASSERT_FALSE(devices_.empty()); |
| 113 | 107 |
| 114 InSequence s; | 108 InSequence s; |
| 115 | 109 |
| 116 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 110 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
| 117 iter != devices_.end(); ++iter) { | 111 iter != devices_.end(); ++iter) { |
| 118 // Opens/closes the devices. | 112 // Opens/closes the devices. |
| 119 int session_id = manager_->Open(*iter); | 113 int session_id = manager_->Open(*iter); |
| 120 | 114 |
| 121 // Expected mock call with expected return value. | 115 // Expected mock call with expected return value. |
| 122 EXPECT_CALL(*audio_input_listener_, | 116 EXPECT_CALL(*audio_input_listener_, |
| 123 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 117 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
| 124 .Times(1); | 118 .Times(1); |
| 125 // Waits for the callback. | 119 // Waits for the callback. |
| 126 message_loop_->RunUntilIdle(); | 120 message_loop_->RunUntilIdle(); |
| 127 | 121 |
| 128 manager_->Close(session_id); | 122 manager_->Close(session_id); |
| 129 EXPECT_CALL(*audio_input_listener_, | 123 EXPECT_CALL(*audio_input_listener_, |
| 130 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 124 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
| 131 .Times(1); | 125 .Times(1); |
| 132 | 126 |
| 133 // Waits for the callback. | 127 // Waits for the callback. |
| 134 message_loop_->RunUntilIdle(); | 128 message_loop_->RunUntilIdle(); |
| 135 } | 129 } |
| 136 } | 130 } |
| 137 | 131 |
| 138 // Opens multiple devices at one time and closes them later. | 132 // Opens multiple devices at one time and closes them later. |
| 139 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenMultipleDevices) { | 133 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenMultipleDevices) { |
| 140 if (!CanRunAudioInputDeviceTests()) | |
| 141 return; | |
| 142 | |
| 143 ASSERT_FALSE(devices_.empty()); | 134 ASSERT_FALSE(devices_.empty()); |
| 144 | 135 |
| 145 InSequence s; | 136 InSequence s; |
| 146 | 137 |
| 147 int index = 0; | 138 int index = 0; |
| 148 scoped_ptr<int[]> session_id(new int[devices_.size()]); | 139 scoped_ptr<int[]> session_id(new int[devices_.size()]); |
| 149 | 140 |
| 150 // Opens the devices in a loop. | 141 // Opens the devices in a loop. |
| 151 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 142 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
| 152 iter != devices_.end(); ++iter, ++index) { | 143 iter != devices_.end(); ++iter, ++index) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 176 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[i])) | 167 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[i])) |
| 177 .Times(1); | 168 .Times(1); |
| 178 | 169 |
| 179 // Waits for the callback. | 170 // Waits for the callback. |
| 180 message_loop_->RunUntilIdle(); | 171 message_loop_->RunUntilIdle(); |
| 181 } | 172 } |
| 182 } | 173 } |
| 183 | 174 |
| 184 // Opens a non-existing device. | 175 // Opens a non-existing device. |
| 185 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenNotExistingDevice) { | 176 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenNotExistingDevice) { |
| 186 if (!CanRunAudioInputDeviceTests()) | |
| 187 return; | |
| 188 InSequence s; | 177 InSequence s; |
| 189 | 178 |
| 190 MediaStreamType stream_type = MEDIA_DEVICE_AUDIO_CAPTURE; | 179 MediaStreamType stream_type = MEDIA_DEVICE_AUDIO_CAPTURE; |
| 191 std::string device_name("device_doesnt_exist"); | 180 std::string device_name("device_doesnt_exist"); |
| 192 std::string device_id("id_doesnt_exist"); | 181 std::string device_id("id_doesnt_exist"); |
| 193 int sample_rate(0); | 182 int sample_rate(0); |
| 194 int channel_config(0); | 183 int channel_config(0); |
| 195 StreamDeviceInfo dummy_device( | 184 StreamDeviceInfo dummy_device( |
| 196 stream_type, device_name, device_id, sample_rate, channel_config, 2048); | 185 stream_type, device_name, device_id, sample_rate, channel_config, 2048); |
| 197 | 186 |
| 198 int session_id = manager_->Open(dummy_device); | 187 int session_id = manager_->Open(dummy_device); |
| 199 EXPECT_CALL(*audio_input_listener_, | 188 EXPECT_CALL(*audio_input_listener_, |
| 200 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 189 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
| 201 .Times(1); | 190 .Times(1); |
| 202 | 191 |
| 203 // Waits for the callback. | 192 // Waits for the callback. |
| 204 message_loop_->RunUntilIdle(); | 193 message_loop_->RunUntilIdle(); |
| 205 } | 194 } |
| 206 | 195 |
| 207 // Opens default device twice. | 196 // Opens default device twice. |
| 208 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { | 197 TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { |
| 209 if (!CanRunAudioInputDeviceTests()) | |
| 210 return; | |
| 211 | |
| 212 ASSERT_FALSE(devices_.empty()); | 198 ASSERT_FALSE(devices_.empty()); |
| 213 | 199 |
| 214 InSequence s; | 200 InSequence s; |
| 215 | 201 |
| 216 // Opens and closes the default device twice. | 202 // Opens and closes the default device twice. |
| 217 int first_session_id = manager_->Open(devices_.front()); | 203 int first_session_id = manager_->Open(devices_.front()); |
| 218 int second_session_id = manager_->Open(devices_.front()); | 204 int second_session_id = manager_->Open(devices_.front()); |
| 219 | 205 |
| 220 // Expected mock calls with expected returned values. | 206 // Expected mock calls with expected returned values. |
| 221 EXPECT_NE(first_session_id, second_session_id); | 207 EXPECT_NE(first_session_id, second_session_id); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 235 .Times(1); | 221 .Times(1); |
| 236 EXPECT_CALL(*audio_input_listener_, | 222 EXPECT_CALL(*audio_input_listener_, |
| 237 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) | 223 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
| 238 .Times(1); | 224 .Times(1); |
| 239 // Waits for the callback. | 225 // Waits for the callback. |
| 240 message_loop_->RunUntilIdle(); | 226 message_loop_->RunUntilIdle(); |
| 241 } | 227 } |
| 242 | 228 |
| 243 // Accesses then closes the sessions after opening the devices. | 229 // Accesses then closes the sessions after opening the devices. |
| 244 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessAndCloseSession) { | 230 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessAndCloseSession) { |
| 245 if (!CanRunAudioInputDeviceTests()) | |
| 246 return; | |
| 247 | |
| 248 ASSERT_FALSE(devices_.empty()); | 231 ASSERT_FALSE(devices_.empty()); |
| 249 | 232 |
| 250 InSequence s; | 233 InSequence s; |
| 251 | 234 |
| 252 int index = 0; | 235 int index = 0; |
| 253 scoped_ptr<int[]> session_id(new int[devices_.size()]); | 236 scoped_ptr<int[]> session_id(new int[devices_.size()]); |
| 254 | 237 |
| 255 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById | 238 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById |
| 256 // for each device. | 239 // for each device. |
| 257 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 240 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 271 manager_->Close(session_id[index]); | 254 manager_->Close(session_id[index]); |
| 272 EXPECT_CALL(*audio_input_listener_, | 255 EXPECT_CALL(*audio_input_listener_, |
| 273 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) | 256 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
| 274 .Times(1); | 257 .Times(1); |
| 275 message_loop_->RunUntilIdle(); | 258 message_loop_->RunUntilIdle(); |
| 276 } | 259 } |
| 277 } | 260 } |
| 278 | 261 |
| 279 // Access an invalid session. | 262 // Access an invalid session. |
| 280 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { | 263 TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { |
| 281 if (!CanRunAudioInputDeviceTests()) | |
| 282 return; | |
| 283 InSequence s; | 264 InSequence s; |
| 284 | 265 |
| 285 // Opens the first device. | 266 // Opens the first device. |
| 286 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); | 267 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); |
| 287 int session_id = manager_->Open(*iter); | 268 int session_id = manager_->Open(*iter); |
| 288 EXPECT_CALL(*audio_input_listener_, | 269 EXPECT_CALL(*audio_input_listener_, |
| 289 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 270 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
| 290 .Times(1); | 271 .Times(1); |
| 291 message_loop_->RunUntilIdle(); | 272 message_loop_->RunUntilIdle(); |
| 292 | 273 |
| 293 // Access a non-opened device. | 274 // Access a non-opened device. |
| 294 // This should fail and return an empty StreamDeviceInfo. | 275 // This should fail and return an empty StreamDeviceInfo. |
| 295 int invalid_session_id = session_id + 1; | 276 int invalid_session_id = session_id + 1; |
| 296 const StreamDeviceInfo* info = | 277 const StreamDeviceInfo* info = |
| 297 manager_->GetOpenedDeviceInfoById(invalid_session_id); | 278 manager_->GetOpenedDeviceInfoById(invalid_session_id); |
| 298 DCHECK(!info); | 279 DCHECK(!info); |
| 299 | 280 |
| 300 manager_->Close(session_id); | 281 manager_->Close(session_id); |
| 301 EXPECT_CALL(*audio_input_listener_, | 282 EXPECT_CALL(*audio_input_listener_, |
| 302 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) | 283 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
| 303 .Times(1); | 284 .Times(1); |
| 304 message_loop_->RunUntilIdle(); | 285 message_loop_->RunUntilIdle(); |
| 305 } | 286 } |
| 306 | 287 |
| 307 } // namespace content | 288 } // namespace content |
| OLD | NEW |