blob: 2310a723184d7776001587c870e33aaf1add7b6e [file] [log] [blame]
Lais Andradec86c1d22020-03-30 20:17:42 +01001/*
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 "PowerHalLoaderTest"
18
19#include <android-base/logging.h>
20#include <android/hardware/power/1.1/IPower.h>
21#include <android/hardware/power/IPower.h>
22
23#include <future>
24#include <gtest/gtest.h>
25
26#include <powermanager/PowerHalLoader.h>
27
28using IPowerV1_0 = android::hardware::power::V1_0::IPower;
29using IPowerV1_1 = android::hardware::power::V1_1::IPower;
30using IPowerAidl = android::hardware::power::IPower;
31
32using namespace android;
33
34// -------------------------------------------------------------------------------------------------
35
36template <typename T>
37sp<T> loadHal();
38
39template <>
40sp<IPowerAidl> loadHal<IPowerAidl>() {
41 return PowerHalLoader::loadAidl();
42}
43
44template <>
45sp<IPowerV1_0> loadHal<IPowerV1_0>() {
46 return PowerHalLoader::loadHidlV1_0();
47}
48
49template <>
50sp<IPowerV1_1> loadHal<IPowerV1_1>() {
51 return PowerHalLoader::loadHidlV1_1();
52}
53
54// -------------------------------------------------------------------------------------------------
55
56template <typename T>
57class PowerHalLoaderTest : public testing::Test {
58public:
59 sp<T> load() {
60 return ::loadHal<T>();
61 }
62 void unload() {
63 PowerHalLoader::unloadAll();
64 }
65};
66
67// -------------------------------------------------------------------------------------------------
68
69typedef ::testing::Types<IPowerAidl, IPowerV1_0, IPowerV1_1> PowerHalTypes;
70TYPED_TEST_SUITE(PowerHalLoaderTest, PowerHalTypes);
71
72TYPED_TEST(PowerHalLoaderTest, TestLoadsOnlyOnce) {
73 sp<TypeParam> firstHal = this->load();
74 if (firstHal == nullptr) {
75 ALOGE("Power HAL not available. Skipping test.");
76 return;
77 }
78 sp<TypeParam> secondHal = this->load();
79 ASSERT_EQ(firstHal, secondHal);
80}
81
82TYPED_TEST(PowerHalLoaderTest, TestUnload) {
83 sp<TypeParam> firstHal = this->load();
84 if (firstHal == nullptr) {
85 ALOGE("Power HAL not available. Skipping test.");
86 return;
87 }
88 this->unload();
89 sp<TypeParam> secondHal = this->load();
90 ASSERT_NE(secondHal, nullptr);
91 ASSERT_NE(firstHal, secondHal);
92}
93
94TYPED_TEST(PowerHalLoaderTest, TestLoadMultiThreadLoadsOnlyOnce) {
95 std::vector<std::future<sp<TypeParam>>> futures;
96 for (int i = 0; i < 10; i++) {
97 futures.push_back(
98 std::async(std::launch::async, &PowerHalLoaderTest<TypeParam>::load, this));
99 }
100
101 futures[0].wait();
102 sp<TypeParam> firstHal = futures[0].get();
103 if (firstHal == nullptr) {
104 ALOGE("Power HAL not available. Skipping test.");
105 return;
106 }
107
108 for (int i = 1; i < 10; i++) {
109 futures[i].wait();
110 sp<TypeParam> currentHal = futures[i].get();
111 ASSERT_EQ(firstHal, currentHal);
112 }
113}