Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <memory> |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 18 | #include <set> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #define LOG_TAG "VtsHalAudioEffectFactory" |
| 23 | |
| 24 | #include <aidl/Gtest.h> |
| 25 | #include <aidl/Vintf.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/properties.h> |
| 28 | #include <android/binder_interface_utils.h> |
| 29 | #include <android/binder_manager.h> |
| 30 | #include <android/binder_process.h> |
| 31 | |
| 32 | #include <aidl/android/hardware/audio/effect/IFactory.h> |
| 33 | |
| 34 | #include "AudioHalBinderServiceUtil.h" |
| 35 | #include "EffectFactoryHelper.h" |
| 36 | #include "TestUtils.h" |
| 37 | |
| 38 | using namespace android; |
| 39 | |
| 40 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 41 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 42 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 43 | using aidl::android::hardware::audio::effect::kEffectNullUuid; |
| 44 | using aidl::android::hardware::audio::effect::kEffectZeroUuid; |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 45 | using aidl::android::hardware::audio::effect::Processing; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 46 | using aidl::android::media::audio::common::AudioSource; |
| 47 | using aidl::android::media::audio::common::AudioStreamType; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 48 | using aidl::android::media::audio::common::AudioUuid; |
| 49 | |
| 50 | /// Effect factory testing. |
| 51 | class EffectFactoryTest : public testing::TestWithParam<std::string> { |
| 52 | public: |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 53 | void SetUp() override { |
| 54 | mFactoryHelper = std::make_unique<EffectFactoryHelper>(GetParam()); |
| 55 | connectAndGetFactory(); |
| 56 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 57 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 58 | void TearDown() override { |
| 59 | for (auto& effect : mEffects) { |
| 60 | const auto status = mEffectFactory->destroyEffect(effect); |
| 61 | EXPECT_STATUS(EX_NONE, status); |
| 62 | } |
| 63 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 64 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 65 | std::unique_ptr<EffectFactoryHelper> mFactoryHelper; |
| 66 | std::shared_ptr<IFactory> mEffectFactory; |
| 67 | std::vector<std::shared_ptr<IEffect>> mEffects; |
| 68 | const Descriptor::Identity kNullDesc = {.uuid = kEffectNullUuid}; |
| 69 | const Descriptor::Identity kZeroDesc = {.uuid = kEffectZeroUuid}; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 70 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 71 | template <typename Functor> |
| 72 | void ForEachId(const std::vector<Descriptor::Identity> ids, Functor functor) { |
| 73 | for (const auto& id : ids) { |
| 74 | SCOPED_TRACE(id.toString()); |
| 75 | functor(id); |
| 76 | } |
| 77 | } |
| 78 | template <typename Functor> |
| 79 | void ForEachEffect(std::vector<std::shared_ptr<IEffect>> effects, Functor functor) { |
| 80 | for (auto& effect : effects) { |
| 81 | functor(effect); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | std::vector<std::shared_ptr<IEffect>> createWithIds( |
| 86 | const std::vector<Descriptor::Identity> ids, |
| 87 | const binder_status_t expectStatus = EX_NONE) { |
| 88 | std::vector<std::shared_ptr<IEffect>> effects; |
| 89 | for (const auto& id : ids) { |
| 90 | std::shared_ptr<IEffect> effect; |
| 91 | EXPECT_STATUS(expectStatus, mEffectFactory->createEffect(id.uuid, &effect)); |
| 92 | if (expectStatus == EX_NONE) { |
| 93 | EXPECT_NE(effect, nullptr) << " null effect with uuid: " << id.uuid.toString(); |
| 94 | effects.push_back(std::move(effect)); |
| 95 | } |
| 96 | } |
| 97 | return effects; |
| 98 | } |
| 99 | void destroyEffects(std::vector<std::shared_ptr<IEffect>> effects, |
| 100 | const binder_status_t expectStatus = EX_NONE) { |
| 101 | for (const auto& effect : effects) { |
| 102 | EXPECT_STATUS(expectStatus, mEffectFactory->destroyEffect(effect)); |
| 103 | } |
| 104 | } |
| 105 | void creatAndDestroyIds(const std::vector<Descriptor::Identity> ids) { |
| 106 | for (const auto& id : ids) { |
| 107 | auto effects = createWithIds({id}); |
| 108 | ASSERT_NO_FATAL_FAILURE(destroyEffects(effects)); |
| 109 | } |
| 110 | } |
| 111 | void connectAndGetFactory() { |
| 112 | ASSERT_NO_FATAL_FAILURE(mFactoryHelper->ConnectToFactoryService()); |
| 113 | mEffectFactory = mFactoryHelper->GetFactory(); |
| 114 | ASSERT_NE(mEffectFactory, nullptr); |
| 115 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | TEST_P(EffectFactoryTest, SetupAndTearDown) { |
| 119 | // Intentionally empty test body. |
| 120 | } |
| 121 | |
| 122 | TEST_P(EffectFactoryTest, CanBeRestarted) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 123 | ASSERT_NO_FATAL_FAILURE(mFactoryHelper->RestartFactoryService()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 126 | /** |
| 127 | * @brief Check at least support list of effect must be supported by aosp: |
| 128 | * https://developer.android.com/reference/android/media/audiofx/AudioEffect |
| 129 | */ |
| 130 | TEST_P(EffectFactoryTest, ExpectAllAospEffectTypes) { |
| 131 | std::vector<Descriptor::Identity> ids; |
| 132 | std::set<AudioUuid> typeUuidSet( |
| 133 | {aidl::android::hardware::audio::effect::kBassBoostTypeUUID, |
| 134 | aidl::android::hardware::audio::effect::kEqualizerTypeUUID, |
| 135 | aidl::android::hardware::audio::effect::kEnvReverbTypeUUID, |
| 136 | aidl::android::hardware::audio::effect::kPresetReverbTypeUUID, |
| 137 | aidl::android::hardware::audio::effect::kDynamicsProcessingTypeUUID, |
| 138 | aidl::android::hardware::audio::effect::kHapticGeneratorTypeUUID, |
| 139 | aidl::android::hardware::audio::effect::kVirtualizerTypeUUID}); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 140 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 141 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 142 | EXPECT_TRUE(ids.size() >= typeUuidSet.size()); |
| 143 | for (const auto& id : ids) { |
| 144 | typeUuidSet.erase(id.type); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 145 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 146 | std::string msg = " missing type UUID:\n"; |
| 147 | for (const auto& uuid : typeUuidSet) { |
| 148 | msg += (uuid.toString() + "\n"); |
| 149 | } |
| 150 | SCOPED_TRACE(msg); |
| 151 | EXPECT_EQ(0UL, typeUuidSet.size()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 154 | TEST_P(EffectFactoryTest, QueryNullTypeUuid) { |
| 155 | std::vector<Descriptor::Identity> ids; |
| 156 | EXPECT_IS_OK(mEffectFactory->queryEffects(kEffectNullUuid, std::nullopt, std::nullopt, &ids)); |
| 157 | EXPECT_EQ(ids.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 160 | TEST_P(EffectFactoryTest, QueriedNullImplUuid) { |
| 161 | std::vector<Descriptor::Identity> ids; |
| 162 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, kEffectNullUuid, std::nullopt, &ids)); |
| 163 | EXPECT_EQ(ids.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 166 | TEST_P(EffectFactoryTest, QueriedNullProxyUuid) { |
| 167 | std::vector<Descriptor::Identity> ids; |
| 168 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, kEffectNullUuid, &ids)); |
| 169 | EXPECT_EQ(ids.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 172 | // create all effects, and then destroy them all together |
| 173 | TEST_P(EffectFactoryTest, CreateAndDestroyEffects) { |
| 174 | std::vector<Descriptor::Identity> ids; |
| 175 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 176 | EXPECT_NE(ids.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 177 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 178 | std::vector<std::shared_ptr<IEffect>> effects; |
| 179 | effects = createWithIds(ids); |
| 180 | EXPECT_EQ(ids.size(), effects.size()); |
| 181 | destroyEffects(effects); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_P(EffectFactoryTest, CreateMultipleInstanceOfSameEffect) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 185 | std::vector<Descriptor::Identity> ids; |
| 186 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 187 | EXPECT_NE(ids.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 188 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 189 | std::vector<std::shared_ptr<IEffect>> effects = createWithIds(ids); |
| 190 | EXPECT_EQ(ids.size(), effects.size()); |
| 191 | std::vector<std::shared_ptr<IEffect>> effects2 = createWithIds(ids); |
| 192 | EXPECT_EQ(ids.size(), effects2.size()); |
| 193 | std::vector<std::shared_ptr<IEffect>> effects3 = createWithIds(ids); |
| 194 | EXPECT_EQ(ids.size(), effects3.size()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 195 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 196 | destroyEffects(effects); |
| 197 | destroyEffects(effects2); |
| 198 | destroyEffects(effects3); |
| 199 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 200 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 201 | // create and destroy each effect one by one |
| 202 | TEST_P(EffectFactoryTest, CreateAndDestroyEffectsOneByOne) { |
| 203 | std::vector<Descriptor::Identity> ids; |
| 204 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 205 | EXPECT_NE(ids.size(), 0UL); |
| 206 | |
| 207 | creatAndDestroyIds(ids); |
| 208 | } |
| 209 | |
| 210 | // for each effect: repeat 3 times create and destroy |
| 211 | TEST_P(EffectFactoryTest, CreateAndDestroyRepeat) { |
| 212 | std::vector<Descriptor::Identity> ids; |
| 213 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 214 | EXPECT_NE(ids.size(), 0UL); |
| 215 | |
| 216 | creatAndDestroyIds(ids); |
| 217 | creatAndDestroyIds(ids); |
| 218 | creatAndDestroyIds(ids); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Expect EX_ILLEGAL_ARGUMENT when create with invalid UUID. |
| 222 | TEST_P(EffectFactoryTest, CreateWithInvalidUuid) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 223 | std::vector<Descriptor::Identity> ids = {kNullDesc, kZeroDesc}; |
| 224 | auto effects = createWithIds(ids, EX_ILLEGAL_ARGUMENT); |
| 225 | EXPECT_EQ(effects.size(), 0UL); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Expect EX_ILLEGAL_ARGUMENT when destroy null interface. |
| 229 | TEST_P(EffectFactoryTest, DestroyWithInvalidInterface) { |
| 230 | std::shared_ptr<IEffect> spDummyEffect(nullptr); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 231 | destroyEffects({spDummyEffect}, EX_ILLEGAL_ARGUMENT); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 234 | // Same descriptor ID should work after service restart. |
| 235 | TEST_P(EffectFactoryTest, CreateDestroyWithRestart) { |
| 236 | std::vector<Descriptor::Identity> ids; |
| 237 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 238 | EXPECT_NE(ids.size(), 0UL); |
| 239 | creatAndDestroyIds(ids); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 240 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 241 | mFactoryHelper->RestartFactoryService(); |
| 242 | |
| 243 | connectAndGetFactory(); |
| 244 | creatAndDestroyIds(ids); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 247 | // Effect handle invalid after restart. |
| 248 | TEST_P(EffectFactoryTest, EffectInvalidAfterRestart) { |
| 249 | std::vector<Descriptor::Identity> ids; |
| 250 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids)); |
| 251 | EXPECT_NE(ids.size(), 0UL); |
| 252 | std::vector<std::shared_ptr<IEffect>> effects = createWithIds(ids); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 253 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 254 | ASSERT_NO_FATAL_FAILURE(mFactoryHelper->RestartFactoryService()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 255 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 256 | connectAndGetFactory(); |
| 257 | destroyEffects(effects, EX_ILLEGAL_ARGUMENT); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 260 | // expect no error with the queryProcessing interface, but don't check number of processing |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 261 | TEST_P(EffectFactoryTest, QueryProcess) { |
| 262 | std::vector<Processing> processing; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame^] | 263 | EXPECT_IS_OK(mEffectFactory->queryProcessing(std::nullopt, &processing)); |
| 264 | |
| 265 | Processing::Type streamType = |
| 266 | Processing::Type::make<Processing::Type::streamType>(AudioStreamType::SYSTEM); |
| 267 | std::vector<Processing> processingFilteredByStream; |
| 268 | EXPECT_IS_OK(mEffectFactory->queryProcessing(streamType, &processingFilteredByStream)); |
| 269 | |
| 270 | Processing::Type source = |
| 271 | Processing::Type::make<Processing::Type::source>(AudioSource::DEFAULT); |
| 272 | std::vector<Processing> processingFilteredBySource; |
| 273 | EXPECT_IS_OK(mEffectFactory->queryProcessing(source, &processingFilteredBySource)); |
| 274 | |
| 275 | EXPECT_TRUE(processing.size() >= processingFilteredByStream.size()); |
| 276 | EXPECT_TRUE(processing.size() >= processingFilteredBySource.size()); |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 279 | INSTANTIATE_TEST_SUITE_P(EffectFactoryTest, EffectFactoryTest, |
| 280 | testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)), |
| 281 | android::PrintInstanceNameToString); |
| 282 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EffectFactoryTest); |
| 283 | |
| 284 | int main(int argc, char** argv) { |
| 285 | ::testing::InitGoogleTest(&argc, argv); |
| 286 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 287 | ABinderProcess_startThreadPool(); |
| 288 | return RUN_ALL_TESTS(); |
| 289 | } |