blob: 754b05b93d7fdfd04f24e90e633d06694525f97a [file] [log] [blame]
Weilin Xub23d0ea2022-05-09 18:26:23 +00001/*
2 * Copyright (C) 2022 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 EGMOCK_VERBOSE 1
18
19#include <aidl/android/hardware/broadcastradio/BnAnnouncementListener.h>
20#include <aidl/android/hardware/broadcastradio/BnTunerCallback.h>
21#include <aidl/android/hardware/broadcastradio/ConfigFlag.h>
22#include <aidl/android/hardware/broadcastradio/IBroadcastRadio.h>
23#include <aidl/android/hardware/broadcastradio/ProgramListChunk.h>
24#include <aidl/android/hardware/broadcastradio/ProgramSelector.h>
25#include <aidl/android/hardware/broadcastradio/VendorKeyValue.h>
26#include <android-base/logging.h>
27#include <android-base/strings.h>
28#include <android-base/thread_annotations.h>
29#include <android/binder_manager.h>
30#include <android/binder_process.h>
31
32#include <aidl/Gtest.h>
33#include <aidl/Vintf.h>
34#include <broadcastradio-utils-aidl/Utils.h>
Weilin Xu25409e52023-09-06 10:36:24 -070035#include <broadcastradio-utils-aidl/UtilsV2.h>
Weilin Xub23d0ea2022-05-09 18:26:23 +000036#include <cutils/bitops.h>
37#include <gmock/gmock.h>
Weilin Xuc3301592023-12-06 16:45:24 -080038#include <gtest/gtest.h>
Weilin Xub23d0ea2022-05-09 18:26:23 +000039
40#include <chrono>
Weilin Xu4420c1d2023-06-21 22:49:04 +000041#include <condition_variable>
Weilin Xub23d0ea2022-05-09 18:26:23 +000042#include <optional>
43#include <regex>
44
45namespace aidl::android::hardware::broadcastradio::vts {
46
47namespace {
48
49using ::aidl::android::hardware::broadcastradio::utils::makeIdentifier;
50using ::aidl::android::hardware::broadcastradio::utils::makeSelectorAmfm;
Weilin Xu0d4207d2022-12-09 00:37:44 +000051using ::aidl::android::hardware::broadcastradio::utils::makeSelectorDab;
Weilin Xub23d0ea2022-05-09 18:26:23 +000052using ::aidl::android::hardware::broadcastradio::utils::resultToInt;
53using ::ndk::ScopedAStatus;
54using ::ndk::SharedRefBase;
Weilin Xub23d0ea2022-05-09 18:26:23 +000055using ::std::vector;
56using ::testing::_;
57using ::testing::AnyNumber;
58using ::testing::ByMove;
59using ::testing::DoAll;
60using ::testing::Invoke;
61using ::testing::SaveArg;
62
63namespace bcutils = ::aidl::android::hardware::broadcastradio::utils;
64
Weilin Xu3277a212022-09-01 19:08:17 +000065const ConfigFlag kConfigFlagValues[] = {
66 ConfigFlag::FORCE_MONO,
67 ConfigFlag::FORCE_ANALOG,
68 ConfigFlag::FORCE_DIGITAL,
69 ConfigFlag::RDS_AF,
70 ConfigFlag::RDS_REG,
71 ConfigFlag::DAB_DAB_LINKING,
72 ConfigFlag::DAB_FM_LINKING,
73 ConfigFlag::DAB_DAB_SOFT_LINKING,
74 ConfigFlag::DAB_FM_SOFT_LINKING,
75};
76
Weilin Xu25409e52023-09-06 10:36:24 -070077constexpr int32_t kAidlVersion1 = 1;
78constexpr int32_t kAidlVersion2 = 2;
79
Weilin Xu25409e52023-09-06 10:36:24 -070080bool isValidAmFmFreq(int64_t freq, int aidlVersion) {
Weilin Xu3277a212022-09-01 19:08:17 +000081 ProgramIdentifier id = bcutils::makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, freq);
Weilin Xu25409e52023-09-06 10:36:24 -070082 if (aidlVersion == kAidlVersion1) {
83 return bcutils::isValid(id);
84 } else if (aidlVersion == kAidlVersion2) {
85 return bcutils::isValidV2(id);
86 }
87 LOG(ERROR) << "Unknown AIDL version " << aidlVersion;
88 return false;
Weilin Xu3277a212022-09-01 19:08:17 +000089}
90
Weilin Xu25409e52023-09-06 10:36:24 -070091void validateRange(const AmFmBandRange& range, int aidlVersion) {
92 EXPECT_TRUE(isValidAmFmFreq(range.lowerBound, aidlVersion));
93 EXPECT_TRUE(isValidAmFmFreq(range.upperBound, aidlVersion));
Weilin Xu3277a212022-09-01 19:08:17 +000094 EXPECT_LT(range.lowerBound, range.upperBound);
95 EXPECT_GT(range.spacing, 0u);
96 EXPECT_EQ((range.upperBound - range.lowerBound) % range.spacing, 0u);
97}
98
99bool supportsFM(const AmFmRegionConfig& config) {
100 for (const auto& range : config.ranges) {
101 if (bcutils::getBand(range.lowerBound) == bcutils::FrequencyBand::FM) {
102 return true;
103 }
104 }
105 return false;
106}
107
Weilin Xub23d0ea2022-05-09 18:26:23 +0000108} // namespace
109
Weilin Xu4420c1d2023-06-21 22:49:04 +0000110class CallbackFlag final {
Weilin Xub23d0ea2022-05-09 18:26:23 +0000111 public:
Weilin Xu4420c1d2023-06-21 22:49:04 +0000112 CallbackFlag(int timeoutMs) { mTimeoutMs = timeoutMs; }
113 /**
114 * Notify that the callback is called.
115 */
116 void notify() {
117 std::unique_lock<std::mutex> lock(mMutex);
118 mCalled = true;
119 lock.unlock();
120 mCv.notify_all();
121 };
122
123 /**
124 * Wait for the timeout passed into the constructor.
125 */
126 bool wait() {
127 std::unique_lock<std::mutex> lock(mMutex);
128 return mCv.wait_for(lock, std::chrono::milliseconds(mTimeoutMs),
129 [this] { return mCalled; });
130 };
131
132 /**
133 * Reset the callback to not called.
134 */
135 void reset() {
136 std::unique_lock<std::mutex> lock(mMutex);
137 mCalled = false;
138 }
139
140 private:
141 std::mutex mMutex;
142 bool mCalled GUARDED_BY(mMutex) = false;
143 std::condition_variable mCv;
144 int mTimeoutMs;
145};
146
147class TunerCallbackImpl final : public BnTunerCallback {
148 public:
Weilin Xu25409e52023-09-06 10:36:24 -0700149 explicit TunerCallbackImpl(int32_t aidlVersion);
Weilin Xu0d4207d2022-12-09 00:37:44 +0000150 ScopedAStatus onTuneFailed(Result result, const ProgramSelector& selector) override;
Weilin Xub23d0ea2022-05-09 18:26:23 +0000151 ScopedAStatus onCurrentProgramInfoChanged(const ProgramInfo& info) override;
152 ScopedAStatus onProgramListUpdated(const ProgramListChunk& chunk) override;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000153 ScopedAStatus onParametersUpdated(const vector<VendorKeyValue>& parameters) override;
154 ScopedAStatus onAntennaStateChange(bool connected) override;
155 ScopedAStatus onConfigFlagUpdated(ConfigFlag in_flag, bool in_value) override;
Weilin Xub23d0ea2022-05-09 18:26:23 +0000156
Weilin Xu4420c1d2023-06-21 22:49:04 +0000157 bool waitOnCurrentProgramInfoChangedCallback();
158 bool waitProgramReady();
159 void reset();
160
161 bool getAntennaConnectionState();
162 ProgramInfo getCurrentProgramInfo();
163 bcutils::ProgramInfoSet getProgramList();
164
165 private:
Weilin Xub23d0ea2022-05-09 18:26:23 +0000166 std::mutex mLock;
Weilin Xu25409e52023-09-06 10:36:24 -0700167 int32_t mCallbackAidlVersion;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000168 bool mAntennaConnectionState GUARDED_BY(mLock);
169 ProgramInfo mCurrentProgramInfo GUARDED_BY(mLock);
Weilin Xub23d0ea2022-05-09 18:26:23 +0000170 bcutils::ProgramInfoSet mProgramList GUARDED_BY(mLock);
Weilin Xu4420c1d2023-06-21 22:49:04 +0000171 CallbackFlag mOnCurrentProgramInfoChangedFlag = CallbackFlag(IBroadcastRadio::TUNER_TIMEOUT_MS);
172 CallbackFlag mOnProgramListReadyFlag = CallbackFlag(IBroadcastRadio::LIST_COMPLETE_TIMEOUT_MS);
Weilin Xub23d0ea2022-05-09 18:26:23 +0000173};
174
175struct AnnouncementListenerMock : public BnAnnouncementListener {
176 MOCK_METHOD1(onListUpdated, ScopedAStatus(const vector<Announcement>&));
177};
178
Weilin Xu25409e52023-09-06 10:36:24 -0700179class BroadcastRadioHalTest : public testing::TestWithParam<std::string> {
Weilin Xub23d0ea2022-05-09 18:26:23 +0000180 protected:
181 void SetUp() override;
182 void TearDown() override;
183
184 bool getAmFmRegionConfig(bool full, AmFmRegionConfig* config);
185 std::optional<bcutils::ProgramInfoSet> getProgramList();
186 std::optional<bcutils::ProgramInfoSet> getProgramList(const ProgramFilter& filter);
187
188 std::shared_ptr<IBroadcastRadio> mModule;
189 Properties mProperties;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000190 std::shared_ptr<TunerCallbackImpl> mCallback;
Weilin Xu25409e52023-09-06 10:36:24 -0700191 int32_t mAidlVersion;
Weilin Xub23d0ea2022-05-09 18:26:23 +0000192};
193
Weilin Xu25409e52023-09-06 10:36:24 -0700194MATCHER_P(InfoHasId, id,
195 std::string(negation ? "does not contain" : "contains") + " " + id.toString()) {
Weilin Xub23d0ea2022-05-09 18:26:23 +0000196 vector<int> ids = bcutils::getAllIds(arg.selector, id.type);
197 return ids.end() != find(ids.begin(), ids.end(), id.value);
198}
199
Weilin Xu25409e52023-09-06 10:36:24 -0700200TunerCallbackImpl::TunerCallbackImpl(int32_t aidlVersion) {
201 mCallbackAidlVersion = aidlVersion;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000202 mAntennaConnectionState = true;
Weilin Xub23d0ea2022-05-09 18:26:23 +0000203}
204
Weilin Xu4420c1d2023-06-21 22:49:04 +0000205ScopedAStatus TunerCallbackImpl::onTuneFailed(Result result, const ProgramSelector& selector) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000206 LOG(DEBUG) << "Tune failed for selector" << selector.toString();
207 EXPECT_TRUE(result == Result::CANCELED);
208 return ndk::ScopedAStatus::ok();
209}
210
Weilin Xu4420c1d2023-06-21 22:49:04 +0000211ScopedAStatus TunerCallbackImpl::onCurrentProgramInfoChanged(const ProgramInfo& info) {
212 LOG(DEBUG) << "onCurrentProgramInfoChanged called";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000213 for (const auto& id : info.selector) {
214 EXPECT_NE(id.type, IdentifierType::INVALID);
215 }
216
217 IdentifierType logically = info.logicallyTunedTo.type;
218 // This field is required for currently tuned program and should be INVALID
219 // for entries from the program list.
220 EXPECT_TRUE(logically == IdentifierType::AMFM_FREQUENCY_KHZ ||
221 logically == IdentifierType::RDS_PI ||
222 logically == IdentifierType::HD_STATION_ID_EXT ||
223 logically == IdentifierType::DAB_SID_EXT ||
224 logically == IdentifierType::DRMO_SERVICE_ID ||
225 logically == IdentifierType::SXM_SERVICE_ID ||
226 (logically >= IdentifierType::VENDOR_START &&
227 logically <= IdentifierType::VENDOR_END) ||
228 logically > IdentifierType::SXM_CHANNEL);
229
230 IdentifierType physically = info.physicallyTunedTo.type;
231 // ditto (see "logically" above)
232 EXPECT_TRUE(physically == IdentifierType::AMFM_FREQUENCY_KHZ ||
Weilin Xu0d4207d2022-12-09 00:37:44 +0000233 physically == IdentifierType::DAB_FREQUENCY_KHZ ||
Weilin Xub23d0ea2022-05-09 18:26:23 +0000234 physically == IdentifierType::DRMO_FREQUENCY_KHZ ||
235 physically == IdentifierType::SXM_CHANNEL ||
236 (physically >= IdentifierType::VENDOR_START &&
237 physically <= IdentifierType::VENDOR_END) ||
238 physically > IdentifierType::SXM_CHANNEL);
239
240 if (logically == IdentifierType::AMFM_FREQUENCY_KHZ) {
Weilin Xu25409e52023-09-06 10:36:24 -0700241 std::optional<std::string> ps;
242 if (mCallbackAidlVersion == kAidlVersion1) {
243 ps = bcutils::getMetadataString(info, Metadata::rdsPs);
244 } else {
245 ps = bcutils::getMetadataStringV2(info, Metadata::rdsPs);
246 }
Weilin Xub23d0ea2022-05-09 18:26:23 +0000247 if (ps.has_value()) {
248 EXPECT_NE(::android::base::Trim(*ps), "")
249 << "Don't use empty RDS_PS as an indicator of missing RSD PS data.";
250 }
251 }
252
Weilin Xu4420c1d2023-06-21 22:49:04 +0000253 {
254 std::lock_guard<std::mutex> lk(mLock);
255 mCurrentProgramInfo = info;
256 }
257
258 mOnCurrentProgramInfoChangedFlag.notify();
259 return ndk::ScopedAStatus::ok();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000260}
261
Weilin Xu4420c1d2023-06-21 22:49:04 +0000262ScopedAStatus TunerCallbackImpl::onProgramListUpdated(const ProgramListChunk& chunk) {
263 LOG(DEBUG) << "onProgramListUpdated called";
264 {
265 std::lock_guard<std::mutex> lk(mLock);
266 updateProgramList(chunk, &mProgramList);
267 }
Weilin Xub23d0ea2022-05-09 18:26:23 +0000268
269 if (chunk.complete) {
Weilin Xu4420c1d2023-06-21 22:49:04 +0000270 mOnProgramListReadyFlag.notify();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000271 }
272
273 return ndk::ScopedAStatus::ok();
274}
275
Weilin Xu4420c1d2023-06-21 22:49:04 +0000276ScopedAStatus TunerCallbackImpl::onParametersUpdated(
277 [[maybe_unused]] const vector<VendorKeyValue>& parameters) {
278 return ndk::ScopedAStatus::ok();
279}
280
281ScopedAStatus TunerCallbackImpl::onAntennaStateChange(bool connected) {
282 if (!connected) {
283 std::lock_guard<std::mutex> lk(mLock);
284 mAntennaConnectionState = false;
285 }
286 return ndk::ScopedAStatus::ok();
287}
288
289ScopedAStatus TunerCallbackImpl::onConfigFlagUpdated([[maybe_unused]] ConfigFlag in_flag,
290 [[maybe_unused]] bool in_value) {
291 return ndk::ScopedAStatus::ok();
292}
293
294bool TunerCallbackImpl::waitOnCurrentProgramInfoChangedCallback() {
295 return mOnCurrentProgramInfoChangedFlag.wait();
296}
297
298bool TunerCallbackImpl::waitProgramReady() {
299 return mOnProgramListReadyFlag.wait();
300}
301
302void TunerCallbackImpl::reset() {
303 mOnCurrentProgramInfoChangedFlag.reset();
304 mOnProgramListReadyFlag.reset();
305}
306
307bool TunerCallbackImpl::getAntennaConnectionState() {
308 std::lock_guard<std::mutex> lk(mLock);
309 return mAntennaConnectionState;
310}
311
312ProgramInfo TunerCallbackImpl::getCurrentProgramInfo() {
313 std::lock_guard<std::mutex> lk(mLock);
314 return mCurrentProgramInfo;
315}
316
317bcutils::ProgramInfoSet TunerCallbackImpl::getProgramList() {
318 std::lock_guard<std::mutex> lk(mLock);
319 return mProgramList;
320}
321
Weilin Xub23d0ea2022-05-09 18:26:23 +0000322void BroadcastRadioHalTest::SetUp() {
323 EXPECT_EQ(mModule.get(), nullptr) << "Module is already open";
324
325 // lookup AIDL service (radio module)
326 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
327 ASSERT_NE(binder, nullptr);
328 mModule = IBroadcastRadio::fromBinder(ndk::SpAIBinder(binder));
329 ASSERT_NE(mModule, nullptr) << "Couldn't find broadcast radio HAL implementation";
330
331 // get module properties
332 auto propResult = mModule->getProperties(&mProperties);
333
334 ASSERT_TRUE(propResult.isOk());
335 EXPECT_FALSE(mProperties.maker.empty());
336 EXPECT_FALSE(mProperties.product.empty());
337 EXPECT_GT(mProperties.supportedIdentifierTypes.size(), 0u);
338
Weilin Xu25409e52023-09-06 10:36:24 -0700339 // get AIDL HAL version
340 ASSERT_TRUE(mModule->getInterfaceVersion(&mAidlVersion).isOk());
341 EXPECT_GE(mAidlVersion, kAidlVersion1);
342 EXPECT_LE(mAidlVersion, kAidlVersion2);
Weilin Xu4420c1d2023-06-21 22:49:04 +0000343
Weilin Xub23d0ea2022-05-09 18:26:23 +0000344 // set callback
Weilin Xu25409e52023-09-06 10:36:24 -0700345 mCallback = SharedRefBase::make<TunerCallbackImpl>(mAidlVersion);
Weilin Xub23d0ea2022-05-09 18:26:23 +0000346 EXPECT_TRUE(mModule->setTunerCallback(mCallback).isOk());
347}
348
349void BroadcastRadioHalTest::TearDown() {
350 if (mModule) {
351 ASSERT_TRUE(mModule->unsetTunerCallback().isOk());
352 }
Weilin Xu4420c1d2023-06-21 22:49:04 +0000353 if (mCallback) {
354 // we expect the antenna is connected through the whole test
355 EXPECT_TRUE(mCallback->getAntennaConnectionState());
356 mCallback = nullptr;
357 }
Weilin Xub23d0ea2022-05-09 18:26:23 +0000358}
359
360bool BroadcastRadioHalTest::getAmFmRegionConfig(bool full, AmFmRegionConfig* config) {
361 auto halResult = mModule->getAmFmRegionConfig(full, config);
362
363 if (halResult.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
364 return false;
365 }
366
367 EXPECT_TRUE(halResult.isOk());
368 return halResult.isOk();
369}
370
371std::optional<bcutils::ProgramInfoSet> BroadcastRadioHalTest::getProgramList() {
372 ProgramFilter emptyFilter = {};
373 return getProgramList(emptyFilter);
374}
375
376std::optional<bcutils::ProgramInfoSet> BroadcastRadioHalTest::getProgramList(
377 const ProgramFilter& filter) {
Weilin Xu4420c1d2023-06-21 22:49:04 +0000378 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000379
380 auto startResult = mModule->startProgramListUpdates(filter);
381
382 if (startResult.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
Weilin Xuc3301592023-12-06 16:45:24 -0800383 LOG(WARNING) << "Program list not supported";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000384 return std::nullopt;
385 }
386 EXPECT_TRUE(startResult.isOk());
387 if (!startResult.isOk()) {
388 return std::nullopt;
389 }
Weilin Xu4420c1d2023-06-21 22:49:04 +0000390 EXPECT_TRUE(mCallback->waitProgramReady());
Weilin Xub23d0ea2022-05-09 18:26:23 +0000391
392 auto stopResult = mModule->stopProgramListUpdates();
393
394 EXPECT_TRUE(stopResult.isOk());
395
Weilin Xu4420c1d2023-06-21 22:49:04 +0000396 return mCallback->getProgramList();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000397}
398
399/**
400 * Test setting tuner callback to null.
401 *
402 * Verifies that:
403 * - Setting to a null tuner callback results with INVALID_ARGUMENTS.
404 */
405TEST_P(BroadcastRadioHalTest, TunerCallbackFailsWithNull) {
406 LOG(DEBUG) << "TunerCallbackFailsWithNull Test";
407
408 auto halResult = mModule->setTunerCallback(nullptr);
409
410 EXPECT_EQ(halResult.getServiceSpecificError(), resultToInt(Result::INVALID_ARGUMENTS));
411}
412
413/**
Weilin Xu3277a212022-09-01 19:08:17 +0000414 * Test fetching AM/FM regional configuration.
415 *
416 * Verifies that:
417 * - AM/FM regional configuration is either set at startup or not supported at all by the hardware;
418 * - FM Deemphasis and RDS are correctly configured for FM-capable radio;
419 */
420TEST_P(BroadcastRadioHalTest, GetAmFmRegionConfig) {
421 LOG(DEBUG) << "GetAmFmRegionConfig Test";
422
423 AmFmRegionConfig config;
424
425 bool supported = getAmFmRegionConfig(/* full= */ false, &config);
426
427 if (!supported) {
Weilin Xuc3301592023-12-06 16:45:24 -0800428 GTEST_SKIP() << "AM/FM not supported";
Weilin Xu3277a212022-09-01 19:08:17 +0000429 }
430
431 EXPECT_LE(popcountll(static_cast<unsigned long long>(config.fmDeemphasis)), 1);
432 EXPECT_LE(popcountll(static_cast<unsigned long long>(config.fmRds)), 1);
433
434 if (supportsFM(config)) {
435 EXPECT_EQ(popcountll(static_cast<unsigned long long>(config.fmDeemphasis)), 1);
436 }
437}
438
439/**
440 * Test fetching ranges of AM/FM regional configuration.
441 *
442 * Verifies that:
443 * - AM/FM regional configuration is either set at startup or not supported at all by the hardware;
444 * - there is at least one AM/FM band configured;
445 * - all channel grids (frequency ranges and spacings) are valid;
446 * - seek spacing is a multiple of the manual spacing value.
447 */
448TEST_P(BroadcastRadioHalTest, GetAmFmRegionConfigRanges) {
449 LOG(DEBUG) << "GetAmFmRegionConfigRanges Test";
450
451 AmFmRegionConfig config;
452
453 bool supported = getAmFmRegionConfig(/* full= */ false, &config);
454
455 if (!supported) {
Weilin Xuc3301592023-12-06 16:45:24 -0800456 GTEST_SKIP() << "AM/FM not supported";
Weilin Xu3277a212022-09-01 19:08:17 +0000457 }
458
459 EXPECT_GT(config.ranges.size(), 0u);
460 for (const auto& range : config.ranges) {
Weilin Xu25409e52023-09-06 10:36:24 -0700461 validateRange(range, mAidlVersion);
Weilin Xu3277a212022-09-01 19:08:17 +0000462 EXPECT_EQ(range.seekSpacing % range.spacing, 0u);
463 EXPECT_GE(range.seekSpacing, range.spacing);
464 }
465}
466
467/**
468 * Test fetching FM regional capabilities.
469 *
470 * Verifies that:
471 * - AM/FM regional capabilities are either available or not supported at all by the hardware;
472 * - there is at least one de-emphasis filter mode supported for FM-capable radio;
473 */
474TEST_P(BroadcastRadioHalTest, GetAmFmRegionConfigCapabilitiesForFM) {
475 LOG(DEBUG) << "GetAmFmRegionConfigCapabilitiesForFM Test";
476
477 AmFmRegionConfig config;
478
479 bool supported = getAmFmRegionConfig(/* full= */ true, &config);
480
481 if (supported && supportsFM(config)) {
482 EXPECT_GE(popcountll(static_cast<unsigned long long>(config.fmDeemphasis)), 1);
483 } else {
Weilin Xuc3301592023-12-06 16:45:24 -0800484 GTEST_SKIP() << "FM not supported";
Weilin Xu3277a212022-09-01 19:08:17 +0000485 }
486}
487
488/**
489 * Test fetching the ranges of AM/FM regional capabilities.
490 *
491 * Verifies that:
492 * - AM/FM regional capabilities are either available or not supported at all by the hardware;
493 * - there is at least one AM/FM range supported;
494 * - all channel grids (frequency ranges and spacings) are valid;
495 * - seek spacing is not set.
496 */
497TEST_P(BroadcastRadioHalTest, GetAmFmRegionConfigCapabilitiesRanges) {
498 LOG(DEBUG) << "GetAmFmRegionConfigCapabilitiesRanges Test";
499
500 AmFmRegionConfig config;
501
502 bool supported = getAmFmRegionConfig(/* full= */ true, &config);
503
504 if (!supported) {
Weilin Xuc3301592023-12-06 16:45:24 -0800505 GTEST_SKIP() << "AM/FM not supported";
Weilin Xu3277a212022-09-01 19:08:17 +0000506 }
507
508 EXPECT_GT(config.ranges.size(), 0u);
509
510 for (const auto& range : config.ranges) {
Weilin Xu25409e52023-09-06 10:36:24 -0700511 validateRange(range, mAidlVersion);
Weilin Xu3277a212022-09-01 19:08:17 +0000512 EXPECT_EQ(range.seekSpacing, 0u);
513 }
514}
515
516/**
517 * Test fetching DAB regional configuration.
518 *
519 * Verifies that:
520 * - DAB regional configuration is either set at startup or not supported at all by the hardware;
521 * - all channel labels match correct format;
522 * - all channel frequencies are in correct range.
523 */
524TEST_P(BroadcastRadioHalTest, GetDabRegionConfig) {
525 LOG(DEBUG) << "GetDabRegionConfig Test";
526 vector<DabTableEntry> config;
527
528 auto halResult = mModule->getDabRegionConfig(&config);
529
530 if (halResult.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
Weilin Xuc3301592023-12-06 16:45:24 -0800531 GTEST_SKIP() << "DAB not supported";
Weilin Xu3277a212022-09-01 19:08:17 +0000532 }
533 ASSERT_TRUE(halResult.isOk());
534
535 std::regex re("^[A-Z0-9][A-Z0-9 ]{0,5}[A-Z0-9]$");
536
537 for (const auto& entry : config) {
Weilin Xu25409e52023-09-06 10:36:24 -0700538 EXPECT_TRUE(std::regex_match(std::string(entry.label), re));
Weilin Xu3277a212022-09-01 19:08:17 +0000539
540 ProgramIdentifier id =
541 bcutils::makeIdentifier(IdentifierType::DAB_FREQUENCY_KHZ, entry.frequencyKhz);
Weilin Xu25409e52023-09-06 10:36:24 -0700542 if (mAidlVersion == kAidlVersion1) {
543 EXPECT_TRUE(bcutils::isValid(id));
544 } else if (mAidlVersion == kAidlVersion2) {
545 EXPECT_TRUE(bcutils::isValidV2(id));
546 } else {
547 LOG(ERROR) << "Unknown callback AIDL version " << mAidlVersion;
548 }
Weilin Xu3277a212022-09-01 19:08:17 +0000549 }
550}
551
552/**
Weilin Xub23d0ea2022-05-09 18:26:23 +0000553 * Test tuning without tuner callback set.
554 *
555 * Verifies that:
556 * - No tuner callback set results in INVALID_STATE, regardless of whether the selector is
557 * supported.
558 */
559TEST_P(BroadcastRadioHalTest, TuneFailsWithoutTunerCallback) {
560 LOG(DEBUG) << "TuneFailsWithoutTunerCallback Test";
561
562 mModule->unsetTunerCallback();
563 int64_t freq = 90900; // 90.9 FM
564 ProgramSelector sel = makeSelectorAmfm(freq);
565
566 auto result = mModule->tune(sel);
567
568 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::INVALID_STATE));
569}
570
571/**
572 * Test tuning with selectors that can be not supported.
573 *
574 * Verifies that:
575 * - if the selector is not supported, an invalid value results with NOT_SUPPORTED, regardless of
576 * whether it is valid;
577 * - if it is supported, the test is ignored;
578 */
579TEST_P(BroadcastRadioHalTest, TuneFailsWithNotSupported) {
Weilin Xu4420c1d2023-06-21 22:49:04 +0000580 LOG(DEBUG) << "TuneFailsWithNotSupported Test";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000581
582 vector<ProgramIdentifier> supportTestId = {
583 makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, 0), // invalid
584 makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, 94900), // valid
585 makeIdentifier(IdentifierType::RDS_PI, 0x10000), // invalid
586 makeIdentifier(IdentifierType::RDS_PI, 0x1001), // valid
587 makeIdentifier(IdentifierType::HD_STATION_ID_EXT, 0x100000000), // invalid
588 makeIdentifier(IdentifierType::HD_STATION_ID_EXT, 0x10000001), // valid
589 makeIdentifier(IdentifierType::DAB_SID_EXT, 0), // invalid
590 makeIdentifier(IdentifierType::DAB_SID_EXT, 0xA00001), // valid
591 makeIdentifier(IdentifierType::DRMO_SERVICE_ID, 0x100000000), // invalid
592 makeIdentifier(IdentifierType::DRMO_SERVICE_ID, 0x10000001), // valid
593 makeIdentifier(IdentifierType::SXM_SERVICE_ID, 0x100000000), // invalid
594 makeIdentifier(IdentifierType::SXM_SERVICE_ID, 0x10000001), // valid
595 };
596
597 auto notSupportedError = resultToInt(Result::NOT_SUPPORTED);
598 for (const auto& id : supportTestId) {
599 ProgramSelector sel{id, {}};
600
Weilin Xub23d0ea2022-05-09 18:26:23 +0000601 if (!bcutils::isSupported(mProperties, sel)) {
Weilin Xu4420c1d2023-06-21 22:49:04 +0000602 auto result = mModule->tune(sel);
603
Weilin Xub23d0ea2022-05-09 18:26:23 +0000604 EXPECT_EQ(result.getServiceSpecificError(), notSupportedError);
605 }
606 }
607}
608
609/**
610 * Test tuning with invalid selectors.
611 *
612 * Verifies that:
613 * - if the selector is not supported, it's ignored;
614 * - if it is supported, an invalid value results with INVALID_ARGUMENTS;
615 */
616TEST_P(BroadcastRadioHalTest, TuneFailsWithInvalid) {
617 LOG(DEBUG) << "TuneFailsWithInvalid Test";
618
619 vector<ProgramIdentifier> invalidId = {
620 makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, 0),
621 makeIdentifier(IdentifierType::RDS_PI, 0x10000),
622 makeIdentifier(IdentifierType::HD_STATION_ID_EXT, 0x100000000),
623 makeIdentifier(IdentifierType::DAB_SID_EXT, 0),
624 makeIdentifier(IdentifierType::DRMO_SERVICE_ID, 0x100000000),
625 makeIdentifier(IdentifierType::SXM_SERVICE_ID, 0x100000000),
626 };
627
628 auto invalidArgumentsError = resultToInt(Result::INVALID_ARGUMENTS);
629 for (const auto& id : invalidId) {
630 ProgramSelector sel{id, {}};
631
Weilin Xub23d0ea2022-05-09 18:26:23 +0000632 if (bcutils::isSupported(mProperties, sel)) {
Weilin Xu4420c1d2023-06-21 22:49:04 +0000633 auto result = mModule->tune(sel);
634
Weilin Xub23d0ea2022-05-09 18:26:23 +0000635 EXPECT_EQ(result.getServiceSpecificError(), invalidArgumentsError);
636 }
637 }
638}
639
640/**
641 * Test tuning with empty program selector.
642 *
643 * Verifies that:
644 * - tune fails with NOT_SUPPORTED when program selector is not initialized.
645 */
646TEST_P(BroadcastRadioHalTest, TuneFailsWithEmpty) {
647 LOG(DEBUG) << "TuneFailsWithEmpty Test";
648
649 // Program type is 1-based, so 0 will always be invalid.
650 ProgramSelector sel = {};
651
652 auto result = mModule->tune(sel);
653
654 ASSERT_EQ(result.getServiceSpecificError(), resultToInt(Result::NOT_SUPPORTED));
655}
656
657/**
658 * Test tuning with FM selector.
659 *
660 * Verifies that:
661 * - if AM/FM selector is not supported, the method returns NOT_SUPPORTED;
662 * - if it is supported, the method succeeds;
663 * - after a successful tune call, onCurrentProgramInfoChanged callback is
664 * invoked carrying a proper selector;
Weilin Xuc3301592023-12-06 16:45:24 -0800665 * - program changes to a program info with the program selector requested.
Weilin Xub23d0ea2022-05-09 18:26:23 +0000666 */
667TEST_P(BroadcastRadioHalTest, FmTune) {
668 LOG(DEBUG) << "FmTune Test";
669
670 int64_t freq = 90900; // 90.9 FM
671 ProgramSelector sel = makeSelectorAmfm(freq);
672 // try tuning
Weilin Xu4420c1d2023-06-21 22:49:04 +0000673 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000674 auto result = mModule->tune(sel);
675
676 // expect a failure if it's not supported
677 if (!bcutils::isSupported(mProperties, sel)) {
678 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::NOT_SUPPORTED));
679 return;
680 }
681
682 // expect a callback if it succeeds
683 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000684 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
685 ProgramInfo infoCb = mCallback->getCurrentProgramInfo();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000686
687 LOG(DEBUG) << "Current program info: " << infoCb.toString();
688
689 // it should tune exactly to what was requested
690 vector<int> freqs = bcutils::getAllIds(infoCb.selector, IdentifierType::AMFM_FREQUENCY_KHZ);
691 EXPECT_NE(freqs.end(), find(freqs.begin(), freqs.end(), freq))
692 << "FM freq " << freq << " kHz is not sent back by callback.";
693}
694
695/**
Weilin Xu3bab6132023-12-06 16:07:43 -0800696 * Test tuning with HD selector.
697 *
698 * Verifies that:
699 * - if AM/FM HD selector is not supported, the method returns NOT_SUPPORTED;
700 * - if it is supported, the method succeeds;
701 * - after a successful tune call, onCurrentProgramInfoChanged callback is
702 * invoked carrying a proper selector;
703 * - program changes to a program info with the program selector requested.
704 */
705TEST_P(BroadcastRadioHalTest, HdTune) {
706 LOG(DEBUG) << "HdTune Test";
707 auto programList = getProgramList();
708 if (!programList) {
Weilin Xuc3301592023-12-06 16:45:24 -0800709 GTEST_SKIP() << "Empty station list, tune cannot be performed";
Weilin Xu3bab6132023-12-06 16:07:43 -0800710 }
711 ProgramSelector hdSel = {};
712 ProgramIdentifier physicallyTunedToExpected = {};
713 bool hdStationPresent = false;
714 for (auto&& programInfo : *programList) {
715 if (programInfo.selector.primaryId.type != IdentifierType::HD_STATION_ID_EXT) {
716 continue;
717 }
718 hdSel = programInfo.selector;
719 hdStationPresent = true;
720 physicallyTunedToExpected = bcutils::makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ,
721 bcutils::getAmFmFrequency(hdSel));
722 break;
723 }
724 if (!hdStationPresent) {
Weilin Xuc3301592023-12-06 16:45:24 -0800725 GTEST_SKIP() << "No HD stations in the list, tune cannot be performed";
Weilin Xu3bab6132023-12-06 16:07:43 -0800726 }
727
728 // try tuning
729 auto result = mModule->tune(hdSel);
730
731 // expect a failure if it's not supported
732 if (!bcutils::isSupported(mProperties, hdSel)) {
733 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::NOT_SUPPORTED));
734 return;
735 }
736 // expect a callback if it succeeds
737 EXPECT_TRUE(result.isOk());
738 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
739 ProgramInfo infoCb = mCallback->getCurrentProgramInfo();
740 LOG(DEBUG) << "Current program info: " << infoCb.toString();
741 // it should tune exactly to what was requested
Weilin Xuaba14a22023-12-13 13:11:22 -0800742 EXPECT_EQ(infoCb.selector.primaryId, hdSel.primaryId);
Weilin Xu3bab6132023-12-06 16:07:43 -0800743 EXPECT_EQ(infoCb.physicallyTunedTo, physicallyTunedToExpected);
744}
745
746/**
Weilin Xub23d0ea2022-05-09 18:26:23 +0000747 * Test tuning with DAB selector.
748 *
749 * Verifies that:
750 * - if DAB selector is not supported, the method returns NOT_SUPPORTED;
751 * - if it is supported, the method succeeds;
752 * - after a successful tune call, onCurrentProgramInfoChanged callback is
753 * invoked carrying a proper selector;
Weilin Xuc3301592023-12-06 16:45:24 -0800754 * - program changes to a program info with the program selector requested.
Weilin Xub23d0ea2022-05-09 18:26:23 +0000755 */
756TEST_P(BroadcastRadioHalTest, DabTune) {
757 LOG(DEBUG) << "DabTune Test";
758 vector<DabTableEntry> config;
759
760 auto halResult = mModule->getDabRegionConfig(&config);
761
762 if (halResult.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
Weilin Xuc3301592023-12-06 16:45:24 -0800763 GTEST_SKIP() << "DAB not supported";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000764 }
765 ASSERT_TRUE(halResult.isOk());
766 ASSERT_NE(config.size(), 0U);
767
Weilin Xu0d4207d2022-12-09 00:37:44 +0000768 auto programList = getProgramList();
769
770 if (!programList) {
Weilin Xuc3301592023-12-06 16:45:24 -0800771 GTEST_SKIP() << "Empty DAB station list, tune cannot be performed";
Weilin Xu0d4207d2022-12-09 00:37:44 +0000772 }
773
Weilin Xub23d0ea2022-05-09 18:26:23 +0000774 ProgramSelector sel = {};
Weilin Xu0d4207d2022-12-09 00:37:44 +0000775 uint64_t freq = 0;
776 bool dabStationPresent = false;
777 for (auto&& programInfo : *programList) {
778 if (!utils::hasId(programInfo.selector, IdentifierType::DAB_FREQUENCY_KHZ)) {
779 continue;
780 }
781 for (auto&& config_entry : config) {
782 if (config_entry.frequencyKhz ==
783 utils::getId(programInfo.selector, IdentifierType::DAB_FREQUENCY_KHZ, 0)) {
784 freq = config_entry.frequencyKhz;
785 break;
786 }
787 }
788 // Do not trigger a tune request if the programList entry does not contain
789 // a valid DAB frequency.
790 if (freq == 0) {
791 continue;
792 }
793 int64_t dabSidExt = utils::getId(programInfo.selector, IdentifierType::DAB_SID_EXT, 0);
794 int64_t dabEns = utils::getId(programInfo.selector, IdentifierType::DAB_ENSEMBLE, 0);
795 sel = makeSelectorDab(dabSidExt, (int32_t)dabEns, freq);
796 dabStationPresent = true;
797 break;
798 }
799
800 if (!dabStationPresent) {
Weilin Xuc3301592023-12-06 16:45:24 -0800801 GTEST_SKIP() << "No DAB stations in the list, tune cannot be performed";
Weilin Xu0d4207d2022-12-09 00:37:44 +0000802 }
Weilin Xub23d0ea2022-05-09 18:26:23 +0000803
804 // try tuning
Weilin Xub23d0ea2022-05-09 18:26:23 +0000805
806 auto result = mModule->tune(sel);
807
808 // expect a failure if it's not supported
809 if (!bcutils::isSupported(mProperties, sel)) {
810 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::NOT_SUPPORTED));
811 return;
812 }
813
814 // expect a callback if it succeeds
815 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000816 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
817 ProgramInfo infoCb = mCallback->getCurrentProgramInfo();
818
Weilin Xub23d0ea2022-05-09 18:26:23 +0000819 LOG(DEBUG) << "Current program info: " << infoCb.toString();
820
821 // it should tune exactly to what was requested
822 vector<int> freqs = bcutils::getAllIds(infoCb.selector, IdentifierType::DAB_FREQUENCY_KHZ);
823 EXPECT_NE(freqs.end(), find(freqs.begin(), freqs.end(), freq))
824 << "DAB freq " << freq << " kHz is not sent back by callback.";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000825}
826
827/**
828 * Test seeking to next/prev station via IBroadcastRadio::seek().
829 *
830 * Verifies that:
831 * - the method succeeds;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000832 * - the program info is changed within kTuneTimeoutMs;
Weilin Xuc3301592023-12-06 16:45:24 -0800833 * - works both directions and with or without ing sub-channel.
Weilin Xub23d0ea2022-05-09 18:26:23 +0000834 */
835TEST_P(BroadcastRadioHalTest, Seek) {
836 LOG(DEBUG) << "Seek Test";
837
Weilin Xu4420c1d2023-06-21 22:49:04 +0000838 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000839
840 auto result = mModule->seek(/* in_directionUp= */ true, /* in_skipSubChannel= */ true);
841
842 if (result.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
Weilin Xuc3301592023-12-06 16:45:24 -0800843 GTEST_SKIP() << "Seek not supported";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000844 }
845
846 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000847 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
Weilin Xub23d0ea2022-05-09 18:26:23 +0000848
Weilin Xu4420c1d2023-06-21 22:49:04 +0000849 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000850
851 result = mModule->seek(/* in_directionUp= */ false, /* in_skipSubChannel= */ false);
852
853 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000854 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
Weilin Xub23d0ea2022-05-09 18:26:23 +0000855}
856
857/**
858 * Test seeking without tuner callback set.
859 *
860 * Verifies that:
861 * - No tuner callback set results in INVALID_STATE.
862 */
863TEST_P(BroadcastRadioHalTest, SeekFailsWithoutTunerCallback) {
864 LOG(DEBUG) << "SeekFailsWithoutTunerCallback Test";
865
866 mModule->unsetTunerCallback();
867
868 auto result = mModule->seek(/* in_directionUp= */ true, /* in_skipSubChannel= */ true);
869
870 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::INVALID_STATE));
871
872 result = mModule->seek(/* in_directionUp= */ false, /* in_skipSubChannel= */ false);
873
874 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::INVALID_STATE));
875}
876
877/**
878 * Test step operation.
879 *
880 * Verifies that:
881 * - the method succeeds or returns NOT_SUPPORTED;
Weilin Xu4420c1d2023-06-21 22:49:04 +0000882 * - the program info is changed within kTuneTimeoutMs if the method succeeded;
Weilin Xub23d0ea2022-05-09 18:26:23 +0000883 * - works both directions.
884 */
885TEST_P(BroadcastRadioHalTest, Step) {
886 LOG(DEBUG) << "Step Test";
887
Weilin Xu4420c1d2023-06-21 22:49:04 +0000888 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000889
890 auto result = mModule->step(/* in_directionUp= */ true);
891
892 if (result.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
Weilin Xuc3301592023-12-06 16:45:24 -0800893 GTEST_SKIP() << "Step not supported";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000894 }
895 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000896 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
Weilin Xub23d0ea2022-05-09 18:26:23 +0000897
Weilin Xu4420c1d2023-06-21 22:49:04 +0000898 mCallback->reset();
Weilin Xub23d0ea2022-05-09 18:26:23 +0000899
900 result = mModule->step(/* in_directionUp= */ false);
901
902 EXPECT_TRUE(result.isOk());
Weilin Xu4420c1d2023-06-21 22:49:04 +0000903 EXPECT_TRUE(mCallback->waitOnCurrentProgramInfoChangedCallback());
Weilin Xub23d0ea2022-05-09 18:26:23 +0000904}
905
906/**
907 * Test step operation without tuner callback set.
908 *
909 * Verifies that:
910 * - No tuner callback set results in INVALID_STATE.
911 */
912TEST_P(BroadcastRadioHalTest, StepFailsWithoutTunerCallback) {
913 LOG(DEBUG) << "StepFailsWithoutTunerCallback Test";
914
915 mModule->unsetTunerCallback();
916
917 auto result = mModule->step(/* in_directionUp= */ true);
918
919 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::INVALID_STATE));
920
921 result = mModule->step(/* in_directionUp= */ false);
922
923 EXPECT_EQ(result.getServiceSpecificError(), resultToInt(Result::INVALID_STATE));
924}
925
926/**
927 * Test tune cancellation.
928 *
929 * Verifies that:
930 * - the method does not crash after being invoked multiple times.
931 *
932 * Since cancel() might be called after the HAL completes an operation (tune, seek, and step)
933 * and before the callback completions, the operation might not be actually canceled and the
934 * effect of cancel() is not deterministic to be tested here.
935 */
936TEST_P(BroadcastRadioHalTest, Cancel) {
937 LOG(DEBUG) << "Cancel Test";
938
939 auto notSupportedError = resultToInt(Result::NOT_SUPPORTED);
940 for (int i = 0; i < 10; i++) {
941 auto result = mModule->seek(/* in_directionUp= */ true, /* in_skipSubChannel= */ true);
942
943 if (result.getServiceSpecificError() == notSupportedError) {
Weilin Xuc3301592023-12-06 16:45:24 -0800944 GTEST_SKIP() << "Cancel is skipped because of seek not supported";
Weilin Xub23d0ea2022-05-09 18:26:23 +0000945 }
946 EXPECT_TRUE(result.isOk());
947
948 auto cancelResult = mModule->cancel();
949
950 ASSERT_TRUE(cancelResult.isOk());
951 }
952}
953
954/**
Weilin Xu3277a212022-09-01 19:08:17 +0000955 * Test IBroadcastRadio::get|setParameters() methods called with no parameters.
956 *
957 * Verifies that:
958 * - callback is called for empty parameters set.
959 */
960TEST_P(BroadcastRadioHalTest, NoParameters) {
961 LOG(DEBUG) << "NoParameters Test";
962
963 vector<VendorKeyValue> parametersResults = {};
964
965 auto halResult = mModule->setParameters({}, &parametersResults);
966
967 ASSERT_TRUE(halResult.isOk());
968 ASSERT_EQ(parametersResults.size(), 0u);
969
970 parametersResults.clear();
971
972 halResult = mModule->getParameters({}, &parametersResults);
973
974 ASSERT_TRUE(halResult.isOk());
975 ASSERT_EQ(parametersResults.size(), 0u);
976}
977
978/**
979 * Test IBroadcastRadio::get|setParameters() methods called with unknown parameters.
980 *
981 * Verifies that:
982 * - unknown parameters are ignored;
983 * - callback is called also for empty results set.
984 */
985TEST_P(BroadcastRadioHalTest, UnknownParameters) {
986 LOG(DEBUG) << "UnknownParameters Test";
987
988 vector<VendorKeyValue> parametersResults = {};
989
990 auto halResult =
991 mModule->setParameters({{"com.android.unknown", "sample"}}, &parametersResults);
992
993 ASSERT_TRUE(halResult.isOk());
994 ASSERT_EQ(parametersResults.size(), 0u);
995
996 parametersResults.clear();
997
998 halResult = mModule->getParameters({"com.android.unknown*", "sample"}, &parametersResults);
999
1000 ASSERT_TRUE(halResult.isOk());
1001 ASSERT_EQ(parametersResults.size(), 0u);
1002}
1003
1004/**
1005 * Test geting image of invalid ID.
1006 *
1007 * Verifies that:
1008 * - getImage call handles argument 0 gracefully.
1009 */
1010TEST_P(BroadcastRadioHalTest, GetNoImage) {
1011 LOG(DEBUG) << "GetNoImage Test";
1012 vector<uint8_t> rawImage;
1013
1014 auto result = mModule->getImage(IBroadcastRadio::INVALID_IMAGE, &rawImage);
1015
1016 ASSERT_TRUE(result.isOk());
1017 ASSERT_EQ(rawImage.size(), 0u);
1018}
1019
1020/**
1021 * Test getting config flags.
1022 *
1023 * Verifies that:
1024 * - isConfigFlagSet either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
1025 * - call success or failure is consistent with setConfigFlag.
1026 */
1027TEST_P(BroadcastRadioHalTest, FetchConfigFlags) {
1028 LOG(DEBUG) << "FetchConfigFlags Test";
1029
1030 for (const auto& flag : kConfigFlagValues) {
1031 bool gotValue = false;
1032
1033 auto halResult = mModule->isConfigFlagSet(flag, &gotValue);
1034
1035 if (halResult.getServiceSpecificError() != resultToInt(Result::NOT_SUPPORTED) &&
1036 halResult.getServiceSpecificError() != resultToInt(Result::INVALID_STATE)) {
1037 ASSERT_TRUE(halResult.isOk());
1038 }
1039
1040 // set must fail or succeed the same way as get
1041 auto setResult = mModule->setConfigFlag(flag, /* value= */ false);
1042
1043 EXPECT_TRUE((halResult.isOk() && setResult.isOk()) ||
1044 (halResult.getServiceSpecificError()) == setResult.getServiceSpecificError());
1045
1046 setResult = mModule->setConfigFlag(flag, /* value= */ true);
1047
1048 EXPECT_TRUE((halResult.isOk() && setResult.isOk()) ||
1049 (halResult.getServiceSpecificError()) == setResult.getServiceSpecificError());
1050 }
1051}
1052
1053/**
1054 * Test setting config flags.
1055 *
1056 * Verifies that:
1057 * - setConfigFlag either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
1058 * - isConfigFlagSet reflects the state requested immediately after the set call.
1059 */
1060TEST_P(BroadcastRadioHalTest, SetConfigFlags) {
1061 LOG(DEBUG) << "SetConfigFlags Test";
1062
1063 auto get = [&](ConfigFlag flag) -> bool {
Weilin Xu978de0a2023-07-19 17:20:19 +00001064 bool gotValue;
Weilin Xu3277a212022-09-01 19:08:17 +00001065
Weilin Xu978de0a2023-07-19 17:20:19 +00001066 auto halResult = mModule->isConfigFlagSet(flag, &gotValue);
Weilin Xu3277a212022-09-01 19:08:17 +00001067
Weilin Xu3277a212022-09-01 19:08:17 +00001068 EXPECT_TRUE(halResult.isOk());
Weilin Xu978de0a2023-07-19 17:20:19 +00001069 return gotValue;
Weilin Xu3277a212022-09-01 19:08:17 +00001070 };
1071
1072 auto notSupportedError = resultToInt(Result::NOT_SUPPORTED);
1073 auto invalidStateError = resultToInt(Result::INVALID_STATE);
1074 for (const auto& flag : kConfigFlagValues) {
1075 auto result = mModule->setConfigFlag(flag, /* value= */ false);
1076
1077 if (result.getServiceSpecificError() == notSupportedError ||
1078 result.getServiceSpecificError() == invalidStateError) {
1079 // setting to true must result in the same error as false
1080 auto secondResult = mModule->setConfigFlag(flag, /* value= */ true);
1081
1082 EXPECT_TRUE((result.isOk() && secondResult.isOk()) ||
1083 result.getServiceSpecificError() == secondResult.getServiceSpecificError());
1084 continue;
1085 } else {
1086 ASSERT_TRUE(result.isOk());
1087 }
1088
1089 // verify false is set
1090 bool value = get(flag);
1091 EXPECT_FALSE(value);
1092
1093 // try setting true this time
1094 result = mModule->setConfigFlag(flag, /* value= */ true);
1095
1096 ASSERT_TRUE(result.isOk());
1097 value = get(flag);
1098 EXPECT_TRUE(value);
1099
1100 // false again
1101 result = mModule->setConfigFlag(flag, /* value= */ false);
1102
1103 ASSERT_TRUE(result.isOk());
1104 value = get(flag);
1105 EXPECT_FALSE(value);
1106 }
1107}
1108
1109/**
Weilin Xub23d0ea2022-05-09 18:26:23 +00001110 * Test getting program list using empty program filter.
1111 *
1112 * Verifies that:
1113 * - startProgramListUpdates either succeeds or returns NOT_SUPPORTED;
Weilin Xu4420c1d2023-06-21 22:49:04 +00001114 * - the complete list is fetched within kProgramListScanTimeoutMs;
Weilin Xub23d0ea2022-05-09 18:26:23 +00001115 * - stopProgramListUpdates does not crash.
1116 */
1117TEST_P(BroadcastRadioHalTest, GetProgramListFromEmptyFilter) {
1118 LOG(DEBUG) << "GetProgramListFromEmptyFilter Test";
1119
1120 getProgramList();
1121}
1122
1123/**
1124 * Test getting program list using AMFM frequency program filter.
1125 *
1126 * Verifies that:
1127 * - startProgramListUpdates either succeeds or returns NOT_SUPPORTED;
Weilin Xu4420c1d2023-06-21 22:49:04 +00001128 * - the complete list is fetched within kProgramListScanTimeoutMs;
Weilin Xub23d0ea2022-05-09 18:26:23 +00001129 * - stopProgramListUpdates does not crash;
1130 * - result for startProgramListUpdates using a filter with AMFM_FREQUENCY_KHZ value of the first
1131 * AMFM program matches the expected result.
1132 */
1133TEST_P(BroadcastRadioHalTest, GetProgramListFromAmFmFilter) {
1134 LOG(DEBUG) << "GetProgramListFromAmFmFilter Test";
1135
1136 std::optional<bcutils::ProgramInfoSet> completeList = getProgramList();
1137 if (!completeList) {
Weilin Xuc3301592023-12-06 16:45:24 -08001138 GTEST_SKIP() << "No program list available";
Weilin Xub23d0ea2022-05-09 18:26:23 +00001139 }
1140
1141 ProgramFilter amfmFilter = {};
1142 int expectedResultSize = 0;
1143 uint64_t expectedFreq = 0;
1144 for (const auto& program : *completeList) {
1145 vector<int> amfmIds =
1146 bcutils::getAllIds(program.selector, IdentifierType::AMFM_FREQUENCY_KHZ);
1147 EXPECT_LE(amfmIds.size(), 1u);
1148 if (amfmIds.size() == 0) {
1149 continue;
1150 }
1151
1152 if (expectedResultSize == 0) {
1153 expectedFreq = amfmIds[0];
1154 amfmFilter.identifiers = {
1155 makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ, expectedFreq)};
1156 expectedResultSize = 1;
1157 } else if (amfmIds[0] == expectedFreq) {
1158 expectedResultSize++;
1159 }
1160 }
1161
1162 if (expectedResultSize == 0) {
Weilin Xuc3301592023-12-06 16:45:24 -08001163 GTEST_SKIP() << "No Am/FM programs available";
Weilin Xub23d0ea2022-05-09 18:26:23 +00001164 }
1165 std::optional<bcutils::ProgramInfoSet> amfmList = getProgramList(amfmFilter);
1166 ASSERT_EQ(amfmList->size(), expectedResultSize) << "amfm filter result size is wrong";
1167}
1168
1169/**
1170 * Test getting program list using DAB ensemble program filter.
1171 *
1172 * Verifies that:
1173 * - startProgramListUpdates either succeeds or returns NOT_SUPPORTED;
Weilin Xu4420c1d2023-06-21 22:49:04 +00001174 * - the complete list is fetched within kProgramListScanTimeoutMs;
Weilin Xub23d0ea2022-05-09 18:26:23 +00001175 * - stopProgramListUpdates does not crash;
1176 * - result for startProgramListUpdates using a filter with DAB_ENSEMBLE value of the first DAB
1177 * program matches the expected result.
1178 */
1179TEST_P(BroadcastRadioHalTest, GetProgramListFromDabFilter) {
1180 LOG(DEBUG) << "GetProgramListFromDabFilter Test";
1181
1182 std::optional<bcutils::ProgramInfoSet> completeList = getProgramList();
1183 if (!completeList) {
Weilin Xuc3301592023-12-06 16:45:24 -08001184 GTEST_SKIP() << "No program list available";
Weilin Xub23d0ea2022-05-09 18:26:23 +00001185 }
1186
1187 ProgramFilter dabFilter = {};
1188 int expectedResultSize = 0;
1189 uint64_t expectedEnsemble = 0;
1190 for (const auto& program : *completeList) {
1191 auto dabEnsembles = bcutils::getAllIds(program.selector, IdentifierType::DAB_ENSEMBLE);
1192 EXPECT_LE(dabEnsembles.size(), 1u);
1193 if (dabEnsembles.size() == 0) {
1194 continue;
1195 }
1196
1197 if (expectedResultSize == 0) {
1198 expectedEnsemble = dabEnsembles[0];
1199 dabFilter.identifiers = {
1200 makeIdentifier(IdentifierType::DAB_ENSEMBLE, expectedEnsemble)};
1201 expectedResultSize = 1;
1202 } else if (dabEnsembles[0] == expectedEnsemble) {
1203 expectedResultSize++;
1204 }
1205 }
1206
1207 if (expectedResultSize == 0) {
Weilin Xuc3301592023-12-06 16:45:24 -08001208 GTEST_SKIP() << "No DAB programs available";
Weilin Xub23d0ea2022-05-09 18:26:23 +00001209 }
1210 std::optional<bcutils::ProgramInfoSet> dabList = getProgramList(dabFilter);
1211 ASSERT_EQ(dabList->size(), expectedResultSize) << "dab filter result size is wrong";
1212}
1213
Weilin Xu3277a212022-09-01 19:08:17 +00001214/**
1215 * Test HD_STATION_NAME correctness.
1216 *
1217 * Verifies that if a program on the list contains HD_STATION_NAME identifier:
1218 * - the program provides station name in its metadata;
1219 * - the identifier matches the name;
1220 * - there is only one identifier of that type.
1221 */
1222TEST_P(BroadcastRadioHalTest, HdRadioStationNameId) {
1223 LOG(DEBUG) << "HdRadioStationNameId Test";
1224
1225 std::optional<bcutils::ProgramInfoSet> list = getProgramList();
1226 if (!list) {
Weilin Xuc3301592023-12-06 16:45:24 -08001227 GTEST_SKIP() << "No program list";
Weilin Xu3277a212022-09-01 19:08:17 +00001228 }
1229
1230 for (const auto& program : *list) {
1231 vector<int> nameIds = bcutils::getAllIds(program.selector, IdentifierType::HD_STATION_NAME);
1232 EXPECT_LE(nameIds.size(), 1u);
1233 if (nameIds.size() == 0) {
1234 continue;
1235 }
1236
Weilin Xu25409e52023-09-06 10:36:24 -07001237 std::optional<std::string> name;
1238 if (mAidlVersion == kAidlVersion1) {
1239 name = bcutils::getMetadataString(program, Metadata::programName);
1240 if (!name) {
1241 name = bcutils::getMetadataString(program, Metadata::rdsPs);
1242 }
1243 } else if (mAidlVersion == kAidlVersion2) {
1244 name = bcutils::getMetadataStringV2(program, Metadata::programName);
1245 if (!name) {
1246 name = bcutils::getMetadataStringV2(program, Metadata::rdsPs);
1247 }
1248 } else {
1249 LOG(ERROR) << "Unknown HAL AIDL version " << mAidlVersion;
Weilin Xu3277a212022-09-01 19:08:17 +00001250 }
Weilin Xu25409e52023-09-06 10:36:24 -07001251
Weilin Xu3277a212022-09-01 19:08:17 +00001252 ASSERT_TRUE(name.has_value());
1253
1254 ProgramIdentifier expectedId = bcutils::makeHdRadioStationName(*name);
1255 EXPECT_EQ(nameIds[0], expectedId.value);
1256 }
1257}
1258
1259/**
1260 * Test announcement listener registration.
1261 *
1262 * Verifies that:
1263 * - registerAnnouncementListener either succeeds or returns NOT_SUPPORTED;
1264 * - if it succeeds, it returns a valid close handle (which is a nullptr otherwise);
1265 * - closing handle does not crash.
1266 */
1267TEST_P(BroadcastRadioHalTest, AnnouncementListenerRegistration) {
1268 LOG(DEBUG) << "AnnouncementListenerRegistration Test";
1269 std::shared_ptr<AnnouncementListenerMock> listener =
1270 SharedRefBase::make<AnnouncementListenerMock>();
1271 std::shared_ptr<ICloseHandle> closeHandle = nullptr;
1272
1273 auto halResult = mModule->registerAnnouncementListener(listener, {AnnouncementType::EMERGENCY},
1274 &closeHandle);
1275
1276 if (halResult.getServiceSpecificError() == resultToInt(Result::NOT_SUPPORTED)) {
1277 ASSERT_EQ(closeHandle.get(), nullptr);
Weilin Xuc3301592023-12-06 16:45:24 -08001278 GTEST_SKIP() << "Announcements not supported";
Weilin Xu3277a212022-09-01 19:08:17 +00001279 }
1280
1281 ASSERT_TRUE(halResult.isOk());
1282 ASSERT_NE(closeHandle.get(), nullptr);
1283
1284 closeHandle->close();
1285}
1286
Weilin Xub23d0ea2022-05-09 18:26:23 +00001287GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BroadcastRadioHalTest);
1288INSTANTIATE_TEST_SUITE_P(
1289 PerInstance, BroadcastRadioHalTest,
1290 testing::ValuesIn(::android::getAidlHalInstanceNames(IBroadcastRadio::descriptor)),
1291 ::android::PrintInstanceNameToString);
1292
1293} // namespace aidl::android::hardware::broadcastradio::vts
1294
1295int main(int argc, char** argv) {
1296 android::base::SetDefaultTag("BcRadio.vts");
1297 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1298 ::testing::InitGoogleTest(&argc, argv);
1299 ABinderProcess_setThreadPoolMaxThreadCount(4);
1300 ABinderProcess_startThreadPool();
1301 return RUN_ALL_TESTS();
1302}