blob: f23280d1c99ed8d91e4c2a77667fba935e721e4f [file] [log] [blame]
Hayden Gomesa521acf2020-11-02 12:29:05 -08001/*
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#define LOG_TAG "VtsAidlHalAudioControlTest"
17
18#include <aidl/Gtest.h>
19#include <aidl/Vintf.h>
20#include <gmock/gmock.h>
21
22#include <android/hardware/automotive/audiocontrol/BnFocusListener.h>
23#include <android/hardware/automotive/audiocontrol/IAudioControl.h>
24#include <android/log.h>
25#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27
28using android::ProcessState;
29using android::sp;
30using android::String16;
31using android::binder::Status;
32using android::hardware::automotive::audiocontrol::AudioFocusChange;
33using android::hardware::automotive::audiocontrol::BnFocusListener;
Hayden Gomesad816702020-12-14 15:29:29 -080034using android::hardware::automotive::audiocontrol::DuckingInfo;
Hayden Gomesa521acf2020-11-02 12:29:05 -080035using android::hardware::automotive::audiocontrol::IAudioControl;
36
Hayden Gomese502a602020-11-06 15:15:26 -080037#include "android_audio_policy_configuration_V7_0.h"
Hayden Gomesa521acf2020-11-02 12:29:05 -080038
39namespace xsd {
Hayden Gomese502a602020-11-06 15:15:26 -080040using namespace android::audio::policy::configuration::V7_0;
Hayden Gomesa521acf2020-11-02 12:29:05 -080041}
42
43class AudioControlAidl : public testing::TestWithParam<std::string> {
44 public:
45 virtual void SetUp() override {
46 audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str()));
47 ASSERT_NE(audioControl, nullptr);
48 }
49
50 sp<IAudioControl> audioControl;
51 int32_t capabilities;
52};
53
54TEST_P(AudioControlAidl, OnSetFadeTowardsFront) {
55 ALOGI("Fader exercise test (silent)");
56
57 // Set the fader all the way to the back
58 ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk());
59
60 // Set the fader all the way to the front
61 ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk());
62
63 // Set the fader part way toward the back
64 ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk());
65
66 // Set the fader to a out of bounds value (driver should clamp)
67 ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk());
68
69 // Set the fader to a negative out of bounds value (driver should clamp)
70 ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk());
71
72 // Set the fader back to the middle
73 ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk());
74}
75
76TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) {
77 ALOGI("Balance exercise test (silent)");
78
79 // Set the balance all the way to the left
80 ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk());
81
82 // Set the balance all the way to the right
83 ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk());
84
85 // Set the balance part way toward the left
86 ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk());
87
88 // Set the balance to a out of bounds value (driver should clamp)
89 ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk());
90
91 // Set the balance to a negative out of bounds value (driver should clamp)
92 ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk());
93
94 // Set the balance back to the middle
95 ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk());
96
97 // Set the balance back to the middle
98 audioControl->setBalanceTowardRight(0.0f).isOk();
99}
100
101struct FocusListenerMock : BnFocusListener {
102 MOCK_METHOD(Status, requestAudioFocus,
103 (const String16& usage, int32_t zoneId, AudioFocusChange focusGain));
104 MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId));
105};
106
107/*
108 * Test focus listener registration.
109 *
110 * Verifies that:
111 * - registerFocusListener succeeds;
112 * - registering a second listener succeeds in replacing the first;
113 * - closing handle does not crash;
114 */
115TEST_P(AudioControlAidl, FocusListenerRegistration) {
116 ALOGI("Focus listener test");
117
118 sp<FocusListenerMock> listener = new FocusListenerMock();
119 ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk());
120
121 sp<FocusListenerMock> listener2 = new FocusListenerMock();
122 ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk());
123};
124
125TEST_P(AudioControlAidl, FocusChangeExercise) {
126 ALOGI("Focus Change test");
127
128 String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str());
129 ASSERT_TRUE(
130 audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk());
131};
132
Hayden Gomesad816702020-12-14 15:29:29 -0800133TEST_P(AudioControlAidl, DuckChangeExercise) {
134 ALOGI("Duck change test");
135
136 DuckingInfo duckingInfo;
137 duckingInfo.zoneId = 0;
138 duckingInfo.deviceAddressesToDuck = {String16("address 1"), String16("address 2")};
139 duckingInfo.deviceAddressesToUnduck = {String16("address 3"), String16("address 4")};
140 duckingInfo.usagesHoldingFocus = {
141 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()),
142 String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
143 .c_str())};
144 std::vector<DuckingInfo> duckingInfos = {duckingInfo};
145 ALOGI("Duck change test start");
146 ASSERT_TRUE(audioControl->onDevicesToDuckChange(duckingInfos).isOk());
147}
148
Hayden Gomesa521acf2020-11-02 12:29:05 -0800149GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl);
150INSTANTIATE_TEST_SUITE_P(
151 Audiocontrol, AudioControlAidl,
152 testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)),
153 android::PrintInstanceNameToString);
154
155int main(int argc, char** argv) {
156 ::testing::InitGoogleTest(&argc, argv);
157 ProcessState::self()->setThreadPoolMaxThreadCount(1);
158 ProcessState::self()->startThreadPool();
159 return RUN_ALL_TESTS();
160}