1 // Copyright 2016 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 // Tests for audio device handler. 17 18 #include "audio_device_handler_mock.h" 19 20 #include <string> 21 22 #include <base/files/file_path.h> 23 #include <base/files/file_util.h> 24 #include <base/files/scoped_temp_dir.h> 25 #include <base/strings/string_number_conversions.h> 26 #include <gmock/gmock.h> 27 #include <gtest/gtest.h> 28 29 using base::FilePath; 30 using base::IntToString; 31 using base::ScopedTempDir; 32 using base::WriteFile; 33 using brillo::AudioDeviceHandlerMock; 34 using testing::_; 35 using testing::AnyNumber; 36 37 namespace brillo { 38 39 class AudioDeviceHandlerTest : public testing::Test { 40 public: 41 void SetUp() override { 42 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); 43 h2w_file_path_ = temp_dir_.path().Append("h2wstate"); 44 } 45 46 void TearDown() override { handler_.Reset(); } 47 48 // Method to store the current state of the audio jack to a file. 49 // 50 // |value| - Value in the h2w file. 51 void WriteToH2WFile(int value) { 52 std::string value_string = IntToString(value); 53 WriteFile(h2w_file_path_, value_string.c_str(), value_string.length()); 54 } 55 56 AudioDeviceHandlerMock handler_; 57 FilePath h2w_file_path_; 58 59 private: 60 ScopedTempDir temp_dir_; 61 }; 62 63 // Test that DisconnectAllSupportedDevices() calls NotifyAudioPolicyService() 64 // the right number of times. 65 TEST_F(AudioDeviceHandlerTest, DisconnectAllSupportedDevicesCallsDisconnect) { 66 EXPECT_CALL(handler_, 67 NotifyAudioPolicyService( 68 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3); 69 handler_.DisconnectAllSupportedDevices(); 70 } 71 72 // Test that Init() calls DisconnectAllSupportedDevices(). 73 TEST_F(AudioDeviceHandlerTest, InitCallsDisconnectAllSupportedDevices) { 74 EXPECT_CALL(handler_, 75 NotifyAudioPolicyService( 76 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3); 77 EXPECT_CALL(handler_, 78 NotifyAudioPolicyService( 79 _, AUDIO_POLICY_DEVICE_STATE_AVAILABLE)).Times(AnyNumber()); 80 handler_.Init(nullptr); 81 } 82 83 // Test GetInitialAudioDeviceState() with just a microphone. 84 TEST_F(AudioDeviceHandlerTest, InitialAudioStateMic) { 85 WriteToH2WFile(2); 86 EXPECT_CALL(handler_, 87 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET, 88 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 89 handler_.GetInitialAudioDeviceState(h2w_file_path_); 90 EXPECT_NE( 91 handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET), 92 handler_.connected_input_devices_.end()); 93 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 94 } 95 96 // Test GetInitialAudioDeviceState() with a headphone. 97 TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadphone) { 98 WriteToH2WFile(1); 99 EXPECT_CALL(handler_, 100 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 101 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 102 handler_.GetInitialAudioDeviceState(h2w_file_path_); 103 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 104 EXPECT_NE( 105 handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), 106 handler_.connected_output_devices_.end()); 107 } 108 109 // Test GetInitialAudioDeviceState() with a headset. 110 TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadset) { 111 WriteToH2WFile(3); 112 EXPECT_CALL(handler_, 113 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET, 114 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 115 EXPECT_CALL(handler_, 116 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET, 117 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 118 handler_.GetInitialAudioDeviceState(h2w_file_path_); 119 EXPECT_NE( 120 handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET), 121 handler_.connected_input_devices_.end()); 122 EXPECT_NE( 123 handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADSET), 124 handler_.connected_output_devices_.end()); 125 } 126 127 // Test GetInitialAudioDeviceState() without any devices connected to the audio 128 // jack. No need to call NotifyAudioPolicyService() since that's already handled 129 // by Init(). 130 TEST_F(AudioDeviceHandlerTest, InitialAudioStateNone) { 131 WriteToH2WFile(0); 132 handler_.GetInitialAudioDeviceState(h2w_file_path_); 133 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 134 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 135 } 136 137 // Test GetInitialAudioDeviceState() with an invalid file. The audio handler 138 // should not fail in this case because it should work on boards that don't 139 // support audio jacks. 140 TEST_F(AudioDeviceHandlerTest, InitialAudioStateInvalid) { 141 FilePath path = h2w_file_path_; 142 handler_.GetInitialAudioDeviceState(h2w_file_path_); 143 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 144 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 145 } 146 147 // Test ProcessEvent() with an empty input_event arg. 148 TEST_F(AudioDeviceHandlerTest, ProcessEventEmpty) { 149 struct input_event event; 150 event.type = 0; 151 event.code = 0; 152 event.value = 0; 153 handler_.ProcessEvent(event); 154 EXPECT_FALSE(handler_.headphone_); 155 EXPECT_FALSE(handler_.microphone_); 156 } 157 158 // Test ProcessEvent() with a microphone present input_event arg. 159 TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophonePresent) { 160 struct input_event event; 161 event.type = EV_SW; 162 event.code = SW_MICROPHONE_INSERT; 163 event.value = 1; 164 handler_.ProcessEvent(event); 165 EXPECT_FALSE(handler_.headphone_); 166 EXPECT_TRUE(handler_.microphone_); 167 } 168 169 // Test ProcessEvent() with a headphone present input_event arg. 170 TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphonePresent) { 171 struct input_event event; 172 event.type = EV_SW; 173 event.code = SW_HEADPHONE_INSERT; 174 event.value = 1; 175 handler_.ProcessEvent(event); 176 EXPECT_TRUE(handler_.headphone_); 177 EXPECT_FALSE(handler_.microphone_); 178 } 179 180 // Test ProcessEvent() with a microphone not present input_event arg. 181 TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophoneNotPresent) { 182 struct input_event event; 183 event.type = EV_SW; 184 event.code = SW_MICROPHONE_INSERT; 185 event.value = 0; 186 handler_.ProcessEvent(event); 187 EXPECT_FALSE(handler_.headphone_); 188 EXPECT_FALSE(handler_.microphone_); 189 } 190 191 // Test ProcessEvent() with a headphone not preset input_event arg. 192 TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphoneNotPresent) { 193 struct input_event event; 194 event.type = EV_SW; 195 event.code = SW_HEADPHONE_INSERT; 196 event.value = 0; 197 handler_.ProcessEvent(event); 198 EXPECT_FALSE(handler_.headphone_); 199 EXPECT_FALSE(handler_.microphone_); 200 } 201 202 // Test ProcessEvent() with an unsupported input_event arg. 203 TEST_F(AudioDeviceHandlerTest, ProcessEventInvalid) { 204 struct input_event event; 205 event.type = EV_SW; 206 event.code = SW_MAX; 207 event.value = 0; 208 handler_.ProcessEvent(event); 209 EXPECT_FALSE(handler_.headphone_); 210 EXPECT_FALSE(handler_.microphone_); 211 } 212 213 // Test UpdateAudioSystem() without any devices connected. 214 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemNone) { 215 EXPECT_CALL(handler_, 216 NotifyAudioPolicyService( 217 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(0); 218 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 219 } 220 221 // Test UpdateAudioSystem() when disconnecting a microphone. 222 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectMic) { 223 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET; 224 handler_.connected_input_devices_.insert(device); 225 EXPECT_CALL(handler_, 226 NotifyAudioPolicyService(device, 227 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 228 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 229 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 230 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 231 } 232 233 // Test UpdateAudioSystem() when disconnecting a headphone. 234 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadphone) { 235 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE; 236 handler_.connected_output_devices_.insert(device); 237 EXPECT_CALL(handler_, 238 NotifyAudioPolicyService(device, 239 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 240 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 241 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 242 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 243 } 244 245 // Test UpdateAudioSystem() when disconnecting a headset & headphones. 246 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadset) { 247 handler_.connected_input_devices_.insert(AUDIO_DEVICE_IN_WIRED_HEADSET); 248 handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADSET); 249 handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADPHONE); 250 EXPECT_CALL(handler_, 251 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET, 252 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 253 EXPECT_CALL(handler_, 254 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET, 255 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 256 EXPECT_CALL(handler_, 257 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 258 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 259 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 260 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 261 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 262 } 263 264 // Test UpdateAudioSystem() when connecting a microphone. 265 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectMic) { 266 handler_.microphone_ = true; 267 EXPECT_CALL(handler_, 268 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET, 269 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 270 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 271 EXPECT_EQ(handler_.connected_input_devices_.size(), 1); 272 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 273 } 274 275 // Test UpdateAudioSystem() when connecting a headphone. 276 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadphone) { 277 handler_.headphone_ = true; 278 EXPECT_CALL(handler_, 279 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 280 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 281 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 282 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 283 EXPECT_EQ(handler_.connected_output_devices_.size(), 1); 284 } 285 286 // Test UpdateAudioSystem() when connecting a headset. 287 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadset) { 288 handler_.headphone_ = true; 289 handler_.microphone_ = true; 290 EXPECT_CALL(handler_, 291 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET, 292 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 293 EXPECT_CALL(handler_, 294 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET, 295 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 296 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_); 297 EXPECT_EQ(handler_.connected_input_devices_.size(), 1); 298 EXPECT_EQ(handler_.connected_output_devices_.size(), 1); 299 } 300 301 // Test ConnectAudioDevice() with an input device. 302 TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceInput) { 303 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET; 304 EXPECT_CALL(handler_, 305 NotifyAudioPolicyService(device, 306 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 307 handler_.ConnectAudioDevice(device); 308 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 309 EXPECT_NE( 310 handler_.connected_input_devices_.find(device), 311 handler_.connected_input_devices_.end()); 312 } 313 314 // Test ConnectAudioDevice() with an output device. 315 TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceOutput) { 316 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET; 317 EXPECT_CALL(handler_, 318 NotifyAudioPolicyService(device, 319 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)); 320 handler_.ConnectAudioDevice(device); 321 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 322 EXPECT_NE( 323 handler_.connected_output_devices_.find(device), 324 handler_.connected_output_devices_.end()); 325 } 326 327 // Test DisconnectAudioDevice() with an input device. 328 TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceInput) { 329 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET; 330 handler_.connected_input_devices_.insert(device); 331 handler_.connected_output_devices_.insert(device); 332 EXPECT_CALL(handler_, 333 NotifyAudioPolicyService(device, 334 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 335 handler_.DisconnectAudioDevice(device); 336 EXPECT_EQ(handler_.connected_input_devices_.size(), 0); 337 EXPECT_EQ(handler_.connected_output_devices_.size(), 1); 338 } 339 340 // Test DisconnectAudioDevice() with an output device. 341 TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceOutput) { 342 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET; 343 handler_.connected_input_devices_.insert(device); 344 handler_.connected_output_devices_.insert(device); 345 EXPECT_CALL(handler_, 346 NotifyAudioPolicyService(device, 347 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 348 handler_.DisconnectAudioDevice(device); 349 EXPECT_EQ(handler_.connected_input_devices_.size(), 1); 350 EXPECT_EQ(handler_.connected_output_devices_.size(), 0); 351 } 352 353 } // namespace brillo 354