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