[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 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 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 12 | #include "base/bind.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 15 | #include "base/run_loop.h" |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 16 | #include "base/system_monitor/system_monitor.h" |
fdoray | 822e6b2 | 2017-04-27 20:47:03 | [diff] [blame] | 17 | #include "base/test/scoped_task_environment.h" |
gab | d85cc46b | 2016-05-11 18:23:20 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 19 | #include "base/values.h" |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 20 | #include "chromeos/audio/audio_devices_pref_handler.h" |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 21 | #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
| 22 | #include "chromeos/dbus/audio_node.h" |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 23 | #include "chromeos/dbus/dbus_thread_manager.h" |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 24 | #include "chromeos/dbus/fake_cras_audio_client.h" |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 25 | #include "media/base/video_facing.h" |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
| 28 | namespace chromeos { |
oshima | 2aaee55 | 2015-07-09 16:50:26 | [diff] [blame] | 29 | namespace { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 30 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 31 | const uint64_t kInternalSpeakerId = 10001; |
| 32 | const uint64_t kHeadphoneId = 10002; |
| 33 | const uint64_t kInternalMicId = 10003; |
| 34 | const uint64_t kUSBMicId = 10004; |
| 35 | const uint64_t kBluetoothHeadsetId = 10005; |
| 36 | const uint64_t kHDMIOutputId = 10006; |
| 37 | const uint64_t kUSBHeadphoneId1 = 10007; |
| 38 | const uint64_t kUSBHeadphoneId2 = 10008; |
| 39 | const uint64_t kMicJackId = 10009; |
| 40 | const uint64_t kKeyboardMicId = 10010; |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 41 | const uint64_t kFrontMicId = 10011; |
| 42 | const uint64_t kRearMicId = 10012; |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 43 | const uint64_t kOtherTypeOutputId = 90001; |
| 44 | const uint64_t kOtherTypeInputId = 90002; |
| 45 | const uint64_t kUSBJabraSpeakerOutputId1 = 90003; |
| 46 | const uint64_t kUSBJabraSpeakerOutputId2 = 90004; |
| 47 | const uint64_t kUSBJabraSpeakerInputId1 = 90005; |
| 48 | const uint64_t kUSBJabraSpeakerInputId2 = 90006; |
| 49 | const uint64_t kUSBCameraInputId = 90007; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 50 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 51 | struct AudioNodeInfo { |
| 52 | bool is_input; |
| 53 | uint64_t id; |
| 54 | const char* const device_name; |
| 55 | const char* const type; |
| 56 | const char* const name; |
| 57 | }; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 58 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 59 | const AudioNodeInfo kInternalSpeaker[] = { |
| 60 | {false, kInternalSpeakerId, "Fake Speaker", "INTERNAL_SPEAKER", "Speaker"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 61 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 62 | const AudioNodeInfo kHeadphone[] = { |
| 63 | {false, kHeadphoneId, "Fake Headphone", "HEADPHONE", "Headphone"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 64 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 65 | const AudioNodeInfo kInternalMic[] = { |
| 66 | {true, kInternalMicId, "Fake Mic", "INTERNAL_MIC", "Internal Mic"}}; |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 67 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 68 | const AudioNodeInfo kMicJack[] = { |
| 69 | {true, kMicJackId, "Fake Mic Jack", "MIC", "Mic Jack"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 70 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 71 | const AudioNodeInfo kUSBMic[] = { |
| 72 | {true, kUSBMicId, "Fake USB Mic", "USB", "USB Microphone"}}; |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 73 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 74 | const AudioNodeInfo kKeyboardMic[] = {{true, kKeyboardMicId, |
| 75 | "Fake Keyboard Mic", "KEYBOARD_MIC", |
| 76 | "Keyboard Mic"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 77 | |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 78 | const AudioNodeInfo kFrontMic[] = { |
| 79 | {true, kFrontMicId, "Fake Front Mic", "FRONT_MIC", "Front Mic"}}; |
| 80 | |
| 81 | const AudioNodeInfo kRearMic[] = { |
| 82 | {true, kRearMicId, "Fake Rear Mic", "REAR_MIC", "Rear Mic"}}; |
| 83 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 84 | const AudioNodeInfo kOtherTypeOutput[] = {{false, kOtherTypeOutputId, |
| 85 | "Output Device", "SOME_OTHER_TYPE", |
| 86 | "Other Type Output Device"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 87 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 88 | const AudioNodeInfo kOtherTypeInput[] = {{true, kOtherTypeInputId, |
| 89 | "Input Device", "SOME_OTHER_TYPE", |
| 90 | "Other Type Input Device"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 91 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 92 | const AudioNodeInfo kBluetoothHeadset[] = {{false, kBluetoothHeadsetId, |
| 93 | "Bluetooth Headset", "BLUETOOTH", |
| 94 | "Bluetooth Headset 1"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 95 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 96 | const AudioNodeInfo kHDMIOutput[] = { |
| 97 | {false, kHDMIOutputId, "HDMI output", "HDMI", "HDMI output"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 98 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 99 | const AudioNodeInfo kUSBHeadphone1[] = { |
| 100 | {false, kUSBHeadphoneId1, "USB Headphone", "USB", "USB Headphone 1"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 101 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 102 | const AudioNodeInfo kUSBHeadphone2[] = { |
| 103 | {false, kUSBHeadphoneId2, "USB Headphone", "USB", "USB Headphone 1"}}; |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 104 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 105 | const AudioNodeInfo kUSBJabraSpeakerOutput1[] = { |
| 106 | {false, kUSBJabraSpeakerOutputId1, "Jabra Speaker 1", "USB", |
| 107 | "Jabra Speaker 1"}}; |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 108 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 109 | const AudioNodeInfo kUSBJabraSpeakerOutput2[] = { |
| 110 | {false, kUSBJabraSpeakerOutputId2, "Jabra Speaker 2", "USB", |
| 111 | "Jabra Speaker 2"}}; |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 112 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 113 | const AudioNodeInfo kUSBJabraSpeakerInput1[] = {{true, kUSBJabraSpeakerInputId1, |
| 114 | "Jabra Speaker 1", "USB", |
| 115 | "Jabra Speaker"}}; |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 116 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 117 | const AudioNodeInfo kUSBJabraSpeakerInput2[] = {{true, kUSBJabraSpeakerInputId2, |
| 118 | "Jabra Speaker 2", "USB", |
| 119 | "Jabra Speaker 2"}}; |
| 120 | |
| 121 | const AudioNodeInfo kUSBCameraInput[] = { |
| 122 | {true, kUSBCameraInputId, "USB Camera", "USB", "USB Camera"}}; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 123 | |
| 124 | class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { |
| 125 | public: |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 126 | TestObserver() {} |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 127 | |
| 128 | int active_output_node_changed_count() const { |
| 129 | return active_output_node_changed_count_; |
| 130 | } |
| 131 | |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 132 | void reset_active_output_node_changed_count() { |
| 133 | active_output_node_changed_count_ = 0; |
| 134 | } |
| 135 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 136 | int active_input_node_changed_count() const { |
| 137 | return active_input_node_changed_count_; |
| 138 | } |
| 139 | |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 140 | void reset_active_input_node_changed_count() { |
mukai | 307596b6 | 2014-11-24 19:56:59 | [diff] [blame] | 141 | active_input_node_changed_count_ = 0; |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 144 | int audio_nodes_changed_count() const { |
| 145 | return audio_nodes_changed_count_; |
| 146 | } |
| 147 | |
| 148 | int output_mute_changed_count() const { |
| 149 | return output_mute_changed_count_; |
| 150 | } |
| 151 | |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 152 | void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; } |
| 153 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 154 | int input_mute_changed_count() const { |
| 155 | return input_mute_changed_count_; |
| 156 | } |
| 157 | |
| 158 | int output_volume_changed_count() const { |
| 159 | return output_volume_changed_count_; |
| 160 | } |
| 161 | |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 162 | void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; } |
| 163 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 164 | int input_gain_changed_count() const { |
| 165 | return input_gain_changed_count_; |
| 166 | } |
| 167 | |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 168 | bool output_mute_by_system() const { return output_mute_by_system_; } |
| 169 | |
warx | 7452462 | 2016-03-31 01:07:21 | [diff] [blame] | 170 | int output_channel_remixing_changed_count() const { |
| 171 | return output_channel_remixing_changed_count_; |
| 172 | } |
| 173 | |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 174 | ~TestObserver() override {} |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 175 | |
| 176 | protected: |
| 177 | // chromeos::CrasAudioHandler::AudioObserver overrides. |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 178 | void OnActiveOutputNodeChanged() override { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 179 | ++active_output_node_changed_count_; |
| 180 | } |
| 181 | |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 182 | void OnActiveInputNodeChanged() override { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 183 | ++active_input_node_changed_count_; |
| 184 | } |
| 185 | |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 186 | void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 187 | |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 188 | void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override { |
jennyz | 170e7fb | 2015-03-30 23:37:39 | [diff] [blame] | 189 | ++output_mute_changed_count_; |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 190 | output_mute_by_system_ = system_adjust; |
jennyz | 170e7fb | 2015-03-30 23:37:39 | [diff] [blame] | 191 | } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 192 | |
jennyz | 170e7fb | 2015-03-30 23:37:39 | [diff] [blame] | 193 | void OnInputMuteChanged(bool /* mute_on */) override { |
| 194 | ++input_mute_changed_count_; |
| 195 | } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 196 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 197 | void OnOutputNodeVolumeChanged(uint64_t /* node_id */, |
jennyz | 481292c | 2015-03-21 01:49:29 | [diff] [blame] | 198 | int /* volume */) override { |
| 199 | ++output_volume_changed_count_; |
| 200 | } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 201 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 202 | void OnInputNodeGainChanged(uint64_t /* node_id */, int /* gain */) override { |
jennyz | 481292c | 2015-03-21 01:49:29 | [diff] [blame] | 203 | ++input_gain_changed_count_; |
| 204 | } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 205 | |
warx | 7452462 | 2016-03-31 01:07:21 | [diff] [blame] | 206 | void OnOuputChannelRemixingChanged(bool /* mono_on */) override { |
| 207 | ++output_channel_remixing_changed_count_; |
| 208 | } |
| 209 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 210 | private: |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 211 | int active_output_node_changed_count_ = 0; |
| 212 | int active_input_node_changed_count_ = 0; |
| 213 | int audio_nodes_changed_count_ = 0; |
| 214 | int output_mute_changed_count_ = 0; |
| 215 | int input_mute_changed_count_ = 0; |
| 216 | int output_volume_changed_count_ = 0; |
| 217 | int input_gain_changed_count_ = 0; |
| 218 | bool output_mute_by_system_ = false; // output mute state adjusted by system. |
| 219 | int output_channel_remixing_changed_count_ = 0; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 220 | |
| 221 | DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 222 | }; |
| 223 | |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 224 | class SystemMonitorObserver |
| 225 | : public base::SystemMonitor::DevicesChangedObserver { |
| 226 | public: |
| 227 | SystemMonitorObserver() : device_changes_received_(0) {} |
| 228 | ~SystemMonitorObserver() override {} |
| 229 | |
| 230 | void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override { |
| 231 | if (device_type == base::SystemMonitor::DeviceType::DEVTYPE_AUDIO) |
| 232 | device_changes_received_++; |
| 233 | } |
| 234 | |
| 235 | int device_changes_received() const { return device_changes_received_; } |
| 236 | |
| 237 | void reset_count() { device_changes_received_ = 0; } |
| 238 | |
| 239 | private: |
| 240 | int device_changes_received_; |
| 241 | }; |
| 242 | |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 243 | class FakeVideoCaptureManager { |
| 244 | public: |
| 245 | FakeVideoCaptureManager() {} |
| 246 | virtual ~FakeVideoCaptureManager() {} |
| 247 | |
| 248 | void AddObserver(media::VideoCaptureObserver* observer) { |
| 249 | observers_.AddObserver(observer); |
| 250 | } |
| 251 | |
| 252 | void RemoveAllObservers() { observers_.Clear(); } |
| 253 | |
| 254 | void NotifyVideoCaptureStarted(media::VideoFacingMode facing) { |
| 255 | for (auto& observer : observers_) |
| 256 | observer.OnVideoCaptureStarted(facing); |
| 257 | } |
| 258 | |
| 259 | void NotifyVideoCaptureStopped(media::VideoFacingMode facing) { |
| 260 | for (auto& observer : observers_) |
| 261 | observer.OnVideoCaptureStopped(facing); |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | base::ObserverList<media::VideoCaptureObserver> observers_; |
| 266 | DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureManager); |
| 267 | }; |
| 268 | |
oshima | 2aaee55 | 2015-07-09 16:50:26 | [diff] [blame] | 269 | } // namespace |
| 270 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 271 | // Test param is the version of stabel device id used by audio node. |
| 272 | class CrasAudioHandlerTest : public testing::TestWithParam<int> { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 273 | public: |
fdoray | 822e6b2 | 2017-04-27 20:47:03 | [diff] [blame] | 274 | CrasAudioHandlerTest() |
| 275 | : scoped_task_environment_( |
| 276 | base::test::ScopedTaskEnvironment::MainThreadType::UI) {} |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 277 | ~CrasAudioHandlerTest() override {} |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 278 | |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 279 | void SetUp() override { |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 280 | system_monitor_.AddDevicesChangedObserver(&system_monitor_observer_); |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 281 | video_capture_manager_.reset(new FakeVideoCaptureManager); |
| 282 | } |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 283 | |
dcheng | ae98daa | 2015-01-21 20:30:49 | [diff] [blame] | 284 | void TearDown() override { |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 285 | system_monitor_.RemoveDevicesChangedObserver(&system_monitor_observer_); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 286 | cras_audio_handler_->RemoveAudioObserver(test_observer_.get()); |
| 287 | test_observer_.reset(); |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 288 | video_capture_manager_->RemoveAllObservers(); |
| 289 | video_capture_manager_.reset(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 290 | CrasAudioHandler::Shutdown(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 291 | audio_pref_handler_ = nullptr; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 292 | DBusThreadManager::Shutdown(); |
| 293 | } |
| 294 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 295 | AudioNode GenerateAudioNode(const AudioNodeInfo* node_info) { |
| 296 | uint64_t stable_device_id_v2 = GetParam() == 1 ? 0 : (node_info->id ^ 0xFF); |
| 297 | uint64_t stable_device_id_v1 = node_info->id; |
| 298 | return AudioNode(node_info->is_input, node_info->id, GetParam() == 2, |
| 299 | stable_device_id_v1, stable_device_id_v2, |
| 300 | node_info->device_name, node_info->type, node_info->name, |
| 301 | false /* is_active*/, 0 /* pluged_time */); |
| 302 | } |
| 303 | |
| 304 | AudioNodeList GenerateAudioNodeList( |
| 305 | const std::vector<const AudioNodeInfo*> nodes) { |
| 306 | AudioNodeList node_list; |
vmpstr | 843b41a | 2017-03-01 21:15:03 | [diff] [blame] | 307 | for (auto* node_info : nodes) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 308 | node_list.push_back(GenerateAudioNode(node_info)); |
| 309 | } |
| 310 | return node_list; |
| 311 | } |
| 312 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 313 | void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) { |
zelidrag | 29fe338 | 2014-08-27 01:44:48 | [diff] [blame] | 314 | DBusThreadManager::Initialize(); |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 315 | fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 316 | DBusThreadManager::Get()->GetCrasAudioClient()); |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 317 | fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 318 | audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
| 319 | CrasAudioHandler::Initialize(audio_pref_handler_); |
| 320 | cras_audio_handler_ = CrasAudioHandler::Get(); |
| 321 | test_observer_.reset(new TestObserver); |
| 322 | cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 323 | video_capture_manager_->AddObserver(cras_audio_handler_); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 324 | base::RunLoop().RunUntilIdle(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 325 | } |
| 326 | |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 327 | // Set up cras audio handlers with |audio_nodes| and set the active state of |
| 328 | // |active_device_in_pref| as active and |activate_by_user| in the pref, |
| 329 | // and rest of nodes in |audio_nodes_in_pref| as inactive. |
| 330 | void SetupCrasAudioHandlerWithActiveNodeInPref( |
| 331 | const AudioNodeList& audio_nodes, |
| 332 | const AudioNodeList& audio_nodes_in_pref, |
| 333 | const AudioDevice& active_device_in_pref, |
| 334 | bool activate_by_user) { |
| 335 | DBusThreadManager::Initialize(); |
| 336 | fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( |
| 337 | DBusThreadManager::Get()->GetCrasAudioClient()); |
| 338 | audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
| 339 | bool active; |
| 340 | for (const AudioNode& node : audio_nodes_in_pref) { |
| 341 | active = node.id == active_device_in_pref.id; |
| 342 | audio_pref_handler_->SetDeviceActive(AudioDevice(node), active, |
| 343 | activate_by_user); |
| 344 | } |
| 345 | |
| 346 | bool activate_by; |
| 347 | EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref, |
| 348 | &active, &activate_by)); |
| 349 | EXPECT_TRUE(active); |
| 350 | EXPECT_EQ(activate_by, activate_by_user); |
| 351 | |
| 352 | fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
| 353 | CrasAudioHandler::Initialize(audio_pref_handler_); |
| 354 | |
| 355 | cras_audio_handler_ = CrasAudioHandler::Get(); |
| 356 | test_observer_.reset(new TestObserver); |
| 357 | cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 358 | base::RunLoop().RunUntilIdle(); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 359 | } |
| 360 | |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 361 | void SetUpCrasAudioHandlerWithPrimaryActiveNode( |
| 362 | const AudioNodeList& audio_nodes, |
| 363 | const AudioNode& primary_active_node) { |
| 364 | DBusThreadManager::Initialize(); |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 365 | fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 366 | DBusThreadManager::Get()->GetCrasAudioClient()); |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 367 | fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 368 | fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id); |
| 369 | audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 370 | CrasAudioHandler::Initialize(audio_pref_handler_); |
| 371 | cras_audio_handler_ = CrasAudioHandler::Get(); |
| 372 | test_observer_.reset(new TestObserver); |
| 373 | cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 374 | base::RunLoop().RunUntilIdle(); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 375 | } |
| 376 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 377 | void ChangeAudioNodes(const AudioNodeList& audio_nodes) { |
satorux | cca5cc1 | 2014-10-27 16:33:59 | [diff] [blame] | 378 | fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting( |
derat | 3698adff | 2014-08-29 16:10:43 | [diff] [blame] | 379 | audio_nodes); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 380 | base::RunLoop().RunUntilIdle(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 381 | } |
| 382 | |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 383 | void VerifySystemMonitorWasCalled() { |
| 384 | // System monitor uses PostTask internally, so we have to process messages |
| 385 | // before verifying the expectation. |
| 386 | base::RunLoop().RunUntilIdle(); |
| 387 | EXPECT_EQ(1, system_monitor_observer_.device_changes_received()); |
| 388 | } |
| 389 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 390 | const AudioDevice* GetDeviceFromId(uint64_t id) { |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 391 | return cras_audio_handler_->GetDeviceFromId(id); |
| 392 | } |
| 393 | |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 394 | int GetActiveDeviceCount() const { |
| 395 | int num_active_nodes = 0; |
| 396 | AudioDeviceList audio_devices; |
| 397 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 398 | for (size_t i = 0; i < audio_devices.size(); ++i) { |
| 399 | if (audio_devices[i].active) |
| 400 | ++num_active_nodes; |
| 401 | } |
| 402 | return num_active_nodes; |
| 403 | } |
| 404 | |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 405 | void SetActiveHDMIRediscover() { |
| 406 | cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true); |
| 407 | } |
| 408 | |
| 409 | void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { |
| 410 | cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); |
| 411 | } |
| 412 | |
| 413 | bool IsDuringHDMIRediscoverGracePeriod() { |
| 414 | return cras_audio_handler_->hdmi_rediscovering(); |
| 415 | } |
| 416 | |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 417 | void RestartAudioClient() { |
| 418 | cras_audio_handler_->AudioClientRestarted(); |
fdoray | f5b47fd1 | 2016-09-13 14:12:36 | [diff] [blame] | 419 | base::RunLoop().RunUntilIdle(); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 420 | } |
| 421 | |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 422 | void StartFrontFacingCamera() { |
| 423 | video_capture_manager_->NotifyVideoCaptureStarted( |
| 424 | media::MEDIA_VIDEO_FACING_USER); |
| 425 | } |
| 426 | |
| 427 | void StopFrontFacingCamera() { |
| 428 | video_capture_manager_->NotifyVideoCaptureStopped( |
| 429 | media::MEDIA_VIDEO_FACING_USER); |
| 430 | } |
| 431 | |
| 432 | void StartRearFacingCamera() { |
| 433 | video_capture_manager_->NotifyVideoCaptureStarted( |
| 434 | media::MEDIA_VIDEO_FACING_ENVIRONMENT); |
| 435 | } |
| 436 | |
| 437 | void StopRearFacingCamera() { |
| 438 | video_capture_manager_->NotifyVideoCaptureStopped( |
| 439 | media::MEDIA_VIDEO_FACING_ENVIRONMENT); |
| 440 | } |
| 441 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 442 | protected: |
fdoray | 822e6b2 | 2017-04-27 20:47:03 | [diff] [blame] | 443 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 444 | base::SystemMonitor system_monitor_; |
| 445 | SystemMonitorObserver system_monitor_observer_; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 446 | CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. |
| 447 | FakeCrasAudioClient* fake_cras_audio_client_ = nullptr; // Not owned. |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 448 | std::unique_ptr<TestObserver> test_observer_; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 449 | scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 450 | std::unique_ptr<FakeVideoCaptureManager> video_capture_manager_; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 451 | |
| 452 | private: |
| 453 | DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
| 454 | }; |
| 455 | |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 456 | class HDMIRediscoverWaiter { |
| 457 | public: |
| 458 | HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test, |
| 459 | int grace_period_duration_in_ms) |
| 460 | : cras_audio_handler_test_(cras_audio_handler_test), |
| 461 | grace_period_duration_in_ms_(grace_period_duration_in_ms) {} |
| 462 | |
| 463 | void WaitUntilTimeOut(int wait_duration_in_ms) { |
| 464 | base::RunLoop run_loop; |
| 465 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 466 | FROM_HERE, run_loop.QuitClosure(), |
| 467 | base::TimeDelta::FromMilliseconds(wait_duration_in_ms)); |
| 468 | run_loop.Run(); |
| 469 | } |
| 470 | |
| 471 | void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) { |
| 472 | if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) { |
| 473 | quit_loop_func.Run(); |
| 474 | return; |
| 475 | } |
| 476 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 477 | FROM_HERE, |
| 478 | base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd, |
| 479 | base::Unretained(this), quit_loop_func), |
| 480 | base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4)); |
| 481 | } |
| 482 | |
| 483 | void WaitUntilHDMIRediscoverGracePeriodEnd() { |
| 484 | base::RunLoop run_loop; |
| 485 | CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure()); |
| 486 | run_loop.Run(); |
| 487 | } |
| 488 | |
| 489 | private: |
| 490 | CrasAudioHandlerTest* cras_audio_handler_test_; // not owned |
| 491 | int grace_period_duration_in_ms_; |
| 492 | |
| 493 | DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter); |
| 494 | }; |
| 495 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 496 | INSTANTIATE_TEST_CASE_P(StableIdV1, CrasAudioHandlerTest, testing::Values(1)); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 497 | INSTANTIATE_TEST_CASE_P(StableIdV2, CrasAudioHandlerTest, testing::Values(2)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 498 | |
| 499 | TEST_P(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) { |
| 500 | AudioNodeList audio_nodes = |
| 501 | GenerateAudioNodeList({kInternalSpeaker, kInternalMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 502 | SetUpCrasAudioHandler(audio_nodes); |
| 503 | |
| 504 | // Verify the audio devices size. |
| 505 | AudioDeviceList audio_devices; |
| 506 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 507 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 508 | |
| 509 | // Verify the internal speaker has been selected as the active output. |
| 510 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 511 | EXPECT_TRUE( |
| 512 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 513 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 514 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 515 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 516 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 517 | |
| 518 | // Ensure the internal microphone has been selected as the active input. |
| 519 | AudioDevice active_input; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 520 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 521 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
| 522 | } |
| 523 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 524 | TEST_P(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) { |
| 525 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 526 | {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 527 | SetUpCrasAudioHandler(audio_nodes); |
| 528 | |
| 529 | // Verify the audio devices size. |
| 530 | AudioDeviceList audio_devices; |
| 531 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 532 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 533 | |
| 534 | // Verify the headphone has been selected as the active output. |
| 535 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 536 | EXPECT_TRUE( |
| 537 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 538 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 539 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 540 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 541 | |
| 542 | // Ensure the USB microphone has been selected as the active input. |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 543 | EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 544 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 545 | } |
| 546 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 547 | TEST_P(CrasAudioHandlerTest, InitializeWithKeyboardMic) { |
| 548 | AudioNodeList audio_nodes = |
| 549 | GenerateAudioNodeList({kInternalSpeaker, kInternalMic, kKeyboardMic}); |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 550 | SetUpCrasAudioHandler(audio_nodes); |
| 551 | |
| 552 | // Verify the audio devices size. |
| 553 | AudioDeviceList audio_devices; |
| 554 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 555 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 556 | EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic()); |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 557 | |
| 558 | // Verify the internal speaker has been selected as the active output. |
| 559 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 560 | EXPECT_TRUE( |
| 561 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 562 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 563 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 564 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 565 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 566 | |
| 567 | // Ensure the internal microphone has been selected as the active input, |
| 568 | // not affected by keyboard mic. |
| 569 | AudioDevice active_input; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 570 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 571 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 572 | const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 573 | EXPECT_FALSE(keyboard_mic->active); |
| 574 | } |
| 575 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 576 | TEST_P(CrasAudioHandlerTest, SetKeyboardMicActive) { |
| 577 | AudioNodeList audio_nodes = |
| 578 | GenerateAudioNodeList({kInternalMic, kKeyboardMic}); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 579 | SetUpCrasAudioHandler(audio_nodes); |
| 580 | |
| 581 | // Verify the audio devices size. |
| 582 | AudioDeviceList audio_devices; |
| 583 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 584 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 585 | EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic()); |
| 586 | |
| 587 | // Ensure the internal microphone has been selected as the active input, |
| 588 | // not affected by keyboard mic. |
| 589 | AudioDevice active_input; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 590 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 591 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 592 | const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 593 | EXPECT_FALSE(keyboard_mic->active); |
| 594 | |
| 595 | // Make keyboard mic active. |
| 596 | cras_audio_handler_->SetKeyboardMicActive(true); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 597 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 598 | const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic->id); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 599 | EXPECT_TRUE(active_keyboard_mic->active); |
| 600 | |
| 601 | // Make keyboard mic inactive. |
| 602 | cras_audio_handler_->SetKeyboardMicActive(false); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 603 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 604 | const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic->id); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 605 | EXPECT_FALSE(inactive_keyboard_mic->active); |
[email protected] | f9a49d34 | 2014-07-29 23:47:20 | [diff] [blame] | 606 | } |
| 607 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 608 | TEST_P(CrasAudioHandlerTest, KeyboardMicNotSetAsPrimaryActive) { |
| 609 | AudioNodeList audio_nodes = GenerateAudioNodeList({kKeyboardMic}); |
jennyz | b46664f | 2016-03-08 18:18:10 | [diff] [blame] | 610 | SetUpCrasAudioHandler(audio_nodes); |
| 611 | |
| 612 | // Verify keyboard mic is not set as primary active input. |
| 613 | AudioDeviceList audio_devices; |
| 614 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 615 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 616 | EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic()); |
| 617 | EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 618 | |
| 619 | // Verify the internal mic is set as primary input. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 620 | audio_nodes.push_back(GenerateAudioNode(kInternalMic)); |
jennyz | b46664f | 2016-03-08 18:18:10 | [diff] [blame] | 621 | ChangeAudioNodes(audio_nodes); |
| 622 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 623 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 624 | EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 625 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
jennyz | b46664f | 2016-03-08 18:18:10 | [diff] [blame] | 626 | } |
| 627 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 628 | TEST_P(CrasAudioHandlerTest, SwitchActiveOutputDevice) { |
| 629 | AudioNodeList audio_nodes = |
| 630 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 631 | SetUpCrasAudioHandler(audio_nodes); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 632 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 633 | AudioDeviceList audio_devices; |
| 634 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 635 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 636 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 637 | |
| 638 | // Verify the initial active output device is headphone. |
| 639 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 640 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 641 | EXPECT_TRUE( |
| 642 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 643 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 644 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 645 | |
| 646 | // Switch the active output to internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 647 | AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 648 | cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
| 649 | CrasAudioHandler::ACTIVATE_BY_USER); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 650 | |
| 651 | // Verify the active output is switched to internal speaker, and the |
| 652 | // ActiveOutputNodeChanged event is fired. |
| 653 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 654 | VerifySystemMonitorWasCalled(); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 655 | EXPECT_TRUE( |
| 656 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 657 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 658 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 659 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 660 | } |
| 661 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 662 | TEST_P(CrasAudioHandlerTest, SwitchActiveInputDevice) { |
| 663 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 664 | SetUpCrasAudioHandler(audio_nodes); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 665 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 666 | AudioDeviceList audio_devices; |
| 667 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 668 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 669 | |
| 670 | // Verify the initial active input device is USB mic. |
| 671 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 672 | EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 673 | |
| 674 | // Switch the active input to internal mic. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 675 | AudioDevice internal_mic(GenerateAudioNode(kInternalMic)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 676 | cras_audio_handler_->SwitchToDevice(internal_mic, true, |
| 677 | CrasAudioHandler::ACTIVATE_BY_USER); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 678 | |
| 679 | // Verify the active output is switched to internal speaker, and the active |
| 680 | // ActiveInputNodeChanged event is fired. |
| 681 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 682 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 683 | } |
| 684 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 685 | TEST_P(CrasAudioHandlerTest, PlugHeadphone) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 686 | // Set up initial audio devices, only with internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 687 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 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 has been selected as the active output. |
| 698 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 699 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 700 | EXPECT_TRUE( |
| 701 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 702 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 703 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 704 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 705 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 706 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 707 | |
| 708 | // Plug the headphone. |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 709 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 710 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 711 | internal_speaker.active = true; |
| 712 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 713 | audio_nodes.push_back(GenerateAudioNode(kHeadphone)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 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()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 718 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 719 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 720 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 721 | |
| 722 | // Verify the active output device is switched to headphone and |
| 723 | // ActiveOutputChanged event is fired. |
| 724 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 725 | EXPECT_TRUE( |
| 726 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 727 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 728 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 729 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 730 | } |
| 731 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 732 | TEST_P(CrasAudioHandlerTest, UnplugHeadphone) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 733 | // Set up initial audio devices, with internal speaker and headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 734 | AudioNodeList audio_nodes = |
| 735 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 736 | SetUpCrasAudioHandler(audio_nodes); |
| 737 | const size_t init_nodes_size = audio_nodes.size(); |
| 738 | |
| 739 | // Verify the audio devices size. |
| 740 | AudioDeviceList audio_devices; |
| 741 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 742 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 743 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 744 | |
| 745 | // Verify the headphone has been selected as the active output. |
| 746 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 747 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 748 | EXPECT_TRUE( |
| 749 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 750 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 751 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 752 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 753 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 754 | |
| 755 | // Unplug the headphone. |
| 756 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 757 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 758 | ChangeAudioNodes(audio_nodes); |
| 759 | |
| 760 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 761 | // removed. |
| 762 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 763 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 764 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 765 | EXPECT_EQ(init_nodes_size - 1, audio_devices.size()); |
| 766 | |
| 767 | // Verify the active output device is switched to internal speaker and |
| 768 | // ActiveOutputChanged event is fired. |
| 769 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 770 | EXPECT_TRUE( |
| 771 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 772 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 773 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 774 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 775 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 776 | } |
| 777 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 778 | TEST_P(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) { |
| 779 | AudioNodeList audio_nodes = |
| 780 | GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 781 | SetUpCrasAudioHandler(audio_nodes); |
| 782 | |
| 783 | // Verify the audio devices size. |
| 784 | AudioDeviceList audio_devices; |
| 785 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 786 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 787 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 788 | |
| 789 | // Verify the bluetooth headset has been selected as the active output. |
| 790 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 791 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 792 | EXPECT_TRUE( |
| 793 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 794 | EXPECT_EQ(kBluetoothHeadset->id, active_output.id); |
| 795 | EXPECT_EQ(kBluetoothHeadset->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 796 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 797 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 798 | } |
| 799 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 800 | TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 801 | // Initialize with internal speaker and headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 802 | AudioNodeList audio_nodes = |
| 803 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 804 | SetUpCrasAudioHandler(audio_nodes); |
| 805 | const size_t init_nodes_size = audio_nodes.size(); |
| 806 | |
| 807 | // Verify the audio devices size. |
| 808 | AudioDeviceList audio_devices; |
| 809 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 810 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 811 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 812 | |
| 813 | // Verify the headphone is selected as the active output initially. |
| 814 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 815 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 816 | EXPECT_TRUE( |
| 817 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 818 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 819 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 820 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 821 | |
| 822 | // Connect to bluetooth headset. Since it is plugged in later than |
| 823 | // headphone, active output should be switched to it. |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 824 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 825 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 826 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 827 | AudioNode headphone = GenerateAudioNode(kHeadphone); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 828 | headphone.plugged_time = 80000000; |
| 829 | headphone.active = true; |
| 830 | audio_nodes.push_back(headphone); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 831 | AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 832 | bluetooth_headset.plugged_time = 90000000; |
| 833 | audio_nodes.push_back(bluetooth_headset); |
| 834 | ChangeAudioNodes(audio_nodes); |
| 835 | |
| 836 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 837 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 838 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 839 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 840 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 841 | |
| 842 | // Verify the active output device is switched to bluetooth headset, and |
| 843 | // ActiveOutputChanged event is fired. |
| 844 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 845 | EXPECT_TRUE( |
| 846 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 847 | EXPECT_EQ(kBluetoothHeadset->id, active_output.id); |
| 848 | EXPECT_EQ(kBluetoothHeadset->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 849 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 850 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 851 | |
| 852 | // Disconnect bluetooth headset. |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 853 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 854 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 855 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 856 | headphone.active = false; |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 857 | audio_nodes.push_back(headphone); |
| 858 | ChangeAudioNodes(audio_nodes); |
| 859 | |
| 860 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 861 | // removed. |
| 862 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 863 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 864 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 865 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 866 | |
| 867 | // Verify the active output device is switched to headphone, and |
| 868 | // ActiveOutputChanged event is fired. |
| 869 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 870 | EXPECT_TRUE( |
| 871 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 872 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 873 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 874 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 875 | } |
| 876 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 877 | TEST_P(CrasAudioHandlerTest, InitializeWithHDMIOutput) { |
| 878 | AudioNodeList audio_nodes = |
| 879 | GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 880 | SetUpCrasAudioHandler(audio_nodes); |
| 881 | |
| 882 | // Verify the audio devices size. |
| 883 | AudioDeviceList audio_devices; |
| 884 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 885 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 886 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 887 | |
| 888 | // Verify the HDMI device has been selected as the active output. |
| 889 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 890 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 891 | EXPECT_TRUE( |
| 892 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 893 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 894 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 895 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 896 | } |
| 897 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 898 | TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 899 | // Initialize with internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 900 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 901 | SetUpCrasAudioHandler(audio_nodes); |
| 902 | const size_t init_nodes_size = audio_nodes.size(); |
| 903 | |
| 904 | // Verify the audio devices size. |
| 905 | AudioDeviceList audio_devices; |
| 906 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 907 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 908 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 909 | |
| 910 | // Verify the internal speaker is selected as the active output initially. |
| 911 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 912 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 913 | EXPECT_TRUE( |
| 914 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 915 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 916 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 917 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 918 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 919 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 920 | |
| 921 | // Connect to HDMI output. |
| 922 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 923 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 924 | internal_speaker.active = true; |
| 925 | internal_speaker.plugged_time = 80000000; |
| 926 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 927 | AudioNode hdmi = GenerateAudioNode(kHDMIOutput); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 928 | hdmi.plugged_time = 90000000; |
| 929 | audio_nodes.push_back(hdmi); |
| 930 | ChangeAudioNodes(audio_nodes); |
| 931 | |
| 932 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 933 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 934 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 935 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 936 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 937 | |
| 938 | // Verify the active output device is switched to hdmi output, and |
| 939 | // ActiveOutputChanged event is fired. |
| 940 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 941 | EXPECT_TRUE( |
| 942 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 943 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 944 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 945 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 946 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 947 | |
| 948 | // Disconnect hdmi headset. |
| 949 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 950 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 951 | ChangeAudioNodes(audio_nodes); |
| 952 | |
| 953 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 954 | // removed. |
| 955 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 956 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 957 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 958 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 959 | |
| 960 | // Verify the active output device is switched to internal speaker, and |
| 961 | // ActiveOutputChanged event is fired. |
| 962 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 963 | EXPECT_TRUE( |
| 964 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 965 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 966 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 967 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 968 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 969 | } |
| 970 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 971 | TEST_P(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 972 | // Initialize with internal speaker, headphone and HDMI output. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 973 | AudioNodeList audio_nodes = |
| 974 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kHDMIOutput}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 975 | SetUpCrasAudioHandler(audio_nodes); |
| 976 | const size_t init_nodes_size = audio_nodes.size(); |
| 977 | |
| 978 | // Verify the audio devices size. |
| 979 | AudioDeviceList audio_devices; |
| 980 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 981 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 982 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 983 | |
| 984 | // Verify the headphone is selected as the active output initially. |
| 985 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 986 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 987 | EXPECT_TRUE( |
| 988 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 989 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 990 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 991 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 992 | |
| 993 | // Disconnect HDMI output. |
| 994 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 995 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 996 | audio_nodes.push_back(GenerateAudioNode(kHDMIOutput)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 997 | ChangeAudioNodes(audio_nodes); |
| 998 | |
| 999 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 1000 | // removed. |
| 1001 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1002 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1003 | EXPECT_EQ(init_nodes_size - 1, audio_devices.size()); |
| 1004 | |
| 1005 | // Verify the active output device is switched to HDMI output, and |
| 1006 | // ActiveOutputChanged event is fired. |
| 1007 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1008 | EXPECT_TRUE( |
| 1009 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1010 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 1011 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1012 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1013 | } |
| 1014 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1015 | TEST_P(CrasAudioHandlerTest, InitializeWithUSBHeadphone) { |
| 1016 | AudioNodeList audio_nodes = |
| 1017 | GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1018 | SetUpCrasAudioHandler(audio_nodes); |
| 1019 | |
| 1020 | // Verify the audio devices size. |
| 1021 | AudioDeviceList audio_devices; |
| 1022 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1023 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 1024 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1025 | |
| 1026 | // Verify the usb headphone has been selected as the active output. |
| 1027 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1028 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1029 | EXPECT_TRUE( |
| 1030 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1031 | EXPECT_EQ(kUSBHeadphone1->id, active_output.id); |
| 1032 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1033 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1034 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1035 | } |
| 1036 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1037 | TEST_P(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1038 | // Initialize with internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1039 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1040 | SetUpCrasAudioHandler(audio_nodes); |
| 1041 | const size_t init_nodes_size = audio_nodes.size(); |
| 1042 | |
| 1043 | // Verify the audio devices size. |
| 1044 | AudioDeviceList audio_devices; |
| 1045 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1046 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1047 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1048 | |
| 1049 | // Verify the internal speaker is selected as the active output initially. |
| 1050 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1051 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1052 | EXPECT_TRUE( |
| 1053 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1054 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1055 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1056 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1057 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1058 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1059 | |
| 1060 | // Plug in usb headphone |
| 1061 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1062 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1063 | internal_speaker.active = true; |
| 1064 | internal_speaker.plugged_time = 80000000; |
| 1065 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1066 | AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1067 | usb_headphone.plugged_time = 90000000; |
| 1068 | audio_nodes.push_back(usb_headphone); |
| 1069 | ChangeAudioNodes(audio_nodes); |
| 1070 | |
| 1071 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 1072 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1073 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1074 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1075 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1076 | |
| 1077 | // Verify the active output device is switched to usb headphone, and |
| 1078 | // ActiveOutputChanged event is fired. |
| 1079 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1080 | EXPECT_TRUE( |
| 1081 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1082 | EXPECT_EQ(kUSBHeadphone1->id, active_output.id); |
| 1083 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1084 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1085 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1086 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1087 | |
| 1088 | // Unplug usb headphone. |
| 1089 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1090 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1091 | ChangeAudioNodes(audio_nodes); |
| 1092 | |
| 1093 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 1094 | // removed. |
| 1095 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1096 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1097 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1098 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1099 | |
| 1100 | // Verify the active output device is switched to internal speaker, and |
| 1101 | // ActiveOutputChanged event is fired. |
| 1102 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1103 | EXPECT_TRUE( |
| 1104 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1105 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1106 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1107 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1108 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 1109 | } |
| 1110 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1111 | TEST_P(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1112 | // Initialize with internal speaker and one usb headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1113 | AudioNodeList audio_nodes = |
| 1114 | GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1115 | SetUpCrasAudioHandler(audio_nodes); |
| 1116 | const size_t init_nodes_size = audio_nodes.size(); |
| 1117 | |
| 1118 | // Verify the audio devices size. |
| 1119 | AudioDeviceList audio_devices; |
| 1120 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1121 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1122 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1123 | |
| 1124 | // Verify the usb headphone is selected as the active output initially. |
| 1125 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1126 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1127 | EXPECT_TRUE( |
| 1128 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1129 | EXPECT_EQ(kUSBHeadphone1->id, active_output.id); |
| 1130 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1131 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1132 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1133 | |
| 1134 | // Plug in another usb headphone. |
| 1135 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1136 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 1137 | AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1138 | usb_headphone_1.active = true; |
| 1139 | usb_headphone_1.plugged_time = 80000000; |
| 1140 | audio_nodes.push_back(usb_headphone_1); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1141 | AudioNode usb_headphone_2 = GenerateAudioNode(kUSBHeadphone2); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1142 | usb_headphone_2.plugged_time = 90000000; |
| 1143 | audio_nodes.push_back(usb_headphone_2); |
| 1144 | ChangeAudioNodes(audio_nodes); |
| 1145 | |
| 1146 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 1147 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1148 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1149 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1150 | |
| 1151 | // Verify the active output device is switched to the 2nd usb headphone, which |
| 1152 | // is plugged later, and ActiveOutputChanged event is fired. |
| 1153 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1154 | EXPECT_TRUE( |
| 1155 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1156 | EXPECT_EQ(kUSBHeadphone2->id, active_output.id); |
| 1157 | EXPECT_EQ(kUSBHeadphone2->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1158 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1159 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1160 | |
| 1161 | // Unplug the 2nd usb headphone. |
| 1162 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1163 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 1164 | audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1165 | ChangeAudioNodes(audio_nodes); |
| 1166 | |
| 1167 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 1168 | // removed. |
| 1169 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
| 1170 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1171 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1172 | |
| 1173 | // Verify the active output device is switched to the first usb headphone, and |
| 1174 | // ActiveOutputChanged event is fired. |
| 1175 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1176 | EXPECT_TRUE( |
| 1177 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1178 | EXPECT_EQ(kUSBHeadphone1->id, active_output.id); |
| 1179 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1180 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1181 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1182 | } |
| 1183 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1184 | TEST_P(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) { |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1185 | // Initialize with internal speaker and one usb headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1186 | AudioNodeList audio_nodes = |
| 1187 | GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1}); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1188 | SetUpCrasAudioHandler(audio_nodes); |
| 1189 | const size_t init_nodes_size = audio_nodes.size(); |
| 1190 | |
| 1191 | // Verify the audio devices size. |
| 1192 | AudioDeviceList audio_devices; |
| 1193 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1194 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1195 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1196 | |
| 1197 | // Verify the usb headphone is selected as the active output initially. |
| 1198 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1199 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1200 | EXPECT_TRUE( |
| 1201 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1202 | EXPECT_EQ(kUSBHeadphone1->id, active_output.id); |
| 1203 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1204 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1205 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1206 | |
| 1207 | // Plug in the headphone jack. |
| 1208 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1209 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 1210 | AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1211 | usb_headphone_1.active = true; |
| 1212 | usb_headphone_1.plugged_time = 80000000; |
| 1213 | audio_nodes.push_back(usb_headphone_1); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1214 | AudioNode headphone_jack = GenerateAudioNode(kHeadphone); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1215 | headphone_jack.plugged_time = 90000000; |
| 1216 | audio_nodes.push_back(headphone_jack); |
| 1217 | ChangeAudioNodes(audio_nodes); |
| 1218 | |
| 1219 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 1220 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1221 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1222 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1223 | |
| 1224 | // Verify the active output device is switched to the headphone jack, which |
| 1225 | // is plugged later, and ActiveOutputChanged event is fired. |
| 1226 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1227 | EXPECT_TRUE( |
| 1228 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1229 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 1230 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1231 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1232 | |
| 1233 | // Select the speaker to be the active output device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1234 | AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 1235 | cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
| 1236 | CrasAudioHandler::ACTIVATE_BY_USER); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1237 | |
| 1238 | // Verify the active output is switched to internal speaker, and the |
| 1239 | // ActiveOutputNodeChanged event is fired. |
| 1240 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1241 | EXPECT_TRUE( |
| 1242 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1243 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1244 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1245 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1246 | |
| 1247 | // Unplug the usb headphone. |
| 1248 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1249 | AudioNode internal_speaker_node(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1250 | internal_speaker_node.active = true; |
| 1251 | internal_speaker_node.plugged_time = 70000000; |
| 1252 | audio_nodes.push_back(internal_speaker_node); |
| 1253 | headphone_jack.active = false; |
| 1254 | audio_nodes.push_back(headphone_jack); |
| 1255 | ChangeAudioNodes(audio_nodes); |
| 1256 | |
| 1257 | // Verify the AudioNodesChanged event is fired and one audio device is |
| 1258 | // removed. |
| 1259 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
| 1260 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1261 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1262 | |
| 1263 | // Verify the active output device remains to be speaker. |
| 1264 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1265 | EXPECT_TRUE( |
| 1266 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1267 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1268 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1269 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ca47325 | 2013-08-15 11:48:39 | [diff] [blame] | 1270 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1271 | } |
| 1272 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1273 | TEST_P(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) { |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1274 | // This tests the case found with crbug.com/273271. |
| 1275 | // Initialize with internal speaker, bluetooth headphone and headphone jack |
| 1276 | // for a new chrome session after user signs out from the previous session. |
| 1277 | // Headphone jack is plugged in later than bluetooth headphone, but bluetooth |
| 1278 | // headphone is selected as the active output by user from previous user |
| 1279 | // session. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1280 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
| 1281 | AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset); |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1282 | bluetooth_headphone.active = true; |
| 1283 | bluetooth_headphone.plugged_time = 70000000; |
| 1284 | audio_nodes.push_back(bluetooth_headphone); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1285 | AudioNode headphone_jack = GenerateAudioNode(kHeadphone); |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1286 | headphone_jack.plugged_time = 80000000; |
| 1287 | audio_nodes.push_back(headphone_jack); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1288 | SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone); |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1289 | const size_t init_nodes_size = audio_nodes.size(); |
| 1290 | |
| 1291 | // Verify the audio devices size. |
| 1292 | AudioDeviceList audio_devices; |
| 1293 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1294 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1295 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1296 | |
| 1297 | // Verify the headphone jack is selected as the active output and all other |
| 1298 | // audio devices are not active. |
| 1299 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1300 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1301 | EXPECT_TRUE( |
| 1302 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1303 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 1304 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1305 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1306 | for (size_t i = 0; i < audio_devices.size(); ++i) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1307 | if (audio_devices[i].id != kHeadphone->id) |
[email protected] | d413fcc | 2013-08-16 23:20:03 | [diff] [blame] | 1308 | EXPECT_FALSE(audio_devices[i].active); |
| 1309 | } |
| 1310 | } |
| 1311 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1312 | TEST_P(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) { |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1313 | // Initialize with internal speaker and bluetooth headset. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1314 | AudioNodeList audio_nodes = |
| 1315 | GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset}); |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1316 | SetUpCrasAudioHandler(audio_nodes); |
| 1317 | const size_t init_nodes_size = audio_nodes.size(); |
| 1318 | |
| 1319 | // Verify the audio devices size. |
| 1320 | AudioDeviceList audio_devices; |
| 1321 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1322 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1323 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1324 | |
| 1325 | // Verify the bluetooth headset is selected as the active output and all other |
| 1326 | // audio devices are not active. |
| 1327 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
| 1328 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1329 | EXPECT_TRUE( |
| 1330 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1331 | EXPECT_EQ(kBluetoothHeadset->id, active_output.id); |
| 1332 | EXPECT_EQ(kBluetoothHeadset->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1333 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1334 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1335 | |
| 1336 | // Cras changes the bluetooth headset's id on the fly. |
| 1337 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1338 | AudioNode internal_speaker(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1339 | internal_speaker.active = false; |
| 1340 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1341 | AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset); |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1342 | // Change bluetooth headphone id. |
| 1343 | bluetooth_headphone.id = kBluetoothHeadsetId + 20000; |
| 1344 | bluetooth_headphone.active = false; |
| 1345 | audio_nodes.push_back(bluetooth_headphone); |
| 1346 | ChangeAudioNodes(audio_nodes); |
| 1347 | |
| 1348 | // Verify NodesChanged event is fired, and the audio devices size is not |
| 1349 | // changed. |
| 1350 | audio_devices.clear(); |
| 1351 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1352 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1353 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1354 | |
| 1355 | // Verify ActiveOutputNodeChanged event is fired, and active device should be |
| 1356 | // bluetooth headphone. |
| 1357 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1358 | EXPECT_TRUE( |
| 1359 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
[email protected] | 110babd | 2013-09-17 01:46:02 | [diff] [blame] | 1360 | EXPECT_EQ(bluetooth_headphone.id, active_output.id); |
| 1361 | } |
| 1362 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1363 | TEST_P(CrasAudioHandlerTest, PlugUSBMic) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1364 | // Set up initial audio devices, only with internal mic. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1365 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1366 | SetUpCrasAudioHandler(audio_nodes); |
| 1367 | const size_t init_nodes_size = audio_nodes.size(); |
| 1368 | |
| 1369 | // Verify the audio devices size. |
| 1370 | AudioDeviceList audio_devices; |
| 1371 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1372 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1373 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1374 | |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1375 | // Verify the internal mic is selected as the active input. |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1376 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1377 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1378 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1379 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1380 | |
| 1381 | // Plug the USB Mic. |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1382 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1383 | AudioNode internal_mic(GenerateAudioNode(kInternalMic)); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1384 | internal_mic.active = true; |
| 1385 | audio_nodes.push_back(internal_mic); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1386 | audio_nodes.push_back(GenerateAudioNode(kUSBMic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1387 | ChangeAudioNodes(audio_nodes); |
| 1388 | |
| 1389 | // Verify the AudioNodesChanged event is fired and new audio device is added. |
| 1390 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1391 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1392 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1393 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1394 | |
| 1395 | // Verify the active input device is switched to USB mic and |
| 1396 | // and ActiveInputChanged event is fired. |
| 1397 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1398 | EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1399 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1400 | } |
| 1401 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1402 | TEST_P(CrasAudioHandlerTest, UnplugUSBMic) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1403 | // Set up initial audio devices, with internal mic and USB Mic. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1404 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1405 | SetUpCrasAudioHandler(audio_nodes); |
| 1406 | const size_t init_nodes_size = audio_nodes.size(); |
| 1407 | |
| 1408 | // Verify the audio devices size. |
| 1409 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1410 | AudioDeviceList audio_devices; |
| 1411 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1412 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1413 | |
| 1414 | // Verify the USB mic is selected as the active output. |
| 1415 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1416 | EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1417 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1418 | system_monitor_observer_.reset_count(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1419 | |
| 1420 | // Unplug the USB Mic. |
| 1421 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1422 | audio_nodes.push_back(GenerateAudioNode(kInternalMic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1423 | ChangeAudioNodes(audio_nodes); |
| 1424 | |
| 1425 | // Verify the AudioNodesChanged event is fired, and one audio device is |
| 1426 | // removed. |
| 1427 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
Max Morin | 7a2d72a | 2017-07-20 19:06:25 | [diff] [blame] | 1428 | VerifySystemMonitorWasCalled(); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1429 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1430 | EXPECT_EQ(init_nodes_size - 1, audio_devices.size()); |
| 1431 | |
| 1432 | // Verify the active input device is switched to internal mic, and |
| 1433 | // and ActiveInputChanged event is fired. |
| 1434 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1435 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1436 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
| 1437 | } |
| 1438 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1439 | TEST_P(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) { |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1440 | // Set up initial audio devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1441 | AudioNodeList audio_nodes = |
| 1442 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kInternalMic}); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1443 | SetUpCrasAudioHandler(audio_nodes); |
| 1444 | const size_t init_nodes_size = audio_nodes.size(); |
| 1445 | |
| 1446 | // Verify the audio devices size. |
| 1447 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1448 | AudioDeviceList audio_devices; |
| 1449 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1450 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1451 | |
| 1452 | // Verify the internal mic is selected as the active input. |
| 1453 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1454 | EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1455 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
| 1456 | |
| 1457 | // Verify the headphone is selected as the active output. |
| 1458 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1459 | EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1460 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1461 | |
| 1462 | // Switch the active output to internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1463 | AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 1464 | cras_audio_handler_->SwitchToDevice(internal_speaker, true, |
| 1465 | CrasAudioHandler::ACTIVATE_BY_USER); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1466 | |
| 1467 | // Verify the active output is switched to internal speaker, and the |
| 1468 | // ActiveOutputNodeChanged event is fired. |
| 1469 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
| 1470 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1471 | EXPECT_TRUE( |
| 1472 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1473 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1474 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1475 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1476 | |
| 1477 | // Plug the USB Mic. |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1478 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1479 | AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1480 | internal_speaker_node.active = true; |
| 1481 | audio_nodes.push_back(internal_speaker_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1482 | audio_nodes.push_back(GenerateAudioNode(kHeadphone)); |
| 1483 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1484 | internal_mic.active = true; |
| 1485 | audio_nodes.push_back(internal_mic); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1486 | audio_nodes.push_back(GenerateAudioNode(kUSBMic)); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1487 | ChangeAudioNodes(audio_nodes); |
| 1488 | |
| 1489 | // Verify the AudioNodesChanged event is fired, one new device is added. |
| 1490 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1491 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1492 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1493 | |
| 1494 | // Verify the active input device is switched to USB mic, and |
| 1495 | // and ActiveInputChanged event is fired. |
| 1496 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1497 | EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1498 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1499 | |
| 1500 | // Verify the active output device is not changed. |
| 1501 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1502 | EXPECT_TRUE( |
| 1503 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1504 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 1505 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1506 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | babc24b | 2013-09-09 23:50:23 | [diff] [blame] | 1507 | } |
| 1508 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1509 | TEST_P(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) { |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1510 | // Set up initial audio devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1511 | AudioNodeList audio_nodes = |
| 1512 | GenerateAudioNodeList({kUSBHeadphone1, kInternalSpeaker, kInternalMic}); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1513 | SetUpCrasAudioHandler(audio_nodes); |
| 1514 | const size_t init_nodes_size = audio_nodes.size(); |
| 1515 | |
| 1516 | // Verify the audio devices size. |
| 1517 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1518 | AudioDeviceList audio_devices; |
| 1519 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1520 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1521 | |
| 1522 | // Verify the internal mic is selected as the active input. |
| 1523 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1524 | EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1525 | EXPECT_FALSE(cras_audio_handler_->has_alternative_input()); |
| 1526 | |
| 1527 | // Verify the USB headphone is selected as the active output. |
| 1528 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1529 | EXPECT_EQ(kUSBHeadphoneId1, |
| 1530 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1531 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1532 | |
| 1533 | // Plug the headphone and auto-unplug internal speaker. |
| 1534 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1535 | AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1536 | usb_headphone_node.active = true; |
| 1537 | audio_nodes.push_back(usb_headphone_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1538 | AudioNode headphone_node = GenerateAudioNode(kHeadphone); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1539 | headphone_node.plugged_time = 1000; |
| 1540 | audio_nodes.push_back(headphone_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1541 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1542 | internal_mic.active = true; |
| 1543 | audio_nodes.push_back(internal_mic); |
| 1544 | ChangeAudioNodes(audio_nodes); |
| 1545 | |
| 1546 | // Verify the AudioNodesChanged event is fired, with nodes count unchanged. |
| 1547 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1548 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1549 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1550 | |
| 1551 | // Verify the active output device is switched to headphone, and |
| 1552 | // an ActiveOutputChanged event is fired. |
| 1553 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1554 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1555 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1556 | |
| 1557 | // Unplug the headphone and internal speaker auto-plugs back. |
| 1558 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1559 | audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1)); |
| 1560 | AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1561 | internal_speaker_node.plugged_time = 2000; |
| 1562 | audio_nodes.push_back(internal_speaker_node); |
| 1563 | audio_nodes.push_back(internal_mic); |
| 1564 | ChangeAudioNodes(audio_nodes); |
| 1565 | |
| 1566 | // Verify the AudioNodesChanged event is fired, with nodes count unchanged. |
| 1567 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
| 1568 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1569 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1570 | |
| 1571 | // Verify the active output device is switched back to USB, and |
| 1572 | // an ActiveOutputChanged event is fired. |
| 1573 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1574 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1575 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1576 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1577 | |
| 1578 | // Verify the active input device is not changed. |
| 1579 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1580 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1581 | } |
| 1582 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1583 | TEST_P(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) { |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1584 | // Set up initial audio devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1585 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 1586 | {kUSBHeadphone1, kInternalSpeaker, kUSBMic, kInternalMic}); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1587 | SetUpCrasAudioHandler(audio_nodes); |
| 1588 | const size_t init_nodes_size = audio_nodes.size(); |
| 1589 | |
| 1590 | // Verify the audio devices size. |
| 1591 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1592 | AudioDeviceList audio_devices; |
| 1593 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1594 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1595 | |
| 1596 | // Verify the internal mic is selected as the active input. |
| 1597 | EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1598 | EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1599 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1600 | |
| 1601 | // Verify the internal speaker is selected as the active output. |
| 1602 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1603 | EXPECT_EQ(kUSBHeadphoneId1, |
| 1604 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1605 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1606 | |
| 1607 | // Plug the headphone and mic, auto-unplug internal mic and speaker. |
| 1608 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1609 | AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1610 | usb_headphone_node.active = true; |
| 1611 | audio_nodes.push_back(usb_headphone_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1612 | AudioNode headphone_node = GenerateAudioNode(kHeadphone); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1613 | headphone_node.plugged_time = 1000; |
| 1614 | audio_nodes.push_back(headphone_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1615 | AudioNode usb_mic = GenerateAudioNode(kUSBMic); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1616 | usb_mic.active = true; |
| 1617 | audio_nodes.push_back(usb_mic); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1618 | AudioNode mic_jack = GenerateAudioNode(kMicJack); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1619 | mic_jack.plugged_time = 1000; |
| 1620 | audio_nodes.push_back(mic_jack); |
| 1621 | ChangeAudioNodes(audio_nodes); |
| 1622 | |
| 1623 | // Verify the AudioNodesChanged event is fired, with nodes count unchanged. |
| 1624 | EXPECT_EQ(1, test_observer_->audio_nodes_changed_count()); |
| 1625 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1626 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1627 | |
| 1628 | // Verify the active output device is switched to headphone, and |
| 1629 | // an ActiveOutputChanged event is fired. |
| 1630 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1631 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1632 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1633 | |
| 1634 | // Verify the active input device is switched to mic jack, and |
| 1635 | // an ActiveInputChanged event is fired. |
| 1636 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1637 | EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1638 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1639 | |
| 1640 | // Unplug the headphone and internal speaker auto-plugs back. |
| 1641 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1642 | audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1)); |
| 1643 | AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1644 | internal_speaker_node.plugged_time = 2000; |
| 1645 | audio_nodes.push_back(internal_speaker_node); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1646 | audio_nodes.push_back(GenerateAudioNode(kUSBMic)); |
| 1647 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1648 | internal_mic.plugged_time = 2000; |
| 1649 | audio_nodes.push_back(internal_mic); |
| 1650 | ChangeAudioNodes(audio_nodes); |
| 1651 | |
| 1652 | // Verify the AudioNodesChanged event is fired, with nodes count unchanged. |
| 1653 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
| 1654 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1655 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1656 | |
| 1657 | // Verify the active output device is switched back to USB, and |
| 1658 | // an ActiveOutputChanged event is fired. |
| 1659 | EXPECT_EQ(2, test_observer_->active_output_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1660 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1661 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1662 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1663 | |
| 1664 | // Verify the active input device is switched back to USB mic, and |
| 1665 | // an ActiveInputChanged event is fired. |
| 1666 | EXPECT_EQ(2, test_observer_->active_input_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1667 | EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ce631ee | 2014-01-23 03:37:06 | [diff] [blame] | 1668 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1669 | } |
| 1670 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1671 | TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) { |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1672 | // Set up initial audio devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1673 | AudioNodeList audio_nodes = |
| 1674 | GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset}); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1675 | SetUpCrasAudioHandler(audio_nodes); |
| 1676 | const size_t init_nodes_size = audio_nodes.size(); |
| 1677 | |
| 1678 | // Verify the audio devices size. |
| 1679 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1680 | AudioDeviceList audio_devices; |
| 1681 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1682 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1683 | |
| 1684 | // Verify the bluetooth headset is selected as the active output. |
| 1685 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1686 | EXPECT_EQ(kBluetoothHeadsetId, |
| 1687 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1688 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1689 | EXPECT_TRUE( |
| 1690 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1691 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1692 | |
| 1693 | // Plug in headphone, but fire NodesChanged signal twice. |
| 1694 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1695 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 1696 | AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1697 | bluetooth_headset.plugged_time = 1000; |
| 1698 | bluetooth_headset.active = true; |
| 1699 | audio_nodes.push_back(bluetooth_headset); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1700 | AudioNode headphone = GenerateAudioNode(kHeadphone); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1701 | headphone.active = false; |
| 1702 | headphone.plugged_time = 2000; |
| 1703 | audio_nodes.push_back(headphone); |
| 1704 | ChangeAudioNodes(audio_nodes); |
| 1705 | ChangeAudioNodes(audio_nodes); |
| 1706 | |
| 1707 | // Verify the active output device is set to headphone. |
| 1708 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1709 | EXPECT_LE(1, test_observer_->active_output_node_changed_count()); |
| 1710 | EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 1711 | EXPECT_TRUE( |
| 1712 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1713 | EXPECT_EQ(headphone.id, active_output.id); |
| 1714 | |
| 1715 | // Verfiy the audio devices data is consistent, i.e., the active output device |
| 1716 | // should be headphone. |
| 1717 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1718 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1719 | for (size_t i = 0; i < audio_devices.size(); ++i) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1720 | if (audio_devices[i].id == kInternalSpeaker->id) |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1721 | EXPECT_FALSE(audio_devices[i].active); |
| 1722 | else if (audio_devices[i].id == bluetooth_headset.id) |
| 1723 | EXPECT_FALSE(audio_devices[i].active); |
| 1724 | else if (audio_devices[i].id == headphone.id) |
| 1725 | EXPECT_TRUE(audio_devices[i].active); |
| 1726 | else |
| 1727 | NOTREACHED(); |
| 1728 | } |
| 1729 | } |
| 1730 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1731 | TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) { |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1732 | // Set up initial audio devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1733 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic}); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1734 | SetUpCrasAudioHandler(audio_nodes); |
| 1735 | const size_t init_nodes_size = audio_nodes.size(); |
| 1736 | |
| 1737 | // Verify the audio devices size. |
| 1738 | EXPECT_EQ(0, test_observer_->audio_nodes_changed_count()); |
| 1739 | AudioDeviceList audio_devices; |
| 1740 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1741 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1742 | |
| 1743 | // Verify the internal mic is selected as the active output. |
| 1744 | EXPECT_EQ(0, test_observer_->active_output_node_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1745 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1746 | EXPECT_FALSE(cras_audio_handler_->has_alternative_output()); |
| 1747 | EXPECT_TRUE(audio_devices[0].active); |
| 1748 | |
| 1749 | // Plug in usb mic, but fire NodesChanged signal twice. |
| 1750 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1751 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1752 | internal_mic.active = true; |
| 1753 | internal_mic.plugged_time = 1000; |
| 1754 | audio_nodes.push_back(internal_mic); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1755 | AudioNode usb_mic = GenerateAudioNode(kUSBMic); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1756 | usb_mic.active = false; |
| 1757 | usb_mic.plugged_time = 2000; |
| 1758 | audio_nodes.push_back(usb_mic); |
| 1759 | ChangeAudioNodes(audio_nodes); |
| 1760 | ChangeAudioNodes(audio_nodes); |
| 1761 | |
| 1762 | // Verify the active output device is set to headphone. |
| 1763 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1764 | EXPECT_LE(1, test_observer_->active_input_node_changed_count()); |
| 1765 | EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1766 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1767 | |
| 1768 | // Verfiy the audio devices data is consistent, i.e., the active input device |
| 1769 | // should be usb mic. |
| 1770 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1771 | EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); |
| 1772 | for (size_t i = 0; i < audio_devices.size(); ++i) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1773 | if (audio_devices[i].id == kInternalMic->id) |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1774 | EXPECT_FALSE(audio_devices[i].active); |
| 1775 | else if (audio_devices[i].id == usb_mic.id) |
| 1776 | EXPECT_TRUE(audio_devices[i].active); |
| 1777 | else |
| 1778 | NOTREACHED(); |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | // This is the case of crbug.com/291303. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1783 | TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) { |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1784 | // Set up audio handler with empty audio_nodes. |
| 1785 | AudioNodeList audio_nodes; |
| 1786 | SetUpCrasAudioHandler(audio_nodes); |
| 1787 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1788 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1789 | internal_speaker.active = false; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1790 | AudioNode headphone = GenerateAudioNode(kHeadphone); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1791 | headphone.active = false; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1792 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1793 | internal_mic.active = false; |
| 1794 | audio_nodes.push_back(internal_speaker); |
| 1795 | audio_nodes.push_back(headphone); |
| 1796 | audio_nodes.push_back(internal_mic); |
| 1797 | const size_t init_nodes_size = audio_nodes.size(); |
| 1798 | |
| 1799 | // Simulate AudioNodesChanged signal being fired twice during system boot. |
| 1800 | ChangeAudioNodes(audio_nodes); |
| 1801 | ChangeAudioNodes(audio_nodes); |
| 1802 | |
| 1803 | // Verify the active output device is set to headphone. |
| 1804 | EXPECT_EQ(2, test_observer_->audio_nodes_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1805 | EXPECT_LE(1, test_observer_->active_output_node_changed_count()); |
| 1806 | EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1807 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1808 | EXPECT_TRUE( |
| 1809 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1810 | EXPECT_EQ(headphone.id, active_output.id); |
| 1811 | |
| 1812 | // Verify the active input device id is set to internal mic. |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1813 | EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | 4642d779 | 2013-09-18 20:27:17 | [diff] [blame] | 1814 | |
| 1815 | // Verfiy the audio devices data is consistent, i.e., the active output device |
| 1816 | // should be headphone, and the active input device should internal mic. |
| 1817 | AudioDeviceList audio_devices; |
| 1818 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1819 | EXPECT_EQ(init_nodes_size, audio_devices.size()); |
| 1820 | for (size_t i = 0; i < audio_devices.size(); ++i) { |
| 1821 | if (audio_devices[i].id == internal_speaker.id) |
| 1822 | EXPECT_FALSE(audio_devices[i].active); |
| 1823 | else if (audio_devices[i].id == headphone.id) |
| 1824 | EXPECT_TRUE(audio_devices[i].active); |
| 1825 | else if (audio_devices[i].id == internal_mic.id) |
| 1826 | EXPECT_TRUE(audio_devices[i].active); |
| 1827 | else |
| 1828 | NOTREACHED(); |
| 1829 | } |
| 1830 | } |
| 1831 | |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1832 | // This is the case of crbug.com/448924. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1833 | TEST_P(CrasAudioHandlerTest, |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1834 | TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) { |
| 1835 | // Set up audio handler with 4 audio_nodes. |
| 1836 | AudioNodeList audio_nodes; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1837 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1838 | internal_speaker.active = false; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1839 | AudioNode headphone = GenerateAudioNode(kHeadphone); |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1840 | headphone.active = false; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1841 | AudioNode internal_mic = GenerateAudioNode(kInternalMic); |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1842 | internal_mic.active = false; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1843 | AudioNode micJack = GenerateAudioNode(kMicJack); |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1844 | micJack.active = false; |
| 1845 | audio_nodes.push_back(internal_speaker); |
| 1846 | audio_nodes.push_back(headphone); |
| 1847 | audio_nodes.push_back(internal_mic); |
| 1848 | audio_nodes.push_back(micJack); |
| 1849 | SetUpCrasAudioHandler(audio_nodes); |
| 1850 | |
| 1851 | // Verify the audio devices size. |
| 1852 | AudioDeviceList audio_devices; |
| 1853 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 1854 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 1855 | |
| 1856 | // Verify the headphone has been selected as the active output. |
| 1857 | AudioDevice active_output; |
| 1858 | EXPECT_TRUE( |
| 1859 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1860 | EXPECT_EQ(kHeadphone->id, active_output.id); |
| 1861 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | 2d8986d | 2015-01-15 18:34:07 | [diff] [blame] | 1862 | EXPECT_TRUE(active_output.active); |
| 1863 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 1864 | |
| 1865 | // Verify the mic Jack has been selected as the active input. |
| 1866 | EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 1867 | const AudioDevice* active_input = GetDeviceFromId(micJack.id); |
| 1868 | EXPECT_TRUE(active_input->active); |
| 1869 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 1870 | |
| 1871 | // Simulate the nodes list in first NodesChanged signal, only headphone is |
| 1872 | // removed, other nodes remains the same. |
| 1873 | AudioNodeList changed_nodes_1; |
| 1874 | internal_speaker.active = false; |
| 1875 | changed_nodes_1.push_back(internal_speaker); |
| 1876 | internal_mic.active = false; |
| 1877 | changed_nodes_1.push_back(internal_mic); |
| 1878 | micJack.active = true; |
| 1879 | changed_nodes_1.push_back(micJack); |
| 1880 | |
| 1881 | // Simulate the nodes list in second NodesChanged signal, the micJac is |
| 1882 | // removed, but the internal_mic is inactive, which does not reflect the |
| 1883 | // active status set from the first NodesChanged signal since this was sent |
| 1884 | // before cras receives the SetActiveOutputNode from the first NodesChanged |
| 1885 | // handling. |
| 1886 | AudioNodeList changed_nodes_2; |
| 1887 | changed_nodes_2.push_back(internal_speaker); |
| 1888 | changed_nodes_2.push_back(internal_mic); |
| 1889 | |
| 1890 | // Simulate AudioNodesChanged signal being fired twice for unplug an audio |
| 1891 | // device with both input and output nodes on it. |
| 1892 | ChangeAudioNodes(changed_nodes_1); |
| 1893 | ChangeAudioNodes(changed_nodes_2); |
| 1894 | |
| 1895 | AudioDeviceList changed_devices; |
| 1896 | cras_audio_handler_->GetAudioDevices(&changed_devices); |
| 1897 | EXPECT_EQ(2u, changed_devices.size()); |
| 1898 | |
| 1899 | // Verify the active output device is set to internal speaker. |
| 1900 | EXPECT_EQ(internal_speaker.id, |
| 1901 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 1902 | EXPECT_TRUE( |
| 1903 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 1904 | EXPECT_EQ(internal_speaker.id, active_output.id); |
| 1905 | EXPECT_TRUE(active_output.active); |
| 1906 | |
| 1907 | // Verify the active input device id is set to internal mic. |
| 1908 | EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 1909 | const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id); |
| 1910 | EXPECT_TRUE(changed_active_input->active); |
| 1911 | } |
| 1912 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1913 | TEST_P(CrasAudioHandlerTest, SetOutputMono) { |
| 1914 | AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone}); |
warx | 7452462 | 2016-03-31 01:07:21 | [diff] [blame] | 1915 | SetUpCrasAudioHandler(audio_nodes); |
| 1916 | EXPECT_EQ(0, test_observer_->output_channel_remixing_changed_count()); |
| 1917 | |
| 1918 | // Set output mono |
| 1919 | cras_audio_handler_->SetOutputMono(true); |
| 1920 | |
| 1921 | // Verify the output is in mono mode, OnOuputChannelRemixingChanged event |
| 1922 | // is fired. |
| 1923 | EXPECT_TRUE(cras_audio_handler_->IsOutputMonoEnabled()); |
| 1924 | EXPECT_EQ(1, test_observer_->output_channel_remixing_changed_count()); |
| 1925 | |
| 1926 | // Set output stereo |
| 1927 | cras_audio_handler_->SetOutputMono(false); |
| 1928 | EXPECT_FALSE(cras_audio_handler_->IsOutputMonoEnabled()); |
| 1929 | EXPECT_EQ(2, test_observer_->output_channel_remixing_changed_count()); |
| 1930 | } |
| 1931 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1932 | TEST_P(CrasAudioHandlerTest, SetOutputMute) { |
| 1933 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1934 | SetUpCrasAudioHandler(audio_nodes); |
| 1935 | EXPECT_EQ(0, test_observer_->output_mute_changed_count()); |
| 1936 | |
| 1937 | // Mute the device. |
| 1938 | cras_audio_handler_->SetOutputMute(true); |
| 1939 | |
| 1940 | // Verify the output is muted, OnOutputMuteChanged event is fired, |
| 1941 | // and mute value is saved in the preferences. |
| 1942 | EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); |
| 1943 | EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1944 | AudioDevice speaker(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1945 | EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker)); |
| 1946 | |
| 1947 | // Unmute the device. |
| 1948 | cras_audio_handler_->SetOutputMute(false); |
| 1949 | |
| 1950 | // Verify the output is unmuted, OnOutputMuteChanged event is fired, |
| 1951 | // and mute value is saved in the preferences. |
| 1952 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 1953 | EXPECT_EQ(2, test_observer_->output_mute_changed_count()); |
| 1954 | EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker)); |
| 1955 | } |
| 1956 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1957 | TEST_P(CrasAudioHandlerTest, SetInputMute) { |
| 1958 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1959 | SetUpCrasAudioHandler(audio_nodes); |
| 1960 | EXPECT_EQ(0, test_observer_->input_mute_changed_count()); |
| 1961 | |
| 1962 | // Mute the device. |
| 1963 | cras_audio_handler_->SetInputMute(true); |
| 1964 | |
[email protected] | f3dd296 | 2014-04-22 04:48:38 | [diff] [blame] | 1965 | // Verify the input is muted, OnInputMuteChanged event is fired. |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1966 | EXPECT_TRUE(cras_audio_handler_->IsInputMuted()); |
| 1967 | EXPECT_EQ(1, test_observer_->input_mute_changed_count()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1968 | |
| 1969 | // Unmute the device. |
| 1970 | cras_audio_handler_->SetInputMute(false); |
| 1971 | |
[email protected] | f3dd296 | 2014-04-22 04:48:38 | [diff] [blame] | 1972 | // Verify the input is unmuted, OnInputMuteChanged event is fired. |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1973 | EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); |
| 1974 | EXPECT_EQ(2, test_observer_->input_mute_changed_count()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1975 | } |
| 1976 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1977 | TEST_P(CrasAudioHandlerTest, SetOutputVolumePercent) { |
| 1978 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1979 | SetUpCrasAudioHandler(audio_nodes); |
| 1980 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 1981 | |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 1982 | const int kVolume = 60; |
| 1983 | cras_audio_handler_->SetOutputVolumePercent(kVolume); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1984 | |
| 1985 | // Verify the output volume is changed to the designated value, |
jennyz | 481292c | 2015-03-21 01:49:29 | [diff] [blame] | 1986 | // OnOutputNodeVolumeChanged event is fired, and the device volume value |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 1987 | // is saved in the preferences. |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1988 | EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 1989 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 1990 | AudioDevice device; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 1991 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1992 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 1993 | EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 1994 | } |
| 1995 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 1996 | TEST_P(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) { |
| 1997 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 1998 | SetUpCrasAudioHandler(audio_nodes); |
| 1999 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2000 | |
| 2001 | const int kVolume1 = 60; |
| 2002 | const int kVolume2 = 80; |
| 2003 | cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( |
| 2004 | kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); |
| 2005 | // Verify the output volume is changed to the designated value, |
| 2006 | // OnOutputNodeVolumeChanged event is not fired, and the device volume value |
| 2007 | // is saved in the preferences. |
| 2008 | EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); |
| 2009 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2010 | AudioDevice device; |
| 2011 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2012 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2013 | EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 2014 | |
| 2015 | // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make |
| 2016 | // sure everything is right. |
| 2017 | cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( |
| 2018 | kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); |
| 2019 | EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); |
| 2020 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2021 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2022 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2023 | EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 2024 | |
| 2025 | // Make a final SetOutputVolumePercent call to check if |
| 2026 | // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent |
| 2027 | // notifying observers. |
| 2028 | cras_audio_handler_->SetOutputVolumePercent(kVolume1); |
| 2029 | EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); |
| 2030 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2031 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2032 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2033 | EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 2034 | } |
| 2035 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2036 | TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasReady) { |
| 2037 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2038 | SetUpCrasAudioHandler(audio_nodes); |
| 2039 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2040 | |
| 2041 | const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent(); |
| 2042 | // Disable the auto OutputNodeVolumeChanged signal. |
| 2043 | fake_cras_audio_client_->set_notify_volume_change_with_delay(true); |
| 2044 | |
| 2045 | fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
| 2046 | RestartAudioClient(); |
| 2047 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2048 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2049 | |
| 2050 | // The correct initialization OutputNodeVolumeChanged event is fired. We |
| 2051 | // should avoid notifying observers. |
| 2052 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2053 | kInternalSpeaker->id, kDefaultVolume); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2054 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2055 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2056 | |
| 2057 | // The later OutputNodeVolumeChanged event after initialization should notify |
| 2058 | // observers. |
| 2059 | const int kVolume = 60; |
| 2060 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2061 | kInternalSpeaker->id, kVolume); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2062 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2063 | EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2064 | } |
| 2065 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2066 | TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasDropRequest) { |
| 2067 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2068 | SetUpCrasAudioHandler(audio_nodes); |
| 2069 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2070 | |
| 2071 | const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent(); |
| 2072 | // Disable the auto OutputNodeVolumeChanged signal. |
| 2073 | fake_cras_audio_client_->set_notify_volume_change_with_delay(true); |
| 2074 | |
| 2075 | fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
| 2076 | RestartAudioClient(); |
| 2077 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2078 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2079 | |
| 2080 | // A wrong initialization OutputNodeVolumeChanged event is fired. This may |
| 2081 | // happen when Cras is not ready and drops request. The approach we use is |
| 2082 | // to log warning message, clear the pending automated volume change reasons, |
| 2083 | // and notify observers about this change. |
| 2084 | const int kVolume1 = 30; |
| 2085 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2086 | kInternalSpeaker->id, kVolume1); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2087 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2088 | EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); |
| 2089 | |
| 2090 | // The later OutputNodeVolumeChanged event should notify observers. |
| 2091 | const int kVolume2 = 60; |
| 2092 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2093 | kInternalSpeaker->id, kVolume2); |
warx | fd7c37b | 2016-08-05 19:38:07 | [diff] [blame] | 2094 | EXPECT_EQ(2, test_observer_->output_volume_changed_count()); |
| 2095 | EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); |
| 2096 | } |
| 2097 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2098 | TEST_P(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) { |
| 2099 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2100 | SetUpCrasAudioHandler(audio_nodes); |
| 2101 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2102 | |
| 2103 | const int kDefaultVolume = 75; |
| 2104 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2105 | |
| 2106 | // Disable the auto OutputNodeVolumeChanged signal. |
| 2107 | fake_cras_audio_client_->set_notify_volume_change_with_delay(true); |
| 2108 | |
| 2109 | // Verify the volume state is not changed before OutputNodeVolumeChanged |
| 2110 | // signal fires. |
| 2111 | const int kVolume = 60; |
| 2112 | cras_audio_handler_->SetOutputVolumePercent(kVolume); |
| 2113 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2114 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2115 | |
| 2116 | // Verify the output volume is changed to the designated value after |
| 2117 | // OnOutputNodeVolumeChanged cras signal fires, and the volume change event |
| 2118 | // has been fired to notify the observers. |
| 2119 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2120 | kInternalSpeaker->id, kVolume); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2121 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2122 | EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2123 | AudioDevice device; |
| 2124 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2125 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2126 | EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 2127 | } |
| 2128 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2129 | TEST_P(CrasAudioHandlerTest, |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2130 | ChangeOutputVolumesWithDelayedSignalForSingleActiveDevice) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2131 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2132 | SetUpCrasAudioHandler(audio_nodes); |
| 2133 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2134 | |
| 2135 | const int kDefaultVolume = 75; |
| 2136 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2137 | |
| 2138 | // Disable the auto OutputNodeVolumeChanged signal. |
| 2139 | fake_cras_audio_client_->set_notify_volume_change_with_delay(true); |
| 2140 | |
| 2141 | // Verify the volume state is not changed before OutputNodeVolumeChanged |
| 2142 | // signal fires. |
| 2143 | const int kVolume1 = 50; |
| 2144 | const int kVolume2 = 60; |
| 2145 | cras_audio_handler_->SetOutputVolumePercent(kVolume1); |
| 2146 | cras_audio_handler_->SetOutputVolumePercent(kVolume2); |
| 2147 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2148 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2149 | |
| 2150 | // Simulate OutputNodeVolumeChanged signal fired with big latency that |
| 2151 | // it lags behind the SetOutputNodeVolume requests. Chrome sets the volume |
| 2152 | // to 50 then 60, but the volume changed signal for 50 comes back after |
| 2153 | // chrome sets the volume to 60. Verify chrome will sync to the designated |
| 2154 | // volume level after all signals arrive. |
| 2155 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2156 | kInternalSpeaker->id, kVolume1); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2157 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2158 | EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); |
| 2159 | |
| 2160 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2161 | kInternalSpeaker->id, kVolume2); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2162 | EXPECT_EQ(2, test_observer_->output_volume_changed_count()); |
| 2163 | EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); |
| 2164 | } |
| 2165 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2166 | TEST_P(CrasAudioHandlerTest, |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2167 | ChangeOutputVolumeFromNonChromeSourceSingleActiveDevice) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2168 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2169 | SetUpCrasAudioHandler(audio_nodes); |
| 2170 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2171 | |
| 2172 | const int kDefaultVolume = 75; |
| 2173 | EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 2174 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2175 | |
| 2176 | // Simulate OutputNodeVolumeChanged signal fired by a non-chrome source. |
| 2177 | // Verify chrome will sync its volume state to the volume from the signal, |
| 2178 | // and notify its observers for the volume change event. |
| 2179 | const int kVolume = 20; |
| 2180 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2181 | kInternalSpeaker->id, kVolume); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2182 | EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 2183 | EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2184 | AudioDevice device; |
| 2185 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2186 | EXPECT_EQ(device.id, kInternalSpeaker->id); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2187 | EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 2188 | } |
| 2189 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2190 | TEST_P(CrasAudioHandlerTest, SetInputGainPercent) { |
| 2191 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2192 | SetUpCrasAudioHandler(audio_nodes); |
| 2193 | EXPECT_EQ(0, test_observer_->input_gain_changed_count()); |
| 2194 | |
| 2195 | cras_audio_handler_->SetInputGainPercent(60); |
| 2196 | |
| 2197 | // Verify the input gain changed to the designated value, |
jennyz | 481292c | 2015-03-21 01:49:29 | [diff] [blame] | 2198 | // OnInputNodeGainChanged event is fired, and the device gain value |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2199 | // is saved in the preferences. |
| 2200 | const int kGain = 60; |
| 2201 | EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent()); |
| 2202 | EXPECT_EQ(1, test_observer_->input_gain_changed_count()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2203 | AudioDevice internal_mic(GenerateAudioNode(kInternalMic)); |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 2204 | EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2205 | } |
| 2206 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2207 | TEST_P(CrasAudioHandlerTest, SetMuteForDevice) { |
| 2208 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2209 | {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2210 | SetUpCrasAudioHandler(audio_nodes); |
| 2211 | |
| 2212 | // Mute the active output device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2213 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2214 | cras_audio_handler_->SetMuteForDevice(kHeadphone->id, true); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2215 | |
| 2216 | // Verify the headphone is muted and mute value is saved in the preferences. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2217 | EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone->id)); |
| 2218 | AudioDevice headphone(GenerateAudioNode(kHeadphone)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2219 | EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone)); |
| 2220 | |
| 2221 | // Mute the non-active output device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2222 | cras_audio_handler_->SetMuteForDevice(kInternalSpeaker->id, true); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2223 | |
| 2224 | // Verify the internal speaker is muted and mute value is saved in the |
| 2225 | // preferences. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2226 | EXPECT_TRUE( |
| 2227 | cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id)); |
| 2228 | AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2229 | EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker)); |
| 2230 | |
| 2231 | // Mute the active input device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2232 | EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2233 | cras_audio_handler_->SetMuteForDevice(kUSBMic->id, true); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2234 | |
[email protected] | f3dd296 | 2014-04-22 04:48:38 | [diff] [blame] | 2235 | // Verify the USB Mic is muted. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2236 | EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic->id)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2237 | |
[email protected] | f3dd296 | 2014-04-22 04:48:38 | [diff] [blame] | 2238 | // Mute the non-active input device should be a no-op, see crbug.com/365050. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2239 | cras_audio_handler_->SetMuteForDevice(kInternalMic->id, true); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2240 | |
[email protected] | f3dd296 | 2014-04-22 04:48:38 | [diff] [blame] | 2241 | // Verify IsInputMutedForDevice returns false for non-active input device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2242 | EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic->id)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2243 | } |
| 2244 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2245 | TEST_P(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) { |
| 2246 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2247 | {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2248 | SetUpCrasAudioHandler(audio_nodes); |
| 2249 | |
| 2250 | // Set volume percent for active output device. |
| 2251 | const int kHeadphoneVolume = 30; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2252 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2253 | cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone->id, |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2254 | kHeadphoneVolume); |
| 2255 | |
| 2256 | // Verify the volume percent of headphone is set, and saved in preferences. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2257 | EXPECT_EQ( |
| 2258 | kHeadphoneVolume, |
| 2259 | cras_audio_handler_->GetOutputVolumePercentForDevice(kHeadphone->id)); |
| 2260 | AudioDevice headphone(GenerateAudioNode(kHeadphone)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2261 | EXPECT_EQ(kHeadphoneVolume, |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 2262 | audio_pref_handler_->GetOutputVolumeValue(&headphone)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2263 | |
| 2264 | // Set volume percent for non-active output device. |
| 2265 | const int kSpeakerVolume = 60; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2266 | cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker->id, |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2267 | kSpeakerVolume); |
| 2268 | |
| 2269 | // Verify the volume percent of speaker is set, and saved in preferences. |
| 2270 | EXPECT_EQ(kSpeakerVolume, |
| 2271 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2272 | kInternalSpeaker->id)); |
| 2273 | AudioDevice speaker(GenerateAudioNode(kInternalSpeaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2274 | EXPECT_EQ(kSpeakerVolume, |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 2275 | audio_pref_handler_->GetOutputVolumeValue(&speaker)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2276 | |
| 2277 | // Set gain percent for active input device. |
| 2278 | const int kUSBMicGain = 30; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2279 | EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2280 | cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic->id, kUSBMicGain); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2281 | |
| 2282 | // Verify the gain percent of USB mic is set, and saved in preferences. |
| 2283 | EXPECT_EQ(kUSBMicGain, |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2284 | cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic->id)); |
| 2285 | AudioDevice usb_mic(GenerateAudioNode(kHeadphone)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2286 | EXPECT_EQ(kUSBMicGain, |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 2287 | audio_pref_handler_->GetInputGainValue(&usb_mic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2288 | |
| 2289 | // Set gain percent for non-active input device. |
| 2290 | const int kInternalMicGain = 60; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2291 | cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic->id, |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2292 | kInternalMicGain); |
| 2293 | |
| 2294 | // Verify the gain percent of internal mic is set, and saved in preferences. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2295 | EXPECT_EQ( |
| 2296 | kInternalMicGain, |
| 2297 | cras_audio_handler_->GetOutputVolumePercentForDevice(kInternalMic->id)); |
| 2298 | AudioDevice internal_mic(GenerateAudioNode(kInternalMic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2299 | EXPECT_EQ(kInternalMicGain, |
[email protected] | 140a3145 | 2013-09-09 18:54:37 | [diff] [blame] | 2300 | audio_pref_handler_->GetInputGainValue(&internal_mic)); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2301 | } |
| 2302 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2303 | TEST_P(CrasAudioHandlerTest, HandleOtherDeviceType) { |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2304 | const size_t kNumValidAudioDevices = 4; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2305 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2306 | {kInternalSpeaker, kOtherTypeOutput, kInternalMic, kOtherTypeInput}); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2307 | SetUpCrasAudioHandler(audio_nodes); |
| 2308 | |
| 2309 | // Verify the audio devices size. |
| 2310 | AudioDeviceList audio_devices; |
| 2311 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2312 | EXPECT_EQ(kNumValidAudioDevices, audio_devices.size()); |
| 2313 | |
| 2314 | // Verify the internal speaker has been selected as the active output, |
| 2315 | // and the output device with some randown unknown type is handled gracefully. |
| 2316 | AudioDevice active_output; |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2317 | EXPECT_TRUE( |
| 2318 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2319 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 2320 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2321 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2322 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 2323 | |
| 2324 | // Ensure the internal microphone has been selected as the active input, |
| 2325 | // and the input device with some random unknown type is handled gracefully. |
| 2326 | AudioDevice active_input; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2327 | EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 2328 | EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 2329 | } |
| 2330 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2331 | TEST_P(CrasAudioHandlerTest, ActiveDeviceSelectionWithStableDeviceId) { |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2332 | AudioNodeList audio_nodes; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2333 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2334 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2335 | AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1); |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2336 | usb_headset.plugged_time = 80000000; |
| 2337 | audio_nodes.push_back(usb_headset); |
| 2338 | SetUpCrasAudioHandler(audio_nodes); |
| 2339 | |
| 2340 | // Verify the audio devices size. |
| 2341 | AudioDeviceList audio_devices; |
| 2342 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2343 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2344 | |
| 2345 | // Initially active node is selected base on priority, so USB headphone |
| 2346 | // is selected. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2347 | EXPECT_EQ(kUSBHeadphone1->id, |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2348 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2349 | |
| 2350 | // Change the active device to internal speaker, now USB headphone becomes |
| 2351 | // inactive. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2352 | AudioDevice speaker(GenerateAudioNode(kInternalSpeaker)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2353 | cras_audio_handler_->SwitchToDevice(speaker, true, |
| 2354 | CrasAudioHandler::ACTIVATE_BY_USER); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2355 | EXPECT_NE(kUSBHeadphone1->id, |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2356 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2357 | |
| 2358 | // Unplug USB headset. |
| 2359 | audio_nodes.clear(); |
| 2360 | internal_speaker.active = true; |
| 2361 | audio_nodes.push_back(internal_speaker); |
| 2362 | ChangeAudioNodes(audio_nodes); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2363 | EXPECT_EQ(kInternalSpeaker->id, |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2364 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2365 | |
| 2366 | // Plug the same USB headset back, id is different, but stable_device_id |
| 2367 | // remains the same. |
| 2368 | usb_headset.active = false; |
| 2369 | usb_headset.id = 98765; |
| 2370 | audio_nodes.push_back(usb_headset); |
| 2371 | ChangeAudioNodes(audio_nodes); |
| 2372 | |
| 2373 | // Since USB headset was inactive before it was unplugged, it won't be |
| 2374 | // selected as active after it's plugged in again. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2375 | EXPECT_EQ(kInternalSpeaker->id, |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2376 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2377 | |
| 2378 | // Plug the second USB headset. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2379 | AudioNode usb_headset2 = GenerateAudioNode(kUSBHeadphone2); |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2380 | usb_headset2.plugged_time = 80000001; |
| 2381 | audio_nodes.push_back(usb_headset2); |
| 2382 | ChangeAudioNodes(audio_nodes); |
| 2383 | |
| 2384 | // Since the second USB device is new, it's selected as the active device |
| 2385 | // by its priority. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2386 | EXPECT_EQ(kUSBHeadphone2->id, |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2387 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2388 | |
| 2389 | // Unplug the second USB headset. |
| 2390 | audio_nodes.clear(); |
| 2391 | internal_speaker.active = false; |
| 2392 | audio_nodes.push_back(internal_speaker); |
| 2393 | audio_nodes.push_back(usb_headset); |
| 2394 | ChangeAudioNodes(audio_nodes); |
| 2395 | |
| 2396 | // There is no active node after USB2 unplugged, the 1st USB got selected |
| 2397 | // by its priority. |
| 2398 | EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2399 | |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2400 | audio_nodes.clear(); |
| 2401 | internal_speaker.active = false; |
| 2402 | audio_nodes.push_back(internal_speaker); |
| 2403 | usb_headset.active = true; |
| 2404 | audio_nodes.push_back(usb_headset); |
hychao | fe100d54 | 2016-01-12 10:17:52 | [diff] [blame] | 2405 | usb_headset2.active = false; |
| 2406 | usb_headset2.plugged_time = 80000002; |
| 2407 | audio_nodes.push_back(usb_headset2); |
| 2408 | ChangeAudioNodes(audio_nodes); |
| 2409 | |
| 2410 | // Plug the second USB again. Since it was the active node before it got |
| 2411 | // unplugged, it is now selected as the active node. |
| 2412 | EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2413 | } |
| 2414 | |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2415 | // Test the device new session case, either via reboot or logout, if there |
| 2416 | // is an active device in the previous session, that device should still |
| 2417 | // be set as active after the new session starts. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2418 | TEST_P(CrasAudioHandlerTest, PersistActiveDeviceAcrossSession) { |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2419 | // Set the active device to internal speaker before the session starts. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2420 | AudioNodeList audio_nodes = |
| 2421 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2422 | SetupCrasAudioHandlerWithActiveNodeInPref( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2423 | audio_nodes, audio_nodes, |
| 2424 | AudioDevice(GenerateAudioNode(kInternalSpeaker)), true); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2425 | |
| 2426 | // Verify the audio devices size. |
| 2427 | AudioDeviceList audio_devices; |
| 2428 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2429 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2430 | |
| 2431 | // Verify the active device is the internal speaker, which is of a lower |
| 2432 | // priority, but selected as active since it was the active device previously. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2433 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2434 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2435 | } |
| 2436 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2437 | TEST_P(CrasAudioHandlerTest, PersistActiveSpeakerAcrossReboot) { |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2438 | // Simulates the device was shut down with three audio devices, and |
| 2439 | // internal speaker being the active one selected by user. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2440 | AudioNodeList audio_nodes_in_pref = |
| 2441 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2442 | |
| 2443 | // Simulate the first NodesChanged signal coming with only one node. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2444 | AudioNodeList audio_nodes = GenerateAudioNodeList({kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2445 | |
| 2446 | SetupCrasAudioHandlerWithActiveNodeInPref( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2447 | audio_nodes, audio_nodes_in_pref, |
| 2448 | AudioDevice(GenerateAudioNode(kInternalSpeaker)), true); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2449 | |
| 2450 | // Verify the usb headphone has been made active. |
| 2451 | AudioDeviceList audio_devices; |
| 2452 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2453 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2454 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2455 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2456 | |
| 2457 | // Simulate another NodesChanged signal coming later with all ndoes. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2458 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 2459 | audio_nodes.push_back(GenerateAudioNode(kHeadphone)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2460 | ChangeAudioNodes(audio_nodes); |
| 2461 | |
| 2462 | // Verify the active output has been restored to internal speaker. |
| 2463 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2464 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2465 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2466 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2467 | } |
| 2468 | |
jennyz | 16e103e | 2017-03-31 18:15:25 | [diff] [blame] | 2469 | // crbug.com/698809. User plug in USB speaker, then unplug it, leave |
| 2470 | // internal speaker as active device. Power down, plug in USB speaker again. |
| 2471 | // When the device powers up again, the first NodesChanged signal comes with |
| 2472 | // only USB speaker; followed by another NodesChanged signal with internal |
| 2473 | // speaker added. |
| 2474 | TEST_P(CrasAudioHandlerTest, USBShouldBeActiveAfterReboot) { |
| 2475 | // Start with both interanl speaker and USB speaker. |
| 2476 | AudioNodeList audio_nodes = |
| 2477 | GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1}); |
| 2478 | |
| 2479 | SetUpCrasAudioHandler(audio_nodes); |
| 2480 | |
| 2481 | // Verify the usb headphone has been made active by priority. |
| 2482 | AudioDeviceList audio_devices; |
| 2483 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2484 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2485 | EXPECT_EQ(kUSBHeadphone1->id, |
| 2486 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2487 | |
| 2488 | // Remove USB headphone. |
| 2489 | audio_nodes.clear(); |
| 2490 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 2491 | ChangeAudioNodes(audio_nodes); |
| 2492 | // Verify the internal speaker becomes the active device by priority. |
| 2493 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2494 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2495 | EXPECT_EQ(kInternalSpeaker->id, |
| 2496 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2497 | |
| 2498 | // Simulate after power off, plug in usb header phone, then power on. |
| 2499 | // The first NodesChanged signal sends usb headphone only. |
| 2500 | audio_nodes.clear(); |
| 2501 | audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1)); |
| 2502 | ChangeAudioNodes(audio_nodes); |
| 2503 | // Verify the usb headerphone becomes the active device by priority. |
| 2504 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2505 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2506 | EXPECT_EQ(kUSBHeadphone1->id, |
| 2507 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2508 | |
| 2509 | // Simulate the second NodesChanged signal comes with internal speaker added. |
| 2510 | audio_nodes.clear(); |
| 2511 | AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1); |
| 2512 | usb_headphone.active = true; |
| 2513 | audio_nodes.push_back(usb_headphone); |
| 2514 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 2515 | ChangeAudioNodes(audio_nodes); |
| 2516 | // Verify the usb headerphone is still the active device. |
| 2517 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2518 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2519 | EXPECT_EQ(kUSBHeadphone1->id, |
| 2520 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2521 | } |
| 2522 | |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2523 | // Test the corner case that headphone is plugged in for the first time on |
| 2524 | // a cros device after the device is shutdown. |
| 2525 | // crbug.com/622045. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2526 | TEST_P(CrasAudioHandlerTest, PlugInHeadphoneFirstTimeAfterPowerDown) { |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2527 | // Simulate plugging headphone for the first on a cros device after it is |
| 2528 | // powered down. Internal speaker is set up in audio prefs as active |
| 2529 | // before the new cros session starts. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2530 | AudioNodeList audio_nodes_in_pref = GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2531 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2532 | AudioNodeList audio_nodes = |
| 2533 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2534 | |
| 2535 | SetupCrasAudioHandlerWithActiveNodeInPref( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2536 | audio_nodes, audio_nodes_in_pref, |
| 2537 | AudioDevice(GenerateAudioNode(kInternalSpeaker)), false); |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2538 | |
| 2539 | // Verify the audio devices size. |
| 2540 | AudioDeviceList audio_devices; |
| 2541 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2542 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2543 | |
| 2544 | // Verify headphone becomes the active output. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2545 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | 6d5579b | 2016-07-28 17:49:11 | [diff] [blame] | 2546 | } |
| 2547 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2548 | TEST_P(CrasAudioHandlerTest, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2549 | PersistActiveUsbHeadphoneAcrossRebootUsbComeLater) { |
| 2550 | // Simulates the device was shut down with three audio devices, and |
| 2551 | // usb headphone being the active one selected by priority. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2552 | AudioNodeList audio_nodes_in_pref = |
| 2553 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2554 | |
| 2555 | // Simulate the first NodesChanged signal coming with only internal speaker |
| 2556 | // and the headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2557 | AudioNodeList audio_nodes = |
| 2558 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2559 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2560 | SetupCrasAudioHandlerWithActiveNodeInPref( |
| 2561 | audio_nodes, audio_nodes_in_pref, |
| 2562 | AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2563 | |
| 2564 | // Verify the headphone has been made active. |
| 2565 | AudioDeviceList audio_devices; |
| 2566 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2567 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2568 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2569 | |
| 2570 | // Simulate USB node comes later with all ndoes. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2571 | AudioNode usb_node = GenerateAudioNode(kUSBHeadphone1); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2572 | usb_node.plugged_time = 80000000; |
| 2573 | audio_nodes.push_back(usb_node); |
| 2574 | ChangeAudioNodes(audio_nodes); |
| 2575 | |
| 2576 | // Verify the active output has been restored to usb headphone. |
| 2577 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2578 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2579 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2580 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2581 | } |
| 2582 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2583 | TEST_P(CrasAudioHandlerTest, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2584 | PersistActiveUsbHeadphoneAcrossRebootUsbComeFirst) { |
| 2585 | // Simulates the device was shut down with three audio devices, and |
| 2586 | // usb headphone being the active one selected by priority. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2587 | AudioNodeList audio_nodes_in_pref = |
| 2588 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2589 | |
| 2590 | // Simulate the first NodesChanged signal coming with only internal speaker |
| 2591 | // and the USB headphone. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2592 | AudioNodeList audio_nodes = |
| 2593 | GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2594 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2595 | SetupCrasAudioHandlerWithActiveNodeInPref( |
| 2596 | audio_nodes, audio_nodes_in_pref, |
| 2597 | AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2598 | |
| 2599 | // Verify the USB headphone has been made active. |
| 2600 | AudioDeviceList audio_devices; |
| 2601 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2602 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2603 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2604 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2605 | |
| 2606 | // Simulate another NodesChanged signal coming later with all ndoes. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2607 | AudioNode headphone_node = GenerateAudioNode(kHeadphone); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2608 | headphone_node.plugged_time = 80000000; |
| 2609 | audio_nodes.push_back(headphone_node); |
| 2610 | ChangeAudioNodes(audio_nodes); |
| 2611 | |
| 2612 | // Verify the active output has been restored to USB headphone. |
| 2613 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2614 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2615 | EXPECT_EQ(kUSBHeadphone1->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2616 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2617 | } |
| 2618 | |
| 2619 | // This covers the crbug.com/586026. Cras lost the active state of the internal |
| 2620 | // speaker when user unplugs the headphone, which is a bug in cras. However, |
| 2621 | // chrome code is still resilient and set the internal speaker back to active. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2622 | TEST_P(CrasAudioHandlerTest, UnplugHeadphoneLostActiveInternalSpeakerByCras) { |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2623 | // Set up with three nodes. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2624 | AudioNodeList audio_nodes = |
| 2625 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2626 | SetUpCrasAudioHandler(audio_nodes); |
| 2627 | |
| 2628 | // Switch the active output to internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2629 | cras_audio_handler_->SwitchToDevice( |
| 2630 | AudioDevice(GenerateAudioNode(kInternalSpeaker)), true, |
| 2631 | CrasAudioHandler::ACTIVATE_BY_USER); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2632 | |
| 2633 | // Verify internal speaker has been made active. |
| 2634 | AudioDeviceList audio_devices; |
| 2635 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2636 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2637 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2638 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2639 | |
| 2640 | // Simulate unplug the headphone. Cras sends NodesChanged signal with |
| 2641 | // both internal speaker and usb headphone being inactive. |
| 2642 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2643 | audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker)); |
| 2644 | audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1)); |
| 2645 | for (const auto& node : audio_nodes) |
| 2646 | ASSERT_FALSE(node.active) << node.id << " expexted to be inactive"; |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2647 | ChangeAudioNodes(audio_nodes); |
| 2648 | |
| 2649 | // Verify the active output is set back to internal speaker. |
| 2650 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2651 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2652 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2653 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2654 | } |
| 2655 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2656 | TEST_P(CrasAudioHandlerTest, RemoveNonActiveDevice) { |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2657 | // Set up with three nodes. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2658 | AudioNodeList audio_nodes = |
| 2659 | GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1}); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2660 | SetUpCrasAudioHandler(audio_nodes); |
| 2661 | |
| 2662 | // Switch the active output to internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2663 | cras_audio_handler_->SwitchToDevice( |
| 2664 | AudioDevice(GenerateAudioNode(kInternalSpeaker)), true, |
| 2665 | CrasAudioHandler::ACTIVATE_BY_USER); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2666 | |
| 2667 | // Verify internal speaker has been made active. |
| 2668 | AudioDeviceList audio_devices; |
| 2669 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2670 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2671 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2672 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2673 | |
| 2674 | // Remove headphone, which is an non-active device. |
| 2675 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2676 | AudioNode speaker = GenerateAudioNode(kInternalSpeaker); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2677 | speaker.active = true; |
| 2678 | audio_nodes.push_back(speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2679 | AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2680 | usb_headphone.active = false; |
| 2681 | audio_nodes.push_back(usb_headphone); |
| 2682 | |
| 2683 | ChangeAudioNodes(audio_nodes); |
| 2684 | |
| 2685 | // Verify the active output remains as internal speaker. |
| 2686 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2687 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2688 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 2689 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2690 | } |
| 2691 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2692 | TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 2693 | // This simulates a typical hotrod audio device configuration. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2694 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2695 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2, |
| 2696 | kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput}); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2697 | SetUpCrasAudioHandler(audio_nodes); |
| 2698 | |
| 2699 | // Verify the audio devices size. |
| 2700 | AudioDeviceList audio_devices; |
| 2701 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2702 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2703 | |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2704 | // Verify only the 1st jabra speaker's output and input are selected as active |
| 2705 | // nodes by CrasAudioHandler. |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2706 | AudioDevice active_output; |
| 2707 | EXPECT_TRUE( |
| 2708 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2709 | EXPECT_EQ(2, GetActiveDeviceCount()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2710 | AudioDevice primary_active_device; |
| 2711 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice( |
| 2712 | &primary_active_device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2713 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id); |
| 2714 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2715 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2716 | |
| 2717 | // Set both jabra speakers's input and output nodes to active, this simulate |
| 2718 | // the call sent by hotrod initialization process. |
| 2719 | test_observer_->reset_active_output_node_changed_count(); |
| 2720 | test_observer_->reset_active_input_node_changed_count(); |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 2721 | cras_audio_handler_->ChangeActiveNodes( |
| 2722 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id, |
| 2723 | kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2724 | |
| 2725 | // Verify both jabra speakers' input/output nodes are made active. |
| 2726 | // num_active_nodes = GetActiveDeviceCount(); |
| 2727 | EXPECT_EQ(4, GetActiveDeviceCount()); |
| 2728 | const AudioDevice* active_output_1 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2729 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2730 | EXPECT_TRUE(active_output_1->active); |
| 2731 | const AudioDevice* active_output_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2732 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2733 | EXPECT_TRUE(active_output_2->active); |
| 2734 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice( |
| 2735 | &primary_active_device)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2736 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2737 | const AudioDevice* active_input_1 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2738 | GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2739 | EXPECT_TRUE(active_input_1->active); |
| 2740 | const AudioDevice* active_input_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2741 | GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2742 | EXPECT_TRUE(active_input_2->active); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2743 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2744 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2745 | |
| 2746 | // Verify only 1 ActiveOutputNodeChanged notification has been sent out |
| 2747 | // by calling ChangeActiveNodes. |
| 2748 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
| 2749 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2750 | |
| 2751 | // Verify all active devices are the not muted and their volume values are |
| 2752 | // the same. |
| 2753 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2754 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2755 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2756 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2757 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2758 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2759 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2760 | kUSBJabraSpeakerOutput1->id)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2761 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2762 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2763 | kUSBJabraSpeakerOutput2->id)); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2764 | |
| 2765 | // Adjust the volume of output devices, verify all active nodes are set to |
| 2766 | // the same volume. |
| 2767 | cras_audio_handler_->SetOutputVolumePercent(25); |
| 2768 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent()); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2769 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2770 | kUSBJabraSpeakerOutput1->id)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2771 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2772 | kUSBJabraSpeakerOutput2->id)); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2773 | } |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 2774 | |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 2775 | TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInit) { |
| 2776 | // This simulates a typical hotrod audio device configuration. |
| 2777 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2778 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2, |
| 2779 | kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput}); |
| 2780 | SetUpCrasAudioHandler(audio_nodes); |
| 2781 | |
| 2782 | // Verify the audio devices size. |
| 2783 | AudioDeviceList audio_devices; |
| 2784 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2785 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2786 | |
| 2787 | // Verify only the 1st jabra speaker's output and input are selected as active |
| 2788 | // nodes by CrasAudioHandler. |
| 2789 | AudioDevice active_output; |
| 2790 | EXPECT_TRUE( |
| 2791 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 2792 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 2793 | AudioDevice primary_active_device; |
| 2794 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice( |
| 2795 | &primary_active_device)); |
| 2796 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id); |
| 2797 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 2798 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2799 | |
| 2800 | // Set both jabra speakers's input and output nodes to active, this simulate |
| 2801 | // the call sent by hotrod initialization process. |
| 2802 | test_observer_->reset_active_output_node_changed_count(); |
| 2803 | test_observer_->reset_active_input_node_changed_count(); |
| 2804 | |
| 2805 | cras_audio_handler_->SetActiveInputNodes( |
| 2806 | {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
| 2807 | |
| 2808 | cras_audio_handler_->SetActiveOutputNodes( |
| 2809 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id}); |
| 2810 | |
| 2811 | // Verify both jabra speakers' input/output nodes are made active. |
| 2812 | // num_active_nodes = GetActiveDeviceCount(); |
| 2813 | EXPECT_EQ(4, GetActiveDeviceCount()); |
| 2814 | const AudioDevice* active_output_1 = |
| 2815 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
| 2816 | EXPECT_TRUE(active_output_1->active); |
| 2817 | const AudioDevice* active_output_2 = |
| 2818 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
| 2819 | EXPECT_TRUE(active_output_2->active); |
| 2820 | EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice( |
| 2821 | &primary_active_device)); |
| 2822 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id); |
| 2823 | const AudioDevice* active_input_1 = |
| 2824 | GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
| 2825 | EXPECT_TRUE(active_input_1->active); |
| 2826 | const AudioDevice* active_input_2 = |
| 2827 | GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
| 2828 | EXPECT_TRUE(active_input_2->active); |
| 2829 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 2830 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2831 | |
| 2832 | // Verify only 1 ActiveOutputNodeChanged notification has been sent out |
| 2833 | // by calling SetActiveNodes. |
| 2834 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
| 2835 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
| 2836 | |
| 2837 | // Verify all active devices are the not muted and their volume values are |
| 2838 | // the same. |
| 2839 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 2840 | EXPECT_FALSE( |
| 2841 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id)); |
| 2842 | EXPECT_FALSE( |
| 2843 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id)); |
| 2844 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2845 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 2846 | kUSBJabraSpeakerOutput1->id)); |
| 2847 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2848 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 2849 | kUSBJabraSpeakerOutput2->id)); |
| 2850 | |
| 2851 | // Adjust the volume of output devices, verify all active nodes are set to |
| 2852 | // the same volume. |
| 2853 | cras_audio_handler_->SetOutputVolumePercent(25); |
| 2854 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent()); |
| 2855 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 2856 | kUSBJabraSpeakerOutput1->id)); |
| 2857 | EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( |
| 2858 | kUSBJabraSpeakerOutput2->id)); |
| 2859 | } |
| 2860 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2861 | TEST_P(CrasAudioHandlerTest, ChangeVolumeHotrodDualSpeakersWithDelayedSignals) { |
| 2862 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2863 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2}); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2864 | SetUpCrasAudioHandler(audio_nodes); |
| 2865 | |
| 2866 | // Verify the audio devices size. |
| 2867 | AudioDeviceList audio_devices; |
| 2868 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2869 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2870 | |
| 2871 | // Set both jabra speakers nodes to active, this simulate |
| 2872 | // the call sent by hotrod initialization process. |
| 2873 | test_observer_->reset_active_output_node_changed_count(); |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 2874 | cras_audio_handler_->ChangeActiveNodes( |
| 2875 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id}); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2876 | |
| 2877 | // Verify both jabra speakers are made active. |
| 2878 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 2879 | const AudioDevice* active_output_1 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2880 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2881 | EXPECT_TRUE(active_output_1->active); |
| 2882 | const AudioDevice* active_output_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2883 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2884 | EXPECT_TRUE(active_output_2->active); |
| 2885 | |
| 2886 | // Verify all active devices are the not muted and their volume values are |
| 2887 | // the same. |
| 2888 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 2889 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2890 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2891 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2892 | cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2893 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2894 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2895 | kUSBJabraSpeakerOutput1->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2896 | EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(), |
| 2897 | cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2898 | kUSBJabraSpeakerOutput2->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2899 | const int kDefaultVolume = 75; |
| 2900 | EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 2901 | |
| 2902 | // Disable the auto OutputNodeVolumeChanged signal. |
| 2903 | fake_cras_audio_client_->set_notify_volume_change_with_delay(true); |
| 2904 | test_observer_->reset_output_volume_changed_count(); |
| 2905 | |
| 2906 | // Adjust the volume of output devices continuously. |
| 2907 | cras_audio_handler_->SetOutputVolumePercent(20); |
| 2908 | cras_audio_handler_->SetOutputVolumePercent(30); |
| 2909 | |
| 2910 | // Sends delayed OutputNodeVolumeChanged signals. |
| 2911 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2912 | kUSBJabraSpeakerOutput2->id, 20); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2913 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2914 | kUSBJabraSpeakerOutput1->id, 20); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2915 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2916 | kUSBJabraSpeakerOutput2->id, 30); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2917 | fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2918 | kUSBJabraSpeakerOutput1->id, 30); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2919 | |
| 2920 | // Verify that both speakers are set to the designated volume level after |
| 2921 | // receiving all delayed signals. |
| 2922 | EXPECT_EQ(4, test_observer_->output_volume_changed_count()); |
| 2923 | EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercent()); |
| 2924 | EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2925 | kUSBJabraSpeakerOutput1->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2926 | EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2927 | kUSBJabraSpeakerOutput2->id)); |
jennyz | 96526d0 | 2016-06-22 17:03:43 | [diff] [blame] | 2928 | } |
| 2929 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2930 | TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) { |
| 2931 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2932 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2, |
| 2933 | kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2934 | // Make the camera input to be plugged in later than jabra's input. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2935 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2936 | usb_camera.plugged_time = 10000000; |
| 2937 | audio_nodes.push_back(usb_camera); |
| 2938 | SetUpCrasAudioHandler(audio_nodes); |
| 2939 | |
| 2940 | // Verify the audio devices size. |
| 2941 | AudioDeviceList audio_devices; |
| 2942 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2943 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2944 | |
| 2945 | // Verify the 1st jabra speaker's output is selected as active output |
| 2946 | // node and camera's input is selected active input by CrasAudioHandler. |
| 2947 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2948 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2949 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 2950 | EXPECT_EQ(kUSBCameraInput->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 2951 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2952 | |
| 2953 | // Set both jabra speakers's input and output nodes to active, this simulates |
| 2954 | // the call sent by hotrod initialization process. |
| 2955 | test_observer_->reset_active_output_node_changed_count(); |
| 2956 | test_observer_->reset_active_input_node_changed_count(); |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 2957 | cras_audio_handler_->ChangeActiveNodes( |
| 2958 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id, |
| 2959 | kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
| 2960 | |
| 2961 | // Verify both jabra speakers' input/output nodes are made active. |
| 2962 | // num_active_nodes = GetActiveDeviceCount(); |
| 2963 | EXPECT_EQ(4, GetActiveDeviceCount()); |
| 2964 | const AudioDevice* active_output_1 = |
| 2965 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
| 2966 | EXPECT_TRUE(active_output_1->active); |
| 2967 | const AudioDevice* active_output_2 = |
| 2968 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
| 2969 | EXPECT_TRUE(active_output_2->active); |
| 2970 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 2971 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2972 | const AudioDevice* active_input_1 = |
| 2973 | GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
| 2974 | EXPECT_TRUE(active_input_1->active); |
| 2975 | const AudioDevice* active_input_2 = |
| 2976 | GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
| 2977 | EXPECT_TRUE(active_input_2->active); |
| 2978 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 2979 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2980 | |
| 2981 | // Verify only 1 ActiveOutputNodeChanged notification has been sent out |
| 2982 | // by calling ChangeActiveNodes. |
| 2983 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
| 2984 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
| 2985 | } |
| 2986 | |
| 2987 | TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInitWithCameraInputActive) { |
| 2988 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 2989 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2, |
| 2990 | kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2}); |
| 2991 | // Make the camera input to be plugged in later than jabra's input. |
| 2992 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
| 2993 | usb_camera.plugged_time = 10000000; |
| 2994 | audio_nodes.push_back(usb_camera); |
| 2995 | SetUpCrasAudioHandler(audio_nodes); |
| 2996 | |
| 2997 | // Verify the audio devices size. |
| 2998 | AudioDeviceList audio_devices; |
| 2999 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3000 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3001 | |
| 3002 | // Verify the 1st jabra speaker's output is selected as active output |
| 3003 | // node and camera's input is selected active input by CrasAudioHandler. |
| 3004 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3005 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3006 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3007 | EXPECT_EQ(kUSBCameraInput->id, |
| 3008 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3009 | |
| 3010 | // Set both jabra speakers's input and output nodes to active, this simulates |
| 3011 | // the call sent by hotrod initialization process. |
| 3012 | test_observer_->reset_active_output_node_changed_count(); |
| 3013 | test_observer_->reset_active_input_node_changed_count(); |
| 3014 | |
| 3015 | cras_audio_handler_->SetActiveOutputNodes( |
| 3016 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id}); |
| 3017 | |
| 3018 | cras_audio_handler_->SetActiveInputNodes( |
| 3019 | {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3020 | |
| 3021 | // Verify both jabra speakers' input/output nodes are made active. |
| 3022 | // num_active_nodes = GetActiveDeviceCount(); |
| 3023 | EXPECT_EQ(4, GetActiveDeviceCount()); |
| 3024 | const AudioDevice* active_output_1 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3025 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3026 | EXPECT_TRUE(active_output_1->active); |
| 3027 | const AudioDevice* active_output_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3028 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3029 | EXPECT_TRUE(active_output_2->active); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3030 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3031 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3032 | const AudioDevice* active_input_1 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3033 | GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3034 | EXPECT_TRUE(active_input_1->active); |
| 3035 | const AudioDevice* active_input_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3036 | GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3037 | EXPECT_TRUE(active_input_2->active); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3038 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3039 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3040 | |
| 3041 | // Verify only 1 ActiveOutputNodeChanged notification has been sent out |
| 3042 | // by calling ChangeActiveNodes. |
| 3043 | EXPECT_EQ(1, test_observer_->active_output_node_changed_count()); |
| 3044 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
| 3045 | } |
| 3046 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3047 | TEST_P(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) { |
| 3048 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3049 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3050 | SetUpCrasAudioHandler(audio_nodes); |
| 3051 | |
| 3052 | // Verify the audio devices size. |
| 3053 | AudioDeviceList audio_devices; |
| 3054 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3055 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3056 | |
| 3057 | // Set all three nodes to be active. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3058 | cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id, |
| 3059 | kUSBJabraSpeakerOutput1->id, |
| 3060 | kUSBJabraSpeakerOutput2->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3061 | |
| 3062 | // Verify all three nodes are active. |
| 3063 | EXPECT_EQ(3, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3064 | const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3065 | EXPECT_TRUE(active_output_1->active); |
| 3066 | const AudioDevice* active_output_2 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3067 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3068 | EXPECT_TRUE(active_output_2->active); |
| 3069 | const AudioDevice* active_output_3 = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3070 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3071 | EXPECT_TRUE(active_output_3->active); |
| 3072 | |
| 3073 | // Now call ChangeActiveDevices with only 2 nodes. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3074 | cras_audio_handler_->ChangeActiveNodes( |
| 3075 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id}); |
| 3076 | |
| 3077 | // Verify only 2 nodes are active. |
| 3078 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3079 | const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id); |
| 3080 | EXPECT_FALSE(output_1->active); |
| 3081 | const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
| 3082 | EXPECT_TRUE(output_2->active); |
| 3083 | const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
| 3084 | EXPECT_TRUE(output_3->active); |
| 3085 | } |
| 3086 | |
| 3087 | TEST_P(CrasAudioHandlerTest, SetActiveNodesWithFewerActives) { |
| 3088 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3089 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2}); |
| 3090 | SetUpCrasAudioHandler(audio_nodes); |
| 3091 | |
| 3092 | // Verify the audio devices size. |
| 3093 | AudioDeviceList audio_devices; |
| 3094 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3095 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3096 | |
| 3097 | // Set all three nodes to be active. |
| 3098 | cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id, |
| 3099 | kUSBJabraSpeakerOutput1->id, |
| 3100 | kUSBJabraSpeakerOutput2->id}); |
| 3101 | |
| 3102 | // Verify all three nodes are active. |
| 3103 | EXPECT_EQ(3, GetActiveDeviceCount()); |
| 3104 | const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id); |
| 3105 | EXPECT_TRUE(active_output_1->active); |
| 3106 | const AudioDevice* active_output_2 = |
| 3107 | GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
| 3108 | EXPECT_TRUE(active_output_2->active); |
| 3109 | const AudioDevice* active_output_3 = |
| 3110 | GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
| 3111 | EXPECT_TRUE(active_output_3->active); |
| 3112 | |
| 3113 | // Now call SetActiveOutputNodes with only 2 nodes. |
| 3114 | cras_audio_handler_->SetActiveOutputNodes( |
| 3115 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3116 | |
| 3117 | // Verify only 2 nodes are active. |
| 3118 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3119 | const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3120 | EXPECT_FALSE(output_1->active); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3121 | const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3122 | EXPECT_TRUE(output_2->active); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3123 | const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3124 | EXPECT_TRUE(output_3->active); |
| 3125 | } |
| 3126 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3127 | TEST_P(CrasAudioHandlerTest, HotrodInitWithSingleJabra) { |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3128 | // Simulates the hotrod initializated with a single jabra device and |
| 3129 | // CrasAudioHandler selected jabra input/output as active devices. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3130 | AudioNodeList audio_nodes = |
| 3131 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3132 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3133 | SetUpCrasAudioHandler(audio_nodes); |
| 3134 | |
| 3135 | // Verify the audio devices size. |
| 3136 | AudioDeviceList audio_devices; |
| 3137 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3138 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3139 | |
| 3140 | // Verify the jabra speaker's output and input are selected as active nodes |
| 3141 | // by CrasAudioHandler. |
| 3142 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3143 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3144 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3145 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3146 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3147 | } |
| 3148 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3149 | TEST_P(CrasAudioHandlerTest, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3150 | ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3151 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3152 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1}); |
| 3153 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3154 | usb_camera.plugged_time = 10000000; |
| 3155 | audio_nodes.push_back(usb_camera); |
| 3156 | SetUpCrasAudioHandler(audio_nodes); |
| 3157 | |
| 3158 | // Verify the audio devices size. |
| 3159 | AudioDeviceList audio_devices; |
| 3160 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3161 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3162 | |
| 3163 | // Verify the jabra speaker's output is selected as active output, and |
| 3164 | // camera's input is selected as active input by CrasAudioHandler |
| 3165 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3166 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3167 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3168 | EXPECT_EQ(kUSBCameraInput->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3169 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3170 | |
| 3171 | // Simulate hotrod app call to set jabra input as active device with only |
| 3172 | // jabra input node in the active node list, which does not conform to the |
| 3173 | // new SetActiveDevices protocol, but just show we can still handle it if |
| 3174 | // this happens. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3175 | cras_audio_handler_->ChangeActiveNodes( |
| 3176 | {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerInput1->id}); |
| 3177 | |
| 3178 | // Verify the jabra speaker's output is selected as active output, and |
| 3179 | // jabra's input is selected as active input. |
| 3180 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3181 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3182 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3183 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3184 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3185 | } |
| 3186 | |
| 3187 | TEST_P(CrasAudioHandlerTest, |
| 3188 | SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) { |
| 3189 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3190 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1}); |
| 3191 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
| 3192 | usb_camera.plugged_time = 10000000; |
| 3193 | audio_nodes.push_back(usb_camera); |
| 3194 | SetUpCrasAudioHandler(audio_nodes); |
| 3195 | |
| 3196 | // Verify the audio devices size. |
| 3197 | AudioDeviceList audio_devices; |
| 3198 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3199 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3200 | |
| 3201 | // Verify the jabra speaker's output is selected as active output, and |
| 3202 | // camera's input is selected as active input by CrasAudioHandler |
| 3203 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3204 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3205 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3206 | EXPECT_EQ(kUSBCameraInput->id, |
| 3207 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3208 | |
| 3209 | // Simulate hotrod app call to set jabra input as active device with only |
| 3210 | // jabra input node in the active node list, which does not conform to the |
| 3211 | // new SetActiveDevices protocol, but just show we can still handle it if |
| 3212 | // this happens. |
| 3213 | cras_audio_handler_->SetActiveOutputNodes({kUSBJabraSpeakerOutput1->id}); |
| 3214 | |
| 3215 | cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3216 | |
| 3217 | // Verify the jabra speaker's output is selected as active output, and |
| 3218 | // jabra's input is selected as active input. |
| 3219 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3220 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3221 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3222 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3223 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3224 | } |
| 3225 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3226 | TEST_P(CrasAudioHandlerTest, ChangeActiveNodesDeactivatePrimaryActiveNode) { |
| 3227 | AudioNodeList audio_nodes = |
| 3228 | GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2}); |
| 3229 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3230 | usb_camera.plugged_time = 10000000; |
| 3231 | audio_nodes.push_back(usb_camera); |
| 3232 | SetUpCrasAudioHandler(audio_nodes); |
| 3233 | |
| 3234 | // Verify the audio devices size. |
| 3235 | AudioDeviceList audio_devices; |
| 3236 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3237 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3238 | |
| 3239 | // Verify the camera's input is selected as active input by CrasAudioHandler |
| 3240 | EXPECT_EQ(1, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3241 | EXPECT_EQ(kUSBCameraInput->id, |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3242 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3243 | |
| 3244 | // Simulate hotrod app call to set jabra input as active device with only |
| 3245 | // jabra input node in the active node list, which does not conform to the |
| 3246 | // new SetActiveDevices protocol, but just show we can still handle it if |
| 3247 | // this happens. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3248 | cras_audio_handler_->ChangeActiveNodes( |
| 3249 | {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id}); |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3250 | |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3251 | // Verify active input devices are set as expected, with primary active input |
| 3252 | // staying the same. |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3253 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3254 | EXPECT_EQ(kUSBCameraInput->id, |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3255 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3256 | |
| 3257 | const AudioDevice* additional_speaker = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3258 | cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3259 | ASSERT_TRUE(additional_speaker); |
| 3260 | EXPECT_TRUE(additional_speaker->active); |
| 3261 | |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3262 | // Update active device list so previously primary active device is not |
| 3263 | // active anymore. |
| 3264 | cras_audio_handler_->ChangeActiveNodes( |
| 3265 | {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3266 | |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3267 | // Verify that list of active devices is correctly set, and that a new primary |
| 3268 | // active input is selected. |
| 3269 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3270 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3271 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3272 | |
| 3273 | additional_speaker = |
| 3274 | cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
| 3275 | ASSERT_TRUE(additional_speaker); |
| 3276 | EXPECT_TRUE(additional_speaker->active); |
| 3277 | } |
| 3278 | |
| 3279 | TEST_P(CrasAudioHandlerTest, SetActiveNodesDeactivatePrimaryActiveNode) { |
| 3280 | AudioNodeList audio_nodes = |
| 3281 | GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2}); |
| 3282 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
| 3283 | usb_camera.plugged_time = 10000000; |
| 3284 | audio_nodes.push_back(usb_camera); |
| 3285 | SetUpCrasAudioHandler(audio_nodes); |
| 3286 | |
| 3287 | // Verify the audio devices size. |
| 3288 | AudioDeviceList audio_devices; |
| 3289 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3290 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3291 | |
| 3292 | // Verify the camera's input is selected as active input by CrasAudioHandler. |
| 3293 | EXPECT_EQ(1, GetActiveDeviceCount()); |
| 3294 | EXPECT_EQ(kUSBCameraInput->id, |
| 3295 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3296 | |
| 3297 | // Add another device to active input device list. |
| 3298 | cras_audio_handler_->SetActiveInputNodes( |
| 3299 | {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id}); |
| 3300 | |
| 3301 | // Verify active input devices are set as expected, with primary active input |
| 3302 | // staying the same. |
| 3303 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3304 | EXPECT_EQ(kUSBCameraInput->id, |
| 3305 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3306 | |
| 3307 | const AudioDevice* additional_speaker = |
| 3308 | cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id); |
| 3309 | ASSERT_TRUE(additional_speaker); |
| 3310 | EXPECT_TRUE(additional_speaker->active); |
| 3311 | |
| 3312 | // Update active device list so previously primary active device is not |
| 3313 | // active anymore. |
| 3314 | cras_audio_handler_->SetActiveInputNodes( |
| 3315 | {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id}); |
| 3316 | |
| 3317 | // Verify that list of active devices is correctly set, and that a new primary |
| 3318 | // active input is selected. |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3319 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3320 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3321 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3322 | |
| 3323 | additional_speaker = |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3324 | cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id); |
tbarzic | 96ea3205 | 2016-11-03 00:46:16 | [diff] [blame] | 3325 | ASSERT_TRUE(additional_speaker); |
| 3326 | EXPECT_TRUE(additional_speaker->active); |
| 3327 | } |
| 3328 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3329 | TEST_P(CrasAudioHandlerTest, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3330 | ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3331 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3332 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1}); |
| 3333 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3334 | usb_camera.plugged_time = 10000000; |
| 3335 | audio_nodes.push_back(usb_camera); |
| 3336 | SetUpCrasAudioHandler(audio_nodes); |
| 3337 | |
| 3338 | // Verify the audio devices size. |
| 3339 | AudioDeviceList audio_devices; |
| 3340 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3341 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3342 | |
| 3343 | // Verify the jabra speaker's output is selected as active output, and |
| 3344 | // camera's input is selected as active input by CrasAudioHandler |
| 3345 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3346 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3347 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3348 | EXPECT_EQ(kUSBCameraInput->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3349 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3350 | |
| 3351 | // Simulate hotrod app call to set jabra input as active device with only |
| 3352 | // jabra input node in the active node list, which does not conform to the |
| 3353 | // new SetActiveDevices protocol, but just show we can still handle it if |
| 3354 | // this happens. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3355 | cras_audio_handler_->ChangeActiveNodes({kUSBJabraSpeakerInput1->id}); |
| 3356 | |
| 3357 | // Verify the jabra speaker's output is selected as active output, and |
| 3358 | // jabra's input is selected as active input. |
| 3359 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3360 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3361 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3362 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3363 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3364 | } |
| 3365 | |
| 3366 | TEST_P(CrasAudioHandlerTest, |
| 3367 | SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) { |
| 3368 | AudioNodeList audio_nodes = GenerateAudioNodeList( |
| 3369 | {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1}); |
| 3370 | AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput); |
| 3371 | usb_camera.plugged_time = 10000000; |
| 3372 | audio_nodes.push_back(usb_camera); |
| 3373 | SetUpCrasAudioHandler(audio_nodes); |
| 3374 | |
| 3375 | // Verify the audio devices size. |
| 3376 | AudioDeviceList audio_devices; |
| 3377 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3378 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3379 | |
| 3380 | // Verify the jabra speaker's output is selected as active output, and |
| 3381 | // camera's input is selected as active input by CrasAudioHandler |
| 3382 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3383 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3384 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3385 | EXPECT_EQ(kUSBCameraInput->id, |
| 3386 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3387 | |
| 3388 | // Simulate hotrod app call to set jabra input as active device with only |
| 3389 | // jabra input node in the active node list, which does not conform to the |
| 3390 | // new SetActiveDevices protocol, but just show we can still handle it if |
| 3391 | // this happens. |
| 3392 | cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3393 | |
| 3394 | // Verify the jabra speaker's output is selected as active output, and |
| 3395 | // jabra's input is selected as active input. |
| 3396 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3397 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3398 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3399 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3400 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3401 | } |
| 3402 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3403 | TEST_P(CrasAudioHandlerTest, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3404 | ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3405 | AudioNodeList audio_nodes = |
| 3406 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3407 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3408 | SetUpCrasAudioHandler(audio_nodes); |
| 3409 | |
| 3410 | // Verify the audio devices size. |
| 3411 | AudioDeviceList audio_devices; |
| 3412 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3413 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3414 | |
| 3415 | // Verify the jabra speaker's output and input are selected as active output |
| 3416 | // by CrasAudioHandler. |
| 3417 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3418 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3419 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3420 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3421 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3422 | |
| 3423 | // Simulate hotrod app call SetActiveDevices to change active output |
| 3424 | // with only complete list of active nodes passed in, which is the new |
| 3425 | // way of hotrod app. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3426 | cras_audio_handler_->ChangeActiveNodes( |
| 3427 | {kHDMIOutput->id, kUSBJabraSpeakerInput1->id}); |
| 3428 | |
| 3429 | // Verify the jabra speaker's output is selected as active output, and |
| 3430 | // jabra's input is selected as active input. |
| 3431 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3432 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3433 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3434 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3435 | } |
| 3436 | |
| 3437 | TEST_P(CrasAudioHandlerTest, |
| 3438 | SetActiveNodesHotrodInitWithSingleJabraChangeOutput) { |
| 3439 | AudioNodeList audio_nodes = |
| 3440 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3441 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
| 3442 | SetUpCrasAudioHandler(audio_nodes); |
| 3443 | |
| 3444 | // Verify the audio devices size. |
| 3445 | AudioDeviceList audio_devices; |
| 3446 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3447 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3448 | |
| 3449 | // Verify the jabra speaker's output and input are selected as active output |
| 3450 | // by CrasAudioHandler. |
| 3451 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3452 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3453 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3454 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3455 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3456 | |
| 3457 | // Simulate hotrod app calling SetActiveDeviceLists to change active input |
| 3458 | // and output with complete list of active nodes passed in. |
| 3459 | cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id}); |
| 3460 | cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3461 | |
| 3462 | // Verify the jabra speaker's output is selected as active output, and |
| 3463 | // jabra's input is selected as active input. |
| 3464 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3465 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3466 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3467 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3468 | } |
| 3469 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3470 | TEST_P(CrasAudioHandlerTest, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3471 | ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) { |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3472 | AudioNodeList audio_nodes = |
| 3473 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3474 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3475 | SetUpCrasAudioHandler(audio_nodes); |
| 3476 | |
| 3477 | // Verify the audio devices size. |
| 3478 | AudioDeviceList audio_devices; |
| 3479 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3480 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3481 | |
| 3482 | // Verify the jabra speaker's output and input are selected as active output |
| 3483 | // by CrasAudioHandler. |
| 3484 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3485 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3486 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3487 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3488 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3489 | |
| 3490 | // Simulate hotrod app call SetActiveDevices to change active output |
| 3491 | // with only a single active output nodes passed in, which is the old |
| 3492 | // way of hotrod app. |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3493 | cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id}); |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3494 | |
| 3495 | // Verify the jabra speaker's output is selected as active output, and |
| 3496 | // jabra's input is selected as active input. |
| 3497 | EXPECT_EQ(2, GetActiveDeviceCount()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3498 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3499 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
jennyz | 2cb9eb1 | 2014-11-05 19:21:05 | [diff] [blame] | 3500 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
jennyz | b47947f | 2014-09-25 00:42:04 | [diff] [blame] | 3501 | } |
| 3502 | |
tbarzic | cae06e2a | 2017-01-17 23:14:47 | [diff] [blame] | 3503 | TEST_P(CrasAudioHandlerTest, SetEmptyActiveOutputNodes) { |
| 3504 | AudioNodeList audio_nodes = |
| 3505 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3506 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
| 3507 | SetUpCrasAudioHandler(audio_nodes); |
| 3508 | |
| 3509 | // Verify the audio devices size. |
| 3510 | AudioDeviceList audio_devices; |
| 3511 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3512 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3513 | |
| 3514 | // Verify the jabra speaker's output and input are selected as active output |
| 3515 | // by CrasAudioHandler. |
| 3516 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3517 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3518 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3519 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3520 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3521 | |
| 3522 | cras_audio_handler_->SetActiveOutputNodes(CrasAudioHandler::NodeIdList()); |
| 3523 | |
| 3524 | // Verify the jabra's input is selected as active input, and that there are |
| 3525 | // no active outputs. |
| 3526 | EXPECT_EQ(1, GetActiveDeviceCount()); |
| 3527 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3528 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3529 | EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3530 | } |
| 3531 | |
| 3532 | TEST_P(CrasAudioHandlerTest, SetEmptyActiveInputNodes) { |
| 3533 | AudioNodeList audio_nodes = |
| 3534 | GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1, |
| 3535 | kUSBJabraSpeakerInput1, kUSBCameraInput}); |
| 3536 | SetUpCrasAudioHandler(audio_nodes); |
| 3537 | |
| 3538 | // Verify the audio devices size. |
| 3539 | AudioDeviceList audio_devices; |
| 3540 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3541 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3542 | |
| 3543 | // Verify the jabra speaker's output and input are selected as active output |
| 3544 | // by CrasAudioHandler. |
| 3545 | EXPECT_EQ(2, GetActiveDeviceCount()); |
| 3546 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3547 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3548 | EXPECT_EQ(kUSBJabraSpeakerInput1->id, |
| 3549 | cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3550 | |
| 3551 | cras_audio_handler_->SetActiveInputNodes(CrasAudioHandler::NodeIdList()); |
| 3552 | |
| 3553 | // Verify the jabra speaker's output is selected as active output, and |
| 3554 | // there are no active inputs.. |
| 3555 | EXPECT_EQ(1, GetActiveDeviceCount()); |
| 3556 | EXPECT_EQ(kUSBJabraSpeakerOutput1->id, |
| 3557 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3558 | EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3559 | } |
| 3560 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3561 | TEST_P(CrasAudioHandlerTest, NoMoreAudioInputDevices) { |
mukai | 307596b6 | 2014-11-24 19:56:59 | [diff] [blame] | 3562 | // Some device like chromebox does not have the internal input device. The |
| 3563 | // active devices should be reset when the user plugs a device and then |
| 3564 | // unplugs it to such device. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3565 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker}); |
mukai | 307596b6 | 2014-11-24 19:56:59 | [diff] [blame] | 3566 | SetUpCrasAudioHandler(audio_nodes); |
| 3567 | |
| 3568 | EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3569 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3570 | audio_nodes.push_back(GenerateAudioNode(kMicJack)); |
mukai | 307596b6 | 2014-11-24 19:56:59 | [diff] [blame] | 3571 | ChangeAudioNodes(audio_nodes); |
| 3572 | |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3573 | EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
mukai | 307596b6 | 2014-11-24 19:56:59 | [diff] [blame] | 3574 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
| 3575 | test_observer_->reset_active_input_node_changed_count(); |
| 3576 | |
| 3577 | audio_nodes.pop_back(); |
| 3578 | ChangeAudioNodes(audio_nodes); |
| 3579 | EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3580 | EXPECT_EQ(1, test_observer_->active_input_node_changed_count()); |
| 3581 | } |
| 3582 | |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3583 | // Test the case in which an HDMI output is plugged in with other higher |
| 3584 | // priority |
| 3585 | // output devices already plugged and user has manually selected an active |
| 3586 | // output. |
| 3587 | // The hotplug of hdmi output should not change user's selection of active |
| 3588 | // device. |
| 3589 | // crbug.com/447826. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3590 | TEST_P(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) { |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3591 | AudioNodeList audio_nodes; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3592 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3593 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3594 | AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1); |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3595 | usb_headset.plugged_time = 80000000; |
| 3596 | audio_nodes.push_back(usb_headset); |
| 3597 | SetUpCrasAudioHandler(audio_nodes); |
| 3598 | |
| 3599 | // Verify the audio devices size. |
| 3600 | AudioDeviceList audio_devices; |
| 3601 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3602 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3603 | |
| 3604 | // Verify the USB headset is selected as active output by default. |
| 3605 | EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3606 | |
| 3607 | // Manually set the active output to internal speaker. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3608 | AudioDevice internal_output(GenerateAudioNode(kInternalSpeaker)); |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 3609 | cras_audio_handler_->SwitchToDevice(internal_output, true, |
| 3610 | CrasAudioHandler::ACTIVATE_BY_USER); |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3611 | |
| 3612 | // Verify the active output is switched to internal speaker. |
| 3613 | EXPECT_EQ(internal_speaker.id, |
| 3614 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3615 | EXPECT_LT(internal_speaker.plugged_time, usb_headset.plugged_time); |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3616 | const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id); |
| 3617 | EXPECT_FALSE(usb_device->active); |
| 3618 | |
| 3619 | // Plug in HDMI output. |
| 3620 | audio_nodes.clear(); |
| 3621 | internal_speaker.active = true; |
| 3622 | audio_nodes.push_back(internal_speaker); |
| 3623 | usb_headset.active = false; |
| 3624 | audio_nodes.push_back(usb_headset); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3625 | AudioNode hdmi = GenerateAudioNode(kHDMIOutput); |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3626 | hdmi.plugged_time = 90000000; |
| 3627 | audio_nodes.push_back(hdmi); |
| 3628 | ChangeAudioNodes(audio_nodes); |
| 3629 | |
| 3630 | // The active output should not change. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3631 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | 731ba26 | 2015-02-05 19:00:03 | [diff] [blame] | 3632 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3633 | } |
| 3634 | |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3635 | // Test the case in which the active device was set to inactive from cras after |
| 3636 | // resuming from suspension state. See crbug.com/478968. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3637 | TEST_P(CrasAudioHandlerTest, ActiveNodeLostAfterResume) { |
| 3638 | AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone, kHDMIOutput}); |
| 3639 | for (const auto& node : audio_nodes) |
| 3640 | ASSERT_FALSE(node.active) << node.id << " expected to be inactive"; |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3641 | SetUpCrasAudioHandler(audio_nodes); |
| 3642 | |
| 3643 | // Verify the headphone is selected as the active output. |
| 3644 | AudioDeviceList audio_devices; |
| 3645 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3646 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3647 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3648 | const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id); |
| 3649 | EXPECT_EQ(kHeadphone->id, active_headphone->id); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3650 | EXPECT_TRUE(active_headphone->active); |
| 3651 | |
| 3652 | // Simulate NodesChanged signal with headphone turning into inactive state, |
| 3653 | // and HDMI node removed. |
| 3654 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3655 | audio_nodes.push_back(GenerateAudioNode(kHeadphone)); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3656 | ChangeAudioNodes(audio_nodes); |
| 3657 | |
| 3658 | // Verify the headphone is set to active again. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3659 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3660 | const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id); |
| 3661 | EXPECT_EQ(kHeadphone->id, headphone_resumed->id); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3662 | EXPECT_TRUE(headphone_resumed->active); |
| 3663 | } |
| 3664 | |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3665 | // In the mirror mode, when the device resumes after being suspended, the hmdi |
| 3666 | // node will be lost first, then re-appear with a different node id, but with |
| 3667 | // the same stable id. If it is set as the non-active node by user before |
| 3668 | // suspend/resume, it should remain inactive after the device resumes even |
| 3669 | // if it has a higher priority than the current active node. |
| 3670 | // crbug.com/443014. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3671 | TEST_P(CrasAudioHandlerTest, HDMIRemainInactiveAfterSuspendResume) { |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3672 | AudioNodeList audio_nodes; |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3673 | AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker); |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3674 | audio_nodes.push_back(internal_speaker); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3675 | AudioNode hdmi_output = GenerateAudioNode(kHDMIOutput); |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3676 | audio_nodes.push_back(hdmi_output); |
| 3677 | SetUpCrasAudioHandler(audio_nodes); |
| 3678 | |
| 3679 | // Verify the hdmi is selected as the active output since it has a higher |
| 3680 | // priority. |
| 3681 | AudioDeviceList audio_devices; |
| 3682 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3683 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3684 | EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3685 | |
| 3686 | // Manually set the active output to internal speaker. |
jennyz | bd23674 | 2016-03-02 20:15:52 | [diff] [blame] | 3687 | cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true, |
| 3688 | CrasAudioHandler::ACTIVATE_BY_USER); |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3689 | EXPECT_EQ(internal_speaker.id, |
| 3690 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3691 | |
| 3692 | // Simulate the suspend and resume of the device during mirror mode. The HDMI |
| 3693 | // node will be lost first. |
| 3694 | audio_nodes.clear(); |
| 3695 | internal_speaker.active = true; |
| 3696 | audio_nodes.push_back(internal_speaker); |
| 3697 | ChangeAudioNodes(audio_nodes); |
| 3698 | |
| 3699 | // Verify the HDMI node is lost, and internal speaker is still the active |
| 3700 | // node. |
| 3701 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3702 | EXPECT_EQ(1u, audio_devices.size()); |
| 3703 | EXPECT_EQ(internal_speaker.id, |
| 3704 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3705 | |
| 3706 | // Simulate the re-appearing of the hdmi node, which comes with a new id, |
| 3707 | // but the same stable device id. |
| 3708 | AudioNode hdmi_output_2(hdmi_output); |
| 3709 | hdmi_output_2.id = 20006; |
| 3710 | hdmi_output_2.plugged_time = internal_speaker.plugged_time + 100; |
| 3711 | audio_nodes.push_back(hdmi_output_2); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3712 | ASSERT_NE(hdmi_output.id, hdmi_output_2.id); |
| 3713 | ASSERT_EQ(hdmi_output.StableDeviceId(), hdmi_output_2.StableDeviceId()); |
jennyz | 639ec6f | 2016-01-19 23:27:30 | [diff] [blame] | 3714 | ChangeAudioNodes(audio_nodes); |
| 3715 | |
| 3716 | // Verify the hdmi node is not set the active, and the current active node |
| 3717 | // , the internal speaker, remains active. |
| 3718 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3719 | EXPECT_EQ(2u, audio_devices.size()); |
| 3720 | EXPECT_EQ(internal_speaker.id, |
| 3721 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3722 | } |
| 3723 | |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3724 | // Test the case in which there are two NodesChanged signal for discovering |
| 3725 | // output devices, and there is race between NodesChange and SetActiveOutput |
| 3726 | // during this process. See crbug.com/478968. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3727 | TEST_P(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) { |
| 3728 | AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone}); |
| 3729 | for (const auto& node : audio_nodes) |
| 3730 | ASSERT_FALSE(node.active) << node.id << " expected to be inactive"; |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3731 | SetUpCrasAudioHandler(audio_nodes); |
| 3732 | |
| 3733 | // Verify the headphone is selected as the active output. |
| 3734 | AudioDeviceList audio_devices; |
| 3735 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3736 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3737 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3738 | const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id); |
| 3739 | EXPECT_EQ(kHeadphone->id, active_headphone->id); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3740 | EXPECT_TRUE(active_headphone->active); |
| 3741 | |
| 3742 | // Simulate NodesChanged signal with headphone turning into inactive state, |
| 3743 | // and add a new HDMI output node. |
| 3744 | audio_nodes.clear(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3745 | audio_nodes.push_back(GenerateAudioNode(kHeadphone)); |
| 3746 | audio_nodes.push_back(GenerateAudioNode(kHDMIOutput)); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3747 | ChangeAudioNodes(audio_nodes); |
| 3748 | |
| 3749 | // Verify the headphone is set to active again. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3750 | EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3751 | const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id); |
| 3752 | EXPECT_EQ(kHeadphone->id, headphone_resumed->id); |
jennyz | 3147abc7 | 2015-04-29 00:13:03 | [diff] [blame] | 3753 | EXPECT_TRUE(headphone_resumed->active); |
| 3754 | } |
| 3755 | |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3756 | // This test HDMI output rediscovering case in crbug.com/503667. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3757 | TEST_P(CrasAudioHandlerTest, HDMIOutputRediscover) { |
| 3758 | AudioNodeList audio_nodes = |
| 3759 | GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput}); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3760 | SetUpCrasAudioHandler(audio_nodes); |
| 3761 | |
| 3762 | // Verify the HDMI device has been selected as the active output, and audio |
| 3763 | // output is not muted. |
| 3764 | AudioDevice active_output; |
| 3765 | EXPECT_TRUE( |
| 3766 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3767 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 3768 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3769 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 3770 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3771 | |
| 3772 | // Trigger HDMI rediscovering grace period, and remove the HDMI node. |
| 3773 | const int grace_period_in_ms = 200; |
| 3774 | SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms); |
| 3775 | SetActiveHDMIRediscover(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3776 | AudioNodeList audio_nodes_lost_hdmi = |
| 3777 | GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3778 | ChangeAudioNodes(audio_nodes_lost_hdmi); |
| 3779 | |
| 3780 | // Verify the active output is switched to internal speaker, it is not muted |
| 3781 | // by preference, but the system output is muted during the grace period. |
| 3782 | EXPECT_TRUE( |
| 3783 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3784 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3785 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3786 | cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id)); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3787 | EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); |
| 3788 | |
| 3789 | // Re-attach the HDMI device after a little delay. |
| 3790 | HDMIRediscoverWaiter waiter(this, grace_period_in_ms); |
| 3791 | waiter.WaitUntilTimeOut(grace_period_in_ms / 4); |
| 3792 | ChangeAudioNodes(audio_nodes); |
| 3793 | |
| 3794 | // After HDMI re-discover grace period, verify HDMI output is selected as the |
| 3795 | // active device and not muted. |
| 3796 | waiter.WaitUntilHDMIRediscoverGracePeriodEnd(); |
| 3797 | EXPECT_TRUE( |
| 3798 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3799 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 3800 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | c770dc77 | 2015-06-29 23:46:06 | [diff] [blame] | 3801 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3802 | } |
| 3803 | |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3804 | // This tests the case of output unmuting event is notified after the hdmi |
| 3805 | // output re-discover grace period ends, see crbug.com/512601. |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3806 | TEST_P(CrasAudioHandlerTest, HDMIOutputUnplugDuringSuspension) { |
| 3807 | AudioNodeList audio_nodes = |
| 3808 | GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput}); |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3809 | SetUpCrasAudioHandler(audio_nodes); |
| 3810 | |
| 3811 | // Verify the HDMI device has been selected as the active output, and audio |
| 3812 | // output is not muted. |
| 3813 | AudioDevice active_output; |
| 3814 | EXPECT_TRUE( |
| 3815 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3816 | EXPECT_EQ(kHDMIOutput->id, active_output.id); |
| 3817 | EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3818 | EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 3819 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3820 | |
| 3821 | // Trigger HDMI rediscovering grace period, and remove the HDMI node. |
| 3822 | const int grace_period_in_ms = 200; |
| 3823 | SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms); |
| 3824 | SetActiveHDMIRediscover(); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3825 | AudioNodeList audio_nodes_lost_hdmi = |
| 3826 | GenerateAudioNodeList({kInternalSpeaker}); |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3827 | ChangeAudioNodes(audio_nodes_lost_hdmi); |
| 3828 | |
| 3829 | // Verify the active output is switched to internal speaker, it is not muted |
| 3830 | // by preference, but the system output is muted during the grace period. |
| 3831 | EXPECT_TRUE( |
| 3832 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3833 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3834 | EXPECT_FALSE( |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3835 | cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id)); |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3836 | EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); |
| 3837 | |
| 3838 | // After HDMI re-discover grace period, verify internal speaker is still the |
| 3839 | // active output and not muted, and unmute event by system is notified. |
| 3840 | test_observer_->reset_output_mute_changed_count(); |
| 3841 | HDMIRediscoverWaiter waiter(this, grace_period_in_ms); |
| 3842 | waiter.WaitUntilHDMIRediscoverGracePeriodEnd(); |
| 3843 | EXPECT_TRUE( |
| 3844 | cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
tbarzic | 3ab01a2 | 2016-12-17 03:12:53 | [diff] [blame] | 3845 | EXPECT_EQ(kInternalSpeaker->id, active_output.id); |
| 3846 | EXPECT_EQ(kInternalSpeaker->id, |
jennyz | c79ccda | 2015-07-28 23:04:35 | [diff] [blame] | 3847 | cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3848 | EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3849 | EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
| 3850 | EXPECT_TRUE(test_observer_->output_mute_by_system()); |
| 3851 | } |
| 3852 | |
jennyz | fbaa3e6 | 2017-03-06 20:14:02 | [diff] [blame] | 3853 | TEST_P(CrasAudioHandlerTest, FrontCameraStartStop) { |
| 3854 | AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic}); |
| 3855 | SetUpCrasAudioHandler(audio_nodes); |
| 3856 | |
| 3857 | // Verify the audio devices size. |
| 3858 | AudioDeviceList audio_devices; |
| 3859 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3860 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3861 | |
| 3862 | // Verify the front mic has been selected as the active input. |
| 3863 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3864 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 3865 | |
| 3866 | // Start the front facing camera. |
| 3867 | StartFrontFacingCamera(); |
| 3868 | // Verify the front mic has been selected as the active input. |
| 3869 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3870 | |
| 3871 | // Stop the front facing camera. |
| 3872 | StopFrontFacingCamera(); |
| 3873 | // Verify the front mic has been selected as the active input. |
| 3874 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3875 | } |
| 3876 | |
| 3877 | TEST_P(CrasAudioHandlerTest, RearCameraStartStop) { |
| 3878 | AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic}); |
| 3879 | SetUpCrasAudioHandler(audio_nodes); |
| 3880 | |
| 3881 | // Verify the audio devices size. |
| 3882 | AudioDeviceList audio_devices; |
| 3883 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3884 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3885 | |
| 3886 | // Verify the front mic has been selected as the active input. |
| 3887 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3888 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 3889 | |
| 3890 | // Start the rear facing camera. |
| 3891 | StartRearFacingCamera(); |
| 3892 | // Verify the active input is switched to the rear mic. |
| 3893 | EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3894 | |
| 3895 | // Stop the rear facing camera. |
| 3896 | StopRearFacingCamera(); |
| 3897 | // Verify the active input is switched back to front mic. |
| 3898 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3899 | } |
| 3900 | |
| 3901 | TEST_P(CrasAudioHandlerTest, SwitchFrontRearCamera) { |
| 3902 | AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic}); |
| 3903 | SetUpCrasAudioHandler(audio_nodes); |
| 3904 | |
| 3905 | // Verify the audio devices size. |
| 3906 | AudioDeviceList audio_devices; |
| 3907 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3908 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3909 | |
| 3910 | // Verify the front mic has been selected as the active input. |
| 3911 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3912 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 3913 | |
| 3914 | // Start the front facing camera. |
| 3915 | StartFrontFacingCamera(); |
| 3916 | // Verify the front mic has been selected as the active input. |
| 3917 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3918 | |
| 3919 | // Simulates the camera app switching from front camera to rear camera. |
| 3920 | StopFrontFacingCamera(); |
| 3921 | StartRearFacingCamera(); |
| 3922 | |
| 3923 | // Verify the rear mic has been selected as the active input. |
| 3924 | EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3925 | } |
| 3926 | |
| 3927 | TEST_P(CrasAudioHandlerTest, StartFrontCameraWithActiveExternalInput) { |
| 3928 | AudioNodeList audio_nodes = |
| 3929 | GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack}); |
| 3930 | SetUpCrasAudioHandler(audio_nodes); |
| 3931 | |
| 3932 | // Verify the audio devices size. |
| 3933 | AudioDeviceList audio_devices; |
| 3934 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3935 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3936 | |
| 3937 | // Verify the mic Jack has been selected as the active input. |
| 3938 | EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3939 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 3940 | |
| 3941 | // Start the front facing camera. |
| 3942 | StartFrontFacingCamera(); |
| 3943 | // Verify the mic Jack has been selected as the active input. |
| 3944 | EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3945 | |
| 3946 | // Stop the front facing camera. |
| 3947 | StopFrontFacingCamera(); |
| 3948 | // Verify the mic Jack remains as the active input. |
| 3949 | EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3950 | } |
| 3951 | |
| 3952 | TEST_P(CrasAudioHandlerTest, StartFrontCameraWithInactiveExternalInput) { |
| 3953 | AudioNodeList audio_nodes = |
| 3954 | GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack}); |
| 3955 | SetUpCrasAudioHandler(audio_nodes); |
| 3956 | |
| 3957 | // Verify the audio devices size. |
| 3958 | AudioDeviceList audio_devices; |
| 3959 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3960 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3961 | |
| 3962 | // Verify the mic Jack has been selected as the active input. |
| 3963 | EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3964 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 3965 | |
| 3966 | // Change the active input to internal mic. |
| 3967 | cras_audio_handler_->SwitchToFrontOrRearMic(); |
| 3968 | // Verify the active input has been switched to front mic. |
| 3969 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3970 | |
| 3971 | // Start the front facing camera. |
| 3972 | StartFrontFacingCamera(); |
| 3973 | // Verify the front mic has been selected as the active input. |
| 3974 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3975 | |
| 3976 | // Stop the front facing camera. |
| 3977 | StopFrontFacingCamera(); |
| 3978 | // Verify the active input remains as front mic. |
| 3979 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3980 | } |
| 3981 | |
| 3982 | TEST_P(CrasAudioHandlerTest, StartFrontCameraWithoutDualMic) { |
| 3983 | AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic}); |
| 3984 | SetUpCrasAudioHandler(audio_nodes); |
| 3985 | |
| 3986 | // Verify the audio devices size. |
| 3987 | AudioDeviceList audio_devices; |
| 3988 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 3989 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 3990 | |
| 3991 | // Verify the mic Jack has been selected as the active input. |
| 3992 | EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3993 | EXPECT_FALSE(cras_audio_handler_->HasDualInternalMic()); |
| 3994 | |
| 3995 | // Start the front facing camera. |
| 3996 | StartFrontFacingCamera(); |
| 3997 | // Verify the internal mic has been selected as the active input. |
| 3998 | EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 3999 | |
| 4000 | // Stop the front facing camera. |
| 4001 | StopFrontFacingCamera(); |
| 4002 | // Verify the active input remains as interanl mic. |
| 4003 | EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4004 | } |
| 4005 | |
| 4006 | TEST_P(CrasAudioHandlerTest, FrontRearCameraBothOn) { |
| 4007 | AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic}); |
| 4008 | SetUpCrasAudioHandler(audio_nodes); |
| 4009 | |
| 4010 | // Verify the audio devices size. |
| 4011 | AudioDeviceList audio_devices; |
| 4012 | cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 4013 | EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 4014 | |
| 4015 | // Verify the front mic has been selected as the active input. |
| 4016 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4017 | EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic()); |
| 4018 | |
| 4019 | // Start the rear facing camera. |
| 4020 | StartRearFacingCamera(); |
| 4021 | // Verify the rear mic has been selected as the active input. |
| 4022 | EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4023 | |
| 4024 | // Start front camera without stopping the front camera. |
| 4025 | StartFrontFacingCamera(); |
| 4026 | // Verify the active microphone does not change. |
| 4027 | EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4028 | |
| 4029 | // Stop the front mic. |
| 4030 | StopFrontFacingCamera(); |
| 4031 | // Verity the active mic does not change when there is still camera on. |
| 4032 | EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4033 | |
| 4034 | // Stop the rear mic. |
| 4035 | StopRearFacingCamera(); |
| 4036 | // Verify the actice mic changes to front mic after both cameras stop. |
| 4037 | EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 4038 | } |
| 4039 | |
[email protected] | ee38bc4 | 2013-07-29 21:41:32 | [diff] [blame] | 4040 | } // namespace chromeos |