• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IAudioControl.h>
25 #include <android/log.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/ProcessState.h>
28 
29 using android::ProcessState;
30 using android::sp;
31 using android::String16;
32 using android::binder::Status;
33 using android::hardware::automotive::audiocontrol::AudioFocusChange;
34 using android::hardware::automotive::audiocontrol::AudioGainConfigInfo;
35 using android::hardware::automotive::audiocontrol::BnAudioGainCallback;
36 using android::hardware::automotive::audiocontrol::BnFocusListener;
37 using android::hardware::automotive::audiocontrol::DuckingInfo;
38 using android::hardware::automotive::audiocontrol::IAudioControl;
39 using android::hardware::automotive::audiocontrol::MutingInfo;
40 using android::hardware::automotive::audiocontrol::Reasons;
41 
42 #include "android_audio_policy_configuration_V7_0.h"
43 
44 namespace xsd {
45 using namespace android::audio::policy::configuration::V7_0;
46 }
47 
48 namespace audiohalcommon = android::hardware::audio::common;
49 namespace audiomediacommon = android::media::audio::common;
50 
51 class AudioControlAidl : public testing::TestWithParam<std::string> {
52   public:
SetUp()53     virtual void SetUp() override {
54         audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
55         ASSERT_NE(audioControl, nullptr);
56     }
57 
58     sp<IAudioControl> audioControl;
59     int32_t capabilities;
60 };
61 
TEST_P(AudioControlAidl,OnSetFadeTowardsFront)62 TEST_P(AudioControlAidl, OnSetFadeTowardsFront) {
63     ALOGI("Fader exercise test (silent)");
64 
65     // Set the fader all the way to the back
66     ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk());
67 
68     // Set the fader all the way to the front
69     ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk());
70 
71     // Set the fader part way toward the back
72     ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk());
73 
74     // Set the fader to a out of bounds value (driver should clamp)
75     ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk());
76 
77     // Set the fader to a negative out of bounds value (driver should clamp)
78     ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk());
79 
80     // Set the fader back to the middle
81     ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk());
82 }
83 
TEST_P(AudioControlAidl,OnSetBalanceTowardsRight)84 TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) {
85     ALOGI("Balance exercise test (silent)");
86 
87     // Set the balance all the way to the left
88     ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk());
89 
90     // Set the balance all the way to the right
91     ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk());
92 
93     // Set the balance part way toward the left
94     ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk());
95 
96     // Set the balance to a out of bounds value (driver should clamp)
97     ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk());
98 
99     // Set the balance to a negative out of bounds value (driver should clamp)
100     ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk());
101 
102     // Set the balance back to the middle
103     ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk());
104 
105     // Set the balance back to the middle
106     audioControl->setBalanceTowardRight(0.0f).isOk();
107 }
108 
109 struct FocusListenerMock : BnFocusListener {
110     MOCK_METHOD(Status, requestAudioFocus,
111                 (const String16& usage, int32_t zoneId, AudioFocusChange focusGain));
112     MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId));
113     MOCK_METHOD(Status, requestAudioFocusWithMetaData,
114                 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId,
115                  AudioFocusChange focusGain));
116     MOCK_METHOD(Status, abandonAudioFocusWithMetaData,
117                 (const audiohalcommon::PlaybackTrackMetadata& metaData, int32_t zoneId));
118 };
119 
120 /*
121  * Test focus listener registration.
122  *
123  * Verifies that:
124  * - registerFocusListener succeeds;
125  * - registering a second listener succeeds in replacing the first;
126  * - closing handle does not crash;
127  */
TEST_P(AudioControlAidl,FocusListenerRegistration)128 TEST_P(AudioControlAidl, FocusListenerRegistration) {
129     ALOGI("Focus listener test");
130 
131     sp<FocusListenerMock> listener = new FocusListenerMock();
132     ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk());
133 
134     sp<FocusListenerMock> listener2 = new FocusListenerMock();
135     ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk());
136 };
137 
TEST_P(AudioControlAidl,FocusChangeExercise)138 TEST_P(AudioControlAidl, FocusChangeExercise) {
139     ALOGI("Focus Change test");
140 
141     String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str());
142     ASSERT_TRUE(
143             audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk());
144 };
145 
TEST_P(AudioControlAidl,MuteChangeExercise)146 TEST_P(AudioControlAidl, MuteChangeExercise) {
147     ALOGI("Mute change test");
148 
149     MutingInfo mutingInfo;
150     mutingInfo.zoneId = 0;
151     mutingInfo.deviceAddressesToMute = {String16("address 1"), String16("address 2")};
152     mutingInfo.deviceAddressesToUnmute = {String16("address 3"), String16("address 4")};
153     std::vector<MutingInfo> mutingInfos = {mutingInfo};
154     ALOGI("Mute change test start");
155     ASSERT_TRUE(audioControl->onDevicesToMuteChange(mutingInfos).isOk());
156 }
157 
TEST_P(AudioControlAidl,DuckChangeExercise)158 TEST_P(AudioControlAidl, DuckChangeExercise) {
159     ALOGI("Duck change test");
160 
161     DuckingInfo duckingInfo;
162     duckingInfo.zoneId = 0;
163     duckingInfo.deviceAddressesToDuck = {String16("address 1"), String16("address 2")};
164     duckingInfo.deviceAddressesToUnduck = {String16("address 3"), String16("address 4")};
165     duckingInfo.usagesHoldingFocus = {
166             String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()),
167             String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
168                              .c_str())};
169     std::vector<DuckingInfo> duckingInfos = {duckingInfo};
170     ALOGI("Duck change test start");
171     ASSERT_TRUE(audioControl->onDevicesToDuckChange(duckingInfos).isOk());
172 }
173 
TEST_P(AudioControlAidl,FocusChangeWithMetaDataExercise)174 TEST_P(AudioControlAidl, FocusChangeWithMetaDataExercise) {
175     ALOGI("Focus Change test");
176 
177     audiohalcommon::PlaybackTrackMetadata metadata;
178     metadata.usage = audiomediacommon::AudioUsage::MEDIA;
179     metadata.contentType = audiomediacommon::AudioContentType::MUSIC;
180     metadata.tags = {"com.google.android=VR"};
181     ASSERT_TRUE(
182             audioControl
183                     ->onAudioFocusChangeWithMetaData(metadata, 0, AudioFocusChange::GAIN_TRANSIENT)
184                     .isOk());
185 };
186 
TEST_P(AudioControlAidl,SetAudioDeviceGainsChangedExercise)187 TEST_P(AudioControlAidl, SetAudioDeviceGainsChangedExercise) {
188     ALOGI("Set Audio Gains Changed test");
189 
190     const std::vector<Reasons> reasons{Reasons::FORCED_MASTER_MUTE, Reasons::NAV_DUCKING};
191     AudioGainConfigInfo agci1;
192     agci1.zoneId = 0;
193     agci1.devicePortAddress = String16("address 1");
194     agci1.volumeIndex = 8;
195 
196     AudioGainConfigInfo agci2;
197     agci1.zoneId = 0;
198     agci1.devicePortAddress = String16("address 2");
199     agci1.volumeIndex = 1;
200 
201     std::vector<AudioGainConfigInfo> gains{agci1, agci2};
202     ASSERT_TRUE(audioControl->setAudioDeviceGainsChanged(reasons, gains).isOk());
203 }
204 
205 /*
206  * Test Audio Gain Callback registration.
207  *
208  * Verifies that:
209  * - registerGainCallback succeeds;
210  * - registering a second callback succeeds in replacing the first;
211  * - closing handle does not crash;
212  */
213 struct AudioGainCallbackMock : BnAudioGainCallback {
214     MOCK_METHOD(Status, onAudioDeviceGainsChanged,
215                 (const std::vector<Reasons>& reasons,
216                  const std::vector<AudioGainConfigInfo>& gains));
217 };
218 
TEST_P(AudioControlAidl,AudioGainCallbackRegistration)219 TEST_P(AudioControlAidl, AudioGainCallbackRegistration) {
220     ALOGI("Focus listener test");
221 
222     sp<AudioGainCallbackMock> gainCallback = new AudioGainCallbackMock();
223     ASSERT_TRUE(audioControl->registerGainCallback(gainCallback).isOk());
224 
225     sp<AudioGainCallbackMock> gainCallback2 = new AudioGainCallbackMock();
226     ASSERT_TRUE(audioControl->registerGainCallback(gainCallback2).isOk());
227 }
228 
229 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
230 INSTANTIATE_TEST_SUITE_P(
231         Audiocontrol, AudioControlAidl,
232         testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
233         android::PrintInstanceNameToString);
234 
main(int argc,char ** argv)235 int main(int argc, char** argv) {
236     ::testing::InitGoogleTest(&argc, argv);
237     ProcessState::self()->setThreadPoolMaxThreadCount(1);
238     ProcessState::self()->startThreadPool();
239     return RUN_ALL_TESTS();
240 }
241