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/BnFocusListener.h>
23 #include <android/hardware/automotive/audiocontrol/IAudioControl.h>
24 #include <android/log.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/ProcessState.h>
27 
28 using android::ProcessState;
29 using android::sp;
30 using android::String16;
31 using android::binder::Status;
32 using android::hardware::automotive::audiocontrol::AudioFocusChange;
33 using android::hardware::automotive::audiocontrol::BnFocusListener;
34 using android::hardware::automotive::audiocontrol::DuckingInfo;
35 using android::hardware::automotive::audiocontrol::IAudioControl;
36 using android::hardware::automotive::audiocontrol::MutingInfo;
37 
38 #include "android_audio_policy_configuration_V7_0.h"
39 
40 namespace xsd {
41 using namespace android::audio::policy::configuration::V7_0;
42 }
43 
44 class AudioControlAidl : public testing::TestWithParam<std::string> {
45   public:
SetUp()46     virtual void SetUp() override {
47         audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
48         ASSERT_NE(audioControl, nullptr);
49     }
50 
51     sp<IAudioControl> audioControl;
52     int32_t capabilities;
53 };
54 
TEST_P(AudioControlAidl,OnSetFadeTowardsFront)55 TEST_P(AudioControlAidl, OnSetFadeTowardsFront) {
56     ALOGI("Fader exercise test (silent)");
57 
58     // Set the fader all the way to the back
59     ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk());
60 
61     // Set the fader all the way to the front
62     ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk());
63 
64     // Set the fader part way toward the back
65     ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk());
66 
67     // Set the fader to a out of bounds value (driver should clamp)
68     ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk());
69 
70     // Set the fader to a negative out of bounds value (driver should clamp)
71     ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk());
72 
73     // Set the fader back to the middle
74     ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk());
75 }
76 
TEST_P(AudioControlAidl,OnSetBalanceTowardsRight)77 TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) {
78     ALOGI("Balance exercise test (silent)");
79 
80     // Set the balance all the way to the left
81     ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk());
82 
83     // Set the balance all the way to the right
84     ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk());
85 
86     // Set the balance part way toward the left
87     ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk());
88 
89     // Set the balance to a out of bounds value (driver should clamp)
90     ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk());
91 
92     // Set the balance to a negative out of bounds value (driver should clamp)
93     ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk());
94 
95     // Set the balance back to the middle
96     ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk());
97 
98     // Set the balance back to the middle
99     audioControl->setBalanceTowardRight(0.0f).isOk();
100 }
101 
102 struct FocusListenerMock : BnFocusListener {
103     MOCK_METHOD(Status, requestAudioFocus,
104                 (const String16& usage, int32_t zoneId, AudioFocusChange focusGain));
105     MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId));
106 };
107 
108 /*
109  * Test focus listener registration.
110  *
111  * Verifies that:
112  * - registerFocusListener succeeds;
113  * - registering a second listener succeeds in replacing the first;
114  * - closing handle does not crash;
115  */
TEST_P(AudioControlAidl,FocusListenerRegistration)116 TEST_P(AudioControlAidl, FocusListenerRegistration) {
117     ALOGI("Focus listener test");
118 
119     sp<FocusListenerMock> listener = new FocusListenerMock();
120     ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk());
121 
122     sp<FocusListenerMock> listener2 = new FocusListenerMock();
123     ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk());
124 };
125 
TEST_P(AudioControlAidl,FocusChangeExercise)126 TEST_P(AudioControlAidl, FocusChangeExercise) {
127     ALOGI("Focus Change test");
128 
129     String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str());
130     ASSERT_TRUE(
131             audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk());
132 };
133 
TEST_P(AudioControlAidl,MuteChangeExercise)134 TEST_P(AudioControlAidl, MuteChangeExercise) {
135     ALOGI("Mute change test");
136 
137     MutingInfo mutingInfo;
138     mutingInfo.zoneId = 0;
139     mutingInfo.deviceAddressesToMute = {String16("address 1"), String16("address 2")};
140     mutingInfo.deviceAddressesToUnmute = {String16("address 3"), String16("address 4")};
141     std::vector<MutingInfo> mutingInfos = {mutingInfo};
142     ALOGI("Mute change test start");
143     ASSERT_TRUE(audioControl->onDevicesToMuteChange(mutingInfos).isOk());
144 }
145 
TEST_P(AudioControlAidl,DuckChangeExercise)146 TEST_P(AudioControlAidl, DuckChangeExercise) {
147     ALOGI("Duck change test");
148 
149     DuckingInfo duckingInfo;
150     duckingInfo.zoneId = 0;
151     duckingInfo.deviceAddressesToDuck = {String16("address 1"), String16("address 2")};
152     duckingInfo.deviceAddressesToUnduck = {String16("address 3"), String16("address 4")};
153     duckingInfo.usagesHoldingFocus = {
154             String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()),
155             String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
156                              .c_str())};
157     std::vector<DuckingInfo> duckingInfos = {duckingInfo};
158     ALOGI("Duck change test start");
159     ASSERT_TRUE(audioControl->onDevicesToDuckChange(duckingInfos).isOk());
160 }
161 
162 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
163 INSTANTIATE_TEST_SUITE_P(
164         Audiocontrol, AudioControlAidl,
165         testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
166         android::PrintInstanceNameToString);
167 
main(int argc,char ** argv)168 int main(int argc, char** argv) {
169     ::testing::InitGoogleTest(&argc, argv);
170     ProcessState::self()->setThreadPoolMaxThreadCount(1);
171     ProcessState::self()->startThreadPool();
172     return RUN_ALL_TESTS();
173 }
174