From ef98134aaec38ad3955efd82b4aa3961e89b78b9 Mon Sep 17 00:00:00 2001 From: Weilin Xu Date: Wed, 13 Dec 2023 16:08:04 -0800 Subject: [PATCH] Add AM/FM/HD unit test for bcradio utils lib Added unit tests for utils method related to AM, FM and HD radio in AIDL broadcast radio utils library. Bug: 310708886 Test: atest broadcastradio_utils_aidl_test Change-Id: I2ba334bb5c0e76cd7fa3b31e79d310bf2a8fa0c2 --- broadcastradio/common/utilsaidl/Android.bp | 21 +++- .../test/BroadcastRadioUtilsTest.cpp | 115 ++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 broadcastradio/common/utilsaidl/test/BroadcastRadioUtilsTest.cpp diff --git a/broadcastradio/common/utilsaidl/Android.bp b/broadcastradio/common/utilsaidl/Android.bp index 4ec635b173..4814778cd6 100644 --- a/broadcastradio/common/utilsaidl/Android.bp +++ b/broadcastradio/common/utilsaidl/Android.bp @@ -26,7 +26,7 @@ package { cc_library_static { name: "android.hardware.broadcastradio@common-utils-aidl-lib", defaults: [ - "VtsBroadcastRadioDefaults", + "BroadcastRadioUtilsDefaults", ], shared_libs: [ "android.hardware.broadcastradio-V1-ndk", @@ -36,7 +36,7 @@ cc_library_static { cc_library_static { name: "android.hardware.broadcastradio@common-utils-aidl-lib-V2", defaults: [ - "VtsBroadcastRadioDefaults", + "BroadcastRadioUtilsDefaults", ], srcs: [ "src/UtilsV2.cpp", @@ -46,8 +46,23 @@ cc_library_static { ], } +cc_test { + name: "broadcastradio_utils_aidl_test", + defaults: [ + "BroadcastRadioUtilsDefaults", + ], + srcs: [ + "test/*.cpp", + ], + static_libs: [ + "android.hardware.broadcastradio@common-utils-aidl-lib-V2", + "android.hardware.broadcastradio-V2-ndk", + ], + test_suites: ["general-tests"], +} + cc_defaults { - name: "VtsBroadcastRadioDefaults", + name: "BroadcastRadioUtilsDefaults", vendor_available: true, relative_install_path: "hw", cflags: [ diff --git a/broadcastradio/common/utilsaidl/test/BroadcastRadioUtilsTest.cpp b/broadcastradio/common/utilsaidl/test/BroadcastRadioUtilsTest.cpp new file mode 100644 index 0000000000..07509495a4 --- /dev/null +++ b/broadcastradio/common/utilsaidl/test/BroadcastRadioUtilsTest.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +namespace aidl::android::hardware::broadcastradio { + +namespace { +constexpr int64_t kFmFrequencyKHz = 97900; +constexpr uint64_t kDabSidExt = 0x0E10000C221u; +constexpr uint32_t kDabEnsemble = 0xCE15u; +constexpr uint64_t kDabFrequencyKhz = 225648u; +constexpr uint64_t kHdStationId = 0xA0000001u; +constexpr uint64_t kHdSubChannel = 1u; +constexpr uint64_t kHdFrequency = 97700u; +} // namespace + +TEST(BroadcastRadioUtilsTest, hasIdWithPrimaryIdType) { + ProgramSelector sel = utils::makeSelectorAmfm(kFmFrequencyKHz); + + ASSERT_TRUE(utils::hasId(sel, IdentifierType::AMFM_FREQUENCY_KHZ)); +} + +TEST(BroadcastRadioUtilsTest, makeIdentifier) { + ProgramIdentifier id = + utils::makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, kFmFrequencyKHz); + + ASSERT_EQ(id.type, IdentifierType::AMFM_FREQUENCY_KHZ); + ASSERT_EQ(id.value, kFmFrequencyKHz); +} + +TEST(BroadcastRadioUtilsTest, makeSelectorAmfm) { + ProgramSelector sel = utils::makeSelectorAmfm(kFmFrequencyKHz); + + ASSERT_EQ(sel.primaryId.type, IdentifierType::AMFM_FREQUENCY_KHZ); + ASSERT_EQ(sel.primaryId.value, kFmFrequencyKHz); + ASSERT_TRUE(sel.secondaryIds.empty()); +} + +TEST(BroadcastRadioUtilsTest, makeSelectorHd) { + ProgramSelector sel = utils::makeSelectorHd(kHdStationId, kHdSubChannel, kHdFrequency); + + ASSERT_EQ(sel.primaryId.type, IdentifierType::HD_STATION_ID_EXT); + ASSERT_TRUE(sel.secondaryIds.empty()); + ASSERT_EQ(utils::getHdSubchannel(sel), static_cast(kHdSubChannel)); + ASSERT_EQ(utils::getHdFrequency(sel), static_cast(kHdFrequency)); +} + +TEST(BroadcastRadioUtilsTest, makeHdRadioStationName) { + std::string stationName = "aB1-FM"; + int64_t expectedIdValue = 0x4D46314241; + + ProgramIdentifier stationNameId = utils::makeHdRadioStationName(stationName); + + ASSERT_EQ(stationNameId.type, IdentifierType::HD_STATION_NAME); + ASSERT_EQ(stationNameId.value, expectedIdValue); +} + +TEST(BroadcastRadioUtilsTest, getHdFrequencyWithoutHdId) { + ProgramSelector sel = utils::makeSelectorDab(kDabSidExt, kDabEnsemble, kDabFrequencyKhz); + + ASSERT_EQ(utils::getHdFrequency(sel), 0u); +} + +TEST(BroadcastRadioUtilsTest, hasAmFmFrequencyWithAmFmSelector) { + ProgramSelector sel = utils::makeSelectorAmfm(kFmFrequencyKHz); + + ASSERT_TRUE(utils::hasAmFmFrequency(sel)); +} + +TEST(BroadcastRadioUtilsTest, hasAmFmFrequencyWithHdSelector) { + ProgramSelector sel = utils::makeSelectorHd(kHdStationId, kHdSubChannel, kHdFrequency); + + ASSERT_TRUE(utils::hasAmFmFrequency(sel)); +} + +TEST(BroadcastRadioUtilsTest, hasAmFmFrequencyWithNonAmFmHdSelector) { + ProgramSelector sel = utils::makeSelectorDab(kDabSidExt, kDabEnsemble, kDabFrequencyKhz); + + ASSERT_FALSE(utils::hasAmFmFrequency(sel)); +} + +TEST(BroadcastRadioUtilsTest, getAmFmFrequencyWithAmFmSelector) { + ProgramSelector sel = utils::makeSelectorAmfm(kFmFrequencyKHz); + + ASSERT_EQ(utils::getAmFmFrequency(sel), static_cast(kFmFrequencyKHz)); +} + +TEST(BroadcastRadioUtilsTest, getAmFmFrequencyWithHdSelector) { + ProgramSelector sel = utils::makeSelectorHd(kHdStationId, kHdSubChannel, kHdFrequency); + + ASSERT_EQ(utils::getAmFmFrequency(sel), static_cast(kHdFrequency)); +} + +TEST(BroadcastRadioUtilsTest, getAmFmFrequencyWithNonAmFmHdSelector) { + ProgramSelector sel = utils::makeSelectorDab(kDabSidExt, kDabEnsemble, kDabFrequencyKhz); + + ASSERT_EQ(utils::getAmFmFrequency(sel), 0u); +} + +} // namespace aidl::android::hardware::broadcastradio