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