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 #pragma once
17 
18 #include <android/hardware/contexthub/1.0/IContexthub.h>
19 #include <gtest/gtest.h>
20 #include <hidl/HidlSupport.h>
21 #include <hidl/ServiceManagement.h>
22 #include <utils/StrongPointer.h>
23 
24 #include <chrono>
25 #include <future>
26 #include <vector>
27 
28 namespace android {
29 namespace hardware {
30 namespace contexthub {
31 namespace vts_utils {
32 
33 // App ID with vendor "GoogT" (Google Testing), app identifier 0x555555. This
34 // app ID is reserved and must never appear in the list of loaded apps.
35 constexpr uint64_t kNonExistentAppId = 0x476f6f6754555555;
36 
37 #define ASSERT_OK(result) ASSERT_EQ(result, ::android::hardware::contexthub::V1_0::Result::OK)
38 #define EXPECT_OK(result) EXPECT_EQ(result, ::android::hardware::contexthub::V1_0::Result::OK)
39 
40 // Helper that does explicit conversion of an enum class to its underlying/base
41 // type. Useful for stream output of enum values.
42 template <typename EnumType>
asBaseType(EnumType value)43 inline constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) {
44     return static_cast<typename std::underlying_type<EnumType>::type>(value);
45 }
46 
47 // Synchronously queries IContexthub::getHubs() and returns the result
48 hidl_vec<V1_0::ContextHub> getHubsSync(V1_0::IContexthub* hubApi);
49 
50 // Create a vector of tuples that include each IContexthub service paired with each hub ID it
51 // exposes via getHubs(). Each tuple represents a test target that we should run the VTS suite
52 // against.
53 template <class IContexthubVersion>
getHalAndHubIdList()54 static std::vector<std::tuple<std::string, std::string>> getHalAndHubIdList() {
55     std::vector<std::tuple<std::string, std::string>> parameters;
56     std::vector<std::string> serviceNames =
57             ::android::hardware::getAllHalInstanceNames(IContexthubVersion::descriptor);
58     for (const std::string& serviceName : serviceNames) {
59         sp<IContexthubVersion> hubApi = IContexthubVersion::getService(serviceName);
60         if (hubApi != nullptr) {
61             hidl_vec<V1_0::ContextHub> hubs = getHubsSync(hubApi.get());
62             for (const V1_0::ContextHub& hub : hubs) {
63                 parameters.push_back(std::make_tuple(serviceName, std::to_string(hub.hubId)));
64             }
65         }
66     }
67 
68     return parameters;
69 }
70 
71 // Wait for a callback to occur (signaled by the given future) up to the
72 // provided timeout. If the future is invalid or the callback does not come
73 // within the given time, returns false.
74 template <class ReturnType>
75 bool waitForCallback(std::future<ReturnType> future, ReturnType* result,
76                      std::chrono::milliseconds timeout = std::chrono::seconds(5)) {
77     auto expiration = std::chrono::system_clock::now() + timeout;
78 
79     EXPECT_NE(result, nullptr);
80     EXPECT_TRUE(future.valid());
81     if (result != nullptr && future.valid()) {
82         std::future_status status = future.wait_until(expiration);
83         EXPECT_NE(status, std::future_status::timeout) << "Timed out waiting for callback";
84 
85         if (status == std::future_status::ready) {
86             *result = future.get();
87             return true;
88         }
89     }
90 
91     return false;
92 }
93 
94 }  // namespace vts_utils
95 }  // namespace contexthub
96 }  // namespace hardware
97 }  // namespace android
98