1 /*
2  * Copyright (C) 2018 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 <stdio.h>
20 #include <string.h>
21 
22 #include <hidl/HidlTransportSupport.h>
23 #include <hwbinder/ProcessState.h>
24 #include <log/log.h>
25 #include <utils/Errors.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <android/hardware/automotive/audiocontrol/1.0/IAudioControl.h>
29 #include <android/hardware/automotive/audiocontrol/1.0/types.h>
30 #include <android/log.h>
31 #include <gtest/gtest.h>
32 #include <hidl/GtestPrinter.h>
33 #include <hidl/ServiceManagement.h>
34 
35 using namespace ::android::hardware::automotive::audiocontrol::V1_0;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38 using ::android::hardware::hidl_enum_range;
39 using ::android::hardware::hidl_handle;
40 using ::android::hardware::hidl_string;
41 using ::android::hardware::hidl_vec;
42 using ::android::sp;
43 
44 // The main test class for the automotive AudioControl HAL
45 class CarAudioControlHidlTest : public ::testing::TestWithParam<std::string> {
46   public:
SetUp()47     virtual void SetUp() override {
48         // Make sure we can connect to the driver
49         pAudioControl = IAudioControl::getService(GetParam());
50         ASSERT_NE(pAudioControl.get(), nullptr);
51     }
52 
TearDown()53     virtual void TearDown() override {}
54 
55    protected:
56     sp<IAudioControl> pAudioControl;  // Every test needs access to the service
57 };
58 
59 //
60 // Tests start here...
61 //
62 
63 /*
64  * Fader exercise test.  Note that only a subjective observer could determine if the
65  * fader actually works.  The only thing we can do is exercise the HAL and if the HAL crashes,
66  * we _might_ get a test failure if that breaks the connection to the driver.
67  */
TEST_P(CarAudioControlHidlTest,FaderExercise)68 TEST_P(CarAudioControlHidlTest, FaderExercise) {
69     ALOGI("Fader exercise test (silent)");
70 
71     // Set the fader all the way to the back
72     pAudioControl->setFadeTowardFront(-1.0f);
73 
74     // Set the fader all the way to the front
75     pAudioControl->setFadeTowardFront(1.0f);
76 
77     // Set the fader part way toward the back
78     pAudioControl->setFadeTowardFront(-0.333f);
79 
80     // Set the fader to a out of bounds value (driver should clamp)
81     pAudioControl->setFadeTowardFront(99999.9f);
82 
83     // Set the fader back to the middle
84     pAudioControl->setFadeTowardFront(0.0f);
85 }
86 
87 /*
88  * Balance exercise test.
89  */
TEST_P(CarAudioControlHidlTest,BalanceExercise)90 TEST_P(CarAudioControlHidlTest, BalanceExercise) {
91     ALOGI("Balance exercise test (silent)");
92 
93     // Set the balance all the way to the left
94     pAudioControl->setBalanceTowardRight(-1.0f);
95 
96     // Set the balance all the way to the right
97     pAudioControl->setBalanceTowardRight(1.0f);
98 
99     // Set the balance part way toward the left
100     pAudioControl->setBalanceTowardRight(-0.333f);
101 
102     // Set the balance to a out of bounds value (driver should clamp)
103     pAudioControl->setBalanceTowardRight(99999.9f);
104 
105     // Set the balance back to the middle
106     pAudioControl->setBalanceTowardRight(0.0f);
107 }
108 
109 /*
110  * Context mapping test.
111  */
TEST_P(CarAudioControlHidlTest,ContextMapping)112 TEST_P(CarAudioControlHidlTest, ContextMapping) {
113     ALOGI("Context mapping test");
114 
115     int bus = -1;
116 
117     // For each defined context, query the driver for the BUS on which it should be delivered
118     for (const auto& ctxt : hidl_enum_range<ContextNumber>()) {
119          bus = pAudioControl->getBusForContext(ctxt);
120 
121          if (ctxt == ContextNumber::INVALID) {
122              // Invalid context should never be mapped to a bus
123              EXPECT_EQ(bus, -1);
124          } else {
125              EXPECT_GE(bus, 0);
126              // TODO:  Consider enumerating the devices on the actual audio hal to validate the
127              // bus IDs.  This would introduce an dependency on the audio HAL, however.  Would that
128              // even work while Android is up and running?
129          }
130     }
131 
132     // Try asking about an invalid context one beyond the last defined to see that it gets back a -1
133     int contextRange = std::distance(hidl_enum_range<ContextNumber>().begin(),
134                                      hidl_enum_range<ContextNumber>().end());
135     bus = pAudioControl->getBusForContext((ContextNumber)contextRange);
136     EXPECT_EQ(bus, -1);
137 
138     // Try asking about an invalid context WAY out of range to see that it gets back a -1
139     bus = pAudioControl->getBusForContext((ContextNumber)~0);
140     EXPECT_EQ(bus, -1);
141 }
142 
143 INSTANTIATE_TEST_SUITE_P(
144         PerInstance, CarAudioControlHidlTest,
145         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAudioControl::descriptor)),
146         android::hardware::PrintInstanceNameToString);