Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromeos/audio/cras_audio_handler.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | |
| 12 #include "chromeos/dbus/audio_node.h" | |
| 13 #include "chromeos/dbus/cras_audio_client_stub_impl.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using chromeos::AudioNode; | |
|
stevenjb
2013/07/26 17:23:30
We could put this entire file inside namespace chr
jennyz
2013/07/26 18:14:44
Done.
| |
| 18 | |
| 19 namespace { | |
| 20 const uint64 kInternalSpeakerId = 10001; | |
| 21 const uint64 kHeadphoneId = 10002; | |
| 22 const uint64 kInternalMicId = 10003; | |
| 23 const uint64 kUSBMicId = 10004; | |
| 24 const uint64 kBluetoothHeadsetId = 10005; | |
| 25 const uint64 kHDMIOutputId = 10006; | |
| 26 const uint64 kUSBHeadphoneId1 = 10007; | |
| 27 const uint64 kUSBHeadphoneId2 = 10008; | |
| 28 const uint64 kOtherTypeOutputId = 90001; | |
| 29 const uint64 kOtherTypeInputId = 90002; | |
| 30 | |
| 31 const AudioNode kInternalSpeaker( | |
| 32 false, | |
| 33 kInternalSpeakerId, | |
| 34 "Fake Speaker", | |
| 35 "INTERNAL_SPEAKER", | |
| 36 "Speaker", | |
| 37 false, | |
| 38 0 | |
| 39 ); | |
| 40 | |
| 41 const AudioNode kHeadphone( | |
| 42 false, | |
| 43 kHeadphoneId, | |
| 44 "Fake Headphone", | |
| 45 "HEADPHONE", | |
| 46 "Headphone", | |
| 47 false, | |
| 48 0 | |
| 49 ); | |
| 50 | |
| 51 const AudioNode kInternalMic( | |
| 52 true, | |
| 53 kInternalMicId, | |
| 54 "Fake Mic", | |
| 55 "INTERNAL_MIC", | |
| 56 "Internal Mic", | |
| 57 false, | |
| 58 0 | |
| 59 ); | |
| 60 | |
| 61 const AudioNode kUSBMic( | |
| 62 true, | |
| 63 kUSBMicId, | |
| 64 "Fake USB Mic", | |
| 65 "USB", | |
| 66 "USB Microphone", | |
| 67 false, | |
| 68 0 | |
| 69 ); | |
| 70 | |
| 71 const AudioNode kOtherTypeOutput( | |
| 72 false, | |
| 73 kOtherTypeOutputId, | |
| 74 "Output Device", | |
| 75 "SOME_OTHER_TYPE", | |
| 76 "Other Type Output Device", | |
| 77 false, | |
| 78 0 | |
| 79 ); | |
| 80 | |
| 81 const AudioNode kOtherTypeInput( | |
| 82 true, | |
| 83 kOtherTypeInputId, | |
| 84 "Input Device", | |
| 85 "SOME_OTHER_TYPE", | |
| 86 "Other Type Input Device", | |
| 87 false, | |
| 88 0 | |
| 89 ); | |
| 90 | |
| 91 const AudioNode kBluetoothHeadset ( | |
| 92 false, | |
| 93 kBluetoothHeadsetId, | |
| 94 "Bluetooth Headset", | |
| 95 "BLUETOOTH", | |
| 96 "Bluetooth Headset 1", | |
| 97 false, | |
| 98 0 | |
| 99 ); | |
| 100 | |
| 101 const AudioNode kHDMIOutput ( | |
| 102 false, | |
| 103 kHDMIOutputId, | |
| 104 "HDMI output", | |
| 105 "HDMI", | |
| 106 "HDMI output", | |
| 107 false, | |
| 108 0 | |
| 109 ); | |
| 110 | |
| 111 const AudioNode kUSBHeadphone1 ( | |
| 112 false, | |
| 113 kUSBHeadphoneId1, | |
| 114 "USB Headphone", | |
| 115 "USB", | |
| 116 "USB Headphone 1", | |
| 117 false, | |
| 118 0 | |
| 119 ); | |
| 120 | |
| 121 const AudioNode kUSBHeadphone2 ( | |
| 122 false, | |
| 123 kUSBHeadphoneId2, | |
| 124 "USB Headphone", | |
| 125 "USB", | |
| 126 "USB Headphone 1", | |
| 127 false, | |
| 128 0 | |
| 129 ); | |
| 130 | |
| 131 | |
| 132 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { | |
| 133 public: | |
| 134 TestObserver() : active_output_node_changed_count_(0), | |
| 135 active_input_node_changed_count_(0), | |
| 136 audio_nodes_changed_count_(0), | |
| 137 output_mute_changed_count_(0), | |
| 138 input_mute_changed_count_(0), | |
| 139 output_volume_changed_count_(0), | |
| 140 input_gain_changed_count_(0) { | |
| 141 } | |
| 142 | |
| 143 int active_output_node_changed_count() const { | |
| 144 return active_output_node_changed_count_; | |
| 145 } | |
| 146 | |
| 147 int active_input_node_changed_count() const { | |
| 148 return active_input_node_changed_count_; | |
| 149 } | |
| 150 | |
| 151 int audio_nodes_changed_count() const { | |
| 152 return audio_nodes_changed_count_; | |
| 153 } | |
| 154 | |
| 155 int output_mute_changed_count() const { | |
| 156 return output_mute_changed_count_; | |
| 157 } | |
| 158 | |
| 159 int input_mute_changed_count() const { | |
| 160 return input_mute_changed_count_; | |
| 161 } | |
| 162 | |
| 163 int output_volume_changed_count() const { | |
| 164 return output_volume_changed_count_; | |
| 165 } | |
| 166 | |
| 167 int input_gain_changed_count() const { | |
| 168 return input_gain_changed_count_; | |
| 169 } | |
| 170 | |
| 171 virtual ~TestObserver() {} | |
| 172 | |
| 173 protected: | |
| 174 // chromeos::CrasAudioHandler::AudioObserver overrides. | |
| 175 virtual void OnActiveOutputNodeChanged() OVERRIDE { | |
| 176 ++active_output_node_changed_count_; | |
| 177 } | |
| 178 | |
| 179 virtual void OnActiveInputNodeChanged() OVERRIDE { | |
| 180 ++active_input_node_changed_count_; | |
| 181 } | |
| 182 | |
| 183 virtual void OnAudioNodesChanged() OVERRIDE { | |
| 184 ++audio_nodes_changed_count_; | |
| 185 } | |
| 186 | |
| 187 virtual void OnOutputMuteChanged() OVERRIDE { | |
| 188 ++output_mute_changed_count_; | |
| 189 } | |
| 190 | |
| 191 virtual void OnInputMuteChanged() OVERRIDE { | |
| 192 ++input_mute_changed_count_; | |
| 193 } | |
| 194 | |
| 195 virtual void OnOutputVolumeChanged() OVERRIDE { | |
| 196 ++output_volume_changed_count_; | |
| 197 } | |
| 198 | |
| 199 virtual void OnInputGainChanged() OVERRIDE { | |
| 200 ++input_gain_changed_count_; | |
| 201 } | |
| 202 | |
| 203 private: | |
| 204 int active_output_node_changed_count_; | |
| 205 int active_input_node_changed_count_; | |
| 206 int audio_nodes_changed_count_; | |
| 207 int output_mute_changed_count_; | |
| 208 int input_mute_changed_count_; | |
| 209 int output_volume_changed_count_; | |
| 210 int input_gain_changed_count_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 213 }; | |
| 214 | |
| 215 } // namespace | |
| 216 | |
| 217 namespace chromeos{ | |
| 218 | |
| 219 class CrasAudioHandlerTest : public testing::Test { | |
| 220 public: | |
| 221 CrasAudioHandlerTest() : cras_audio_handler_(NULL), | |
| 222 cras_audio_client_stub_(NULL) { | |
| 223 } | |
| 224 virtual ~CrasAudioHandlerTest() {} | |
| 225 | |
| 226 virtual void SetUp() OVERRIDE { | |
| 227 } | |
| 228 | |
| 229 virtual void TearDown() OVERRIDE { | |
| 230 cras_audio_handler_->RemoveAudioObserver(test_observer_.get()); | |
| 231 test_observer_.reset(); | |
| 232 CrasAudioHandler::Shutdown(); | |
| 233 audio_pref_handler_ = NULL; | |
| 234 DBusThreadManager::Shutdown(); | |
| 235 } | |
| 236 | |
| 237 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) { | |
| 238 DBusThreadManager::InitializeWithStub(); | |
| 239 cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>( | |
| 240 DBusThreadManager::Get()->GetCrasAudioClient()); | |
| 241 cras_audio_client_stub_->SetAudioDevices(audio_nodes); | |
| 242 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); | |
| 243 CrasAudioHandler::Initialize(audio_pref_handler_); | |
| 244 cras_audio_handler_ = CrasAudioHandler::Get(); | |
| 245 test_observer_.reset(new TestObserver); | |
| 246 cras_audio_handler_->AddAudioObserver(test_observer_.get()); | |
| 247 message_loop_.RunUntilIdle(); | |
| 248 } | |
| 249 | |
| 250 void ChangeAudioNodes(const AudioNodeList& audio_nodes) { | |
| 251 cras_audio_client_stub_->ChangeAudioNodes(audio_nodes); | |
| 252 message_loop_.RunUntilIdle(); | |
| 253 } | |
| 254 | |
| 255 protected: | |
| 256 base::MessageLoopForUI message_loop_; | |
| 257 CrasAudioHandler* cras_audio_handler_; // Not owned. | |
| 258 CrasAudioClientStubImpl* cras_audio_client_stub_; // Not owned. | |
|
stevenjb
2013/07/26 17:23:30
Unused?
jennyz
2013/07/26 18:14:44
It is used in both SetUpCrasAudioHandler and Chang
| |
| 259 scoped_ptr<TestObserver> test_observer_; | |
| 260 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; | |
| 261 | |
| 262 private: | |
| 263 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); | |
| 264 }; | |
| 265 | |
| 266 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) { | |
| 267 const size_t kNumStubAudioDevices = 2; | |
|
stevenjb
2013/07/26 17:23:30
Maybe use audio_nodes.size() instead, here and bel
jennyz
2013/07/26 18:14:44
Done.
| |
| 268 AudioNodeList audio_nodes; | |
| 269 audio_nodes.push_back(kInternalSpeaker); | |
| 270 audio_nodes.push_back(kInternalMic); | |
| 271 SetUpCrasAudioHandler(audio_nodes); | |
| 272 | |
| 273 // Verify the audio devices size. | |
| 274 AudioDeviceList audio_devices; | |
| 275 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 276 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 277 | |
| 278 // Verify the internal speaker has been selected as the active output. | |
| 279 AudioDevice active_output; | |
| 280 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 281 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 282 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 283 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 284 | |
| 285 // Ensure the internal microphone has been selected as the active input. | |
| 286 AudioDevice active_input; | |
| 287 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 288 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 289 } | |
| 290 | |
| 291 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) { | |
| 292 const size_t kNumStubAudioDevices = 4; | |
| 293 AudioNodeList audio_nodes; | |
| 294 audio_nodes.push_back(kInternalSpeaker); | |
| 295 audio_nodes.push_back(kHeadphone); | |
| 296 audio_nodes.push_back(kInternalMic); | |
| 297 audio_nodes.push_back(kUSBMic); | |
| 298 SetUpCrasAudioHandler(audio_nodes); | |
| 299 | |
| 300 // Verify the audio devices size. | |
| 301 AudioDeviceList audio_devices; | |
| 302 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 303 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 304 | |
| 305 // Verify the headphone has been selected as the active output. | |
| 306 AudioDevice active_output; | |
| 307 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 308 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 309 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 310 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 311 | |
| 312 // Ensure the USB microphone has been selected as the active input. | |
| 313 AudioDevice active_input; | |
| 314 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 315 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 316 } | |
| 317 | |
| 318 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) { | |
| 319 const size_t kNumStubAudioDevices = 2; | |
| 320 AudioNodeList audio_nodes; | |
| 321 audio_nodes.push_back(kInternalSpeaker); | |
| 322 audio_nodes.push_back(kHeadphone); | |
| 323 SetUpCrasAudioHandler(audio_nodes); | |
| 324 AudioDeviceList audio_devices; | |
| 325 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 326 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 327 | |
| 328 // Verify the initial active output device is headphone. | |
| 329 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 330 AudioDevice active_output; | |
| 331 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 332 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 333 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 334 | |
| 335 // Switch the active output to internal speaker. | |
| 336 AudioDevice internal_speaker(kInternalSpeaker); | |
| 337 cras_audio_handler_->SwitchToDevice(internal_speaker); | |
| 338 | |
| 339 // Verify the active output is switched to internal speaker, and the | |
| 340 // ActiveOutputNodeChanged event is fired. | |
| 341 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 342 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 343 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 344 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 345 } | |
| 346 | |
| 347 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) { | |
| 348 const size_t kNumStubAudioDevices = 2; | |
| 349 AudioNodeList audio_nodes; | |
| 350 audio_nodes.push_back(kInternalMic); | |
| 351 audio_nodes.push_back(kUSBMic); | |
| 352 SetUpCrasAudioHandler(audio_nodes); | |
| 353 AudioDeviceList audio_devices; | |
| 354 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 355 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 356 | |
| 357 // Verify the initial active input device is USB mic. | |
| 358 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 359 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 360 | |
| 361 // Switch the active input to internal mic. | |
| 362 AudioDevice internal_mic(kInternalMic); | |
| 363 cras_audio_handler_->SwitchToDevice(internal_mic); | |
| 364 | |
| 365 // Verify the active output is switched to internal speaker, and the active | |
| 366 // ActiveInputNodeChanged event is fired. | |
| 367 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 368 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 369 } | |
| 370 | |
| 371 TEST_F(CrasAudioHandlerTest, PlugHeadphone) { | |
| 372 // Set up initial audio devices, only with internal speaker. | |
| 373 const size_t kNumStubAudioDevices = 1; | |
| 374 AudioNodeList audio_nodes; | |
| 375 audio_nodes.push_back(kInternalSpeaker); | |
| 376 SetUpCrasAudioHandler(audio_nodes); | |
| 377 | |
| 378 // Verify the audio devices size. | |
| 379 AudioDeviceList audio_devices; | |
| 380 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 381 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 382 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 383 | |
| 384 // Verify the internal speaker has been selected as the active output. | |
| 385 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 386 AudioDevice active_output; | |
| 387 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 388 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 389 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 390 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 391 | |
| 392 // Plug the headphone. | |
| 393 audio_nodes.push_back(kHeadphone); | |
| 394 ChangeAudioNodes(audio_nodes); | |
| 395 | |
| 396 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 397 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 398 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 399 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 400 | |
| 401 // Verify the active output device is switched to headphone and | |
| 402 // ActiveOutputChanged event is fired. | |
| 403 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 404 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 405 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 406 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 407 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 408 } | |
| 409 | |
| 410 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) { | |
| 411 // Set up initial audio devices, with internal speaker and headphone. | |
| 412 const size_t kNumStubAudioDevices = 2; | |
| 413 AudioNodeList audio_nodes; | |
| 414 audio_nodes.push_back(kInternalSpeaker); | |
| 415 audio_nodes.push_back(kHeadphone); | |
| 416 SetUpCrasAudioHandler(audio_nodes); | |
| 417 | |
| 418 // Verify the audio devices size. | |
| 419 AudioDeviceList audio_devices; | |
| 420 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 421 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 422 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 423 | |
| 424 // Verify the headphone has been selected as the active output. | |
| 425 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 426 AudioDevice active_output; | |
| 427 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 428 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 429 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 430 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 431 | |
| 432 // Unplug the headphone. | |
| 433 audio_nodes.clear(); | |
| 434 audio_nodes.push_back(kInternalSpeaker); | |
| 435 ChangeAudioNodes(audio_nodes); | |
| 436 | |
| 437 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 438 // removed. | |
| 439 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 440 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 441 EXPECT_EQ(kNumStubAudioDevices - 1, audio_devices.size()); | |
| 442 | |
| 443 // Verify the active output device is switched to internal speaker and | |
| 444 // ActiveOutputChanged event is fired. | |
| 445 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 446 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 447 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 448 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 449 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 450 } | |
| 451 | |
| 452 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) { | |
| 453 const size_t kNumStubAudioDevices = 2; | |
| 454 AudioNodeList audio_nodes; | |
| 455 audio_nodes.push_back(kInternalSpeaker); | |
| 456 audio_nodes.push_back(kBluetoothHeadset); | |
| 457 SetUpCrasAudioHandler(audio_nodes); | |
| 458 | |
| 459 // Verify the audio devices size. | |
| 460 AudioDeviceList audio_devices; | |
| 461 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 462 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 463 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 464 | |
| 465 // Verify the bluetooth headset has been selected as the active output. | |
| 466 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 467 AudioDevice active_output; | |
| 468 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 469 EXPECT_EQ(kBluetoothHeadset.id, active_output.id); | |
| 470 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 471 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 472 } | |
| 473 | |
| 474 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) { | |
| 475 // Initialize with internal speaker and headphone. | |
| 476 const size_t kNumStubAudioDevices = 2; | |
| 477 AudioNodeList audio_nodes; | |
| 478 audio_nodes.push_back(kInternalSpeaker); | |
| 479 audio_nodes.push_back(kHeadphone); | |
| 480 SetUpCrasAudioHandler(audio_nodes); | |
| 481 | |
| 482 // Verify the audio devices size. | |
| 483 AudioDeviceList audio_devices; | |
| 484 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 485 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 486 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 487 | |
| 488 // Verify the headphone is selected as the active output initially. | |
| 489 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 490 AudioDevice active_output; | |
| 491 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 492 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 493 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 494 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 495 | |
| 496 // Connect to bluetooth headset. Since it is plugged in later than | |
| 497 // headphone, active output should be switched to it. | |
| 498 audio_nodes.clear(); | |
| 499 audio_nodes.push_back(kInternalSpeaker); | |
| 500 AudioNode headphone(kHeadphone); | |
| 501 headphone.plugged_time = 80000000; | |
| 502 headphone.active = true; | |
| 503 audio_nodes.push_back(headphone); | |
| 504 AudioNode bluetooth_headset(kBluetoothHeadset); | |
| 505 bluetooth_headset.plugged_time = 90000000; | |
| 506 audio_nodes.push_back(bluetooth_headset); | |
| 507 ChangeAudioNodes(audio_nodes); | |
| 508 | |
| 509 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 510 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 511 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 512 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 513 | |
| 514 // Verify the active output device is switched to bluetooth headset, and | |
| 515 // ActiveOutputChanged event is fired. | |
| 516 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 517 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 518 EXPECT_EQ(kBluetoothHeadset.id, active_output.id); | |
| 519 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 520 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 521 | |
| 522 // Disconnect bluetooth headset. | |
| 523 audio_nodes.clear(); | |
| 524 audio_nodes.push_back(kInternalSpeaker); | |
| 525 audio_nodes.push_back(headphone); | |
| 526 ChangeAudioNodes(audio_nodes); | |
| 527 | |
| 528 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 529 // removed. | |
| 530 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); | |
| 531 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 532 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 533 | |
| 534 // Verify the active output device is switched to headphone, and | |
| 535 // ActiveOutputChanged event is fired. | |
| 536 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); | |
| 537 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 538 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 539 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 540 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 541 } | |
| 542 | |
| 543 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) { | |
| 544 const size_t kNumStubAudioDevices = 2; | |
| 545 AudioNodeList audio_nodes; | |
| 546 audio_nodes.push_back(kInternalSpeaker); | |
| 547 audio_nodes.push_back(kHDMIOutput); | |
| 548 SetUpCrasAudioHandler(audio_nodes); | |
| 549 | |
| 550 // Verify the audio devices size. | |
| 551 AudioDeviceList audio_devices; | |
| 552 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 553 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 554 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 555 | |
| 556 // Verify the HDMI device has been selected as the active output. | |
| 557 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 558 AudioDevice active_output; | |
| 559 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 560 EXPECT_EQ(kHDMIOutput.id, active_output.id); | |
| 561 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 562 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 563 } | |
| 564 | |
| 565 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) { | |
| 566 // Initialize with internal speaker. | |
| 567 const size_t kNumStubAudioDevices = 1; | |
| 568 AudioNodeList audio_nodes; | |
| 569 audio_nodes.push_back(kInternalSpeaker); | |
| 570 SetUpCrasAudioHandler(audio_nodes); | |
| 571 | |
| 572 // Verify the audio devices size. | |
| 573 AudioDeviceList audio_devices; | |
| 574 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 575 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 576 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 577 | |
| 578 // Verify the internal speaker is selected as the active output initially. | |
| 579 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 580 AudioDevice active_output; | |
| 581 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 582 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 583 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 584 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 585 | |
| 586 // Connect to HDMI output. | |
| 587 audio_nodes.clear(); | |
| 588 AudioNode internal_speaker(kInternalSpeaker); | |
| 589 internal_speaker.active = true; | |
| 590 internal_speaker.plugged_time = 80000000; | |
| 591 audio_nodes.push_back(internal_speaker); | |
| 592 AudioNode hdmi(kHDMIOutput); | |
| 593 hdmi.plugged_time = 90000000; | |
| 594 audio_nodes.push_back(hdmi); | |
| 595 ChangeAudioNodes(audio_nodes); | |
| 596 | |
| 597 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 598 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 599 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 600 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 601 | |
| 602 // Verify the active output device is switched to hdmi output, and | |
| 603 // ActiveOutputChanged event is fired. | |
| 604 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 605 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 606 EXPECT_EQ(kHDMIOutput.id, active_output.id); | |
| 607 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 608 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 609 | |
| 610 // Disconnect hdmi headset. | |
| 611 audio_nodes.clear(); | |
| 612 audio_nodes.push_back(kInternalSpeaker); | |
| 613 ChangeAudioNodes(audio_nodes); | |
| 614 | |
| 615 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 616 // removed. | |
| 617 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); | |
| 618 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 619 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 620 | |
| 621 // Verify the active output device is switched to internal speaker, and | |
| 622 // ActiveOutputChanged event is fired. | |
| 623 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); | |
| 624 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 625 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 626 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 627 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 628 } | |
| 629 | |
| 630 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) { | |
| 631 // Initialize with internal speaker, headphone and HDMI output. | |
| 632 const size_t kNumStubAudioDevices = 3; | |
| 633 AudioNodeList audio_nodes; | |
| 634 audio_nodes.push_back(kInternalSpeaker); | |
| 635 audio_nodes.push_back(kHeadphone); | |
| 636 audio_nodes.push_back(kHDMIOutput); | |
| 637 SetUpCrasAudioHandler(audio_nodes); | |
| 638 | |
| 639 // Verify the audio devices size. | |
| 640 AudioDeviceList audio_devices; | |
| 641 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 642 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 643 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 644 | |
| 645 // Verify the headphone is selected as the active output initially. | |
| 646 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 647 AudioDevice active_output; | |
| 648 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 649 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 650 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 651 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 652 | |
| 653 // Disconnect HDMI output. | |
| 654 audio_nodes.clear(); | |
| 655 audio_nodes.push_back(kInternalSpeaker); | |
| 656 audio_nodes.push_back(kHDMIOutput); | |
| 657 ChangeAudioNodes(audio_nodes); | |
| 658 | |
| 659 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 660 // removed. | |
| 661 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 662 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 663 EXPECT_EQ(kNumStubAudioDevices - 1, audio_devices.size()); | |
| 664 | |
| 665 // Verify the active output device is switched to HDMI output, and | |
| 666 // ActiveOutputChanged event is fired. | |
| 667 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 668 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 669 EXPECT_EQ(kHDMIOutput.id, active_output.id); | |
| 670 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 671 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 672 } | |
| 673 | |
| 674 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) { | |
| 675 const size_t kNumStubAudioDevices = 2; | |
| 676 AudioNodeList audio_nodes; | |
| 677 audio_nodes.push_back(kInternalSpeaker); | |
| 678 audio_nodes.push_back(kUSBHeadphone1); | |
| 679 SetUpCrasAudioHandler(audio_nodes); | |
| 680 | |
| 681 // Verify the audio devices size. | |
| 682 AudioDeviceList audio_devices; | |
| 683 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 684 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 685 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 686 | |
| 687 // Verify the usb headphone has been selected as the active output. | |
| 688 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 689 AudioDevice active_output; | |
| 690 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 691 EXPECT_EQ(kUSBHeadphone1.id, active_output.id); | |
| 692 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 693 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 694 } | |
| 695 | |
| 696 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) { | |
| 697 // Initialize with internal speaker. | |
| 698 const size_t kNumStubAudioDevices = 1; | |
| 699 AudioNodeList audio_nodes; | |
| 700 audio_nodes.push_back(kInternalSpeaker); | |
| 701 SetUpCrasAudioHandler(audio_nodes); | |
| 702 | |
| 703 // Verify the audio devices size. | |
| 704 AudioDeviceList audio_devices; | |
| 705 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 706 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 707 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 708 | |
| 709 // Verify the internal speaker is selected as the active output initially. | |
| 710 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 711 AudioDevice active_output; | |
| 712 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 713 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 714 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 715 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 716 | |
| 717 // Plug in usb headphone | |
| 718 audio_nodes.clear(); | |
| 719 AudioNode internal_speaker(kInternalSpeaker); | |
| 720 internal_speaker.active = true; | |
| 721 internal_speaker.plugged_time = 80000000; | |
| 722 audio_nodes.push_back(internal_speaker); | |
| 723 AudioNode usb_headphone(kUSBHeadphone1); | |
| 724 usb_headphone.plugged_time = 90000000; | |
| 725 audio_nodes.push_back(usb_headphone); | |
| 726 ChangeAudioNodes(audio_nodes); | |
| 727 | |
| 728 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 729 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 730 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 731 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 732 | |
| 733 // Verify the active output device is switched to usb headphone, and | |
| 734 // ActiveOutputChanged event is fired. | |
| 735 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 736 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 737 EXPECT_EQ(kUSBHeadphone1.id, active_output.id); | |
| 738 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 739 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 740 | |
| 741 // Unplug usb headphone. | |
| 742 audio_nodes.clear(); | |
| 743 audio_nodes.push_back(kInternalSpeaker); | |
| 744 ChangeAudioNodes(audio_nodes); | |
| 745 | |
| 746 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 747 // removed. | |
| 748 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); | |
| 749 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 750 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 751 | |
| 752 // Verify the active output device is switched to internal speaker, and | |
| 753 // ActiveOutputChanged event is fired. | |
| 754 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); | |
| 755 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 756 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 757 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 758 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 759 } | |
| 760 | |
| 761 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) { | |
| 762 // Initialize with internal speaker and one usb headphone. | |
| 763 const size_t kNumStubAudioDevices = 2; | |
| 764 AudioNodeList audio_nodes; | |
| 765 audio_nodes.push_back(kInternalSpeaker); | |
| 766 audio_nodes.push_back(kUSBHeadphone1); | |
| 767 SetUpCrasAudioHandler(audio_nodes); | |
| 768 | |
| 769 // Verify the audio devices size. | |
| 770 AudioDeviceList audio_devices; | |
| 771 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 772 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 773 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 774 | |
| 775 // Verify the usb headphone is selected as the active output initially. | |
| 776 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 777 AudioDevice active_output; | |
| 778 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 779 EXPECT_EQ(kUSBHeadphone1.id, active_output.id); | |
| 780 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 781 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 782 | |
| 783 // Plug in another usb headphone. | |
| 784 audio_nodes.clear(); | |
| 785 audio_nodes.push_back(kInternalSpeaker); | |
| 786 AudioNode usb_headphone_1(kUSBHeadphone1); | |
| 787 usb_headphone_1.active = true; | |
| 788 usb_headphone_1.plugged_time = 80000000; | |
| 789 audio_nodes.push_back(usb_headphone_1); | |
| 790 AudioNode usb_headphone_2(kUSBHeadphone2); | |
| 791 usb_headphone_2.plugged_time = 90000000; | |
| 792 audio_nodes.push_back(usb_headphone_2); | |
| 793 ChangeAudioNodes(audio_nodes); | |
| 794 | |
| 795 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 796 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 797 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 798 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 799 | |
| 800 // Verify the active output device is switched to the 2nd usb headphone, which | |
| 801 // is plugged later, and ActiveOutputChanged event is fired. | |
| 802 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 803 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 804 EXPECT_EQ(kUSBHeadphone2.id, active_output.id); | |
| 805 EXPECT_EQ(kUSBHeadphone2.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 806 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 807 | |
| 808 // Unplug the 2nd usb headphone. | |
| 809 audio_nodes.clear(); | |
| 810 audio_nodes.push_back(kInternalSpeaker); | |
| 811 audio_nodes.push_back(kUSBHeadphone1); | |
| 812 ChangeAudioNodes(audio_nodes); | |
| 813 | |
| 814 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 815 // removed. | |
| 816 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); | |
| 817 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 818 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 819 | |
| 820 // Verify the active output device is switched to the first usb headphone, and | |
| 821 // ActiveOutputChanged event is fired. | |
| 822 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); | |
| 823 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 824 EXPECT_EQ(kUSBHeadphone1.id, active_output.id); | |
| 825 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 826 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 827 } | |
| 828 | |
| 829 TEST_F(CrasAudioHandlerTest, PlugUSBMic) { | |
| 830 // Set up initial audio devices, only with internal mic. | |
| 831 const size_t kNumStubAudioDevices = 1; | |
| 832 AudioNodeList audio_nodes; | |
| 833 audio_nodes.push_back(kInternalMic); | |
| 834 SetUpCrasAudioHandler(audio_nodes); | |
| 835 | |
| 836 // Verify the audio devices size. | |
| 837 AudioDeviceList audio_devices; | |
| 838 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 839 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 840 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 841 | |
| 842 // Verify the internal mic is selected as the active output. | |
| 843 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 844 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 845 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 846 | |
| 847 // Plug the USB Mic. | |
| 848 audio_nodes.push_back(kUSBMic); | |
| 849 ChangeAudioNodes(audio_nodes); | |
| 850 | |
| 851 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 852 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 853 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 854 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 855 | |
| 856 // Verify the active input device is switched to USB mic and | |
| 857 // and ActiveInputChanged event is fired. | |
| 858 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 859 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 860 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 861 } | |
| 862 | |
| 863 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) { | |
| 864 // Set up initial audio devices, with internal mic and USB Mic. | |
| 865 const size_t kNumStubAudioDevices = 2; | |
| 866 AudioNodeList audio_nodes; | |
| 867 audio_nodes.push_back(kInternalMic); | |
| 868 audio_nodes.push_back(kUSBMic); | |
| 869 SetUpCrasAudioHandler(audio_nodes); | |
| 870 | |
| 871 // Verify the audio devices size. | |
| 872 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 873 AudioDeviceList audio_devices; | |
| 874 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 875 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 876 | |
| 877 // Verify the USB mic is selected as the active output. | |
| 878 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 879 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 880 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 881 | |
| 882 // Unplug the USB Mic. | |
| 883 audio_nodes.clear(); | |
| 884 audio_nodes.push_back(kInternalMic); | |
| 885 ChangeAudioNodes(audio_nodes); | |
| 886 | |
| 887 // Verify the AudioNodesChanged event is fired, and one audio device is | |
| 888 // removed. | |
| 889 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 890 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 891 EXPECT_EQ(kNumStubAudioDevices - 1, audio_devices.size()); | |
| 892 | |
| 893 // Verify the active input device is switched to internal mic, and | |
| 894 // and ActiveInputChanged event is fired. | |
| 895 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 896 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 897 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 898 } | |
| 899 | |
| 900 TEST_F(CrasAudioHandlerTest, SetOutputMute) { | |
| 901 AudioNodeList audio_nodes; | |
| 902 audio_nodes.push_back(kInternalSpeaker); | |
| 903 SetUpCrasAudioHandler(audio_nodes); | |
| 904 EXPECT_EQ(0, test_observer_->output_mute_changed_count()); | |
| 905 | |
| 906 // Mute the device. | |
| 907 cras_audio_handler_->SetOutputMute(true); | |
| 908 | |
| 909 // Verify the output is muted, OnOutputMuteChanged event is fired, | |
| 910 // and mute value is saved in the preferences. | |
| 911 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); | |
| 912 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | |
| 913 AudioDevice speaker(kInternalSpeaker); | |
| 914 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker)); | |
| 915 | |
| 916 // Unmute the device. | |
| 917 cras_audio_handler_->SetOutputMute(false); | |
| 918 | |
| 919 // Verify the output is unmuted, OnOutputMuteChanged event is fired, | |
| 920 // and mute value is saved in the preferences. | |
| 921 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | |
| 922 EXPECT_EQ(2, test_observer_->output_mute_changed_count()); | |
| 923 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker)); | |
| 924 } | |
| 925 | |
| 926 TEST_F(CrasAudioHandlerTest, SetInputMute) { | |
| 927 AudioNodeList audio_nodes; | |
| 928 audio_nodes.push_back(kInternalMic); | |
| 929 SetUpCrasAudioHandler(audio_nodes); | |
| 930 EXPECT_EQ(0, test_observer_->input_mute_changed_count()); | |
| 931 | |
| 932 // Mute the device. | |
| 933 cras_audio_handler_->SetInputMute(true); | |
| 934 | |
| 935 // Verify the input is muted, OnInputMuteChanged event is fired, | |
| 936 // and mute value is saved in the preferences. | |
| 937 EXPECT_TRUE(cras_audio_handler_->IsInputMuted()); | |
| 938 EXPECT_EQ(1, test_observer_->input_mute_changed_count()); | |
| 939 AudioDevice internal_mic(kInternalMic); | |
| 940 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 941 | |
| 942 // Unmute the device. | |
| 943 cras_audio_handler_->SetInputMute(false); | |
| 944 | |
| 945 // Verify the input is unmuted, OnInputMuteChanged event is fired, | |
| 946 // and mute value is saved in the preferences. | |
| 947 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); | |
| 948 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); | |
| 949 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 950 } | |
| 951 | |
| 952 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { | |
| 953 AudioNodeList audio_nodes; | |
| 954 audio_nodes.push_back(kInternalSpeaker); | |
| 955 SetUpCrasAudioHandler(audio_nodes); | |
| 956 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 957 | |
| 958 cras_audio_handler_->SetOutputVolumePercent(60); | |
| 959 | |
| 960 // Verify the output volume is changed to the designated value, | |
| 961 // OnOutputVolumeChanged event is fired, and the device volume value | |
| 962 // is saved the preferences. | |
| 963 const int kVolume = 60; | |
| 964 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 965 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 966 AudioDevice device; | |
| 967 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device)); | |
| 968 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 969 EXPECT_EQ(kVolume, audio_pref_handler_->GetVolumeGainValue(device)); | |
| 970 } | |
| 971 | |
| 972 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) { | |
| 973 AudioNodeList audio_nodes; | |
| 974 audio_nodes.push_back(kInternalMic); | |
| 975 SetUpCrasAudioHandler(audio_nodes); | |
| 976 EXPECT_EQ(0, test_observer_->input_gain_changed_count()); | |
| 977 | |
| 978 cras_audio_handler_->SetInputGainPercent(60); | |
| 979 | |
| 980 // Verify the input gain changed to the designated value, | |
| 981 // OnInputGainChanged event is fired, and the device gain value | |
| 982 // is saved in the preferences. | |
| 983 const int kGain = 60; | |
| 984 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent()); | |
| 985 EXPECT_EQ(1, test_observer_->input_gain_changed_count()); | |
| 986 AudioDevice internal_mic(kInternalMic); | |
| 987 EXPECT_EQ(kGain, audio_pref_handler_->GetVolumeGainValue(internal_mic)); | |
| 988 } | |
| 989 | |
| 990 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) { | |
| 991 AudioNodeList audio_nodes; | |
| 992 audio_nodes.push_back(kInternalSpeaker); | |
| 993 audio_nodes.push_back(kHeadphone); | |
| 994 audio_nodes.push_back(kInternalMic); | |
| 995 audio_nodes.push_back(kUSBMic); | |
| 996 SetUpCrasAudioHandler(audio_nodes); | |
| 997 | |
| 998 // Mute the active output device. | |
| 999 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 1000 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true); | |
| 1001 | |
| 1002 // Verify the headphone is muted and mute value is saved in the preferences. | |
| 1003 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id)); | |
| 1004 AudioDevice headphone(kHeadphone); | |
| 1005 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone)); | |
| 1006 | |
| 1007 // Mute the non-active output device. | |
| 1008 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true); | |
| 1009 | |
| 1010 // Verify the internal speaker is muted and mute value is saved in the | |
| 1011 // preferences. | |
| 1012 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id)); | |
| 1013 AudioDevice internal_speaker(kInternalSpeaker); | |
| 1014 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker)); | |
| 1015 | |
| 1016 // Mute the active input device. | |
| 1017 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 1018 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true); | |
| 1019 | |
| 1020 // Verify the USB Mic is muted and mute state is saved in the preferences. | |
| 1021 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kUSBMic.id)); | |
| 1022 AudioDevice usb_mic(kUSBMic); | |
| 1023 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(usb_mic)); | |
| 1024 | |
| 1025 // Mute the non-active input device. | |
| 1026 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true); | |
| 1027 | |
| 1028 // Verify the internal mic is muted and mute value is saved in the | |
| 1029 // preferences. | |
| 1030 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalMic.id)); | |
| 1031 AudioDevice internal_mic(kInternalMic); | |
| 1032 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 1033 } | |
| 1034 | |
| 1035 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) { | |
| 1036 AudioNodeList audio_nodes; | |
| 1037 audio_nodes.push_back(kInternalSpeaker); | |
| 1038 audio_nodes.push_back(kHeadphone); | |
| 1039 audio_nodes.push_back(kInternalMic); | |
| 1040 audio_nodes.push_back(kUSBMic); | |
| 1041 SetUpCrasAudioHandler(audio_nodes); | |
| 1042 | |
| 1043 // Set volume percent for active output device. | |
| 1044 const int kHeadphoneVolume = 30; | |
| 1045 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 1046 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id, | |
| 1047 kHeadphoneVolume); | |
| 1048 | |
| 1049 // Verify the volume percent of headphone is set, and saved in preferences. | |
| 1050 EXPECT_EQ(kHeadphoneVolume, | |
| 1051 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 1052 kHeadphone.id)); | |
| 1053 AudioDevice headphone(kHeadphone); | |
| 1054 EXPECT_EQ(kHeadphoneVolume, | |
| 1055 audio_pref_handler_->GetVolumeGainValue(headphone)); | |
| 1056 | |
| 1057 // Set volume percent for non-active output device. | |
| 1058 const int kSpeakerVolume = 60; | |
| 1059 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id, | |
| 1060 kSpeakerVolume); | |
| 1061 | |
| 1062 // Verify the volume percent of speaker is set, and saved in preferences. | |
| 1063 EXPECT_EQ(kSpeakerVolume, | |
| 1064 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 1065 kInternalSpeaker.id)); | |
| 1066 AudioDevice speaker(kInternalSpeaker); | |
| 1067 EXPECT_EQ(kSpeakerVolume, | |
| 1068 audio_pref_handler_->GetVolumeGainValue(speaker)); | |
| 1069 | |
| 1070 // Set gain percent for active input device. | |
| 1071 const int kUSBMicGain = 30; | |
| 1072 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 1073 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id, | |
| 1074 kUSBMicGain); | |
| 1075 | |
| 1076 // Verify the gain percent of USB mic is set, and saved in preferences. | |
| 1077 EXPECT_EQ(kUSBMicGain, | |
| 1078 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id)); | |
| 1079 AudioDevice usb_mic(kHeadphone); | |
| 1080 EXPECT_EQ(kUSBMicGain, | |
| 1081 audio_pref_handler_->GetVolumeGainValue(usb_mic)); | |
| 1082 | |
| 1083 // Set gain percent for non-active input device. | |
| 1084 const int kInternalMicGain = 60; | |
| 1085 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id, | |
| 1086 kInternalMicGain); | |
| 1087 | |
| 1088 // Verify the gain percent of internal mic is set, and saved in preferences. | |
| 1089 EXPECT_EQ(kInternalMicGain, | |
| 1090 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 1091 kInternalMic.id)); | |
| 1092 AudioDevice internal_mic(kInternalMic); | |
| 1093 EXPECT_EQ(kInternalMicGain, | |
| 1094 audio_pref_handler_->GetVolumeGainValue(internal_mic)); | |
| 1095 } | |
| 1096 | |
| 1097 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) { | |
| 1098 const size_t kNumValidAudioDevices = 4; | |
| 1099 AudioNodeList audio_nodes; | |
| 1100 audio_nodes.push_back(kInternalSpeaker); | |
| 1101 audio_nodes.push_back(kOtherTypeOutput); | |
| 1102 audio_nodes.push_back(kInternalMic); | |
| 1103 audio_nodes.push_back(kOtherTypeInput); | |
| 1104 SetUpCrasAudioHandler(audio_nodes); | |
| 1105 | |
| 1106 // Verify the audio devices size. | |
| 1107 AudioDeviceList audio_devices; | |
| 1108 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 1109 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size()); | |
| 1110 | |
| 1111 // Verify the internal speaker has been selected as the active output, | |
| 1112 // and the output device with some randown unknown type is handled gracefully. | |
| 1113 AudioDevice active_output; | |
| 1114 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 1115 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 1116 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 1117 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 1118 | |
| 1119 // Ensure the internal microphone has been selected as the active input, | |
| 1120 // and the input device with some random unknown type is handled gracefully. | |
| 1121 AudioDevice active_input; | |
| 1122 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 1123 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 1124 } | |
| 1125 | |
| 1126 } // namespace chromeos | |
| OLD | NEW |