Chromium Code Reviews| 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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 explicit 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 14 matching lines...) Expand all Loading... | |
| 87 protected: | 104 protected: |
| 88 virtual void SetUp() { | 105 virtual void SetUp() { |
| 89 // The test must run on Browser::IO. | 106 // The test must run on Browser::IO. |
| 90 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 107 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 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 // Gets 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 |
| 102 SyncWithAudioInputDeviceManagerThread(); | 119 // Waits for the callback. |
| 120 message_loop_->RunAllPending(); | |
| 103 } | 121 } |
| 104 | 122 |
| 105 virtual void TearDown() { | 123 virtual void TearDown() { |
| 106 manager_->Unregister(); | 124 manager_->Unregister(); |
| 107 io_thread_.reset(); | 125 io_thread_.reset(); |
| 108 } | 126 } |
| 109 | 127 |
| 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_; | 128 scoped_ptr<MessageLoop> message_loop_; |
| 134 scoped_ptr<BrowserThreadImpl> io_thread_; | 129 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 135 scoped_ptr<AudioInputDeviceManager> manager_; | 130 scoped_ptr<AudioInputDeviceManager> manager_; |
| 136 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; | 131 scoped_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
| 137 | 132 |
| 138 private: | 133 private: |
| 139 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest); | 134 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManagerTest); |
| 140 }; | 135 }; |
| 141 | 136 |
| 142 // Test the devices can be opened and closed. | 137 // Tests that the devices can be opened and closed. |
| 143 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) { | 138 TEST_F(AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
| 144 if (!CanRunAudioInputDeviceTests()) | 139 if (!CanRunAudioInputDeviceTests()) |
| 145 return; | 140 return; |
| 146 InSequence s; | 141 InSequence s; |
| 147 | 142 |
| 148 for (StreamDeviceInfoArray::const_iterator iter = | 143 for (StreamDeviceInfoArray::const_iterator iter = |
| 149 audio_input_listener_->devices_.begin(); | 144 audio_input_listener_->devices_.begin(); |
| 150 iter != audio_input_listener_->devices_.end(); ++iter) { | 145 iter != audio_input_listener_->devices_.end(); ++iter) { |
| 151 // Open/close the devices. | 146 // Opens/closes the devices. |
| 152 int session_id = manager_->Open(*iter); | 147 int session_id = manager_->Open(*iter); |
| 153 manager_->Close(session_id); | 148 manager_->Close(session_id); |
| 154 | 149 |
| 155 // Expected mock call with expected return value. | 150 // Expected mock call with expected return value. |
| 156 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) | 151 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) |
| 157 .Times(1); | 152 .Times(1); |
| 158 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) | 153 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
| 159 .Times(1); | 154 .Times(1); |
| 160 SyncWithAudioInputDeviceManagerThread(); | 155 |
| 156 // Waits for the callback. | |
| 157 message_loop_->RunAllPending(); | |
| 161 } | 158 } |
| 162 } | 159 } |
| 163 | 160 |
| 164 // Open multiple devices at one time and close them later. | 161 // Opens multiple devices at one time and closes them later. |
| 165 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) { | 162 TEST_F(AudioInputDeviceManagerTest, OpenMultipleDevices) { |
| 166 if (!CanRunAudioInputDeviceTests()) | 163 if (!CanRunAudioInputDeviceTests()) |
| 167 return; | 164 return; |
| 168 InSequence s; | 165 InSequence s; |
| 169 | 166 |
| 170 int index = 0; | 167 int index = 0; |
| 171 const int kDeviceSize = audio_input_listener_->devices_.size(); | 168 const int kDeviceSize = audio_input_listener_->devices_.size(); |
| 172 scoped_array<int> session_id(new int[kDeviceSize]); | 169 scoped_array<int> session_id(new int[kDeviceSize]); |
| 173 | 170 |
| 174 // Open the devices in a loop. | 171 // Opens the devices in a loop. |
| 175 for (StreamDeviceInfoArray::const_iterator iter = | 172 for (StreamDeviceInfoArray::const_iterator iter = |
| 176 audio_input_listener_->devices_.begin(); | 173 audio_input_listener_->devices_.begin(); |
| 177 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { | 174 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { |
| 178 // Open the devices. | 175 // Opens the devices. |
| 179 session_id[index] = manager_->Open(*iter); | 176 session_id[index] = manager_->Open(*iter); |
| 180 | 177 |
| 181 // Expected mock call with expected return value. | 178 // Expected mock call with expected return value. |
| 182 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, | 179 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| 183 session_id[index])) | 180 session_id[index])) |
| 184 .Times(1); | 181 .Times(1); |
| 185 SyncWithAudioInputDeviceManagerThread(); | 182 |
| 183 // Waits for the callback. | |
| 184 message_loop_->RunAllPending(); | |
| 186 } | 185 } |
| 187 | 186 |
| 188 // Check if the session_ids are unique | 187 // Checks if the session_ids are unique |
| 189 for (int i = 0; i < kDeviceSize - 1; ++i) { | 188 for (int i = 0; i < kDeviceSize - 1; ++i) { |
| 190 for (int k = i+1; k < kDeviceSize; ++k) { | 189 for (int k = i+1; k < kDeviceSize; ++k) { |
| 191 EXPECT_TRUE(session_id[i] != session_id[k]); | 190 EXPECT_TRUE(session_id[i] != session_id[k]); |
| 192 } | 191 } |
| 193 } | 192 } |
| 194 | 193 |
| 195 for (int i = 0; i < kDeviceSize; ++i) { | 194 for (int i = 0; i < kDeviceSize; ++i) { |
| 196 // Close the devices. | 195 // Close the devices. |
| 197 manager_->Close(session_id[i]); | 196 manager_->Close(session_id[i]); |
| 198 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id[i])) | 197 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id[i])) |
| 199 .Times(1); | 198 .Times(1); |
| 200 SyncWithAudioInputDeviceManagerThread(); | 199 |
| 200 // Waits for the callback. | |
| 201 message_loop_->RunAllPending(); | |
| 201 } | 202 } |
| 202 } | 203 } |
| 203 | 204 |
| 204 // Try to open a non-existing device. | 205 // Tries to open a non-existing device. |
| 205 TEST_F(AudioInputDeviceManagerTest, OpenNotExistingDevice) { | 206 TEST_F(AudioInputDeviceManagerTest, OpenNotExistingDevice) { |
| 206 if (!CanRunAudioInputDeviceTests()) | 207 if (!CanRunAudioInputDeviceTests()) |
| 207 return; | 208 return; |
| 208 InSequence s; | 209 InSequence s; |
| 209 | 210 |
| 210 MediaStreamType stream_type = kAudioCapture; | 211 MediaStreamType stream_type = kAudioCapture; |
| 211 std::string device_name("device_doesnt_exist"); | 212 std::string device_name("device_doesnt_exist"); |
| 212 std::string device_id("id_doesnt_exist"); | 213 std::string device_id("id_doesnt_exist"); |
| 213 StreamDeviceInfo dummy_device(stream_type, device_name, device_id, false); | 214 StreamDeviceInfo dummy_device(stream_type, device_name, device_id, false); |
| 214 | 215 |
| 215 // This should fail and trigger error code 'kDeviceNotAvailable'. | |
| 216 int session_id = manager_->Open(dummy_device); | 216 int session_id = manager_->Open(dummy_device); |
| 217 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) | |
| 218 .Times(1); | |
| 217 | 219 |
| 218 EXPECT_CALL(*audio_input_listener_, Error(_, session_id, kDeviceNotAvailable)) | 220 // Wait for the callback. |
| 219 .Times(1); | 221 message_loop_->RunAllPending(); |
| 220 SyncWithAudioInputDeviceManagerThread(); | |
| 221 } | |
| 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 } | 222 } |
| 243 | 223 |
| 244 // Opening default device twice should work. | 224 // Opening default device twice should work. |
| 245 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { | 225 TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { |
| 246 if (!CanRunAudioInputDeviceTests()) | 226 if (!CanRunAudioInputDeviceTests()) |
| 247 return; | 227 return; |
| 248 InSequence s; | 228 InSequence s; |
| 249 | 229 |
| 250 // Open/close the default device twice. | 230 // Opens/closes the default device twice. |
| 251 int first_session_id = manager_->Open( | 231 int first_session_id = manager_->Open( |
| 252 audio_input_listener_->devices_.front()); | 232 audio_input_listener_->devices_.front()); |
| 253 int second_session_id = manager_->Open( | 233 int second_session_id = manager_->Open( |
| 254 audio_input_listener_->devices_.front()); | 234 audio_input_listener_->devices_.front()); |
| 255 manager_->Close(first_session_id); | 235 manager_->Close(first_session_id); |
| 256 manager_->Close(second_session_id); | 236 manager_->Close(second_session_id); |
| 257 | 237 |
| 258 // Expected mock calls with expected return values. | 238 // Expected mock calls with expected return values. |
| 259 EXPECT_NE(first_session_id, second_session_id); | 239 EXPECT_NE(first_session_id, second_session_id); |
| 260 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) | 240 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) |
| 261 .Times(1); | 241 .Times(1); |
| 262 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) | 242 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) |
| 263 .Times(1); | 243 .Times(1); |
| 264 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) | 244 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) |
| 265 .Times(1); | 245 .Times(1); |
| 266 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) | 246 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
| 267 .Times(1); | 247 .Times(1); |
| 268 SyncWithAudioInputDeviceManagerThread(); | 248 |
| 249 // Waits for the callback. | |
| 250 message_loop_->RunAllPending(); | |
| 269 } | 251 } |
| 270 | 252 |
| 271 // Test the Start and Close function after opening the devices. | 253 // Tests the Start and Close function after opening the devices. |
|
henrika (OOO until Aug 14)
2011/11/25 09:48:25
functions
no longer working on chromium
2011/11/25 14:14:14
Done.
| |
| 272 TEST_F(AudioInputDeviceManagerTest, StartAndStopDevice) { | 254 TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) { |
| 273 if (!CanRunAudioInputDeviceTests()) | 255 if (!CanRunAudioInputDeviceTests()) |
| 274 return; | 256 return; |
| 275 InSequence s; | 257 InSequence s; |
| 276 | 258 |
| 277 int index = 0; | 259 int index = 0; |
| 278 const int kDeviceSize = audio_input_listener_->devices_.size(); | 260 const int kDeviceSize = audio_input_listener_->devices_.size(); |
| 279 scoped_array<int> session_id(new int[kDeviceSize]); | 261 scoped_array<int> session_id(new int[kDeviceSize]); |
| 280 | 262 |
| 281 // Create the EventHandler for the sessions. | 263 // Creates the EventHandler for the sessions. |
| 282 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 264 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| 283 audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); | 265 audio_input_event_handler( |
| 266 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | |
| 284 | 267 |
| 285 // Loop through the devices, and Open/start/stop/close each device. | 268 // Loops through the devices, and Open/start/stop/close each device. |
|
henrika (OOO until Aug 14)
2011/11/25 09:48:25
Fix language and use Open()/Start() etc.
no longer working on chromium
2011/11/25 14:14:14
Done.
| |
| 286 for (StreamDeviceInfoArray::const_iterator iter = | 269 for (StreamDeviceInfoArray::const_iterator iter = |
| 287 audio_input_listener_->devices_.begin(); | 270 audio_input_listener_->devices_.begin(); |
| 288 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { | 271 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { |
| 289 // Note that no stop device notification for Event Handler as we have | 272 // Note that no stop device notification for Event Handler as we have |
| 290 // stopped the device before calling close. | 273 // stopped the device before calling close. |
| 291 // Open/start/stop/close the device. | 274 // Open/start/stop/close the device. |
| 292 session_id[index] = manager_->Open(*iter); | 275 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, | 276 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| 299 session_id[index])) | 277 session_id[index])) |
| 300 .Times(1); | 278 .Times(1); |
| 279 message_loop_->RunAllPending(); | |
| 280 | |
| 281 manager_->Start(session_id[index], audio_input_event_handler.get()); | |
| 301 EXPECT_CALL(*audio_input_event_handler, | 282 EXPECT_CALL(*audio_input_event_handler, |
| 302 OnDeviceStarted(session_id[index], iter->device_id)) | 283 DeviceStarted(session_id[index], iter->device_id)) |
| 303 .Times(1); | 284 .Times(1); |
| 285 message_loop_->RunAllPending(); | |
| 286 | |
| 287 manager_->Stop(session_id[index]); | |
| 288 manager_->Close(session_id[index]); | |
| 304 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, | 289 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, |
| 305 session_id[index])) | 290 session_id[index])) |
| 306 .Times(1); | 291 .Times(1); |
| 307 SyncWithAudioInputDeviceManagerThread(); | 292 message_loop_->RunAllPending(); |
| 308 } | 293 } |
| 309 } | 294 } |
| 310 | 295 |
| 311 // Test the behavior of calling Close without calling Stop. | 296 // Tests the behavior of calling Close without calling Stop. |
| 312 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopDevice) { | 297 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) { |
| 313 if (!CanRunAudioInputDeviceTests()) | 298 if (!CanRunAudioInputDeviceTests()) |
| 314 return; | 299 return; |
| 315 InSequence s; | 300 InSequence s; |
| 316 | 301 |
| 317 int index = 0; | 302 int index = 0; |
| 318 const int kDeviceSize = audio_input_listener_->devices_.size(); | 303 const int kDeviceSize = audio_input_listener_->devices_.size(); |
| 319 scoped_array<int> session_id(new int[kDeviceSize]); | 304 scoped_array<int> session_id(new int[kDeviceSize]); |
| 320 | 305 |
| 321 // Create the EventHandlers for the sessions. | 306 // Create the EventHandlers for the sessions. |
| 322 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 307 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| 323 audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); | 308 audio_input_event_handler( |
| 309 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | |
| 324 | 310 |
| 325 // Loop through the devices, and open/start/close the devices. | 311 // Loop through the devices, and open/start/close the devices. |
| 326 // Note that we do not call stop. | 312 // Note that we do not call stop. |
| 327 for (StreamDeviceInfoArray::const_iterator iter = | 313 for (StreamDeviceInfoArray::const_iterator iter = |
| 328 audio_input_listener_->devices_.begin(); | 314 audio_input_listener_->devices_.begin(); |
| 329 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { | 315 iter != audio_input_listener_->devices_.end(); ++iter, ++index) { |
| 330 // Open/start/close the device. | 316 // Open/start/close the device. |
| 331 session_id[index] = manager_->Open(*iter); | 317 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, | 318 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| 337 session_id[index])) | 319 session_id[index])) |
| 338 .Times(1); | 320 .Times(1); |
| 321 message_loop_->RunAllPending(); | |
| 322 | |
| 323 manager_->Start(session_id[index], audio_input_event_handler.get()); | |
| 339 EXPECT_CALL(*audio_input_event_handler, | 324 EXPECT_CALL(*audio_input_event_handler, |
| 340 OnDeviceStarted(session_id[index], iter->device_id)) | 325 DeviceStarted(session_id[index], iter->device_id)) |
| 341 .Times(1); | 326 .Times(1); |
| 327 message_loop_->RunAllPending(); | |
| 328 | |
| 342 // Event Handler should get a stop device notification as no stop is called | 329 // Event Handler should get a stop device notification as no stop is called |
| 343 // before closing the device. | 330 // before closing the device. |
| 331 manager_->Close(session_id[index]); | |
| 344 EXPECT_CALL(*audio_input_event_handler, | 332 EXPECT_CALL(*audio_input_event_handler, |
| 345 OnDeviceStopped(session_id[index])) | 333 DeviceStopped(session_id[index])) |
| 346 .Times(1); | 334 .Times(1); |
| 347 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, | 335 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, |
| 348 session_id[index])) | 336 session_id[index])) |
| 349 .Times(1); | 337 .Times(1); |
| 350 SyncWithAudioInputDeviceManagerThread(); | 338 message_loop_->RunAllPending(); |
| 351 } | 339 } |
| 352 } | 340 } |
| 353 | 341 |
| 354 // Should be able to start the default device twice. | 342 // Should be able to start the same device twice. |
| 355 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { | 343 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { |
| 356 if (!CanRunAudioInputDeviceTests()) | 344 if (!CanRunAudioInputDeviceTests()) |
| 357 return; | 345 return; |
| 358 InSequence s; | 346 InSequence s; |
| 359 | 347 |
| 360 // Create one EventHandler for each session. | 348 // Create one EventHandler for each session. |
| 361 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 349 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| 362 first_audio_input_event_handler( | 350 first_event_handler( |
| 363 new MockAudioInputDeviceManagerEventHandler()); | 351 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| 364 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | 352 scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| 365 second_audio_input_event_handler( | 353 second_event_handler( |
| 366 new MockAudioInputDeviceManagerEventHandler()); | 354 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| 367 | 355 |
| 368 // Open the default device twice. | 356 // Open the default device twice. |
| 369 StreamDeviceInfoArray::const_iterator iter = | 357 StreamDeviceInfoArray::const_iterator iter = |
| 370 audio_input_listener_->devices_.begin(); | 358 audio_input_listener_->devices_.begin(); |
| 371 int first_session_id = manager_->Open(*iter); | 359 int first_session_id = manager_->Open(*iter); |
| 372 int second_session_id = manager_->Open(*iter); | 360 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); | 361 EXPECT_NE(first_session_id, second_session_id); |
| 384 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) | 362 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) |
| 385 .Times(1); | 363 .Times(1); |
| 386 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) | 364 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) |
| 387 .Times(1); | 365 .Times(1); |
| 388 EXPECT_CALL(*first_audio_input_event_handler, | 366 message_loop_->RunAllPending(); |
| 389 OnDeviceStarted(first_session_id, | 367 |
| 390 AudioManagerBase::kDefaultDeviceId)) | 368 // Start/stop/close the default device twice. |
|
henrika (OOO until Aug 14)
2011/11/25 09:48:25
Start()/Stop() etc.
no longer working on chromium
2011/11/25 14:14:14
Done.
| |
| 369 manager_->Start(first_session_id, first_event_handler.get()); | |
| 370 manager_->Start(second_session_id, second_event_handler.get()); | |
| 371 EXPECT_CALL(*first_event_handler, | |
| 372 DeviceStarted(first_session_id, | |
| 373 AudioManagerBase::kDefaultDeviceId)) | |
| 391 .Times(1); | 374 .Times(1); |
| 392 EXPECT_CALL(*second_audio_input_event_handler, | 375 EXPECT_CALL(*second_event_handler, |
| 393 OnDeviceStarted(second_session_id, | 376 DeviceStarted(second_session_id, |
| 394 AudioManagerBase::kDefaultDeviceId)) | 377 AudioManagerBase::kDefaultDeviceId)) |
| 395 .Times(1); | 378 .Times(1); |
| 379 message_loop_->RunAllPending(); | |
| 380 | |
| 381 manager_->Stop(first_session_id); | |
| 382 manager_->Stop(second_session_id); | |
| 383 manager_->Close(first_session_id); | |
| 384 manager_->Close(second_session_id); | |
| 396 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) | 385 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) |
| 397 .Times(1); | 386 .Times(1); |
| 398 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) | 387 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
| 399 .Times(1); | 388 .Times(1); |
| 400 SyncWithAudioInputDeviceManagerThread(); | 389 message_loop_->RunAllPending(); |
| 390 } | |
| 391 | |
| 392 // Starts an invalid session device, it should fail. | |
|
tommi (sloooow) - chröme
2011/11/25 12:48:42
"it should fail" suggests that the test should fai
no longer working on chromium
2011/11/25 14:14:14
Done.
| |
| 393 TEST_F(AudioInputDeviceManagerTest, StartInvalidSession) { | |
| 394 if (!CanRunAudioInputDeviceTests()) | |
| 395 return; | |
| 396 InSequence s; | |
| 397 | |
| 398 // Creates the EventHandlers for the sessions. | |
| 399 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | |
| 400 audio_input_event_handler( | |
| 401 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | |
| 402 | |
| 403 // Opens the first device. | |
| 404 StreamDeviceInfoArray::const_iterator iter = | |
| 405 audio_input_listener_->devices_.begin(); | |
| 406 int session_id = manager_->Open(*iter); | |
| 407 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) | |
| 408 .Times(1); | |
| 409 message_loop_->RunAllPending(); | |
| 410 | |
| 411 // Starts a non-opened device. | |
| 412 // This should fail and trigger error code 'kDeviceNotAvailable'. | |
| 413 int invalid_session_id = session_id + 1; | |
| 414 manager_->Start(invalid_session_id, audio_input_event_handler.get()); | |
| 415 EXPECT_CALL(*audio_input_event_handler, | |
| 416 DeviceStarted(invalid_session_id, | |
| 417 AudioInputDeviceManager::kInvalidDeviceId)) | |
| 418 .Times(1); | |
| 419 message_loop_->RunAllPending(); | |
| 420 | |
| 421 manager_->Close(session_id); | |
| 422 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) | |
| 423 .Times(1); | |
| 424 message_loop_->RunAllPending(); | |
| 425 } | |
| 426 | |
| 427 // Starts a session twice, the first one should succeed, while the second | |
| 428 // should fail. | |
| 429 TEST_F(AudioInputDeviceManagerTest, StartSessionTwice) { | |
| 430 if (!CanRunAudioInputDeviceTests()) | |
| 431 return; | |
| 432 InSequence s; | |
| 433 | |
| 434 // Creates the EventHandlers for the sessions. | |
| 435 scoped_ptr<MockAudioInputDeviceManagerEventHandler> | |
| 436 audio_input_event_handler( | |
| 437 new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); | |
| 438 | |
| 439 // Opens the first device. | |
| 440 StreamDeviceInfoArray::const_iterator iter = | |
| 441 audio_input_listener_->devices_.begin(); | |
| 442 int session_id = manager_->Open(*iter); | |
| 443 EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) | |
| 444 .Times(1); | |
| 445 message_loop_->RunAllPending(); | |
| 446 | |
| 447 // Starts the session, it should succeed. | |
| 448 manager_->Start(session_id, audio_input_event_handler.get()); | |
| 449 EXPECT_CALL(*audio_input_event_handler, | |
| 450 DeviceStarted(session_id, | |
| 451 AudioManagerBase::kDefaultDeviceId)) | |
| 452 .Times(1); | |
| 453 message_loop_->RunAllPending(); | |
| 454 | |
| 455 // Starts the session for the second time, it should fail. | |
| 456 manager_->Start(session_id, audio_input_event_handler.get()); | |
| 457 EXPECT_CALL(*audio_input_event_handler, | |
| 458 DeviceStarted(session_id, | |
| 459 AudioInputDeviceManager::kInvalidDeviceId)) | |
| 460 .Times(1); | |
| 461 | |
| 462 manager_->Stop(session_id); | |
| 463 manager_->Close(session_id); | |
| 464 EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) | |
| 465 .Times(1); | |
| 466 message_loop_->RunAllPending(); | |
| 401 } | 467 } |
| 402 | 468 |
| 403 } // namespace media_stream | 469 } // namespace media_stream |
| OLD | NEW |