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 using testing::AtLeast;
37
38 namespace brillo {
39
40 class AudioDeviceHandlerTest : public testing::Test {
41 public:
SetUp()42 void SetUp() override {
43 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
44 h2w_file_path_ = temp_dir_.path().Append("h2wstate");
45 }
46
TearDown()47 void TearDown() override { handler_.Reset(); }
48
49 // Method to store the current state of the audio jack to a file.
50 //
51 // |value| - Value in the h2w file.
WriteToH2WFile(int value)52 void WriteToH2WFile(int value) {
53 std::string value_string = IntToString(value);
54 WriteFile(h2w_file_path_, value_string.c_str(), value_string.length());
55 }
56
57 AudioDeviceHandlerMock handler_;
58 FilePath h2w_file_path_;
59
60 private:
61 ScopedTempDir temp_dir_;
62 };
63
64 // Test that DisconnectAllSupportedDevices() calls NotifyAudioPolicyService()
65 // the right number of times.
TEST_F(AudioDeviceHandlerTest,DisconnectAllSupportedDevicesCallsDisconnect)66 TEST_F(AudioDeviceHandlerTest, DisconnectAllSupportedDevicesCallsDisconnect) {
67 EXPECT_CALL(handler_,
68 NotifyAudioPolicyService(
69 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3);
70 handler_.DisconnectAllSupportedDevices();
71 EXPECT_EQ(handler_.changed_devices_.size(), 3);
72 }
73
74 // Test that Init() calls DisconnectAllSupportedDevices().
TEST_F(AudioDeviceHandlerTest,InitCallsDisconnectAllSupportedDevices)75 TEST_F(AudioDeviceHandlerTest, InitCallsDisconnectAllSupportedDevices) {
76 EXPECT_CALL(handler_,
77 NotifyAudioPolicyService(
78 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3);
79 EXPECT_CALL(handler_, TriggerCallback(
80 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected))
81 .Times(AtLeast(1));
82 EXPECT_CALL(handler_,
83 NotifyAudioPolicyService(
84 _, AUDIO_POLICY_DEVICE_STATE_AVAILABLE)).Times(AnyNumber());
85 EXPECT_CALL(handler_, TriggerCallback(
86 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected))
87 .Times(AnyNumber());
88 handler_.Init(nullptr);
89 }
90
91 // Test GetInitialAudioDeviceState() with just a microphone.
TEST_F(AudioDeviceHandlerTest,InitialAudioStateMic)92 TEST_F(AudioDeviceHandlerTest, InitialAudioStateMic) {
93 WriteToH2WFile(2);
94 EXPECT_CALL(handler_,
95 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
96 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
97 EXPECT_CALL(handler_, TriggerCallback(
98 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
99 handler_.GetInitialAudioDeviceState(h2w_file_path_);
100 EXPECT_NE(
101 handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET),
102 handler_.connected_input_devices_.end());
103 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
104 EXPECT_EQ(handler_.changed_devices_.size(), 1);
105 EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_IN_WIRED_HEADSET);
106 }
107
108 // Test GetInitialAudioDeviceState() with a headphone.
TEST_F(AudioDeviceHandlerTest,InitialAudioStateHeadphone)109 TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadphone) {
110 WriteToH2WFile(1);
111 EXPECT_CALL(handler_,
112 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
113 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
114 EXPECT_CALL(handler_, TriggerCallback(
115 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
116 handler_.GetInitialAudioDeviceState(h2w_file_path_);
117 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
118 EXPECT_NE(
119 handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
120 handler_.connected_output_devices_.end());
121 EXPECT_EQ(handler_.changed_devices_.size(), 1);
122 EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
123 }
124
125 // Test GetInitialAudioDeviceState() with a headset.
TEST_F(AudioDeviceHandlerTest,InitialAudioStateHeadset)126 TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadset) {
127 WriteToH2WFile(3);
128 EXPECT_CALL(handler_, TriggerCallback(
129 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
130 EXPECT_CALL(handler_,
131 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
132 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
133 EXPECT_CALL(handler_,
134 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
135 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
136 handler_.GetInitialAudioDeviceState(h2w_file_path_);
137 EXPECT_NE(
138 handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET),
139 handler_.connected_input_devices_.end());
140 EXPECT_NE(
141 handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADSET),
142 handler_.connected_output_devices_.end());
143 EXPECT_EQ(handler_.changed_devices_.size(), 2);
144 }
145
146 // Test GetInitialAudioDeviceState() without any devices connected to the audio
147 // jack. No need to call NotifyAudioPolicyService() since that's already handled
148 // by Init().
TEST_F(AudioDeviceHandlerTest,InitialAudioStateNone)149 TEST_F(AudioDeviceHandlerTest, InitialAudioStateNone) {
150 WriteToH2WFile(0);
151 EXPECT_CALL(handler_, TriggerCallback(_));
152 handler_.GetInitialAudioDeviceState(h2w_file_path_);
153 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
154 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
155 EXPECT_EQ(handler_.changed_devices_.size(), 0);
156 }
157
158 // Test GetInitialAudioDeviceState() with an invalid file. The audio handler
159 // should not fail in this case because it should work on boards that don't
160 // support audio jacks.
TEST_F(AudioDeviceHandlerTest,InitialAudioStateInvalid)161 TEST_F(AudioDeviceHandlerTest, InitialAudioStateInvalid) {
162 FilePath path = h2w_file_path_;
163 handler_.GetInitialAudioDeviceState(h2w_file_path_);
164 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
165 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
166 }
167
168 // Test ProcessEvent() with an empty input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventEmpty)169 TEST_F(AudioDeviceHandlerTest, ProcessEventEmpty) {
170 struct input_event event;
171 event.type = 0;
172 event.code = 0;
173 event.value = 0;
174 EXPECT_CALL(handler_, TriggerCallback(_));
175 handler_.ProcessEvent(event);
176 EXPECT_FALSE(handler_.headphone_);
177 EXPECT_FALSE(handler_.microphone_);
178 }
179
180 // Test ProcessEvent() with a microphone present input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventMicrophonePresent)181 TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophonePresent) {
182 struct input_event event;
183 event.type = EV_SW;
184 event.code = SW_MICROPHONE_INSERT;
185 event.value = 1;
186 handler_.ProcessEvent(event);
187 EXPECT_FALSE(handler_.headphone_);
188 EXPECT_TRUE(handler_.microphone_);
189 }
190
191 // Test ProcessEvent() with a headphone present input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventHeadphonePresent)192 TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphonePresent) {
193 struct input_event event;
194 event.type = EV_SW;
195 event.code = SW_HEADPHONE_INSERT;
196 event.value = 1;
197 handler_.ProcessEvent(event);
198 EXPECT_TRUE(handler_.headphone_);
199 EXPECT_FALSE(handler_.microphone_);
200 }
201
202 // Test ProcessEvent() with a microphone not present input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventMicrophoneNotPresent)203 TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophoneNotPresent) {
204 struct input_event event;
205 event.type = EV_SW;
206 event.code = SW_MICROPHONE_INSERT;
207 event.value = 0;
208 handler_.ProcessEvent(event);
209 EXPECT_FALSE(handler_.headphone_);
210 EXPECT_FALSE(handler_.microphone_);
211 }
212
213 // Test ProcessEvent() with a headphone not preset input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventHeadphoneNotPresent)214 TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphoneNotPresent) {
215 struct input_event event;
216 event.type = EV_SW;
217 event.code = SW_HEADPHONE_INSERT;
218 event.value = 0;
219 handler_.ProcessEvent(event);
220 EXPECT_FALSE(handler_.headphone_);
221 EXPECT_FALSE(handler_.microphone_);
222 }
223
224 // Test ProcessEvent() with an unsupported input_event arg.
TEST_F(AudioDeviceHandlerTest,ProcessEventInvalid)225 TEST_F(AudioDeviceHandlerTest, ProcessEventInvalid) {
226 struct input_event event;
227 event.type = EV_SW;
228 event.code = SW_MAX;
229 event.value = 0;
230 handler_.ProcessEvent(event);
231 EXPECT_FALSE(handler_.headphone_);
232 EXPECT_FALSE(handler_.microphone_);
233 }
234
235 // Test UpdateAudioSystem() without any devices connected.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemNone)236 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemNone) {
237 EXPECT_CALL(handler_,
238 NotifyAudioPolicyService(
239 _, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(0);
240 EXPECT_CALL(handler_, TriggerCallback(
241 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
242 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
243 EXPECT_EQ(handler_.changed_devices_.size(), 0);
244 }
245
246 // Test UpdateAudioSystem() when disconnecting a microphone.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemDisconnectMic)247 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectMic) {
248 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
249 handler_.connected_input_devices_.insert(device);
250 EXPECT_CALL(handler_,
251 NotifyAudioPolicyService(device,
252 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
253 EXPECT_CALL(handler_, TriggerCallback(
254 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
255 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
256 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
257 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
258 EXPECT_EQ(handler_.changed_devices_.size(), 1);
259 EXPECT_EQ(handler_.changed_devices_[0], device);
260 }
261
262 // Test UpdateAudioSystem() when disconnecting a headphone.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemDisconnectHeadphone)263 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadphone) {
264 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
265 handler_.connected_output_devices_.insert(device);
266 EXPECT_CALL(handler_,
267 NotifyAudioPolicyService(device,
268 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
269 EXPECT_CALL(handler_, TriggerCallback(
270 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
271 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
272 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
273 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
274 EXPECT_EQ(handler_.changed_devices_.size(), 1);
275 EXPECT_EQ(handler_.changed_devices_[0], device);
276 }
277
278 // Test UpdateAudioSystem() when disconnecting a headset & headphones.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemDisconnectHeadset)279 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadset) {
280 handler_.connected_input_devices_.insert(AUDIO_DEVICE_IN_WIRED_HEADSET);
281 handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADSET);
282 handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
283 EXPECT_CALL(handler_,
284 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
285 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
286 EXPECT_CALL(handler_,
287 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
288 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
289 EXPECT_CALL(handler_,
290 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
291 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
292 EXPECT_CALL(handler_, TriggerCallback(
293 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
294 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
295 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
296 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
297 EXPECT_EQ(handler_.changed_devices_.size(), 3);
298 }
299
300 // Test UpdateAudioSystem() when connecting a microphone.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemConnectMic)301 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectMic) {
302 handler_.microphone_ = true;
303 EXPECT_CALL(handler_,
304 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
305 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
306 EXPECT_CALL(handler_, TriggerCallback(
307 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
308 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
309 EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
310 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
311 EXPECT_EQ(handler_.changed_devices_.size(), 1);
312 EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_IN_WIRED_HEADSET);
313 }
314
315 // Test UpdateAudioSystem() when connecting a headphone.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemConnectHeadphone)316 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadphone) {
317 handler_.headphone_ = true;
318 EXPECT_CALL(handler_,
319 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
320 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
321 EXPECT_CALL(handler_, TriggerCallback(
322 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
323 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
324 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
325 EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
326 EXPECT_EQ(handler_.changed_devices_.size(), 1);
327 EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
328 }
329
330 // Test UpdateAudioSystem() when connecting a headset.
TEST_F(AudioDeviceHandlerTest,UpdateAudioSystemConnectHeadset)331 TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadset) {
332 handler_.headphone_ = true;
333 handler_.microphone_ = true;
334 EXPECT_CALL(handler_,
335 NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
336 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
337 EXPECT_CALL(handler_,
338 NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
339 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
340 EXPECT_CALL(handler_, TriggerCallback(
341 AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
342 handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
343 EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
344 EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
345 EXPECT_EQ(handler_.changed_devices_.size(), 2);
346 }
347
348 // Test ConnectAudioDevice() with an input device.
TEST_F(AudioDeviceHandlerTest,ConnectAudioDeviceInput)349 TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceInput) {
350 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
351 EXPECT_CALL(handler_,
352 NotifyAudioPolicyService(device,
353 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
354 handler_.ConnectAudioDevice(device);
355 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
356 EXPECT_NE(
357 handler_.connected_input_devices_.find(device),
358 handler_.connected_input_devices_.end());
359 EXPECT_EQ(handler_.changed_devices_.size(), 1);
360 EXPECT_EQ(handler_.changed_devices_[0], device);
361 }
362
363 // Test ConnectAudioDevice() with an output device.
TEST_F(AudioDeviceHandlerTest,ConnectAudioDeviceOutput)364 TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceOutput) {
365 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET;
366 EXPECT_CALL(handler_,
367 NotifyAudioPolicyService(device,
368 AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
369 handler_.ConnectAudioDevice(device);
370 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
371 EXPECT_NE(
372 handler_.connected_output_devices_.find(device),
373 handler_.connected_output_devices_.end());
374 EXPECT_EQ(handler_.changed_devices_.size(), 1);
375 EXPECT_EQ(handler_.changed_devices_[0], device);
376 }
377
378 // Test DisconnectAudioDevice() with an input device.
TEST_F(AudioDeviceHandlerTest,DisconnectAudioDeviceInput)379 TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceInput) {
380 audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
381 handler_.connected_input_devices_.insert(device);
382 handler_.connected_output_devices_.insert(device);
383 EXPECT_CALL(handler_,
384 NotifyAudioPolicyService(device,
385 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
386 handler_.DisconnectAudioDevice(device);
387 EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
388 EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
389 EXPECT_EQ(handler_.changed_devices_.size(), 1);
390 EXPECT_EQ(handler_.changed_devices_[0], device);
391 }
392
393 // Test DisconnectAudioDevice() with an output device.
TEST_F(AudioDeviceHandlerTest,DisconnectAudioDeviceOutput)394 TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceOutput) {
395 audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET;
396 handler_.connected_input_devices_.insert(device);
397 handler_.connected_output_devices_.insert(device);
398 EXPECT_CALL(handler_,
399 NotifyAudioPolicyService(device,
400 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
401 handler_.DisconnectAudioDevice(device);
402 EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
403 EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
404 EXPECT_EQ(handler_.changed_devices_.size(), 1);
405 EXPECT_EQ(handler_.changed_devices_[0], device);
406 }
407
408 } // namespace brillo
409