Sham Rathod | d4f15e3 | 2022-11-18 14:25:52 +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 | #define LOG_TAG "VtsHalHapticGeneratorTargetTest" |
| 18 | |
| 19 | #include <Utils.h> |
| 20 | #include <aidl/Vintf.h> |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame^] | 21 | #include <android/binder_enums.h> |
| 22 | #include <unordered_set> |
| 23 | |
Sham Rathod | d4f15e3 | 2022-11-18 14:25:52 +0530 | [diff] [blame] | 24 | #include "EffectHelper.h" |
| 25 | |
| 26 | using namespace android; |
| 27 | |
| 28 | using aidl::android::hardware::audio::effect::Capability; |
| 29 | using aidl::android::hardware::audio::effect::Descriptor; |
| 30 | using aidl::android::hardware::audio::effect::HapticGenerator; |
| 31 | using aidl::android::hardware::audio::effect::IEffect; |
| 32 | using aidl::android::hardware::audio::effect::IFactory; |
| 33 | using aidl::android::hardware::audio::effect::kHapticGeneratorTypeUUID; |
| 34 | using aidl::android::hardware::audio::effect::Parameter; |
| 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_HAPTIC_SCALE_ID, |
| 43 | PARAM_HAPTIC_SCALE_VIBRATOR_SCALE, |
| 44 | PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY, |
| 45 | PARAM_VIBRATION_INFORMATION_Q_FACTOR, |
| 46 | PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE, |
| 47 | }; |
| 48 | using HapticGeneratorParamTestParam = |
| 49 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, |
| 50 | HapticGenerator::VibratorScale, float, float, float>; |
| 51 | |
| 52 | /* |
| 53 | * Testing parameter range, assuming the parameter supported by effect is in this range. |
| 54 | * Parameter should be within the valid range defined in the documentation, |
| 55 | * for any supported value test expects EX_NONE from IEffect.setParameter(), |
| 56 | * otherwise expect EX_ILLEGAL_ARGUMENT. |
| 57 | */ |
| 58 | |
| 59 | // TODO : Update the test values once range/capability is updated by implementation |
| 60 | const int MIN_ID = std::numeric_limits<int>::min(); |
| 61 | const int MAX_ID = std::numeric_limits<int>::max(); |
| 62 | const float MIN_FLOAT = std::numeric_limits<float>::min(); |
| 63 | const float MAX_FLOAT = std::numeric_limits<float>::max(); |
| 64 | |
| 65 | const std::vector<int> kHapticScaleIdValues = {MIN_ID, 0, MAX_ID}; |
| 66 | const std::vector<HapticGenerator::VibratorScale> kVibratorScaleValues = { |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame^] | 67 | ndk::enum_range<HapticGenerator::VibratorScale>().begin(), |
| 68 | ndk::enum_range<HapticGenerator::VibratorScale>().end()}; |
Sham Rathod | d4f15e3 | 2022-11-18 14:25:52 +0530 | [diff] [blame] | 69 | |
| 70 | const std::vector<float> kResonantFrequencyValues = {MIN_FLOAT, 100, MAX_FLOAT}; |
| 71 | const std::vector<float> kQFactorValues = {MIN_FLOAT, 100, MAX_FLOAT}; |
| 72 | const std::vector<float> kMaxAmplitude = {MIN_FLOAT, 100, MAX_FLOAT}; |
| 73 | |
| 74 | class HapticGeneratorParamTest : public ::testing::TestWithParam<HapticGeneratorParamTestParam>, |
| 75 | public EffectHelper { |
| 76 | public: |
| 77 | HapticGeneratorParamTest() |
| 78 | : mParamHapticScaleId(std::get<PARAM_HAPTIC_SCALE_ID>(GetParam())), |
| 79 | mParamVibratorScale(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(GetParam())), |
| 80 | mParamResonantFrequency( |
| 81 | std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(GetParam())), |
| 82 | mParamQFactor(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(GetParam())), |
| 83 | mParamMaxAmplitude(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(GetParam())) { |
| 84 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 85 | } |
| 86 | void SetUp() override { |
| 87 | ASSERT_NE(nullptr, mFactory); |
| 88 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 89 | |
| 90 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 91 | Parameter::Common common = EffectHelper::createParamCommon( |
| 92 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 93 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 94 | IEffect::OpenEffectReturn ret; |
| 95 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 96 | ASSERT_NE(nullptr, mEffect); |
| 97 | } |
| 98 | |
| 99 | void TearDown() override { |
| 100 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 101 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 102 | } |
| 103 | |
| 104 | Parameter::Specific getDefaultParamSpecific() { |
| 105 | HapticGenerator::HapticScale hapticScale = {.id = 0, |
| 106 | .scale = HapticGenerator::VibratorScale::MUTE}; |
| 107 | HapticGenerator hg = HapticGenerator::make<HapticGenerator::hapticScale>(hapticScale); |
| 108 | Parameter::Specific specific = |
| 109 | Parameter::Specific::make<Parameter::Specific::hapticGenerator>(hg); |
| 110 | return specific; |
| 111 | } |
| 112 | |
| 113 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 114 | std::shared_ptr<IFactory> mFactory; |
| 115 | std::shared_ptr<IEffect> mEffect; |
| 116 | Descriptor mDescriptor; |
| 117 | int mParamHapticScaleId = 0; |
| 118 | HapticGenerator::VibratorScale mParamVibratorScale = HapticGenerator::VibratorScale::MUTE; |
| 119 | float mParamResonantFrequency = 0; |
| 120 | float mParamQFactor = 0; |
| 121 | float mParamMaxAmplitude = 0; |
| 122 | |
| 123 | void SetAndGetHapticGeneratorParameters() { |
| 124 | for (auto& it : mTags) { |
| 125 | auto& tag = it.first; |
| 126 | auto& hg = it.second; |
| 127 | |
| 128 | // set parameter |
| 129 | Parameter expectParam; |
| 130 | Parameter::Specific specific; |
| 131 | specific.set<Parameter::Specific::hapticGenerator>(hg); |
| 132 | expectParam.set<Parameter::specific>(specific); |
| 133 | EXPECT_STATUS(EX_NONE, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 134 | |
| 135 | // get parameter |
| 136 | Parameter getParam; |
| 137 | Parameter::Id id; |
| 138 | HapticGenerator::Id hgId; |
| 139 | hgId.set<HapticGenerator::Id::commonTag>(tag); |
| 140 | id.set<Parameter::Id::hapticGeneratorTag>(hgId); |
| 141 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 142 | EXPECT_EQ(expectParam, getParam); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void addHapticScaleParam(int id, HapticGenerator::VibratorScale scale) { |
| 147 | HapticGenerator hg; |
| 148 | HapticGenerator::HapticScale hapticScale = {.id = id, .scale = scale}; |
| 149 | hg.set<HapticGenerator::hapticScale>(hapticScale); |
| 150 | mTags.push_back({HapticGenerator::hapticScale, hg}); |
| 151 | } |
| 152 | |
| 153 | void addVibratorInformationParam(float resonantFrequencyHz, float qFactor, float maxAmplitude) { |
| 154 | HapticGenerator hg; |
| 155 | HapticGenerator::VibratorInformation vibrationInfo = { |
| 156 | .resonantFrequencyHz = resonantFrequencyHz, |
| 157 | .qFactor = qFactor, |
| 158 | .maxAmplitude = maxAmplitude}; |
| 159 | hg.set<HapticGenerator::vibratorInfo>(vibrationInfo); |
| 160 | mTags.push_back({HapticGenerator::vibratorInfo, hg}); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | std::vector<std::pair<HapticGenerator::Tag, HapticGenerator>> mTags; |
| 165 | |
| 166 | void CleanUp() { mTags.clear(); } |
| 167 | }; |
| 168 | |
| 169 | TEST_P(HapticGeneratorParamTest, SetAndGetHapticScale) { |
| 170 | EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale)); |
| 171 | SetAndGetHapticGeneratorParameters(); |
| 172 | } |
| 173 | |
| 174 | TEST_P(HapticGeneratorParamTest, SetAndGetVibratorInformation) { |
| 175 | EXPECT_NO_FATAL_FAILURE(addVibratorInformationParam(mParamResonantFrequency, mParamQFactor, |
| 176 | mParamMaxAmplitude)); |
| 177 | SetAndGetHapticGeneratorParameters(); |
| 178 | } |
| 179 | |
| 180 | INSTANTIATE_TEST_SUITE_P( |
| 181 | HapticGeneratorValidTest, HapticGeneratorParamTest, |
| 182 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 183 | IFactory::descriptor, kHapticGeneratorTypeUUID)), |
| 184 | testing::ValuesIn(kHapticScaleIdValues), |
| 185 | testing::ValuesIn(kVibratorScaleValues), |
| 186 | testing::ValuesIn(kResonantFrequencyValues), |
| 187 | testing::ValuesIn(kQFactorValues), testing::ValuesIn(kMaxAmplitude)), |
| 188 | [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) { |
| 189 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 190 | std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param)); |
| 191 | std::string hapticScaleVibScale = std::to_string( |
| 192 | static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param))); |
| 193 | std::string resonantFrequency = std::to_string( |
| 194 | std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param)); |
| 195 | std::string qFactor = |
| 196 | std::to_string(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(info.param)); |
| 197 | std::string maxAmplitude = |
| 198 | std::to_string(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(info.param)); |
| 199 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 200 | descriptor.common.name + "_UUID_" + |
| 201 | descriptor.common.id.uuid.toString() + "_hapticScaleId" + |
| 202 | hapticScaleID + "_hapticScaleVibScale" + hapticScaleVibScale + |
| 203 | "_resonantFrequency" + resonantFrequency + "_qFactor" + qFactor + |
| 204 | "_maxAmplitude" + maxAmplitude; |
| 205 | std::replace_if( |
| 206 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 207 | return name; |
| 208 | }); |
| 209 | |
| 210 | INSTANTIATE_TEST_SUITE_P( |
| 211 | HapticGeneratorInvalidTest, HapticGeneratorParamTest, |
| 212 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 213 | IFactory::descriptor, kHapticGeneratorTypeUUID)), |
| 214 | testing::Values(MIN_ID - 1), |
| 215 | testing::Values(HapticGenerator::VibratorScale::MUTE), |
| 216 | testing::Values(MIN_FLOAT), testing::Values(MIN_FLOAT), |
| 217 | testing::Values(MIN_FLOAT)), |
| 218 | [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) { |
| 219 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 220 | std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param)); |
| 221 | std::string hapticScaleVibScale = std::to_string( |
| 222 | static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param))); |
| 223 | std::string resonantFrequency = std::to_string( |
| 224 | std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param)); |
| 225 | std::string qFactor = |
| 226 | std::to_string(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(info.param)); |
| 227 | std::string maxAmplitude = |
| 228 | std::to_string(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(info.param)); |
| 229 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 230 | descriptor.common.name + "_UUID_" + |
| 231 | descriptor.common.id.uuid.toString() + "_hapticScaleId" + |
| 232 | hapticScaleID + "_hapticScaleVibScale" + hapticScaleVibScale + |
| 233 | "_resonantFrequency" + resonantFrequency + "_qFactor" + qFactor + |
| 234 | "_maxAmplitude" + maxAmplitude; |
| 235 | std::replace_if( |
| 236 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 237 | return name; |
| 238 | }); |
| 239 | |
| 240 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HapticGeneratorParamTest); |
| 241 | |
| 242 | int main(int argc, char** argv) { |
| 243 | ::testing::InitGoogleTest(&argc, argv); |
| 244 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 245 | ABinderProcess_startThreadPool(); |
| 246 | return RUN_ALL_TESTS(); |
| 247 | } |