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