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