blob: 4e7e963981432aa89c767d486f729dd0ce4fbfdc [file] [log] [blame]
Hayden Gomesa521acf2020-11-02 12:29:05 -08001/*
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
Francois Gaffiee5af2822022-02-09 11:36:55 +010022#include <android/hardware/automotive/audiocontrol/BnAudioGainCallback.h>
Hayden Gomesa521acf2020-11-02 12:29:05 -080023#include <android/hardware/automotive/audiocontrol/BnFocusListener.h>
Raj Goparajua8bf9072023-02-05 14:58:40 -080024#include <android/hardware/automotive/audiocontrol/BnModuleChangeCallback.h>
Hayden Gomesa521acf2020-11-02 12:29:05 -080025#include <android/hardware/automotive/audiocontrol/IAudioControl.h>
26#include <android/log.h>
27#include <binder/IServiceManager.h>
28#include <binder/ProcessState.h>
29
30using android::ProcessState;
31using android::sp;
32using android::String16;
33using android::binder::Status;
34using android::hardware::automotive::audiocontrol::AudioFocusChange;
Francois Gaffiee5af2822022-02-09 11:36:55 +010035using android::hardware::automotive::audiocontrol::AudioGainConfigInfo;
36using android::hardware::automotive::audiocontrol::BnAudioGainCallback;
Hayden Gomesa521acf2020-11-02 12:29:05 -080037using android::hardware::automotive::audiocontrol::BnFocusListener;
Raj Goparajua8bf9072023-02-05 14:58:40 -080038using android::hardware::automotive::audiocontrol::BnModuleChangeCallback;
Hayden Gomesad816702020-12-14 15:29:29 -080039using android::hardware::automotive::audiocontrol::DuckingInfo;
Hayden Gomesa521acf2020-11-02 12:29:05 -080040using android::hardware::automotive::audiocontrol::IAudioControl;
Raj Goparajua8bf9072023-02-05 14:58:40 -080041using android::hardware::automotive::audiocontrol::IModuleChangeCallback;
Oscar Azucenab8e5cd02020-12-16 13:53:14 -080042using android::hardware::automotive::audiocontrol::MutingInfo;
Francois Gaffiee5af2822022-02-09 11:36:55 +010043using android::hardware::automotive::audiocontrol::Reasons;
Raj Goparajua8bf9072023-02-05 14:58:40 -080044using ::testing::AnyOf;
45using ::testing::Eq;
Hayden Gomesa521acf2020-11-02 12:29:05 -080046
Hayden Gomese502a602020-11-06 15:15:26 -080047#include "android_audio_policy_configuration_V7_0.h"
Hayden Gomesa521acf2020-11-02 12:29:05 -080048
49namespace xsd {
Hayden Gomese502a602020-11-06 15:15:26 -080050using namespace android::audio::policy::configuration::V7_0;
Hayden Gomesa521acf2020-11-02 12:29:05 -080051}
52
Francois Gaffiee5af2822022-02-09 11:36:55 +010053namespace audiohalcommon = android::hardware::audio::common;
54namespace audiomediacommon = android::media::audio::common;
55
Raj Goparaju4a118d42023-05-13 13:07:41 -070056namespace {
57constexpr int32_t kAidlVersionThree = 3;
58}
59
Hayden Gomesa521acf2020-11-02 12:29:05 -080060class AudioControlAidl : public testing::TestWithParam<std::string> {
61 public:
62 virtual void SetUp() override {
63 audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
64 ASSERT_NE(audioControl, nullptr);
Raj Goparaju4a118d42023-05-13 13:07:41 -070065 aidlVersion = audioControl->getInterfaceVersion();
Hayden Gomesa521acf2020-11-02 12:29:05 -080066 }
67
Raj Goparajua8bf9072023-02-05 14:58:40 -080068 void TearDown() override { audioControl = nullptr; }
69
Raj Goparaju4a118d42023-05-13 13:07:41 -070070 bool isAidlVersionAtleast(int version) const { return aidlVersion >= version; }
71
Hayden Gomesa521acf2020-11-02 12:29:05 -080072 sp<IAudioControl> audioControl;
73 int32_t capabilities;
Raj Goparaju4a118d42023-05-13 13:07:41 -070074 int32_t aidlVersion;
Hayden Gomesa521acf2020-11-02 12:29:05 -080075};
76
77TEST_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
99TEST_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
124struct 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));
Francois Gaffiee5af2822022-02-09 11:36:55 +0100128 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));
Hayden Gomesa521acf2020-11-02 12:29:05 -0800133};
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 */
143TEST_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
153TEST_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
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800161TEST_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
Hayden Gomesad816702020-12-14 15:29:29 -0800173TEST_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
Francois Gaffiee5af2822022-02-09 11:36:55 +0100189TEST_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
202TEST_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 */
228struct AudioGainCallbackMock : BnAudioGainCallback {
229 MOCK_METHOD(Status, onAudioDeviceGainsChanged,
230 (const std::vector<Reasons>& reasons,
231 const std::vector<AudioGainConfigInfo>& gains));
232};
233
234TEST_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
Raj Goparajua8bf9072023-02-05 14:58:40 -0800244/*
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 */
254struct ModuleChangeCallbackMock : BnModuleChangeCallback {
255 MOCK_METHOD(Status, onAudioPortsChanged,
256 (const std::vector<android::media::audio::common::AudioPort>& audioPorts));
257};
258
259TEST_P(AudioControlAidl, RegisterModuleChangeCallbackTwiceThrowsException) {
260 ALOGI("Register Module change callback test");
Raj Goparaju4a118d42023-05-13 13:07:41 -0700261 if (!isAidlVersionAtleast(kAidlVersionThree)) {
262 GTEST_SKIP() << "Device does not support the new APIs for module change callback";
263 return;
264 }
265
Raj Goparajua8bf9072023-02-05 14:58:40 -0800266 // 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
283TEST_P(AudioControlAidl, RegisterModuleChangeNullCallbackThrowsException) {
284 ALOGI("Register Module change callback with nullptr test");
Raj Goparaju4a118d42023-05-13 13:07:41 -0700285 if (!isAidlVersionAtleast(kAidlVersionThree)) {
286 GTEST_SKIP() << "Device does not support the new APIs for module change callback";
287 return;
288 }
289
Raj Goparajua8bf9072023-02-05 14:58:40 -0800290 auto status = audioControl->setModuleChangeCallback(nullptr);
291 EXPECT_THAT(status.exceptionCode(),
292 AnyOf(Eq(Status::EX_ILLEGAL_ARGUMENT), Eq(Status::EX_UNSUPPORTED_OPERATION)));
293}
294
Hayden Gomesa521acf2020-11-02 12:29:05 -0800295GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
296INSTANTIATE_TEST_SUITE_P(
297 Audiocontrol, AudioControlAidl,
298 testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
299 android::PrintInstanceNameToString);
300
301int 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}