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