Hayden Gomes | a521acf | 2020-11-02 12:29:05 -0800 | [diff] [blame] | 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 | #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 | |
| 28 | using android::ProcessState; |
| 29 | using android::sp; |
| 30 | using android::String16; |
| 31 | using android::binder::Status; |
| 32 | using android::hardware::automotive::audiocontrol::AudioFocusChange; |
| 33 | using android::hardware::automotive::audiocontrol::BnFocusListener; |
| 34 | using android::hardware::automotive::audiocontrol::IAudioControl; |
| 35 | |
| 36 | #include "audio_policy_configuration_V7_0.h" |
| 37 | |
| 38 | namespace xsd { |
| 39 | using namespace audio::policy::configuration::V7_0; |
| 40 | } |
| 41 | |
| 42 | class AudioControlAidl : public testing::TestWithParam<std::string> { |
| 43 | public: |
| 44 | virtual void SetUp() override { |
| 45 | audioControl = android::waitForDeclaredService<IAudioControl>(String16(GetParam().c_str())); |
| 46 | ASSERT_NE(audioControl, nullptr); |
| 47 | } |
| 48 | |
| 49 | sp<IAudioControl> audioControl; |
| 50 | int32_t capabilities; |
| 51 | }; |
| 52 | |
| 53 | TEST_P(AudioControlAidl, OnSetFadeTowardsFront) { |
| 54 | ALOGI("Fader exercise test (silent)"); |
| 55 | |
| 56 | // Set the fader all the way to the back |
| 57 | ASSERT_TRUE(audioControl->setFadeTowardFront(-1.0f).isOk()); |
| 58 | |
| 59 | // Set the fader all the way to the front |
| 60 | ASSERT_TRUE(audioControl->setFadeTowardFront(1.0f).isOk()); |
| 61 | |
| 62 | // Set the fader part way toward the back |
| 63 | ASSERT_TRUE(audioControl->setFadeTowardFront(-0.333f).isOk()); |
| 64 | |
| 65 | // Set the fader to a out of bounds value (driver should clamp) |
| 66 | ASSERT_TRUE(audioControl->setFadeTowardFront(99999.9f).isOk()); |
| 67 | |
| 68 | // Set the fader to a negative out of bounds value (driver should clamp) |
| 69 | ASSERT_TRUE(audioControl->setFadeTowardFront(-99999.9f).isOk()); |
| 70 | |
| 71 | // Set the fader back to the middle |
| 72 | ASSERT_TRUE(audioControl->setFadeTowardFront(0.0f).isOk()); |
| 73 | } |
| 74 | |
| 75 | TEST_P(AudioControlAidl, OnSetBalanceTowardsRight) { |
| 76 | ALOGI("Balance exercise test (silent)"); |
| 77 | |
| 78 | // Set the balance all the way to the left |
| 79 | ASSERT_TRUE(audioControl->setBalanceTowardRight(-1.0f).isOk()); |
| 80 | |
| 81 | // Set the balance all the way to the right |
| 82 | ASSERT_TRUE(audioControl->setBalanceTowardRight(1.0f).isOk()); |
| 83 | |
| 84 | // Set the balance part way toward the left |
| 85 | ASSERT_TRUE(audioControl->setBalanceTowardRight(-0.333f).isOk()); |
| 86 | |
| 87 | // Set the balance to a out of bounds value (driver should clamp) |
| 88 | ASSERT_TRUE(audioControl->setBalanceTowardRight(99999.9f).isOk()); |
| 89 | |
| 90 | // Set the balance to a negative out of bounds value (driver should clamp) |
| 91 | ASSERT_TRUE(audioControl->setBalanceTowardRight(-99999.9f).isOk()); |
| 92 | |
| 93 | // Set the balance back to the middle |
| 94 | ASSERT_TRUE(audioControl->setBalanceTowardRight(0.0f).isOk()); |
| 95 | |
| 96 | // Set the balance back to the middle |
| 97 | audioControl->setBalanceTowardRight(0.0f).isOk(); |
| 98 | } |
| 99 | |
| 100 | struct FocusListenerMock : BnFocusListener { |
| 101 | MOCK_METHOD(Status, requestAudioFocus, |
| 102 | (const String16& usage, int32_t zoneId, AudioFocusChange focusGain)); |
| 103 | MOCK_METHOD(Status, abandonAudioFocus, (const String16& usage, int32_t zoneId)); |
| 104 | }; |
| 105 | |
| 106 | /* |
| 107 | * Test focus listener registration. |
| 108 | * |
| 109 | * Verifies that: |
| 110 | * - registerFocusListener succeeds; |
| 111 | * - registering a second listener succeeds in replacing the first; |
| 112 | * - closing handle does not crash; |
| 113 | */ |
| 114 | TEST_P(AudioControlAidl, FocusListenerRegistration) { |
| 115 | ALOGI("Focus listener test"); |
| 116 | |
| 117 | sp<FocusListenerMock> listener = new FocusListenerMock(); |
| 118 | ASSERT_TRUE(audioControl->registerFocusListener(listener).isOk()); |
| 119 | |
| 120 | sp<FocusListenerMock> listener2 = new FocusListenerMock(); |
| 121 | ASSERT_TRUE(audioControl->registerFocusListener(listener2).isOk()); |
| 122 | }; |
| 123 | |
| 124 | TEST_P(AudioControlAidl, FocusChangeExercise) { |
| 125 | ALOGI("Focus Change test"); |
| 126 | |
| 127 | String16 usage = String16(xsd::toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA).c_str()); |
| 128 | ASSERT_TRUE( |
| 129 | audioControl->onAudioFocusChange(usage, 0, AudioFocusChange::GAIN_TRANSIENT).isOk()); |
| 130 | }; |
| 131 | |
| 132 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioControlAidl); |
| 133 | INSTANTIATE_TEST_SUITE_P( |
| 134 | Audiocontrol, AudioControlAidl, |
| 135 | testing::ValuesIn(android::getAidlHalInstanceNames(IAudioControl::descriptor)), |
| 136 | android::PrintInstanceNameToString); |
| 137 | |
| 138 | int main(int argc, char** argv) { |
| 139 | ::testing::InitGoogleTest(&argc, argv); |
| 140 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 141 | ProcessState::self()->startThreadPool(); |
| 142 | return RUN_ALL_TESTS(); |
| 143 | } |