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(); |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 231 | BandConfig& getBand(unsigned idx); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 232 | |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 233 | static const nsecs_t kConnectCallbacktimeoutNs = seconds_to_nanoseconds(1); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 234 | static const nsecs_t kConfigCallbacktimeoutNs = seconds_to_nanoseconds(10); |
| 235 | static const nsecs_t kTuneCallbacktimeoutNs = seconds_to_nanoseconds(30); |
| 236 | |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 237 | Class radioClass; |
| 238 | bool skipped; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 239 | sp<IBroadcastRadio> mRadio; |
| 240 | Properties mHalProperties; |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 241 | bool mHalPropertiesInitialized = false; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 242 | sp<ITuner> mTuner; |
| 243 | sp<MyCallback> mTunerCallback; |
| 244 | Mutex mLock; |
| 245 | Condition mCallbackCond; |
| 246 | bool mCallbackCalled; |
| 247 | bool mBoolCallbackData; |
| 248 | Result mResultCallbackData; |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 249 | ProgramInfo mProgramInfoCallbackData; |
| 250 | BandConfig mBandConfigCallbackData; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 251 | bool mHwFailure; |
| 252 | }; |
| 253 | |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 254 | namespace android { |
| 255 | namespace hardware { |
| 256 | namespace broadcastradio { |
| 257 | namespace V1_0 { |
| 258 | |
| 259 | /** |
| 260 | * Compares two BandConfig objects for testing purposes. |
| 261 | */ |
| 262 | static bool operator==(const BandConfig& l, const BandConfig& r) { |
| 263 | if (l.type != r.type) return false; |
| 264 | if (l.antennaConnected != r.antennaConnected) return false; |
| 265 | if (l.lowerLimit != r.lowerLimit) return false; |
| 266 | if (l.upperLimit != r.upperLimit) return false; |
| 267 | if (l.spacings != r.spacings) return false; |
| 268 | if (l.type == Band::AM || l.type == Band::AM_HD) { |
| 269 | return l.ext.am == r.ext.am; |
| 270 | } else if (l.type == Band::FM || l.type == Band::FM_HD) { |
| 271 | return l.ext.fm == r.ext.fm; |
| 272 | } else { |
| 273 | // unsupported type |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | } // V1_0 |
| 279 | } // broadcastradio |
| 280 | } // hardware |
| 281 | } // android |
| 282 | |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 283 | bool BroadcastRadioHidlTest::getProperties() |
| 284 | { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 285 | if (mHalPropertiesInitialized) return true; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 286 | |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 287 | Result halResult = Result::NOT_INITIALIZED; |
| 288 | auto hidlReturn = mRadio->getProperties([&](Result result, const Properties& properties) { |
| 289 | halResult = result; |
| 290 | if (result == Result::OK) { |
| 291 | mHalProperties = properties; |
| 292 | } |
| 293 | }); |
| 294 | |
| 295 | EXPECT_TRUE(hidlReturn.isOk()); |
| 296 | EXPECT_EQ(Result::OK, halResult); |
| 297 | EXPECT_EQ(radioClass, mHalProperties.classId); |
| 298 | EXPECT_GT(mHalProperties.numTuners, 0u); |
| 299 | if (radioClass == Class::AM_FM) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 300 | EXPECT_GT(mHalProperties.bands.size(), 0u); |
| 301 | } |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 302 | |
| 303 | if (hidlReturn.isOk() && halResult == Result::OK) { |
| 304 | mHalPropertiesInitialized = true; |
| 305 | return true; |
| 306 | } |
| 307 | return false; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | bool BroadcastRadioHidlTest::openTuner() |
| 311 | { |
| 312 | if (!getProperties()) { |
| 313 | return false; |
| 314 | } |
| 315 | if (mTuner.get() == nullptr) { |
| 316 | Result halResult = Result::NOT_INITIALIZED; |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 317 | auto openCb = [&](Result result, const sp<ITuner>& tuner) { |
| 318 | halResult = result; |
| 319 | if (result == Result::OK) { |
| 320 | mTuner = tuner; |
| 321 | } |
| 322 | }; |
| 323 | auto hidlReturn = mRadio->openTuner(getBand(0), true, mTunerCallback, openCb); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 324 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 325 | EXPECT_EQ(Result::OK, halResult); |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 326 | if (radioClass == Class::AM_FM) { |
| 327 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 328 | } |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 329 | } |
| 330 | EXPECT_NE(nullptr, mTuner.get()); |
| 331 | return nullptr != mTuner.get(); |
| 332 | } |
| 333 | |
| 334 | bool BroadcastRadioHidlTest::checkAntenna() |
| 335 | { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 336 | if (radioClass != Class::AM_FM) return true; |
| 337 | |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 338 | BandConfig halConfig; |
| 339 | Result halResult = Result::NOT_INITIALIZED; |
| 340 | Return<void> hidlReturn = |
| 341 | mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 342 | halResult = result; |
| 343 | if (result == Result::OK) { |
| 344 | halConfig = config; |
| 345 | } |
| 346 | }); |
| 347 | |
| 348 | return ((halResult == Result::OK) && (halConfig.antennaConnected == true)); |
| 349 | } |
| 350 | |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 351 | BandConfig& BroadcastRadioHidlTest::getBand(unsigned idx) { |
| 352 | static BandConfig dummyBandConfig = {}; |
| 353 | if (radioClass == Class::AM_FM) { |
| 354 | EXPECT_GT(mHalProperties.bands.size(), idx); |
| 355 | if (mHalProperties.bands.size() > idx) { |
| 356 | return mHalProperties.bands[idx]; |
| 357 | } else { |
| 358 | return dummyBandConfig; |
| 359 | } |
| 360 | } else { |
| 361 | return dummyBandConfig; |
| 362 | } |
| 363 | } |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * Test IBroadcastRadio::getProperties() method |
| 367 | * |
| 368 | * Verifies that: |
| 369 | * - the HAL implements the method |
| 370 | * - the method returns 0 (no error) |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 371 | * - the implementation class is radioClass |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 372 | * - the implementation supports at least one tuner |
| 373 | * - the implementation supports at one band |
| 374 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 375 | TEST_P(BroadcastRadioHidlTest, GetProperties) { |
| 376 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 377 | EXPECT_EQ(true, getProperties()); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Test IBroadcastRadio::openTuner() method |
| 382 | * |
| 383 | * Verifies that: |
| 384 | * - the HAL implements the method |
| 385 | * - the method returns 0 (no error) and a valid ITuner interface |
| 386 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 387 | TEST_P(BroadcastRadioHidlTest, OpenTuner) { |
| 388 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 389 | EXPECT_EQ(true, openTuner()); |
| 390 | } |
| 391 | |
| 392 | /** |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 393 | * Test IBroadcastRadio::openTuner() after ITuner disposal. |
| 394 | * |
| 395 | * Verifies that: |
| 396 | * - ITuner destruction gets propagated through HAL |
| 397 | * - the openTuner method works well when called for the second time |
| 398 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 399 | TEST_P(BroadcastRadioHidlTest, ReopenTuner) { |
| 400 | RETURN_IF_SKIPPED; |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 401 | EXPECT_TRUE(openTuner()); |
| 402 | mTuner.clear(); |
| 403 | EXPECT_TRUE(openTuner()); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Test IBroadcastRadio::openTuner() method called twice. |
| 408 | * |
| 409 | * Verifies that: |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 410 | * - the openTuner method fails with INVALID_STATE or succeeds when called for the second time |
| 411 | * without deleting previous ITuner instance |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 412 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 413 | TEST_P(BroadcastRadioHidlTest, OpenTunerTwice) { |
| 414 | RETURN_IF_SKIPPED; |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 415 | EXPECT_TRUE(openTuner()); |
| 416 | |
| 417 | Result halResult = Result::NOT_INITIALIZED; |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 418 | auto openCb = [&](Result result, const sp<ITuner>&) { halResult = result; }; |
| 419 | auto hidlReturn = mRadio->openTuner(getBand(0), true, mTunerCallback, openCb); |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 420 | EXPECT_TRUE(hidlReturn.isOk()); |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 421 | if (halResult == Result::INVALID_STATE) { |
| 422 | EXPECT_TRUE(waitForCallback(kConfigCallbacktimeoutNs)); |
| 423 | } |
Tomasz Wasilczyk | be71e9c | 2016-12-22 11:49:17 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 427 | * Test ITuner::setConfiguration() and getConfiguration methods |
| 428 | * |
| 429 | * Verifies that: |
| 430 | * - the HAL implements both methods |
| 431 | * - the methods return 0 (no error) |
| 432 | * - the configuration callback is received within kConfigCallbacktimeoutNs ns |
| 433 | * - the configuration read back from HAl has the same class Id |
| 434 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 435 | TEST_P(BroadcastRadioHidlTest, SetAndGetConfiguration) { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 436 | if (radioClass != Class::AM_FM) skipped = true; |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 437 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 438 | ASSERT_EQ(true, openTuner()); |
| 439 | // test setConfiguration |
| 440 | mCallbackCalled = false; |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 441 | Return<Result> hidlResult = mTuner->setConfiguration(getBand(1)); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 442 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 443 | EXPECT_EQ(Result::OK, hidlResult); |
| 444 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 445 | EXPECT_EQ(Result::OK, mResultCallbackData); |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 446 | EXPECT_EQ(getBand(1), mBandConfigCallbackData); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 447 | |
| 448 | // test getConfiguration |
| 449 | BandConfig halConfig; |
| 450 | Result halResult; |
| 451 | Return<void> hidlReturn = |
| 452 | mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 453 | halResult = result; |
| 454 | if (result == Result::OK) { |
| 455 | halConfig = config; |
| 456 | } |
| 457 | }); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 458 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 459 | EXPECT_EQ(Result::OK, halResult); |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 460 | EXPECT_EQ(getBand(1), halConfig); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
Tomasz Wasilczyk | 10877cd | 2017-03-07 17:04:26 -0800 | [diff] [blame] | 464 | * Test ITuner::setConfiguration() with invalid arguments. |
| 465 | * |
| 466 | * Verifies that: |
| 467 | * - the methods returns INVALID_ARGUMENTS on invalid arguments |
| 468 | * - the method recovers and succeeds after passing correct arguments |
| 469 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 470 | TEST_P(BroadcastRadioHidlTest, SetConfigurationFails) { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 471 | if (radioClass != Class::AM_FM) skipped = true; |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 472 | RETURN_IF_SKIPPED; |
Tomasz Wasilczyk | 10877cd | 2017-03-07 17:04:26 -0800 | [diff] [blame] | 473 | ASSERT_EQ(true, openTuner()); |
| 474 | |
| 475 | // Let's define a config that's bad for sure. |
| 476 | BandConfig badConfig = {}; |
| 477 | badConfig.type = Band::FM; |
| 478 | badConfig.lowerLimit = 0xFFFFFFFF; |
| 479 | badConfig.upperLimit = 0; |
| 480 | badConfig.spacings = (std::vector<uint32_t>){ 0 }; |
| 481 | |
| 482 | // Test setConfiguration failing on bad data. |
| 483 | mCallbackCalled = false; |
| 484 | auto setResult = mTuner->setConfiguration(badConfig); |
| 485 | EXPECT_TRUE(setResult.isOk()); |
| 486 | EXPECT_EQ(Result::INVALID_ARGUMENTS, setResult); |
| 487 | |
| 488 | // Test setConfiguration recovering after passing good data. |
| 489 | mCallbackCalled = false; |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 490 | setResult = mTuner->setConfiguration(getBand(0)); |
Tomasz Wasilczyk | 10877cd | 2017-03-07 17:04:26 -0800 | [diff] [blame] | 491 | EXPECT_TRUE(setResult.isOk()); |
| 492 | EXPECT_EQ(Result::OK, setResult); |
| 493 | EXPECT_EQ(true, waitForCallback(kConfigCallbacktimeoutNs)); |
| 494 | EXPECT_EQ(Result::OK, mResultCallbackData); |
| 495 | } |
| 496 | |
| 497 | /** |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 498 | * Test ITuner::scan |
| 499 | * |
| 500 | * Verifies that: |
| 501 | * - the HAL implements the method |
| 502 | * - the method returns 0 (no error) |
| 503 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame] | 504 | * - skipping sub-channel or not does not fail the call |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 505 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 506 | TEST_P(BroadcastRadioHidlTest, Scan) { |
| 507 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 508 | ASSERT_EQ(true, openTuner()); |
| 509 | ASSERT_TRUE(checkAntenna()); |
| 510 | // test scan UP |
| 511 | mCallbackCalled = false; |
| 512 | Return<Result> hidlResult = mTuner->scan(Direction::UP, true); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 513 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 514 | EXPECT_EQ(Result::OK, hidlResult); |
| 515 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 516 | |
| 517 | // test scan DOWN |
| 518 | mCallbackCalled = false; |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame] | 519 | hidlResult = mTuner->scan(Direction::DOWN, false); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 520 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 521 | EXPECT_EQ(Result::OK, hidlResult); |
| 522 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Test ITuner::step |
| 527 | * |
| 528 | * Verifies that: |
| 529 | * - the HAL implements the method |
| 530 | * - the method returns 0 (no error) |
| 531 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame] | 532 | * - skipping sub-channel or not does not fail the call |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 533 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 534 | TEST_P(BroadcastRadioHidlTest, Step) { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 535 | if (radioClass != Class::AM_FM) skipped = true; |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 536 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 537 | ASSERT_EQ(true, openTuner()); |
| 538 | ASSERT_TRUE(checkAntenna()); |
| 539 | // test step UP |
| 540 | mCallbackCalled = false; |
Tomasz Wasilczyk | a8dec0f | 2017-03-14 10:20:53 -0700 | [diff] [blame] | 541 | Return<Result> hidlResult = mTuner->step(Direction::UP, false); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 542 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 543 | EXPECT_EQ(Result::OK, hidlResult); |
| 544 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 545 | |
| 546 | // test step DOWN |
| 547 | mCallbackCalled = false; |
| 548 | hidlResult = mTuner->step(Direction::DOWN, true); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 549 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 550 | EXPECT_EQ(Result::OK, hidlResult); |
| 551 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Test ITuner::tune, getProgramInformation and cancel methods |
| 556 | * |
| 557 | * Verifies that: |
| 558 | * - the HAL implements the methods |
| 559 | * - the methods return 0 (no error) |
| 560 | * - the tuned callback is received within kTuneCallbacktimeoutNs ns after tune() |
| 561 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 562 | TEST_P(BroadcastRadioHidlTest, TuneAndGetProgramInformationAndCancel) { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 563 | if (radioClass != Class::AM_FM) skipped = true; |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 564 | RETURN_IF_SKIPPED; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 565 | ASSERT_EQ(true, openTuner()); |
| 566 | ASSERT_TRUE(checkAntenna()); |
| 567 | |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 568 | auto& band = getBand(0); |
| 569 | |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 570 | // test tune |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 571 | ASSERT_GT(band.spacings.size(), 0u); |
| 572 | ASSERT_GT(band.upperLimit, band.lowerLimit); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 573 | |
| 574 | // test scan UP |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 575 | uint32_t lowerLimit = band.lowerLimit; |
| 576 | uint32_t upperLimit = band.upperLimit; |
| 577 | uint32_t spacing = band.spacings[0]; |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 578 | |
| 579 | uint32_t channel = |
| 580 | lowerLimit + (((upperLimit - lowerLimit) / 2 + spacing - 1) / spacing) * spacing; |
| 581 | mCallbackCalled = false; |
| 582 | mResultCallbackData = Result::NOT_INITIALIZED; |
| 583 | Return<Result> hidlResult = mTuner->tune(channel, 0); |
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 | EXPECT_EQ(true, waitForCallback(kTuneCallbacktimeoutNs)); |
Tomasz Wasilczyk | f3c036d | 2017-03-03 15:07:50 -0800 | [diff] [blame] | 587 | EXPECT_EQ(channel, mProgramInfoCallbackData.channel); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 588 | |
| 589 | // test getProgramInformation |
| 590 | ProgramInfo halInfo; |
| 591 | Result halResult = Result::NOT_INITIALIZED; |
| 592 | Return<void> hidlReturn = mTuner->getProgramInformation( |
Tomasz Wasilczyk | 5cc9d86 | 2017-01-06 14:19:11 -0800 | [diff] [blame] | 593 | [&](Result result, const ProgramInfo& info) { |
| 594 | halResult = result; |
| 595 | if (result == Result::OK) { |
| 596 | halInfo = info; |
| 597 | } |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 598 | }); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 599 | EXPECT_TRUE(hidlReturn.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 600 | EXPECT_EQ(Result::OK, halResult); |
| 601 | if (mResultCallbackData == Result::OK) { |
| 602 | EXPECT_EQ(true, halInfo.tuned); |
| 603 | EXPECT_LE(halInfo.channel, upperLimit); |
| 604 | EXPECT_GE(halInfo.channel, lowerLimit); |
| 605 | } else { |
| 606 | EXPECT_EQ(false, halInfo.tuned); |
| 607 | } |
| 608 | |
| 609 | // test cancel |
| 610 | mTuner->tune(lowerLimit, 0); |
| 611 | hidlResult = mTuner->cancel(); |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 612 | EXPECT_TRUE(hidlResult.isOk()); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 613 | EXPECT_EQ(Result::OK, hidlResult); |
| 614 | } |
| 615 | |
Tomasz Wasilczyk | 59d985d | 2017-03-03 13:02:15 -0800 | [diff] [blame] | 616 | /** |
| 617 | * Test ITuner::tune failing when channel out of the range is provided. |
| 618 | * |
| 619 | * Verifies that: |
| 620 | * - the method returns INVALID_ARGUMENTS when applicable |
| 621 | * - the method recovers and succeeds after passing correct arguments |
| 622 | */ |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 623 | TEST_P(BroadcastRadioHidlTest, TuneFailsOutOfBounds) { |
Tomasz Wasilczyk | da72d37 | 2017-06-26 16:08:51 -0700 | [diff] [blame^] | 624 | if (radioClass != Class::AM_FM) skipped = true; |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 625 | RETURN_IF_SKIPPED; |
Tomasz Wasilczyk | 59d985d | 2017-03-03 13:02:15 -0800 | [diff] [blame] | 626 | ASSERT_TRUE(openTuner()); |
| 627 | ASSERT_TRUE(checkAntenna()); |
| 628 | |
| 629 | // get current channel bounds |
| 630 | BandConfig halConfig; |
| 631 | Result halResult; |
| 632 | auto configResult = mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 633 | halResult = result; |
| 634 | halConfig = config; |
| 635 | }); |
| 636 | ASSERT_TRUE(configResult.isOk()); |
| 637 | ASSERT_EQ(Result::OK, halResult); |
| 638 | |
| 639 | // try to tune slightly above the limit and expect to fail |
| 640 | auto badChannel = halConfig.upperLimit + halConfig.spacings[0]; |
| 641 | auto tuneResult = mTuner->tune(badChannel, 0); |
| 642 | EXPECT_TRUE(tuneResult.isOk()); |
| 643 | EXPECT_EQ(Result::INVALID_ARGUMENTS, tuneResult); |
| 644 | EXPECT_TRUE(waitForCallback(kTuneCallbacktimeoutNs)); |
| 645 | |
| 646 | // tuning exactly at the limit should succeed |
| 647 | auto goodChannel = halConfig.upperLimit; |
| 648 | tuneResult = mTuner->tune(goodChannel, 0); |
| 649 | EXPECT_TRUE(tuneResult.isOk()); |
| 650 | EXPECT_EQ(Result::OK, tuneResult); |
| 651 | EXPECT_TRUE(waitForCallback(kTuneCallbacktimeoutNs)); |
| 652 | } |
| 653 | |
Tomasz Wasilczyk | 2f46101 | 2017-03-14 11:19:15 -0700 | [diff] [blame] | 654 | INSTANTIATE_TEST_CASE_P( |
| 655 | BroadcastRadioHidlTestCases, |
| 656 | BroadcastRadioHidlTest, |
| 657 | ::testing::Values(Class::AM_FM, Class::SAT, Class::DT)); |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 658 | |
| 659 | int main(int argc, char** argv) { |
Eric Laurent | 566fcda | 2016-11-23 10:36:36 -0800 | [diff] [blame] | 660 | ::testing::InitGoogleTest(&argc, argv); |
| 661 | int status = RUN_ALL_TESTS(); |
| 662 | ALOGI("Test result = %d", status); |
| 663 | return status; |
| 664 | } |