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