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