Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "broadcastradio.vts" |
| 18 | |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 19 | #include <VtsHalHidlTargetTestBase.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 21 | #include <call-barrier.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
| 23 | #include <cutils/properties.h> |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 24 | #include <gmock/gmock.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 25 | #include <hidl/HidlTransportSupport.h> |
| 26 | #include <utils/threads.h> |
| 27 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 28 | #include <chrono> |
| 29 | |
| 30 | #include <android/hardware/broadcastradio/1.1/IBroadcastRadio.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 31 | #include <android/hardware/broadcastradio/1.1/IBroadcastRadioFactory.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 32 | #include <android/hardware/broadcastradio/1.1/ITuner.h> |
| 33 | #include <android/hardware/broadcastradio/1.1/ITunerCallback.h> |
| 34 | #include <android/hardware/broadcastradio/1.1/types.h> |
| 35 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 36 | #include "mock-timeout.h" |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 37 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 38 | namespace android { |
| 39 | namespace hardware { |
| 40 | namespace broadcastradio { |
| 41 | namespace V1_1 { |
| 42 | namespace vts { |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 43 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 44 | using namespace std::chrono_literals; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 45 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 46 | using testing::_; |
| 47 | using testing::AnyNumber; |
| 48 | using testing::ByMove; |
| 49 | using testing::DoAll; |
| 50 | using testing::SaveArg; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 51 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 52 | using broadcastradio::vts::CallBarrier; |
| 53 | using V1_0::BandConfig; |
| 54 | using V1_0::Class; |
| 55 | using V1_0::MetaData; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 56 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 57 | static constexpr auto kConfigTimeout = 10s; |
| 58 | static constexpr auto kConnectModuleTimeout = 1s; |
| 59 | static constexpr auto kTuneTimeout = 30s; |
| 60 | static constexpr auto kFullScanTimeout = 1min; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 61 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 62 | static void printSkipped(std::string msg) { |
| 63 | std::cout << "[ SKIPPED ] " << msg << std::endl; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 66 | class TunerCallbackMock : public ITunerCallback { |
| 67 | public: |
| 68 | MOCK_METHOD0(hardwareFailure, Return<void>()); |
| 69 | MOCK_TIMEOUT_METHOD2(configChange, Return<void>(Result, const BandConfig&)); |
| 70 | MOCK_METHOD2(tuneComplete, Return<void>(Result, const V1_0::ProgramInfo&)); |
| 71 | MOCK_TIMEOUT_METHOD2(tuneComplete_1_1, Return<void>(Result, const ProgramInfo&)); |
| 72 | MOCK_METHOD1(afSwitch, Return<void>(const V1_0::ProgramInfo&)); |
| 73 | MOCK_METHOD1(afSwitch_1_1, Return<void>(const ProgramInfo&)); |
| 74 | MOCK_METHOD1(antennaStateChange, Return<void>(bool connected)); |
| 75 | MOCK_METHOD1(trafficAnnouncement, Return<void>(bool active)); |
| 76 | MOCK_METHOD1(emergencyAnnouncement, Return<void>(bool active)); |
| 77 | MOCK_METHOD3(newMetadata, Return<void>(uint32_t ch, uint32_t subCh, const hidl_vec<MetaData>&)); |
| 78 | MOCK_METHOD1(backgroundScanAvailable, Return<void>(bool)); |
| 79 | MOCK_TIMEOUT_METHOD1(backgroundScanComplete, Return<void>(ProgramListResult)); |
| 80 | MOCK_METHOD0(programListChanged, Return<void>()); |
| 81 | }; |
| 82 | |
| 83 | class BroadcastRadioHalTest : public ::testing::VtsHalHidlTargetTestBase, |
| 84 | public ::testing::WithParamInterface<Class> { |
| 85 | protected: |
| 86 | virtual void SetUp() override; |
| 87 | virtual void TearDown() override; |
| 88 | |
| 89 | // TODO(b/36864490): check all bands for good test conditions (ie. FM is more likely to have |
| 90 | // any stations on the list, so don't pick AM blindly). |
| 91 | bool openTuner(unsigned band); |
| 92 | const BandConfig& getBand(unsigned idx); |
| 93 | |
| 94 | Class radioClass; |
| 95 | bool skipped = false; |
| 96 | |
| 97 | sp<IBroadcastRadio> mRadioModule; |
| 98 | sp<ITuner> mTuner; |
| 99 | sp<TunerCallbackMock> mCallback = new TunerCallbackMock(); |
| 100 | |
| 101 | private: |
| 102 | hidl_vec<BandConfig> mBands; |
| 103 | }; |
| 104 | |
| 105 | void BroadcastRadioHalTest::SetUp() { |
| 106 | radioClass = GetParam(); |
| 107 | |
| 108 | // set general expectations for a callback |
| 109 | EXPECT_CALL(*mCallback, hardwareFailure()).Times(0); |
| 110 | |
| 111 | // lookup HIDL service |
| 112 | auto factory = getService<IBroadcastRadioFactory>(); |
| 113 | ASSERT_NE(nullptr, factory.get()); |
| 114 | |
| 115 | // connect radio module |
| 116 | Result connectResult; |
| 117 | CallBarrier onConnect; |
| 118 | factory->connectModule(radioClass, [&](Result ret, const sp<V1_0::IBroadcastRadio>& radio) { |
| 119 | connectResult = ret; |
| 120 | if (ret == Result::OK) mRadioModule = IBroadcastRadio::castFrom(radio); |
| 121 | onConnect.call(); |
| 122 | }); |
| 123 | ASSERT_TRUE(onConnect.waitForCall(kConnectModuleTimeout)); |
| 124 | |
| 125 | if (connectResult == Result::INVALID_ARGUMENTS) { |
| 126 | printSkipped("This device class is not supported."); |
| 127 | skipped = true; |
| 128 | return; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 129 | } |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 130 | ASSERT_EQ(connectResult, Result::OK); |
| 131 | ASSERT_NE(nullptr, mRadioModule.get()); |
| 132 | |
| 133 | // get module properties |
| 134 | Properties prop11; |
| 135 | auto& prop10 = prop11.base; |
| 136 | auto propResult = |
| 137 | mRadioModule->getProperties_1_1([&](const Properties& properties) { prop11 = properties; }); |
| 138 | |
| 139 | ASSERT_TRUE(propResult.isOk()); |
| 140 | EXPECT_EQ(radioClass, prop10.classId); |
| 141 | EXPECT_GT(prop10.numTuners, 0u); |
| 142 | if (radioClass == Class::AM_FM) { |
| 143 | EXPECT_GT(prop10.bands.size(), 0u); |
| 144 | } |
| 145 | mBands = prop10.bands; |
| 146 | } |
| 147 | |
| 148 | void BroadcastRadioHalTest::TearDown() { |
| 149 | mTuner.clear(); |
| 150 | mRadioModule.clear(); |
| 151 | // TODO(b/36864490): wait (with timeout) until mCallback has only one reference |
| 152 | } |
| 153 | |
| 154 | bool BroadcastRadioHalTest::openTuner(unsigned band) { |
| 155 | EXPECT_EQ(nullptr, mTuner.get()); |
| 156 | |
| 157 | if (radioClass == Class::AM_FM) { |
| 158 | EXPECT_TIMEOUT_CALL(*mCallback, configChange, Result::OK, _); |
| 159 | } |
| 160 | |
| 161 | Result halResult = Result::NOT_INITIALIZED; |
| 162 | auto openCb = [&](Result result, const sp<V1_0::ITuner>& tuner) { |
| 163 | halResult = result; |
| 164 | if (result != Result::OK) return; |
| 165 | mTuner = ITuner::castFrom(tuner); |
| 166 | }; |
| 167 | auto hidlResult = mRadioModule->openTuner(getBand(band), true, mCallback, openCb); |
| 168 | |
| 169 | EXPECT_TRUE(hidlResult.isOk()); |
| 170 | EXPECT_EQ(Result::OK, halResult); |
| 171 | EXPECT_NE(nullptr, mTuner.get()); |
| 172 | if (radioClass == Class::AM_FM && mTuner != nullptr) { |
| 173 | EXPECT_TIMEOUT_CALL_WAIT(*mCallback, configChange, kConfigTimeout); |
| 174 | |
| 175 | BandConfig halConfig; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 176 | Result halResult = Result::NOT_INITIALIZED; |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 177 | mTuner->getConfiguration([&](Result result, const BandConfig& config) { |
| 178 | halResult = result; |
| 179 | halConfig = config; |
| 180 | }); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 181 | EXPECT_EQ(Result::OK, halResult); |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 182 | EXPECT_TRUE(halConfig.antennaConnected); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 183 | } |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 184 | |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 185 | EXPECT_NE(nullptr, mTuner.get()); |
| 186 | return nullptr != mTuner.get(); |
| 187 | } |
| 188 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 189 | const BandConfig& BroadcastRadioHalTest::getBand(unsigned idx) { |
| 190 | static const BandConfig dummyBandConfig = {}; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 191 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 192 | if (radioClass != Class::AM_FM) { |
| 193 | ALOGD("Not AM/FM radio, returning dummy band config"); |
| 194 | return dummyBandConfig; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 197 | EXPECT_GT(mBands.size(), idx); |
| 198 | if (mBands.size() <= idx) { |
| 199 | ALOGD("Band index out of bound, returning dummy band config"); |
| 200 | return dummyBandConfig; |
| 201 | } |
| 202 | |
| 203 | auto& band = mBands[idx]; |
| 204 | ALOGD("Returning %s band", toString(band.type).c_str()); |
| 205 | return band; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 208 | /** |
| 209 | * Test IBroadcastRadio::openTuner() method called twice. |
| 210 | * |
| 211 | * Verifies that: |
| 212 | * - the openTuner method succeeds when called for the second time without |
| 213 | * deleting previous ITuner instance. |
| 214 | * |
| 215 | * This is a more strict requirement than in 1.0, where a second openTuner |
| 216 | * might fail. |
| 217 | */ |
| 218 | TEST_P(BroadcastRadioHalTest, OpenTunerTwice) { |
| 219 | if (skipped) return; |
| 220 | ASSERT_TRUE(openTuner(0)); |
| 221 | |
| 222 | Result halResult = Result::NOT_INITIALIZED; |
| 223 | auto openCb = [&](Result result, const sp<V1_0::ITuner>&) { halResult = result; }; |
| 224 | auto hidlResult = mRadioModule->openTuner(getBand(0), true, mCallback, openCb); |
| 225 | ASSERT_TRUE(hidlResult.isOk()); |
| 226 | ASSERT_EQ(Result::OK, halResult); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Test tuning to program list entry. |
| 231 | * |
| 232 | * Verifies that: |
| 233 | * - getProgramList either succeeds or returns NOT_STARTED/NOT_READY status; |
| 234 | * - if the program list is NOT_STARTED, startBackgroundScan makes it completed |
| 235 | * within a full scan timeout and the next getProgramList call succeeds; |
| 236 | * - if the program list is not empty, tune_1_1 call succeeds. |
| 237 | */ |
| 238 | TEST_P(BroadcastRadioHalTest, TuneFromProgramList) { |
| 239 | if (skipped) return; |
| 240 | ASSERT_TRUE(openTuner(0)); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 241 | |
| 242 | ProgramInfo firstProgram; |
| 243 | bool isListEmpty; |
| 244 | ProgramListResult getListResult = ProgramListResult::NOT_INITIALIZED; |
| 245 | auto getListCb = [&](ProgramListResult result, const hidl_vec<ProgramInfo>& list) { |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 246 | ALOGD("getListCb(%s, ProgramInfo[%zu])", toString(result).c_str(), list.size()); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 247 | getListResult = result; |
| 248 | if (result != ProgramListResult::OK) return; |
| 249 | isListEmpty = (list.size() == 0); |
| 250 | // don't copy the whole list out, it might be heavy |
| 251 | if (!isListEmpty) firstProgram = list[0]; |
| 252 | }; |
| 253 | |
| 254 | // first try... |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 255 | EXPECT_TIMEOUT_CALL(*mCallback, backgroundScanComplete, ProgramListResult::OK) |
| 256 | .Times(AnyNumber()); |
| 257 | auto hidlResult = mTuner->getProgramList("", getListCb); |
| 258 | ASSERT_TRUE(hidlResult.isOk()); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 259 | |
| 260 | if (getListResult == ProgramListResult::NOT_STARTED) { |
| 261 | auto result = mTuner->startBackgroundScan(); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 262 | ASSERT_EQ(ProgramListResult::OK, result); |
| 263 | getListResult = ProgramListResult::NOT_READY; // continue as in NOT_READY case |
| 264 | } |
| 265 | if (getListResult == ProgramListResult::NOT_READY) { |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 266 | EXPECT_TIMEOUT_CALL_WAIT(*mCallback, backgroundScanComplete, kFullScanTimeout); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 267 | |
| 268 | // second (last) try... |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 269 | hidlResult = mTuner->getProgramList("", getListCb); |
| 270 | ASSERT_TRUE(hidlResult.isOk()); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 271 | ASSERT_EQ(ProgramListResult::OK, getListResult); |
| 272 | } |
| 273 | |
| 274 | if (isListEmpty) { |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 275 | printSkipped("Program list is empty."); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 279 | ProgramInfo infoCb; |
| 280 | EXPECT_CALL(*mCallback, tuneComplete(_, _)); |
| 281 | EXPECT_TIMEOUT_CALL(*mCallback, tuneComplete_1_1, Result::OK, _) |
| 282 | .WillOnce(DoAll(SaveArg<1>(&infoCb), testing::Return(ByMove(Void())))); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 283 | auto tuneResult = mTuner->tune_1_1(firstProgram.selector); |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 284 | ASSERT_EQ(Result::OK, tuneResult); |
| 285 | EXPECT_TIMEOUT_CALL_WAIT(*mCallback, tuneComplete_1_1, kTuneTimeout); |
| 286 | EXPECT_EQ(firstProgram.selector.primaryId, infoCb.selector.primaryId); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 287 | } |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 288 | |
Tomasz Wasilczyk | c9ba646 | 2017-07-07 13:28:00 -0700 | [diff] [blame^] | 289 | INSTANTIATE_TEST_CASE_P(BroadcastRadioHalTestCases, BroadcastRadioHalTest, |
| 290 | ::testing::Values(Class::AM_FM, Class::SAT, Class::DT)); |
| 291 | |
| 292 | } // namespace vts |
| 293 | } // namespace V1_1 |
| 294 | } // namespace broadcastradio |
| 295 | } // namespace hardware |
| 296 | } // namespace android |
| 297 | |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 298 | int main(int argc, char** argv) { |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 299 | ::testing::InitGoogleTest(&argc, argv); |
| 300 | int status = RUN_ALL_TESTS(); |
| 301 | ALOGI("Test result = %d", status); |
| 302 | return status; |
| 303 | } |