blob: 9ee40ede01741ddf2b3b65a0f77d85dc5a7a684d [file] [log] [blame]
Arthur Ishigurod06c45e2020-10-06 13:24:29 -07001/*
2 * Copyright (C) 2020 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 "contexthub_hidl_hal_test"
18
19#include "ContexthubCallbackBase.h"
20#include "ContexthubHidlTestBase.h"
21#include "VtsHalContexthubUtils.h"
22
23#include <android-base/logging.h>
24#include <android/hardware/contexthub/1.0/IContexthub.h>
25#include <android/hardware/contexthub/1.1/IContexthub.h>
26#include <android/hardware/contexthub/1.2/IContexthub.h>
27#include <android/log.h>
28#include <gtest/gtest.h>
29#include <hidl/GtestPrinter.h>
30#include <hidl/ServiceManagement.h>
31#include <log/log.h>
32
33#include <cinttypes>
34
Anthony Stangea2c3db12020-12-16 15:04:02 -050035using ::android::sp;
36using ::android::hardware::hidl_string;
37using ::android::hardware::hidl_vec;
38using ::android::hardware::Return;
39using ::android::hardware::Void;
40using ::android::hardware::contexthub::V1_0::ContextHub;
41using ::android::hardware::contexthub::V1_0::Result;
42using ::android::hardware::contexthub::V1_0::TransactionResult;
Arthur Ishigurod06c45e2020-10-06 13:24:29 -070043using ::android::hardware::contexthub::V1_1::SettingValue;
Anthony Stangea2c3db12020-12-16 15:04:02 -050044using ::android::hardware::contexthub::V1_2::ContextHubMsg;
45using ::android::hardware::contexthub::V1_2::HubAppInfo;
Arthur Ishigurod06c45e2020-10-06 13:24:29 -070046using ::android::hardware::contexthub::V1_2::IContexthub;
Anthony Stangea2c3db12020-12-16 15:04:02 -050047using ::android::hardware::contexthub::V1_2::IContexthubCallback;
Arthur Ishigurod06c45e2020-10-06 13:24:29 -070048using ::android::hardware::contexthub::V1_2::Setting;
Anthony Stangea2c3db12020-12-16 15:04:02 -050049using ::android::hardware::contexthub::vts_utils::asBaseType;
Arthur Ishigurod06c45e2020-10-06 13:24:29 -070050using ::android::hardware::contexthub::vts_utils::ContexthubCallbackBase;
51using ::android::hardware::contexthub::vts_utils::ContexthubHidlTestBase;
52using ::android::hardware::contexthub::vts_utils::getHalAndHubIdList;
Anthony Stangea2c3db12020-12-16 15:04:02 -050053using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
54using ::android::hardware::contexthub::vts_utils::waitForCallback;
Arthur Ishigurod06c45e2020-10-06 13:24:29 -070055
56namespace {
57
58const std::vector<std::tuple<std::string, std::string>> kTestParameters =
59 getHalAndHubIdList<IContexthub>();
60
Anthony Stangea2c3db12020-12-16 15:04:02 -050061class ContexthubCallbackV1_2 : public ContexthubCallbackBase<IContexthubCallback> {
62 public:
63 virtual Return<void> handleClientMsg_1_2(
64 const ContextHubMsg& /*msg*/,
65 const hidl_vec<hidl_string>& /*msgContentPerms*/) override {
66 ALOGD("Got client message callback");
67 return Void();
68 }
69
70 virtual Return<void> handleAppsInfo_1_2(const hidl_vec<HubAppInfo>& /*appInfo*/) override {
71 ALOGD("Got app info callback");
72 return Void();
73 }
74};
75
76class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {
77 public:
78 Result registerCallback_1_2(sp<IContexthubCallback> cb) {
79 return hubApi->registerCallback_1_2(getHubId(), cb);
80 }
81};
82
83// Ensures that the metadata reported in getHubs_1_2() is valid
84TEST_P(ContexthubHidlTest, TestGetHubs_1_2) {
85 hidl_vec<ContextHub> hubList;
86 hubApi->getHubs_1_2(
87 [&hubList](const hidl_vec<ContextHub>& hubs,
88 const hidl_vec<hidl_string>& /*hubPermissions*/) { hubList = hubs; });
89
90 ALOGD("System reports %zu hubs", hubList.size());
91
92 for (const ContextHub& hub : hubList) {
93 ALOGD("Checking hub ID %" PRIu32, hub.hubId);
94
95 EXPECT_FALSE(hub.name.empty());
96 EXPECT_FALSE(hub.vendor.empty());
97 EXPECT_FALSE(hub.toolchain.empty());
98 EXPECT_GT(hub.peakMips, 0);
99 EXPECT_GE(hub.stoppedPowerDrawMw, 0);
100 EXPECT_GE(hub.sleepPowerDrawMw, 0);
101 EXPECT_GT(hub.peakPowerDrawMw, 0);
102
103 // Minimum 128 byte MTU as required by CHRE API v1.0
104 EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128));
105 }
106}
107
108TEST_P(ContexthubHidlTest, TestRegisterCallback) {
109 ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId());
110 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
111}
112
113TEST_P(ContexthubHidlTest, TestRegisterNullCallback) {
114 ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId());
115 ASSERT_OK(registerCallback_1_2(nullptr));
116}
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700117
118// In VTS, we only test that sending the values doesn't cause things to blow up - other test
119// suites verify the expected E2E behavior in CHRE
120TEST_P(ContexthubHidlTest, TestOnWifiSettingChanged) {
Anthony Stangea2c3db12020-12-16 15:04:02 -0500121 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700122 hubApi->onSettingChanged_1_2(Setting::WIFI_AVAILABLE, SettingValue::DISABLED);
123 hubApi->onSettingChanged_1_2(Setting::WIFI_AVAILABLE, SettingValue::ENABLED);
Anthony Stangea2c3db12020-12-16 15:04:02 -0500124 ASSERT_OK(registerCallback_1_2(nullptr));
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700125}
126
127TEST_P(ContexthubHidlTest, TestOnAirplaneModeSettingChanged) {
Anthony Stangea2c3db12020-12-16 15:04:02 -0500128 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700129 hubApi->onSettingChanged_1_2(Setting::AIRPLANE_MODE, SettingValue::DISABLED);
130 hubApi->onSettingChanged_1_2(Setting::AIRPLANE_MODE, SettingValue::ENABLED);
Anthony Stangea2c3db12020-12-16 15:04:02 -0500131 ASSERT_OK(registerCallback_1_2(nullptr));
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700132}
133
karthik bharadwaj98e579c2021-03-24 01:39:55 -0700134TEST_P(ContexthubHidlTest, TestOnMicrophoneSettingChanged) {
Anthony Stangea2c3db12020-12-16 15:04:02 -0500135 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
karthik bharadwaj98e579c2021-03-24 01:39:55 -0700136 hubApi->onSettingChanged_1_2(Setting::MICROPHONE, SettingValue::DISABLED);
137 hubApi->onSettingChanged_1_2(Setting::MICROPHONE, SettingValue::ENABLED);
Anthony Stangea2c3db12020-12-16 15:04:02 -0500138 ASSERT_OK(registerCallback_1_2(nullptr));
139}
140
141// Helper callback that puts the async appInfo callback data into a promise
142class QueryAppsCallback : public ContexthubCallbackV1_2 {
143 public:
144 virtual Return<void> handleAppsInfo_1_2(const hidl_vec<HubAppInfo>& appInfo) override {
145 ALOGD("Got app info callback with %zu apps", appInfo.size());
146 promise.set_value(appInfo);
147 return Void();
148 }
149
150 std::promise<hidl_vec<HubAppInfo>> promise;
151};
152
153// Calls queryApps() and checks the returned metadata
154TEST_P(ContexthubHidlTest, TestQueryApps) {
155 hidl_vec<hidl_string> hubPerms;
156 hubApi->getHubs_1_2([&hubPerms](const hidl_vec<ContextHub>& /*hubs*/,
157 const hidl_vec<hidl_string>& hubPermissions) {
158 hubPerms = hubPermissions;
159 });
160
161 ALOGD("TestQueryApps called, hubId %u", getHubId());
162 sp<QueryAppsCallback> cb = new QueryAppsCallback();
163 ASSERT_OK(registerCallback_1_2(cb));
164
165 Result result = hubApi->queryApps(getHubId());
166 ASSERT_OK(result);
167
168 ALOGD("Waiting for app info callback");
169 hidl_vec<HubAppInfo> appList;
170 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appList));
171 for (const HubAppInfo& appInfo : appList) {
172 EXPECT_NE(appInfo.info_1_0.appId, UINT64_C(0));
173 EXPECT_NE(appInfo.info_1_0.appId, kNonExistentAppId);
174 for (std::string permission : appInfo.permissions) {
175 ASSERT_TRUE(hubPerms.contains(permission));
176 }
177 }
178}
179
180// Helper callback that puts the TransactionResult for the expectedTxnId into a
181// promise
182class TxnResultCallback : public ContexthubCallbackV1_2 {
183 public:
184 virtual Return<void> handleTxnResult(uint32_t txnId, TransactionResult result) override {
185 ALOGD("Got transaction result callback for txnId %" PRIu32 " (expecting %" PRIu32
186 ") with result %" PRId32,
187 txnId, expectedTxnId, result);
188 if (txnId == expectedTxnId) {
189 promise.set_value(result);
190 }
191 return Void();
192 }
193
194 uint32_t expectedTxnId = 0;
195 std::promise<TransactionResult> promise;
196};
197
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700198GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubHidlTest);
199INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters),
200 android::hardware::PrintInstanceTupleNameToString<>);
201
Arthur Ishigurod06c45e2020-10-06 13:24:29 -0700202} // anonymous namespace