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