• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <VtsHalHidlTargetTestBase.h>
32 #include <VtsHalHidlTargetTestEnvBase.h>
33 
34 using ::android::sp;
35 using ::android::hardware::hidl_handle;
36 using ::android::hardware::hidl_memory;
37 using ::android::hardware::hidl_string;
38 using ::android::hardware::hidl_vec;
39 using ::android::hardware::MQDescriptorSync;
40 using ::android::hardware::Return;
41 using ::android::hardware::Void;
42 using ::android::hardware::audio::common::utils::mkEnumBitfield;
43 using ::android::hidl::allocator::V1_0::IAllocator;
44 using ::android::hidl::memory::V1_0::IMemory;
45 using namespace ::android::hardware::audio::common::CPP_VERSION;
46 using namespace ::android::hardware::audio::effect::CPP_VERSION;
47 
48 #ifndef ARRAY_SIZE
49 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
50 #endif
51 
52 // Test environment for Audio Effects Factory HIDL HAL.
53 class AudioEffectsFactoryHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
54    public:
55     // get the test environment singleton
Instance()56     static AudioEffectsFactoryHidlEnvironment* Instance() {
57         static AudioEffectsFactoryHidlEnvironment* instance =
58             new AudioEffectsFactoryHidlEnvironment;
59         return instance;
60     }
61 
registerTestServices()62     virtual void registerTestServices() override { registerTestService<IEffectsFactory>(); }
63 };
64 
65 // The main test class for Audio Effects Factory HIDL HAL.
66 class AudioEffectsFactoryHidlTest : public ::testing::VtsHalHidlTargetTestBase {
67    public:
SetUp()68     void SetUp() override {
69         effectsFactory = ::testing::VtsHalHidlTargetTestBase::getService<IEffectsFactory>(
70             AudioEffectsFactoryHidlEnvironment::Instance()->getServiceName<IEffectsFactory>());
71         ASSERT_NE(effectsFactory, nullptr);
72     }
73 
TearDown()74     void TearDown() override { effectsFactory.clear(); }
75 
76    protected:
description(const std::string & description)77     static void description(const std::string& description) {
78         RecordProperty("description", description);
79     }
80 
81     sp<IEffectsFactory> effectsFactory;
82 };
83 
TEST_F(AudioEffectsFactoryHidlTest,EnumerateEffects)84 TEST_F(AudioEffectsFactoryHidlTest, EnumerateEffects) {
85     description("Verify that EnumerateEffects returns at least one effect");
86     Result retval = Result::NOT_INITIALIZED;
87     size_t effectCount = 0;
88     Return<void> ret =
89         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
90             retval = r;
91             effectCount = result.size();
92         });
93     EXPECT_TRUE(ret.isOk());
94     EXPECT_EQ(Result::OK, retval);
95     EXPECT_GT(effectCount, 0u);
96 }
97 
TEST_F(AudioEffectsFactoryHidlTest,CreateEffect)98 TEST_F(AudioEffectsFactoryHidlTest, CreateEffect) {
99     description("Verify that an effect can be created via CreateEffect");
100     bool gotEffect = false;
101     Uuid effectUuid;
102     Return<void> ret =
103         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
104             if (r == Result::OK && result.size() > 0) {
105                 gotEffect = true;
106                 effectUuid = result[0].uuid;
107             }
108         });
109     ASSERT_TRUE(ret.isOk());
110     ASSERT_TRUE(gotEffect);
111     Result retval = Result::NOT_INITIALIZED;
112     sp<IEffect> effect;
113     ret = effectsFactory->createEffect(
114         effectUuid, 1 /*session*/, 1 /*ioHandle*/,
115         [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) {
116             retval = r;
117             if (r == Result::OK) {
118                 effect = result;
119             }
120         });
121     EXPECT_TRUE(ret.isOk());
122     EXPECT_EQ(Result::OK, retval);
123     EXPECT_NE(nullptr, effect.get());
124 }
125 
TEST_F(AudioEffectsFactoryHidlTest,GetDescriptor)126 TEST_F(AudioEffectsFactoryHidlTest, GetDescriptor) {
127     description(
128         "Verify that effects factory can provide an effect descriptor via "
129         "GetDescriptor");
130     hidl_vec<EffectDescriptor> allDescriptors;
131     Return<void> ret =
132         effectsFactory->getAllDescriptors([&](Result r, const hidl_vec<EffectDescriptor>& result) {
133             if (r == Result::OK) {
134                 allDescriptors = result;
135             }
136         });
137     ASSERT_TRUE(ret.isOk());
138     ASSERT_GT(allDescriptors.size(), 0u);
139     for (size_t i = 0; i < allDescriptors.size(); ++i) {
140         ret = effectsFactory->getDescriptor(allDescriptors[i].uuid,
141                                             [&](Result r, const EffectDescriptor& result) {
142                                                 EXPECT_EQ(r, Result::OK);
143                                                 EXPECT_EQ(result, allDescriptors[i]);
144                                             });
145     }
146     EXPECT_TRUE(ret.isOk());
147 }
148 
TEST_F(AudioEffectsFactoryHidlTest,DebugDumpInvalidArgument)149 TEST_F(AudioEffectsFactoryHidlTest, DebugDumpInvalidArgument) {
150     description("Verify that debugDump doesn't crash on invalid arguments");
151 #if MAJOR_VERSION == 2
152     Return<void> ret = effectsFactory->debugDump(hidl_handle());
153 #elif MAJOR_VERSION >= 4
154     Return<void> ret = effectsFactory->debug(hidl_handle(), {});
155 #endif
156     ASSERT_TRUE(ret.isOk());
157 }
158 
159 // Equalizer effect is required by CDD, but only the type is fixed.
160 // This is the same UUID as AudioEffect.EFFECT_TYPE_EQUALIZER in Java.
161 static const Uuid EQUALIZER_EFFECT_TYPE = {
162     0x0bed4300, 0xddd6, 0x11db, 0x8f34,
163     std::array<uint8_t, 6>{{0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}};
164 // Loudness Enhancer effect is required by CDD, but only the type is fixed.
165 // This is the same UUID as AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER in Java.
166 static const Uuid LOUDNESS_ENHANCER_EFFECT_TYPE = {
167     0xfe3199be, 0xaed0, 0x413f, 0x87bb,
168     std::array<uint8_t, 6>{{0x11, 0x26, 0x0e, 0xb6, 0x3c, 0xf1}}};
169 
170 // The main test class for Audio Effect HIDL HAL.
171 class AudioEffectHidlTest : public ::testing::VtsHalHidlTargetTestBase {
172    public:
SetUp()173     void SetUp() override {
174         effectsFactory = ::testing::VtsHalHidlTargetTestBase::getService<IEffectsFactory>();
175         ASSERT_NE(nullptr, effectsFactory.get());
176 
177         findAndCreateEffect(getEffectType());
178         ASSERT_NE(nullptr, effect.get());
179 
180         Return<Result> ret = effect->init();
181         ASSERT_TRUE(ret.isOk());
182         ASSERT_EQ(Result::OK, ret);
183     }
184 
TearDown()185     void TearDown() override {
186         effect.clear();
187         effectsFactory.clear();
188     }
189 
190    protected:
description(const std::string & description)191     static void description(const std::string& description) {
192         RecordProperty("description", description);
193     }
194 
getEffectType()195     virtual Uuid getEffectType() { return EQUALIZER_EFFECT_TYPE; }
196 
197     void findAndCreateEffect(const Uuid& type);
198     void findEffectInstance(const Uuid& type, Uuid* uuid);
199     void getChannelCount(uint32_t* channelCount);
200 
201     sp<IEffectsFactory> effectsFactory;
202     sp<IEffect> effect;
203 };
204 
findAndCreateEffect(const Uuid & type)205 void AudioEffectHidlTest::findAndCreateEffect(const Uuid& type) {
206     Uuid effectUuid;
207     findEffectInstance(type, &effectUuid);
208     Return<void> ret = effectsFactory->createEffect(
209         effectUuid, 1 /*session*/, 1 /*ioHandle*/,
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_F(AudioEffectHidlTest,Close)253 TEST_F(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_F(AudioEffectHidlTest,GetDescriptor)260 TEST_F(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_F(AudioEffectHidlTest,GetSetConfig)275 TEST_F(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_F(AudioEffectHidlTest,GetConfigReverse)294 TEST_F(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_F(AudioEffectHidlTest,GetSupportedAuxChannelsConfigs)300 TEST_F(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_F(AudioEffectHidlTest,GetAuxChannelsConfig)307 TEST_F(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_F(AudioEffectHidlTest,SetAuxChannelsConfig)313 TEST_F(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_F(AudioEffectHidlTest,Reset)352 TEST_F(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_F(AudioEffectHidlTest,DisableEnableDisable)377 TEST_F(AudioEffectHidlTest, DisableEnableDisable) {
378     description("Verify Disable -> Enable -> Disable sequence for an effect");
379     Return<Result> ret = effect->disable();
380     EXPECT_TRUE(ret.isOk());
381     EXPECT_EQ(Result::INVALID_ARGUMENTS, ret);
382     ret = effect->enable();
383     EXPECT_TRUE(ret.isOk());
384     EXPECT_EQ(Result::OK, ret);
385     ret = effect->disable();
386     EXPECT_TRUE(ret.isOk());
387     EXPECT_EQ(Result::OK, ret);
388 }
389 
TEST_F(AudioEffectHidlTest,SetDevice)390 TEST_F(AudioEffectHidlTest, SetDevice) {
391     description("Verify that SetDevice works for an output chain effect");
392     Return<Result> ret = effect->setDevice(mkEnumBitfield(AudioDevice::OUT_SPEAKER));
393     EXPECT_TRUE(ret.isOk());
394     EXPECT_EQ(Result::OK, ret);
395 }
396 
TEST_F(AudioEffectHidlTest,SetAndGetVolume)397 TEST_F(AudioEffectHidlTest, SetAndGetVolume) {
398     description("Verify that SetAndGetVolume method works for an effect");
399     uint32_t channelCount;
400     getChannelCount(&channelCount);
401     hidl_vec<uint32_t> volumes;
402     volumes.resize(channelCount);
403     for (uint32_t i = 0; i < channelCount; ++i) {
404         volumes[i] = 0;
405     }
406     Result retval = Result::NOT_INITIALIZED;
407     Return<void> ret =
408         effect->setAndGetVolume(volumes, [&](Result r, const hidl_vec<uint32_t>&) { retval = r; });
409     EXPECT_TRUE(ret.isOk());
410     EXPECT_EQ(Result::OK, retval);
411 }
412 
TEST_F(AudioEffectHidlTest,VolumeChangeNotification)413 TEST_F(AudioEffectHidlTest, VolumeChangeNotification) {
414     description("Verify that effect accepts VolumeChangeNotification");
415     uint32_t channelCount;
416     getChannelCount(&channelCount);
417     hidl_vec<uint32_t> volumes;
418     volumes.resize(channelCount);
419     for (uint32_t i = 0; i < channelCount; ++i) {
420         volumes[i] = 0;
421     }
422     Return<Result> ret = effect->volumeChangeNotification(volumes);
423     EXPECT_TRUE(ret.isOk());
424     EXPECT_EQ(Result::OK, ret);
425 }
426 
TEST_F(AudioEffectHidlTest,SetAudioMode)427 TEST_F(AudioEffectHidlTest, SetAudioMode) {
428     description("Verify that SetAudioMode works for an effect");
429     Return<Result> ret = effect->setAudioMode(AudioMode::NORMAL);
430     EXPECT_TRUE(ret.isOk());
431     EXPECT_EQ(Result::OK, ret);
432 }
433 
TEST_F(AudioEffectHidlTest,SetConfigReverse)434 TEST_F(AudioEffectHidlTest, SetConfigReverse) {
435     description("Verify that SetConfigReverse does not crash");
436     Return<Result> ret = effect->setConfigReverse(EffectConfig(), nullptr, nullptr);
437     EXPECT_TRUE(ret.isOk());
438 }
439 
TEST_F(AudioEffectHidlTest,SetInputDevice)440 TEST_F(AudioEffectHidlTest, SetInputDevice) {
441     description("Verify that SetInputDevice does not crash");
442     Return<Result> ret = effect->setInputDevice(mkEnumBitfield(AudioDevice::IN_BUILTIN_MIC));
443     EXPECT_TRUE(ret.isOk());
444 }
445 
TEST_F(AudioEffectHidlTest,SetAudioSource)446 TEST_F(AudioEffectHidlTest, SetAudioSource) {
447     description("Verify that SetAudioSource does not crash");
448     Return<Result> ret = effect->setAudioSource(AudioSource::MIC);
449     EXPECT_TRUE(ret.isOk());
450 }
451 
TEST_F(AudioEffectHidlTest,Offload)452 TEST_F(AudioEffectHidlTest, Offload) {
453     description("Verify that calling Offload method does not crash");
454     EffectOffloadParameter offloadParam;
455     offloadParam.isOffload = false;
456     offloadParam.ioHandle = static_cast<int>(AudioHandleConsts::AUDIO_IO_HANDLE_NONE);
457     Return<Result> ret = effect->offload(offloadParam);
458     EXPECT_TRUE(ret.isOk());
459 }
460 
TEST_F(AudioEffectHidlTest,PrepareForProcessing)461 TEST_F(AudioEffectHidlTest, PrepareForProcessing) {
462     description("Verify that PrepareForProcessing method works for an effect");
463     Result retval = Result::NOT_INITIALIZED;
464     Return<void> ret = effect->prepareForProcessing(
465         [&](Result r, const MQDescriptorSync<Result>&) { retval = r; });
466     EXPECT_TRUE(ret.isOk());
467     EXPECT_EQ(Result::OK, retval);
468 }
469 
TEST_F(AudioEffectHidlTest,SetProcessBuffers)470 TEST_F(AudioEffectHidlTest, SetProcessBuffers) {
471     description("Verify that SetProcessBuffers works for an effect");
472     sp<IAllocator> ashmem = IAllocator::getService("ashmem");
473     ASSERT_NE(nullptr, ashmem.get());
474     bool success = false;
475     AudioBuffer buffer;
476     Return<void> ret = ashmem->allocate(1024, [&](bool s, const hidl_memory& memory) {
477         success = s;
478         if (s) {
479             buffer.data = memory;
480         }
481     });
482     ASSERT_TRUE(ret.isOk());
483     ASSERT_TRUE(success);
484     Return<Result> ret2 = effect->setProcessBuffers(buffer, buffer);
485     EXPECT_TRUE(ret2.isOk());
486     EXPECT_EQ(Result::OK, ret2);
487 }
488 
TEST_F(AudioEffectHidlTest,Command)489 TEST_F(AudioEffectHidlTest, Command) {
490     description("Verify that Command does not crash");
491     Return<void> ret =
492         effect->command(0, hidl_vec<uint8_t>(), 0, [&](int32_t, const hidl_vec<uint8_t>&) {});
493     EXPECT_TRUE(ret.isOk());
494 }
495 
TEST_F(AudioEffectHidlTest,SetParameter)496 TEST_F(AudioEffectHidlTest, SetParameter) {
497     description("Verify that SetParameter does not crash");
498     Return<Result> ret = effect->setParameter(hidl_vec<uint8_t>(), hidl_vec<uint8_t>());
499     EXPECT_TRUE(ret.isOk());
500 }
501 
TEST_F(AudioEffectHidlTest,GetParameter)502 TEST_F(AudioEffectHidlTest, GetParameter) {
503     description("Verify that GetParameter does not crash");
504     Return<void> ret =
505         effect->getParameter(hidl_vec<uint8_t>(), 0, [&](Result, const hidl_vec<uint8_t>&) {});
506     EXPECT_TRUE(ret.isOk());
507 }
508 
TEST_F(AudioEffectHidlTest,GetSupportedConfigsForFeature)509 TEST_F(AudioEffectHidlTest, GetSupportedConfigsForFeature) {
510     description("Verify that GetSupportedConfigsForFeature does not crash");
511     Return<void> ret = effect->getSupportedConfigsForFeature(
512         0, 0, 0, [&](Result, uint32_t, const hidl_vec<uint8_t>&) {});
513     EXPECT_TRUE(ret.isOk());
514 }
515 
TEST_F(AudioEffectHidlTest,GetCurrentConfigForFeature)516 TEST_F(AudioEffectHidlTest, GetCurrentConfigForFeature) {
517     description("Verify that GetCurrentConfigForFeature does not crash");
518     Return<void> ret =
519         effect->getCurrentConfigForFeature(0, 0, [&](Result, const hidl_vec<uint8_t>&) {});
520     EXPECT_TRUE(ret.isOk());
521 }
522 
TEST_F(AudioEffectHidlTest,SetCurrentConfigForFeature)523 TEST_F(AudioEffectHidlTest, SetCurrentConfigForFeature) {
524     description("Verify that SetCurrentConfigForFeature does not crash");
525     Return<Result> ret = effect->setCurrentConfigForFeature(0, hidl_vec<uint8_t>());
526     EXPECT_TRUE(ret.isOk());
527 }
528 
529 // The main test class for Equalizer Audio Effect HIDL HAL.
530 class EqualizerAudioEffectHidlTest : public AudioEffectHidlTest {
531    public:
SetUp()532     void SetUp() override {
533         AudioEffectHidlTest::SetUp();
534         equalizer = IEqualizerEffect::castFrom(effect);
535         ASSERT_NE(nullptr, equalizer.get());
536     }
537 
538    protected:
getEffectType()539     Uuid getEffectType() override { return EQUALIZER_EFFECT_TYPE; }
540     void getNumBands(uint16_t* numBands);
541     void getLevelRange(int16_t* minLevel, int16_t* maxLevel);
542     void getBandFrequencyRange(uint16_t band, uint32_t* minFreq, uint32_t* centerFreq,
543                                uint32_t* maxFreq);
544     void getPresetCount(size_t* count);
545 
546     sp<IEqualizerEffect> equalizer;
547 };
548 
getNumBands(uint16_t * numBands)549 void EqualizerAudioEffectHidlTest::getNumBands(uint16_t* numBands) {
550     Result retval = Result::NOT_INITIALIZED;
551     Return<void> ret = equalizer->getNumBands([&](Result r, uint16_t b) {
552         retval = r;
553         if (retval == Result::OK) {
554             *numBands = b;
555         }
556     });
557     ASSERT_TRUE(ret.isOk());
558     ASSERT_EQ(Result::OK, retval);
559 }
560 
getLevelRange(int16_t * minLevel,int16_t * maxLevel)561 void EqualizerAudioEffectHidlTest::getLevelRange(int16_t* minLevel, int16_t* maxLevel) {
562     Result retval = Result::NOT_INITIALIZED;
563     Return<void> ret = equalizer->getLevelRange([&](Result r, int16_t min, int16_t max) {
564         retval = r;
565         if (retval == Result::OK) {
566             *minLevel = min;
567             *maxLevel = max;
568         }
569     });
570     ASSERT_TRUE(ret.isOk());
571     ASSERT_EQ(Result::OK, retval);
572 }
573 
getBandFrequencyRange(uint16_t band,uint32_t * minFreq,uint32_t * centerFreq,uint32_t * maxFreq)574 void EqualizerAudioEffectHidlTest::getBandFrequencyRange(uint16_t band, uint32_t* minFreq,
575                                                          uint32_t* centerFreq, uint32_t* maxFreq) {
576     Result retval = Result::NOT_INITIALIZED;
577     Return<void> ret =
578         equalizer->getBandFrequencyRange(band, [&](Result r, uint32_t min, uint32_t max) {
579             retval = r;
580             if (retval == Result::OK) {
581                 *minFreq = min;
582                 *maxFreq = max;
583             }
584         });
585     ASSERT_TRUE(ret.isOk());
586     ASSERT_EQ(Result::OK, retval);
587     ret = equalizer->getBandCenterFrequency(band, [&](Result r, uint32_t center) {
588         retval = r;
589         if (retval == Result::OK) {
590             *centerFreq = center;
591         }
592     });
593     ASSERT_TRUE(ret.isOk());
594     ASSERT_EQ(Result::OK, retval);
595 }
596 
getPresetCount(size_t * count)597 void EqualizerAudioEffectHidlTest::getPresetCount(size_t* count) {
598     Result retval = Result::NOT_INITIALIZED;
599     Return<void> ret = equalizer->getPresetNames([&](Result r, const hidl_vec<hidl_string>& names) {
600         retval = r;
601         if (retval == Result::OK) {
602             *count = names.size();
603         }
604     });
605     ASSERT_TRUE(ret.isOk());
606     ASSERT_EQ(Result::OK, retval);
607 }
608 
TEST_F(EqualizerAudioEffectHidlTest,GetNumBands)609 TEST_F(EqualizerAudioEffectHidlTest, GetNumBands) {
610     description("Verify that Equalizer effect reports at least one band");
611     uint16_t numBands = 0;
612     getNumBands(&numBands);
613     EXPECT_GT(numBands, 0);
614 }
615 
TEST_F(EqualizerAudioEffectHidlTest,GetLevelRange)616 TEST_F(EqualizerAudioEffectHidlTest, GetLevelRange) {
617     description("Verify that Equalizer effect reports adequate band level range");
618     int16_t minLevel = 0x7fff, maxLevel = 0;
619     getLevelRange(&minLevel, &maxLevel);
620     EXPECT_GT(maxLevel, minLevel);
621 }
622 
TEST_F(EqualizerAudioEffectHidlTest,GetSetBandLevel)623 TEST_F(EqualizerAudioEffectHidlTest, GetSetBandLevel) {
624     description("Verify that manipulating band levels works for Equalizer effect");
625     uint16_t numBands = 0;
626     getNumBands(&numBands);
627     ASSERT_GT(numBands, 0);
628     int16_t levels[3]{0x7fff, 0, 0};
629     getLevelRange(&levels[0], &levels[2]);
630     ASSERT_GT(levels[2], levels[0]);
631     levels[1] = (levels[2] + levels[0]) / 2;
632     for (uint16_t i = 0; i < numBands; ++i) {
633         for (size_t j = 0; j < ARRAY_SIZE(levels); ++j) {
634             Return<Result> ret = equalizer->setBandLevel(i, levels[j]);
635             EXPECT_TRUE(ret.isOk());
636             EXPECT_EQ(Result::OK, ret);
637             Result retval = Result::NOT_INITIALIZED;
638             int16_t actualLevel;
639             Return<void> ret2 = equalizer->getBandLevel(i, [&](Result r, int16_t l) {
640                 retval = r;
641                 if (retval == Result::OK) {
642                     actualLevel = l;
643                 }
644             });
645             EXPECT_TRUE(ret2.isOk());
646             EXPECT_EQ(Result::OK, retval);
647             EXPECT_EQ(levels[j], actualLevel);
648         }
649     }
650 }
651 
TEST_F(EqualizerAudioEffectHidlTest,GetBandCenterFrequencyAndRange)652 TEST_F(EqualizerAudioEffectHidlTest, GetBandCenterFrequencyAndRange) {
653     description("Verify that Equalizer effect reports adequate band frequency range");
654     uint16_t numBands = 0;
655     getNumBands(&numBands);
656     ASSERT_GT(numBands, 0);
657     for (uint16_t i = 0; i < numBands; ++i) {
658         uint32_t minFreq = 0xffffffff, centerFreq = 0xffffffff, maxFreq = 0xffffffff;
659         getBandFrequencyRange(i, &minFreq, &centerFreq, &maxFreq);
660         // Note: NXP legacy implementation reports "1" as upper bound for last band,
661         // so this check fails.
662         EXPECT_GE(maxFreq, centerFreq);
663         EXPECT_GE(centerFreq, minFreq);
664     }
665 }
666 
TEST_F(EqualizerAudioEffectHidlTest,GetBandForFrequency)667 TEST_F(EqualizerAudioEffectHidlTest, GetBandForFrequency) {
668     description("Verify that Equalizer effect supports GetBandForFrequency correctly");
669     uint16_t numBands = 0;
670     getNumBands(&numBands);
671     ASSERT_GT(numBands, 0);
672     for (uint16_t i = 0; i < numBands; ++i) {
673         uint32_t freqs[3]{0, 0, 0};
674         getBandFrequencyRange(i, &freqs[0], &freqs[1], &freqs[2]);
675         // NXP legacy implementation reports "1" as upper bound for last band, some
676         // of the checks fail.
677         for (size_t j = 0; j < ARRAY_SIZE(freqs); ++j) {
678             if (j == 0) {
679                 freqs[j]++;
680             }  // Min frequency is an open interval.
681             Result retval = Result::NOT_INITIALIZED;
682             uint16_t actualBand = numBands + 1;
683             Return<void> ret = equalizer->getBandForFrequency(freqs[j], [&](Result r, uint16_t b) {
684                 retval = r;
685                 if (retval == Result::OK) {
686                     actualBand = b;
687                 }
688             });
689             EXPECT_TRUE(ret.isOk());
690             EXPECT_EQ(Result::OK, retval);
691             EXPECT_EQ(i, actualBand) << "Frequency: " << freqs[j];
692         }
693     }
694 }
695 
TEST_F(EqualizerAudioEffectHidlTest,GetPresetNames)696 TEST_F(EqualizerAudioEffectHidlTest, GetPresetNames) {
697     description("Verify that Equalizer effect reports at least one preset");
698     size_t presetCount;
699     getPresetCount(&presetCount);
700     EXPECT_GT(presetCount, 0u);
701 }
702 
TEST_F(EqualizerAudioEffectHidlTest,GetSetCurrentPreset)703 TEST_F(EqualizerAudioEffectHidlTest, GetSetCurrentPreset) {
704     description("Verify that manipulating the current preset for Equalizer effect");
705     size_t presetCount;
706     getPresetCount(&presetCount);
707     ASSERT_GT(presetCount, 0u);
708     for (uint16_t i = 0; i < presetCount; ++i) {
709         Return<Result> ret = equalizer->setCurrentPreset(i);
710         EXPECT_TRUE(ret.isOk());
711         EXPECT_EQ(Result::OK, ret);
712         Result retval = Result::NOT_INITIALIZED;
713         uint16_t actualPreset = 0xffff;
714         Return<void> ret2 = equalizer->getCurrentPreset([&](Result r, uint16_t p) {
715             retval = r;
716             if (retval == Result::OK) {
717                 actualPreset = p;
718             }
719         });
720         EXPECT_TRUE(ret2.isOk());
721         EXPECT_EQ(Result::OK, retval);
722         EXPECT_EQ(i, actualPreset);
723     }
724 }
725 
TEST_F(EqualizerAudioEffectHidlTest,GetSetAllProperties)726 TEST_F(EqualizerAudioEffectHidlTest, GetSetAllProperties) {
727     description(
728         "Verify that setting band levels and presets works via Get / "
729         "SetAllProperties for Equalizer effect");
730     using AllProperties =
731         ::android::hardware::audio::effect::CPP_VERSION::IEqualizerEffect::AllProperties;
732     uint16_t numBands = 0;
733     getNumBands(&numBands);
734     ASSERT_GT(numBands, 0);
735     AllProperties props;
736     props.bandLevels.resize(numBands);
737     for (size_t i = 0; i < numBands; ++i) {
738         props.bandLevels[i] = 0;
739     }
740 
741     AllProperties actualProps;
742     Result retval = Result::NOT_INITIALIZED;
743 
744     // Verify setting of the band levels via properties.
745     props.curPreset = -1;
746     Return<Result> ret = equalizer->setAllProperties(props);
747     EXPECT_TRUE(ret.isOk());
748     EXPECT_EQ(Result::OK, ret);
749     Return<void> ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
750         retval = r;
751         if (retval == Result::OK) {
752             actualProps = p;
753         }
754     });
755     EXPECT_TRUE(ret2.isOk());
756     EXPECT_EQ(Result::OK, retval);
757     EXPECT_EQ(props.bandLevels, actualProps.bandLevels);
758 
759     // Verify setting of the current preset via properties.
760     props.curPreset = 0;  // Assuming there is at least one preset.
761     ret = equalizer->setAllProperties(props);
762     EXPECT_TRUE(ret.isOk());
763     EXPECT_EQ(Result::OK, ret);
764     ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) {
765         retval = r;
766         if (retval == Result::OK) {
767             actualProps = p;
768         }
769     });
770     EXPECT_TRUE(ret2.isOk());
771     EXPECT_EQ(Result::OK, retval);
772     EXPECT_EQ(props.curPreset, actualProps.curPreset);
773 }
774 
775 // The main test class for Equalizer Audio Effect HIDL HAL.
776 class LoudnessEnhancerAudioEffectHidlTest : public AudioEffectHidlTest {
777    public:
SetUp()778     void SetUp() override {
779         AudioEffectHidlTest::SetUp();
780         enhancer = ILoudnessEnhancerEffect::castFrom(effect);
781         ASSERT_NE(nullptr, enhancer.get());
782     }
783 
784    protected:
getEffectType()785     Uuid getEffectType() override { return LOUDNESS_ENHANCER_EFFECT_TYPE; }
786 
787     sp<ILoudnessEnhancerEffect> enhancer;
788 };
789 
TEST_F(LoudnessEnhancerAudioEffectHidlTest,GetSetTargetGain)790 TEST_F(LoudnessEnhancerAudioEffectHidlTest, GetSetTargetGain) {
791     description(
792         "Verify that manipulating the target gain works for Loudness Enhancer "
793         "effect");
794     const int32_t gain = 100;
795     Return<Result> ret = enhancer->setTargetGain(gain);
796     EXPECT_TRUE(ret.isOk());
797     EXPECT_EQ(Result::OK, ret);
798     int32_t actualGain = 0;
799     Result retval;
800     Return<void> ret2 = enhancer->getTargetGain([&](Result r, int32_t g) {
801         retval = r;
802         if (retval == Result::OK) {
803             actualGain = g;
804         }
805     });
806     EXPECT_TRUE(ret2.isOk());
807     EXPECT_EQ(Result::OK, retval);
808     EXPECT_EQ(gain, actualGain);
809 }
810 
main(int argc,char ** argv)811 int main(int argc, char** argv) {
812     ::testing::AddGlobalTestEnvironment(AudioEffectsFactoryHidlEnvironment::Instance());
813     ::testing::InitGoogleTest(&argc, argv);
814     AudioEffectsFactoryHidlEnvironment::Instance()->init(&argc, argv);
815     int status = RUN_ALL_TESTS();
816     LOG(INFO) << "Test result = " << status;
817     return status;
818 }
819