1 /*
2  * Copyright (C) 2020 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 "VtsHalAudioControlTest"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <hidl/HidlTransportSupport.h>
27 #include <hwbinder/ProcessState.h>
28 #include <log/log.h>
29 #include <utils/Errors.h>
30 #include <utils/StrongPointer.h>
31 
32 #include <android/hardware/audio/common/6.0/types.h>
33 #include <android/hardware/automotive/audiocontrol/2.0/IAudioControl.h>
34 #include <android/hardware/automotive/audiocontrol/2.0/types.h>
35 #include <android/log.h>
36 
37 using namespace ::android::hardware::automotive::audiocontrol::V2_0;
38 using ::android::sp;
39 using ::android::hardware::hidl_bitfield;
40 using ::android::hardware::hidl_enum_range;
41 using ::android::hardware::hidl_handle;
42 using ::android::hardware::hidl_string;
43 using ::android::hardware::hidl_vec;
44 using ::android::hardware::Return;
45 using ::android::hardware::Void;
46 using ::android::hardware::audio::common::V6_0::AudioUsage;
47 
48 // The main test class for the automotive AudioControl HAL
49 class CarAudioControlHidlTest : public testing::TestWithParam<std::string> {
50   public:
SetUp()51     virtual void SetUp() override {
52         // Make sure we can connect to the driver
53         pAudioControl = IAudioControl::getService(GetParam());
54         ASSERT_NE(pAudioControl.get(), nullptr);
55     }
56 
TearDown()57     virtual void TearDown() override {}
58 
59   protected:
60     sp<IAudioControl> pAudioControl;  // Every test needs access to the service
61 };
62 
63 //
64 // Tests start here...
65 //
66 
67 /*
68  * Fader exercise test.  Note that only a subjective observer could determine if the
69  * fader actually works.  The only thing we can do is exercise the HAL and if the HAL crashes,
70  * we _might_ get a test failure if that breaks the connection to the driver.
71  */
TEST_P(CarAudioControlHidlTest,FaderExercise)72 TEST_P(CarAudioControlHidlTest, FaderExercise) {
73     ALOGI("Fader exercise test (silent)");
74 
75     // Set the fader all the way to the back
76     pAudioControl->setFadeTowardFront(-1.0f);
77 
78     // Set the fader all the way to the front
79     pAudioControl->setFadeTowardFront(1.0f);
80 
81     // Set the fader part way toward the back
82     pAudioControl->setFadeTowardFront(-0.333f);
83 
84     // Set the fader to a out of bounds value (driver should clamp)
85     pAudioControl->setFadeTowardFront(99999.9f);
86 
87     // Set the fader back to the middle
88     pAudioControl->setFadeTowardFront(0.0f);
89 }
90 
91 /*
92  * Balance exercise test.
93  */
TEST_P(CarAudioControlHidlTest,BalanceExercise)94 TEST_P(CarAudioControlHidlTest, BalanceExercise) {
95     ALOGI("Balance exercise test (silent)");
96 
97     // Set the balance all the way to the left
98     pAudioControl->setBalanceTowardRight(-1.0f);
99 
100     // Set the balance all the way to the right
101     pAudioControl->setBalanceTowardRight(1.0f);
102 
103     // Set the balance part way toward the left
104     pAudioControl->setBalanceTowardRight(-0.333f);
105 
106     // Set the balance to a out of bounds value (driver should clamp)
107     pAudioControl->setBalanceTowardRight(99999.9f);
108 
109     // Set the balance back to the middle
110     pAudioControl->setBalanceTowardRight(0.0f);
111 }
112 
113 struct FocusListenerMock : public IFocusListener {
114     MOCK_METHOD(Return<void>, requestAudioFocus,
115                 (hidl_bitfield<AudioUsage> usage, int zoneId,
116                  hidl_bitfield<AudioFocusChange> focusGain));
117     MOCK_METHOD(Return<void>, abandonAudioFocus, (hidl_bitfield<AudioUsage> usage, int zoneId));
118 };
119 
120 /*
121  * Test focus listener registration.
122  *
123  * Verifies that:
124  * - registerFocusListener succeeds;
125  * - registering a second listener succeeds in replacing the first;
126  * - closing handle does not crash;
127  */
TEST_P(CarAudioControlHidlTest,FocusListenerRegistration)128 TEST_P(CarAudioControlHidlTest, FocusListenerRegistration) {
129     ALOGI("Focus listener test");
130 
131     sp<FocusListenerMock> listener = new FocusListenerMock();
132 
133     auto hidlResult = pAudioControl->registerFocusListener(listener);
134     ASSERT_TRUE(hidlResult.isOk());
135 
136     sp<FocusListenerMock> listener2 = new FocusListenerMock();
137 
138     auto hidlResult2 = pAudioControl->registerFocusListener(listener2);
139     ASSERT_TRUE(hidlResult2.isOk());
140 
141     const sp<ICloseHandle>& closeHandle = hidlResult2;
142     closeHandle->close();
143 };
144 
TEST_P(CarAudioControlHidlTest,FocusChangeExercise)145 TEST_P(CarAudioControlHidlTest, FocusChangeExercise) {
146     ALOGI("Focus Change test");
147 
148     pAudioControl->onAudioFocusChange(AudioUsage::MEDIA | 0, 0,
149                                       AudioFocusChange::GAIN_TRANSIENT | 0);
150 };
151 
152 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CarAudioControlHidlTest);
153 INSTANTIATE_TEST_SUITE_P(
154         PerInstance, CarAudioControlHidlTest,
155         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAudioControl::descriptor)),
156         android::hardware::PrintInstanceNameToString);