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