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