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; | |
| 18 | |
| 19 namespace { | |
| 20 const uint64 kInternalSpeakerId = 10001; | |
| 21 const uint64 kHeadphoneId = 10002; | |
| 22 const uint64 kInternalMicId = 10003; | |
| 23 const uint64 kUSBMicId = 10004; | |
| 24 | |
| 25 const AudioNode kInternalSpeaker( | |
| 26 false, | |
| 27 kInternalSpeakerId, | |
| 28 "Fake Speaker", | |
| 29 "INTERNAL_SPEAKER", | |
| 30 "Speaker", | |
| 31 false, | |
| 32 0 | |
| 33 ); | |
| 34 | |
| 35 const AudioNode kHeadphone( | |
| 36 false, | |
| 37 kHeadphoneId, | |
| 38 "Fake Headphone", | |
| 39 "HEADPHONE", | |
| 40 "Headphone", | |
| 41 false, | |
| 42 0 | |
| 43 ); | |
| 44 | |
| 45 const AudioNode kInternalMic( | |
| 46 true, | |
| 47 kInternalMicId, | |
| 48 "Fake Mic", | |
| 49 "INTERNAL_MIC", | |
| 50 "Internal Mic", | |
| 51 false, | |
| 52 0 | |
| 53 ); | |
| 54 | |
| 55 const AudioNode kUSBMic( | |
|
rkc
2013/07/23 20:50:53
We want to add some invalid devices to add negativ
jennyz
2013/07/24 21:12:25
The audio node may give us some bogus audio device
| |
| 56 true, | |
| 57 kUSBMicId, | |
| 58 "Fake USB Mic", | |
| 59 "USB", | |
| 60 "USB Microphone", | |
| 61 false, | |
| 62 0 | |
| 63 ); | |
| 64 | |
| 65 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { | |
| 66 public: | |
| 67 TestObserver() : active_output_node_changed_count_(0), | |
| 68 active_input_node_changed_count_(0), | |
| 69 audio_nodes_changed_count_(0), | |
| 70 output_mute_changed_count_(0), | |
| 71 input_mute_changed_count_(0), | |
| 72 output_volume_changed_count_(0), | |
| 73 input_gain_changed_count_(0) { | |
| 74 } | |
| 75 | |
| 76 int active_output_node_changed_count() const { | |
| 77 return active_output_node_changed_count_; | |
| 78 } | |
| 79 | |
| 80 int active_input_node_changed_count() const { | |
| 81 return active_input_node_changed_count_; | |
| 82 } | |
| 83 | |
| 84 int audio_nodes_changed_count() const { | |
| 85 return audio_nodes_changed_count_; | |
| 86 } | |
| 87 | |
| 88 int output_mute_changed_count() const { | |
| 89 return output_mute_changed_count_; | |
| 90 } | |
| 91 | |
| 92 int input_mute_changed_count() const { | |
| 93 return input_mute_changed_count_; | |
| 94 } | |
| 95 | |
| 96 int output_volume_changed_count() const { | |
| 97 return output_volume_changed_count_; | |
| 98 } | |
| 99 | |
| 100 int input_gain_changed_count() const { | |
| 101 return input_gain_changed_count_; | |
| 102 } | |
| 103 | |
| 104 virtual ~TestObserver() {} | |
| 105 | |
| 106 protected: | |
| 107 // chromeos::CrasAudioHandler::AudioObserver overrides. | |
| 108 virtual void OnActiveOutputNodeChanged() OVERRIDE { | |
| 109 ++active_output_node_changed_count_; | |
| 110 } | |
| 111 | |
| 112 virtual void OnActiveInputNodeChanged() OVERRIDE { | |
| 113 ++active_input_node_changed_count_; | |
| 114 } | |
| 115 | |
| 116 virtual void OnAudioNodesChanged() OVERRIDE { | |
| 117 ++audio_nodes_changed_count_; | |
| 118 } | |
| 119 | |
| 120 virtual void OnOutputMuteChanged() OVERRIDE { | |
| 121 ++output_mute_changed_count_; | |
| 122 } | |
| 123 | |
| 124 virtual void OnInputMuteChanged() OVERRIDE { | |
| 125 ++input_mute_changed_count_; | |
| 126 } | |
| 127 | |
| 128 virtual void OnOutputVolumeChanged() OVERRIDE { | |
| 129 ++output_volume_changed_count_; | |
| 130 } | |
| 131 | |
| 132 virtual void OnInputGainChanged() OVERRIDE { | |
| 133 ++input_gain_changed_count_; | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 int active_output_node_changed_count_; | |
| 138 int active_input_node_changed_count_; | |
| 139 int audio_nodes_changed_count_; | |
| 140 int output_mute_changed_count_; | |
| 141 int input_mute_changed_count_; | |
| 142 int output_volume_changed_count_; | |
| 143 int input_gain_changed_count_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 146 }; | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 150 namespace chromeos{ | |
| 151 | |
| 152 class CrasAudioHandlerTest : public testing::Test { | |
| 153 public: | |
| 154 CrasAudioHandlerTest() : cras_audio_handler_(NULL), | |
| 155 cras_audio_client_stub_(NULL) { | |
| 156 } | |
| 157 virtual ~CrasAudioHandlerTest() {} | |
| 158 | |
| 159 virtual void SetUp() OVERRIDE { | |
| 160 } | |
| 161 | |
| 162 virtual void TearDown() OVERRIDE { | |
| 163 cras_audio_handler_->RemoveAudioObserver(test_observer_.get()); | |
| 164 test_observer_.reset(); | |
| 165 CrasAudioHandler::Shutdown(); | |
| 166 audio_pref_handler_ = NULL; | |
| 167 DBusThreadManager::Shutdown(); | |
| 168 } | |
| 169 | |
| 170 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) { | |
| 171 DBusThreadManager::InitializeWithStub(); | |
| 172 cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>( | |
| 173 DBusThreadManager::Get()->GetCrasAudioClient()); | |
| 174 cras_audio_client_stub_->SetAudioDevices(audio_nodes); | |
| 175 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); | |
| 176 CrasAudioHandler::Initialize(audio_pref_handler_); | |
| 177 cras_audio_handler_ = CrasAudioHandler::Get(); | |
| 178 test_observer_.reset(new TestObserver); | |
| 179 cras_audio_handler_->AddAudioObserver(test_observer_.get()); | |
| 180 message_loop_.RunUntilIdle(); | |
| 181 } | |
| 182 | |
| 183 void ChangeAudioNodes(const AudioNodeList& audio_nodes) { | |
| 184 cras_audio_client_stub_->ChangeAudioNodes(audio_nodes); | |
| 185 message_loop_.RunUntilIdle(); | |
| 186 } | |
| 187 | |
| 188 protected: | |
| 189 base::MessageLoopForUI message_loop_; | |
| 190 CrasAudioHandler* cras_audio_handler_; // Not owned. | |
| 191 CrasAudioClientStubImpl* cras_audio_client_stub_; // Not owned. | |
| 192 scoped_ptr<TestObserver> test_observer_; | |
| 193 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; | |
| 194 | |
| 195 private: | |
| 196 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); | |
| 197 }; | |
| 198 | |
| 199 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) { | |
| 200 const size_t kNumStubAudioDevices = 2; | |
| 201 AudioNodeList audio_nodes; | |
| 202 audio_nodes.push_back(kInternalSpeaker); | |
| 203 audio_nodes.push_back(kInternalMic); | |
| 204 SetUpCrasAudioHandler(audio_nodes); | |
| 205 | |
| 206 // Verify the audio devices size. | |
| 207 AudioDeviceList audio_devices; | |
| 208 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 209 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 210 | |
| 211 // Verify the internal speaker has been selected as the active output. | |
| 212 AudioDevice active_output; | |
| 213 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 214 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 215 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 216 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 217 | |
| 218 // Ensure the internal microphone has been selected as the active input. | |
| 219 AudioDevice active_input; | |
| 220 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 221 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 222 } | |
| 223 | |
| 224 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) { | |
| 225 const size_t kNumStubAudioDevices = 4; | |
| 226 AudioNodeList audio_nodes; | |
| 227 audio_nodes.push_back(kInternalSpeaker); | |
| 228 audio_nodes.push_back(kHeadphone); | |
| 229 audio_nodes.push_back(kInternalMic); | |
| 230 audio_nodes.push_back(kUSBMic); | |
| 231 SetUpCrasAudioHandler(audio_nodes); | |
| 232 | |
| 233 // Verify the audio devices size. | |
| 234 AudioDeviceList audio_devices; | |
| 235 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 236 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 237 | |
| 238 // Verify the headphone has been selected as the active output. | |
| 239 AudioDevice active_output; | |
| 240 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 241 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 242 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 243 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 244 | |
| 245 // Ensure the USB microphone has been selected as the active input. | |
| 246 AudioDevice active_input; | |
| 247 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 248 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 249 } | |
| 250 | |
| 251 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) { | |
| 252 const size_t kNumStubAudioDevices = 2; | |
| 253 AudioNodeList audio_nodes; | |
| 254 audio_nodes.push_back(kInternalSpeaker); | |
| 255 audio_nodes.push_back(kHeadphone); | |
| 256 SetUpCrasAudioHandler(audio_nodes); | |
| 257 AudioDeviceList audio_devices; | |
| 258 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 259 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 260 | |
| 261 // Verify the initial active output device is headphone. | |
| 262 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 263 AudioDevice active_output; | |
| 264 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 265 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 266 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 267 | |
| 268 // Switch the active output to internal speaker. | |
| 269 AudioDevice internal_speaker(kInternalSpeaker); | |
| 270 cras_audio_handler_->SwitchToDevice(internal_speaker); | |
| 271 | |
| 272 // Verify the active output is switched to internal speaker, and the | |
| 273 // ActiveOutputNodeChanged event is fired. | |
| 274 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 275 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 276 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 277 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 278 } | |
| 279 | |
| 280 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) { | |
| 281 const size_t kNumStubAudioDevices = 2; | |
| 282 AudioNodeList audio_nodes; | |
| 283 audio_nodes.push_back(kInternalMic); | |
| 284 audio_nodes.push_back(kUSBMic); | |
| 285 SetUpCrasAudioHandler(audio_nodes); | |
| 286 AudioDeviceList audio_devices; | |
| 287 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 288 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 289 | |
| 290 // Verify the initial active input device is USB mic. | |
| 291 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 292 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 293 | |
| 294 // Switch the active input to internal mic. | |
| 295 AudioDevice internal_mic(kInternalMic); | |
| 296 cras_audio_handler_->SwitchToDevice(internal_mic); | |
| 297 | |
| 298 // Verify the active output is switched to internal speaker, and the active | |
| 299 // ActiveInputNodeChanged event is fired. | |
| 300 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 301 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 302 } | |
| 303 | |
| 304 TEST_F(CrasAudioHandlerTest, PlugHeadphone) { | |
| 305 // Set up initial audio devices, only with internal speaker. | |
| 306 const size_t kNumStubAudioDevices = 1; | |
| 307 AudioNodeList audio_nodes; | |
| 308 audio_nodes.push_back(kInternalSpeaker); | |
| 309 SetUpCrasAudioHandler(audio_nodes); | |
| 310 | |
| 311 // Verify the audio devices size. | |
| 312 AudioDeviceList audio_devices; | |
| 313 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 314 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 315 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 316 | |
| 317 // Verify the internal speaker has been selected as the active output. | |
| 318 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 319 AudioDevice active_output; | |
| 320 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 321 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 322 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 323 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 324 | |
| 325 // Plug the headphone. | |
| 326 audio_nodes.push_back(kHeadphone); | |
| 327 ChangeAudioNodes(audio_nodes); | |
| 328 | |
| 329 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 330 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 331 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 332 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 333 | |
| 334 // Verify the active output device is switched to headphone and | |
| 335 // and ActiveOutputChanged event is fired. | |
| 336 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 337 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 338 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 339 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 340 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 341 } | |
| 342 | |
| 343 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) { | |
| 344 // Set up initial audio devices, with internal speaker and headphone. | |
| 345 const size_t kNumStubAudioDevices = 2; | |
| 346 AudioNodeList audio_nodes; | |
| 347 audio_nodes.push_back(kInternalSpeaker); | |
| 348 audio_nodes.push_back(kHeadphone); | |
| 349 SetUpCrasAudioHandler(audio_nodes); | |
| 350 | |
| 351 // Verify the audio devices size. | |
| 352 AudioDeviceList audio_devices; | |
| 353 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 354 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 355 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 356 | |
| 357 // Verify the headphone has been selected as the active output. | |
| 358 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | |
| 359 AudioDevice active_output; | |
| 360 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 361 EXPECT_EQ(kHeadphone.id, active_output.id); | |
| 362 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 363 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | |
| 364 | |
| 365 // Unplug the headphone. | |
| 366 audio_nodes.clear(); | |
| 367 audio_nodes.push_back(kInternalSpeaker); | |
| 368 ChangeAudioNodes(audio_nodes); | |
| 369 | |
| 370 // Verify the AudioNodesChanged event is fired and one audio device is | |
| 371 // removed. | |
| 372 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 373 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 374 EXPECT_EQ(kNumStubAudioDevices - 1, audio_devices.size()); | |
| 375 | |
| 376 // Verify the active output device is switched to internal speaker and | |
| 377 // and ActiveOutputChanged event is fired. | |
| 378 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | |
| 379 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output)); | |
| 380 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | |
| 381 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 382 EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); | |
| 383 } | |
| 384 | |
| 385 TEST_F(CrasAudioHandlerTest, PlugUSBMic) { | |
| 386 // Set up initial audio devices, only with internal mic. | |
| 387 const size_t kNumStubAudioDevices = 1; | |
| 388 AudioNodeList audio_nodes; | |
| 389 audio_nodes.push_back(kInternalMic); | |
| 390 SetUpCrasAudioHandler(audio_nodes); | |
| 391 | |
| 392 // Verify the audio devices size. | |
| 393 AudioDeviceList audio_devices; | |
| 394 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 395 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 396 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 397 | |
| 398 // Verify the internal mic is selected as the active output. | |
| 399 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 400 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 401 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 402 | |
| 403 // Plug the USB Mic. | |
| 404 audio_nodes.push_back(kUSBMic); | |
| 405 ChangeAudioNodes(audio_nodes); | |
| 406 | |
| 407 // Verify the AudioNodesChanged event is fired and new audio device is added. | |
| 408 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 409 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 410 EXPECT_EQ(kNumStubAudioDevices + 1, audio_devices.size()); | |
| 411 | |
| 412 // Verify the active input device is switched to USB mic and | |
| 413 // and ActiveInputChanged event is fired. | |
| 414 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 415 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 416 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 417 } | |
| 418 | |
| 419 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) { | |
| 420 // Set up initial audio devices, with internal mic and USB Mic. | |
| 421 const size_t kNumStubAudioDevices = 2; | |
| 422 AudioNodeList audio_nodes; | |
| 423 audio_nodes.push_back(kInternalMic); | |
| 424 audio_nodes.push_back(kUSBMic); | |
| 425 SetUpCrasAudioHandler(audio_nodes); | |
| 426 | |
| 427 // Verify the audio devices size. | |
| 428 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); | |
| 429 AudioDeviceList audio_devices; | |
| 430 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 431 EXPECT_EQ(kNumStubAudioDevices, audio_devices.size()); | |
| 432 | |
| 433 // Verify the USB mic is selected as the active output. | |
| 434 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | |
| 435 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode()); | |
| 436 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | |
| 437 | |
| 438 // Unplug the USB Mic. | |
| 439 audio_nodes.clear(); | |
| 440 audio_nodes.push_back(kInternalMic); | |
| 441 ChangeAudioNodes(audio_nodes); | |
| 442 | |
| 443 // Verify the AudioNodesChanged event is fired, and one audio device is | |
| 444 // removed. | |
| 445 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); | |
| 446 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
| 447 EXPECT_EQ(kNumStubAudioDevices - 1, audio_devices.size()); | |
| 448 | |
| 449 // Verify the active input device is switched to internal mic, and | |
| 450 // and ActiveInputChanged event is fired. | |
| 451 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | |
| 452 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 453 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | |
| 454 } | |
| 455 | |
| 456 TEST_F(CrasAudioHandlerTest, SetOutputMute) { | |
| 457 AudioNodeList audio_nodes; | |
| 458 audio_nodes.push_back(kInternalSpeaker); | |
| 459 SetUpCrasAudioHandler(audio_nodes); | |
| 460 EXPECT_EQ(0, test_observer_->output_mute_changed_count()); | |
| 461 | |
| 462 // Mute the device. | |
| 463 cras_audio_handler_->SetOutputMute(true); | |
| 464 | |
| 465 // Verify the output is muted, OnOutputMuteChanged event is fired, | |
| 466 // and mute value is saved in the preferences. | |
| 467 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); | |
| 468 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | |
| 469 AudioDevice speaker(kInternalSpeaker); | |
| 470 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker)); | |
| 471 | |
| 472 // Unmute the device. | |
| 473 cras_audio_handler_->SetOutputMute(false); | |
| 474 | |
| 475 // Verify the output is unmuted, OnOutputMuteChanged event is fired, | |
| 476 // and mute value is saved in the preferences. | |
| 477 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | |
| 478 EXPECT_EQ(2, test_observer_->output_mute_changed_count()); | |
| 479 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker)); | |
| 480 } | |
| 481 | |
| 482 TEST_F(CrasAudioHandlerTest, SetInputMute) { | |
| 483 AudioNodeList audio_nodes; | |
| 484 audio_nodes.push_back(kInternalMic); | |
| 485 SetUpCrasAudioHandler(audio_nodes); | |
| 486 EXPECT_EQ(0, test_observer_->input_mute_changed_count()); | |
| 487 | |
| 488 // Mute the device. | |
| 489 cras_audio_handler_->SetInputMute(true); | |
| 490 | |
| 491 // Verify the input is muted, OnInputMuteChanged event is fired, | |
| 492 // and mute value is saved in the preferences. | |
| 493 EXPECT_TRUE(cras_audio_handler_->IsInputMuted()); | |
| 494 EXPECT_EQ(1, test_observer_->input_mute_changed_count()); | |
| 495 AudioDevice internal_mic(kInternalMic); | |
| 496 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 497 | |
| 498 // Unmute the device. | |
| 499 cras_audio_handler_->SetInputMute(false); | |
| 500 | |
| 501 // Verify the input is unmuted, OnInputMuteChanged event is fired, | |
| 502 // and mute value is saved in the preferences. | |
| 503 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); | |
| 504 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); | |
| 505 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 506 } | |
| 507 | |
| 508 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { | |
| 509 AudioNodeList audio_nodes; | |
| 510 audio_nodes.push_back(kInternalSpeaker); | |
| 511 SetUpCrasAudioHandler(audio_nodes); | |
| 512 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 513 | |
| 514 cras_audio_handler_->SetOutputVolumePercent(60); | |
| 515 | |
| 516 // Verify the output volume is changed to the designated value, | |
| 517 // OnOutputVolumeChanged event is fired, and the device volume value | |
| 518 // is saved the preferences. | |
| 519 const int kVolume = 60; | |
| 520 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 521 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 522 AudioDevice device; | |
| 523 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device)); | |
| 524 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 525 EXPECT_EQ(kVolume, audio_pref_handler_->GetVolumeGainValue(device)); | |
| 526 } | |
| 527 | |
| 528 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) { | |
| 529 AudioNodeList audio_nodes; | |
| 530 audio_nodes.push_back(kInternalMic); | |
| 531 SetUpCrasAudioHandler(audio_nodes); | |
| 532 EXPECT_EQ(0, test_observer_->input_gain_changed_count()); | |
| 533 | |
| 534 cras_audio_handler_->SetInputGainPercent(60); | |
| 535 | |
| 536 // Verify the input gain changed to the designated value, | |
| 537 // OnInputGainChanged event is fired, and the device gain value | |
| 538 // is saved in the preferences. | |
| 539 const int kGain = 60; | |
| 540 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent()); | |
| 541 EXPECT_EQ(1, test_observer_->input_gain_changed_count()); | |
| 542 AudioDevice internal_mic(kInternalMic); | |
| 543 EXPECT_EQ(kGain, audio_pref_handler_->GetVolumeGainValue(internal_mic)); | |
| 544 } | |
| 545 | |
| 546 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) { | |
| 547 AudioNodeList audio_nodes; | |
| 548 audio_nodes.push_back(kInternalSpeaker); | |
| 549 audio_nodes.push_back(kHeadphone); | |
| 550 audio_nodes.push_back(kInternalMic); | |
| 551 audio_nodes.push_back(kUSBMic); | |
| 552 SetUpCrasAudioHandler(audio_nodes); | |
| 553 | |
| 554 // Mute the active output device. | |
| 555 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 556 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true); | |
| 557 | |
| 558 // Verify the headphone is muted and mute value is saved in the preferences. | |
| 559 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id)); | |
| 560 AudioDevice headphone(kHeadphone); | |
| 561 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone)); | |
| 562 | |
| 563 // Mute the non-active output device. | |
| 564 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true); | |
| 565 | |
| 566 // Verify the internal speaker is muted and mute value is saved in the | |
| 567 // preferences. | |
| 568 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id)); | |
| 569 AudioDevice internal_speaker(kInternalSpeaker); | |
| 570 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker)); | |
| 571 | |
| 572 // Mute the active input device. | |
| 573 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 574 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true); | |
| 575 | |
| 576 // Verify the USB Mic is muted and mute state is saved in the preferences. | |
| 577 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kUSBMic.id)); | |
| 578 AudioDevice usb_mic(kUSBMic); | |
| 579 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(usb_mic)); | |
| 580 | |
| 581 // Mute the non-active input device. | |
| 582 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true); | |
| 583 | |
| 584 // Verify the internal mic is muted and mute value is saved in the | |
| 585 // preferences. | |
| 586 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalMic.id)); | |
| 587 AudioDevice internal_mic(kInternalMic); | |
| 588 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic)); | |
| 589 } | |
| 590 | |
| 591 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) { | |
| 592 AudioNodeList audio_nodes; | |
| 593 audio_nodes.push_back(kInternalSpeaker); | |
| 594 audio_nodes.push_back(kHeadphone); | |
| 595 audio_nodes.push_back(kInternalMic); | |
| 596 audio_nodes.push_back(kUSBMic); | |
| 597 SetUpCrasAudioHandler(audio_nodes); | |
| 598 | |
| 599 // Set volume percent for active output device. | |
| 600 const int kHeadphoneVolume = 30; | |
| 601 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode()); | |
| 602 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id, | |
| 603 kHeadphoneVolume); | |
| 604 | |
| 605 // Verify the volume percent of headphone is set, and saved in preferences. | |
| 606 EXPECT_EQ(kHeadphoneVolume, | |
| 607 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 608 kHeadphone.id)); | |
| 609 AudioDevice headphone(kHeadphone); | |
| 610 EXPECT_EQ(kHeadphoneVolume, | |
| 611 audio_pref_handler_->GetVolumeGainValue(headphone)); | |
| 612 | |
| 613 // Set volume percent for non-active output device. | |
| 614 const int kSpeakerVolume = 60; | |
| 615 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id, | |
| 616 kSpeakerVolume); | |
| 617 | |
| 618 // Verify the volume percent of speaker is set, and saved in preferences. | |
| 619 EXPECT_EQ(kSpeakerVolume, | |
| 620 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 621 kInternalSpeaker.id)); | |
| 622 AudioDevice speaker(kInternalSpeaker); | |
| 623 EXPECT_EQ(kSpeakerVolume, | |
| 624 audio_pref_handler_->GetVolumeGainValue(speaker)); | |
| 625 | |
| 626 // Set gain percent for active input device. | |
| 627 const int kUSBMicGain = 30; | |
| 628 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode()); | |
| 629 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id, | |
| 630 kUSBMicGain); | |
| 631 | |
| 632 // Verify the gain percent of USB mic is set, and saved in preferences. | |
| 633 EXPECT_EQ(kUSBMicGain, | |
| 634 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id)); | |
| 635 AudioDevice usb_mic(kHeadphone); | |
| 636 EXPECT_EQ(kUSBMicGain, | |
| 637 audio_pref_handler_->GetVolumeGainValue(usb_mic)); | |
| 638 | |
| 639 // Set gain percent for non-active input device. | |
| 640 const int kInternalMicGain = 60; | |
| 641 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id, | |
| 642 kInternalMicGain); | |
| 643 | |
| 644 // Verify the gain percent of internal mic is set, and saved in preferences. | |
| 645 EXPECT_EQ(kInternalMicGain, | |
| 646 cras_audio_handler_->GetOutputVolumePercentForDevice( | |
| 647 kInternalMic.id)); | |
| 648 AudioDevice internal_mic(kInternalMic); | |
| 649 EXPECT_EQ(kInternalMicGain, | |
| 650 audio_pref_handler_->GetVolumeGainValue(internal_mic)); | |
| 651 } | |
| 652 | |
| 653 } // namespace chromeos | |
| OLD | NEW |