blob: d58fcf259b01d9bf1c7edae6f586fa005aa980f7 [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"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000027#include "effect-impl/EffectUUID.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000028
29using namespace android;
30
31using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000032using aidl::android::hardware::audio::effect::EffectNullUuid;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000033using aidl::android::hardware::audio::effect::IEffect;
34using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yao08b687d2022-10-13 21:11:11 +000035using aidl::android::hardware::audio::effect::Processing;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000036using aidl::android::media::audio::common::AudioUuid;
37
38class EffectFactoryHelper {
39 public:
40 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {}
41
42 void ConnectToFactoryService() {
43 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName));
44 ASSERT_NE(mEffectFactory, nullptr);
45 }
46
47 void RestartFactoryService() {
48 ASSERT_NE(mEffectFactory, nullptr);
49 mEffectFactory = IFactory::fromBinder(binderUtil.restartService());
50 ASSERT_NE(mEffectFactory, nullptr);
51 ClearEffectMap();
52 }
53
54 void QueryEffects(const std::optional<AudioUuid>& in_type,
55 const std::optional<AudioUuid>& in_instance,
Shunkai Yao6afc8552022-10-26 22:47:20 +000056 const std::optional<AudioUuid>& in_proxy,
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000057 std::vector<Descriptor::Identity>* _aidl_return) {
58 ASSERT_NE(mEffectFactory, nullptr);
Shunkai Yao6afc8552022-10-26 22:47:20 +000059 EXPECT_IS_OK(mEffectFactory->queryEffects(in_type, in_instance, in_proxy, _aidl_return));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000060 mIds = *_aidl_return;
61 }
62
Shunkai Yao08b687d2022-10-13 21:11:11 +000063 void QueryProcessing(const std::optional<Processing::Type>& in_type,
64 std::vector<Processing>* _aidl_return) {
65 ASSERT_NE(mEffectFactory, nullptr);
66 EXPECT_IS_OK(mEffectFactory->queryProcessing(in_type, _aidl_return));
67 // only update the whole list if no filter applied
68 if (!in_type.has_value()) {
69 mProcesses = *_aidl_return;
70 }
71 }
72
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000073 void CreateEffects() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074 for (const auto& id : mIds) {
75 std::shared_ptr<IEffect> effect;
76 EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect));
77 EXPECT_NE(effect, nullptr) << id.toString();
78 if (effect) {
79 mEffectIdMap[effect] = id;
80 }
81 }
82 }
83
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000084 void QueryAndCreateEffects(const AudioUuid& type = EffectNullUuid) {
85 std::vector<Descriptor::Identity> ids;
86 ASSERT_NE(mEffectFactory, nullptr);
87
88 if (type == EffectNullUuid) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000089 EXPECT_IS_OK(
90 mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000091 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +000092 EXPECT_IS_OK(mEffectFactory->queryEffects(type, std::nullopt, std::nullopt, &ids));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000093 }
94 for (const auto& id : ids) {
95 ASSERT_EQ(id.type, type);
96 std::shared_ptr<IEffect> effect;
97 EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect));
98 EXPECT_NE(effect, nullptr) << id.toString();
99 if (effect) {
100 mEffectIdMap[effect] = id;
101 }
102 }
103 }
104
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000105 void CreateEffectsAndExpect(
106 const std::vector<std::pair<Descriptor::Identity, binder_exception_t>>& uuid_status) {
107 ASSERT_NE(mEffectFactory, nullptr);
108 for (const auto& it : uuid_status) {
109 std::shared_ptr<IEffect> effect;
110 auto status = mEffectFactory->createEffect(it.first.uuid, &effect);
111 EXPECT_STATUS(it.second, status);
112 if (effect) {
113 mEffectIdMap[effect] = it.first;
114 }
115 }
116 }
117
118 void DestroyEffectAndExpect(std::shared_ptr<IEffect>& instance, binder_exception_t exception) {
119 ASSERT_NE(mEffectFactory, nullptr);
120 auto status = mEffectFactory->destroyEffect(instance);
121 EXPECT_STATUS(exception, status);
122 }
123
124 void QueryAndCreateAllEffects() {
125 ASSERT_NE(mEffectFactory, nullptr);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000126 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt,
127 &mCompleteIds));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000128 for (const auto& id : mCompleteIds) {
129 std::shared_ptr<IEffect> effect;
130 EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect));
131 EXPECT_NE(effect, nullptr) << id.toString();
132 mEffectIdMap[effect] = id;
133 }
134 }
135
136 void DestroyEffects(const binder_exception_t expected = EX_NONE, const int remaining = 0) {
137 ASSERT_NE(mEffectFactory, nullptr);
138
139 for (auto it = mEffectIdMap.begin(); it != mEffectIdMap.end();) {
140 auto erased = it++;
141 auto status = mEffectFactory->destroyEffect(erased->first);
142 EXPECT_STATUS(expected, status);
143 if (status.isOk()) {
144 mEffectIdMap.erase(erased);
145 }
146 }
147 EXPECT_EQ((unsigned int)remaining, mEffectIdMap.size());
148 }
149
150 std::shared_ptr<IFactory> GetFactory() { return mEffectFactory; }
151 const std::vector<Descriptor::Identity>& GetEffectIds() { return mIds; }
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000152 const std::vector<Descriptor::Identity>& GetCompleteEffectIdList() const {
153 return mCompleteIds;
154 }
155 const std::map<std::shared_ptr<IEffect>, Descriptor::Identity>& GetEffectMap() const {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000156 return mEffectIdMap;
157 }
158 void ClearEffectMap() { mEffectIdMap.clear(); }
159
160 private:
161 std::shared_ptr<IFactory> mEffectFactory;
162 std::string mServiceName;
163 AudioHalBinderServiceUtil binderUtil;
164 std::vector<Descriptor::Identity> mIds;
165 std::vector<Descriptor::Identity> mCompleteIds;
Shunkai Yao08b687d2022-10-13 21:11:11 +0000166 std::vector<Processing> mProcesses;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000167
168 std::map<std::shared_ptr<IEffect>, Descriptor::Identity> mEffectIdMap;
169};