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