1 /*
2  * Copyright (C) 2018 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 <vector>
18 
19 #define LOG_TAG "AudioEffectHidlHalTest"
20 #include <android-base/logging.h>
21 #if MAJOR_VERSION <= 6
22 #include <system/audio.h>
23 #endif
24 
25 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffect.h)
26 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffectsFactory.h)
27 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEqualizerEffect.h)
28 #include PATH(android/hardware/audio/effect/FILE_VERSION/ILoudnessEnhancerEffect.h)
29 #include PATH(android/hardware/audio/effect/FILE_VERSION/types.h)
30 #include <android/hidl/allocator/1.0/IAllocator.h>
31 #include <android/hidl/memory/1.0/IMemory.h>
32 #if MAJOR_VERSION >= 7
33 #include <android_audio_policy_configuration_V7_0-enums.h>
34 #endif
35 
36 #include <common/all-versions/VersionUtils.h>
37 
38 #include <cutils/properties.h>
39 #include <gtest/gtest.h>
40 #include <hidl/GtestPrinter.h>
41 #include <hidl/ServiceManagement.h>
42 
43 using ::android::sp;
44 using ::android::hardware::hidl_handle;
45 using ::android::hardware::hidl_memory;
46 using ::android::hardware::hidl_string;
47 using ::android::hardware::hidl_vec;
48 using ::android::hardware::MQDescriptorSync;
49 using ::android::hardware::Return;
50 using ::android::hardware::Void;
51 using ::android::hardware::audio::common::utils::mkEnumBitfield;
52 using ::android::hidl::allocator::V1_0::IAllocator;
53 using ::android::hidl::memory::V1_0::IMemory;
54 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
55 using namespace ::android::hardware::audio::effect::CPP_VERSION;
56 #if MAJOR_VERSION >= 7
57 // Make an alias for enumerations generated from the APM config XSD.
58 namespace xsd {
59 using namespace ::android::audio::policy::configuration::CPP_VERSION;
60 }
61 #endif
62 
63 #ifndef ARRAY_SIZE
64 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
65 #endif
66 
67 class AudioEffectsFactoryHidlTest : public ::testing::TestWithParam<std::string> {
68   public:
SetUp()69     void SetUp() override {
70         effectsFactory = IEffectsFactory::getService(GetParam());
71         ASSERT_NE(effectsFactory, nullptr);
72     }
TearDown()73     void TearDown() override { effectsFactory.clear(); }
74 
75    protected:
description(const std::string & description)76     static void description(const std::string& description) {
77         RecordProperty("description", description);
78     }
79 
80     sp<IEffectsFactory> effectsFactory;
81 };
82 
TEST_P(AudioEffectsFactoryHidlTest,EnumerateEffects)83 TEST_P(AudioEffectsFactoryHidlTest, EnumerateEffects) {
84     description("Verify that EnumerateEffects returns at least one effect");
85     Result retval = Result::NOT_INITIALIZED;
86     size_t effectCount = 0;
87     Return<void> ret =
88         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
89             retval = r;
90             effectCount = result.size();
91         });
92     EXPECT_TRUE(ret.isOk());
93     EXPECT_EQ(Result::OK, retval);
94     EXPECT_GT(effectCount, 0u);
95 }
96 
TEST_P(AudioEffectsFactoryHidlTest,CreateEffect)97 TEST_P(AudioEffectsFactoryHidlTest, CreateEffect) {
98     description("Verify that an effect can be created via CreateEffect");
99     bool gotEffect = false;
100     Uuid effectUuid;
101     Return<void> ret =
102         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
103             if (r == Result::OK && result.size() > 0) {
104                 gotEffect = true;
105                 effectUuid = result[0].uuid;
106             }
107         });
108     ASSERT_TRUE(ret.isOk());
109     ASSERT_TRUE(gotEffect);
110     Result retval = Result::NOT_INITIALIZED;
111     sp<IEffect> effect;
112     ret = effectsFactory->createEffect(
113             effectUuid, 1 /*session*/, 1 /*ioHandle*/,
114 #if MAJOR_VERSION >= 6
115             0 /*device*/,
116 #endif
117             [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) {
118                 retval = r;
119                 if (r == Result::OK) {
120                     effect = result;
121                 }
122             });
123     EXPECT_TRUE(ret.isOk());
124     EXPECT_EQ(Result::OK, retval);
125     EXPECT_NE(nullptr, effect.get());
126 }
127 
TEST_P(AudioEffectsFactoryHidlTest,GetDescriptor)128 TEST_P(AudioEffectsFactoryHidlTest, GetDescriptor) {
129     description(
130         "Verify that effects factory can provide an effect descriptor via "
131         "GetDescriptor");
132     hidl_vec<EffectDescriptor> allDescriptors;
133     Return<void> ret =
134         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
135             if (r == Result::OK) {
136                 allDescriptors = result;
137             }
138         });
139     ASSERT_TRUE(ret.isOk());
140     ASSERT_GT(allDescriptors.size(), 0u);
141     for (size_t i = 0; i < allDescriptors.size(); ++i) {
142         ret = effectsFactory->getDescriptor(allDescriptors[i].uuid,
143                                             [&](Result r, const EffectDescriptor& result) {
144                                                 EXPECT_EQ(r, Result::OK);
145                                                 EXPECT_EQ(result, allDescriptors[i]);
146                                             });
147     }
148     EXPECT_TRUE(ret.isOk());
149 }
150 
TEST_P(AudioEffectsFactoryHidlTest,DebugDumpInvalidArgument)151 TEST_P(AudioEffectsFactoryHidlTest, DebugDumpInvalidArgument) {
152     description("Verify that debugDump doesn't crash on invalid arguments");
153 #if MAJOR_VERSION == 2
154     Return<void> ret = effectsFactory->debugDump(hidl_handle());
155 #elif MAJOR_VERSION >= 4
156     Return<void> ret = effectsFactory->debug(hidl_handle(), {});
157 #endif
158     ASSERT_TRUE(ret.isOk());
159 }
160 
161 // Equalizer effect is required by CDD, but only the type is fixed.
162 // This is the same UUID as AudioEffect.EFFECT_TYPE_EQUALIZER in Java.
163 static const Uuid EQUALIZER_EFFECT_TYPE = {
164     0x0bed4300, 0xddd6, 0x11db, 0x8f34,
165     std::array<uint8_t, 6>{{0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}};
166 // Loudness Enhancer effect is required by CDD, but only the type is fixed.
167 // This is the same UUID as AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER in Java.
168 static const Uuid LOUDNESS_ENHANCER_EFFECT_TYPE = {
169     0xfe3199be, 0xaed0, 0x413f, 0x87bb,
170     std::array<uint8_t, 6>{{0x11, 0x26, 0x0e, 0xb6, 0x3c, 0xf1}}};
171 
172 enum { PARAM_FACTORY_NAME, PARAM_EFFECT_UUID, PARAM_USE_AFTER_CLOSE };
173 using EffectParameter = std::tuple<std::string, Uuid, bool>;
174 
EffectParameterToString(const::testing::TestParamInfo<EffectParameter> & info)175 static inline std::string EffectParameterToString(
176         const ::testing::TestParamInfo<EffectParameter>& info) {
177     std::string prefix = std::get<PARAM_USE_AFTER_CLOSE>(info.param) ? "UseAfterClose_" : "";
178     return prefix.append(
179             ::android::hardware::PrintInstanceNameToString(::testing::TestParamInfo<std::string>{
180                     std::get<PARAM_FACTORY_NAME>(info.param), info.index}));
181 }
182 
183 // The main test class for Audio Effect HIDL HAL.
184 class AudioEffectHidlTest : public ::testing::TestWithParam<EffectParameter> {
185   public:
SetUp()186     void SetUp() override {
187         effectsFactory = IEffectsFactory::getService(std::get<PARAM_FACTORY_NAME>(GetParam()));
188         ASSERT_NE(nullptr, effectsFactory.get());
189 
190         ASSERT_NO_FATAL_FAILURE(findAndCreateEffect(getEffectType()));
191         ASSERT_NE(nullptr, effect.get());
192 
193         Return<Result> ret = effect->init();
194         ASSERT_TRUE(ret.isOk());
195         ASSERT_EQ(Result::OK, ret);
196 
197         useAfterClose = std::get<PARAM_USE_AFTER_CLOSE>(GetParam());
198         if (useAfterClose) {
199             Return<Result> ret = effect->close();
200             ASSERT_TRUE(ret.isOk());
201             ASSERT_EQ(Result::OK, ret);
202         }
203     }
204 
TearDown()205     void TearDown() override {
206         effect.clear();
207         effectsFactory.clear();
208     }
209 
210    protected:
description(const std::string & description)211     static void description(const std::string& description) {
212         RecordProperty("description", description);
213     }
214 
getEffectType() const215     Uuid getEffectType() const { return std::get<PARAM_EFFECT_UUID>(GetParam()); }
216 
217     void checkResult(const Result& result);
218     void checkResultForUseAfterClose(const Result& result);
219     void findAndCreateEffect(const Uuid& type);
220     void findEffectInstance(const Uuid& type, Uuid* uuid);
221     void getChannelCount(uint32_t* channelCount);
222 
223     sp<IEffectsFactory> effectsFactory;
224     sp<IEffect> effect;
225     bool useAfterClose;
226 };
227 
checkResult(const Result & result)228 void AudioEffectHidlTest::checkResult(const Result& result) {
229     if (!useAfterClose) {
230         ASSERT_EQ(Result::OK, result);
231     } else {
232         ASSERT_NO_FATAL_FAILURE(checkResultForUseAfterClose(result));
233     }
234 }
235 
checkResultForUseAfterClose(const Result & result)236 void AudioEffectHidlTest::checkResultForUseAfterClose(const Result& result) {
237     if (useAfterClose) {
238         // The actual error does not matter. It's important that the effect did not crash
239         // while executing any command after a call to "close", and that the returned status
240         // is not OK.
241         ASSERT_NE(Result::OK, result);
242     }
243 }
244 
findAndCreateEffect(const Uuid & type)245 void AudioEffectHidlTest::findAndCreateEffect(const Uuid& type) {
246     Uuid effectUuid;
247     ASSERT_NO_FATAL_FAILURE(findEffectInstance(type, &effectUuid));
248     Return<void> ret = effectsFactory->createEffect(
249             effectUuid, 1 /*session*/, 1 /*ioHandle*/,
250 #if MAJOR_VERSION >= 6
251             0 /*device*/,
252 #endif
253             [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) {
254                 if (r == Result::OK) {
255                     effect = result;
256                 }
257             });
258     ASSERT_TRUE(ret.isOk());
259 }
260 
findEffectInstance(const Uuid & type,Uuid * uuid)261 void AudioEffectHidlTest::findEffectInstance(const Uuid& type, Uuid* uuid) {
262     bool effectFound = false;
263     Return<void> ret =
264         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
265             if (r == Result::OK) {
266                 for (const auto& desc : result) {
267                     if (desc.type == type) {
268                         effectFound = true;
269                         *uuid = desc.uuid;
270                         break;
271                     }
272                 }
273             }
274         });
275     ASSERT_TRUE(ret.isOk());
276     ASSERT_TRUE(effectFound);
277 }
278 
getChannelCount(uint32_t * channelCount)279 void AudioEffectHidlTest::getChannelCount(uint32_t* channelCount) {
280     Result retval;
281     EffectConfig currentConfig;
282     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
283         retval = r;
284         if (r == Result::OK) {
285             currentConfig = conf;
286         }
287     });
288     ASSERT_TRUE(ret.isOk());
289     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
290     if (useAfterClose) {
291         *channelCount = 1;
292         return;
293     }
294 #if MAJOR_VERSION <= 6
295     ASSERT_TRUE(audio_channel_mask_is_valid(
296         static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels)));
297     *channelCount = audio_channel_count_from_out_mask(
298         static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels));
299 #else
300     ASSERT_EQ(AudioConfigBaseOptional::ChannelMask::hidl_discriminator::value,
301               currentConfig.outputCfg.base.channelMask.getDiscriminator());
302     *channelCount = android::audio::policy::configuration::V7_0::getChannelCount(
303             currentConfig.outputCfg.base.channelMask.value());
304     ASSERT_NE(*channelCount, 0);
305 #endif
306 }
307 
TEST_P(AudioEffectHidlTest,Close)308 TEST_P(AudioEffectHidlTest, Close) {
309     description("Verify that an effect can be closed");
310     Return<Result> ret = effect->close();
311     EXPECT_TRUE(ret.isOk());
312     ASSERT_NO_FATAL_FAILURE(checkResult(ret));
313 }
314 
TEST_P(AudioEffectHidlTest,GetDescriptor)315 TEST_P(AudioEffectHidlTest, GetDescriptor) {
316     description("Verify that an effect can return its own descriptor via GetDescriptor");
317     Result retval = Result::NOT_INITIALIZED;
318     Uuid actualType;
319     Return<void> ret = effect->getDescriptor([&](Result r, const EffectDescriptor& desc) {
320         retval = r;
321         if (r == Result::OK) {
322             actualType = desc.type;
323         }
324     });
325     EXPECT_TRUE(ret.isOk());
326     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
327     if (useAfterClose) return;
328     EXPECT_EQ(getEffectType(), actualType);
329 }
330 
TEST_P(AudioEffectHidlTest,GetSetConfig)331 TEST_P(AudioEffectHidlTest, GetSetConfig) {
332     description(
333         "Verify that it is possible to manipulate effect config via Get / "
334         "SetConfig");
335     Result retval = Result::NOT_INITIALIZED;
336     EffectConfig currentConfig;
337     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
338         retval = r;
339         if (r == Result::OK) {
340             currentConfig = conf;
341         }
342     });
343     EXPECT_TRUE(ret.isOk());
344     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
345     if (useAfterClose) return;
346     Return<Result> ret2 = effect->setConfig(currentConfig, nullptr, nullptr);
347     EXPECT_TRUE(ret2.isOk());
348     EXPECT_EQ(Result::OK, ret2);
349 }
350 
351 #if MAJOR_VERSION >= 7
generateInvalidConfigs(const EffectBufferConfig & src)352 std::vector<EffectBufferConfig> generateInvalidConfigs(const EffectBufferConfig& src) {
353     std::vector<EffectBufferConfig> result;
354     EffectBufferConfig invalidFormat = src;
355     invalidFormat.base.format.value("random_string");
356     result.push_back(std::move(invalidFormat));
357     EffectBufferConfig invalidChannelMask = src;
358     invalidChannelMask.base.channelMask.value("random_string");
359     result.push_back(std::move(invalidChannelMask));
360     return result;
361 }
362 
TEST_P(AudioEffectHidlTest,SetConfigInvalidArguments)363 TEST_P(AudioEffectHidlTest, SetConfigInvalidArguments) {
364     description("Verify that invalid arguments are rejected by SetConfig");
365     Result retval = Result::NOT_INITIALIZED;
366     EffectConfig currentConfig;
367     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
368         retval = r;
369         if (r == Result::OK) {
370             currentConfig = conf;
371         }
372     });
373     EXPECT_TRUE(ret.isOk());
374     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
375     if (useAfterClose) return;
376     for (const auto& invalidInputCfg : generateInvalidConfigs(currentConfig.inputCfg)) {
377         EffectConfig invalidConfig = currentConfig;
378         invalidConfig.inputCfg = invalidInputCfg;
379         Return<Result> ret = effect->setConfig(invalidConfig, nullptr, nullptr);
380         EXPECT_TRUE(ret.isOk());
381         EXPECT_EQ(Result::INVALID_ARGUMENTS, ret);
382     }
383     for (const auto& invalidOutputCfg : generateInvalidConfigs(currentConfig.outputCfg)) {
384         EffectConfig invalidConfig = currentConfig;
385         invalidConfig.outputCfg = invalidOutputCfg;
386         Return<Result> ret = effect->setConfig(invalidConfig, nullptr, nullptr);
387         EXPECT_TRUE(ret.isOk());
388         EXPECT_EQ(Result::INVALID_ARGUMENTS, ret);
389     }
390 }
391 #endif
392 
TEST_P(AudioEffectHidlTest,GetConfigReverse)393 TEST_P(AudioEffectHidlTest, GetConfigReverse) {
394     description("Verify that GetConfigReverse does not crash");
395     Result retval = Result::OK;
396     Return<void> ret = effect->getConfigReverse([&](Result r, const EffectConfig&) { retval = r; });
397     EXPECT_TRUE(ret.isOk());
398     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(retval));
399 }
400 
TEST_P(AudioEffectHidlTest,GetSupportedAuxChannelsConfigs)401 TEST_P(AudioEffectHidlTest, GetSupportedAuxChannelsConfigs) {
402     description("Verify that GetSupportedAuxChannelsConfigs does not crash");
403     Result retval = Result::OK;
404     Return<void> ret = effect->getSupportedAuxChannelsConfigs(
405             0, [&](Result r, const hidl_vec<EffectAuxChannelsConfig>&) { retval = r; });
406     EXPECT_TRUE(ret.isOk());
407     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(retval));
408 }
409 
TEST_P(AudioEffectHidlTest,GetAuxChannelsConfig)410 TEST_P(AudioEffectHidlTest, GetAuxChannelsConfig) {
411     description("Verify that GetAuxChannelsConfig does not crash");
412     Result retval = Result::OK;
413     Return<void> ret = effect->getAuxChannelsConfig(
414             [&](Result r, const EffectAuxChannelsConfig&) { retval = r; });
415     EXPECT_TRUE(ret.isOk());
416     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(retval));
417 }
418 
TEST_P(AudioEffectHidlTest,SetAuxChannelsConfig)419 TEST_P(AudioEffectHidlTest, SetAuxChannelsConfig) {
420     description("Verify that SetAuxChannelsConfig does not crash");
421     Return<Result> ret = effect->setAuxChannelsConfig(EffectAuxChannelsConfig());
422     EXPECT_TRUE(ret.isOk());
423     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
424 }
425 
426 // Not generated automatically because AudioBuffer contains
427 // instances of hidl_memory which can't be compared properly
428 // in general case due to presence of handles.
429 //
430 // However, in this particular case, handles must not present
431 // thus comparison is possible.
432 //
433 // operator== must be defined in the same namespace as the structures.
434 namespace android {
435 namespace hardware {
436 namespace audio {
437 namespace effect {
438 namespace CPP_VERSION {
operator ==(const AudioBuffer & lhs,const AudioBuffer & rhs)439 inline bool operator==(const AudioBuffer& lhs, const AudioBuffer& rhs) {
440     return lhs.id == rhs.id && lhs.frameCount == rhs.frameCount && lhs.data.handle() == nullptr &&
441            rhs.data.handle() == nullptr;
442 }
443 
444 #if MAJOR_VERSION <= 6
operator ==(const EffectBufferConfig & lhs,const EffectBufferConfig & rhs)445 inline bool operator==(const EffectBufferConfig& lhs, const EffectBufferConfig& rhs) {
446     return lhs.buffer == rhs.buffer &&
447            lhs.samplingRateHz == rhs.samplingRateHz && lhs.channels == rhs.channels &&
448            lhs.format == rhs.format &&
449            lhs.accessMode == rhs.accessMode && lhs.mask == rhs.mask;
450 }
451 #else
operator ==(const EffectBufferConfig & lhs,const EffectBufferConfig & rhs)452 inline bool operator==(const EffectBufferConfig& lhs, const EffectBufferConfig& rhs) {
453     return lhs.buffer.getDiscriminator() == rhs.buffer.getDiscriminator() &&
454            (lhs.buffer.getDiscriminator() ==
455                     EffectBufferConfig::OptionalBuffer::hidl_discriminator::unspecified ||
456             lhs.buffer.buf() == rhs.buffer.buf()) &&
457            lhs.base == rhs.base && lhs.accessMode == rhs.accessMode;
458 }
459 #endif  // MAJOR_VERSION <= 6
460 
operator ==(const EffectConfig & lhs,const EffectConfig & rhs)461 inline bool operator==(const EffectConfig& lhs, const EffectConfig& rhs) {
462     return lhs.inputCfg == rhs.inputCfg && lhs.outputCfg == rhs.outputCfg;
463 }
464 }  // namespace CPP_VERSION
465 }  // namespace effect
466 }  // namespace audio
467 }  // namespace hardware
468 }  // namespace android
469 
TEST_P(AudioEffectHidlTest,Reset)470 TEST_P(AudioEffectHidlTest, Reset) {
471     description("Verify that Reset preserves effect configuration");
472     Result retval = Result::NOT_INITIALIZED;
473     EffectConfig originalConfig;
474     if (!useAfterClose) {
475         Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
476             retval = r;
477             if (r == Result::OK) {
478                 originalConfig = conf;
479             }
480         });
481         ASSERT_TRUE(ret.isOk());
482         ASSERT_EQ(Result::OK, retval);
483     }
484     Return<Result> ret2 = effect->reset();
485     EXPECT_TRUE(ret2.isOk());
486     EXPECT_NO_FATAL_FAILURE(checkResult(ret2));
487     if (!useAfterClose) {
488         EffectConfig configAfterReset;
489         Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
490             retval = r;
491             if (r == Result::OK) {
492                 configAfterReset = conf;
493             }
494         });
495         EXPECT_EQ(originalConfig, configAfterReset);
496     }
497 }
498 
TEST_P(AudioEffectHidlTest,DisableEnableDisable)499 TEST_P(AudioEffectHidlTest, DisableEnableDisable) {
500     description("Verify Disable -> Enable -> Disable sequence for an effect");
501     Return<Result> ret = effect->disable();
502     EXPECT_TRUE(ret.isOk());
503     if (!useAfterClose) {
504         // Note: some legacy effects may return -EINVAL (INVALID_ARGUMENTS),
505         //       more canonical is to return -ENOSYS (NOT_SUPPORTED)
506         EXPECT_TRUE(ret == Result::NOT_SUPPORTED || ret == Result::INVALID_ARGUMENTS);
507     } else {
508         EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
509     }
510     ret = effect->enable();
511     EXPECT_TRUE(ret.isOk());
512     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
513     ret = effect->disable();
514     EXPECT_TRUE(ret.isOk());
515     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
516 }
517 
518 #if MAJOR_VERSION >= 7
TEST_P(AudioEffectHidlTest,SetDeviceInvalidDeviceAddress)519 TEST_P(AudioEffectHidlTest, SetDeviceInvalidDeviceAddress) {
520     description("Verify that invalid device address is rejected by SetDevice");
521     if (useAfterClose) {
522         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
523     }
524     DeviceAddress device{.deviceType = "random_string"};
525     Return<Result> ret = effect->setDevice(device);
526     EXPECT_TRUE(ret.isOk());
527     EXPECT_EQ(Result::INVALID_ARGUMENTS, ret);
528 }
529 #endif
530 
TEST_P(AudioEffectHidlTest,SetDevice)531 TEST_P(AudioEffectHidlTest, SetDevice) {
532     description("Verify that SetDevice works for an output chain effect");
533 #if MAJOR_VERSION <= 6
534     Return<Result> ret = effect->setDevice(mkEnumBitfield(AudioDevice::OUT_SPEAKER));
535 #else
536     DeviceAddress device{.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER)};
537     Return<Result> ret = effect->setDevice(device);
538 #endif
539     EXPECT_TRUE(ret.isOk());
540     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
541 }
542 
TEST_P(AudioEffectHidlTest,SetAndGetVolume)543 TEST_P(AudioEffectHidlTest, SetAndGetVolume) {
544     description("Verify that SetAndGetVolume method works for an effect");
545     uint32_t channelCount;
546     ASSERT_NO_FATAL_FAILURE(getChannelCount(&channelCount));
547     hidl_vec<uint32_t> volumes;
548     volumes.resize(channelCount);
549     for (uint32_t i = 0; i < channelCount; ++i) {
550         volumes[i] = 0;
551     }
552     Result retval = Result::NOT_INITIALIZED;
553     Return<void> ret =
554         effect->setAndGetVolume(volumes, [&](Result r, const hidl_vec<uint32_t>&) { retval = r; });
555     EXPECT_TRUE(ret.isOk());
556     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
557 }
558 
TEST_P(AudioEffectHidlTest,VolumeChangeNotification)559 TEST_P(AudioEffectHidlTest, VolumeChangeNotification) {
560     description("Verify that effect accepts VolumeChangeNotification");
561     uint32_t channelCount;
562     ASSERT_NO_FATAL_FAILURE(getChannelCount(&channelCount));
563     hidl_vec<uint32_t> volumes;
564     volumes.resize(channelCount);
565     for (uint32_t i = 0; i < channelCount; ++i) {
566         volumes[i] = 0;
567     }
568     Return<Result> ret = effect->volumeChangeNotification(volumes);
569     EXPECT_TRUE(ret.isOk());
570     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
571 }
572 
TEST_P(AudioEffectHidlTest,SetAudioMode)573 TEST_P(AudioEffectHidlTest, SetAudioMode) {
574     description("Verify that SetAudioMode works for an effect");
575     Return<Result> ret = effect->setAudioMode(AudioMode::NORMAL);
576     EXPECT_TRUE(ret.isOk());
577     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
578 }
579 
TEST_P(AudioEffectHidlTest,SetConfigReverse)580 TEST_P(AudioEffectHidlTest, SetConfigReverse) {
581     description("Verify that SetConfigReverse does not crash");
582     Return<Result> ret = effect->setConfigReverse(EffectConfig(), nullptr, nullptr);
583     EXPECT_TRUE(ret.isOk());
584     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
585 }
586 
587 #if MAJOR_VERSION >= 7
TEST_P(AudioEffectHidlTest,SetInputDeviceInvalidDeviceAddress)588 TEST_P(AudioEffectHidlTest, SetInputDeviceInvalidDeviceAddress) {
589     description("Verify that invalid device address is rejected by SetInputDevice");
590     if (useAfterClose) {
591         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
592     }
593     DeviceAddress device{.deviceType = "random_string"};
594     Return<Result> ret = effect->setInputDevice(device);
595     EXPECT_TRUE(ret.isOk());
596     EXPECT_TRUE(ret == Result::INVALID_ARGUMENTS || ret == Result::NOT_SUPPORTED)
597             << ::testing::PrintToString(ret);
598 }
599 #endif
600 
TEST_P(AudioEffectHidlTest,SetInputDevice)601 TEST_P(AudioEffectHidlTest, SetInputDevice) {
602     description("Verify that SetInputDevice does not crash");
603 #if MAJOR_VERSION <= 6
604     Return<Result> ret = effect->setInputDevice(mkEnumBitfield(AudioDevice::IN_BUILTIN_MIC));
605 #else
606     DeviceAddress device{.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_IN_BUILTIN_MIC)};
607     Return<Result> ret = effect->setInputDevice(device);
608 #endif
609     EXPECT_TRUE(ret.isOk());
610     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
611 }
612 
613 #if MAJOR_VERSION >= 7
TEST_P(AudioEffectHidlTest,SetInvalidAudioSource)614 TEST_P(AudioEffectHidlTest, SetInvalidAudioSource) {
615     description("Verify that an invalid audio source is rejected by SetAudioSource");
616     if (useAfterClose) {
617         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
618     }
619     Return<Result> ret = effect->setAudioSource("random_string");
620     ASSERT_TRUE(ret.isOk());
621     EXPECT_TRUE(ret == Result::INVALID_ARGUMENTS || ret == Result::NOT_SUPPORTED)
622             << ::testing::PrintToString(ret);
623 }
624 #endif
625 
TEST_P(AudioEffectHidlTest,SetAudioSource)626 TEST_P(AudioEffectHidlTest, SetAudioSource) {
627     description("Verify that SetAudioSource does not crash");
628 #if MAJOR_VERSION <= 6
629     Return<Result> ret = effect->setAudioSource(AudioSource::MIC);
630 #else
631     Return<Result> ret = effect->setAudioSource(toString(xsd::AudioSource::AUDIO_SOURCE_MIC));
632 #endif
633     EXPECT_TRUE(ret.isOk());
634     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
635 }
636 
TEST_P(AudioEffectHidlTest,Offload)637 TEST_P(AudioEffectHidlTest, Offload) {
638     description("Verify that calling Offload method does not crash");
639     Return<Result> ret = effect->offload(EffectOffloadParameter{});
640     EXPECT_TRUE(ret.isOk());
641     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
642 }
643 
TEST_P(AudioEffectHidlTest,PrepareForProcessing)644 TEST_P(AudioEffectHidlTest, PrepareForProcessing) {
645     description("Verify that PrepareForProcessing method works for an effect");
646     Result retval = Result::NOT_INITIALIZED;
647     Return<void> ret = effect->prepareForProcessing(
648         [&](Result r, const MQDescriptorSync<Result>&) { retval = r; });
649     EXPECT_TRUE(ret.isOk());
650     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
651 }
652 
TEST_P(AudioEffectHidlTest,SetProcessBuffers)653 TEST_P(AudioEffectHidlTest, SetProcessBuffers) {
654     description("Verify that SetProcessBuffers works for an effect");
655     sp<IAllocator> ashmem = IAllocator::getService("ashmem");
656     ASSERT_NE(nullptr, ashmem.get());
657     bool success = false;
658     AudioBuffer buffer;
659     Return<void> ret = ashmem->allocate(1024, [&](bool s, const hidl_memory& memory) {
660         success = s;
661         if (s) {
662             buffer.data = memory;
663         }
664     });
665     ASSERT_TRUE(ret.isOk());
666     ASSERT_TRUE(success);
667     Return<Result> ret2 = effect->setProcessBuffers(buffer, buffer);
668     EXPECT_TRUE(ret2.isOk());
669     EXPECT_NO_FATAL_FAILURE(checkResult(ret2));
670 }
671 
TEST_P(AudioEffectHidlTest,Command)672 TEST_P(AudioEffectHidlTest, Command) {
673     description("Verify that Command does not crash");
674     Return<void> ret =
675         effect->command(0, hidl_vec<uint8_t>(), 0, [&](int32_t, const hidl_vec<uint8_t>&) {});
676     EXPECT_TRUE(ret.isOk());
677 }
678 
TEST_P(AudioEffectHidlTest,SetParameter)679 TEST_P(AudioEffectHidlTest, SetParameter) {
680     description("Verify that SetParameter does not crash");
681     Return<Result> ret = effect->setParameter(hidl_vec<uint8_t>(), hidl_vec<uint8_t>());
682     EXPECT_TRUE(ret.isOk());
683     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(ret));
684 }
685 
TEST_P(AudioEffectHidlTest,GetParameter)686 TEST_P(AudioEffectHidlTest, GetParameter) {
687     description("Verify that GetParameter does not crash");
688     Return<void> ret =
689         effect->getParameter(hidl_vec<uint8_t>(), 0, [&](Result, const hidl_vec<uint8_t>&) {});
690     EXPECT_TRUE(ret.isOk());
691 }
692 
TEST_P(AudioEffectHidlTest,GetParameterInvalidMaxReplySize)693 TEST_P(AudioEffectHidlTest, GetParameterInvalidMaxReplySize) {
694     description("Verify that GetParameter caps the maximum reply size");
695     const bool isNewDeviceLaunchingOnTPlus = property_get_int32("ro.vendor.api_level", 0) >= 33;
696     if (!isNewDeviceLaunchingOnTPlus) {
697         GTEST_SKIP() << "The test only applies to devices launching on T or later";
698     }
699     if (useAfterClose) {
700         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
701     }
702     // Use a non-empty parameter to avoid being rejected by any earlier checks.
703     hidl_vec<uint8_t> parameter;
704     parameter.resize(16);
705     // Use very large size to ensure that the service does not crash. Since parameters
706     // are specific to each effect, and some effects may not have parameters at all,
707     // simply checking the return value would not reveal an issue of using an uncapped value.
708     const uint32_t veryLargeReplySize = std::numeric_limits<uint32_t>::max() - 100;
709     Result retval = Result::OK;
710     Return<void> ret =
711             effect->getParameter(parameter, veryLargeReplySize,
712                                  [&](Result r, const hidl_vec<uint8_t>&) { retval = r; });
713     EXPECT_TRUE(ret.isOk());
714     EXPECT_EQ(Result::INVALID_ARGUMENTS, retval);
715 }
716 
TEST_P(AudioEffectHidlTest,GetSupportedConfigsForFeature)717 TEST_P(AudioEffectHidlTest, GetSupportedConfigsForFeature) {
718     description("Verify that GetSupportedConfigsForFeature does not crash");
719     Result retval = Result::OK;
720     Return<void> ret = effect->getSupportedConfigsForFeature(
721             0, 0, 0, [&](Result r, uint32_t, const hidl_vec<uint8_t>&) { retval = r; });
722     EXPECT_TRUE(ret.isOk());
723     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(retval));
724 }
725 
TEST_P(AudioEffectHidlTest,GetCurrentConfigForFeature)726 TEST_P(AudioEffectHidlTest, GetCurrentConfigForFeature) {
727     description("Verify that GetCurrentConfigForFeature does not crash");
728     Result retval = Result::OK;
729     Return<void> ret = effect->getCurrentConfigForFeature(
730             0, 0, [&](Result r, const hidl_vec<uint8_t>&) { retval = r; });
731     EXPECT_TRUE(ret.isOk());
732     EXPECT_NO_FATAL_FAILURE(checkResultForUseAfterClose(retval));
733 }
734 
TEST_P(AudioEffectHidlTest,SetCurrentConfigForFeature)735 TEST_P(AudioEffectHidlTest, SetCurrentConfigForFeature) {
736     description("Verify that SetCurrentConfigForFeature does not crash");
737     Return<Result> ret = effect->setCurrentConfigForFeature(0, hidl_vec<uint8_t>());
738     EXPECT_TRUE(ret.isOk());
739 }
740 
TEST_P(AudioEffectHidlTest,GetSupportedConfigsForFeatureInvalidConfigSize)741 TEST_P(AudioEffectHidlTest, GetSupportedConfigsForFeatureInvalidConfigSize) {
742     description("Verify that GetSupportedConfigsForFeature caps the maximum config size");
743     const bool isNewDeviceLaunchingOnTPlus = property_get_int32("ro.vendor.api_level", 0) >= 33;
744     if (!isNewDeviceLaunchingOnTPlus) {
745         GTEST_SKIP() << "The test only applies to devices launching on T or later";
746     }
747     if (useAfterClose) {
748         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
749     }
750     // Use very large size to ensure that the service does not crash.
751     const uint32_t veryLargeConfigSize = std::numeric_limits<uint32_t>::max() - 100;
752     Result retval = Result::OK;
753     Return<void> ret = effect->getSupportedConfigsForFeature(
754             0, 1, veryLargeConfigSize,
755             [&](Result r, uint32_t, const hidl_vec<uint8_t>&) { retval = r; });
756     EXPECT_TRUE(ret.isOk());
757     EXPECT_EQ(Result::INVALID_ARGUMENTS, retval);
758 }
759 
TEST_P(AudioEffectHidlTest,GetCurrentConfigForFeatureInvalidConfigSize)760 TEST_P(AudioEffectHidlTest, GetCurrentConfigForFeatureInvalidConfigSize) {
761     description("Verify that GetCurrentConfigForFeature caps the maximum config size");
762     const bool isNewDeviceLaunchingOnTPlus = property_get_int32("ro.vendor.api_level", 0) >= 33;
763     if (!isNewDeviceLaunchingOnTPlus) {
764         GTEST_SKIP() << "The test only applies to devices launching on T or later";
765     }
766     if (useAfterClose) {
767         GTEST_SKIP() << "Does not make sense for the useAfterClose case";
768     }
769     // Use very large size to ensure that the service does not crash.
770     const uint32_t veryLargeConfigSize = std::numeric_limits<uint32_t>::max() - 100;
771     Result retval = Result::OK;
772     Return<void> ret = effect->getCurrentConfigForFeature(
773             0, veryLargeConfigSize, [&](Result r, const hidl_vec<uint8_t>&) { retval = r; });
774     EXPECT_TRUE(ret.isOk());
775     EXPECT_EQ(Result::INVALID_ARGUMENTS, retval);
776 }
777 
778 // The main test class for Equalizer Audio Effect HIDL HAL.
779 class EqualizerAudioEffectHidlTest : public AudioEffectHidlTest {
780   public:
SetUp()781     void SetUp() override {
782         AudioEffectHidlTest::SetUp();
783         equalizer = IEqualizerEffect::castFrom(effect);
784         ASSERT_NE(nullptr, equalizer.get());
785     }
786 
TearDown()787     void TearDown() override {
788         equalizer.clear();
789         AudioEffectHidlTest::TearDown();
790     }
791 
792   protected:
793     void getNumBands(uint16_t* numBands);
794     void getLevelRange(int16_t* minLevel, int16_t* maxLevel);
795     void getBandFrequencyRange(uint16_t band, uint32_t* minFreq, uint32_t* centerFreq,
796                                uint32_t* maxFreq);
797     void getPresetCount(size_t* count);
798 
799     sp<IEqualizerEffect> equalizer;
800 };
801 
getNumBands(uint16_t * numBands)802 void EqualizerAudioEffectHidlTest::getNumBands(uint16_t* numBands) {
803     Result retval = Result::NOT_INITIALIZED;
804     Return<void> ret = equalizer->getNumBands([&](Result r, uint16_t b) {
805         retval = r;
806         if (retval == Result::OK) {
807             *numBands = b;
808         }
809     });
810     ASSERT_TRUE(ret.isOk());
811     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
812     if (useAfterClose) *numBands = 1;
813 }
814 
getLevelRange(int16_t * minLevel,int16_t * maxLevel)815 void EqualizerAudioEffectHidlTest::getLevelRange(int16_t* minLevel, int16_t* maxLevel) {
816     Result retval = Result::NOT_INITIALIZED;
817     Return<void> ret = equalizer->getLevelRange([&](Result r, int16_t min, int16_t max) {
818         retval = r;
819         if (retval == Result::OK) {
820             *minLevel = min;
821             *maxLevel = max;
822         }
823     });
824     ASSERT_TRUE(ret.isOk());
825     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
826     if (useAfterClose) {
827         *minLevel = 0;
828         *maxLevel = 255;
829     }
830 }
831 
getBandFrequencyRange(uint16_t band,uint32_t * minFreq,uint32_t * centerFreq,uint32_t * maxFreq)832 void EqualizerAudioEffectHidlTest::getBandFrequencyRange(uint16_t band, uint32_t* minFreq,
833                                                          uint32_t* centerFreq, uint32_t* maxFreq) {
834     Result retval = Result::NOT_INITIALIZED;
835     Return<void> ret =
836         equalizer->getBandFrequencyRange(band, [&](Result r, uint32_t min, uint32_t max) {
837             retval = r;
838             if (retval == Result::OK) {
839                 *minFreq = min;
840                 *maxFreq = max;
841             }
842         });
843     ASSERT_TRUE(ret.isOk());
844     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
845     ret = equalizer->getBandCenterFrequency(band, [&](Result r, uint32_t center) {
846         retval = r;
847         if (retval == Result::OK) {
848             *centerFreq = center;
849         }
850     });
851     ASSERT_TRUE(ret.isOk());
852     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
853     if (useAfterClose) {
854         *minFreq = 20;
855         *centerFreq = 10000;
856         *maxFreq = 20000;
857     }
858 }
859 
getPresetCount(size_t * count)860 void EqualizerAudioEffectHidlTest::getPresetCount(size_t* count) {
861     Result retval = Result::NOT_INITIALIZED;
862     Return<void> ret = equalizer->getPresetNames([&](Result r, const hidl_vec<hidl_string>& names) {
863         retval = r;
864         if (retval == Result::OK) {
865             *count = names.size();
866         }
867     });
868     ASSERT_TRUE(ret.isOk());
869     ASSERT_NO_FATAL_FAILURE(checkResult(retval));
870     if (useAfterClose) *count = 1;
871 }
872 
TEST_P(EqualizerAudioEffectHidlTest,GetNumBands)873 TEST_P(EqualizerAudioEffectHidlTest, GetNumBands) {
874     description("Verify that Equalizer effect reports at least one band");
875     uint16_t numBands = 0;
876     ASSERT_NO_FATAL_FAILURE(getNumBands(&numBands));
877     EXPECT_GT(numBands, 0);
878 }
879 
TEST_P(EqualizerAudioEffectHidlTest,GetLevelRange)880 TEST_P(EqualizerAudioEffectHidlTest, GetLevelRange) {
881     description("Verify that Equalizer effect reports adequate band level range");
882     int16_t minLevel = 0x7fff, maxLevel = 0;
883     ASSERT_NO_FATAL_FAILURE(getLevelRange(&minLevel, &maxLevel));
884     EXPECT_GT(maxLevel, minLevel);
885 }
886 
TEST_P(EqualizerAudioEffectHidlTest,GetSetBandLevel)887 TEST_P(EqualizerAudioEffectHidlTest, GetSetBandLevel) {
888     description("Verify that manipulating band levels works for Equalizer effect");
889     uint16_t numBands = 0;
890     ASSERT_NO_FATAL_FAILURE(getNumBands(&numBands));
891     ASSERT_GT(numBands, 0);
892     int16_t levels[3]{0x7fff, 0, 0};
893     ASSERT_NO_FATAL_FAILURE(getLevelRange(&levels[0], &levels[2]));
894     ASSERT_GT(levels[2], levels[0]);
895     levels[1] = (levels[2] + levels[0]) / 2;
896     for (uint16_t i = 0; i < numBands; ++i) {
897         for (size_t j = 0; j < ARRAY_SIZE(levels); ++j) {
898             Return<Result> ret = equalizer->setBandLevel(i, levels[j]);
899             EXPECT_TRUE(ret.isOk());
900             EXPECT_NO_FATAL_FAILURE(checkResult(ret));
901             Result retval = Result::NOT_INITIALIZED;
902             int16_t actualLevel;
903             Return<void> ret2 = equalizer->getBandLevel(i, [&](Result r, int16_t l) {
904                 retval = r;
905                 if (retval == Result::OK) {
906                     actualLevel = l;
907                 }
908             });
909             EXPECT_TRUE(ret2.isOk());
910             EXPECT_NO_FATAL_FAILURE(checkResult(retval));
911             if (!useAfterClose) {
912                 EXPECT_EQ(levels[j], actualLevel);
913             }
914         }
915     }
916 }
917 
TEST_P(EqualizerAudioEffectHidlTest,GetBandCenterFrequencyAndRange)918 TEST_P(EqualizerAudioEffectHidlTest, GetBandCenterFrequencyAndRange) {
919     description("Verify that Equalizer effect reports adequate band frequency range");
920     uint16_t numBands = 0;
921     ASSERT_NO_FATAL_FAILURE(getNumBands(&numBands));
922     ASSERT_GT(numBands, 0);
923     for (uint16_t i = 0; i < numBands; ++i) {
924         uint32_t minFreq = 0xffffffff, centerFreq = 0xffffffff, maxFreq = 0xffffffff;
925         ASSERT_NO_FATAL_FAILURE(getBandFrequencyRange(i, &minFreq, &centerFreq, &maxFreq));
926         // Note: NXP legacy implementation reports "1" as upper bound for last band,
927         // so this check fails.
928         EXPECT_GE(maxFreq, centerFreq);
929         EXPECT_GE(centerFreq, minFreq);
930     }
931 }
932 
TEST_P(EqualizerAudioEffectHidlTest,GetBandForFrequency)933 TEST_P(EqualizerAudioEffectHidlTest, GetBandForFrequency) {
934     description("Verify that Equalizer effect supports GetBandForFrequency correctly");
935     uint16_t numBands = 0;
936     ASSERT_NO_FATAL_FAILURE(getNumBands(&numBands));
937     ASSERT_GT(numBands, 0);
938     for (uint16_t i = 0; i < numBands; ++i) {
939         uint32_t freqs[3]{0, 0, 0};
940         getBandFrequencyRange(i, &freqs[0], &freqs[1], &freqs[2]);
941         // NXP legacy implementation reports "1" as upper bound for last band, some
942         // of the checks fail.
943         for (size_t j = 0; j < ARRAY_SIZE(freqs); ++j) {
944             if (j == 0) {
945                 freqs[j]++;
946             }  // Min frequency is an open interval.
947             Result retval = Result::NOT_INITIALIZED;
948             uint16_t actualBand = numBands + 1;
949             Return<void> ret = equalizer->getBandForFrequency(freqs[j], [&](Result r, uint16_t b) {
950                 retval = r;
951                 if (retval == Result::OK) {
952                     actualBand = b;
953                 }
954             });
955             EXPECT_TRUE(ret.isOk());
956             EXPECT_NO_FATAL_FAILURE(checkResult(retval));
957             if (!useAfterClose) {
958                 EXPECT_EQ(i, actualBand) << "Frequency: " << freqs[j];
959             }
960         }
961     }
962 }
963 
TEST_P(EqualizerAudioEffectHidlTest,GetPresetNames)964 TEST_P(EqualizerAudioEffectHidlTest, GetPresetNames) {
965     description("Verify that Equalizer effect reports at least one preset");
966     size_t presetCount;
967     ASSERT_NO_FATAL_FAILURE(getPresetCount(&presetCount));
968     EXPECT_GT(presetCount, 0u);
969 }
970 
TEST_P(EqualizerAudioEffectHidlTest,GetSetCurrentPreset)971 TEST_P(EqualizerAudioEffectHidlTest, GetSetCurrentPreset) {
972     description("Verify that manipulating the current preset for Equalizer effect");
973     size_t presetCount;
974     ASSERT_NO_FATAL_FAILURE(getPresetCount(&presetCount));
975     ASSERT_GT(presetCount, 0u);
976     for (uint16_t i = 0; i < presetCount; ++i) {
977         Return<Result> ret = equalizer->setCurrentPreset(i);
978         EXPECT_TRUE(ret.isOk());
979         EXPECT_NO_FATAL_FAILURE(checkResult(ret));
980         Result retval = Result::NOT_INITIALIZED;
981         uint16_t actualPreset = 0xffff;
982         Return<void> ret2 = equalizer->getCurrentPreset([&](Result r, uint16_t p) {
983             retval = r;
984             if (retval == Result::OK) {
985                 actualPreset = p;
986             }
987         });
988         EXPECT_TRUE(ret2.isOk());
989         EXPECT_NO_FATAL_FAILURE(checkResult(retval));
990         if (!useAfterClose) {
991             EXPECT_EQ(i, actualPreset);
992         }
993     }
994 }
995 
TEST_P(EqualizerAudioEffectHidlTest,GetSetAllProperties)996 TEST_P(EqualizerAudioEffectHidlTest, GetSetAllProperties) {
997     description(
998         "Verify that setting band levels and presets works via Get / "
999         "SetAllProperties for Equalizer effect");
1000     using AllProperties =
1001         ::android::hardware::audio::effect::CPP_VERSION::IEqualizerEffect::AllProperties;
1002     uint16_t numBands = 0;
1003     ASSERT_NO_FATAL_FAILURE(getNumBands(&numBands));
1004     ASSERT_GT(numBands, 0);
1005     AllProperties props;
1006     props.bandLevels.resize(numBands);
1007     for (size_t i = 0; i < numBands; ++i) {
1008         props.bandLevels[i] = 0;
1009     }
1010 
1011     AllProperties actualProps;
1012     Result retval = Result::NOT_INITIALIZED;
1013 
1014     // Verify setting of the band levels via properties.
1015     props.curPreset = -1;
1016     Return<Result> ret = equalizer->setAllProperties(props);
1017     EXPECT_TRUE(ret.isOk());
1018     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
1019     Return<void> ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
1020         retval = r;
1021         if (retval == Result::OK) {
1022             actualProps = p;
1023         }
1024     });
1025     EXPECT_TRUE(ret2.isOk());
1026     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
1027     if (!useAfterClose) {
1028         EXPECT_EQ(props.bandLevels, actualProps.bandLevels);
1029     }
1030 
1031     // Verify setting of the current preset via properties.
1032     props.curPreset = 0;  // Assuming there is at least one preset.
1033     ret = equalizer->setAllProperties(props);
1034     EXPECT_TRUE(ret.isOk());
1035     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
1036     ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
1037         retval = r;
1038         if (retval == Result::OK) {
1039             actualProps = p;
1040         }
1041     });
1042     EXPECT_TRUE(ret2.isOk());
1043     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
1044     if (!useAfterClose) {
1045         EXPECT_EQ(props.curPreset, actualProps.curPreset);
1046     }
1047 }
1048 
1049 // The main test class for Equalizer Audio Effect HIDL HAL.
1050 class LoudnessEnhancerAudioEffectHidlTest : public AudioEffectHidlTest {
1051   public:
SetUp()1052     void SetUp() override {
1053         AudioEffectHidlTest::SetUp();
1054         enhancer = ILoudnessEnhancerEffect::castFrom(effect);
1055         ASSERT_NE(nullptr, enhancer.get());
1056     }
1057 
TearDown()1058     void TearDown() override {
1059         enhancer.clear();
1060         AudioEffectHidlTest::TearDown();
1061     }
1062 
1063   protected:
1064     sp<ILoudnessEnhancerEffect> enhancer;
1065 };
1066 
TEST_P(LoudnessEnhancerAudioEffectHidlTest,GetSetTargetGain)1067 TEST_P(LoudnessEnhancerAudioEffectHidlTest, GetSetTargetGain) {
1068     description(
1069         "Verify that manipulating the target gain works for Loudness Enhancer "
1070         "effect");
1071     const int32_t gain = 100;
1072     Return<Result> ret = enhancer->setTargetGain(gain);
1073     EXPECT_TRUE(ret.isOk());
1074     EXPECT_NO_FATAL_FAILURE(checkResult(ret));
1075     int32_t actualGain = 0;
1076     Result retval;
1077     Return<void> ret2 = enhancer->getTargetGain([&](Result r, int32_t g) {
1078         retval = r;
1079         if (retval == Result::OK) {
1080             actualGain = g;
1081         }
1082     });
1083     EXPECT_TRUE(ret2.isOk());
1084     EXPECT_NO_FATAL_FAILURE(checkResult(retval));
1085     if (!useAfterClose) {
1086         EXPECT_EQ(gain, actualGain);
1087     }
1088 }
1089 
1090 INSTANTIATE_TEST_SUITE_P(EffectsFactory, AudioEffectsFactoryHidlTest,
1091                          ::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
1092                                  IEffectsFactory::descriptor)),
1093                          ::android::hardware::PrintInstanceNameToString);
1094 INSTANTIATE_TEST_SUITE_P(
1095         Equalizer_IEffect, AudioEffectHidlTest,
1096         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
1097                                    IEffectsFactory::descriptor)),
1098                            ::testing::Values(EQUALIZER_EFFECT_TYPE),
1099                            ::testing::Values(false, true) /*useAfterClose*/),
1100         EffectParameterToString);
1101 INSTANTIATE_TEST_SUITE_P(
1102         LoudnessEnhancer_IEffect, AudioEffectHidlTest,
1103         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
1104                                    IEffectsFactory::descriptor)),
1105                            ::testing::Values(LOUDNESS_ENHANCER_EFFECT_TYPE),
1106                            ::testing::Values(false, true) /*useAfterClose*/),
1107         EffectParameterToString);
1108 INSTANTIATE_TEST_SUITE_P(
1109         Equalizer, EqualizerAudioEffectHidlTest,
1110         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
1111                                    IEffectsFactory::descriptor)),
1112                            ::testing::Values(EQUALIZER_EFFECT_TYPE),
1113                            ::testing::Values(false, true) /*useAfterClose*/),
1114         EffectParameterToString);
1115 INSTANTIATE_TEST_SUITE_P(
1116         LoudnessEnhancer, LoudnessEnhancerAudioEffectHidlTest,
1117         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
1118                                    IEffectsFactory::descriptor)),
1119                            ::testing::Values(LOUDNESS_ENHANCER_EFFECT_TYPE),
1120                            ::testing::Values(false, true) /*useAfterClose*/),
1121         EffectParameterToString);
1122 // When the VTS test runs on a device lacking the corresponding HAL version the parameter
1123 // list is empty, this isn't a problem.
1124 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectsFactoryHidlTest);
1125 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectHidlTest);
1126 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerAudioEffectHidlTest);
1127 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LoudnessEnhancerAudioEffectHidlTest);
1128