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 #define LOG_TAG "AudioEffectHidlHalTest"
18 #include <android-base/logging.h>
19 #include <system/audio.h>
20 
21 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffect.h)
22 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffectsFactory.h)
23 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEqualizerEffect.h)
24 #include PATH(android/hardware/audio/effect/FILE_VERSION/ILoudnessEnhancerEffect.h)
25 #include PATH(android/hardware/audio/effect/FILE_VERSION/types.h)
26 #include <android/hidl/allocator/1.0/IAllocator.h>
27 #include <android/hidl/memory/1.0/IMemory.h>
28 
29 #include <common/all-versions/VersionUtils.h>
30 
31 #include <gtest/gtest.h>
32 #include <hidl/GtestPrinter.h>
33 #include <hidl/ServiceManagement.h>
34 
35 using ::android::sp;
36 using ::android::hardware::hidl_handle;
37 using ::android::hardware::hidl_memory;
38 using ::android::hardware::hidl_string;
39 using ::android::hardware::hidl_vec;
40 using ::android::hardware::MQDescriptorSync;
41 using ::android::hardware::Return;
42 using ::android::hardware::Void;
43 using ::android::hardware::audio::common::utils::mkEnumBitfield;
44 using ::android::hidl::allocator::V1_0::IAllocator;
45 using ::android::hidl::memory::V1_0::IMemory;
46 using namespace ::android::hardware::audio::common::CPP_VERSION;
47 using namespace ::android::hardware::audio::effect::CPP_VERSION;
48 
49 #ifndef ARRAY_SIZE
50 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
51 #endif
52 
53 class AudioEffectsFactoryHidlTest : public ::testing::TestWithParam<std::string> {
54   public:
SetUp()55     void SetUp() override {
56         effectsFactory = IEffectsFactory::getService(GetParam());
57         ASSERT_NE(effectsFactory, nullptr);
58     }
TearDown()59     void TearDown() override { effectsFactory.clear(); }
60 
61    protected:
description(const std::string & description)62     static void description(const std::string& description) {
63         RecordProperty("description", description);
64     }
65 
66     sp<IEffectsFactory> effectsFactory;
67 };
68 
TEST_P(AudioEffectsFactoryHidlTest,EnumerateEffects)69 TEST_P(AudioEffectsFactoryHidlTest, EnumerateEffects) {
70     description("Verify that EnumerateEffects returns at least one effect");
71     Result retval = Result::NOT_INITIALIZED;
72     size_t effectCount = 0;
73     Return<void> ret =
74         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
75             retval = r;
76             effectCount = result.size();
77         });
78     EXPECT_TRUE(ret.isOk());
79     EXPECT_EQ(Result::OK, retval);
80     EXPECT_GT(effectCount, 0u);
81 }
82 
TEST_P(AudioEffectsFactoryHidlTest,CreateEffect)83 TEST_P(AudioEffectsFactoryHidlTest, CreateEffect) {
84     description("Verify that an effect can be created via CreateEffect");
85     bool gotEffect = false;
86     Uuid effectUuid;
87     Return<void> ret =
88         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
89             if (r == Result::OK && result.size() > 0) {
90                 gotEffect = true;
91                 effectUuid = result[0].uuid;
92             }
93         });
94     ASSERT_TRUE(ret.isOk());
95     ASSERT_TRUE(gotEffect);
96     Result retval = Result::NOT_INITIALIZED;
97     sp<IEffect> effect;
98     ret = effectsFactory->createEffect(
99             effectUuid, 1 /*session*/, 1 /*ioHandle*/,
100 #if MAJOR_VERSION >= 6
101             0 /*device*/,
102 #endif
103             [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) {
104                 retval = r;
105                 if (r == Result::OK) {
106                     effect = result;
107                 }
108             });
109     EXPECT_TRUE(ret.isOk());
110     EXPECT_EQ(Result::OK, retval);
111     EXPECT_NE(nullptr, effect.get());
112 }
113 
TEST_P(AudioEffectsFactoryHidlTest,GetDescriptor)114 TEST_P(AudioEffectsFactoryHidlTest, GetDescriptor) {
115     description(
116         "Verify that effects factory can provide an effect descriptor via "
117         "GetDescriptor");
118     hidl_vec<EffectDescriptor> allDescriptors;
119     Return<void> ret =
120         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
121             if (r == Result::OK) {
122                 allDescriptors = result;
123             }
124         });
125     ASSERT_TRUE(ret.isOk());
126     ASSERT_GT(allDescriptors.size(), 0u);
127     for (size_t i = 0; i < allDescriptors.size(); ++i) {
128         ret = effectsFactory->getDescriptor(allDescriptors[i].uuid,
129                                             [&](Result r, const EffectDescriptor& result) {
130                                                 EXPECT_EQ(r, Result::OK);
131                                                 EXPECT_EQ(result, allDescriptors[i]);
132                                             });
133     }
134     EXPECT_TRUE(ret.isOk());
135 }
136 
TEST_P(AudioEffectsFactoryHidlTest,DebugDumpInvalidArgument)137 TEST_P(AudioEffectsFactoryHidlTest, DebugDumpInvalidArgument) {
138     description("Verify that debugDump doesn't crash on invalid arguments");
139 #if MAJOR_VERSION == 2
140     Return<void> ret = effectsFactory->debugDump(hidl_handle());
141 #elif MAJOR_VERSION >= 4
142     Return<void> ret = effectsFactory->debug(hidl_handle(), {});
143 #endif
144     ASSERT_TRUE(ret.isOk());
145 }
146 
147 // Equalizer effect is required by CDD, but only the type is fixed.
148 // This is the same UUID as AudioEffect.EFFECT_TYPE_EQUALIZER in Java.
149 static const Uuid EQUALIZER_EFFECT_TYPE = {
150     0x0bed4300, 0xddd6, 0x11db, 0x8f34,
151     std::array<uint8_t, 6>{{0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}};
152 // Loudness Enhancer effect is required by CDD, but only the type is fixed.
153 // This is the same UUID as AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER in Java.
154 static const Uuid LOUDNESS_ENHANCER_EFFECT_TYPE = {
155     0xfe3199be, 0xaed0, 0x413f, 0x87bb,
156     std::array<uint8_t, 6>{{0x11, 0x26, 0x0e, 0xb6, 0x3c, 0xf1}}};
157 
158 enum { PARAM_FACTORY_NAME, PARAM_EFFECT_UUID };
159 using EffectParameter = std::tuple<std::string, Uuid>;
160 
EffectParameterToString(const::testing::TestParamInfo<EffectParameter> & info)161 static inline std::string EffectParameterToString(
162         const ::testing::TestParamInfo<EffectParameter>& info) {
163     return ::android::hardware::PrintInstanceNameToString(::testing::TestParamInfo<std::string>{
164             std::get<PARAM_FACTORY_NAME>(info.param), info.index});
165 }
166 
167 // The main test class for Audio Effect HIDL HAL.
168 class AudioEffectHidlTest : public ::testing::TestWithParam<EffectParameter> {
169   public:
SetUp()170     void SetUp() override {
171         effectsFactory = IEffectsFactory::getService(std::get<PARAM_FACTORY_NAME>(GetParam()));
172         ASSERT_NE(nullptr, effectsFactory.get());
173 
174         findAndCreateEffect(getEffectType());
175         ASSERT_NE(nullptr, effect.get());
176 
177         Return<Result> ret = effect->init();
178         ASSERT_TRUE(ret.isOk());
179         ASSERT_EQ(Result::OK, ret);
180     }
181 
TearDown()182     void TearDown() override {
183         effect.clear();
184         effectsFactory.clear();
185     }
186 
187    protected:
description(const std::string & description)188     static void description(const std::string& description) {
189         RecordProperty("description", description);
190     }
191 
getEffectType() const192     Uuid getEffectType() const { return std::get<PARAM_EFFECT_UUID>(GetParam()); }
193 
194     void findAndCreateEffect(const Uuid& type);
195     void findEffectInstance(const Uuid& type, Uuid* uuid);
196     void getChannelCount(uint32_t* channelCount);
197 
198     sp<IEffectsFactory> effectsFactory;
199     sp<IEffect> effect;
200 };
201 
findAndCreateEffect(const Uuid & type)202 void AudioEffectHidlTest::findAndCreateEffect(const Uuid& type) {
203     Uuid effectUuid;
204     findEffectInstance(type, &effectUuid);
205     Return<void> ret = effectsFactory->createEffect(
206             effectUuid, 1 /*session*/, 1 /*ioHandle*/,
207 #if MAJOR_VERSION >= 6
208             0 /*device*/,
209 #endif
210             [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) {
211                 if (r == Result::OK) {
212                     effect = result;
213                 }
214             });
215     ASSERT_TRUE(ret.isOk());
216 }
217 
findEffectInstance(const Uuid & type,Uuid * uuid)218 void AudioEffectHidlTest::findEffectInstance(const Uuid& type, Uuid* uuid) {
219     bool effectFound = false;
220     Return<void> ret =
221         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
222             if (r == Result::OK) {
223                 for (const auto& desc : result) {
224                     if (desc.type == type) {
225                         effectFound = true;
226                         *uuid = desc.uuid;
227                         break;
228                     }
229                 }
230             }
231         });
232     ASSERT_TRUE(ret.isOk());
233     ASSERT_TRUE(effectFound);
234 }
235 
getChannelCount(uint32_t * channelCount)236 void AudioEffectHidlTest::getChannelCount(uint32_t* channelCount) {
237     Result retval;
238     EffectConfig currentConfig;
239     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
240         retval = r;
241         if (r == Result::OK) {
242             currentConfig = conf;
243         }
244     });
245     ASSERT_TRUE(ret.isOk());
246     ASSERT_EQ(Result::OK, retval);
247     ASSERT_TRUE(audio_channel_mask_is_valid(
248         static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels)));
249     *channelCount = audio_channel_count_from_out_mask(
250         static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels));
251 }
252 
TEST_P(AudioEffectHidlTest,Close)253 TEST_P(AudioEffectHidlTest, Close) {
254     description("Verify that an effect can be closed");
255     Return<Result> ret = effect->close();
256     EXPECT_TRUE(ret.isOk());
257     EXPECT_EQ(Result::OK, ret);
258 }
259 
TEST_P(AudioEffectHidlTest,GetDescriptor)260 TEST_P(AudioEffectHidlTest, GetDescriptor) {
261     description("Verify that an effect can return its own descriptor via GetDescriptor");
262     Result retval = Result::NOT_INITIALIZED;
263     Uuid actualType;
264     Return<void> ret = effect->getDescriptor([&](Result r, const EffectDescriptor& desc) {
265         retval = r;
266         if (r == Result::OK) {
267             actualType = desc.type;
268         }
269     });
270     EXPECT_TRUE(ret.isOk());
271     EXPECT_EQ(Result::OK, retval);
272     EXPECT_EQ(getEffectType(), actualType);
273 }
274 
TEST_P(AudioEffectHidlTest,GetSetConfig)275 TEST_P(AudioEffectHidlTest, GetSetConfig) {
276     description(
277         "Verify that it is possible to manipulate effect config via Get / "
278         "SetConfig");
279     Result retval = Result::NOT_INITIALIZED;
280     EffectConfig currentConfig;
281     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
282         retval = r;
283         if (r == Result::OK) {
284             currentConfig = conf;
285         }
286     });
287     EXPECT_TRUE(ret.isOk());
288     EXPECT_EQ(Result::OK, retval);
289     Return<Result> ret2 = effect->setConfig(currentConfig, nullptr, nullptr);
290     EXPECT_TRUE(ret2.isOk());
291     EXPECT_EQ(Result::OK, ret2);
292 }
293 
TEST_P(AudioEffectHidlTest,GetConfigReverse)294 TEST_P(AudioEffectHidlTest, GetConfigReverse) {
295     description("Verify that GetConfigReverse does not crash");
296     Return<void> ret = effect->getConfigReverse([&](Result, const EffectConfig&) {});
297     EXPECT_TRUE(ret.isOk());
298 }
299 
TEST_P(AudioEffectHidlTest,GetSupportedAuxChannelsConfigs)300 TEST_P(AudioEffectHidlTest, GetSupportedAuxChannelsConfigs) {
301     description("Verify that GetSupportedAuxChannelsConfigs does not crash");
302     Return<void> ret = effect->getSupportedAuxChannelsConfigs(
303         0, [&](Result, const hidl_vec<EffectAuxChannelsConfig>&) {});
304     EXPECT_TRUE(ret.isOk());
305 }
306 
TEST_P(AudioEffectHidlTest,GetAuxChannelsConfig)307 TEST_P(AudioEffectHidlTest, GetAuxChannelsConfig) {
308     description("Verify that GetAuxChannelsConfig does not crash");
309     Return<void> ret = effect->getAuxChannelsConfig([&](Result, const EffectAuxChannelsConfig&) {});
310     EXPECT_TRUE(ret.isOk());
311 }
312 
TEST_P(AudioEffectHidlTest,SetAuxChannelsConfig)313 TEST_P(AudioEffectHidlTest, SetAuxChannelsConfig) {
314     description("Verify that SetAuxChannelsConfig does not crash");
315     Return<Result> ret = effect->setAuxChannelsConfig(EffectAuxChannelsConfig());
316     EXPECT_TRUE(ret.isOk());
317 }
318 
319 // Not generated automatically because AudioBuffer contains
320 // instances of hidl_memory which can't be compared properly
321 // in general case due to presence of handles.
322 //
323 // However, in this particular case, handles must not present
324 // thus comparison is possible.
325 //
326 // operator== must be defined in the same namespace as the structures.
327 namespace android {
328 namespace hardware {
329 namespace audio {
330 namespace effect {
331 namespace CPP_VERSION {
operator ==(const AudioBuffer & lhs,const AudioBuffer & rhs)332 inline bool operator==(const AudioBuffer& lhs, const AudioBuffer& rhs) {
333     return lhs.id == rhs.id && lhs.frameCount == rhs.frameCount && lhs.data.handle() == nullptr &&
334            rhs.data.handle() == nullptr;
335 }
336 
operator ==(const EffectBufferConfig & lhs,const EffectBufferConfig & rhs)337 inline bool operator==(const EffectBufferConfig& lhs, const EffectBufferConfig& rhs) {
338     return lhs.buffer == rhs.buffer && lhs.samplingRateHz == rhs.samplingRateHz &&
339            lhs.channels == rhs.channels && lhs.format == rhs.format &&
340            lhs.accessMode == rhs.accessMode && lhs.mask == rhs.mask;
341 }
342 
operator ==(const EffectConfig & lhs,const EffectConfig & rhs)343 inline bool operator==(const EffectConfig& lhs, const EffectConfig& rhs) {
344     return lhs.inputCfg == rhs.inputCfg && lhs.outputCfg == rhs.outputCfg;
345 }
346 }  // namespace CPP_VERSION
347 }  // namespace effect
348 }  // namespace audio
349 }  // namespace hardware
350 }  // namespace android
351 
TEST_P(AudioEffectHidlTest,Reset)352 TEST_P(AudioEffectHidlTest, Reset) {
353     description("Verify that Reset preserves effect configuration");
354     Result retval = Result::NOT_INITIALIZED;
355     EffectConfig originalConfig;
356     Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
357         retval = r;
358         if (r == Result::OK) {
359             originalConfig = conf;
360         }
361     });
362     ASSERT_TRUE(ret.isOk());
363     ASSERT_EQ(Result::OK, retval);
364     Return<Result> ret2 = effect->reset();
365     EXPECT_TRUE(ret2.isOk());
366     EXPECT_EQ(Result::OK, ret2);
367     EffectConfig configAfterReset;
368     ret = effect->getConfig([&](Result r, const EffectConfig& conf) {
369         retval = r;
370         if (r == Result::OK) {
371             configAfterReset = conf;
372         }
373     });
374     EXPECT_EQ(originalConfig, configAfterReset);
375 }
376 
TEST_P(AudioEffectHidlTest,DisableEnableDisable)377 TEST_P(AudioEffectHidlTest, DisableEnableDisable) {
378     description("Verify Disable -> Enable -> Disable sequence for an effect");
379     Return<Result> ret = effect->disable();
380     EXPECT_TRUE(ret.isOk());
381     // Note: some legacy effects may return -EINVAL (INVALID_ARGUMENTS),
382     //       more canonical is to return -ENOSYS (NOT_SUPPORTED)
383     EXPECT_TRUE(ret == Result::NOT_SUPPORTED || ret == Result::INVALID_ARGUMENTS);
384     ret = effect->enable();
385     EXPECT_TRUE(ret.isOk());
386     EXPECT_EQ(Result::OK, ret);
387     ret = effect->disable();
388     EXPECT_TRUE(ret.isOk());
389     EXPECT_EQ(Result::OK, ret);
390 }
391 
TEST_P(AudioEffectHidlTest,SetDevice)392 TEST_P(AudioEffectHidlTest, SetDevice) {
393     description("Verify that SetDevice works for an output chain effect");
394     Return<Result> ret = effect->setDevice(mkEnumBitfield(AudioDevice::OUT_SPEAKER));
395     EXPECT_TRUE(ret.isOk());
396     EXPECT_EQ(Result::OK, ret);
397 }
398 
TEST_P(AudioEffectHidlTest,SetAndGetVolume)399 TEST_P(AudioEffectHidlTest, SetAndGetVolume) {
400     description("Verify that SetAndGetVolume method works for an effect");
401     uint32_t channelCount;
402     getChannelCount(&channelCount);
403     hidl_vec<uint32_t> volumes;
404     volumes.resize(channelCount);
405     for (uint32_t i = 0; i < channelCount; ++i) {
406         volumes[i] = 0;
407     }
408     Result retval = Result::NOT_INITIALIZED;
409     Return<void> ret =
410         effect->setAndGetVolume(volumes, [&](Result r, const hidl_vec<uint32_t>&) { retval = r; });
411     EXPECT_TRUE(ret.isOk());
412     EXPECT_EQ(Result::OK, retval);
413 }
414 
TEST_P(AudioEffectHidlTest,VolumeChangeNotification)415 TEST_P(AudioEffectHidlTest, VolumeChangeNotification) {
416     description("Verify that effect accepts VolumeChangeNotification");
417     uint32_t channelCount;
418     getChannelCount(&channelCount);
419     hidl_vec<uint32_t> volumes;
420     volumes.resize(channelCount);
421     for (uint32_t i = 0; i < channelCount; ++i) {
422         volumes[i] = 0;
423     }
424     Return<Result> ret = effect->volumeChangeNotification(volumes);
425     EXPECT_TRUE(ret.isOk());
426     EXPECT_EQ(Result::OK, ret);
427 }
428 
TEST_P(AudioEffectHidlTest,SetAudioMode)429 TEST_P(AudioEffectHidlTest, SetAudioMode) {
430     description("Verify that SetAudioMode works for an effect");
431     Return<Result> ret = effect->setAudioMode(AudioMode::NORMAL);
432     EXPECT_TRUE(ret.isOk());
433     EXPECT_EQ(Result::OK, ret);
434 }
435 
TEST_P(AudioEffectHidlTest,SetConfigReverse)436 TEST_P(AudioEffectHidlTest, SetConfigReverse) {
437     description("Verify that SetConfigReverse does not crash");
438     Return<Result> ret = effect->setConfigReverse(EffectConfig(), nullptr, nullptr);
439     EXPECT_TRUE(ret.isOk());
440 }
441 
TEST_P(AudioEffectHidlTest,SetInputDevice)442 TEST_P(AudioEffectHidlTest, SetInputDevice) {
443     description("Verify that SetInputDevice does not crash");
444     Return<Result> ret = effect->setInputDevice(mkEnumBitfield(AudioDevice::IN_BUILTIN_MIC));
445     EXPECT_TRUE(ret.isOk());
446 }
447 
TEST_P(AudioEffectHidlTest,SetAudioSource)448 TEST_P(AudioEffectHidlTest, SetAudioSource) {
449     description("Verify that SetAudioSource does not crash");
450     Return<Result> ret = effect->setAudioSource(AudioSource::MIC);
451     EXPECT_TRUE(ret.isOk());
452 }
453 
TEST_P(AudioEffectHidlTest,Offload)454 TEST_P(AudioEffectHidlTest, Offload) {
455     description("Verify that calling Offload method does not crash");
456     EffectOffloadParameter offloadParam;
457     offloadParam.isOffload = false;
458     offloadParam.ioHandle = static_cast<int>(AudioHandleConsts::AUDIO_IO_HANDLE_NONE);
459     Return<Result> ret = effect->offload(offloadParam);
460     EXPECT_TRUE(ret.isOk());
461 }
462 
TEST_P(AudioEffectHidlTest,PrepareForProcessing)463 TEST_P(AudioEffectHidlTest, PrepareForProcessing) {
464     description("Verify that PrepareForProcessing method works for an effect");
465     Result retval = Result::NOT_INITIALIZED;
466     Return<void> ret = effect->prepareForProcessing(
467         [&](Result r, const MQDescriptorSync<Result>&) { retval = r; });
468     EXPECT_TRUE(ret.isOk());
469     EXPECT_EQ(Result::OK, retval);
470 }
471 
TEST_P(AudioEffectHidlTest,SetProcessBuffers)472 TEST_P(AudioEffectHidlTest, SetProcessBuffers) {
473     description("Verify that SetProcessBuffers works for an effect");
474     sp<IAllocator> ashmem = IAllocator::getService("ashmem");
475     ASSERT_NE(nullptr, ashmem.get());
476     bool success = false;
477     AudioBuffer buffer;
478     Return<void> ret = ashmem->allocate(1024, [&](bool s, const hidl_memory& memory) {
479         success = s;
480         if (s) {
481             buffer.data = memory;
482         }
483     });
484     ASSERT_TRUE(ret.isOk());
485     ASSERT_TRUE(success);
486     Return<Result> ret2 = effect->setProcessBuffers(buffer, buffer);
487     EXPECT_TRUE(ret2.isOk());
488     EXPECT_EQ(Result::OK, ret2);
489 }
490 
TEST_P(AudioEffectHidlTest,Command)491 TEST_P(AudioEffectHidlTest, Command) {
492     description("Verify that Command does not crash");
493     Return<void> ret =
494         effect->command(0, hidl_vec<uint8_t>(), 0, [&](int32_t, const hidl_vec<uint8_t>&) {});
495     EXPECT_TRUE(ret.isOk());
496 }
497 
TEST_P(AudioEffectHidlTest,SetParameter)498 TEST_P(AudioEffectHidlTest, SetParameter) {
499     description("Verify that SetParameter does not crash");
500     Return<Result> ret = effect->setParameter(hidl_vec<uint8_t>(), hidl_vec<uint8_t>());
501     EXPECT_TRUE(ret.isOk());
502 }
503 
TEST_P(AudioEffectHidlTest,GetParameter)504 TEST_P(AudioEffectHidlTest, GetParameter) {
505     description("Verify that GetParameter does not crash");
506     Return<void> ret =
507         effect->getParameter(hidl_vec<uint8_t>(), 0, [&](Result, const hidl_vec<uint8_t>&) {});
508     EXPECT_TRUE(ret.isOk());
509 }
510 
TEST_P(AudioEffectHidlTest,GetSupportedConfigsForFeature)511 TEST_P(AudioEffectHidlTest, GetSupportedConfigsForFeature) {
512     description("Verify that GetSupportedConfigsForFeature does not crash");
513     Return<void> ret = effect->getSupportedConfigsForFeature(
514         0, 0, 0, [&](Result, uint32_t, const hidl_vec<uint8_t>&) {});
515     EXPECT_TRUE(ret.isOk());
516 }
517 
TEST_P(AudioEffectHidlTest,GetCurrentConfigForFeature)518 TEST_P(AudioEffectHidlTest, GetCurrentConfigForFeature) {
519     description("Verify that GetCurrentConfigForFeature does not crash");
520     Return<void> ret =
521         effect->getCurrentConfigForFeature(0, 0, [&](Result, const hidl_vec<uint8_t>&) {});
522     EXPECT_TRUE(ret.isOk());
523 }
524 
TEST_P(AudioEffectHidlTest,SetCurrentConfigForFeature)525 TEST_P(AudioEffectHidlTest, SetCurrentConfigForFeature) {
526     description("Verify that SetCurrentConfigForFeature does not crash");
527     Return<Result> ret = effect->setCurrentConfigForFeature(0, hidl_vec<uint8_t>());
528     EXPECT_TRUE(ret.isOk());
529 }
530 
531 // The main test class for Equalizer Audio Effect HIDL HAL.
532 class EqualizerAudioEffectHidlTest : public AudioEffectHidlTest {
533   public:
SetUp()534     void SetUp() override {
535         AudioEffectHidlTest::SetUp();
536         equalizer = IEqualizerEffect::castFrom(effect);
537         ASSERT_NE(nullptr, equalizer.get());
538     }
539 
TearDown()540     void TearDown() override {
541         equalizer.clear();
542         AudioEffectHidlTest::TearDown();
543     }
544 
545   protected:
546     void getNumBands(uint16_t* numBands);
547     void getLevelRange(int16_t* minLevel, int16_t* maxLevel);
548     void getBandFrequencyRange(uint16_t band, uint32_t* minFreq, uint32_t* centerFreq,
549                                uint32_t* maxFreq);
550     void getPresetCount(size_t* count);
551 
552     sp<IEqualizerEffect> equalizer;
553 };
554 
getNumBands(uint16_t * numBands)555 void EqualizerAudioEffectHidlTest::getNumBands(uint16_t* numBands) {
556     Result retval = Result::NOT_INITIALIZED;
557     Return<void> ret = equalizer->getNumBands([&](Result r, uint16_t b) {
558         retval = r;
559         if (retval == Result::OK) {
560             *numBands = b;
561         }
562     });
563     ASSERT_TRUE(ret.isOk());
564     ASSERT_EQ(Result::OK, retval);
565 }
566 
getLevelRange(int16_t * minLevel,int16_t * maxLevel)567 void EqualizerAudioEffectHidlTest::getLevelRange(int16_t* minLevel, int16_t* maxLevel) {
568     Result retval = Result::NOT_INITIALIZED;
569     Return<void> ret = equalizer->getLevelRange([&](Result r, int16_t min, int16_t max) {
570         retval = r;
571         if (retval == Result::OK) {
572             *minLevel = min;
573             *maxLevel = max;
574         }
575     });
576     ASSERT_TRUE(ret.isOk());
577     ASSERT_EQ(Result::OK, retval);
578 }
579 
getBandFrequencyRange(uint16_t band,uint32_t * minFreq,uint32_t * centerFreq,uint32_t * maxFreq)580 void EqualizerAudioEffectHidlTest::getBandFrequencyRange(uint16_t band, uint32_t* minFreq,
581                                                          uint32_t* centerFreq, uint32_t* maxFreq) {
582     Result retval = Result::NOT_INITIALIZED;
583     Return<void> ret =
584         equalizer->getBandFrequencyRange(band, [&](Result r, uint32_t min, uint32_t max) {
585             retval = r;
586             if (retval == Result::OK) {
587                 *minFreq = min;
588                 *maxFreq = max;
589             }
590         });
591     ASSERT_TRUE(ret.isOk());
592     ASSERT_EQ(Result::OK, retval);
593     ret = equalizer->getBandCenterFrequency(band, [&](Result r, uint32_t center) {
594         retval = r;
595         if (retval == Result::OK) {
596             *centerFreq = center;
597         }
598     });
599     ASSERT_TRUE(ret.isOk());
600     ASSERT_EQ(Result::OK, retval);
601 }
602 
getPresetCount(size_t * count)603 void EqualizerAudioEffectHidlTest::getPresetCount(size_t* count) {
604     Result retval = Result::NOT_INITIALIZED;
605     Return<void> ret = equalizer->getPresetNames([&](Result r, const hidl_vec<hidl_string>& names) {
606         retval = r;
607         if (retval == Result::OK) {
608             *count = names.size();
609         }
610     });
611     ASSERT_TRUE(ret.isOk());
612     ASSERT_EQ(Result::OK, retval);
613 }
614 
TEST_P(EqualizerAudioEffectHidlTest,GetNumBands)615 TEST_P(EqualizerAudioEffectHidlTest, GetNumBands) {
616     description("Verify that Equalizer effect reports at least one band");
617     uint16_t numBands = 0;
618     getNumBands(&numBands);
619     EXPECT_GT(numBands, 0);
620 }
621 
TEST_P(EqualizerAudioEffectHidlTest,GetLevelRange)622 TEST_P(EqualizerAudioEffectHidlTest, GetLevelRange) {
623     description("Verify that Equalizer effect reports adequate band level range");
624     int16_t minLevel = 0x7fff, maxLevel = 0;
625     getLevelRange(&minLevel, &maxLevel);
626     EXPECT_GT(maxLevel, minLevel);
627 }
628 
TEST_P(EqualizerAudioEffectHidlTest,GetSetBandLevel)629 TEST_P(EqualizerAudioEffectHidlTest, GetSetBandLevel) {
630     description("Verify that manipulating band levels works for Equalizer effect");
631     uint16_t numBands = 0;
632     getNumBands(&numBands);
633     ASSERT_GT(numBands, 0);
634     int16_t levels[3]{0x7fff, 0, 0};
635     getLevelRange(&levels[0], &levels[2]);
636     ASSERT_GT(levels[2], levels[0]);
637     levels[1] = (levels[2] + levels[0]) / 2;
638     for (uint16_t i = 0; i < numBands; ++i) {
639         for (size_t j = 0; j < ARRAY_SIZE(levels); ++j) {
640             Return<Result> ret = equalizer->setBandLevel(i, levels[j]);
641             EXPECT_TRUE(ret.isOk());
642             EXPECT_EQ(Result::OK, ret);
643             Result retval = Result::NOT_INITIALIZED;
644             int16_t actualLevel;
645             Return<void> ret2 = equalizer->getBandLevel(i, [&](Result r, int16_t l) {
646                 retval = r;
647                 if (retval == Result::OK) {
648                     actualLevel = l;
649                 }
650             });
651             EXPECT_TRUE(ret2.isOk());
652             EXPECT_EQ(Result::OK, retval);
653             EXPECT_EQ(levels[j], actualLevel);
654         }
655     }
656 }
657 
TEST_P(EqualizerAudioEffectHidlTest,GetBandCenterFrequencyAndRange)658 TEST_P(EqualizerAudioEffectHidlTest, GetBandCenterFrequencyAndRange) {
659     description("Verify that Equalizer effect reports adequate band frequency range");
660     uint16_t numBands = 0;
661     getNumBands(&numBands);
662     ASSERT_GT(numBands, 0);
663     for (uint16_t i = 0; i < numBands; ++i) {
664         uint32_t minFreq = 0xffffffff, centerFreq = 0xffffffff, maxFreq = 0xffffffff;
665         getBandFrequencyRange(i, &minFreq, &centerFreq, &maxFreq);
666         // Note: NXP legacy implementation reports "1" as upper bound for last band,
667         // so this check fails.
668         EXPECT_GE(maxFreq, centerFreq);
669         EXPECT_GE(centerFreq, minFreq);
670     }
671 }
672 
TEST_P(EqualizerAudioEffectHidlTest,GetBandForFrequency)673 TEST_P(EqualizerAudioEffectHidlTest, GetBandForFrequency) {
674     description("Verify that Equalizer effect supports GetBandForFrequency correctly");
675     uint16_t numBands = 0;
676     getNumBands(&numBands);
677     ASSERT_GT(numBands, 0);
678     for (uint16_t i = 0; i < numBands; ++i) {
679         uint32_t freqs[3]{0, 0, 0};
680         getBandFrequencyRange(i, &freqs[0], &freqs[1], &freqs[2]);
681         // NXP legacy implementation reports "1" as upper bound for last band, some
682         // of the checks fail.
683         for (size_t j = 0; j < ARRAY_SIZE(freqs); ++j) {
684             if (j == 0) {
685                 freqs[j]++;
686             }  // Min frequency is an open interval.
687             Result retval = Result::NOT_INITIALIZED;
688             uint16_t actualBand = numBands + 1;
689             Return<void> ret = equalizer->getBandForFrequency(freqs[j], [&](Result r, uint16_t b) {
690                 retval = r;
691                 if (retval == Result::OK) {
692                     actualBand = b;
693                 }
694             });
695             EXPECT_TRUE(ret.isOk());
696             EXPECT_EQ(Result::OK, retval);
697             EXPECT_EQ(i, actualBand) << "Frequency: " << freqs[j];
698         }
699     }
700 }
701 
TEST_P(EqualizerAudioEffectHidlTest,GetPresetNames)702 TEST_P(EqualizerAudioEffectHidlTest, GetPresetNames) {
703     description("Verify that Equalizer effect reports at least one preset");
704     size_t presetCount;
705     getPresetCount(&presetCount);
706     EXPECT_GT(presetCount, 0u);
707 }
708 
TEST_P(EqualizerAudioEffectHidlTest,GetSetCurrentPreset)709 TEST_P(EqualizerAudioEffectHidlTest, GetSetCurrentPreset) {
710     description("Verify that manipulating the current preset for Equalizer effect");
711     size_t presetCount;
712     getPresetCount(&presetCount);
713     ASSERT_GT(presetCount, 0u);
714     for (uint16_t i = 0; i < presetCount; ++i) {
715         Return<Result> ret = equalizer->setCurrentPreset(i);
716         EXPECT_TRUE(ret.isOk());
717         EXPECT_EQ(Result::OK, ret);
718         Result retval = Result::NOT_INITIALIZED;
719         uint16_t actualPreset = 0xffff;
720         Return<void> ret2 = equalizer->getCurrentPreset([&](Result r, uint16_t p) {
721             retval = r;
722             if (retval == Result::OK) {
723                 actualPreset = p;
724             }
725         });
726         EXPECT_TRUE(ret2.isOk());
727         EXPECT_EQ(Result::OK, retval);
728         EXPECT_EQ(i, actualPreset);
729     }
730 }
731 
TEST_P(EqualizerAudioEffectHidlTest,GetSetAllProperties)732 TEST_P(EqualizerAudioEffectHidlTest, GetSetAllProperties) {
733     description(
734         "Verify that setting band levels and presets works via Get / "
735         "SetAllProperties for Equalizer effect");
736     using AllProperties =
737         ::android::hardware::audio::effect::CPP_VERSION::IEqualizerEffect::AllProperties;
738     uint16_t numBands = 0;
739     getNumBands(&numBands);
740     ASSERT_GT(numBands, 0);
741     AllProperties props;
742     props.bandLevels.resize(numBands);
743     for (size_t i = 0; i < numBands; ++i) {
744         props.bandLevels[i] = 0;
745     }
746 
747     AllProperties actualProps;
748     Result retval = Result::NOT_INITIALIZED;
749 
750     // Verify setting of the band levels via properties.
751     props.curPreset = -1;
752     Return<Result> ret = equalizer->setAllProperties(props);
753     EXPECT_TRUE(ret.isOk());
754     EXPECT_EQ(Result::OK, ret);
755     Return<void> ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
756         retval = r;
757         if (retval == Result::OK) {
758             actualProps = p;
759         }
760     });
761     EXPECT_TRUE(ret2.isOk());
762     EXPECT_EQ(Result::OK, retval);
763     EXPECT_EQ(props.bandLevels, actualProps.bandLevels);
764 
765     // Verify setting of the current preset via properties.
766     props.curPreset = 0;  // Assuming there is at least one preset.
767     ret = equalizer->setAllProperties(props);
768     EXPECT_TRUE(ret.isOk());
769     EXPECT_EQ(Result::OK, ret);
770     ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
771         retval = r;
772         if (retval == Result::OK) {
773             actualProps = p;
774         }
775     });
776     EXPECT_TRUE(ret2.isOk());
777     EXPECT_EQ(Result::OK, retval);
778     EXPECT_EQ(props.curPreset, actualProps.curPreset);
779 }
780 
781 // The main test class for Equalizer Audio Effect HIDL HAL.
782 class LoudnessEnhancerAudioEffectHidlTest : public AudioEffectHidlTest {
783   public:
SetUp()784     void SetUp() override {
785         AudioEffectHidlTest::SetUp();
786         enhancer = ILoudnessEnhancerEffect::castFrom(effect);
787         ASSERT_NE(nullptr, enhancer.get());
788     }
789 
TearDown()790     void TearDown() override {
791         enhancer.clear();
792         AudioEffectHidlTest::TearDown();
793     }
794 
795   protected:
796     sp<ILoudnessEnhancerEffect> enhancer;
797 };
798 
TEST_P(LoudnessEnhancerAudioEffectHidlTest,GetSetTargetGain)799 TEST_P(LoudnessEnhancerAudioEffectHidlTest, GetSetTargetGain) {
800     description(
801         "Verify that manipulating the target gain works for Loudness Enhancer "
802         "effect");
803     const int32_t gain = 100;
804     Return<Result> ret = enhancer->setTargetGain(gain);
805     EXPECT_TRUE(ret.isOk());
806     EXPECT_EQ(Result::OK, ret);
807     int32_t actualGain = 0;
808     Result retval;
809     Return<void> ret2 = enhancer->getTargetGain([&](Result r, int32_t g) {
810         retval = r;
811         if (retval == Result::OK) {
812             actualGain = g;
813         }
814     });
815     EXPECT_TRUE(ret2.isOk());
816     EXPECT_EQ(Result::OK, retval);
817     EXPECT_EQ(gain, actualGain);
818 }
819 
820 INSTANTIATE_TEST_SUITE_P(EffectsFactory, AudioEffectsFactoryHidlTest,
821                          ::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
822                                  IEffectsFactory::descriptor)),
823                          ::android::hardware::PrintInstanceNameToString);
824 INSTANTIATE_TEST_SUITE_P(
825         Equalizer_IEffect, AudioEffectHidlTest,
826         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
827                                    IEffectsFactory::descriptor)),
828                            ::testing::Values(EQUALIZER_EFFECT_TYPE)),
829         EffectParameterToString);
830 INSTANTIATE_TEST_SUITE_P(
831         LoudnessEnhancer_IEffect, AudioEffectHidlTest,
832         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
833                                    IEffectsFactory::descriptor)),
834                            ::testing::Values(LOUDNESS_ENHANCER_EFFECT_TYPE)),
835         EffectParameterToString);
836 INSTANTIATE_TEST_SUITE_P(
837         Equalizer, EqualizerAudioEffectHidlTest,
838         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
839                                    IEffectsFactory::descriptor)),
840                            ::testing::Values(EQUALIZER_EFFECT_TYPE)),
841         EffectParameterToString);
842 INSTANTIATE_TEST_SUITE_P(
843         LoudnessEnhancer, LoudnessEnhancerAudioEffectHidlTest,
844         ::testing::Combine(::testing::ValuesIn(::android::hardware::getAllHalInstanceNames(
845                                    IEffectsFactory::descriptor)),
846                            ::testing::Values(LOUDNESS_ENHANCER_EFFECT_TYPE)),
847         EffectParameterToString);
848 // When the VTS test runs on a device lacking the corresponding HAL version the parameter
849 // list is empty, this isn't a problem.
850 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectsFactoryHidlTest);
851 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectHidlTest);
852 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerAudioEffectHidlTest);
853 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LoudnessEnhancerAudioEffectHidlTest);
854