blob: cf94e580b4109b53b9382021c0c544e00fe842ee [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#pragma once
18
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24#include <android/binder_auto_utils.h>
25
26#include "TestUtils.h"
27
28using namespace android;
29
30using aidl::android::hardware::audio::effect::Descriptor;
31using aidl::android::hardware::audio::effect::IEffect;
32using aidl::android::hardware::audio::effect::IFactory;
33using aidl::android::hardware::audio::effect::Parameter;
Shunkai Yao08b687d2022-10-13 21:11:11 +000034using aidl::android::hardware::audio::effect::Processing;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000035using aidl::android::media::audio::common::AudioUuid;
36
37class EffectFactoryHelper {
38 public:
39 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {}
40
41 void ConnectToFactoryService() {
42 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName));
43 ASSERT_NE(mEffectFactory, nullptr);
44 }
45
46 void RestartFactoryService() {
47 ASSERT_NE(mEffectFactory, nullptr);
48 mEffectFactory = IFactory::fromBinder(binderUtil.restartService());
49 ASSERT_NE(mEffectFactory, nullptr);
50 ClearEffectMap();
51 }
52
53 void QueryEffects(const std::optional<AudioUuid>& in_type,
54 const std::optional<AudioUuid>& in_instance,
55 std::vector<Descriptor::Identity>* _aidl_return) {
56 ASSERT_NE(mEffectFactory, nullptr);
57 EXPECT_IS_OK(mEffectFactory->queryEffects(in_type, in_instance, _aidl_return));
58 mIds = *_aidl_return;
59 }
60
Shunkai Yao08b687d2022-10-13 21:11:11 +000061 void QueryProcessing(const std::optional<Processing::Type>& in_type,
62 std::vector<Processing>* _aidl_return) {
63 ASSERT_NE(mEffectFactory, nullptr);
64 EXPECT_IS_OK(mEffectFactory->queryProcessing(in_type, _aidl_return));
65 // only update the whole list if no filter applied
66 if (!in_type.has_value()) {
67 mProcesses = *_aidl_return;
68 }
69 }
70
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000071 void CreateEffects() {
72 ASSERT_NE(mEffectFactory, nullptr);
73 for (const auto& id : mIds) {
74 std::shared_ptr<IEffect> effect;
75 EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect));
76 EXPECT_NE(effect, nullptr) << id.toString();
77 if (effect) {
78 mEffectIdMap[effect] = id;
79 }
80 }
81 }
82
83 void CreateEffectsAndExpect(
84 const std::vector<std::pair<Descriptor::Identity, binder_exception_t>>& uuid_status) {
85 ASSERT_NE(mEffectFactory, nullptr);
86 for (const auto& it : uuid_status) {
87 std::shared_ptr<IEffect> effect;
88 auto status = mEffectFactory->createEffect(it.first.uuid, &effect);
89 EXPECT_STATUS(it.second, status);
90 if (effect) {
91 mEffectIdMap[effect] = it.first;
92 }
93 }
94 }
95
96 void DestroyEffectAndExpect(std::shared_ptr<IEffect>& instance, binder_exception_t exception) {
97 ASSERT_NE(mEffectFactory, nullptr);
98 auto status = mEffectFactory->destroyEffect(instance);
99 EXPECT_STATUS(exception, status);
100 }
101
102 void QueryAndCreateAllEffects() {
103 ASSERT_NE(mEffectFactory, nullptr);
104 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, &mCompleteIds));
105 for (const auto& id : mCompleteIds) {
106 std::shared_ptr<IEffect> effect;
107 EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect));
108 EXPECT_NE(effect, nullptr) << id.toString();
109 mEffectIdMap[effect] = id;
110 }
111 }
112
113 void DestroyEffects(const binder_exception_t expected = EX_NONE, const int remaining = 0) {
114 ASSERT_NE(mEffectFactory, nullptr);
115
116 for (auto it = mEffectIdMap.begin(); it != mEffectIdMap.end();) {
117 auto erased = it++;
118 auto status = mEffectFactory->destroyEffect(erased->first);
119 EXPECT_STATUS(expected, status);
120 if (status.isOk()) {
121 mEffectIdMap.erase(erased);
122 }
123 }
124 EXPECT_EQ((unsigned int)remaining, mEffectIdMap.size());
125 }
126
127 std::shared_ptr<IFactory> GetFactory() { return mEffectFactory; }
128 const std::vector<Descriptor::Identity>& GetEffectIds() { return mIds; }
129 const std::vector<Descriptor::Identity>& GetCompleteEffectIdList() { return mCompleteIds; }
130 const std::map<std::shared_ptr<IEffect>, Descriptor::Identity>& GetEffectMap() {
131 return mEffectIdMap;
132 }
133 void ClearEffectMap() { mEffectIdMap.clear(); }
134
135 private:
136 std::shared_ptr<IFactory> mEffectFactory;
137 std::string mServiceName;
138 AudioHalBinderServiceUtil binderUtil;
139 std::vector<Descriptor::Identity> mIds;
140 std::vector<Descriptor::Identity> mCompleteIds;
Shunkai Yao08b687d2022-10-13 21:11:11 +0000141 std::vector<Processing> mProcesses;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000142
143 std::map<std::shared_ptr<IEffect>, Descriptor::Identity> mEffectIdMap;
144};