blob: df35bae08692ff7c5e5289eba6df611dc5d90ac2 [file] [log] [blame]
Vlad Popaf4fe41c2022-12-14 11:57:26 +01001/*
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#define LOG_TAG "VtsHalSoundDose.Factory"
18#include <android-base/logging.h>
19
20#include <aidl/Gtest.h>
21#include <aidl/Vintf.h>
22#include <aidl/android/hardware/audio/sounddose/ISoundDoseFactory.h>
23#include <android/binder_manager.h>
24
25#include <memory>
26
27namespace android::hardware::audio::common::testing {
28
29namespace detail {
30
31inline ::testing::AssertionResult assertIsOk(const char* expr, const ::ndk::ScopedAStatus& status) {
32 if (status.isOk()) {
33 return ::testing::AssertionSuccess();
34 }
35 return ::testing::AssertionFailure()
36 << "Expected the transaction \'" << expr << "\' to succeed\n"
37 << " but it has failed with: " << status;
38}
39
40} // namespace detail
41
42} // namespace android::hardware::audio::common::testing
43
44// Test that the transaction status 'isOk'
45#define EXPECT_IS_OK(ret) \
46 EXPECT_PRED_FORMAT1(::android::hardware::audio::common::testing::detail::assertIsOk, ret)
47
48using namespace android;
49
Vlad Popac88ea612022-12-28 17:04:58 +010050using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Vlad Popaf4fe41c2022-12-14 11:57:26 +010051using aidl::android::hardware::audio::sounddose::ISoundDoseFactory;
52
53class SoundDoseFactory : public testing::TestWithParam<std::string> {
54 public:
55 void SetUp() override { ASSERT_NO_FATAL_FAILURE(ConnectToService(GetParam())); }
56
57 void TearDown() override {}
58
59 void ConnectToService(const std::string& interfaceName) {
60 ndk::SpAIBinder binder =
61 ndk::SpAIBinder(AServiceManager_waitForService(interfaceName.c_str()));
62 if (binder == nullptr) {
63 LOG(ERROR) << "Failed to get service " << interfaceName;
64 } else {
65 LOG(DEBUG) << "Succeeded to get service " << interfaceName;
66 }
67 soundDoseFactory = ISoundDoseFactory::fromBinder(binder);
68 ASSERT_NE(soundDoseFactory, nullptr);
69 }
70
71 std::shared_ptr<ISoundDoseFactory> soundDoseFactory;
72};
73
74TEST_P(SoundDoseFactory, GetSoundDoseForSameModule) {
75 const std::string module = "primary";
76
77 std::shared_ptr<ISoundDose> soundDose1;
78 EXPECT_IS_OK(soundDoseFactory->getSoundDose(module, &soundDose1));
79
80 if (soundDose1 == nullptr) {
81 LOG(WARNING) << "Primary module does not support sound dose";
82 return;
83 }
84
85 std::shared_ptr<ISoundDose> soundDose2;
86 EXPECT_IS_OK(soundDoseFactory->getSoundDose(module, &soundDose2));
87 EXPECT_NE(nullptr, soundDose2);
88 EXPECT_EQ(soundDose1->asBinder(), soundDose2->asBinder())
89 << "getSoundDose must return the same interface for the same module";
90}
91
92INSTANTIATE_TEST_SUITE_P(
93 SoundDoseFactoryTest, SoundDoseFactory,
94 testing::ValuesIn(android::getAidlHalInstanceNames(ISoundDoseFactory::descriptor)),
95 android::PrintInstanceNameToString);
96GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SoundDoseFactory);