Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: content/browser/renderer_host/media/audio_input_device_manager_unittest.cc

Issue 8677012: Refactor the AudioInputDeviceManager by removing the device thread. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 StreamDeviceInfoArray devices_; 47 StreamDeviceInfoArray devices_;
48 48
49 private: 49 private:
50 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerListener); 50 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerListener);
51 }; 51 };
52 52
53 class MockAudioInputDeviceManagerEventHandler 53 class MockAudioInputDeviceManagerEventHandler
54 : public AudioInputDeviceManagerEventHandler { 54 : public AudioInputDeviceManagerEventHandler {
55 public: 55 public:
56 MockAudioInputDeviceManagerEventHandler() {} 56 MockAudioInputDeviceManagerEventHandler(MessageLoop* message_loop)
57 : message_loop_(message_loop) {}
57 virtual ~MockAudioInputDeviceManagerEventHandler() {} 58 virtual ~MockAudioInputDeviceManagerEventHandler() {}
58 59
59 MOCK_METHOD2(OnDeviceStarted, void(int, const std::string&)); 60 MOCK_METHOD2(DeviceStarted, void(int, const std::string&));
60 MOCK_METHOD1(OnDeviceStopped, void(int)); 61 MOCK_METHOD1(DeviceStopped, void(int));
62
63 virtual void OnDeviceStarted(int session_id,
64 const std::string& device_id) {
65 message_loop_->PostTask(
66 FROM_HERE, base::Bind(
67 &MockAudioInputDeviceManagerEventHandler::DeviceStarted,
68 base::Unretained(this), session_id, device_id));
69 }
70
71 virtual void OnDeviceStopped(int session_id) {
72 message_loop_->PostTask(
73 FROM_HERE, base::Bind(
74 &MockAudioInputDeviceManagerEventHandler::DeviceStopped,
75 base::Unretained(this), session_id));
76 }
61 77
62 private: 78 private:
79 MessageLoop* message_loop_;
63 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler); 80 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler);
64 }; 81 };
65 82
66 // Returns true if machine has audio input device, else returns false. 83 // Returns true if machine has audio input device, else returns false.
67 static bool CanRunAudioInputDeviceTests() { 84 static bool CanRunAudioInputDeviceTests() {
68 AudioManager* audio_manager = AudioManager::GetAudioManager(); 85 AudioManager* audio_manager = AudioManager::GetAudioManager();
69 if (!audio_manager) 86 if (!audio_manager)
70 return false; 87 return false;
71 88
72 return audio_manager->HasAudioInputDevices(); 89 return audio_manager->HasAudioInputDevices();
(...skipping 18 matching lines...) Expand all
91 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 108 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
92 message_loop_.get())); 109 message_loop_.get()));
93 manager_.reset(new media_stream::AudioInputDeviceManager()); 110 manager_.reset(new media_stream::AudioInputDeviceManager());
94 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); 111 audio_input_listener_.reset(new MockAudioInputDeviceManagerListener());
95 manager_->Register(audio_input_listener_.get()); 112 manager_->Register(audio_input_listener_.get());
96 113
97 // Get the enumerated device list from the AudioInputDeviceManager. 114 // Get the enumerated device list from the AudioInputDeviceManager.
98 manager_->EnumerateDevices(); 115 manager_->EnumerateDevices();
99 EXPECT_CALL(*audio_input_listener_, DevicesEnumerated(_)) 116 EXPECT_CALL(*audio_input_listener_, DevicesEnumerated(_))
100 .Times(1); 117 .Times(1);
101 // Sync up the threads to make sure we get the list. 118 // Wait for the callback.
henrika (OOO until Aug 14) 2011/11/24 15:22:26 As I've understand it all comments but the first i
no longer working on chromium 2011/11/24 16:00:42 Done.
102 SyncWithAudioInputDeviceManagerThread(); 119 message_loop_->RunAllPending();
103 } 120 }
104 121
105 virtual void TearDown() { 122 virtual void TearDown() {
106 manager_->Unregister(); 123 manager_->Unregister();
107 io_thread_.reset(); 124 io_thread_.reset();
108 } 125 }
109 126
110 // Called on the AudioInputDeviceManager thread.
111 static void PostQuitMessageLoop(MessageLoop* message_loop) {
112 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
113 }
114
115 // Called on the main thread.
116 static void PostQuitOnAudioInputDeviceManagerThread(
117 MessageLoop* message_loop, AudioInputDeviceManager* manager) {
118 manager->message_loop()->PostTask(
119 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop));
120 }
121
122 // SyncWithAudioInputDeviceManagerThread() waits until all pending tasks on
123 // the audio_input_device_manager thread are executed while also processing
124 // pending task in message_loop_ on the current thread.
125 void SyncWithAudioInputDeviceManagerThread() {
126 message_loop_->PostTask(
127 FROM_HERE,
128 base::Bind(&PostQuitOnAudioInputDeviceManagerThread,
129 message_loop_.get(),
130 manager_.get()));
131 message_loop_->Run();
132 }
133 scoped_ptr<MessageLoop> message_loop_; 127 scoped_ptr<MessageLoop> message_loop_;
134 scoped_ptr<BrowserThreadImpl> io_thread_; 128 scoped_ptr<BrowserThreadImpl> io_thread_;
135 scoped_ptr<AudioInputDeviceManager> manager_; 129 scoped_ptr<AudioInputDeviceManager> manager_;
136 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; 130 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_;
137 131
138 private: 132 private:
139 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest); 133 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest);
140 }; 134 };
141 135
142 // Test the devices can be opened and closed. 136 // Test the devices can be opened and closed.
henrika (OOO until Aug 14) 2011/11/24 15:22:26 Tests that...
no longer working on chromium 2011/11/24 16:00:42 Done.
143 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) { 137 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) {
144 if (!CanRunAudioInputDeviceTests()) 138 if (!CanRunAudioInputDeviceTests())
145 return; 139 return;
146 InSequence s; 140 InSequence s;
147 141
148 for (StreamDeviceInfoArray::const_iterator iter = 142 for (StreamDeviceInfoArray::const_iterator iter =
149 audio_input_listener_->devices_.begin(); 143 audio_input_listener_->devices_.begin();
150 iter != audio_input_listener_->devices_.end(); ++iter) { 144 iter != audio_input_listener_->devices_.end(); ++iter) {
151 // Open/close the devices. 145 // Open/close the devices.
152 int session_id = manager_->Open(*iter); 146 int session_id = manager_->Open(*iter);
153 manager_->Close(session_id); 147 manager_->Close(session_id);
154 148
155 // Expected mock call with expected return value. 149 // Expected mock call with expected return value.
156 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) 150 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id))
157 .Times(1); 151 .Times(1);
158 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) 152 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id))
159 .Times(1); 153 .Times(1);
160 SyncWithAudioInputDeviceManagerThread(); 154 // Wait for the callback.
155 message_loop_->RunAllPending();
161 } 156 }
162 } 157 }
163 158
164 // Open multiple devices at one time and close them later. 159 // Open multiple devices at one time and close them later.
165 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) { 160 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) {
166 if (!CanRunAudioInputDeviceTests()) 161 if (!CanRunAudioInputDeviceTests())
167 return; 162 return;
168 InSequence s; 163 InSequence s;
169 164
170 int index = 0; 165 int index = 0;
171 const int kDeviceSize = audio_input_listener_->devices_.size(); 166 const int kDeviceSize = audio_input_listener_->devices_.size();
172 scoped_array<int> session_id(new int[kDeviceSize]); 167 scoped_array<int> session_id(new int[kDeviceSize]);
173 168
174 // Open the devices in a loop. 169 // Open the devices in a loop.
175 for (StreamDeviceInfoArray::const_iterator iter = 170 for (StreamDeviceInfoArray::const_iterator iter =
176 audio_input_listener_->devices_.begin(); 171 audio_input_listener_->devices_.begin();
177 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { 172 iter != audio_input_listener_->devices_.end(); ++iter, ++index) {
178 // Open the devices. 173 // Open the devices.
179 session_id[index] = manager_->Open(*iter); 174 session_id[index] = manager_->Open(*iter);
180 175
181 // Expected mock call with expected return value. 176 // Expected mock call with expected return value.
182 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, 177 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture,
183 session_id[index])) 178 session_id[index]))
184 .Times(1); 179 .Times(1);
185 SyncWithAudioInputDeviceManagerThread(); 180 // Wait for the callback.
181 message_loop_->RunAllPending();
186 } 182 }
187 183
188 // Check if the session_ids are unique 184 // Check if the session_ids are unique
189 for (int i = 0; i < kDeviceSize - 1; ++i) { 185 for (int i = 0; i < kDeviceSize - 1; ++i) {
190 for (int k = i+1; k < kDeviceSize; ++k) { 186 for (int k = i+1; k < kDeviceSize; ++k) {
191 EXPECT_TRUE(session_id[i] != session_id[k]); 187 EXPECT_TRUE(session_id[i] != session_id[k]);
192 } 188 }
193 } 189 }
194 190
195 for (int i = 0; i < kDeviceSize; ++i) { 191 for (int i = 0; i < kDeviceSize; ++i) {
196 // Close the devices. 192 // Close the devices.
197 manager_->Close(session_id[i]); 193 manager_->Close(session_id[i]);
198 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id[i])) 194 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id[i]))
199 .Times(1); 195 .Times(1);
200 SyncWithAudioInputDeviceManagerThread(); 196 // Wait for the callback.
197 message_loop_->RunAllPending();
201 } 198 }
202 } 199 }
203 200
204 // Try to open a non-existing device. 201 // Try to open a non-existing device.
205 TEST_F(AudioInputDeviceManagerTest, OpenNotExistingDevice) { 202 TEST_F(AudioInputDeviceManagerTest, OpenNotExistingDevice) {
206 if (!CanRunAudioInputDeviceTests()) 203 if (!CanRunAudioInputDeviceTests())
207 return; 204 return;
208 InSequence s; 205 InSequence s;
209 206
210 MediaStreamType stream_type = kAudioCapture; 207 MediaStreamType stream_type = kAudioCapture;
211 std::string device_name("device_doesnt_exist"); 208 std::string device_name("device_doesnt_exist");
212 std::string device_id("id_doesnt_exist"); 209 std::string device_id("id_doesnt_exist");
213 StreamDeviceInfo dummy_device(stream_type, device_name, device_id, false); 210 StreamDeviceInfo dummy_device(stream_type, device_name, device_id, false);
214 211
215 // This should fail and trigger error code 'kDeviceNotAvailable'. 212 // This should still pass.
henrika (OOO until Aug 14) 2011/11/24 15:22:26 Comment does not add much value to the test descri
no longer working on chromium 2011/11/24 16:00:42 Done.
216 int session_id = manager_->Open(dummy_device); 213 int session_id = manager_->Open(dummy_device);
217 214 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id))
218 EXPECT_CALL(*audio_input_listener_, Error(_, session_id, kDeviceNotAvailable))
219 .Times(1); 215 .Times(1);
220 SyncWithAudioInputDeviceManagerThread(); 216 // Wait for the callback.
221 } 217 message_loop_->RunAllPending();
222
223 // Try open an invalid device.
224 TEST_F(AudioInputDeviceManagerTest, OpenInvalidDevice) {
225 if (!CanRunAudioInputDeviceTests())
226 return;
227 InSequence s;
228
229 MediaStreamType stream_type = kAudioCapture;
230 std::string device_name;
231 std::string device_id;
232 device_name = audio_input_listener_->devices_.front().name;
233 device_id = "wrong_id";
234 StreamDeviceInfo invalid_device(stream_type, device_name, device_id, false);
235
236 // This should fail and trigger error code 'kDeviceNotAvailable'.
237 int session_id = manager_->Open(invalid_device);
238
239 EXPECT_CALL(*audio_input_listener_, Error(_, session_id, kDeviceNotAvailable))
240 .Times(1);
241 SyncWithAudioInputDeviceManagerThread();
242 } 218 }
243 219
244 // Opening default device twice should work. 220 // Opening default device twice should work.
245 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { 221 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) {
246 if (!CanRunAudioInputDeviceTests()) 222 if (!CanRunAudioInputDeviceTests())
247 return; 223 return;
248 InSequence s; 224 InSequence s;
249 225
250 // Open/close the default device twice. 226 // Open/close the default device twice.
251 int first_session_id = manager_->Open( 227 int first_session_id = manager_->Open(
252 audio_input_listener_->devices_.front()); 228 audio_input_listener_->devices_.front());
253 int second_session_id = manager_->Open( 229 int second_session_id = manager_->Open(
254 audio_input_listener_->devices_.front()); 230 audio_input_listener_->devices_.front());
255 manager_->Close(first_session_id); 231 manager_->Close(first_session_id);
256 manager_->Close(second_session_id); 232 manager_->Close(second_session_id);
257 233
258 // Expected mock calls with expected return values. 234 // Expected mock calls with expected return values.
259 EXPECT_NE(first_session_id, second_session_id); 235 EXPECT_NE(first_session_id, second_session_id);
260 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) 236 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id))
261 .Times(1); 237 .Times(1);
262 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) 238 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id))
263 .Times(1); 239 .Times(1);
264 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) 240 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id))
265 .Times(1); 241 .Times(1);
266 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) 242 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id))
267 .Times(1); 243 .Times(1);
268 SyncWithAudioInputDeviceManagerThread(); 244 // Wait for the callback.
245 message_loop_->RunAllPending();
269 } 246 }
270 247
271 // Test the Start and Close function after opening the devices. 248 // Test the Start and Close function after opening the devices.
272 TEST_F(AudioInputDeviceManagerTest, StartAndStopDevice) { 249 TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) {
273 if (!CanRunAudioInputDeviceTests()) 250 if (!CanRunAudioInputDeviceTests())
274 return; 251 return;
275 InSequence s; 252 InSequence s;
276 253
277 int index = 0; 254 int index = 0;
278 const int kDeviceSize = audio_input_listener_->devices_.size(); 255 const int kDeviceSize = audio_input_listener_->devices_.size();
279 scoped_array<int> session_id(new int[kDeviceSize]); 256 scoped_array<int> session_id(new int[kDeviceSize]);
280 257
281 // Create the EventHandler for the sessions. 258 // Create the EventHandler for the sessions.
282 scoped_ptr<MockAudioInputDeviceManagerEventHandler> 259 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
283 audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); 260 audio_input_event_handler(
261 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
284 262
285 // Loop through the devices, and Open/start/stop/close each device. 263 // Loop through the devices, and Open/start/stop/close each device.
286 for (StreamDeviceInfoArray::const_iterator iter = 264 for (StreamDeviceInfoArray::const_iterator iter =
287 audio_input_listener_->devices_.begin(); 265 audio_input_listener_->devices_.begin();
288 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { 266 iter != audio_input_listener_->devices_.end(); ++iter, ++index) {
289 // Note that no stop device notification for Event Handler as we have 267 // Note that no stop device notification for Event Handler as we have
290 // stopped the device before calling close. 268 // stopped the device before calling close.
291 // Open/start/stop/close the device. 269 // Open/start/stop/close the device.
292 session_id[index] = manager_->Open(*iter); 270 session_id[index] = manager_->Open(*iter);
293 manager_->Start(session_id[index], audio_input_event_handler.get());
294 manager_->Stop(session_id[index]);
295 manager_->Close(session_id[index]);
296
297 // Expected mock calls with expected return values.
298 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, 271 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture,
299 session_id[index])) 272 session_id[index]))
300 .Times(1); 273 .Times(1);
274 message_loop_->RunAllPending();
275
276 manager_->Start(session_id[index], audio_input_event_handler.get());
301 EXPECT_CALL(*audio_input_event_handler, 277 EXPECT_CALL(*audio_input_event_handler,
302 OnDeviceStarted(session_id[index], iter->device_id)) 278 DeviceStarted(session_id[index], iter->device_id))
303 .Times(1); 279 .Times(1);
280 message_loop_->RunAllPending();
281
282 manager_->Stop(session_id[index]);
283 manager_->Close(session_id[index]);
304 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, 284 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture,
305 session_id[index])) 285 session_id[index]))
306 .Times(1); 286 .Times(1);
307 SyncWithAudioInputDeviceManagerThread(); 287 message_loop_->RunAllPending();
308 } 288 }
309 } 289 }
310 290
311 // Test the behavior of calling Close without calling Stop. 291 // Test the behavior of calling Close without calling Stop.
312 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopDevice) { 292 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) {
313 if (!CanRunAudioInputDeviceTests()) 293 if (!CanRunAudioInputDeviceTests())
314 return; 294 return;
315 InSequence s; 295 InSequence s;
316 296
317 int index = 0; 297 int index = 0;
318 const int kDeviceSize = audio_input_listener_->devices_.size(); 298 const int kDeviceSize = audio_input_listener_->devices_.size();
319 scoped_array<int> session_id(new int[kDeviceSize]); 299 scoped_array<int> session_id(new int[kDeviceSize]);
320 300
321 // Create the EventHandlers for the sessions. 301 // Create the EventHandlers for the sessions.
322 scoped_ptr<MockAudioInputDeviceManagerEventHandler> 302 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
323 audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); 303 audio_input_event_handler(
304 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
324 305
325 // Loop through the devices, and open/start/close the devices. 306 // Loop through the devices, and open/start/close the devices.
326 // Note that we do not call stop. 307 // Note that we do not call stop.
327 for (StreamDeviceInfoArray::const_iterator iter = 308 for (StreamDeviceInfoArray::const_iterator iter =
328 audio_input_listener_->devices_.begin(); 309 audio_input_listener_->devices_.begin();
329 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { 310 iter != audio_input_listener_->devices_.end(); ++iter, ++index) {
330 // Open/start/close the device. 311 // Open/start/close the device.
331 session_id[index] = manager_->Open(*iter); 312 session_id[index] = manager_->Open(*iter);
332 manager_->Start(session_id[index], audio_input_event_handler.get());
333 manager_->Close(session_id[index]);
334
335 // Expected mock calls with expected return values.
336 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, 313 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture,
337 session_id[index])) 314 session_id[index]))
338 .Times(1); 315 .Times(1);
316 message_loop_->RunAllPending();
317
318 manager_->Start(session_id[index], audio_input_event_handler.get());
339 EXPECT_CALL(*audio_input_event_handler, 319 EXPECT_CALL(*audio_input_event_handler,
340 OnDeviceStarted(session_id[index], iter->device_id)) 320 DeviceStarted(session_id[index], iter->device_id))
341 .Times(1); 321 .Times(1);
322 message_loop_->RunAllPending();
323
342 // Event Handler should get a stop device notification as no stop is called 324 // Event Handler should get a stop device notification as no stop is called
343 // before closing the device. 325 // before closing the device.
326 manager_->Close(session_id[index]);
344 EXPECT_CALL(*audio_input_event_handler, 327 EXPECT_CALL(*audio_input_event_handler,
345 OnDeviceStopped(session_id[index])) 328 DeviceStopped(session_id[index]))
346 .Times(1); 329 .Times(1);
347 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, 330 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture,
348 session_id[index])) 331 session_id[index]))
349 .Times(1); 332 .Times(1);
350 SyncWithAudioInputDeviceManagerThread(); 333 message_loop_->RunAllPending();
351 } 334 }
352 } 335 }
353 336
354 // Should be able to start the default device twice. 337 // Should be able to start the same device twice.
355 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { 338 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) {
356 if (!CanRunAudioInputDeviceTests()) 339 if (!CanRunAudioInputDeviceTests())
357 return; 340 return;
358 InSequence s; 341 InSequence s;
359 342
360 // Create one EventHandler for each session. 343 // Create one EventHandler for each session.
361 scoped_ptr<MockAudioInputDeviceManagerEventHandler> 344 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
362 first_audio_input_event_handler( 345 first_event_handler(
363 new MockAudioInputDeviceManagerEventHandler()); 346 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
364 scoped_ptr<MockAudioInputDeviceManagerEventHandler> 347 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
365 second_audio_input_event_handler( 348 second_event_handler(
366 new MockAudioInputDeviceManagerEventHandler()); 349 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
367 350
368 // Open the default device twice. 351 // Open the default device twice.
369 StreamDeviceInfoArray::const_iterator iter = 352 StreamDeviceInfoArray::const_iterator iter =
370 audio_input_listener_->devices_.begin(); 353 audio_input_listener_->devices_.begin();
371 int first_session_id = manager_->Open(*iter); 354 int first_session_id = manager_->Open(*iter);
372 int second_session_id = manager_->Open(*iter); 355 int second_session_id = manager_->Open(*iter);
373
374 // Start/stop/close the default device twice.
375 manager_->Start(first_session_id, first_audio_input_event_handler.get());
376 manager_->Start(second_session_id, second_audio_input_event_handler.get());
377 manager_->Stop(first_session_id);
378 manager_->Stop(second_session_id);
379 manager_->Close(first_session_id);
380 manager_->Close(second_session_id);
381
382 // Expected mock calls with expected return values.
383 EXPECT_NE(first_session_id, second_session_id); 356 EXPECT_NE(first_session_id, second_session_id);
384 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) 357 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id))
385 .Times(1); 358 .Times(1);
386 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) 359 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id))
387 .Times(1); 360 .Times(1);
388 EXPECT_CALL(*first_audio_input_event_handler, 361 message_loop_->RunAllPending();
389 OnDeviceStarted(first_session_id, 362
390 AudioManagerBase::kDefaultDeviceId)) 363 // Start/stop/close the default device twice.
364 manager_->Start(first_session_id, first_event_handler.get());
365 manager_->Start(second_session_id, second_event_handler.get());
366 EXPECT_CALL(*first_event_handler,
367 DeviceStarted(first_session_id,
368 AudioManagerBase::kDefaultDeviceId))
391 .Times(1); 369 .Times(1);
392 EXPECT_CALL(*second_audio_input_event_handler, 370 EXPECT_CALL(*second_event_handler,
393 OnDeviceStarted(second_session_id, 371 DeviceStarted(second_session_id,
394 AudioManagerBase::kDefaultDeviceId)) 372 AudioManagerBase::kDefaultDeviceId))
395 .Times(1); 373 .Times(1);
374 message_loop_->RunAllPending();
375
376 manager_->Stop(first_session_id);
377 manager_->Stop(second_session_id);
378 manager_->Close(first_session_id);
379 manager_->Close(second_session_id);
396 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) 380 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id))
397 .Times(1); 381 .Times(1);
398 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) 382 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id))
399 .Times(1); 383 .Times(1);
400 SyncWithAudioInputDeviceManagerThread(); 384 message_loop_->RunAllPending();
385 }
386
387 // Try start a invalid session device.
henrika (OOO until Aug 14) 2011/11/24 15:22:26 an
no longer working on chromium 2011/11/24 16:00:42 Done.
388 TEST_F(AudioInputDeviceManagerTest, StartInvalidSession) {
389 if (!CanRunAudioInputDeviceTests())
390 return;
391 InSequence s;
392
393 // Create the EventHandlers for the sessions.
394 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
395 audio_input_event_handler(
396 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
397
398 // Open the first device.
399 StreamDeviceInfoArray::const_iterator iter =
400 audio_input_listener_->devices_.begin();
401 int session_id = manager_->Open(*iter);
402 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id))
403 .Times(1);
404 message_loop_->RunAllPending();
405
406 // Start a non-opened device.
407 // This should fail and trigger error code 'kDeviceNotAvailable'.
408 int invalid_session_id = session_id + 1;
409 manager_->Start(invalid_session_id, audio_input_event_handler.get());
410 EXPECT_CALL(*audio_input_event_handler,
411 DeviceStarted(invalid_session_id,
412 AudioInputDeviceManager::kInvalidDeviceId))
413 .Times(1);
414 message_loop_->RunAllPending();
415
416 manager_->Close(session_id);
417 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id))
418 .Times(1);
419 message_loop_->RunAllPending();
420 }
421
422 // Try start a session twice.
henrika (OOO until Aug 14) 2011/11/24 15:22:26 Try to start.. And please add some more about the
no longer working on chromium 2011/11/24 16:00:42 Done.
423 TEST_F(AudioInputDeviceManagerTest, StartSessionTwice) {
424 if (!CanRunAudioInputDeviceTests())
425 return;
426 InSequence s;
427
428 // Create the EventHandlers for the sessions.
429 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
430 audio_input_event_handler(
431 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
432
433 // Open the first device.
434 StreamDeviceInfoArray::const_iterator iter =
435 audio_input_listener_->devices_.begin();
436 int session_id = manager_->Open(*iter);
437 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id))
438 .Times(1);
439 message_loop_->RunAllPending();
440
441 // Start the session, it should succeed.
442 manager_->Start(session_id, audio_input_event_handler.get());
443 EXPECT_CALL(*audio_input_event_handler,
444 DeviceStarted(session_id,
445 AudioManagerBase::kDefaultDeviceId))
446 .Times(1);
447 message_loop_->RunAllPending();
448
449 // Start the session for the second time, it should fail.
450 manager_->Start(session_id, audio_input_event_handler.get());
451 EXPECT_CALL(*audio_input_event_handler,
452 DeviceStarted(session_id,
453 AudioInputDeviceManager::kInvalidDeviceId))
454 .Times(1);
455
456 manager_->Stop(session_id);
457 manager_->Close(session_id);
458 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id))
459 .Times(1);
460 message_loop_->RunAllPending();
401 } 461 }
402 462
403 } // namespace media_stream 463 } // namespace media_stream
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698