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> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #define LOG_TAG "VtsHalAudioEffectFactory" |
| 22 | |
| 23 | #include <aidl/Gtest.h> |
| 24 | #include <aidl/Vintf.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | #include <android/binder_interface_utils.h> |
| 28 | #include <android/binder_manager.h> |
| 29 | #include <android/binder_process.h> |
| 30 | |
| 31 | #include <aidl/android/hardware/audio/effect/IFactory.h> |
| 32 | |
| 33 | #include "AudioHalBinderServiceUtil.h" |
| 34 | #include "EffectFactoryHelper.h" |
| 35 | #include "TestUtils.h" |
| 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 40 | using aidl::android::hardware::audio::effect::EffectNullUuid; |
| 41 | using aidl::android::hardware::audio::effect::EffectZeroUuid; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 42 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 43 | using aidl::android::hardware::audio::effect::Processing; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 44 | using aidl::android::media::audio::common::AudioUuid; |
| 45 | |
| 46 | /// Effect factory testing. |
| 47 | class EffectFactoryTest : public testing::TestWithParam<std::string> { |
| 48 | public: |
| 49 | void SetUp() override { ASSERT_NO_FATAL_FAILURE(mFactory.ConnectToFactoryService()); } |
| 50 | |
| 51 | void TearDown() override { mFactory.DestroyEffects(); } |
| 52 | |
| 53 | EffectFactoryHelper mFactory = EffectFactoryHelper(GetParam()); |
| 54 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 55 | const Descriptor::Identity nullDesc = {.uuid = EffectNullUuid}; |
| 56 | const Descriptor::Identity zeroDesc = {.uuid = EffectZeroUuid}; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST_P(EffectFactoryTest, SetupAndTearDown) { |
| 60 | // Intentionally empty test body. |
| 61 | } |
| 62 | |
| 63 | TEST_P(EffectFactoryTest, CanBeRestarted) { |
| 64 | ASSERT_NO_FATAL_FAILURE(mFactory.RestartFactoryService()); |
| 65 | } |
| 66 | |
| 67 | TEST_P(EffectFactoryTest, QueriedDescriptorList) { |
| 68 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 69 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 70 | EXPECT_NE(descriptors.size(), 0UL); |
| 71 | } |
| 72 | |
| 73 | TEST_P(EffectFactoryTest, DescriptorUUIDNotNull) { |
| 74 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 75 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 76 | // TODO: Factory eventually need to return the full list of MUST supported AOSP effects. |
| 77 | for (auto& desc : descriptors) { |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 78 | EXPECT_NE(desc.type, EffectNullUuid); |
| 79 | EXPECT_NE(desc.uuid, EffectNullUuid); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | TEST_P(EffectFactoryTest, QueriedDescriptorNotExistType) { |
| 84 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 85 | mFactory.QueryEffects(EffectNullUuid, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 86 | EXPECT_EQ(descriptors.size(), 0UL); |
| 87 | } |
| 88 | |
| 89 | TEST_P(EffectFactoryTest, QueriedDescriptorNotExistInstance) { |
| 90 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 91 | mFactory.QueryEffects(std::nullopt, EffectNullUuid, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 92 | EXPECT_EQ(descriptors.size(), 0UL); |
| 93 | } |
| 94 | |
| 95 | TEST_P(EffectFactoryTest, CreateAndDestroyOnce) { |
| 96 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 97 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 98 | auto numIds = mFactory.GetEffectIds().size(); |
| 99 | EXPECT_NE(numIds, 0UL); |
| 100 | |
| 101 | auto& effectMap = mFactory.GetEffectMap(); |
| 102 | EXPECT_EQ(effectMap.size(), 0UL); |
| 103 | mFactory.CreateEffects(); |
| 104 | EXPECT_EQ(effectMap.size(), numIds); |
| 105 | mFactory.DestroyEffects(); |
| 106 | EXPECT_EQ(effectMap.size(), 0UL); |
| 107 | } |
| 108 | |
| 109 | TEST_P(EffectFactoryTest, CreateAndDestroyRepeat) { |
| 110 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 111 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 112 | auto numIds = mFactory.GetEffectIds().size(); |
| 113 | EXPECT_NE(numIds, 0UL); |
| 114 | |
| 115 | auto& effectMap = mFactory.GetEffectMap(); |
| 116 | EXPECT_EQ(effectMap.size(), 0UL); |
| 117 | mFactory.CreateEffects(); |
| 118 | EXPECT_EQ(effectMap.size(), numIds); |
| 119 | mFactory.DestroyEffects(); |
| 120 | EXPECT_EQ(effectMap.size(), 0UL); |
| 121 | |
| 122 | // Create and destroy again |
| 123 | mFactory.CreateEffects(); |
| 124 | EXPECT_EQ(effectMap.size(), numIds); |
| 125 | mFactory.DestroyEffects(); |
| 126 | EXPECT_EQ(effectMap.size(), 0UL); |
| 127 | } |
| 128 | |
| 129 | TEST_P(EffectFactoryTest, CreateMultipleInstanceOfSameEffect) { |
| 130 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 131 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 132 | auto numIds = mFactory.GetEffectIds().size(); |
| 133 | EXPECT_NE(numIds, 0UL); |
| 134 | |
| 135 | auto& effectMap = mFactory.GetEffectMap(); |
| 136 | EXPECT_EQ(effectMap.size(), 0UL); |
| 137 | mFactory.CreateEffects(); |
| 138 | EXPECT_EQ(effectMap.size(), numIds); |
| 139 | // Create effect instances of same implementation |
| 140 | mFactory.CreateEffects(); |
| 141 | EXPECT_EQ(effectMap.size(), 2 * numIds); |
| 142 | |
| 143 | mFactory.CreateEffects(); |
| 144 | EXPECT_EQ(effectMap.size(), 3 * numIds); |
| 145 | |
| 146 | mFactory.DestroyEffects(); |
| 147 | EXPECT_EQ(effectMap.size(), 0UL); |
| 148 | } |
| 149 | |
| 150 | // Expect EX_ILLEGAL_ARGUMENT when create with invalid UUID. |
| 151 | TEST_P(EffectFactoryTest, CreateWithInvalidUuid) { |
| 152 | std::vector<std::pair<Descriptor::Identity, binder_status_t>> descriptors; |
| 153 | descriptors.push_back(std::make_pair(nullDesc, EX_ILLEGAL_ARGUMENT)); |
| 154 | descriptors.push_back(std::make_pair(zeroDesc, EX_ILLEGAL_ARGUMENT)); |
| 155 | |
| 156 | auto& effectMap = mFactory.GetEffectMap(); |
| 157 | mFactory.CreateEffectsAndExpect(descriptors); |
| 158 | EXPECT_EQ(effectMap.size(), 0UL); |
| 159 | } |
| 160 | |
| 161 | // Expect EX_ILLEGAL_ARGUMENT when destroy null interface. |
| 162 | TEST_P(EffectFactoryTest, DestroyWithInvalidInterface) { |
| 163 | std::shared_ptr<IEffect> spDummyEffect(nullptr); |
| 164 | |
| 165 | mFactory.DestroyEffectAndExpect(spDummyEffect, EX_ILLEGAL_ARGUMENT); |
| 166 | } |
| 167 | |
| 168 | TEST_P(EffectFactoryTest, CreateAndRemoveReference) { |
| 169 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 170 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 171 | auto numIds = mFactory.GetEffectIds().size(); |
| 172 | EXPECT_NE(numIds, 0UL); |
| 173 | |
| 174 | auto& effectMap = mFactory.GetEffectMap(); |
| 175 | EXPECT_EQ(effectMap.size(), 0UL); |
| 176 | mFactory.CreateEffects(); |
| 177 | EXPECT_EQ(effectMap.size(), numIds); |
| 178 | // remove all reference |
| 179 | mFactory.ClearEffectMap(); |
| 180 | EXPECT_EQ(effectMap.size(), 0UL); |
| 181 | } |
| 182 | |
| 183 | TEST_P(EffectFactoryTest, CreateRemoveReferenceAndCreateDestroy) { |
| 184 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 185 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 186 | auto numIds = mFactory.GetEffectIds().size(); |
| 187 | EXPECT_NE(numIds, 0UL); |
| 188 | |
| 189 | auto& effectMap = mFactory.GetEffectMap(); |
| 190 | EXPECT_EQ(effectMap.size(), 0UL); |
| 191 | mFactory.CreateEffects(); |
| 192 | EXPECT_EQ(effectMap.size(), numIds); |
| 193 | // remove all reference |
| 194 | mFactory.ClearEffectMap(); |
| 195 | EXPECT_EQ(effectMap.size(), 0UL); |
| 196 | |
| 197 | // Create and destroy again |
| 198 | mFactory.CreateEffects(); |
| 199 | EXPECT_EQ(effectMap.size(), numIds); |
| 200 | mFactory.DestroyEffects(); |
| 201 | EXPECT_EQ(effectMap.size(), 0UL); |
| 202 | } |
| 203 | |
| 204 | TEST_P(EffectFactoryTest, CreateRestartAndCreateDestroy) { |
| 205 | std::vector<Descriptor::Identity> descriptors; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 206 | mFactory.QueryEffects(std::nullopt, std::nullopt, std::nullopt, &descriptors); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 207 | auto numIds = mFactory.GetEffectIds().size(); |
| 208 | auto& effectMap = mFactory.GetEffectMap(); |
| 209 | mFactory.CreateEffects(); |
| 210 | EXPECT_EQ(effectMap.size(), numIds); |
| 211 | ASSERT_NO_FATAL_FAILURE(mFactory.RestartFactoryService()); |
| 212 | |
| 213 | mFactory.CreateEffects(); |
| 214 | EXPECT_EQ(effectMap.size(), numIds); |
| 215 | mFactory.DestroyEffects(); |
| 216 | EXPECT_EQ(effectMap.size(), 0UL); |
| 217 | } |
| 218 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 219 | TEST_P(EffectFactoryTest, QueryProcess) { |
| 220 | std::vector<Processing> processing; |
| 221 | mFactory.QueryProcessing(std::nullopt, &processing); |
| 222 | // TODO: verify the number of process in example implementation after audio_effects.xml migrated |
| 223 | } |
| 224 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 225 | INSTANTIATE_TEST_SUITE_P(EffectFactoryTest, EffectFactoryTest, |
| 226 | testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)), |
| 227 | android::PrintInstanceNameToString); |
| 228 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EffectFactoryTest); |
| 229 | |
| 230 | int main(int argc, char** argv) { |
| 231 | ::testing::InitGoogleTest(&argc, argv); |
| 232 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 233 | ABinderProcess_startThreadPool(); |
| 234 | return RUN_ALL_TESTS(); |
| 235 | } |