Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "BroadcastRadioHidlHalTest" |
Yuexi Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 18 | #include <VtsHalHidlTargetBaseTest.h> |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <cutils/native_handle.h> |
| 21 | #include <cutils/properties.h> |
Martijn Coenen | 0282237 | 2016-12-29 14:03:41 +0100 | [diff] [blame] | 22 | #include <hidl/HidlTransportSupport.h> |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 23 | #include <utils/threads.h> |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 24 | |
| 25 | #include <android/hardware/broadcastradio/1.0/IBroadcastRadioFactory.h> |
| 26 | #include <android/hardware/broadcastradio/1.0/IBroadcastRadio.h> |
| 27 | #include <android/hardware/broadcastradio/1.0/ITuner.h> |
| 28 | #include <android/hardware/broadcastradio/1.0/ITunerCallback.h> |
| 29 | #include <android/hardware/broadcastradio/1.0/types.h> |
| 30 | |
| 31 | |
| 32 | using ::android::sp; |
| 33 | using ::android::Mutex; |
| 34 | using ::android::Condition; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 35 | using ::android::hardware::Return; |
| 36 | using ::android::hardware::Status; |
| 37 | using ::android::hardware::Void; |
| 38 | using ::android::hardware::broadcastradio::V1_0::IBroadcastRadioFactory; |
| 39 | using ::android::hardware::broadcastradio::V1_0::IBroadcastRadio; |
| 40 | using ::android::hardware::broadcastradio::V1_0::ITuner; |
| 41 | using ::android::hardware::broadcastradio::V1_0::ITunerCallback; |
| 42 | using ::android::hardware::broadcastradio::V1_0::Result; |
| 43 | using ::android::hardware::broadcastradio::V1_0::Class; |
| 44 | using ::android::hardware::broadcastradio::V1_0::Properties; |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 45 | using ::android::hardware::broadcastradio::V1_0::Band; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 46 | using ::android::hardware::broadcastradio::V1_0::BandConfig; |
| 47 | using ::android::hardware::broadcastradio::V1_0::Direction; |
| 48 | using ::android::hardware::broadcastradio::V1_0::ProgramInfo; |
| 49 | using ::android::hardware::broadcastradio::V1_0::MetaData; |
| 50 | |
| 51 | |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 52 | // The main test class for Broadcast Radio HIDL HAL. |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 53 | |
Yuexi Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 54 | class BroadcastRadioHidlTest : public ::testing::VtsHalHidlTargetBaseTest { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 55 | protected: |
| 56 | virtual void SetUp() override { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 57 | sp<IBroadcastRadioFactory> factory = |
Yuexi Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 58 | ::testing::VtsHalHidlTargetBaseTest::getService<IBroadcastRadioFactory>(); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 59 | if (factory != 0) { |
| 60 | factory->connectModule(Class::AM_FM, |
| 61 | [&](Result retval, const ::android::sp<IBroadcastRadio>& result) { |
| 62 | if (retval == Result::OK) { |
| 63 | mRadio = result; |
| 64 | } |
| 65 | }); |
| 66 | } |
| 67 | mTunerCallback = new MyCallback(this); |
| 68 | ASSERT_NE(nullptr, mRadio.get()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 69 | ASSERT_NE(nullptr, mTunerCallback.get()); |
| 70 | } |
| 71 | |
| 72 | virtual void TearDown() override { |
| 73 | mTuner.clear(); |
| 74 | mRadio.clear(); |
| 75 | } |
| 76 | |
| 77 | class MyCallback : public ITunerCallback { |
| 78 | public: |
| 79 | |
| 80 | // ITunerCallback methods (see doc in ITunerCallback.hal) |
| 81 | virtual Return<void> hardwareFailure() { |
| 82 | ALOGI("%s", __FUNCTION__); |
| 83 | mParentTest->onHwFailureCallback(); |
| 84 | return Void(); |
| 85 | } |
| 86 | |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 87 | virtual Return<void> configChange(Result result, const BandConfig& config) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 88 | ALOGI("%s result %d", __FUNCTION__, result); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 89 | mParentTest->onConfigChangeCallback(result, config); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 90 | return Void(); |
| 91 | } |
| 92 | |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 93 | virtual Return<void> tuneComplete(Result result, const ProgramInfo& info) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 94 | ALOGI("%s result %d", __FUNCTION__, result); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 95 | mParentTest->onTuneCompleteCallback(result, info); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 96 | return Void(); |
| 97 | } |
| 98 | |
| 99 | virtual Return<void> afSwitch(const ProgramInfo& info __unused) { |
| 100 | return Void(); |
| 101 | } |
| 102 | |
| 103 | virtual Return<void> antennaStateChange(bool connected) { |
| 104 | ALOGI("%s connected %d", __FUNCTION__, connected); |
| 105 | return Void(); |
| 106 | } |
| 107 | |
| 108 | virtual Return<void> trafficAnnouncement(bool active) { |
| 109 | ALOGI("%s active %d", __FUNCTION__, active); |
| 110 | return Void(); |
| 111 | } |
| 112 | |
| 113 | virtual Return<void> emergencyAnnouncement(bool active) { |
| 114 | ALOGI("%s active %d", __FUNCTION__, active); |
| 115 | return Void(); |
| 116 | } |
| 117 | |
| 118 | virtual Return<void> newMetadata(uint32_t channel __unused, uint32_t subChannel __unused, |
| 119 | const ::android::hardware::hidl_vec<MetaData>& metadata __unused) { |
| 120 | ALOGI("%s", __FUNCTION__); |
| 121 | return Void(); |
| 122 | } |
| 123 | |
| 124 | MyCallback(BroadcastRadioHidlTest *parentTest) : mParentTest(parentTest) {} |
| 125 | |
| 126 | private: |
| 127 | // BroadcastRadioHidlTest instance to which callbacks will be notified. |
| 128 | BroadcastRadioHidlTest *mParentTest; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Method called by MyCallback when a callback with no status or boolean value is received |
| 134 | */ |
| 135 | void onCallback() { |
| 136 | Mutex::Autolock _l(mLock); |
| 137 | onCallback_l(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Method called by MyCallback when hardwareFailure() callback is received |
| 142 | */ |
| 143 | void onHwFailureCallback() { |
| 144 | Mutex::Autolock _l(mLock); |
| 145 | mHwFailure = true; |
| 146 | onCallback_l(); |
| 147 | } |
| 148 | |
| 149 | /** |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 150 | * Method called by MyCallback when configChange() callback is received. |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 151 | */ |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 152 | void onConfigChangeCallback(Result result, const BandConfig& config) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 153 | Mutex::Autolock _l(mLock); |
| 154 | mResultCallbackData = result; |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 155 | mBandConfigCallbackData = config; |
| 156 | onCallback_l(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Method called by MyCallback when tuneComplete() callback is received. |
| 161 | */ |
| 162 | void onTuneCompleteCallback(Result result, const ProgramInfo& info) { |
| 163 | Mutex::Autolock _l(mLock); |
| 164 | mResultCallbackData = result; |
| 165 | mProgramInfoCallbackData = info; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 166 | onCallback_l(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Method called by MyCallback when a boolean indication is received |
| 171 | */ |
| 172 | void onBoolCallback(bool result) { |
| 173 | Mutex::Autolock _l(mLock); |
| 174 | mBoolCallbackData = result; |
| 175 | onCallback_l(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | BroadcastRadioHidlTest() : |
| 180 | mCallbackCalled(false), mBoolCallbackData(false), |
| 181 | mResultCallbackData(Result::OK), mHwFailure(false) {} |
| 182 | |
| 183 | void onCallback_l() { |
| 184 | if (!mCallbackCalled) { |
| 185 | mCallbackCalled = true; |
| 186 | mCallbackCond.broadcast(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | bool waitForCallback(nsecs_t reltime = 0) { |
| 192 | Mutex::Autolock _l(mLock); |
| 193 | nsecs_t endTime = systemTime() + reltime; |
| 194 | while (!mCallbackCalled) { |
| 195 | if (reltime == 0) { |
| 196 | mCallbackCond.wait(mLock); |
| 197 | } else { |
| 198 | nsecs_t now = systemTime(); |
| 199 | if (now > endTime) { |
| 200 | return false; |
| 201 | } |
| 202 | mCallbackCond.waitRelative(mLock, endTime - now); |
| 203 | } |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool getProperties(); |
| 209 | bool openTuner(); |
| 210 | bool checkAntenna(); |
| 211 | |
| 212 | static const nsecs_t kConfigCallbacktimeoutNs = seconds_to_nanoseconds(10); |
| 213 | static const nsecs_t kTuneCallbacktimeoutNs = seconds_to_nanoseconds(30); |
| 214 | |
| 215 | sp<IBroadcastRadio> mRadio; |
| 216 | Properties mHalProperties; |
| 217 | sp<ITuner> mTuner; |
| 218 | sp<MyCallback> mTunerCallback; |
| 219 | Mutex mLock; |
| 220 | Condition mCallbackCond; |
| 221 | bool mCallbackCalled; |
| 222 | bool mBoolCallbackData; |
| 223 | Result mResultCallbackData; |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 224 | ProgramInfo mProgramInfoCallbackData; |
| 225 | BandConfig mBandConfigCallbackData; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 226 | bool mHwFailure; |
| 227 | }; |
| 228 | |
| 229 | // A class for test environment setup (kept since this file is a template). |
| 230 | class BroadcastRadioHidlEnvironment : public ::testing::Environment { |
| 231 | public: |
| 232 | virtual void SetUp() {} |
| 233 | virtual void TearDown() {} |
| 234 | }; |
| 235 | |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 236 | namespace android { |
| 237 | namespace hardware { |
| 238 | namespace broadcastradio { |
| 239 | namespace V1_0 { |
| 240 | |
| 241 | /** |
| 242 | * Compares two BandConfig objects for testing purposes. |
| 243 | */ |
| 244 | static bool operator==(const BandConfig& l, const BandConfig& r) { |
| 245 | if (l.type != r.type) return false; |
| 246 | if (l.antennaConnected != r.antennaConnected) return false; |
| 247 | if (l.lowerLimit != r.lowerLimit) return false; |
| 248 | if (l.upperLimit != r.upperLimit) return false; |
| 249 | if (l.spacings != r.spacings) return false; |
| 250 | if (l.type == Band::AM || l.type == Band::AM_HD) { |
| 251 | return l.ext.am == r.ext.am; |
| 252 | } else if (l.type == Band::FM || l.type == Band::FM_HD) { |
| 253 | return l.ext.fm == r.ext.fm; |
| 254 | } else { |
| 255 | // unsupported type |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } // V1_0 |
| 261 | } // broadcastradio |
| 262 | } // hardware |
| 263 | } // android |
| 264 | |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 265 | bool BroadcastRadioHidlTest::getProperties() |
| 266 | { |
| 267 | if (mHalProperties.bands.size() == 0) { |
| 268 | Result halResult = Result::NOT_INITIALIZED; |
| 269 | Return<void> hidlReturn = |
| 270 | mRadio->getProperties([&](Result result, const Properties& properties) { |
| 271 | halResult = result; |
| 272 | if (result == Result::OK) { |
| 273 | mHalProperties = properties; |
| 274 | } |
| 275 | }); |
| 276 | |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 277 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 278 | EXPECT_EQ(Result::OK, halResult); |
| 279 | EXPECT_EQ(Class::AM_FM, mHalProperties.classId); |
| 280 | EXPECT_GT(mHalProperties.numTuners, 0u); |
| 281 | EXPECT_GT(mHalProperties.bands.size(), 0u); |
| 282 | } |
| 283 | return mHalProperties.bands.size() > 0; |
| 284 | } |
| 285 | |
| 286 | bool BroadcastRadioHidlTest::openTuner() |
| 287 | { |
| 288 | if (!getProperties()) { |
| 289 | return false; |
| 290 | } |
| 291 | if (mTuner.get() == nullptr) { |
| 292 | Result halResult = Result::NOT_INITIALIZED; |
| 293 | Return<void> hidlReturn = |
| 294 | mRadio->openTuner(mHalProperties.bands[0], true, mTunerCallback, |
| 295 | [&](Result result, const sp<ITuner>& tuner) { |
| 296 | halResult = result; |
| 297 | if (result == Result::OK) { |
| 298 | mTuner = tuner; |
| 299 | } |
| 300 | }); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 301 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 302 | EXPECT_EQ(Result::OK, halResult); |
| 303 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 304 | } |
| 305 | EXPECT_NE(nullptr, mTuner.get()); |
| 306 | return nullptr != mTuner.get(); |
| 307 | } |
| 308 | |
| 309 | bool BroadcastRadioHidlTest::checkAntenna() |
| 310 | { |
| 311 | BandConfig halConfig; |
| 312 | Result halResult = Result::NOT_INITIALIZED; |
| 313 | Return<void> hidlReturn = |
| 314 | mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 315 | halResult = result; |
| 316 | if (result == Result::OK) { |
| 317 | halConfig = config; |
| 318 | } |
| 319 | }); |
| 320 | |
| 321 | return ((halResult == Result::OK) && (halConfig.antennaConnected == true)); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /** |
| 326 | * Test IBroadcastRadio::getProperties() method |
| 327 | * |
| 328 | * Verifies that: |
| 329 | * - the HAL implements the method |
| 330 | * - the method returns 0 (no error) |
| 331 | * - the implementation class is AM_FM |
| 332 | * - the implementation supports at least one tuner |
| 333 | * - the implementation supports at one band |
| 334 | */ |
| 335 | TEST_F(BroadcastRadioHidlTest, GetProperties) { |
| 336 | EXPECT_EQ(true, getProperties()); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Test IBroadcastRadio::openTuner() method |
| 341 | * |
| 342 | * Verifies that: |
| 343 | * - the HAL implements the method |
| 344 | * - the method returns 0 (no error) and a valid ITuner interface |
| 345 | */ |
| 346 | TEST_F(BroadcastRadioHidlTest, OpenTuner) { |
| 347 | EXPECT_EQ(true, openTuner()); |
| 348 | } |
| 349 | |
| 350 | /** |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 351 | * Test IBroadcastRadio::openTuner() after ITuner disposal. |
| 352 | * |
| 353 | * Verifies that: |
| 354 | * - ITuner destruction gets propagated through HAL |
| 355 | * - the openTuner method works well when called for the second time |
| 356 | */ |
| 357 | TEST_F(BroadcastRadioHidlTest, ReopenTuner) { |
| 358 | EXPECT_TRUE(openTuner()); |
| 359 | mTuner.clear(); |
| 360 | EXPECT_TRUE(openTuner()); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Test IBroadcastRadio::openTuner() method called twice. |
| 365 | * |
| 366 | * Verifies that: |
| 367 | * - the openTuner method fails when called for the second time without deleting previous |
| 368 | * ITuner instance |
| 369 | */ |
| 370 | TEST_F(BroadcastRadioHidlTest, OpenTunerTwice) { |
| 371 | EXPECT_TRUE(openTuner()); |
| 372 | |
| 373 | Result halResult = Result::NOT_INITIALIZED; |
| 374 | Return<void> hidlReturn = |
| 375 | mRadio->openTuner(mHalProperties.bands[0], true, mTunerCallback, |
| 376 | [&](Result result, const sp<ITuner>&) { |
| 377 | halResult = result; |
| 378 | }); |
| 379 | EXPECT_TRUE(hidlReturn.isOk()); |
| 380 | EXPECT_EQ(Result::INVALID_STATE, halResult); |
| 381 | EXPECT_TRUE(waitForCallback(kConfigCallbacktimeoutNs)); |
| 382 | } |
| 383 | |
| 384 | /** |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 385 | * Test ITuner::setConfiguration() and getConfiguration methods |
| 386 | * |
| 387 | * Verifies that: |
| 388 | * - the HAL implements both methods |
| 389 | * - the methods return 0 (no error) |
| 390 | * - the configuration callback is received within kConfigCallbacktimeoutNs ns |
| 391 | * - the configuration read back from HAl has the same class Id |
| 392 | */ |
| 393 | TEST_F(BroadcastRadioHidlTest, SetAndGetConfiguration) { |
| 394 | ASSERT_EQ(true, openTuner()); |
| 395 | // test setConfiguration |
| 396 | mCallbackCalled = false; |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 397 | Return<Result> hidlResult = mTuner->setConfiguration(mHalProperties.bands[1]); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 398 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 399 | EXPECT_EQ(Result::OK, hidlResult); |
| 400 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 401 | EXPECT_EQ(Result::OK, mResultCallbackData); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 402 | EXPECT_EQ(mHalProperties.bands[1], mBandConfigCallbackData); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 403 | |
| 404 | // test getConfiguration |
| 405 | BandConfig halConfig; |
| 406 | Result halResult; |
| 407 | Return<void> hidlReturn = |
| 408 | mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 409 | halResult = result; |
| 410 | if (result == Result::OK) { |
| 411 | halConfig = config; |
| 412 | } |
| 413 | }); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 414 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 415 | EXPECT_EQ(Result::OK, halResult); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 416 | EXPECT_EQ(mHalProperties.bands[1], halConfig); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
Tomasz Wasilczyk | 10877cd | 2017-03-07 17:04:26 -0800 | [diff] [blame] | 420 | * Test ITuner::setConfiguration() with invalid arguments. |
| 421 | * |
| 422 | * Verifies that: |
| 423 | * - the methods returns INVALID_ARGUMENTS on invalid arguments |
| 424 | * - the method recovers and succeeds after passing correct arguments |
| 425 | */ |
| 426 | TEST_F(BroadcastRadioHidlTest, SetConfigurationFails) { |
| 427 | ASSERT_EQ(true, openTuner()); |
| 428 | |
| 429 | // Let's define a config that's bad for sure. |
| 430 | BandConfig badConfig = {}; |
| 431 | badConfig.type = Band::FM; |
| 432 | badConfig.lowerLimit = 0xFFFFFFFF; |
| 433 | badConfig.upperLimit = 0; |
| 434 | badConfig.spacings = (std::vector<uint32_t>){ 0 }; |
| 435 | |
| 436 | // Test setConfiguration failing on bad data. |
| 437 | mCallbackCalled = false; |
| 438 | auto setResult = mTuner->setConfiguration(badConfig); |
| 439 | EXPECT_TRUE(setResult.isOk()); |
| 440 | EXPECT_EQ(Result::INVALID_ARGUMENTS, setResult); |
| 441 | |
| 442 | // Test setConfiguration recovering after passing good data. |
| 443 | mCallbackCalled = false; |
| 444 | setResult = mTuner->setConfiguration(mHalProperties.bands[0]); |
| 445 | EXPECT_TRUE(setResult.isOk()); |
| 446 | EXPECT_EQ(Result::OK, setResult); |
| 447 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 448 | EXPECT_EQ(Result::OK, mResultCallbackData); |
| 449 | } |
| 450 | |
| 451 | /** |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 452 | * Test ITuner::scan |
| 453 | * |
| 454 | * Verifies that: |
| 455 | * - the HAL implements the method |
| 456 | * - the method returns 0 (no error) |
| 457 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame^] | 458 | * - skipping sub-channel or not does not fail the call |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 459 | */ |
| 460 | TEST_F(BroadcastRadioHidlTest, Scan) { |
| 461 | ASSERT_EQ(true, openTuner()); |
| 462 | ASSERT_TRUE(checkAntenna()); |
| 463 | // test scan UP |
| 464 | mCallbackCalled = false; |
| 465 | Return<Result> hidlResult = mTuner->scan(Direction::UP, true); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 466 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 467 | EXPECT_EQ(Result::OK, hidlResult); |
| 468 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 469 | |
| 470 | // test scan DOWN |
| 471 | mCallbackCalled = false; |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame^] | 472 | hidlResult = mTuner->scan(Direction::DOWN, false); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 473 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 474 | EXPECT_EQ(Result::OK, hidlResult); |
| 475 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Test ITuner::step |
| 480 | * |
| 481 | * Verifies that: |
| 482 | * - the HAL implements the method |
| 483 | * - the method returns 0 (no error) |
| 484 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame^] | 485 | * - skipping sub-channel or not does not fail the call |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 486 | */ |
| 487 | TEST_F(BroadcastRadioHidlTest, Step) { |
| 488 | ASSERT_EQ(true, openTuner()); |
| 489 | ASSERT_TRUE(checkAntenna()); |
| 490 | // test step UP |
| 491 | mCallbackCalled = false; |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame^] | 492 | Return<Result> hidlResult = mTuner->step(Direction::UP, false); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 493 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 494 | EXPECT_EQ(Result::OK, hidlResult); |
| 495 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 496 | |
| 497 | // test step DOWN |
| 498 | mCallbackCalled = false; |
| 499 | hidlResult = mTuner->step(Direction::DOWN, true); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 500 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 501 | EXPECT_EQ(Result::OK, hidlResult); |
| 502 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Test ITuner::tune, getProgramInformation and cancel methods |
| 507 | * |
| 508 | * Verifies that: |
| 509 | * - the HAL implements the methods |
| 510 | * - the methods return 0 (no error) |
| 511 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns after tune() |
| 512 | */ |
| 513 | TEST_F(BroadcastRadioHidlTest, TuneAndGetProgramInformationAndCancel) { |
| 514 | ASSERT_EQ(true, openTuner()); |
| 515 | ASSERT_TRUE(checkAntenna()); |
| 516 | |
| 517 | // test tune |
| 518 | ASSERT_GT(mHalProperties.bands[0].spacings.size(), 0u); |
| 519 | ASSERT_GT(mHalProperties.bands[0].upperLimit, mHalProperties.bands[0].lowerLimit); |
| 520 | |
| 521 | // test scan UP |
| 522 | uint32_t lowerLimit = mHalProperties.bands[0].lowerLimit; |
| 523 | uint32_t upperLimit = mHalProperties.bands[0].upperLimit; |
| 524 | uint32_t spacing = mHalProperties.bands[0].spacings[0]; |
| 525 | |
| 526 | uint32_t channel = |
| 527 | lowerLimit + (((upperLimit - lowerLimit) / 2 + spacing - 1) / spacing) * spacing; |
| 528 | mCallbackCalled = false; |
| 529 | mResultCallbackData = Result::NOT_INITIALIZED; |
| 530 | Return<Result> hidlResult = mTuner->tune(channel, 0); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 531 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 532 | EXPECT_EQ(Result::OK, hidlResult); |
| 533 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 534 | EXPECT_EQ(channel, mProgramInfoCallbackData.channel); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 535 | |
| 536 | // test getProgramInformation |
| 537 | ProgramInfo halInfo; |
| 538 | Result halResult = Result::NOT_INITIALIZED; |
| 539 | Return<void> hidlReturn = mTuner->getProgramInformation( |
Tomasz Wasilczyk | 5cc9d86 | 2017-01-06 14:19:11 -0800 | [diff] [blame] | 540 | [&](Result result, const ProgramInfo& info) { |
| 541 | halResult = result; |
| 542 | if (result == Result::OK) { |
| 543 | halInfo = info; |
| 544 | } |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 545 | }); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 546 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 547 | EXPECT_EQ(Result::OK, halResult); |
| 548 | if (mResultCallbackData == Result::OK) { |
| 549 | EXPECT_EQ(true, halInfo.tuned); |
| 550 | EXPECT_LE(halInfo.channel, upperLimit); |
| 551 | EXPECT_GE(halInfo.channel, lowerLimit); |
| 552 | } else { |
| 553 | EXPECT_EQ(false, halInfo.tuned); |
| 554 | } |
| 555 | |
| 556 | // test cancel |
| 557 | mTuner->tune(lowerLimit, 0); |
| 558 | hidlResult = mTuner->cancel(); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 559 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 560 | EXPECT_EQ(Result::OK, hidlResult); |
| 561 | } |
| 562 | |
Tomasz Wasilczyk | 59d985d | 2017-03-03 13:02:15 -0800 | [diff] [blame] | 563 | /** |
| 564 | * Test ITuner::tune failing when channel out of the range is provided. |
| 565 | * |
| 566 | * Verifies that: |
| 567 | * - the method returns INVALID_ARGUMENTS when applicable |
| 568 | * - the method recovers and succeeds after passing correct arguments |
| 569 | */ |
| 570 | TEST_F(BroadcastRadioHidlTest, TuneFailsOutOfBounds) { |
| 571 | ASSERT_TRUE(openTuner()); |
| 572 | ASSERT_TRUE(checkAntenna()); |
| 573 | |
| 574 | // get current channel bounds |
| 575 | BandConfig halConfig; |
| 576 | Result halResult; |
| 577 | auto configResult = mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 578 | halResult = result; |
| 579 | halConfig = config; |
| 580 | }); |
| 581 | ASSERT_TRUE(configResult.isOk()); |
| 582 | ASSERT_EQ(Result::OK, halResult); |
| 583 | |
| 584 | // try to tune slightly above the limit and expect to fail |
| 585 | auto badChannel = halConfig.upperLimit + halConfig.spacings[0]; |
| 586 | auto tuneResult = mTuner->tune(badChannel, 0); |
| 587 | EXPECT_TRUE(tuneResult.isOk()); |
| 588 | EXPECT_EQ(Result::INVALID_ARGUMENTS, tuneResult); |
| 589 | EXPECT_TRUE(waitForCallback(kTuneCallbacktimeoutNs)); |
| 590 | |
| 591 | // tuning exactly at the limit should succeed |
| 592 | auto goodChannel = halConfig.upperLimit; |
| 593 | tuneResult = mTuner->tune(goodChannel, 0); |
| 594 | EXPECT_TRUE(tuneResult.isOk()); |
| 595 | EXPECT_EQ(Result::OK, tuneResult); |
| 596 | EXPECT_TRUE(waitForCallback(kTuneCallbacktimeoutNs)); |
| 597 | } |
| 598 | |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 599 | |
| 600 | int main(int argc, char** argv) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 601 | ::testing::AddGlobalTestEnvironment(new BroadcastRadioHidlEnvironment); |
| 602 | ::testing::InitGoogleTest(&argc, argv); |
| 603 | int status = RUN_ALL_TESTS(); |
| 604 | ALOGI("Test result = %d", status); |
| 605 | return status; |
| 606 | } |