1 /*
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
35 using ::android::sp;
36 using ::android::hardware::hidl_string;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::Return;
39 using ::android::hardware::Void;
40 using ::android::hardware::contexthub::V1_0::ContextHub;
41 using ::android::hardware::contexthub::V1_0::Result;
42 using ::android::hardware::contexthub::V1_0::TransactionResult;
43 using ::android::hardware::contexthub::V1_1::SettingValue;
44 using ::android::hardware::contexthub::V1_2::ContextHubMsg;
45 using ::android::hardware::contexthub::V1_2::HubAppInfo;
46 using ::android::hardware::contexthub::V1_2::IContexthub;
47 using ::android::hardware::contexthub::V1_2::IContexthubCallback;
48 using ::android::hardware::contexthub::V1_2::Setting;
49 using ::android::hardware::contexthub::vts_utils::asBaseType;
50 using ::android::hardware::contexthub::vts_utils::ContexthubCallbackBase;
51 using ::android::hardware::contexthub::vts_utils::ContexthubHidlTestBase;
52 using ::android::hardware::contexthub::vts_utils::getHalAndHubIdList;
53 using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
54 using ::android::hardware::contexthub::vts_utils::waitForCallback;
55
56 namespace {
57
58 const std::vector<std::tuple<std::string, std::string>> kTestParameters =
59 getHalAndHubIdList<IContexthub>();
60
61 class ContexthubCallbackV1_2 : public ContexthubCallbackBase<IContexthubCallback> {
62 public:
handleClientMsg_1_2(const ContextHubMsg &,const hidl_vec<hidl_string> &)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
handleAppsInfo_1_2(const hidl_vec<HubAppInfo> &)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
76 class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {
77 public:
registerCallback_1_2(sp<IContexthubCallback> cb)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
TEST_P(ContexthubHidlTest,TestGetHubs_1_2)84 TEST_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
TEST_P(ContexthubHidlTest,TestRegisterCallback)108 TEST_P(ContexthubHidlTest, TestRegisterCallback) {
109 ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId());
110 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
111 }
112
TEST_P(ContexthubHidlTest,TestRegisterNullCallback)113 TEST_P(ContexthubHidlTest, TestRegisterNullCallback) {
114 ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId());
115 ASSERT_OK(registerCallback_1_2(nullptr));
116 }
117
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
TEST_P(ContexthubHidlTest,TestOnWifiSettingChanged)120 TEST_P(ContexthubHidlTest, TestOnWifiSettingChanged) {
121 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
122 hubApi->onSettingChanged_1_2(Setting::WIFI_AVAILABLE, SettingValue::DISABLED);
123 hubApi->onSettingChanged_1_2(Setting::WIFI_AVAILABLE, SettingValue::ENABLED);
124 ASSERT_OK(registerCallback_1_2(nullptr));
125 }
126
TEST_P(ContexthubHidlTest,TestOnAirplaneModeSettingChanged)127 TEST_P(ContexthubHidlTest, TestOnAirplaneModeSettingChanged) {
128 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
129 hubApi->onSettingChanged_1_2(Setting::AIRPLANE_MODE, SettingValue::DISABLED);
130 hubApi->onSettingChanged_1_2(Setting::AIRPLANE_MODE, SettingValue::ENABLED);
131 ASSERT_OK(registerCallback_1_2(nullptr));
132 }
133
TEST_P(ContexthubHidlTest,TestOnMicrophoneSettingChanged)134 TEST_P(ContexthubHidlTest, TestOnMicrophoneSettingChanged) {
135 ASSERT_OK(registerCallback_1_2(new ContexthubCallbackV1_2()));
136 hubApi->onSettingChanged_1_2(Setting::MICROPHONE, SettingValue::DISABLED);
137 hubApi->onSettingChanged_1_2(Setting::MICROPHONE, SettingValue::ENABLED);
138 ASSERT_OK(registerCallback_1_2(nullptr));
139 }
140
141 // Helper callback that puts the async appInfo callback data into a promise
142 class QueryAppsCallback : public ContexthubCallbackV1_2 {
143 public:
handleAppsInfo_1_2(const hidl_vec<HubAppInfo> & appInfo)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
TEST_P(ContexthubHidlTest,TestQueryApps)154 TEST_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
182 class TxnResultCallback : public ContexthubCallbackV1_2 {
183 public:
handleTxnResult(uint32_t txnId,TransactionResult result)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
198 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubHidlTest);
199 INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters),
200 android::hardware::PrintInstanceTupleNameToString<>);
201
202 } // anonymous namespace
203