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 #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 
27 namespace android::hardware::audio::common::testing {
28 
29 namespace detail {
30 
assertIsOk(const char * expr,const::ndk::ScopedAStatus & status)31 inline ::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 
48 using namespace android;
49 
50 using aidl::android::hardware::audio::core::sounddose::ISoundDose;
51 using aidl::android::hardware::audio::sounddose::ISoundDoseFactory;
52 
53 class SoundDoseFactory : public testing::TestWithParam<std::string> {
54   public:
SetUp()55     void SetUp() override { ASSERT_NO_FATAL_FAILURE(ConnectToService(GetParam())); }
56 
TearDown()57     void TearDown() override {}
58 
ConnectToService(const std::string & interfaceName)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 
74 // @VsrTest = VSR-5.5-002.001
TEST_P(SoundDoseFactory,GetSoundDoseForSameModule)75 TEST_P(SoundDoseFactory, GetSoundDoseForSameModule) {
76     const std::string module = "primary";
77 
78     std::shared_ptr<ISoundDose> soundDose1;
79     EXPECT_IS_OK(soundDoseFactory->getSoundDose(module, &soundDose1));
80 
81     if (soundDose1 == nullptr) {
82         LOG(WARNING) << "Primary module does not support sound dose";
83         return;
84     }
85 
86     std::shared_ptr<ISoundDose> soundDose2;
87     EXPECT_IS_OK(soundDoseFactory->getSoundDose(module, &soundDose2));
88     EXPECT_NE(nullptr, soundDose2);
89     EXPECT_EQ(soundDose1->asBinder(), soundDose2->asBinder())
90             << "getSoundDose must return the same interface for the same module";
91 }
92 
93 INSTANTIATE_TEST_SUITE_P(
94         SoundDoseFactoryTest, SoundDoseFactory,
95         testing::ValuesIn(android::getAidlHalInstanceNames(ISoundDoseFactory::descriptor)),
96         android::PrintInstanceNameToString);
97 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SoundDoseFactory);
98