blob: f4e3b5ab9de47eab017f04b31c42a332513b8123 [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
Hayden Gomesa521acf2020-11-02 12:29:05 -080056class AudioControlAidl : public testing::TestWithParam<std::string> {
57 public:
58 virtual void SetUp() override {
59 audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
60 ASSERT_NE(audioControl, nullptr);
61 }
62
Raj Goparajua8bf9072023-02-05 14:58:40 -080063 void TearDown() override { audioControl = nullptr; }
64
Hayden Gomesa521acf2020-11-02 12:29:05 -080065 sp<IAudioControl> audioControl;
66 int32_t capabilities;
67};
68
69TEST_P(AudioControlAidl, OnSetFadeTowardsFront) {
70 ALOGI("Fader exercise test (silent)");
71
72 // Set the fader all the way to the back
73 ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk());
74
75 // Set the fader all the way to the front
76 ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk());
77
78 // Set the fader part way toward the back
79 ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk());
80
81 // Set the fader to a out of bounds value (driver should clamp)
82 ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk());
83
84 // Set the fader to a negative out of bounds value (driver should clamp)
85 ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk());
86
87 // Set the fader back to the middle
88 ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk());
89}
90
91TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) {
92 ALOGI("Balance exercise test (silent)");
93
94 // Set the balance all the way to the left
95 ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk());
96
97 // Set the balance all the way to the right
98 ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk());
99
100 // Set the balance part way toward the left
101 ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk());
102
103 // Set the balance to a out of bounds value (driver should clamp)
104 ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk());
105
106 // Set the balance to a negative out of bounds value (driver should clamp)
107 ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk());
108
109 // Set the balance back to the middle
110 ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk());
111
112 // Set the balance back to the middle
113 audioControl->setBalanceTowardRight(0.0f).isOk();
114}
115
116struct FocusListenerMock : BnFocusListener {
117 MOCK_METHOD(Status, requestAudioFocus,
118 (const String16& usage, int32_t zoneId, AudioFocusChange focusGain));
119 MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId));
Francois Gaffiee5af2822022-02-09 11:36:55 +0100120 MOCK_METHOD(Status, requestAudioFocusWithMetaData,
121 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId,
122 AudioFocusChange focusGain));
123 MOCK_METHOD(Status, abandonAudioFocusWithMetaData,
124 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId));
Hayden Gomesa521acf2020-11-02 12:29:05 -0800125};
126
127/*
128 * Test focus listener registration.
129 *
130 * Verifies that:
131 * - registerFocusListener succeeds;
132 * - registering a second listener succeeds in replacing the first;
133 * - closing handle does not crash;
134 */
135TEST_P(AudioControlAidl, FocusListenerRegistration) {
136 ALOGI("Focus listener test");
137
138 sp<FocusListenerMock> listener = new FocusListenerMock();
139 ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk());
140
141 sp<FocusListenerMock> listener2 = new FocusListenerMock();
142 ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk());
143};
144
145TEST_P(AudioControlAidl, FocusChangeExercise) {
146 ALOGI("Focus Change test");
147
148 String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str());
149 ASSERT_TRUE(
150 audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk());
151};
152
Oscar Azucenab8e5cd02020-12-16 13:53:14 -0800153TEST_P(AudioControlAidl, MuteChangeExercise) {
154 ALOGI("Mute change test");
155
156 MutingInfo mutingInfo;
157 mutingInfo.zoneId = 0;
158 mutingInfo.deviceAddressesToMute = {String16("address 1"), String16("address 2")};
159 mutingInfo.deviceAddressesToUnmute = {String16("address 3"), String16("address 4")};
160 std::vector<MutingInfo> mutingInfos = {mutingInfo};
161 ALOGI("Mute change test start");
162 ASSERT_TRUE(audioControl->onDevicesToMuteChange(mutingInfos).isOk());
163}
164
Hayden Gomesad816702020-12-14 15:29:29 -0800165TEST_P(AudioControlAidl, DuckChangeExercise) {
166 ALOGI("Duck change test");
167
168 DuckingInfo duckingInfo;
169 duckingInfo.zoneId = 0;
170 duckingInfo.deviceAddressesToDuck = {String16("address 1"), String16("address 2")};
171 duckingInfo.deviceAddressesToUnduck = {String16("address 3"), String16("address 4")};
172 duckingInfo.usagesHoldingFocus = {
173 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()),
174 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
175 .c_str())};
176 std::vector<DuckingInfo> duckingInfos = {duckingInfo};
177 ALOGI("Duck change test start");
178 ASSERT_TRUE(audioControl->onDevicesToDuckChange(duckingInfos).isOk());
179}
180
Francois Gaffiee5af2822022-02-09 11:36:55 +0100181TEST_P(AudioControlAidl, FocusChangeWithMetaDataExercise) {
182 ALOGI("Focus Change test");
183
184 audiohalcommon::PlaybackTrackMetadata metadata;
185 metadata.usage = audiomediacommon::AudioUsage::MEDIA;
186 metadata.contentType = audiomediacommon::AudioContentType::MUSIC;
187 metadata.tags = {"com.google.android=VR"};
188 ASSERT_TRUE(
189 audioControl
190 ->onAudioFocusChangeWithMetaData(metadata, 0, AudioFocusChange::GAIN_TRANSIENT)
191 .isOk());
192};
193
194TEST_P(AudioControlAidl, SetAudioDeviceGainsChangedExercise) {
195 ALOGI("Set Audio Gains Changed test");
196
197 const std::vector<Reasons> reasons{Reasons::FORCED_MASTER_MUTE, Reasons::NAV_DUCKING};
198 AudioGainConfigInfo agci1;
199 agci1.zoneId = 0;
200 agci1.devicePortAddress = String16("address 1");
201 agci1.volumeIndex = 8;
202
203 AudioGainConfigInfo agci2;
204 agci1.zoneId = 0;
205 agci1.devicePortAddress = String16("address 2");
206 agci1.volumeIndex = 1;
207
208 std::vector<AudioGainConfigInfo> gains{agci1, agci2};
209 ASSERT_TRUE(audioControl->setAudioDeviceGainsChanged(reasons, gains).isOk());
210}
211
212/*
213 * Test Audio Gain Callback registration.
214 *
215 * Verifies that:
216 * - registerGainCallback succeeds;
217 * - registering a second callback succeeds in replacing the first;
218 * - closing handle does not crash;
219 */
220struct AudioGainCallbackMock : BnAudioGainCallback {
221 MOCK_METHOD(Status, onAudioDeviceGainsChanged,
222 (const std::vector<Reasons>& reasons,
223 const std::vector<AudioGainConfigInfo>& gains));
224};
225
226TEST_P(AudioControlAidl, AudioGainCallbackRegistration) {
227 ALOGI("Focus listener test");
228
229 sp<AudioGainCallbackMock> gainCallback = new AudioGainCallbackMock();
230 ASSERT_TRUE(audioControl->registerGainCallback(gainCallback).isOk());
231
232 sp<AudioGainCallbackMock> gainCallback2 = new AudioGainCallbackMock();
233 ASSERT_TRUE(audioControl->registerGainCallback(gainCallback2).isOk());
234}
235
Raj Goparajua8bf9072023-02-05 14:58:40 -0800236/*
237 * Test Module change Callback registration.
238 *
239 * Verifies that:
240 * - setModuleChangeCallback succeeds
241 * - setting a double callback fails with exception
242 * - clearModuleChangeCallback succeeds
243 * - setting with nullptr callback fails with exception
244 * - closing handle does not crash
245 */
246struct ModuleChangeCallbackMock : BnModuleChangeCallback {
247 MOCK_METHOD(Status, onAudioPortsChanged,
248 (const std::vector<android::media::audio::common::AudioPort>& audioPorts));
249};
250
251TEST_P(AudioControlAidl, RegisterModuleChangeCallbackTwiceThrowsException) {
252 ALOGI("Register Module change callback test");
253 // make sure no stale callbacks.
254 audioControl->clearModuleChangeCallback();
255
256 sp<ModuleChangeCallbackMock> moduleChangeCallback = new ModuleChangeCallbackMock();
257 auto status = audioControl->setModuleChangeCallback(moduleChangeCallback);
258 EXPECT_THAT(status.exceptionCode(),
259 AnyOf(Eq(Status::EX_NONE), Eq(Status::EX_UNSUPPORTED_OPERATION)));
260 if (!status.isOk()) return;
261
262 sp<ModuleChangeCallbackMock> moduleChangeCallback2 = new ModuleChangeCallbackMock();
263 // no need to check for unsupported feature
264 EXPECT_EQ(Status::EX_ILLEGAL_STATE,
265 audioControl->setModuleChangeCallback(moduleChangeCallback2).exceptionCode());
266 ASSERT_TRUE(audioControl->clearModuleChangeCallback().isOk());
267 ASSERT_TRUE(audioControl->setModuleChangeCallback(moduleChangeCallback2).isOk());
268}
269
270TEST_P(AudioControlAidl, RegisterModuleChangeNullCallbackThrowsException) {
271 ALOGI("Register Module change callback with nullptr test");
272 auto status = audioControl->setModuleChangeCallback(nullptr);
273 EXPECT_THAT(status.exceptionCode(),
274 AnyOf(Eq(Status::EX_ILLEGAL_ARGUMENT), Eq(Status::EX_UNSUPPORTED_OPERATION)));
275}
276
Hayden Gomesa521acf2020-11-02 12:29:05 -0800277GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
278INSTANTIATE_TEST_SUITE_P(
279 Audiocontrol, AudioControlAidl,
280 testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
281 android::PrintInstanceNameToString);
282
283int main(int argc, char** argv) {
284 ::testing::InitGoogleTest(&argc, argv);
285 ProcessState::self()->setThreadPoolMaxThreadCount(1);
286 ProcessState::self()->startThreadPool();
287 return RUN_ALL_TESTS();
288}