OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/audio/cras_audio_handler.h" | 5 #include "chromeos/audio/cras_audio_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chromeos/audio/audio_devices_pref_handler.h" | |
18 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 19 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
19 #include "chromeos/dbus/audio_node.h" | 20 #include "chromeos/dbus/audio_node.h" |
20 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
21 #include "chromeos/dbus/fake_cras_audio_client.h" | 22 #include "chromeos/dbus/fake_cras_audio_client.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 namespace chromeos { | 25 namespace chromeos { |
25 namespace { | 26 namespace { |
26 | 27 |
27 const uint64_t kInternalSpeakerId = 10001; | 28 const uint64_t kInternalSpeakerId = 10001; |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 DBusThreadManager::Get()->GetCrasAudioClient()); | 318 DBusThreadManager::Get()->GetCrasAudioClient()); |
318 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | 319 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
319 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); | 320 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
320 CrasAudioHandler::Initialize(audio_pref_handler_); | 321 CrasAudioHandler::Initialize(audio_pref_handler_); |
321 cras_audio_handler_ = CrasAudioHandler::Get(); | 322 cras_audio_handler_ = CrasAudioHandler::Get(); |
322 test_observer_.reset(new TestObserver); | 323 test_observer_.reset(new TestObserver); |
323 cras_audio_handler_->AddAudioObserver(test_observer_.get()); | 324 cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
324 message_loop_.RunUntilIdle(); | 325 message_loop_.RunUntilIdle(); |
325 } | 326 } |
326 | 327 |
328 // Set up cras audio handlers with |audio_nodes| and set the active state of | |
329 // |active_device_in_pref| as active and |activate_by_user| in the pref, | |
330 // and rest of nodes in |audio_nodes_in_pref| as inactive. | |
331 void SetupCrasAudioHandlerWithActiveNodeInPref( | |
332 const AudioNodeList& audio_nodes, | |
333 const AudioNodeList& audio_nodes_in_pref, | |
334 const AudioDevice& active_device_in_pref, | |
335 bool activate_by_user) { | |
336 DBusThreadManager::Initialize(); | |
337 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( | |
338 DBusThreadManager::Get()->GetCrasAudioClient()); | |
339 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); | |
340 bool active; | |
341 for (const AudioNode& node : audio_nodes_in_pref) { | |
342 active = node.id == active_device_in_pref.id; | |
343 audio_pref_handler_->SetDeviceActive(AudioDevice(node), active, | |
344 activate_by_user); | |
345 } | |
346 | |
347 bool activate_by; | |
348 EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref, | |
349 &active, &activate_by)); | |
350 EXPECT_TRUE(active); | |
351 EXPECT_EQ(activate_by, activate_by_user); | |
352 | |
353 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | |
354 CrasAudioHandler::Initialize(audio_pref_handler_); | |
355 | |
356 cras_audio_handler_ = CrasAudioHandler::Get(); | |
357 test_observer_.reset(new TestObserver); | |
358 cras_audio_handler_->AddAudioObserver(test_observer_.get()); | |
359 message_loop_.RunUntilIdle(); | |
360 } | |
361 | |
327 void SetUpCrasAudioHandlerWithPrimaryActiveNode( | 362 void SetUpCrasAudioHandlerWithPrimaryActiveNode( |
328 const AudioNodeList& audio_nodes, | 363 const AudioNodeList& audio_nodes, |
329 const AudioNode& primary_active_node) { | 364 const AudioNode& primary_active_node) { |
330 DBusThreadManager::Initialize(); | 365 DBusThreadManager::Initialize(); |
331 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( | 366 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( |
332 DBusThreadManager::Get()->GetCrasAudioClient()); | 367 DBusThreadManager::Get()->GetCrasAudioClient()); |
333 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | 368 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
334 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id), | 369 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id), |
335 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); | 370 audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
336 CrasAudioHandler::Initialize(audio_pref_handler_); | 371 CrasAudioHandler::Initialize(audio_pref_handler_); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 // Verify the initial active output device is headphone. | 587 // Verify the initial active output device is headphone. |
553 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | 588 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
554 AudioDevice active_output; | 589 AudioDevice active_output; |
555 EXPECT_TRUE( | 590 EXPECT_TRUE( |
556 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 591 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
557 EXPECT_EQ(kHeadphone.id, active_output.id); | 592 EXPECT_EQ(kHeadphone.id, active_output.id); |
558 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 593 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
559 | 594 |
560 // Switch the active output to internal speaker. | 595 // Switch the active output to internal speaker. |
561 AudioDevice internal_speaker(kInternalSpeaker); | 596 AudioDevice internal_speaker(kInternalSpeaker); |
562 cras_audio_handler_->SwitchToDevice(internal_speaker, true); | 597 cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
598 CrasAudioHandler::ACTIVATE_BY_USER); | |
563 | 599 |
564 // Verify the active output is switched to internal speaker, and the | 600 // Verify the active output is switched to internal speaker, and the |
565 // ActiveOutputNodeChanged event is fired. | 601 // ActiveOutputNodeChanged event is fired. |
566 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | 602 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
567 EXPECT_TRUE( | 603 EXPECT_TRUE( |
568 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 604 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
569 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 605 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
570 EXPECT_EQ(kInternalSpeaker.id, | 606 EXPECT_EQ(kInternalSpeaker.id, |
571 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 607 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
572 } | 608 } |
573 | 609 |
574 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) { | 610 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) { |
575 AudioNodeList audio_nodes; | 611 AudioNodeList audio_nodes; |
576 audio_nodes.push_back(kInternalMic); | 612 audio_nodes.push_back(kInternalMic); |
577 audio_nodes.push_back(kUSBMic); | 613 audio_nodes.push_back(kUSBMic); |
578 SetUpCrasAudioHandler(audio_nodes); | 614 SetUpCrasAudioHandler(audio_nodes); |
579 AudioDeviceList audio_devices; | 615 AudioDeviceList audio_devices; |
580 cras_audio_handler_->GetAudioDevices(&audio_devices); | 616 cras_audio_handler_->GetAudioDevices(&audio_devices); |
581 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | 617 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
582 | 618 |
583 // Verify the initial active input device is USB mic. | 619 // Verify the initial active input device is USB mic. |
584 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); | 620 EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
585 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); | 621 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
586 | 622 |
587 // Switch the active input to internal mic. | 623 // Switch the active input to internal mic. |
588 AudioDevice internal_mic(kInternalMic); | 624 AudioDevice internal_mic(kInternalMic); |
589 cras_audio_handler_->SwitchToDevice(internal_mic, true); | 625 cras_audio_handler_->SwitchToDevice(internal_mic, true, |
626 CrasAudioHandler::ACTIVATE_BY_USER); | |
590 | 627 |
591 // Verify the active output is switched to internal speaker, and the active | 628 // Verify the active output is switched to internal speaker, and the active |
592 // ActiveInputNodeChanged event is fired. | 629 // ActiveInputNodeChanged event is fired. |
593 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); | 630 EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
594 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); | 631 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
595 } | 632 } |
596 | 633 |
597 TEST_F(CrasAudioHandlerTest, PlugHeadphone) { | 634 TEST_F(CrasAudioHandlerTest, PlugHeadphone) { |
598 // Set up initial audio devices, only with internal speaker. | 635 // Set up initial audio devices, only with internal speaker. |
599 AudioNodeList audio_nodes; | 636 AudioNodeList audio_nodes; |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1133 // is plugged later, and ActiveOutputChanged event is fired. | 1170 // is plugged later, and ActiveOutputChanged event is fired. |
1134 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | 1171 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
1135 EXPECT_TRUE( | 1172 EXPECT_TRUE( |
1136 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 1173 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
1137 EXPECT_EQ(kHeadphone.id, active_output.id); | 1174 EXPECT_EQ(kHeadphone.id, active_output.id); |
1138 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 1175 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
1139 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | 1176 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
1140 | 1177 |
1141 // Select the speaker to be the active output device. | 1178 // Select the speaker to be the active output device. |
1142 AudioDevice internal_speaker(kInternalSpeaker); | 1179 AudioDevice internal_speaker(kInternalSpeaker); |
1143 cras_audio_handler_->SwitchToDevice(internal_speaker, true); | 1180 cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
1181 CrasAudioHandler::ACTIVATE_BY_USER); | |
1144 | 1182 |
1145 // Verify the active output is switched to internal speaker, and the | 1183 // Verify the active output is switched to internal speaker, and the |
1146 // ActiveOutputNodeChanged event is fired. | 1184 // ActiveOutputNodeChanged event is fired. |
1147 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); | 1185 EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
1148 EXPECT_TRUE( | 1186 EXPECT_TRUE( |
1149 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 1187 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
1150 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 1188 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
1151 EXPECT_EQ(kInternalSpeaker.id, | 1189 EXPECT_EQ(kInternalSpeaker.id, |
1152 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 1190 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
1153 | 1191 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1364 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); | 1402 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
1365 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); | 1403 EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
1366 | 1404 |
1367 // Verify the headphone is selected as the active output. | 1405 // Verify the headphone is selected as the active output. |
1368 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); | 1406 EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
1369 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 1407 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
1370 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | 1408 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
1371 | 1409 |
1372 // Switch the active output to internal speaker. | 1410 // Switch the active output to internal speaker. |
1373 AudioDevice internal_speaker(kInternalSpeaker); | 1411 AudioDevice internal_speaker(kInternalSpeaker); |
1374 cras_audio_handler_->SwitchToDevice(internal_speaker, true); | 1412 cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
1413 CrasAudioHandler::ACTIVATE_BY_USER); | |
1375 | 1414 |
1376 // Verify the active output is switched to internal speaker, and the | 1415 // Verify the active output is switched to internal speaker, and the |
1377 // ActiveOutputNodeChanged event is fired. | 1416 // ActiveOutputNodeChanged event is fired. |
1378 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); | 1417 EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
1379 AudioDevice active_output; | 1418 AudioDevice active_output; |
1380 EXPECT_TRUE( | 1419 EXPECT_TRUE( |
1381 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 1420 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
1382 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 1421 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
1383 EXPECT_EQ(kInternalSpeaker.id, | 1422 EXPECT_EQ(kInternalSpeaker.id, |
1384 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 1423 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2059 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | 2098 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
2060 | 2099 |
2061 // Initially active node is selected base on priority, so USB headphone | 2100 // Initially active node is selected base on priority, so USB headphone |
2062 // is selected. | 2101 // is selected. |
2063 EXPECT_EQ(kUSBHeadphone1.id, | 2102 EXPECT_EQ(kUSBHeadphone1.id, |
2064 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2103 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2065 | 2104 |
2066 // Change the active device to internal speaker, now USB headphone becomes | 2105 // Change the active device to internal speaker, now USB headphone becomes |
2067 // inactive. | 2106 // inactive. |
2068 AudioDevice speaker(kInternalSpeaker); | 2107 AudioDevice speaker(kInternalSpeaker); |
2069 cras_audio_handler_->SwitchToDevice(speaker, true); | 2108 cras_audio_handler_->SwitchToDevice(speaker, true, |
2109 CrasAudioHandler::ACTIVATE_BY_USER); | |
2070 EXPECT_NE(kUSBHeadphone1.id, | 2110 EXPECT_NE(kUSBHeadphone1.id, |
2071 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2111 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2072 | 2112 |
2073 // Unplug USB headset. | 2113 // Unplug USB headset. |
2074 audio_nodes.clear(); | 2114 audio_nodes.clear(); |
2075 internal_speaker.active = true; | 2115 internal_speaker.active = true; |
2076 audio_nodes.push_back(internal_speaker); | 2116 audio_nodes.push_back(internal_speaker); |
2077 ChangeAudioNodes(audio_nodes); | 2117 ChangeAudioNodes(audio_nodes); |
2078 EXPECT_EQ(kInternalSpeaker.id, | 2118 EXPECT_EQ(kInternalSpeaker.id, |
2079 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2119 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
(...skipping 25 matching lines...) Expand all Loading... | |
2105 audio_nodes.clear(); | 2145 audio_nodes.clear(); |
2106 internal_speaker.active = false; | 2146 internal_speaker.active = false; |
2107 audio_nodes.push_back(internal_speaker); | 2147 audio_nodes.push_back(internal_speaker); |
2108 audio_nodes.push_back(usb_headset); | 2148 audio_nodes.push_back(usb_headset); |
2109 ChangeAudioNodes(audio_nodes); | 2149 ChangeAudioNodes(audio_nodes); |
2110 | 2150 |
2111 // There is no active node after USB2 unplugged, the 1st USB got selected | 2151 // There is no active node after USB2 unplugged, the 1st USB got selected |
2112 // by its priority. | 2152 // by its priority. |
2113 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2153 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2114 | 2154 |
2155 audio_nodes.clear(); | |
2156 internal_speaker.active = false; | |
2157 audio_nodes.push_back(internal_speaker); | |
2158 usb_headset.active = true; | |
2159 audio_nodes.push_back(usb_headset); | |
2115 usb_headset2.active = false; | 2160 usb_headset2.active = false; |
2116 usb_headset2.plugged_time = 80000002; | 2161 usb_headset2.plugged_time = 80000002; |
2117 audio_nodes.push_back(usb_headset2); | 2162 audio_nodes.push_back(usb_headset2); |
2118 ChangeAudioNodes(audio_nodes); | 2163 ChangeAudioNodes(audio_nodes); |
2119 | 2164 |
2120 // Plug the second USB again. Since it was the active node before it got | 2165 // Plug the second USB again. Since it was the active node before it got |
2121 // unplugged, it is now selected as the active node. | 2166 // unplugged, it is now selected as the active node. |
2122 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2167 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2123 } | 2168 } |
2124 | 2169 |
2170 // Test the device new session case, either via reboot or logout, if there | |
2171 // is an active device in the previous session, that device should still | |
2172 // be set as active after the new session starts. | |
2173 TEST_F(CrasAudioHandlerTest, PersistActiveDeviceAcrossSession) { | |
2174 // Set the active device to internal speaker before the session starts. | |
2175 AudioNodeList audio_nodes; | |
2176 audio_nodes.push_back(kInternalSpeaker); | |
2177 audio_nodes.push_back(kHeadphone); | |
2178 SetupCrasAudioHandlerWithActiveNodeInPref( | |
2179 audio_nodes, audio_nodes, AudioDevice(kInternalSpeaker), true); | |
2180 | |
2181 // Verify the audio devices size. | |
2182 AudioDeviceList audio_devices; | |
2183 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2184 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2185 | |
2186 // Verify the active device is the internal speaker, which is of a lower | |
2187 // priority, but selected as active since it was the active device previously. | |
2188 EXPECT_EQ(kInternalSpeaker.id, | |
2189 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2190 } | |
2191 | |
2192 TEST_F(CrasAudioHandlerTest, PersistActiveSpeakerAcrossReboot) { | |
2193 // Simulates the device was shut down with three audio devices, and | |
2194 // internal speaker being the active one selected by user. | |
2195 AudioNodeList audio_nodes_in_pref; | |
2196 audio_nodes_in_pref.push_back(kInternalSpeaker); | |
2197 audio_nodes_in_pref.push_back(kHeadphone); | |
2198 audio_nodes_in_pref.push_back(kUSBHeadphone1); | |
2199 | |
2200 // Simulate the first NodesChanged signal coming with only one node. | |
2201 AudioNodeList audio_nodes; | |
2202 audio_nodes.push_back(kUSBHeadphone1); | |
2203 | |
2204 SetupCrasAudioHandlerWithActiveNodeInPref( | |
2205 audio_nodes, audio_nodes_in_pref, AudioDevice(kInternalSpeaker), true); | |
2206 | |
2207 // Verify the usb headphone has been made active. | |
2208 AudioDeviceList audio_devices; | |
2209 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2210 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2211 EXPECT_EQ(kUSBHeadphone1.id, | |
2212 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2213 | |
2214 // Simulate another NodesChanged signal coming later with all ndoes. | |
2215 audio_nodes.push_back(kInternalSpeaker); | |
2216 audio_nodes.push_back(kHeadphone); | |
2217 ChangeAudioNodes(audio_nodes); | |
2218 | |
2219 // Verify the active output has been restored to internal speaker. | |
2220 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2221 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2222 EXPECT_EQ(kInternalSpeaker.id, | |
2223 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2224 } | |
2225 | |
2226 TEST_F(CrasAudioHandlerTest, | |
2227 PersistActiveUsbHeadphoneAcrossRebootUsbComeLater) { | |
2228 // Simulates the device was shut down with three audio devices, and | |
2229 // usb headphone being the active one selected by priority. | |
2230 AudioNodeList audio_nodes_in_pref; | |
2231 audio_nodes_in_pref.push_back(kInternalSpeaker); | |
2232 audio_nodes_in_pref.push_back(kHeadphone); | |
2233 audio_nodes_in_pref.push_back(kUSBHeadphone1); | |
2234 | |
2235 // Simulate the first NodesChanged signal coming with only internal speaker | |
2236 // and the headphone. | |
2237 AudioNodeList audio_nodes; | |
2238 audio_nodes.push_back(kInternalSpeaker); | |
2239 audio_nodes.push_back(kHeadphone); | |
2240 | |
2241 SetupCrasAudioHandlerWithActiveNodeInPref(audio_nodes, audio_nodes_in_pref, | |
2242 AudioDevice(kUSBHeadphone1), false); | |
2243 | |
2244 // Verify the headphone has been made active. | |
2245 AudioDeviceList audio_devices; | |
2246 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2247 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2248 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2249 | |
2250 // Simulate USB node comes later with all ndoes. | |
2251 AudioNode usb_node(kUSBHeadphone1); | |
2252 usb_node.plugged_time = 80000000; | |
2253 audio_nodes.push_back(usb_node); | |
2254 ChangeAudioNodes(audio_nodes); | |
2255 | |
2256 // Verify the active output has been restored to usb headphone. | |
2257 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2258 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2259 EXPECT_EQ(kUSBHeadphone1.id, | |
2260 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2261 } | |
2262 | |
2263 TEST_F(CrasAudioHandlerTest, | |
2264 PersistActiveUsbHeadphoneAcrossRebootUsbComeFirst) { | |
2265 // Simulates the device was shut down with three audio devices, and | |
2266 // usb headphone being the active one selected by priority. | |
2267 AudioNodeList audio_nodes_in_pref; | |
2268 audio_nodes_in_pref.push_back(kInternalSpeaker); | |
2269 audio_nodes_in_pref.push_back(kHeadphone); | |
2270 audio_nodes_in_pref.push_back(kUSBHeadphone1); | |
2271 | |
2272 // Simulate the first NodesChanged signal coming with only internal speaker | |
2273 // and the USB headphone. | |
2274 AudioNodeList audio_nodes; | |
2275 audio_nodes.push_back(kInternalSpeaker); | |
2276 audio_nodes.push_back(kUSBHeadphone1); | |
2277 | |
2278 SetupCrasAudioHandlerWithActiveNodeInPref(audio_nodes, audio_nodes_in_pref, | |
2279 AudioDevice(kUSBHeadphone1), false); | |
2280 | |
2281 // Verify the USB headphone has been made active. | |
2282 AudioDeviceList audio_devices; | |
2283 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2284 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2285 EXPECT_EQ(kUSBHeadphone1.id, | |
2286 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2287 | |
2288 // Simulate another NodesChanged signal coming later with all ndoes. | |
2289 AudioNode headphone_node(kHeadphone); | |
2290 headphone_node.plugged_time = 80000000; | |
2291 audio_nodes.push_back(headphone_node); | |
2292 ChangeAudioNodes(audio_nodes); | |
2293 | |
2294 // Verify the active output has been restored to USB headphone. | |
2295 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2296 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2297 EXPECT_EQ(kUSBHeadphone1.id, | |
2298 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2299 } | |
2300 | |
2301 // This covers the crbug.com/586026. Cras lost the active state of the internal | |
2302 // speaker when user unplugs the headphone, which is a bug in cras. However, | |
2303 // chrome code is still resilient and set the internal speaker back to active. | |
2304 TEST_F(CrasAudioHandlerTest, UnplugHeadphoneLostActiveInternalSpeakerByCras) { | |
2305 // Set up with three nodes. | |
2306 AudioNodeList audio_nodes; | |
2307 audio_nodes.push_back(kInternalSpeaker); | |
2308 audio_nodes.push_back(kHeadphone); | |
2309 audio_nodes.push_back(kUSBHeadphone1); | |
2310 SetUpCrasAudioHandler(audio_nodes); | |
2311 | |
2312 // Switch the active output to internal speaker. | |
2313 cras_audio_handler_->SwitchToDevice(AudioDevice(kInternalSpeaker), true, | |
2314 CrasAudioHandler::ACTIVATE_BY_PRIORITY); | |
hychao
2016/03/02 15:34:05
When there are headphone and USB-headphone plugged
jennyz
2016/03/02 19:33:37
Good catch. Fixed.
| |
2315 | |
2316 // Verify internal speaker has been made active. | |
2317 AudioDeviceList audio_devices; | |
2318 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2319 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2320 EXPECT_EQ(kInternalSpeaker.id, | |
2321 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2322 | |
2323 // Simulate unplug the headphone. Cras sends NodesChanged signal with | |
2324 // both internal speaker and usb headphone being inactive. | |
2325 audio_nodes.clear(); | |
2326 audio_nodes.push_back(kInternalSpeaker); | |
2327 audio_nodes.push_back(kUSBHeadphone1); | |
2328 EXPECT_FALSE(kInternalSpeaker.active); | |
2329 EXPECT_FALSE(kUSBHeadphone1.active); | |
2330 ChangeAudioNodes(audio_nodes); | |
2331 | |
2332 // Verify the active output is set back to internal speaker. | |
2333 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2334 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2335 EXPECT_EQ(kInternalSpeaker.id, | |
2336 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2337 } | |
2338 | |
2339 TEST_F(CrasAudioHandlerTest, RemoveNonActiveDevice) { | |
2340 // Set up with three nodes. | |
2341 AudioNodeList audio_nodes; | |
2342 audio_nodes.push_back(kInternalSpeaker); | |
2343 audio_nodes.push_back(kHeadphone); | |
2344 audio_nodes.push_back(kUSBHeadphone1); | |
2345 SetUpCrasAudioHandler(audio_nodes); | |
2346 | |
2347 // Switch the active output to internal speaker. | |
2348 cras_audio_handler_->SwitchToDevice(AudioDevice(kInternalSpeaker), true, | |
2349 CrasAudioHandler::ACTIVATE_BY_PRIORITY); | |
hychao
2016/03/02 15:34:05
same question as above
jennyz
2016/03/02 19:33:37
Fixed.
| |
2350 | |
2351 // Verify internal speaker has been made active. | |
2352 AudioDeviceList audio_devices; | |
2353 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2354 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2355 EXPECT_EQ(kInternalSpeaker.id, | |
2356 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2357 | |
2358 // Remove headphone, which is an non-active device. | |
2359 audio_nodes.clear(); | |
2360 AudioNode speaker(kInternalSpeaker); | |
2361 speaker.active = true; | |
2362 audio_nodes.push_back(speaker); | |
2363 AudioNode usb_headphone(kUSBHeadphone1); | |
2364 usb_headphone.active = false; | |
2365 audio_nodes.push_back(usb_headphone); | |
2366 | |
2367 ChangeAudioNodes(audio_nodes); | |
2368 | |
2369 // Verify the active output remains as internal speaker. | |
2370 cras_audio_handler_->GetAudioDevices(&audio_devices); | |
2371 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | |
2372 EXPECT_EQ(kInternalSpeaker.id, | |
2373 cras_audio_handler_->GetPrimaryActiveOutputNode()); | |
2374 } | |
2375 | |
2125 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { | 2376 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { |
2126 AudioNodeList audio_nodes; | 2377 AudioNodeList audio_nodes; |
2127 audio_nodes.push_back(kHDMIOutput); | 2378 audio_nodes.push_back(kHDMIOutput); |
2128 audio_nodes.push_back(kUSBJabraSpeakerOutput1); | 2379 audio_nodes.push_back(kUSBJabraSpeakerOutput1); |
2129 audio_nodes.push_back(kUSBJabraSpeakerOutput2); | 2380 audio_nodes.push_back(kUSBJabraSpeakerOutput2); |
2130 audio_nodes.push_back(kUSBJabraSpeakerInput1); | 2381 audio_nodes.push_back(kUSBJabraSpeakerInput1); |
2131 audio_nodes.push_back(kUSBJabraSpeakerInput2); | 2382 audio_nodes.push_back(kUSBJabraSpeakerInput2); |
2132 audio_nodes.push_back(kUSBCameraInput); | 2383 audio_nodes.push_back(kUSBCameraInput); |
2133 SetUpCrasAudioHandler(audio_nodes); | 2384 SetUpCrasAudioHandler(audio_nodes); |
2134 | 2385 |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2545 // Verify the audio devices size. | 2796 // Verify the audio devices size. |
2546 AudioDeviceList audio_devices; | 2797 AudioDeviceList audio_devices; |
2547 cras_audio_handler_->GetAudioDevices(&audio_devices); | 2798 cras_audio_handler_->GetAudioDevices(&audio_devices); |
2548 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | 2799 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
2549 | 2800 |
2550 // Verify the USB headset is selected as active output by default. | 2801 // Verify the USB headset is selected as active output by default. |
2551 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2802 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2552 | 2803 |
2553 // Manually set the active output to internal speaker. | 2804 // Manually set the active output to internal speaker. |
2554 AudioDevice internal_output(kInternalSpeaker); | 2805 AudioDevice internal_output(kInternalSpeaker); |
2555 cras_audio_handler_->SwitchToDevice(internal_output, true); | 2806 cras_audio_handler_->SwitchToDevice(internal_output, true, |
2807 CrasAudioHandler::ACTIVATE_BY_USER); | |
2556 | 2808 |
2557 // Verify the active output is switched to internal speaker. | 2809 // Verify the active output is switched to internal speaker. |
2558 EXPECT_EQ(internal_speaker.id, | 2810 EXPECT_EQ(internal_speaker.id, |
2559 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2811 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2560 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time); | 2812 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time); |
2561 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id); | 2813 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id); |
2562 EXPECT_FALSE(usb_device->active); | 2814 EXPECT_FALSE(usb_device->active); |
2563 | 2815 |
2564 // Plug in HDMI output. | 2816 // Plug in HDMI output. |
2565 audio_nodes.clear(); | 2817 audio_nodes.clear(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2624 SetUpCrasAudioHandler(audio_nodes); | 2876 SetUpCrasAudioHandler(audio_nodes); |
2625 | 2877 |
2626 // Verify the hdmi is selected as the active output since it has a higher | 2878 // Verify the hdmi is selected as the active output since it has a higher |
2627 // priority. | 2879 // priority. |
2628 AudioDeviceList audio_devices; | 2880 AudioDeviceList audio_devices; |
2629 cras_audio_handler_->GetAudioDevices(&audio_devices); | 2881 cras_audio_handler_->GetAudioDevices(&audio_devices); |
2630 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | 2882 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
2631 EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2883 EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2632 | 2884 |
2633 // Manually set the active output to internal speaker. | 2885 // Manually set the active output to internal speaker. |
2634 cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true); | 2886 cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true, |
2887 CrasAudioHandler::ACTIVATE_BY_USER); | |
2635 EXPECT_EQ(internal_speaker.id, | 2888 EXPECT_EQ(internal_speaker.id, |
2636 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2889 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2637 | 2890 |
2638 // Simulate the suspend and resume of the device during mirror mode. The HDMI | 2891 // Simulate the suspend and resume of the device during mirror mode. The HDMI |
2639 // node will be lost first. | 2892 // node will be lost first. |
2640 audio_nodes.clear(); | 2893 audio_nodes.clear(); |
2641 internal_speaker.active = true; | 2894 internal_speaker.active = true; |
2642 audio_nodes.push_back(internal_speaker); | 2895 audio_nodes.push_back(internal_speaker); |
2643 ChangeAudioNodes(audio_nodes); | 2896 ChangeAudioNodes(audio_nodes); |
2644 | 2897 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2792 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 3045 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
2793 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 3046 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
2794 EXPECT_EQ(kInternalSpeaker.id, | 3047 EXPECT_EQ(kInternalSpeaker.id, |
2795 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 3048 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2796 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | 3049 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
2797 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | 3050 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
2798 EXPECT_TRUE(test_observer_->output_mute_by_system()); | 3051 EXPECT_TRUE(test_observer_->output_mute_by_system()); |
2799 } | 3052 } |
2800 | 3053 |
2801 } // namespace chromeos | 3054 } // namespace chromeos |
OLD | NEW |