1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #define LOG_TAG "VtsAidlHalAudioControlTest"
17
18 #include <aidl/Gtest.h>
19 #include <aidl/Vintf.h>
20 #include <gmock/gmock.h>
21
22 #include <android/hardware/automotive/audiocontrol/BnAudioGainCallback.h>
23 #include <android/hardware/automotive/audiocontrol/BnFocusListener.h>
24 #include <android/hardware/automotive/audiocontrol/BnModuleChangeCallback.h>
25 #include <android/hardware/automotive/audiocontrol/IAudioControl.h>
26 #include <android/log.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/ProcessState.h>
29
30 using android::ProcessState;
31 using android::sp;
32 using android::String16;
33 using android::binder::Status;
34 using android::hardware::automotive::audiocontrol::AudioFocusChange;
35 using android::hardware::automotive::audiocontrol::AudioGainConfigInfo;
36 using android::hardware::automotive::audiocontrol::BnAudioGainCallback;
37 using android::hardware::automotive::audiocontrol::BnFocusListener;
38 using android::hardware::automotive::audiocontrol::BnModuleChangeCallback;
39 using android::hardware::automotive::audiocontrol::DuckingInfo;
40 using android::hardware::automotive::audiocontrol::IAudioControl;
41 using android::hardware::automotive::audiocontrol::IModuleChangeCallback;
42 using android::hardware::automotive::audiocontrol::MutingInfo;
43 using android::hardware::automotive::audiocontrol::Reasons;
44 using ::testing::AnyOf;
45 using ::testing::Eq;
46
47 #include "android_audio_policy_configuration_V7_0.h"
48
49 namespace xsd {
50 using namespace android::audio::policy::configuration::V7_0;
51 }
52
53 namespace audiohalcommon = android::hardware::audio::common;
54 namespace audiomediacommon = android::media::audio::common;
55
56 namespace {
57 constexpr int32_t kAidlVersionThree = 3;
58 }
59
60 class AudioControlAidl : public testing::TestWithParam<std::string> {
61 public:
SetUp()62 virtual void SetUp() override {
63 audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
64 ASSERT_NE(audioControl, nullptr);
65 aidlVersion = audioControl->getInterfaceVersion();
66 }
67
TearDown()68 void TearDown() override { audioControl = nullptr; }
69
isAidlVersionAtleast(int version) const70 bool isAidlVersionAtleast(int version) const { return aidlVersion >= version; }
71
72 sp<IAudioControl> audioControl;
73 int32_t capabilities;
74 int32_t aidlVersion;
75 };
76
TEST_P(AudioControlAidl,OnSetFadeTowardsFront)77 TEST_P(AudioControlAidl, OnSetFadeTowardsFront) {
78 ALOGI("Fader exercise test (silent)");
79
80 // Set the fader all the way to the back
81 ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk());
82
83 // Set the fader all the way to the front
84 ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk());
85
86 // Set the fader part way toward the back
87 ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk());
88
89 // Set the fader to a out of bounds value (driver should clamp)
90 ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk());
91
92 // Set the fader to a negative out of bounds value (driver should clamp)
93 ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk());
94
95 // Set the fader back to the middle
96 ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk());
97 }
98
TEST_P(AudioControlAidl,OnSetBalanceTowardsRight)99 TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) {
100 ALOGI("Balance exercise test (silent)");
101
102 // Set the balance all the way to the left
103 ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk());
104
105 // Set the balance all the way to the right
106 ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk());
107
108 // Set the balance part way toward the left
109 ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk());
110
111 // Set the balance to a out of bounds value (driver should clamp)
112 ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk());
113
114 // Set the balance to a negative out of bounds value (driver should clamp)
115 ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk());
116
117 // Set the balance back to the middle
118 ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk());
119
120 // Set the balance back to the middle
121 audioControl->setBalanceTowardRight(0.0f).isOk();
122 }
123
124 struct FocusListenerMock : BnFocusListener {
125 MOCK_METHOD(Status, requestAudioFocus,
126 (const String16& usage, int32_t zoneId, AudioFocusChange focusGain));
127 MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId));
128 MOCK_METHOD(Status, requestAudioFocusWithMetaData,
129 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId,
130 AudioFocusChange focusGain));
131 MOCK_METHOD(Status, abandonAudioFocusWithMetaData,
132 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId));
133 };
134
135 /*
136 * Test focus listener registration.
137 *
138 * Verifies that:
139 * - registerFocusListener succeeds;
140 * - registering a second listener succeeds in replacing the first;
141 * - closing handle does not crash;
142 */
TEST_P(AudioControlAidl,FocusListenerRegistration)143 TEST_P(AudioControlAidl, FocusListenerRegistration) {
144 ALOGI("Focus listener test");
145
146 sp<FocusListenerMock> listener = new FocusListenerMock();
147 ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk());
148
149 sp<FocusListenerMock> listener2 = new FocusListenerMock();
150 ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk());
151 };
152
TEST_P(AudioControlAidl,FocusChangeExercise)153 TEST_P(AudioControlAidl, FocusChangeExercise) {
154 ALOGI("Focus Change test");
155
156 String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str());
157 ASSERT_TRUE(
158 audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk());
159 };
160
TEST_P(AudioControlAidl,MuteChangeExercise)161 TEST_P(AudioControlAidl, MuteChangeExercise) {
162 ALOGI("Mute change test");
163
164 MutingInfo mutingInfo;
165 mutingInfo.zoneId = 0;
166 mutingInfo.deviceAddressesToMute = {String16("address 1"), String16("address 2")};
167 mutingInfo.deviceAddressesToUnmute = {String16("address 3"), String16("address 4")};
168 std::vector<MutingInfo> mutingInfos = {mutingInfo};
169 ALOGI("Mute change test start");
170 ASSERT_TRUE(audioControl->onDevicesToMuteChange(mutingInfos).isOk());
171 }
172
TEST_P(AudioControlAidl,DuckChangeExercise)173 TEST_P(AudioControlAidl, DuckChangeExercise) {
174 ALOGI("Duck change test");
175
176 DuckingInfo duckingInfo;
177 duckingInfo.zoneId = 0;
178 duckingInfo.deviceAddressesToDuck = {String16("address 1"), String16("address 2")};
179 duckingInfo.deviceAddressesToUnduck = {String16("address 3"), String16("address 4")};
180 duckingInfo.usagesHoldingFocus = {
181 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()),
182 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
183 .c_str())};
184 std::vector<DuckingInfo> duckingInfos = {duckingInfo};
185 ALOGI("Duck change test start");
186 ASSERT_TRUE(audioControl->onDevicesToDuckChange(duckingInfos).isOk());
187 }
188
TEST_P(AudioControlAidl,FocusChangeWithMetaDataExercise)189 TEST_P(AudioControlAidl, FocusChangeWithMetaDataExercise) {
190 ALOGI("Focus Change test");
191
192 audiohalcommon::PlaybackTrackMetadata metadata;
193 metadata.usage = audiomediacommon::AudioUsage::MEDIA;
194 metadata.contentType = audiomediacommon::AudioContentType::MUSIC;
195 metadata.tags = {"com.google.android=VR"};
196 ASSERT_TRUE(
197 audioControl
198 ->onAudioFocusChangeWithMetaData(metadata, 0, AudioFocusChange::GAIN_TRANSIENT)
199 .isOk());
200 };
201
TEST_P(AudioControlAidl,SetAudioDeviceGainsChangedExercise)202 TEST_P(AudioControlAidl, SetAudioDeviceGainsChangedExercise) {
203 ALOGI("Set Audio Gains Changed test");
204
205 const std::vector<Reasons> reasons{Reasons::FORCED_MASTER_MUTE, Reasons::NAV_DUCKING};
206 AudioGainConfigInfo agci1;
207 agci1.zoneId = 0;
208 agci1.devicePortAddress = String16("address 1");
209 agci1.volumeIndex = 8;
210
211 AudioGainConfigInfo agci2;
212 agci1.zoneId = 0;
213 agci1.devicePortAddress = String16("address 2");
214 agci1.volumeIndex = 1;
215
216 std::vector<AudioGainConfigInfo> gains{agci1, agci2};
217 ASSERT_TRUE(audioControl->setAudioDeviceGainsChanged(reasons, gains).isOk());
218 }
219
220 /*
221 * Test Audio Gain Callback registration.
222 *
223 * Verifies that:
224 * - registerGainCallback succeeds;
225 * - registering a second callback succeeds in replacing the first;
226 * - closing handle does not crash;
227 */
228 struct AudioGainCallbackMock : BnAudioGainCallback {
229 MOCK_METHOD(Status, onAudioDeviceGainsChanged,
230 (const std::vector<Reasons>& reasons,
231 const std::vector<AudioGainConfigInfo>& gains));
232 };
233
TEST_P(AudioControlAidl,AudioGainCallbackRegistration)234 TEST_P(AudioControlAidl, AudioGainCallbackRegistration) {
235 ALOGI("Focus listener test");
236
237 sp<AudioGainCallbackMock> gainCallback = new AudioGainCallbackMock();
238 ASSERT_TRUE(audioControl->registerGainCallback(gainCallback).isOk());
239
240 sp<AudioGainCallbackMock> gainCallback2 = new AudioGainCallbackMock();
241 ASSERT_TRUE(audioControl->registerGainCallback(gainCallback2).isOk());
242 }
243
244 /*
245 * Test Module change Callback registration.
246 *
247 * Verifies that:
248 * - setModuleChangeCallback succeeds
249 * - setting a double callback fails with exception
250 * - clearModuleChangeCallback succeeds
251 * - setting with nullptr callback fails with exception
252 * - closing handle does not crash
253 */
254 struct ModuleChangeCallbackMock : BnModuleChangeCallback {
255 MOCK_METHOD(Status, onAudioPortsChanged,
256 (const std::vector<android::media::audio::common::AudioPort>& audioPorts));
257 };
258
TEST_P(AudioControlAidl,RegisterModuleChangeCallbackTwiceThrowsException)259 TEST_P(AudioControlAidl, RegisterModuleChangeCallbackTwiceThrowsException) {
260 ALOGI("Register Module change callback test");
261 if (!isAidlVersionAtleast(kAidlVersionThree)) {
262 GTEST_SKIP() << "Device does not support the new APIs for module change callback";
263 return;
264 }
265
266 // make sure no stale callbacks.
267 audioControl->clearModuleChangeCallback();
268
269 sp<ModuleChangeCallbackMock> moduleChangeCallback = new ModuleChangeCallbackMock();
270 auto status = audioControl->setModuleChangeCallback(moduleChangeCallback);
271 EXPECT_THAT(status.exceptionCode(),
272 AnyOf(Eq(Status::EX_NONE), Eq(Status::EX_UNSUPPORTED_OPERATION)));
273 if (!status.isOk()) return;
274
275 sp<ModuleChangeCallbackMock> moduleChangeCallback2 = new ModuleChangeCallbackMock();
276 // no need to check for unsupported feature
277 EXPECT_EQ(Status::EX_ILLEGAL_STATE,
278 audioControl->setModuleChangeCallback(moduleChangeCallback2).exceptionCode());
279 ASSERT_TRUE(audioControl->clearModuleChangeCallback().isOk());
280 ASSERT_TRUE(audioControl->setModuleChangeCallback(moduleChangeCallback2).isOk());
281 }
282
TEST_P(AudioControlAidl,RegisterModuleChangeNullCallbackThrowsException)283 TEST_P(AudioControlAidl, RegisterModuleChangeNullCallbackThrowsException) {
284 ALOGI("Register Module change callback with nullptr test");
285 if (!isAidlVersionAtleast(kAidlVersionThree)) {
286 GTEST_SKIP() << "Device does not support the new APIs for module change callback";
287 return;
288 }
289
290 auto status = audioControl->setModuleChangeCallback(nullptr);
291 EXPECT_THAT(status.exceptionCode(),
292 AnyOf(Eq(Status::EX_ILLEGAL_ARGUMENT), Eq(Status::EX_UNSUPPORTED_OPERATION)));
293 }
294
295 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
296 INSTANTIATE_TEST_SUITE_P(
297 Audiocontrol, AudioControlAidl,
298 testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
299 android::PrintInstanceNameToString);
300
main(int argc,char ** argv)301 int main(int argc, char** argv) {
302 ::testing::InitGoogleTest(&argc, argv);
303 ProcessState::self()->setThreadPoolMaxThreadCount(1);
304 ProcessState::self()->startThreadPool();
305 return RUN_ALL_TESTS();
306 }
307