Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 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 | |
Francois Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 22 | #include <android/hardware/automotive/audiocontrol/BnAudioGainCallback.h> |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 23 | #include <android/hardware/automotive/audiocontrol/BnFocusListener.h> |
Raj Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 24 | #include <android/hardware/automotive/audiocontrol/BnModuleChangeCallback.h> |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 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; |
Francois Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 35 | using android::hardware::automotive::audiocontrol::AudioGainConfigInfo; |
| 36 | using android::hardware::automotive::audiocontrol::BnAudioGainCallback; |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 37 | using android::hardware::automotive::audiocontrol::BnFocusListener; |
Raj Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 38 | using android::hardware::automotive::audiocontrol::BnModuleChangeCallback; |
Hayden Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 39 | using android::hardware::automotive::audiocontrol::DuckingInfo; |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 40 | using android::hardware::automotive::audiocontrol::IAudioControl; |
Raj Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 41 | using android::hardware::automotive::audiocontrol::IModuleChangeCallback; |
Oscar Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 42 | using android::hardware::automotive::audiocontrol::MutingInfo; |
Francois Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 43 | using android::hardware::automotive::audiocontrol::Reasons; |
Raj Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 44 | using ::testing::AnyOf; |
| 45 | using ::testing::Eq; |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 46 | |
Hayden Gomes | e502a60 | 2020-11-06 15:15:26 -0800 | [diff] [blame] | 47 | #include "android_audio_policy_configuration_V7_0.h" |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 48 | |
| 49 | namespace xsd { |
Hayden Gomes | e502a60 | 2020-11-06 15:15:26 -0800 | [diff] [blame] | 50 | using namespace android::audio::policy::configuration::V7_0; |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Francois Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 53 | namespace audiohalcommon = android::hardware::audio::common; |
| 54 | namespace audiomediacommon = android::media::audio::common; |
| 55 | |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 56 | class 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 Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 63 | void TearDown() override { audioControl = nullptr; } |
| 64 | |
Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 65 | sp<IAudioControl> audioControl; |
| 66 | int32_t capabilities; |
| 67 | }; |
| 68 | |
| 69 | TEST_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 | |
| 91 | TEST_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 | |
| 116 | struct 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 Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 120 | 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 Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 125 | }; |
| 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 | */ |
| 135 | TEST_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 | |
| 145 | TEST_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 Azucena | b8e5cd0 | 2020-12-16 13:53:14 -0800 | [diff] [blame] | 153 | TEST_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 Gomes | ad81670 | 2020-12-14 15:29:29 -0800 | [diff] [blame] | 165 | TEST_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 Gaffie | e5af282 | 2022-02-09 11:36:55 +0100 | [diff] [blame] | 181 | TEST_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 | |
| 194 | TEST_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 | */ |
| 220 | struct AudioGainCallbackMock : BnAudioGainCallback { |
| 221 | MOCK_METHOD(Status, onAudioDeviceGainsChanged, |
| 222 | (const std::vector<Reasons>& reasons, |
| 223 | const std::vector<AudioGainConfigInfo>& gains)); |
| 224 | }; |
| 225 | |
| 226 | TEST_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 Goparaju | a8bf907 | 2023-02-05 14:58:40 -0800 | [diff] [blame] | 236 | /* |
| 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 | */ |
| 246 | struct ModuleChangeCallbackMock : BnModuleChangeCallback { |
| 247 | MOCK_METHOD(Status, onAudioPortsChanged, |
| 248 | (const std::vector<android::media::audio::common::AudioPort>& audioPorts)); |
| 249 | }; |
| 250 | |
| 251 | TEST_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 | |
| 270 | TEST_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 Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 277 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl); |
| 278 | INSTANTIATE_TEST_SUITE_P( |
| 279 | Audiocontrol, AudioControlAidl, |
| 280 | testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)), |
| 281 | android::PrintInstanceNameToString); |
| 282 | |
| 283 | int 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 | } |