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 | #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 | |
| 28 | using namespace android; |
| 29 | |
| 30 | using aidl::android::hardware::audio::effect::Descriptor; |
| 31 | using aidl::android::hardware::audio::effect::IEffect; |
| 32 | using aidl::android::hardware::audio::effect::IFactory; |
| 33 | using aidl::android::hardware::audio::effect::Parameter; |
| 34 | using aidl::android::media::audio::common::AudioUuid; |
| 35 | |
| 36 | class EffectFactoryHelper { |
| 37 | public: |
| 38 | explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {} |
| 39 | |
| 40 | void ConnectToFactoryService() { |
| 41 | mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName)); |
| 42 | ASSERT_NE(mEffectFactory, nullptr); |
| 43 | } |
| 44 | |
| 45 | void RestartFactoryService() { |
| 46 | ASSERT_NE(mEffectFactory, nullptr); |
| 47 | mEffectFactory = IFactory::fromBinder(binderUtil.restartService()); |
| 48 | ASSERT_NE(mEffectFactory, nullptr); |
| 49 | ClearEffectMap(); |
| 50 | } |
| 51 | |
| 52 | void QueryEffects(const std::optional<AudioUuid>& in_type, |
| 53 | const std::optional<AudioUuid>& in_instance, |
| 54 | std::vector<Descriptor::Identity>* _aidl_return) { |
| 55 | ASSERT_NE(mEffectFactory, nullptr); |
| 56 | EXPECT_IS_OK(mEffectFactory->queryEffects(in_type, in_instance, _aidl_return)); |
| 57 | mIds = *_aidl_return; |
| 58 | } |
| 59 | |
| 60 | void CreateEffects() { |
| 61 | ASSERT_NE(mEffectFactory, nullptr); |
| 62 | for (const auto& id : mIds) { |
| 63 | std::shared_ptr<IEffect> effect; |
| 64 | EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect)); |
| 65 | EXPECT_NE(effect, nullptr) << id.toString(); |
| 66 | if (effect) { |
| 67 | mEffectIdMap[effect] = id; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void CreateEffectsAndExpect( |
| 73 | const std::vector<std::pair<Descriptor::Identity, binder_exception_t>>& uuid_status) { |
| 74 | ASSERT_NE(mEffectFactory, nullptr); |
| 75 | for (const auto& it : uuid_status) { |
| 76 | std::shared_ptr<IEffect> effect; |
| 77 | auto status = mEffectFactory->createEffect(it.first.uuid, &effect); |
| 78 | EXPECT_STATUS(it.second, status); |
| 79 | if (effect) { |
| 80 | mEffectIdMap[effect] = it.first; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void DestroyEffectAndExpect(std::shared_ptr<IEffect>& instance, binder_exception_t exception) { |
| 86 | ASSERT_NE(mEffectFactory, nullptr); |
| 87 | auto status = mEffectFactory->destroyEffect(instance); |
| 88 | EXPECT_STATUS(exception, status); |
| 89 | } |
| 90 | |
| 91 | void QueryAndCreateAllEffects() { |
| 92 | ASSERT_NE(mEffectFactory, nullptr); |
| 93 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, &mCompleteIds)); |
| 94 | for (const auto& id : mCompleteIds) { |
| 95 | std::shared_ptr<IEffect> effect; |
| 96 | EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect)); |
| 97 | EXPECT_NE(effect, nullptr) << id.toString(); |
| 98 | mEffectIdMap[effect] = id; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void DestroyEffects(const binder_exception_t expected = EX_NONE, const int remaining = 0) { |
| 103 | ASSERT_NE(mEffectFactory, nullptr); |
| 104 | |
| 105 | for (auto it = mEffectIdMap.begin(); it != mEffectIdMap.end();) { |
| 106 | auto erased = it++; |
| 107 | auto status = mEffectFactory->destroyEffect(erased->first); |
| 108 | EXPECT_STATUS(expected, status); |
| 109 | if (status.isOk()) { |
| 110 | mEffectIdMap.erase(erased); |
| 111 | } |
| 112 | } |
| 113 | EXPECT_EQ((unsigned int)remaining, mEffectIdMap.size()); |
| 114 | } |
| 115 | |
| 116 | std::shared_ptr<IFactory> GetFactory() { return mEffectFactory; } |
| 117 | const std::vector<Descriptor::Identity>& GetEffectIds() { return mIds; } |
| 118 | const std::vector<Descriptor::Identity>& GetCompleteEffectIdList() { return mCompleteIds; } |
| 119 | const std::map<std::shared_ptr<IEffect>, Descriptor::Identity>& GetEffectMap() { |
| 120 | return mEffectIdMap; |
| 121 | } |
| 122 | void ClearEffectMap() { mEffectIdMap.clear(); } |
| 123 | |
| 124 | private: |
| 125 | std::shared_ptr<IFactory> mEffectFactory; |
| 126 | std::string mServiceName; |
| 127 | AudioHalBinderServiceUtil binderUtil; |
| 128 | std::vector<Descriptor::Identity> mIds; |
| 129 | std::vector<Descriptor::Identity> mCompleteIds; |
| 130 | |
| 131 | std::map<std::shared_ptr<IEffect>, Descriptor::Identity> mEffectIdMap; |
| 132 | }; |