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