1 /*
2  * Copyright (C) 2022 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 
17 #include <unordered_set>
18 
19 #define LOG_TAG "VtsHalVisualizerTest"
20 #include <android-base/logging.h>
21 #include <android/binder_enums.h>
22 
23 #include "EffectHelper.h"
24 
25 using namespace android;
26 
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer;
29 using aidl::android::hardware::audio::effect::IEffect;
30 using aidl::android::hardware::audio::effect::IFactory;
31 using aidl::android::hardware::audio::effect::Parameter;
32 using aidl::android::hardware::audio::effect::Visualizer;
33 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
34 
35 /**
36  * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
37  * VtsAudioEffectTargetTest.
38  */
39 enum ParamName {
40     PARAM_INSTANCE_NAME,
41     PARAM_CAPTURE_SIZE,
42     PARAM_SCALING_MODE,
43     PARAM_MEASUREMENT_MODE,
44     PARAM_LATENCY,
45 };
46 using VisualizerParamTestParam =
47         std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, Visualizer::ScalingMode,
48                    Visualizer::MeasurementMode, int>;
49 
50 class VisualizerParamTest : public ::testing::TestWithParam<VisualizerParamTestParam>,
51                             public EffectHelper {
52   public:
VisualizerParamTest()53     VisualizerParamTest()
54         : mCaptureSize(std::get<PARAM_CAPTURE_SIZE>(GetParam())),
55           mScalingMode(std::get<PARAM_SCALING_MODE>(GetParam())),
56           mMeasurementMode(std::get<PARAM_MEASUREMENT_MODE>(GetParam())),
57           mLatency(std::get<PARAM_LATENCY>(GetParam())) {
58         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
59     }
60 
SetUp()61     void SetUp() override {
62         ASSERT_NE(nullptr, mFactory);
63         ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
64 
65         Parameter::Common common = createParamCommon(
66                 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
67                 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
68         IEffect::OpenEffectReturn ret;
69         ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt, &ret, EX_NONE));
70         ASSERT_NE(nullptr, mEffect);
71     }
72 
TearDown()73     void TearDown() override {
74         ASSERT_NO_FATAL_FAILURE(close(mEffect));
75         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
76     }
77 
78     static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
79     std::shared_ptr<IFactory> mFactory;
80     std::shared_ptr<IEffect> mEffect;
81     Descriptor mDescriptor;
82     int mCaptureSize;
83     Visualizer::ScalingMode mScalingMode = Visualizer::ScalingMode::NORMALIZED;
84     Visualizer::MeasurementMode mMeasurementMode = Visualizer::MeasurementMode::NONE;
85     int mLatency = 0;
86 
SetAndGetParameters()87     void SetAndGetParameters() {
88         for (auto& it : mCommonTags) {
89             auto& tag = it.first;
90             auto& vs = it.second;
91 
92             // validate parameter
93             Descriptor desc;
94             ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
95             const bool valid = isParameterValid<Visualizer, Range::visualizer>(vs, desc);
96             const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
97 
98             // set parameter
99             Parameter expectParam;
100             Parameter::Specific specific;
101             specific.set<Parameter::Specific::visualizer>(vs);
102             expectParam.set<Parameter::specific>(specific);
103             EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
104 
105             // only get if parameter in range and set success
106             if (expected == EX_NONE) {
107                 Parameter getParam;
108                 Parameter::Id id;
109                 Visualizer::Id vsId;
110                 vsId.set<Visualizer::Id::commonTag>(tag);
111                 id.set<Parameter::Id::visualizerTag>(vsId);
112                 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam))
113                         << " with: " << id.toString();
114                 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
115                                                  << "\ngetParam:" << getParam.toString();
116             }
117         }
118     }
119 
addCaptureSizeParam(int captureSize)120     void addCaptureSizeParam(int captureSize) {
121         mCommonTags.push_back({Visualizer::captureSamples,
122                                Visualizer::make<Visualizer::captureSamples>(captureSize)});
123     }
124 
addScalingModeParam(Visualizer::ScalingMode scalingMode)125     void addScalingModeParam(Visualizer::ScalingMode scalingMode) {
126         mCommonTags.push_back(
127                 {Visualizer::scalingMode, Visualizer::make<Visualizer::scalingMode>(scalingMode)});
128     }
129 
addMeasurementModeParam(Visualizer::MeasurementMode measurementMode)130     void addMeasurementModeParam(Visualizer::MeasurementMode measurementMode) {
131         mCommonTags.push_back({Visualizer::measurementMode,
132                                Visualizer::make<Visualizer::measurementMode>(measurementMode)});
133     }
134 
addLatencyParam(int latency)135     void addLatencyParam(int latency) {
136         mCommonTags.push_back(
137                 {Visualizer::latencyMs, Visualizer::make<Visualizer::latencyMs>(latency)});
138     }
139 
getMeasurementModeValues()140     static std::unordered_set<Visualizer::MeasurementMode> getMeasurementModeValues() {
141         return {ndk::enum_range<Visualizer::MeasurementMode>().begin(),
142                 ndk::enum_range<Visualizer::MeasurementMode>().end()};
143     }
144 
getScalingModeValues()145     static std::unordered_set<Visualizer::ScalingMode> getScalingModeValues() {
146         return {ndk::enum_range<Visualizer::ScalingMode>().begin(),
147                 ndk::enum_range<Visualizer::ScalingMode>().end()};
148     }
149 
150   private:
151     std::vector<std::pair<Visualizer::Tag, Visualizer>> mCommonTags;
CleanUp()152     void CleanUp() { mCommonTags.clear(); }
153 };
154 
TEST_P(VisualizerParamTest,SetAndGetCaptureSize)155 TEST_P(VisualizerParamTest, SetAndGetCaptureSize) {
156     EXPECT_NO_FATAL_FAILURE(addCaptureSizeParam(mCaptureSize));
157     SetAndGetParameters();
158 }
159 
TEST_P(VisualizerParamTest,SetAndGetScalingMode)160 TEST_P(VisualizerParamTest, SetAndGetScalingMode) {
161     EXPECT_NO_FATAL_FAILURE(addScalingModeParam(mScalingMode));
162     SetAndGetParameters();
163 }
164 
TEST_P(VisualizerParamTest,SetAndGetMeasurementMode)165 TEST_P(VisualizerParamTest, SetAndGetMeasurementMode) {
166     EXPECT_NO_FATAL_FAILURE(addMeasurementModeParam(mMeasurementMode));
167     SetAndGetParameters();
168 }
169 
TEST_P(VisualizerParamTest,SetAndGetLatency)170 TEST_P(VisualizerParamTest, SetAndGetLatency) {
171     EXPECT_NO_FATAL_FAILURE(addLatencyParam(mLatency));
172     SetAndGetParameters();
173 }
174 
175 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
176 INSTANTIATE_TEST_SUITE_P(
177         VisualizerParamTest, VisualizerParamTest,
178         ::testing::Combine(
179                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
180                                           IFactory::descriptor, getEffectTypeUuidVisualizer())),
181                 testing::ValuesIn(EffectHelper::getTestValueSet<Visualizer, int, Range::visualizer,
182                                                                 Visualizer::captureSamples>(
183                         kDescPair, EffectHelper::expandTestValueBasic<int>)),
184                 testing::ValuesIn(VisualizerParamTest::getScalingModeValues()),
185                 testing::ValuesIn(VisualizerParamTest::getMeasurementModeValues()),
186                 testing::ValuesIn(EffectHelper::getTestValueSet<Visualizer, int, Range::visualizer,
187                                                                 Visualizer::latencyMs>(
188                         kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anon64971efd0102(const testing::TestParamInfo<VisualizerParamTest::ParamType>& info) 189         [](const testing::TestParamInfo<VisualizerParamTest::ParamType>& info) {
190             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
191             std::string captureSize = std::to_string(std::get<PARAM_CAPTURE_SIZE>(info.param));
192             std::string scalingMode = aidl::android::hardware::audio::effect::toString(
193                     std::get<PARAM_SCALING_MODE>(info.param));
194             std::string measurementMode = aidl::android::hardware::audio::effect::toString(
195                     std::get<PARAM_MEASUREMENT_MODE>(info.param));
196             std::string latency = std::to_string(std::get<PARAM_LATENCY>(info.param));
197 
198             std::string name = getPrefix(descriptor) + "_captureSize" + captureSize +
199                                "_scalingMode" + scalingMode + "_measurementMode" + measurementMode +
200                                "_latency" + latency;
201             std::replace_if(
202                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
203             return name;
204         });
205 
206 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VisualizerParamTest);
207 
main(int argc,char ** argv)208 int main(int argc, char** argv) {
209     ::testing::InitGoogleTest(&argc, argv);
210     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
211     ABinderProcess_setThreadPoolMaxThreadCount(1);
212     ABinderProcess_startThreadPool();
213     return RUN_ALL_TESTS();
214 }
215