OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
12 #include "content/browser/renderer_host/media/audio_input_device_manager_event_h
andler.h" | 12 #include "content/browser/renderer_host/media/audio_input_device_manager_event_h
andler.h" |
13 #include "media/audio/audio_manager_base.h" | 13 #include "media/audio/audio_manager_base.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using ::testing::_; | |
18 using ::testing::AnyNumber; | |
19 using ::testing::InSequence; | |
20 using ::testing::Return; | |
21 using content::BrowserThread; | 17 using content::BrowserThread; |
22 | |
23 using content::BrowserThreadImpl; | 18 using content::BrowserThreadImpl; |
| 19 using media_stream::AudioInputDeviceManager; |
| 20 using testing::_; |
| 21 using testing::AnyNumber; |
| 22 using testing::InSequence; |
| 23 using testing::Return; |
24 | 24 |
25 namespace media_stream { | 25 namespace media_stream { |
26 | 26 |
27 class MockAudioInputDeviceManagerListener | 27 class MockAudioInputDeviceManagerListener |
28 : public MediaStreamProviderListener { | 28 : public MediaStreamProviderListener { |
29 public: | 29 public: |
30 MockAudioInputDeviceManagerListener() {} | 30 MockAudioInputDeviceManagerListener() {} |
31 virtual ~MockAudioInputDeviceManagerListener() {} | 31 virtual ~MockAudioInputDeviceManagerListener() {} |
32 | 32 |
33 MOCK_METHOD2(Opened, void(MediaStreamType, const int)); | 33 MOCK_METHOD2(Opened, void(MediaStreamType, const int)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 FROM_HERE, base::Bind( | 73 FROM_HERE, base::Bind( |
74 &MockAudioInputDeviceManagerEventHandler::DeviceStopped, | 74 &MockAudioInputDeviceManagerEventHandler::DeviceStopped, |
75 base::Unretained(this), session_id)); | 75 base::Unretained(this), session_id)); |
76 } | 76 } |
77 | 77 |
78 private: | 78 private: |
79 MessageLoop* message_loop_; | 79 MessageLoop* message_loop_; |
80 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler); | 80 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler); |
81 }; | 81 }; |
82 | 82 |
83 // Returns true if machine has audio input device, else returns false. | |
84 static bool CanRunAudioInputDeviceTests() { | |
85 AudioManager* audio_manager = AudioManager::GetAudioManager(); | |
86 if (!audio_manager) | |
87 return false; | |
88 | |
89 return audio_manager->HasAudioInputDevices(); | |
90 } | |
91 | |
92 ACTION_P(ExitMessageLoop, message_loop) { | 83 ACTION_P(ExitMessageLoop, message_loop) { |
93 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 84 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
94 } | 85 } |
95 | 86 |
96 class AudioInputDeviceManagerTest: public testing::Test { | 87 class AudioInputDeviceManagerTest : public testing::Test { |
97 public: | 88 public: |
98 AudioInputDeviceManagerTest() | 89 AudioInputDeviceManagerTest() |
99 : message_loop_(), | 90 : message_loop_(), |
100 io_thread_(), | 91 io_thread_(), |
101 manager_(), | 92 manager_(), |
102 audio_input_listener_() {} | 93 audio_input_listener_() { |
| 94 } |
| 95 |
| 96 // Returns true iff machine has an audio input device. |
| 97 bool CanRunAudioInputDeviceTests() { |
| 98 return audio_manager_->HasAudioInputDevices(); |
| 99 } |
103 | 100 |
104 protected: | 101 protected: |
105 virtual void SetUp() { | 102 virtual void SetUp() { |
106 // The test must run on Browser::IO. | 103 // The test must run on Browser::IO. |
107 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 104 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
108 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 105 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
109 message_loop_.get())); | 106 message_loop_.get())); |
110 manager_.reset(new media_stream::AudioInputDeviceManager()); | 107 audio_manager_ = AudioManager::Create(); |
| 108 |
| 109 manager_.reset(new AudioInputDeviceManager(audio_manager_)); |
111 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); | 110 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
112 manager_->Register(audio_input_listener_.get()); | 111 manager_->Register(audio_input_listener_.get()); |
113 | 112 |
114 // Gets the enumerated device list from the AudioInputDeviceManager. | 113 // Gets the enumerated device list from the AudioInputDeviceManager. |
115 manager_->EnumerateDevices(); | 114 manager_->EnumerateDevices(); |
116 EXPECT_CALL(*audio_input_listener_, DevicesEnumerated(_)) | 115 EXPECT_CALL(*audio_input_listener_, DevicesEnumerated(_)) |
117 .Times(1); | 116 .Times(1); |
118 | 117 |
119 // Waits for the callback. | 118 // Waits for the callback. |
120 message_loop_->RunAllPending(); | 119 message_loop_->RunAllPending(); |
121 } | 120 } |
122 | 121 |
123 virtual void TearDown() { | 122 virtual void TearDown() { |
124 manager_->Unregister(); | 123 manager_->Unregister(); |
125 io_thread_.reset(); | 124 io_thread_.reset(); |
126 } | 125 } |
127 | 126 |
128 scoped_ptr<MessageLoop> message_loop_; | 127 scoped_ptr<MessageLoop> message_loop_; |
129 scoped_ptr<BrowserThreadImpl> io_thread_; | 128 scoped_ptr<BrowserThreadImpl> io_thread_; |
130 scoped_ptr<AudioInputDeviceManager> manager_; | 129 scoped_ptr<AudioInputDeviceManager> manager_; |
131 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 130 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
| 131 scoped_refptr<AudioManager> audio_manager_; |
132 | 132 |
133 private: | 133 private: |
134 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest); | 134 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest); |
135 }; | 135 }; |
136 | 136 |
137 // Opens and closes the devices. | 137 // Opens and closes the devices. |
138 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 138 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
139 if (!CanRunAudioInputDeviceTests()) | 139 if (!CanRunAudioInputDeviceTests()) |
140 return; | 140 return; |
| 141 |
| 142 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 143 |
141 InSequence s; | 144 InSequence s; |
142 | 145 |
143 for (StreamDeviceInfoArray::const_iterator iter = | 146 for (StreamDeviceInfoArray::const_iterator iter = |
144 audio_input_listener_->devices_.begin(); | 147 audio_input_listener_->devices_.begin(); |
145 iter != audio_input_listener_->devices_.end(); ++iter) { | 148 iter != audio_input_listener_->devices_.end(); ++iter) { |
146 // Opens/closes the devices. | 149 // Opens/closes the devices. |
147 int session_id = manager_->Open(*iter); | 150 int session_id = manager_->Open(*iter); |
148 manager_->Close(session_id); | 151 manager_->Close(session_id); |
149 | 152 |
150 // Expected mock call with expected return value. | 153 // Expected mock call with expected return value. |
151 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) | 154 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) |
152 .Times(1); | 155 .Times(1); |
153 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) | 156 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
154 .Times(1); | 157 .Times(1); |
155 | 158 |
156 // Waits for the callback. | 159 // Waits for the callback. |
157 message_loop_->RunAllPending(); | 160 message_loop_->RunAllPending(); |
158 } | 161 } |
159 } | 162 } |
160 | 163 |
161 // Opens multiple devices at one time and closes them later. | 164 // Opens multiple devices at one time and closes them later. |
162 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) { | 165 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) { |
163 if (!CanRunAudioInputDeviceTests()) | 166 if (!CanRunAudioInputDeviceTests()) |
164 return; | 167 return; |
| 168 |
| 169 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 170 |
165 InSequence s; | 171 InSequence s; |
166 | 172 |
167 int index = 0; | 173 int index = 0; |
168 const int kDeviceSize = audio_input_listener_->devices_.size(); | 174 const int kDeviceSize = audio_input_listener_->devices_.size(); |
169 scoped_array<int> session_id(new int[kDeviceSize]); | 175 scoped_array<int> session_id(new int[kDeviceSize]); |
170 | 176 |
171 // Opens the devices in a loop. | 177 // Opens the devices in a loop. |
172 for (StreamDeviceInfoArray::const_iterator iter = | 178 for (StreamDeviceInfoArray::const_iterator iter = |
173 audio_input_listener_->devices_.begin(); | 179 audio_input_listener_->devices_.begin(); |
174 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { | 180 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 .Times(1); | 224 .Times(1); |
219 | 225 |
220 // Waits for the callback. | 226 // Waits for the callback. |
221 message_loop_->RunAllPending(); | 227 message_loop_->RunAllPending(); |
222 } | 228 } |
223 | 229 |
224 // Opens default device twice. | 230 // Opens default device twice. |
225 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { | 231 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { |
226 if (!CanRunAudioInputDeviceTests()) | 232 if (!CanRunAudioInputDeviceTests()) |
227 return; | 233 return; |
| 234 |
| 235 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 236 |
228 InSequence s; | 237 InSequence s; |
229 | 238 |
230 // Opens and closes the default device twice. | 239 // Opens and closes the default device twice. |
231 int first_session_id = manager_->Open( | 240 int first_session_id = manager_->Open( |
232 audio_input_listener_->devices_.front()); | 241 audio_input_listener_->devices_.front()); |
233 int second_session_id = manager_->Open( | 242 int second_session_id = manager_->Open( |
234 audio_input_listener_->devices_.front()); | 243 audio_input_listener_->devices_.front()); |
235 manager_->Close(first_session_id); | 244 manager_->Close(first_session_id); |
236 manager_->Close(second_session_id); | 245 manager_->Close(second_session_id); |
237 | 246 |
238 // Expected mock calls with expected returned values. | 247 // Expected mock calls with expected returned values. |
239 EXPECT_NE(first_session_id, second_session_id); | 248 EXPECT_NE(first_session_id, second_session_id); |
240 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) | 249 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) |
241 .Times(1); | 250 .Times(1); |
242 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) | 251 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) |
243 .Times(1); | 252 .Times(1); |
244 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) | 253 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) |
245 .Times(1); | 254 .Times(1); |
246 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) | 255 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
247 .Times(1); | 256 .Times(1); |
248 | 257 |
249 // Waits for the callback. | 258 // Waits for the callback. |
250 message_loop_->RunAllPending(); | 259 message_loop_->RunAllPending(); |
251 } | 260 } |
252 | 261 |
253 // Starts and closes the sessions after opening the devices. | 262 // Starts and closes the sessions after opening the devices. |
254 TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) { | 263 TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) { |
255 if (!CanRunAudioInputDeviceTests()) | 264 if (!CanRunAudioInputDeviceTests()) |
256 return; | 265 return; |
| 266 |
| 267 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 268 |
257 InSequence s; | 269 InSequence s; |
258 | 270 |
259 int index = 0; | 271 int index = 0; |
260 const int kDeviceSize = audio_input_listener_->devices_.size(); | 272 const int kDeviceSize = audio_input_listener_->devices_.size(); |
261 scoped_array<int> session_id(new int[kDeviceSize]); | 273 scoped_array<int> session_id(new int[kDeviceSize]); |
262 | 274 |
263 // Creates the EventHandler for the sessions. | 275 // Creates the EventHandler for the sessions. |
264 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 276 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
265 audio_input_event_handler( | 277 audio_input_event_handler( |
266 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | 278 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
(...skipping 23 matching lines...) Expand all Loading... |
290 session_id[index])) | 302 session_id[index])) |
291 .Times(1); | 303 .Times(1); |
292 message_loop_->RunAllPending(); | 304 message_loop_->RunAllPending(); |
293 } | 305 } |
294 } | 306 } |
295 | 307 |
296 // Tests the behavior of calling Close() without calling Stop(). | 308 // Tests the behavior of calling Close() without calling Stop(). |
297 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) { | 309 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) { |
298 if (!CanRunAudioInputDeviceTests()) | 310 if (!CanRunAudioInputDeviceTests()) |
299 return; | 311 return; |
| 312 |
| 313 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 314 |
300 InSequence s; | 315 InSequence s; |
301 | 316 |
302 int index = 0; | 317 int index = 0; |
303 const int kDeviceSize = audio_input_listener_->devices_.size(); | 318 const int kDeviceSize = audio_input_listener_->devices_.size(); |
304 scoped_array<int> session_id(new int[kDeviceSize]); | 319 scoped_array<int> session_id(new int[kDeviceSize]); |
305 | 320 |
306 // Creates the EventHandlers for the sessions. | 321 // Creates the EventHandlers for the sessions. |
307 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 322 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
308 audio_input_event_handler( | 323 audio_input_event_handler( |
309 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | 324 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
(...skipping 26 matching lines...) Expand all Loading... |
336 session_id[index])) | 351 session_id[index])) |
337 .Times(1); | 352 .Times(1); |
338 message_loop_->RunAllPending(); | 353 message_loop_->RunAllPending(); |
339 } | 354 } |
340 } | 355 } |
341 | 356 |
342 // Starts the same device twice. | 357 // Starts the same device twice. |
343 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { | 358 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { |
344 if (!CanRunAudioInputDeviceTests()) | 359 if (!CanRunAudioInputDeviceTests()) |
345 return; | 360 return; |
| 361 |
| 362 ASSERT_FALSE(audio_input_listener_->devices_.empty()); |
| 363 |
346 InSequence s; | 364 InSequence s; |
347 | 365 |
348 // Create one EventHandler for each session. | 366 // Create one EventHandler for each session. |
349 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 367 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
350 first_event_handler( | 368 first_event_handler( |
351 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | 369 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
352 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 370 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
353 second_event_handler( | 371 second_event_handler( |
354 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | 372 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
355 | 373 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 .Times(1); | 478 .Times(1); |
461 | 479 |
462 manager_->Stop(session_id); | 480 manager_->Stop(session_id); |
463 manager_->Close(session_id); | 481 manager_->Close(session_id); |
464 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) | 482 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
465 .Times(1); | 483 .Times(1); |
466 message_loop_->RunAllPending(); | 484 message_loop_->RunAllPending(); |
467 } | 485 } |
468 | 486 |
469 } // namespace media_stream | 487 } // namespace media_stream |
OLD | NEW |