Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #include <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include "VtsHalContexthubUtilsCommon.h" |
| 20 | |
| 21 | #include <android/hardware/contexthub/BnContextHub.h> |
| 22 | #include <android/hardware/contexthub/BnContextHubCallback.h> |
| 23 | #include <android/hardware/contexthub/IContextHub.h> |
| 24 | #include <android/hardware/contexthub/IContextHubCallback.h> |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include <binder/ProcessState.h> |
| 27 | #include <log/log.h> |
| 28 | |
| 29 | #include <cinttypes> |
| 30 | #include <future> |
| 31 | |
| 32 | using ::android::ProcessState; |
| 33 | using ::android::sp; |
| 34 | using ::android::String16; |
| 35 | using ::android::binder::Status; |
| 36 | using ::android::hardware::contexthub::AsyncEventType; |
| 37 | using ::android::hardware::contexthub::ContextHubInfo; |
| 38 | using ::android::hardware::contexthub::ContextHubMessage; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 39 | using ::android::hardware::contexthub::HostEndpointInfo; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 40 | using ::android::hardware::contexthub::IContextHub; |
| 41 | using ::android::hardware::contexthub::IContextHubCallbackDefault; |
| 42 | using ::android::hardware::contexthub::NanoappBinary; |
| 43 | using ::android::hardware::contexthub::NanoappInfo; |
Arthur Ishiguro | 0810307 | 2021-12-09 18:30:49 +0000 | [diff] [blame] | 44 | using ::android::hardware::contexthub::NanoappRpcService; |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 45 | using ::android::hardware::contexthub::NanSessionRequest; |
| 46 | using ::android::hardware::contexthub::NanSessionStateUpdate; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 47 | using ::android::hardware::contexthub::Setting; |
| 48 | using ::android::hardware::contexthub::vts_utils::kNonExistentAppId; |
| 49 | using ::android::hardware::contexthub::vts_utils::waitForCallback; |
| 50 | |
| 51 | class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> { |
| 52 | public: |
| 53 | virtual void SetUp() override { |
| 54 | contextHub = android::waitForDeclaredService<IContextHub>( |
| 55 | String16(std::get<0>(GetParam()).c_str())); |
| 56 | ASSERT_NE(contextHub, nullptr); |
| 57 | } |
| 58 | |
| 59 | uint32_t getHubId() { return std::get<1>(GetParam()); } |
| 60 | |
| 61 | void testSettingChanged(Setting setting); |
| 62 | |
| 63 | sp<IContextHub> contextHub; |
| 64 | }; |
| 65 | |
| 66 | TEST_P(ContextHubAidl, TestGetHubs) { |
| 67 | std::vector<ContextHubInfo> hubs; |
| 68 | ASSERT_TRUE(contextHub->getContextHubs(&hubs).isOk()); |
| 69 | |
| 70 | ALOGD("System reports %zu hubs", hubs.size()); |
| 71 | |
| 72 | for (const ContextHubInfo& hub : hubs) { |
| 73 | ALOGD("Checking hub ID %" PRIu32, hub.id); |
| 74 | |
| 75 | EXPECT_GT(hub.name.size(), 0); |
| 76 | EXPECT_GT(hub.vendor.size(), 0); |
| 77 | EXPECT_GT(hub.toolchain.size(), 0); |
| 78 | EXPECT_GT(hub.peakMips, 0); |
| 79 | EXPECT_GT(hub.chrePlatformId, 0); |
| 80 | EXPECT_GT(hub.chreApiMajorVersion, 0); |
Arthur Ishiguro | 6471f61 | 2021-10-28 21:59:55 +0000 | [diff] [blame] | 81 | EXPECT_GE(hub.chreApiMinorVersion, 0); |
| 82 | EXPECT_GE(hub.chrePatchVersion, 0); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 83 | |
| 84 | // Minimum 128 byte MTU as required by CHRE API v1.0 |
| 85 | EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128)); |
| 86 | } |
| 87 | } |
| 88 | |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 89 | TEST_P(ContextHubAidl, TestEnableTestMode) { |
| 90 | Status status = contextHub->setTestMode(true); |
| 91 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 92 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 93 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 94 | } else { |
| 95 | ASSERT_TRUE(status.isOk()); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 96 | } |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_P(ContextHubAidl, TestDisableTestMode) { |
| 100 | Status status = contextHub->setTestMode(false); |
| 101 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 102 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 103 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 104 | } else { |
| 105 | ASSERT_TRUE(status.isOk()); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 106 | } |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 109 | class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback { |
| 110 | public: |
| 111 | Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override { |
| 112 | return Status::ok(); |
| 113 | } |
| 114 | |
| 115 | Status handleContextHubMessage(const ContextHubMessage& /* msg */, |
| 116 | const std::vector<String16>& /* msgContentPerms */) override { |
| 117 | return Status::ok(); |
| 118 | } |
| 119 | |
| 120 | Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); } |
| 121 | |
| 122 | Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override { |
| 123 | return Status::ok(); |
| 124 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 125 | |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 126 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 127 | return Status::ok(); |
| 128 | } |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 131 | TEST_P(ContextHubAidl, TestRegisterCallback) { |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 132 | sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make(); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 133 | ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 136 | // Helper callback that puts the async appInfo callback data into a promise |
| 137 | class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback { |
| 138 | public: |
| 139 | Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override { |
| 140 | ALOGD("Got app info callback with %zu apps", appInfo.size()); |
| 141 | promise.set_value(appInfo); |
| 142 | return Status::ok(); |
| 143 | } |
| 144 | |
| 145 | Status handleContextHubMessage(const ContextHubMessage& /* msg */, |
| 146 | const std::vector<String16>& /* msgContentPerms */) override { |
| 147 | return Status::ok(); |
| 148 | } |
| 149 | |
| 150 | Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); } |
| 151 | |
| 152 | Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override { |
| 153 | return Status::ok(); |
| 154 | } |
| 155 | |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 156 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 157 | return Status::ok(); |
| 158 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 159 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 160 | std::promise<std::vector<NanoappInfo>> promise; |
| 161 | }; |
| 162 | |
| 163 | // Calls queryApps() and checks the returned metadata |
| 164 | TEST_P(ContextHubAidl, TestQueryApps) { |
| 165 | sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make(); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 166 | ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk()); |
| 167 | ASSERT_TRUE(contextHub->queryNanoapps(getHubId()).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 168 | |
| 169 | std::vector<NanoappInfo> appInfoList; |
| 170 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList)); |
| 171 | for (const NanoappInfo& appInfo : appInfoList) { |
| 172 | EXPECT_NE(appInfo.nanoappId, UINT64_C(0)); |
| 173 | EXPECT_NE(appInfo.nanoappId, kNonExistentAppId); |
Arthur Ishiguro | 0810307 | 2021-12-09 18:30:49 +0000 | [diff] [blame] | 174 | |
| 175 | // Verify services are unique. |
| 176 | std::set<uint64_t> existingServiceIds; |
| 177 | for (const NanoappRpcService& rpcService : appInfo.rpcServices) { |
| 178 | EXPECT_NE(rpcService.id, UINT64_C(0)); |
| 179 | EXPECT_EQ(existingServiceIds.count(rpcService.id), 0); |
| 180 | existingServiceIds.insert(rpcService.id); |
| 181 | } |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Matthew Sedam | d70f84d | 2023-03-06 18:34:24 +0000 | [diff] [blame] | 185 | // Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps |
| 186 | TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) { |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 187 | std::vector<int64_t> preloadedNanoappIds; |
Matthew Sedam | d70f84d | 2023-03-06 18:34:24 +0000 | [diff] [blame] | 188 | Status status = contextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds); |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 189 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 190 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 191 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 192 | } else { |
| 193 | ASSERT_TRUE(status.isOk()); |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 194 | } |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 197 | // Helper callback that puts the TransactionResult for the expectedTransactionId into a |
| 198 | // promise |
| 199 | class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback { |
| 200 | public: |
| 201 | Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override { |
| 202 | return Status::ok(); |
| 203 | } |
| 204 | |
| 205 | Status handleContextHubMessage(const ContextHubMessage& /* msg */, |
| 206 | const std::vector<String16>& /* msgContentPerms */) override { |
| 207 | return Status::ok(); |
| 208 | } |
| 209 | |
| 210 | Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); } |
| 211 | |
| 212 | Status handleTransactionResult(int32_t transactionId, bool success) override { |
| 213 | ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32 |
| 214 | ") with success %d", |
| 215 | transactionId, expectedTransactionId, success); |
| 216 | if (transactionId == expectedTransactionId) { |
| 217 | promise.set_value(success); |
| 218 | } |
| 219 | return Status::ok(); |
| 220 | } |
| 221 | |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 222 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 223 | return Status::ok(); |
| 224 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 225 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 226 | uint32_t expectedTransactionId = 0; |
| 227 | std::promise<bool> promise; |
| 228 | }; |
| 229 | |
| 230 | // Parameterized fixture that sets the callback to TransactionResultCallback |
| 231 | class ContextHubTransactionTest : public ContextHubAidl { |
| 232 | public: |
| 233 | virtual void SetUp() override { |
| 234 | ContextHubAidl::SetUp(); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 235 | ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make(); |
| 239 | }; |
| 240 | |
| 241 | TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) { |
| 242 | ContextHubMessage message; |
| 243 | message.nanoappId = kNonExistentAppId; |
| 244 | message.messageType = 1; |
| 245 | message.messageBody.resize(4); |
| 246 | std::fill(message.messageBody.begin(), message.messageBody.end(), 0); |
| 247 | |
| 248 | ALOGD("Sending message to non-existent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 249 | ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) { |
| 253 | cb->expectedTransactionId = 0123; |
| 254 | NanoappBinary emptyApp; |
| 255 | |
| 256 | emptyApp.nanoappId = kNonExistentAppId; |
| 257 | emptyApp.nanoappVersion = 1; |
| 258 | emptyApp.flags = 0; |
| 259 | emptyApp.targetChreApiMajorVersion = 1; |
| 260 | emptyApp.targetChreApiMinorVersion = 0; |
| 261 | |
| 262 | ALOGD("Loading empty nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 263 | bool success = contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 264 | if (success) { |
| 265 | bool transactionSuccess; |
| 266 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 267 | ASSERT_FALSE(transactionSuccess); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) { |
| 272 | cb->expectedTransactionId = 1234; |
| 273 | |
| 274 | ALOGD("Unloading nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 275 | bool success = |
| 276 | contextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
| 277 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 278 | if (success) { |
| 279 | bool transactionSuccess; |
| 280 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 281 | ASSERT_FALSE(transactionSuccess); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) { |
| 286 | cb->expectedTransactionId = 2345; |
| 287 | |
| 288 | ALOGD("Enabling nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 289 | bool success = |
| 290 | contextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
| 291 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 292 | if (success) { |
| 293 | bool transactionSuccess; |
| 294 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 295 | ASSERT_FALSE(transactionSuccess); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) { |
| 300 | cb->expectedTransactionId = 3456; |
| 301 | |
| 302 | ALOGD("Disabling nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 303 | bool success = |
| 304 | contextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
| 305 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 306 | if (success) { |
| 307 | bool transactionSuccess; |
| 308 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 309 | ASSERT_FALSE(transactionSuccess); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void ContextHubAidl::testSettingChanged(Setting setting) { |
| 314 | // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests |
| 315 | // verify the expected E2E behavior in CHRE |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 316 | sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make(); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 317 | ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 318 | |
| 319 | ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk()); |
| 320 | ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST_P(ContextHubAidl, TestOnLocationSettingChanged) { |
| 324 | testSettingChanged(Setting::LOCATION); |
| 325 | } |
| 326 | |
| 327 | TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) { |
| 328 | testSettingChanged(Setting::WIFI_MAIN); |
| 329 | } |
| 330 | |
| 331 | TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) { |
| 332 | testSettingChanged(Setting::WIFI_SCANNING); |
| 333 | } |
| 334 | |
| 335 | TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) { |
| 336 | testSettingChanged(Setting::AIRPLANE_MODE); |
| 337 | } |
| 338 | |
| 339 | TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) { |
| 340 | testSettingChanged(Setting::MICROPHONE); |
| 341 | } |
| 342 | |
Anthony | a6b6500 | 2022-01-20 20:49:10 +0000 | [diff] [blame] | 343 | TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) { |
| 344 | testSettingChanged(Setting::BT_MAIN); |
| 345 | } |
| 346 | |
| 347 | TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) { |
| 348 | testSettingChanged(Setting::BT_SCANNING); |
| 349 | } |
| 350 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 351 | std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() { |
| 352 | std::vector<std::tuple<std::string, int32_t>> tuples; |
| 353 | auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor); |
| 354 | std::vector<ContextHubInfo> contextHubInfos; |
| 355 | |
| 356 | for (int i = 0; i < contextHubAidlNames.size(); i++) { |
| 357 | auto contextHubName = contextHubAidlNames[i].c_str(); |
| 358 | auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName)); |
| 359 | if (contextHub->getContextHubs(&contextHubInfos).isOk()) { |
| 360 | for (auto& info : contextHubInfos) { |
| 361 | tuples.push_back(std::make_tuple(contextHubName, info.id)); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return tuples; |
| 367 | } |
| 368 | |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 369 | TEST_P(ContextHubAidl, TestHostConnection) { |
| 370 | constexpr char16_t kHostEndpointId = 1; |
| 371 | HostEndpointInfo hostEndpointInfo; |
Arthur Ishiguro | 16f4062 | 2023-02-10 18:11:12 +0000 | [diff] [blame] | 372 | hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 373 | hostEndpointInfo.hostEndpointId = kHostEndpointId; |
| 374 | |
| 375 | ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk()); |
| 376 | ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk()); |
| 377 | } |
| 378 | |
| 379 | TEST_P(ContextHubAidl, TestInvalidHostConnection) { |
| 380 | constexpr char16_t kHostEndpointId = 1; |
| 381 | |
Arthur Ishiguro | 5dba921 | 2022-02-01 17:03:32 +0000 | [diff] [blame] | 382 | ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk()); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 385 | TEST_P(ContextHubAidl, TestNanSessionStateChange) { |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 386 | NanSessionStateUpdate update; |
| 387 | update.state = true; |
Rocky Fang | 7814098 | 2023-06-16 00:42:41 +0000 | [diff] [blame] | 388 | Status status = contextHub->onNanSessionStateChanged(update); |
| 389 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 390 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
| 391 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 392 | } else { |
| 393 | ASSERT_TRUE(status.isOk()); |
| 394 | update.state = false; |
| 395 | ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk()); |
| 396 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 399 | std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) { |
| 400 | return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param)); |
| 401 | } |
| 402 | |
| 403 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl); |
| 404 | INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()), |
| 405 | PrintGeneratedTest); |
| 406 | |
| 407 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest); |
| 408 | INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest, |
| 409 | testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest); |
| 410 | |
| 411 | int main(int argc, char** argv) { |
| 412 | ::testing::InitGoogleTest(&argc, argv); |
| 413 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 414 | ProcessState::self()->startThreadPool(); |
| 415 | return RUN_ALL_TESTS(); |
| 416 | } |