blob: 81fca4c8d618a8041dde7735236f7a5a46729176 [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"
18
19#include <VtsHalHidlTargetTestBase.h>
20#include <android-base/logging.h>
21#include <android/hardware/broadcastradio/2.0/IBroadcastRadio.h>
22#include <android/hardware/broadcastradio/2.0/ITunerCallback.h>
23#include <android/hardware/broadcastradio/2.0/ITunerSession.h>
24#include <android/hardware/broadcastradio/2.0/types.h>
25#include <broadcastradio-utils-2x/Utils.h>
26#include <broadcastradio-vts-utils/call-barrier.h>
27#include <broadcastradio-vts-utils/mock-timeout.h>
28#include <broadcastradio-vts-utils/pointer-utils.h>
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080029#include <cutils/bitops.h>
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080030#include <gmock/gmock.h>
31
32#include <chrono>
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -080033#include <optional>
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080034#include <regex>
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080035
36namespace android {
37namespace hardware {
38namespace broadcastradio {
39namespace V2_0 {
40namespace vts {
41
42using namespace std::chrono_literals;
43
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080044using std::unordered_set;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080045using std::vector;
46using testing::_;
47using testing::AnyNumber;
48using testing::ByMove;
49using testing::DoAll;
50using testing::Invoke;
51using testing::SaveArg;
52
53using broadcastradio::vts::CallBarrier;
54using broadcastradio::vts::clearAndWait;
55using utils::make_identifier;
56using utils::make_selector_amfm;
57
58namespace timeout {
59
60static constexpr auto tune = 30s;
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080061static constexpr auto programListScan = 5min;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080062
63} // namespace timeout
64
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -080065static const ConfigFlag gConfigFlagValues[] = {
Tomasz Wasilczyk30240f62017-12-20 14:19:21 -080066 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,
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -080075};
76
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080077class TunerCallbackMock : public ITunerCallback {
78 public:
79 TunerCallbackMock();
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080080
81 MOCK_METHOD2(onTuneFailed, Return<void>(Result, const ProgramSelector&));
82 MOCK_TIMEOUT_METHOD1(onCurrentProgramInfoChanged, Return<void>(const ProgramInfo&));
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080083 Return<void> onProgramListUpdated(const ProgramListChunk& chunk);
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080084 MOCK_METHOD1(onAntennaStateChange, Return<void>(bool connected));
85 MOCK_METHOD1(onParametersUpdated, Return<void>(const hidl_vec<VendorKeyValue>& parameters));
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080086
87 MOCK_TIMEOUT_METHOD0(onProgramListReady, void());
88
89 std::mutex mLock;
90 utils::ProgramInfoSet mProgramList;
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080091};
92
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -080093struct AnnouncementObserverMock : public IAnnouncementObserver {
94 MOCK_METHOD1(onListUpdated, Return<void>(const hidl_vec<Announcement>&));
95};
96
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080097class BroadcastRadioHalTest : public ::testing::VtsHalHidlTargetTestBase {
98 protected:
99 virtual void SetUp() override;
100 virtual void TearDown() override;
101
102 bool openSession();
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800103 bool getAmFmRegionConfig(bool full, AmFmRegionConfig* config);
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800104 std::optional<utils::ProgramInfoSet> getProgramList();
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800105
106 sp<IBroadcastRadio> mModule;
107 Properties mProperties;
108 sp<ITunerSession> mSession;
109 sp<TunerCallbackMock> mCallback = new TunerCallbackMock();
110};
111
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800112static void printSkipped(std::string msg) {
113 std::cout << "[ SKIPPED ] " << msg << std::endl;
114}
115
116TunerCallbackMock::TunerCallbackMock() {
117 // we expect the antenna is connected through the whole test
118 EXPECT_CALL(*this, onAntennaStateChange(false)).Times(0);
119}
120
121Return<void> TunerCallbackMock::onProgramListUpdated(const ProgramListChunk& chunk) {
122 std::lock_guard<std::mutex> lk(mLock);
123
124 updateProgramList(mProgramList, chunk);
125
126 if (chunk.complete) onProgramListReady();
127
128 return {};
129}
130
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800131void BroadcastRadioHalTest::SetUp() {
132 EXPECT_EQ(nullptr, mModule.get()) << "Module is already open";
133
134 // lookup HIDL service (radio module)
135 mModule = getService<IBroadcastRadio>();
136 ASSERT_NE(nullptr, mModule.get()) << "Couldn't find broadcast radio HAL implementation";
137
138 // get module properties
139 auto propResult = mModule->getProperties([&](const Properties& p) { mProperties = p; });
140 ASSERT_TRUE(propResult.isOk());
141
142 EXPECT_FALSE(mProperties.maker.empty());
143 EXPECT_FALSE(mProperties.product.empty());
144 EXPECT_GT(mProperties.supportedIdentifierTypes.size(), 0u);
145}
146
147void BroadcastRadioHalTest::TearDown() {
148 mSession.clear();
149 mModule.clear();
150 clearAndWait(mCallback, 1s);
151}
152
153bool BroadcastRadioHalTest::openSession() {
154 EXPECT_EQ(nullptr, mSession.get()) << "Session is already open";
155
156 Result halResult = Result::UNKNOWN_ERROR;
157 auto openCb = [&](Result result, const sp<ITunerSession>& session) {
158 halResult = result;
159 if (result != Result::OK) return;
160 mSession = session;
161 };
162 auto hidlResult = mModule->openSession(mCallback, openCb);
163
164 EXPECT_TRUE(hidlResult.isOk());
165 EXPECT_EQ(Result::OK, halResult);
166 EXPECT_NE(nullptr, mSession.get());
167
168 return nullptr != mSession.get();
169}
170
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800171bool BroadcastRadioHalTest::getAmFmRegionConfig(bool full, AmFmRegionConfig* config) {
172 auto halResult = Result::UNKNOWN_ERROR;
173 auto cb = [&](Result result, AmFmRegionConfig configCb) {
174 halResult = result;
175 if (config) *config = configCb;
176 };
177
178 auto hidlResult = mModule->getAmFmRegionConfig(full, cb);
179 EXPECT_TRUE(hidlResult.isOk());
180
181 if (halResult == Result::NOT_SUPPORTED) return false;
182
183 EXPECT_EQ(Result::OK, halResult);
184 return halResult == Result::OK;
185}
186
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800187std::optional<utils::ProgramInfoSet> BroadcastRadioHalTest::getProgramList() {
188 EXPECT_TIMEOUT_CALL(*mCallback, onProgramListReady).Times(AnyNumber());
189
190 auto startResult = mSession->startProgramListUpdates({});
191 if (startResult == Result::NOT_SUPPORTED) {
192 printSkipped("Program list not supported");
193 return nullopt;
194 }
195 EXPECT_EQ(Result::OK, startResult);
196 if (startResult != Result::OK) return nullopt;
197
198 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onProgramListReady, timeout::programListScan);
199
200 auto stopResult = mSession->stopProgramListUpdates();
201 EXPECT_TRUE(stopResult.isOk());
202
203 return mCallback->mProgramList;
204}
205
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800206/**
207 * Test session opening.
208 *
209 * Verifies that:
210 * - the method succeeds on a first and subsequent calls;
211 * - the method succeeds when called for the second time without
212 * closing previous session.
213 */
214TEST_F(BroadcastRadioHalTest, OpenSession) {
215 // simply open session for the first time
216 ASSERT_TRUE(openSession());
217
218 // drop (without explicit close) and re-open the session
219 mSession.clear();
220 ASSERT_TRUE(openSession());
221
222 // open the second session (the first one should be forcibly closed)
223 auto secondSession = mSession;
224 mSession.clear();
225 ASSERT_TRUE(openSession());
226}
227
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800228static bool isValidAmFmFreq(uint64_t freq) {
229 auto id = utils::make_identifier(IdentifierType::AMFM_FREQUENCY, freq);
230 return utils::isValid(id);
231}
232
233static void validateRange(const AmFmBandRange& range) {
234 EXPECT_TRUE(isValidAmFmFreq(range.lowerBound));
235 EXPECT_TRUE(isValidAmFmFreq(range.upperBound));
236 EXPECT_LT(range.lowerBound, range.upperBound);
237 EXPECT_GT(range.spacing, 0u);
238 EXPECT_EQ(0u, (range.upperBound - range.lowerBound) % range.spacing);
239}
240
241static bool supportsFM(const AmFmRegionConfig& config) {
242 for (auto&& range : config.ranges) {
243 if (utils::getBand(range.lowerBound) == utils::FrequencyBand::FM) return true;
244 }
245 return false;
246}
247
248/**
249 * Test fetching AM/FM regional configuration.
250 *
251 * Verifies that:
252 * - AM/FM regional configuration is either set at startup or not supported at all by the hardware;
253 * - there is at least one AM/FM band configured;
254 * - FM Deemphasis and RDS are correctly configured for FM-capable radio;
255 * - all channel grids (frequency ranges and spacings) are valid;
256 * - scan spacing is a multiply of manual spacing value.
257 */
258TEST_F(BroadcastRadioHalTest, GetAmFmRegionConfig) {
259 AmFmRegionConfig config;
260 bool supported = getAmFmRegionConfig(false, &config);
261 if (!supported) {
262 printSkipped("AM/FM not supported");
263 return;
264 }
265
266 EXPECT_GT(config.ranges.size(), 0u);
267 EXPECT_LE(popcountll(config.fmDeemphasis), 1);
268 EXPECT_LE(popcountll(config.fmRds), 1);
269
270 for (auto&& range : config.ranges) {
271 validateRange(range);
272 EXPECT_EQ(0u, range.scanSpacing % range.spacing);
273 EXPECT_GE(range.scanSpacing, range.spacing);
274 }
275
276 if (supportsFM(config)) {
277 EXPECT_EQ(popcountll(config.fmDeemphasis), 1);
278 }
279}
280
281/**
282 * Test fetching AM/FM regional capabilities.
283 *
284 * Verifies that:
285 * - AM/FM regional capabilities are either available or not supported at all by the hardware;
286 * - there is at least one AM/FM range supported;
287 * - there is at least one de-emphasis filter mode supported for FM-capable radio;
288 * - all channel grids (frequency ranges and spacings) are valid;
289 * - scan spacing is not set.
290 */
291TEST_F(BroadcastRadioHalTest, GetAmFmRegionConfigCapabilities) {
292 AmFmRegionConfig config;
293 bool supported = getAmFmRegionConfig(true, &config);
294 if (!supported) {
295 printSkipped("AM/FM not supported");
296 return;
297 }
298
299 EXPECT_GT(config.ranges.size(), 0u);
300
301 for (auto&& range : config.ranges) {
302 validateRange(range);
303 EXPECT_EQ(0u, range.scanSpacing);
304 }
305
306 if (supportsFM(config)) {
307 EXPECT_GE(popcountll(config.fmDeemphasis), 1);
308 }
309}
310
311/**
312 * Test fetching DAB regional configuration.
313 *
314 * Verifies that:
315 * - DAB regional configuration is either set at startup or not supported at all by the hardware;
316 * - all channel labels match correct format;
317 * - all channel frequencies are in correct range.
318 */
319TEST_F(BroadcastRadioHalTest, GetDabRegionConfig) {
320 Result halResult;
321 hidl_vec<DabTableEntry> config;
322 auto cb = [&](Result result, hidl_vec<DabTableEntry> configCb) {
323 halResult = result;
324 config = configCb;
325 };
326 auto hidlResult = mModule->getDabRegionConfig(cb);
327 ASSERT_TRUE(hidlResult.isOk());
328
329 if (halResult == Result::NOT_SUPPORTED) {
330 printSkipped("DAB not supported");
331 return;
332 }
333 ASSERT_EQ(Result::OK, halResult);
334
335 std::regex re("^[A-Z0-9]{2,5}$");
336 // double-check correctness of the test
337 ASSERT_TRUE(std::regex_match("5A", re));
338 ASSERT_FALSE(std::regex_match("5a", re));
339 ASSERT_FALSE(std::regex_match("123ABC", re));
340
341 for (auto&& entry : config) {
342 EXPECT_TRUE(std::regex_match(std::string(entry.label), re));
343
344 auto id = utils::make_identifier(IdentifierType::DAB_FREQUENCY, entry.frequency);
345 EXPECT_TRUE(utils::isValid(id));
346 }
347}
348
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800349/**
350 * Test tuning with FM selector.
351 *
352 * Verifies that:
353 * - if AM/FM selector is not supported, the method returns NOT_SUPPORTED;
354 * - if it is supported, the method succeeds;
355 * - after a successful tune call, onCurrentProgramInfoChanged callback is
356 * invoked carrying a proper selector;
357 * - program changes exactly to what was requested.
358 */
359TEST_F(BroadcastRadioHalTest, FmTune) {
360 ASSERT_TRUE(openSession());
361
362 uint64_t freq = 100100; // 100.1 FM
363 auto sel = make_selector_amfm(freq);
364
365 // try tuning
366 ProgramInfo infoCb = {};
367 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _)
368 .Times(AnyNumber())
369 .WillOnce(DoAll(SaveArg<0>(&infoCb), testing::Return(ByMove(Void()))));
370 auto result = mSession->tune(sel);
371
372 // expect a failure if it's not supported
373 if (!utils::isSupported(mProperties, sel)) {
374 EXPECT_EQ(Result::NOT_SUPPORTED, result);
375 return;
376 }
377
378 // expect a callback if it succeeds
379 EXPECT_EQ(Result::OK, result);
380 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
381
382 // it should tune exactly to what was requested
383 auto freqs = utils::getAllIds(infoCb.selector, IdentifierType::AMFM_FREQUENCY);
384 EXPECT_NE(freqs.end(), find(freqs.begin(), freqs.end(), freq));
385}
386
387/**
388 * Test tuning with invalid selectors.
389 *
390 * Verifies that:
391 * - if the selector is not supported, it's ignored;
392 * - if it is supported, an invalid value results with INVALID_ARGUMENTS;
393 */
394TEST_F(BroadcastRadioHalTest, TuneFailsWithInvalid) {
395 ASSERT_TRUE(openSession());
396
397 vector<ProgramIdentifier> invalid = {
398 make_identifier(IdentifierType::AMFM_FREQUENCY, 0),
399 make_identifier(IdentifierType::RDS_PI, 0x10000),
400 make_identifier(IdentifierType::HD_STATION_ID_EXT, 0x100000000),
401 make_identifier(IdentifierType::DAB_SID_EXT, 0),
402 make_identifier(IdentifierType::DRMO_SERVICE_ID, 0x100000000),
403 make_identifier(IdentifierType::SXM_SERVICE_ID, 0x100000000),
404 };
405
406 for (auto&& id : invalid) {
407 ProgramSelector sel{id, {}};
408
409 auto result = mSession->tune(sel);
410
411 if (utils::isSupported(mProperties, sel)) {
412 EXPECT_EQ(Result::INVALID_ARGUMENTS, result);
413 } else {
414 EXPECT_EQ(Result::NOT_SUPPORTED, result);
415 }
416 }
417}
418
419/**
420 * Test tuning with empty program selector.
421 *
422 * Verifies that:
423 * - tune fails with NOT_SUPPORTED when program selector is not initialized.
424 */
425TEST_F(BroadcastRadioHalTest, TuneFailsWithEmpty) {
426 ASSERT_TRUE(openSession());
427
428 // Program type is 1-based, so 0 will always be invalid.
429 ProgramSelector sel = {};
430 auto result = mSession->tune(sel);
431 ASSERT_EQ(Result::NOT_SUPPORTED, result);
432}
433
434/**
435 * Test scanning to next/prev station.
436 *
437 * Verifies that:
438 * - the method succeeds;
439 * - the program info is changed within timeout::tune;
440 * - works both directions and with or without skipping sub-channel.
441 */
442TEST_F(BroadcastRadioHalTest, Scan) {
443 ASSERT_TRUE(openSession());
444
445 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
446 auto result = mSession->scan(true /* up */, true /* skip subchannel */);
447 EXPECT_EQ(Result::OK, result);
448 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
449
450 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
451 result = mSession->scan(false /* down */, false /* don't skip subchannel */);
452 EXPECT_EQ(Result::OK, result);
453 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
454}
455
456/**
457 * Test step operation.
458 *
459 * Verifies that:
460 * - the method succeeds or returns NOT_SUPPORTED;
461 * - the program info is changed within timeout::tune if the method succeeded;
462 * - works both directions.
463 */
464TEST_F(BroadcastRadioHalTest, Step) {
465 ASSERT_TRUE(openSession());
466
467 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _).Times(AnyNumber());
468 auto result = mSession->step(true /* up */);
469 if (result == Result::NOT_SUPPORTED) return;
470 EXPECT_EQ(Result::OK, result);
471 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
472
473 EXPECT_TIMEOUT_CALL(*mCallback, onCurrentProgramInfoChanged, _);
474 result = mSession->step(false /* down */);
475 EXPECT_EQ(Result::OK, result);
476 EXPECT_TIMEOUT_CALL_WAIT(*mCallback, onCurrentProgramInfoChanged, timeout::tune);
477}
478
479/**
480 * Test tune cancellation.
481 *
482 * Verifies that:
483 * - the method does not crash after being invoked multiple times.
484 */
485TEST_F(BroadcastRadioHalTest, Cancel) {
486 ASSERT_TRUE(openSession());
487
488 for (int i = 0; i < 10; i++) {
489 auto scanResult = mSession->scan(true /* up */, true /* skip subchannel */);
490 ASSERT_EQ(Result::OK, scanResult);
491
492 auto cancelResult = mSession->cancel();
493 ASSERT_TRUE(cancelResult.isOk());
494 }
495}
496
497/**
498 * Test IBroadcastRadio::get|setParameters() methods called with no parameters.
499 *
500 * Verifies that:
501 * - callback is called for empty parameters set.
502 */
503TEST_F(BroadcastRadioHalTest, NoParameters) {
504 ASSERT_TRUE(openSession());
505
506 hidl_vec<VendorKeyValue> halResults = {};
507 bool wasCalled = false;
508 auto cb = [&](hidl_vec<VendorKeyValue> results) {
509 wasCalled = true;
510 halResults = results;
511 };
512
513 auto hidlResult = mSession->setParameters({}, cb);
514 ASSERT_TRUE(hidlResult.isOk());
515 ASSERT_TRUE(wasCalled);
516 ASSERT_EQ(0u, halResults.size());
517
518 wasCalled = false;
519 hidlResult = mSession->getParameters({}, cb);
520 ASSERT_TRUE(hidlResult.isOk());
521 ASSERT_TRUE(wasCalled);
522 ASSERT_EQ(0u, halResults.size());
523}
524
525/**
526 * Test IBroadcastRadio::get|setParameters() methods called with unknown parameters.
527 *
528 * Verifies that:
529 * - unknown parameters are ignored;
530 * - callback is called also for empty results set.
531 */
532TEST_F(BroadcastRadioHalTest, UnknownParameters) {
533 ASSERT_TRUE(openSession());
534
535 hidl_vec<VendorKeyValue> halResults = {};
536 bool wasCalled = false;
537 auto cb = [&](hidl_vec<VendorKeyValue> results) {
538 wasCalled = true;
539 halResults = results;
540 };
541
542 auto hidlResult = mSession->setParameters({{"com.google.unknown", "dummy"}}, cb);
543 ASSERT_TRUE(hidlResult.isOk());
544 ASSERT_TRUE(wasCalled);
545 ASSERT_EQ(0u, halResults.size());
546
547 wasCalled = false;
548 hidlResult = mSession->getParameters({{"com.google.unknown*", "dummy"}}, cb);
549 ASSERT_TRUE(hidlResult.isOk());
550 ASSERT_TRUE(wasCalled);
551 ASSERT_EQ(0u, halResults.size());
552}
553
554/**
555 * Test session closing.
556 *
557 * Verifies that:
558 * - the method does not crash after being invoked multiple times.
559 */
560TEST_F(BroadcastRadioHalTest, Close) {
561 ASSERT_TRUE(openSession());
562
563 for (int i = 0; i < 10; i++) {
564 auto cancelResult = mSession->close();
565 ASSERT_TRUE(cancelResult.isOk());
566 }
567}
568
569/**
570 * Test geting image of invalid ID.
571 *
572 * Verifies that:
573 * - getImage call handles argument 0 gracefully.
574 */
575TEST_F(BroadcastRadioHalTest, GetNoImage) {
576 size_t len = 0;
577 auto result = mModule->getImage(0, [&](hidl_vec<uint8_t> rawImage) { len = rawImage.size(); });
578
579 ASSERT_TRUE(result.isOk());
580 ASSERT_EQ(0u, len);
581}
582
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800583/**
584 * Test getting config flags.
585 *
586 * Verifies that:
Tomasz Wasilczyk3dd452a2018-01-12 14:57:45 -0800587 * - isConfigFlagSet either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800588 * - call success or failure is consistent with setConfigFlag.
589 */
Tomasz Wasilczyk3dd452a2018-01-12 14:57:45 -0800590TEST_F(BroadcastRadioHalTest, FetchConfigFlags) {
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800591 ASSERT_TRUE(openSession());
592
593 for (auto flag : gConfigFlagValues) {
594 auto halResult = Result::UNKNOWN_ERROR;
595 auto cb = [&](Result result, bool) { halResult = result; };
Tomasz Wasilczyk3dd452a2018-01-12 14:57:45 -0800596 auto hidlResult = mSession->isConfigFlagSet(flag, cb);
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800597 EXPECT_TRUE(hidlResult.isOk());
598
599 if (halResult != Result::NOT_SUPPORTED && halResult != Result::INVALID_STATE) {
600 ASSERT_EQ(Result::OK, halResult);
601 }
602
603 // set must fail or succeed the same way as get
604 auto setResult = mSession->setConfigFlag(flag, false);
605 EXPECT_EQ(halResult, setResult);
606 setResult = mSession->setConfigFlag(flag, true);
607 EXPECT_EQ(halResult, setResult);
608 }
609}
610
611/**
612 * Test setting config flags.
613 *
614 * Verifies that:
615 * - setConfigFlag either succeeds or ends with NOT_SUPPORTED or INVALID_STATE;
Tomasz Wasilczyk3dd452a2018-01-12 14:57:45 -0800616 * - isConfigFlagSet reflects the state requested immediately after the set call.
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800617 */
618TEST_F(BroadcastRadioHalTest, SetConfigFlags) {
619 ASSERT_TRUE(openSession());
620
621 auto get = [&](ConfigFlag flag) {
622 auto halResult = Result::UNKNOWN_ERROR;
623 bool gotValue = false;
624 auto cb = [&](Result result, bool value) {
625 halResult = result;
626 gotValue = value;
627 };
Tomasz Wasilczyk3dd452a2018-01-12 14:57:45 -0800628 auto hidlResult = mSession->isConfigFlagSet(flag, cb);
Tomasz Wasilczyk43fe8942017-12-14 11:44:12 -0800629 EXPECT_TRUE(hidlResult.isOk());
630 EXPECT_EQ(Result::OK, halResult);
631 return gotValue;
632 };
633
634 for (auto flag : gConfigFlagValues) {
635 auto result = mSession->setConfigFlag(flag, false);
636 if (result == Result::NOT_SUPPORTED || result == Result::INVALID_STATE) {
637 // setting to true must result in the same error as false
638 auto secondResult = mSession->setConfigFlag(flag, true);
639 EXPECT_EQ(result, secondResult);
640 continue;
641 }
642 ASSERT_EQ(Result::OK, result);
643
644 // verify false is set
645 auto value = get(flag);
646 EXPECT_FALSE(value);
647
648 // try setting true this time
649 result = mSession->setConfigFlag(flag, true);
650 ASSERT_EQ(Result::OK, result);
651 value = get(flag);
652 EXPECT_TRUE(value);
653
654 // false again
655 result = mSession->setConfigFlag(flag, false);
656 ASSERT_EQ(Result::OK, result);
657 value = get(flag);
658 EXPECT_FALSE(value);
659 }
660}
661
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800662/**
663 * Test getting program list.
664 *
665 * Verifies that:
666 * - startProgramListUpdates either succeeds or returns NOT_SUPPORTED;
667 * - the complete list is fetched within timeout::programListScan;
668 * - stopProgramListUpdates does not crash.
669 */
670TEST_F(BroadcastRadioHalTest, GetProgramList) {
671 ASSERT_TRUE(openSession());
672
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800673 getProgramList();
674}
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800675
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800676/**
677 * Test HD_STATION_NAME correctness.
678 *
679 * Verifies that if a program on the list contains HD_STATION_NAME identifier:
680 * - the program provides station name in its metadata;
681 * - the identifier matches the name;
682 * - there is only one identifier of that type.
683 */
684TEST_F(BroadcastRadioHalTest, HdRadioStationNameId) {
685 ASSERT_TRUE(openSession());
686
687 auto list = getProgramList();
688 if (!list) return;
689
690 for (auto&& program : *list) {
691 auto nameIds = utils::getAllIds(program.selector, IdentifierType::HD_STATION_NAME);
692 EXPECT_LE(nameIds.size(), 1u);
693 if (nameIds.size() == 0) continue;
694
695 auto name = utils::getMetadataString(program, MetadataKey::PROGRAM_NAME);
696 if (!name) name = utils::getMetadataString(program, MetadataKey::RDS_PS);
697 ASSERT_TRUE(name.has_value());
698
699 auto expectedId = utils::make_hdradio_station_name(*name);
700 EXPECT_EQ(expectedId.value, nameIds[0]);
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800701 }
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800702}
703
Tomasz Wasilczyk6a9f8562017-12-27 09:46:43 -0800704/**
705 * Test announcement observer registration.
706 *
707 * Verifies that:
708 * - registerAnnouncementObserver either succeeds or returns NOT_SUPPORTED;
709 * - if it succeeds, it returns a valid close handle (which is a nullptr otherwise);
710 * - closing handle does not crash.
711 */
712TEST_F(BroadcastRadioHalTest, AnnouncementObserverRegistration) {
713 sp<AnnouncementObserverMock> observer = new AnnouncementObserverMock();
714
715 Result halResult = Result::UNKNOWN_ERROR;
716 sp<ICloseHandle> closeHandle = nullptr;
717 auto cb = [&](Result result, const sp<ICloseHandle>& closeHandle_) {
718 halResult = result;
719 closeHandle = closeHandle_;
720 };
721
722 auto hidlResult =
723 mModule->registerAnnouncementObserver({AnnouncementType::EMERGENCY}, observer, cb);
724 ASSERT_TRUE(hidlResult.isOk());
725
726 if (halResult == Result::NOT_SUPPORTED) {
727 ASSERT_EQ(nullptr, closeHandle.get());
728 printSkipped("Announcements not supported");
729 return;
730 }
731
732 ASSERT_EQ(Result::OK, halResult);
733 ASSERT_NE(nullptr, closeHandle.get());
734
735 closeHandle->close();
736}
737
Tomasz Wasilczyk4ce63822017-12-21 14:25:54 -0800738// TODO(b/70939328): test ProgramInfo's currentlyTunedId and
739// currentlyTunedChannel once the program list is implemented.
740
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800741} // namespace vts
742} // namespace V2_0
743} // namespace broadcastradio
744} // namespace hardware
745} // namespace android
746
747int main(int argc, char** argv) {
748 ::testing::InitGoogleTest(&argc, argv);
749 int status = RUN_ALL_TESTS();
750 ALOGI("Test result = %d", status);
751 return status;
752}