blob: feb62c61352eeed4559e52104042b9fb13d8796a [file] [log] [blame]
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -08001/*
2 * Copyright (C) 2017 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 LOG_TAG "BcRadio.vts"
Tomasz Wasilczykdb902862018-01-14 17:22:03 -080018#define LOG_NDEBUG 0
19#define EGMOCK_VERBOSE 1
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080020
21#include <VtsHalHidlTargetTestBase.h>
22#include <android-base/logging.h>
23#include <android/hardware/broadcastradio/2.0/IBroadcastRadio.h>
24#include <android/hardware/broadcastradio/2.0/ITunerCallback.h>
25#include <android/hardware/broadcastradio/2.0/ITunerSession.h>
26#include <android/hardware/broadcastradio/2.0/types.h>
27#include <broadcastradio-utils-2x/Utils.h>
28#include <broadcastradio-vts-utils/call-barrier.h>
29#include <broadcastradio-vts-utils/mock-timeout.h>
30#include <broadcastradio-vts-utils/pointer-utils.h>
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080031#include <cutils/bitops.h>
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080032#include <gmock/gmock.h>
33
34#include <chrono>
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -080035#include <optional>
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080036#include <regex>
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080037
38namespace android {
39namespace hardware {
40namespace broadcastradio {
41namespace V2_0 {
42namespace vts {
43
44using namespace std::chrono_literals;
45
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080046using std::unordered_set;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080047using std::vector;
48using testing::_;
49using testing::AnyNumber;
50using testing::ByMove;
51using testing::DoAll;
52using testing::Invoke;
53using testing::SaveArg;
54
55using broadcastradio::vts::CallBarrier;
56using broadcastradio::vts::clearAndWait;
57using utils::make_identifier;
58using utils::make_selector_amfm;
59
60namespace timeout {
61
62static constexpr auto tune = 30s;
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080063static constexpr auto programListScan = 5min;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080064
65} // namespace timeout
66
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -080067static const ConfigFlag gConfigFlagValues[] = {
Tomasz Wasilczyk30240f62017-12-20 14:19:21 -080068 ConfigFlag::FORCE_MONO,
69 ConfigFlag::FORCE_ANALOG,
70 ConfigFlag::FORCE_DIGITAL,
71 ConfigFlag::RDS_AF,
72 ConfigFlag::RDS_REG,
73 ConfigFlag::DAB_DAB_LINKING,
74 ConfigFlag::DAB_FM_LINKING,
75 ConfigFlag::DAB_DAB_SOFT_LINKING,
76 ConfigFlag::DAB_FM_SOFT_LINKING,
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -080077};
78
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080079class TunerCallbackMock : public ITunerCallback {
80 public:
81 TunerCallbackMock();
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080082
83 MOCK_METHOD2(onTuneFailed, Return<void>(Result, const ProgramSelector&));
84 MOCK_TIMEOUT_METHOD1(onCurrentProgramInfoChanged, Return<void>(const ProgramInfo&));
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080085 Return<void> onProgramListUpdated(const ProgramListChunk& chunk);
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080086 MOCK_METHOD1(onAntennaStateChange, Return<void>(bool connected));
87 MOCK_METHOD1(onParametersUpdated, Return<void>(const hidl_vec<VendorKeyValue>& parameters));
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080088
89 MOCK_TIMEOUT_METHOD0(onProgramListReady, void());
90
91 std::mutex mLock;
92 utils::ProgramInfoSet mProgramList;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080093};
94
Tomasz Wasilczyk0d5ef5d2018-01-10 10:58:20 -080095struct AnnouncementListenerMock : public IAnnouncementListener {
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -080096 MOCK_METHOD1(onListUpdated, Return<void>(const hidl_vec<Announcement>&));
97};
98
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080099class BroadcastRadioHalTest : public ::testing::VtsHalHidlTargetTestBase {
100 protected:
101 virtual void SetUp() override;
102 virtual void TearDown() override;
103
104 bool openSession();
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800105 bool getAmFmRegionConfig(bool full, AmFmRegionConfig* config);
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800106 std::optional<utils::ProgramInfoSet> getProgramList();
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800107
108 sp<IBroadcastRadio> mModule;
109 Properties mProperties;
110 sp<ITunerSession> mSession;
111 sp<TunerCallbackMock> mCallback = new TunerCallbackMock();
112};
113
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800114static void printSkipped(std::string msg) {
115 std::cout << "[ SKIPPED ] " << msg << std::endl;
116}
117
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800118MATCHER_P(InfoHasId, id,
119 std::string(negation ? "does not contain" : "contains") + " " + toString(id)) {
120 auto ids = utils::getAllIds(arg.selector, utils::getType(id));
121 return ids.end() != find(ids.begin(), ids.end(), id.value);
122}
123
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800124TunerCallbackMock::TunerCallbackMock() {
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800125 EXPECT_TIMEOUT_CALL(*this, onCurrentProgramInfoChanged, _).Times(AnyNumber());
126
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800127 // we expect the antenna is connected through the whole test
128 EXPECT_CALL(*this, onAntennaStateChange(false)).Times(0);
129}
130
131Return<void> TunerCallbackMock::onProgramListUpdated(const ProgramListChunk& chunk) {
132 std::lock_guard<std::mutex> lk(mLock);
133
134 updateProgramList(mProgramList, chunk);
135
136 if (chunk.complete) onProgramListReady();
137
138 return {};
139}
140
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800141void BroadcastRadioHalTest::SetUp() {
142 EXPECT_EQ(nullptr, mModule.get()) << "Module is already open";
143
144 // lookup HIDL service (radio module)
145 mModule = getService<IBroadcastRadio>();
146 ASSERT_NE(nullptr, mModule.get()) << "Couldn't find broadcast radio HAL implementation";
147
148 // get module properties
149 auto propResult = mModule->getProperties([&](const Properties& p) { mProperties = p; });
150 ASSERT_TRUE(propResult.isOk());
151
152 EXPECT_FALSE(mProperties.maker.empty());
153 EXPECT_FALSE(mProperties.product.empty());
154 EXPECT_GT(mProperties.supportedIdentifierTypes.size(), 0u);
155}
156
157void BroadcastRadioHalTest::TearDown() {
158 mSession.clear();
159 mModule.clear();
160 clearAndWait(mCallback, 1s);
161}
162
163bool BroadcastRadioHalTest::openSession() {
164 EXPECT_EQ(nullptr, mSession.get()) << "Session is already open";
165
166 Result halResult = Result::UNKNOWN_ERROR;
167 auto openCb = [&](Result result, const sp<ITunerSession>& session) {
168 halResult = result;
169 if (result != Result::OK) return;
170 mSession = session;
171 };
172 auto hidlResult = mModule->openSession(mCallback, openCb);
173
174 EXPECT_TRUE(hidlResult.isOk());
175 EXPECT_EQ(Result::OK, halResult);
176 EXPECT_NE(nullptr, mSession.get());
177
178 return nullptr != mSession.get();
179}
180
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800181bool BroadcastRadioHalTest::getAmFmRegionConfig(bool full, AmFmRegionConfig* config) {
182 auto halResult = Result::UNKNOWN_ERROR;
183 auto cb = [&](Result result, AmFmRegionConfig configCb) {
184 halResult = result;
185 if (config) *config = configCb;
186 };
187
188 auto hidlResult = mModule->getAmFmRegionConfig(full, cb);
189 EXPECT_TRUE(hidlResult.isOk());
190
191 if (halResult == Result::NOT_SUPPORTED) return false;
192
193 EXPECT_EQ(Result::OK, halResult);
194 return halResult == Result::OK;
195}
196
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800197std::optional<utils::ProgramInfoSet> BroadcastRadioHalTest::getProgramList() {
198 EXPECT_TIMEOUT_CALL(*mCallback, onProgramListReady).Times(AnyNumber());
199
200 auto startResult = mSession->startProgramListUpdates({});
201 if (startResult == Result::NOT_SUPPORTED) {
202 printSkipped("Program list not supported");
203 return nullopt;
204 }
205 EXPECT_EQ(Result::OK, startResult);
206 if (startResult != Result::OK) return nullopt;
207
208 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onProgramListReady, timeout::programListScan);
209
210 auto stopResult = mSession->stopProgramListUpdates();
211 EXPECT_TRUE(stopResult.isOk());
212
213 return mCallback->mProgramList;
214}
215
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800216/**
217 * Test session opening.
218 *
219 * Verifies that:
220 * - the method succeeds on a first and subsequent calls;
221 * - the method succeeds when called for the second time without
222 * closing previous session.
223 */
224TEST_F(BroadcastRadioHalTest, OpenSession) {
225 // simply open session for the first time
226 ASSERT_TRUE(openSession());
227
228 // drop (without explicit close) and re-open the session
229 mSession.clear();
230 ASSERT_TRUE(openSession());
231
232 // open the second session (the first one should be forcibly closed)
233 auto secondSession = mSession;
234 mSession.clear();
235 ASSERT_TRUE(openSession());
236}
237
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800238static bool isValidAmFmFreq(uint64_t freq) {
239 auto id = utils::make_identifier(IdentifierType::AMFM_FREQUENCY, freq);
240 return utils::isValid(id);
241}
242
243static void validateRange(const AmFmBandRange& range) {
244 EXPECT_TRUE(isValidAmFmFreq(range.lowerBound));
245 EXPECT_TRUE(isValidAmFmFreq(range.upperBound));
246 EXPECT_LT(range.lowerBound, range.upperBound);
247 EXPECT_GT(range.spacing, 0u);
248 EXPECT_EQ(0u, (range.upperBound - range.lowerBound) % range.spacing);
249}
250
251static bool supportsFM(const AmFmRegionConfig& config) {
252 for (auto&& range : config.ranges) {
253 if (utils::getBand(range.lowerBound) == utils::FrequencyBand::FM) return true;
254 }
255 return false;
256}
257
258/**
259 * Test fetching AM/FM regional configuration.
260 *
261 * Verifies that:
262 * - AM/FM regional configuration is either set at startup or not supported at all by the hardware;
263 * - there is at least one AM/FM band configured;
264 * - FM Deemphasis and RDS are correctly configured for FM-capable radio;
265 * - all channel grids (frequency ranges and spacings) are valid;
266 * - scan spacing is a multiply of manual spacing value.
267 */
268TEST_F(BroadcastRadioHalTest, GetAmFmRegionConfig) {
269 AmFmRegionConfig config;
270 bool supported = getAmFmRegionConfig(false, &config);
271 if (!supported) {
272 printSkipped("AM/FM not supported");
273 return;
274 }
275
276 EXPECT_GT(config.ranges.size(), 0u);
277 EXPECT_LE(popcountll(config.fmDeemphasis), 1);
278 EXPECT_LE(popcountll(config.fmRds), 1);
279
280 for (auto&& range : config.ranges) {
281 validateRange(range);
282 EXPECT_EQ(0u, range.scanSpacing % range.spacing);
283 EXPECT_GE(range.scanSpacing, range.spacing);
284 }
285
286 if (supportsFM(config)) {
287 EXPECT_EQ(popcountll(config.fmDeemphasis), 1);
288 }
289}
290
291/**
292 * Test fetching AM/FM regional capabilities.
293 *
294 * Verifies that:
295 * - AM/FM regional capabilities are either available or not supported at all by the hardware;
296 * - there is at least one AM/FM range supported;
297 * - there is at least one de-emphasis filter mode supported for FM-capable radio;
298 * - all channel grids (frequency ranges and spacings) are valid;
299 * - scan spacing is not set.
300 */
301TEST_F(BroadcastRadioHalTest, GetAmFmRegionConfigCapabilities) {
302 AmFmRegionConfig config;
303 bool supported = getAmFmRegionConfig(true, &config);
304 if (!supported) {
305 printSkipped("AM/FM not supported");
306 return;
307 }
308
309 EXPECT_GT(config.ranges.size(), 0u);
310
311 for (auto&& range : config.ranges) {
312 validateRange(range);
313 EXPECT_EQ(0u, range.scanSpacing);
314 }
315
316 if (supportsFM(config)) {
317 EXPECT_GE(popcountll(config.fmDeemphasis), 1);
318 }
319}
320
321/**
322 * Test fetching DAB regional configuration.
323 *
324 * Verifies that:
325 * - DAB regional configuration is either set at startup or not supported at all by the hardware;
326 * - all channel labels match correct format;
327 * - all channel frequencies are in correct range.
328 */
329TEST_F(BroadcastRadioHalTest, GetDabRegionConfig) {
330 Result halResult;
331 hidl_vec<DabTableEntry> config;
332 auto cb = [&](Result result, hidl_vec<DabTableEntry> configCb) {
333 halResult = result;
334 config = configCb;
335 };
336 auto hidlResult = mModule->getDabRegionConfig(cb);
337 ASSERT_TRUE(hidlResult.isOk());
338
339 if (halResult == Result::NOT_SUPPORTED) {
340 printSkipped("DAB not supported");
341 return;
342 }
343 ASSERT_EQ(Result::OK, halResult);
344
345 std::regex re("^[A-Z0-9]{2,5}$");
346 // double-check correctness of the test
347 ASSERT_TRUE(std::regex_match("5A", re));
348 ASSERT_FALSE(std::regex_match("5a", re));
349 ASSERT_FALSE(std::regex_match("123ABC", re));
350
351 for (auto&& entry : config) {
352 EXPECT_TRUE(std::regex_match(std::string(entry.label), re));
353
354 auto id = utils::make_identifier(IdentifierType::DAB_FREQUENCY, entry.frequency);
355 EXPECT_TRUE(utils::isValid(id));
356 }
357}
358
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800359/**
360 * Test tuning with FM selector.
361 *
362 * Verifies that:
363 * - if AM/FM selector is not supported, the method returns NOT_SUPPORTED;
364 * - if it is supported, the method succeeds;
365 * - after a successful tune call, onCurrentProgramInfoChanged callback is
366 * invoked carrying a proper selector;
367 * - program changes exactly to what was requested.
368 */
369TEST_F(BroadcastRadioHalTest, FmTune) {
370 ASSERT_TRUE(openSession());
371
372 uint64_t freq = 100100; // 100.1 FM
373 auto sel = make_selector_amfm(freq);
374
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800375 /* TODO(b/69958777): there is a race condition between tune() and onCurrentProgramInfoChanged
376 * callback setting infoCb, because egmock cannot distinguish calls with different matchers
377 * (there is one here and one in callback constructor).
378 *
379 * This sleep workaround will fix default implementation, but the real HW tests will still be
380 * flaky. We probably need to implement egmock alternative based on actions.
381 */
382 std::this_thread::sleep_for(100ms);
383
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800384 // try tuning
385 ProgramInfo infoCb = {};
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800386 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged,
387 InfoHasId(utils::make_identifier(IdentifierType::AMFM_FREQUENCY, freq)))
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800388 .Times(AnyNumber())
389 .WillOnce(DoAll(SaveArg<0>(&infoCb), testing::Return(ByMove(Void()))));
390 auto result = mSession->tune(sel);
391
392 // expect a failure if it's not supported
393 if (!utils::isSupported(mProperties, sel)) {
394 EXPECT_EQ(Result::NOT_SUPPORTED, result);
395 return;
396 }
397
398 // expect a callback if it succeeds
399 EXPECT_EQ(Result::OK, result);
400 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
401
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800402 ALOGD("current program info: %s", toString(infoCb).c_str());
403
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800404 // it should tune exactly to what was requested
405 auto freqs = utils::getAllIds(infoCb.selector, IdentifierType::AMFM_FREQUENCY);
406 EXPECT_NE(freqs.end(), find(freqs.begin(), freqs.end(), freq));
407}
408
409/**
410 * Test tuning with invalid selectors.
411 *
412 * Verifies that:
413 * - if the selector is not supported, it's ignored;
414 * - if it is supported, an invalid value results with INVALID_ARGUMENTS;
415 */
416TEST_F(BroadcastRadioHalTest, TuneFailsWithInvalid) {
417 ASSERT_TRUE(openSession());
418
419 vector<ProgramIdentifier> invalid = {
420 make_identifier(IdentifierType::AMFM_FREQUENCY, 0),
421 make_identifier(IdentifierType::RDS_PI, 0x10000),
422 make_identifier(IdentifierType::HD_STATION_ID_EXT, 0x100000000),
423 make_identifier(IdentifierType::DAB_SID_EXT, 0),
424 make_identifier(IdentifierType::DRMO_SERVICE_ID, 0x100000000),
425 make_identifier(IdentifierType::SXM_SERVICE_ID, 0x100000000),
426 };
427
428 for (auto&& id : invalid) {
429 ProgramSelector sel{id, {}};
430
431 auto result = mSession->tune(sel);
432
433 if (utils::isSupported(mProperties, sel)) {
434 EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
435 } else {
436 EXPECT_EQ(Result::NOT_SUPPORTED, result);
437 }
438 }
439}
440
441/**
442 * Test tuning with empty program selector.
443 *
444 * Verifies that:
445 * - tune fails with NOT_SUPPORTED when program selector is not initialized.
446 */
447TEST_F(BroadcastRadioHalTest, TuneFailsWithEmpty) {
448 ASSERT_TRUE(openSession());
449
450 // Program type is 1-based, so 0 will always be invalid.
451 ProgramSelector sel = {};
452 auto result = mSession->tune(sel);
453 ASSERT_EQ(Result::NOT_SUPPORTED, result);
454}
455
456/**
457 * Test scanning to next/prev station.
458 *
459 * Verifies that:
460 * - the method succeeds;
461 * - the program info is changed within timeout::tune;
462 * - works both directions and with or without skipping sub-channel.
463 */
464TEST_F(BroadcastRadioHalTest, Scan) {
465 ASSERT_TRUE(openSession());
466
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800467 // TODO(b/69958777): see FmTune workaround
468 std::this_thread::sleep_for(100ms);
469
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800470 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
471 auto result = mSession->scan(true /* up */, true /* skip subchannel */);
472 EXPECT_EQ(Result::OK, result);
473 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
474
475 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
476 result = mSession->scan(false /* down */, false /* don't skip subchannel */);
477 EXPECT_EQ(Result::OK, result);
478 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
479}
480
481/**
482 * Test step operation.
483 *
484 * Verifies that:
485 * - the method succeeds or returns NOT_SUPPORTED;
486 * - the program info is changed within timeout::tune if the method succeeded;
487 * - works both directions.
488 */
489TEST_F(BroadcastRadioHalTest, Step) {
490 ASSERT_TRUE(openSession());
491
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800492 // TODO(b/69958777): see FmTune workaround
493 std::this_thread::sleep_for(100ms);
494
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800495 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _).Times(AnyNumber());
496 auto result = mSession->step(true /* up */);
Tomasz Wasilczykdb902862018-01-14 17:22:03 -0800497 if (result == Result::NOT_SUPPORTED) {
498 printSkipped("step not supported");
499 return;
500 }
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800501 EXPECT_EQ(Result::OK, result);
502 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
503
504 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
505 result = mSession->step(false /* down */);
506 EXPECT_EQ(Result::OK, result);
507 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
508}
509
510/**
511 * Test tune cancellation.
512 *
513 * Verifies that:
514 * - the method does not crash after being invoked multiple times.
515 */
516TEST_F(BroadcastRadioHalTest, Cancel) {
517 ASSERT_TRUE(openSession());
518
519 for (int i = 0; i < 10; i++) {
520 auto scanResult = mSession->scan(true /* up */, true /* skip subchannel */);
521 ASSERT_EQ(Result::OK, scanResult);
522
523 auto cancelResult = mSession->cancel();
524 ASSERT_TRUE(cancelResult.isOk());
525 }
526}
527
528/**
529 * Test IBroadcastRadio::get|setParameters() methods called with no parameters.
530 *
531 * Verifies that:
532 * - callback is called for empty parameters set.
533 */
534TEST_F(BroadcastRadioHalTest, NoParameters) {
535 ASSERT_TRUE(openSession());
536
537 hidl_vec<VendorKeyValue> halResults = {};
538 bool wasCalled = false;
539 auto cb = [&](hidl_vec<VendorKeyValue> results) {
540 wasCalled = true;
541 halResults = results;
542 };
543
544 auto hidlResult = mSession->setParameters({}, cb);
545 ASSERT_TRUE(hidlResult.isOk());
546 ASSERT_TRUE(wasCalled);
547 ASSERT_EQ(0u, halResults.size());
548
549 wasCalled = false;
550 hidlResult = mSession->getParameters({}, cb);
551 ASSERT_TRUE(hidlResult.isOk());
552 ASSERT_TRUE(wasCalled);
553 ASSERT_EQ(0u, halResults.size());
554}
555
556/**
557 * Test IBroadcastRadio::get|setParameters() methods called with unknown parameters.
558 *
559 * Verifies that:
560 * - unknown parameters are ignored;
561 * - callback is called also for empty results set.
562 */
563TEST_F(BroadcastRadioHalTest, UnknownParameters) {
564 ASSERT_TRUE(openSession());
565
566 hidl_vec<VendorKeyValue> halResults = {};
567 bool wasCalled = false;
568 auto cb = [&](hidl_vec<VendorKeyValue> results) {
569 wasCalled = true;
570 halResults = results;
571 };
572
573 auto hidlResult = mSession->setParameters({{"com.google.unknown", "dummy"}}, cb);
574 ASSERT_TRUE(hidlResult.isOk());
575 ASSERT_TRUE(wasCalled);
576 ASSERT_EQ(0u, halResults.size());
577
578 wasCalled = false;
579 hidlResult = mSession->getParameters({{"com.google.unknown*", "dummy"}}, cb);
580 ASSERT_TRUE(hidlResult.isOk());
581 ASSERT_TRUE(wasCalled);
582 ASSERT_EQ(0u, halResults.size());
583}
584
585/**
586 * Test session closing.
587 *
588 * Verifies that:
589 * - the method does not crash after being invoked multiple times.
590 */
591TEST_F(BroadcastRadioHalTest, Close) {
592 ASSERT_TRUE(openSession());
593
594 for (int i = 0; i < 10; i++) {
595 auto cancelResult = mSession->close();
596 ASSERT_TRUE(cancelResult.isOk());
597 }
598}
599
600/**
601 * Test geting image of invalid ID.
602 *
603 * Verifies that:
604 * - getImage call handles argument 0 gracefully.
605 */
606TEST_F(BroadcastRadioHalTest, GetNoImage) {
607 size_t len = 0;
608 auto result = mModule->getImage(0, [&](hidl_vec<uint8_t> rawImage) { len = rawImage.size(); });
609
610 ASSERT_TRUE(result.isOk());
611 ASSERT_EQ(0u, len);
612}
613
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800614/**
615 * Test getting config flags.
616 *
617 * Verifies that:
618 * - getConfigFlag either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
619 * - call success or failure is consistent with setConfigFlag.
620 */
621TEST_F(BroadcastRadioHalTest, GetConfigFlags) {
622 ASSERT_TRUE(openSession());
623
624 for (auto flag : gConfigFlagValues) {
625 auto halResult = Result::UNKNOWN_ERROR;
626 auto cb = [&](Result result, bool) { halResult = result; };
627 auto hidlResult = mSession->getConfigFlag(flag, cb);
628 EXPECT_TRUE(hidlResult.isOk());
629
630 if (halResult != Result::NOT_SUPPORTED && halResult != Result::INVALID_STATE) {
631 ASSERT_EQ(Result::OK, halResult);
632 }
633
634 // set must fail or succeed the same way as get
635 auto setResult = mSession->setConfigFlag(flag, false);
636 EXPECT_EQ(halResult, setResult);
637 setResult = mSession->setConfigFlag(flag, true);
638 EXPECT_EQ(halResult, setResult);
639 }
640}
641
642/**
643 * Test setting config flags.
644 *
645 * Verifies that:
646 * - setConfigFlag either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
647 * - getConfigFlag reflects the state requested immediately after the set call.
648 */
649TEST_F(BroadcastRadioHalTest, SetConfigFlags) {
650 ASSERT_TRUE(openSession());
651
652 auto get = [&](ConfigFlag flag) {
653 auto halResult = Result::UNKNOWN_ERROR;
654 bool gotValue = false;
655 auto cb = [&](Result result, bool value) {
656 halResult = result;
657 gotValue = value;
658 };
659 auto hidlResult = mSession->getConfigFlag(flag, cb);
660 EXPECT_TRUE(hidlResult.isOk());
661 EXPECT_EQ(Result::OK, halResult);
662 return gotValue;
663 };
664
665 for (auto flag : gConfigFlagValues) {
666 auto result = mSession->setConfigFlag(flag, false);
667 if (result == Result::NOT_SUPPORTED || result == Result::INVALID_STATE) {
668 // setting to true must result in the same error as false
669 auto secondResult = mSession->setConfigFlag(flag, true);
670 EXPECT_EQ(result, secondResult);
671 continue;
672 }
673 ASSERT_EQ(Result::OK, result);
674
675 // verify false is set
676 auto value = get(flag);
677 EXPECT_FALSE(value);
678
679 // try setting true this time
680 result = mSession->setConfigFlag(flag, true);
681 ASSERT_EQ(Result::OK, result);
682 value = get(flag);
683 EXPECT_TRUE(value);
684
685 // false again
686 result = mSession->setConfigFlag(flag, false);
687 ASSERT_EQ(Result::OK, result);
688 value = get(flag);
689 EXPECT_FALSE(value);
690 }
691}
692
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800693/**
694 * Test getting program list.
695 *
696 * Verifies that:
697 * - startProgramListUpdates either succeeds or returns NOT_SUPPORTED;
698 * - the complete list is fetched within timeout::programListScan;
699 * - stopProgramListUpdates does not crash.
700 */
701TEST_F(BroadcastRadioHalTest, GetProgramList) {
702 ASSERT_TRUE(openSession());
703
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800704 getProgramList();
705}
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800706
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800707/**
708 * Test HD_STATION_NAME correctness.
709 *
710 * Verifies that if a program on the list contains HD_STATION_NAME identifier:
711 * - the program provides station name in its metadata;
712 * - the identifier matches the name;
713 * - there is only one identifier of that type.
714 */
715TEST_F(BroadcastRadioHalTest, HdRadioStationNameId) {
716 ASSERT_TRUE(openSession());
717
718 auto list = getProgramList();
719 if (!list) return;
720
721 for (auto&& program : *list) {
722 auto nameIds = utils::getAllIds(program.selector, IdentifierType::HD_STATION_NAME);
723 EXPECT_LE(nameIds.size(), 1u);
724 if (nameIds.size() == 0) continue;
725
726 auto name = utils::getMetadataString(program, MetadataKey::PROGRAM_NAME);
727 if (!name) name = utils::getMetadataString(program, MetadataKey::RDS_PS);
728 ASSERT_TRUE(name.has_value());
729
730 auto expectedId = utils::make_hdradio_station_name(*name);
731 EXPECT_EQ(expectedId.value, nameIds[0]);
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800732 }
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800733}
734
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800735/**
Tomasz Wasilczyk0d5ef5d2018-01-10 10:58:20 -0800736 * Test announcement listener registration.
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800737 *
738 * Verifies that:
Tomasz Wasilczyk0d5ef5d2018-01-10 10:58:20 -0800739 * - registerAnnouncementListener either succeeds or returns NOT_SUPPORTED;
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800740 * - if it succeeds, it returns a valid close handle (which is a nullptr otherwise);
741 * - closing handle does not crash.
742 */
Tomasz Wasilczyk0d5ef5d2018-01-10 10:58:20 -0800743TEST_F(BroadcastRadioHalTest, AnnouncementListenerRegistration) {
744 sp<AnnouncementListenerMock> listener = new AnnouncementListenerMock();
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800745
746 Result halResult = Result::UNKNOWN_ERROR;
747 sp<ICloseHandle> closeHandle = nullptr;
748 auto cb = [&](Result result, const sp<ICloseHandle>& closeHandle_) {
749 halResult = result;
750 closeHandle = closeHandle_;
751 };
752
753 auto hidlResult =
Tomasz Wasilczyk0d5ef5d2018-01-10 10:58:20 -0800754 mModule->registerAnnouncementListener({AnnouncementType::EMERGENCY}, listener, cb);
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800755 ASSERT_TRUE(hidlResult.isOk());
756
757 if (halResult == Result::NOT_SUPPORTED) {
758 ASSERT_EQ(nullptr, closeHandle.get());
759 printSkipped("Announcements not supported");
760 return;
761 }
762
763 ASSERT_EQ(Result::OK, halResult);
764 ASSERT_NE(nullptr, closeHandle.get());
765
766 closeHandle->close();
767}
768
Tomasz Wasilczyk4ce63822017-12-21 14:25:54 -0800769// TODO(b/70939328): test ProgramInfo's currentlyTunedId and
770// currentlyTunedChannel once the program list is implemented.
771
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800772} // namespace vts
773} // namespace V2_0
774} // namespace broadcastradio
775} // namespace hardware
776} // namespace android
777
778int main(int argc, char** argv) {
779 ::testing::InitGoogleTest(&argc, argv);
780 int status = RUN_ALL_TESTS();
781 ALOGI("Test result = %d", status);
782 return status;
783}