15f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph/* 25f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Copyright (C) 2018 The Android Open Source Project 35f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * 45f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Licensed under the Apache License, Version 2.0 (the "License"); 55f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * you may not use this file except in compliance with the License. 65f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * You may obtain a copy of the License at 75f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * 85f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * http://www.apache.org/licenses/LICENSE-2.0 95f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * 105f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Unless required by applicable law or agreed to in writing, software 115f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * distributed under the License is distributed on an "AS IS" BASIS, 125f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * See the License for the specific language governing permissions and 145f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * limitations under the License. 155f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph */ 165f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 175f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#define LOG_TAG "VtsHalAudioControlTest" 185f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 195f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <stdio.h> 205f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <string.h> 215f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 225f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <hidl/HidlTransportSupport.h> 235f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <hwbinder/ProcessState.h> 245f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <log/log.h> 255f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <utils/Errors.h> 265f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <utils/StrongPointer.h> 275f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 285f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <android/hardware/automotive/audiocontrol/1.0/types.h> 295f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <android/hardware/automotive/audiocontrol/1.0/IAudioControl.h> 305f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <android/log.h> 315f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 325f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph#include <VtsHalHidlTargetTestBase.h> 335f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 345f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing namespace ::android::hardware::automotive::audiocontrol::V1_0; 355f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::Return; 365f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::Void; 375f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::hidl_enum_iterator; 385f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::hidl_handle; 395f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::hidl_string; 405f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::hardware::hidl_vec; 415f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphusing ::android::sp; 425f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 435f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 445f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph// Boiler plate for test harness 455f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphclass CarAudioControlHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { 465f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph public: 475f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // get the test environment singleton 485f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph static CarAudioControlHidlEnvironment* Instance() { 495f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph static CarAudioControlHidlEnvironment* instance = new CarAudioControlHidlEnvironment; 505f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph return instance; 515f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph } 525f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 535f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph virtual void registerTestServices() override { registerTestService<IAudioControl>(); } 545f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph private: 555f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph CarAudioControlHidlEnvironment() {} 565f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph}; 575f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 585f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 595f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph// The main test class for the automotive AudioControl HAL 605f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphclass CarAudioControlHidlTest : public ::testing::VtsHalHidlTargetTestBase { 615f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolphpublic: 625f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph virtual void SetUp() override { 635f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Make sure we can connect to the driver 645f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl = ::testing::VtsHalHidlTargetTestBase::getService<IAudioControl>( 655f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph CarAudioControlHidlEnvironment::Instance()-> 665f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph getServiceName<IAudioControl>()); 675f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph ASSERT_NE(pAudioControl.get(), nullptr); 685f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph } 695f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 705f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph virtual void TearDown() override {} 715f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 725f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph protected: 735f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph sp<IAudioControl> pAudioControl; // Every test needs access to the service 745f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph}; 755f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 765f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph// 775f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph// Tests start here... 785f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph// 795f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 805f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph/* 815f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Fader exercise test. Note that only a subjective observer could determine if the 825f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * fader actually works. The only thing we can do is exercise the HAL and if the HAL crashes, 835f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * we _might_ get a test failure if that breaks the connection to the driver. 845f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph */ 855f2568b2e1d010c8a24519ee360eec1141f6c859Scott RandolphTEST_F(CarAudioControlHidlTest, FaderExercise) { 865f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph ALOGI("Fader exercise test (silent)"); 875f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 885f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the fader all the way to the back 895f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setFadeTowardFront(-1.0f); 905f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 915f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the fader all the way to the front 925f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setFadeTowardFront(1.0f); 935f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 945f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the fader part way toward the back 955f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setFadeTowardFront(-0.333f); 965f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 975f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the fader to a out of bounds value (driver should clamp) 985f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setFadeTowardFront(99999.9f); 995f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1005f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the fader back to the middle 1015f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setFadeTowardFront(0.0f); 1025f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph} 1035f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1045f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph/* 1055f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Balance exercise test. 1065f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph */ 1075f2568b2e1d010c8a24519ee360eec1141f6c859Scott RandolphTEST_F(CarAudioControlHidlTest, BalanceExercise) { 1085f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph ALOGI("Balance exercise test (silent)"); 1095f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1105f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the balance all the way to the left 1115f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setBalanceTowardRight(-1.0f); 1125f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1135f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the balance all the way to the right 1145f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setBalanceTowardRight(1.0f); 1155f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1165f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the balance part way toward the left 1175f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setBalanceTowardRight(-0.333f); 1185f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1195f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the balance to a out of bounds value (driver should clamp) 1205f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setBalanceTowardRight(99999.9f); 1215f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1225f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Set the balance back to the middle 1235f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph pAudioControl->setBalanceTowardRight(0.0f); 1245f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph} 1255f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1265f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph/* 1275f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph * Context mapping test. 1285f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph */ 1295f2568b2e1d010c8a24519ee360eec1141f6c859Scott RandolphTEST_F(CarAudioControlHidlTest, ContextMapping) { 1305f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph ALOGI("Context mapping test"); 1315f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1325f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph int bus = -1; 1335f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1345f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // For each defined context, query the driver for the BUS on which it should be delivered 1355f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph for (const auto& ctxt : hidl_enum_iterator<ContextNumber>()) { 1365f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph bus = pAudioControl->getBusForContext(ctxt); 1375f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1385f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph if (ctxt == ContextNumber::INVALID) { 1395f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Invalid context should never be mapped to a bus 1405f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph EXPECT_EQ(bus, -1); 1415f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph } else { 1425f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph EXPECT_GE(bus, 0); 1435f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // TODO: Consider enumerating the devices on the actual audio hal to validate the 1445f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // bus IDs. This would introduce an dependency on the audio HAL, however. Would that 1455f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // even work while Android is up and running? 1465f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph } 1475f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph } 1485f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1495f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Try asking about an invalid context one beyond the last defined to see that it gets back a -1 1505f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph int contextRange = std::distance(hidl_enum_iterator<ContextNumber>().begin(), 1515f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph hidl_enum_iterator<ContextNumber>().end()); 1525f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph bus = pAudioControl->getBusForContext((ContextNumber)contextRange); 1535f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph EXPECT_EQ(bus, -1); 1545f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph 1555f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph // Try asking about an invalid context WAY out of range to see that it gets back a -1 1565f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph bus = pAudioControl->getBusForContext((ContextNumber)~0); 1575f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph EXPECT_EQ(bus, -1); 1585f2568b2e1d010c8a24519ee360eec1141f6c859Scott Randolph} 159