blob: 7d9735421b6ace1a30e774c679f865e23cb383ad [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
Xiang Wang99f6f3c2023-05-22 13:12:16 -070019#include <aidl/android/hardware/power/IPower.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010020#include <android/hardware/power/1.1/IPower.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010021#include <gtest/gtest.h>
22#include <powermanager/PowerHalLoader.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010023
24#include <future>
Lais Andradec86c1d22020-03-30 20:17:42 +010025
26using IPowerV1_0 = android::hardware::power::V1_0::IPower;
27using IPowerV1_1 = android::hardware::power::V1_1::IPower;
Matt Buckleyc3894a42022-09-01 21:17:15 +000028using IPowerV1_2 = android::hardware::power::V1_2::IPower;
29using IPowerV1_3 = android::hardware::power::V1_3::IPower;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070030using IPowerAidl = aidl::android::hardware::power::IPower;
Lais Andradec86c1d22020-03-30 20:17:42 +010031
32using namespace android;
Lais Andradeb59a9b52020-05-07 17:23:42 +010033using namespace android::power;
34using namespace testing;
Lais Andradec86c1d22020-03-30 20:17:42 +010035
36// -------------------------------------------------------------------------------------------------
37
38template <typename T>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070039T loadHal();
Lais Andradec86c1d22020-03-30 20:17:42 +010040
41template <>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070042std::shared_ptr<IPowerAidl> loadHal<std::shared_ptr<IPowerAidl>>() {
Lais Andradec86c1d22020-03-30 20:17:42 +010043 return PowerHalLoader::loadAidl();
44}
45
46template <>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070047sp<IPowerV1_0> loadHal<sp<IPowerV1_0>>() {
Lais Andradec86c1d22020-03-30 20:17:42 +010048 return PowerHalLoader::loadHidlV1_0();
49}
50
51template <>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070052sp<IPowerV1_1> loadHal<sp<IPowerV1_1>>() {
Lais Andradec86c1d22020-03-30 20:17:42 +010053 return PowerHalLoader::loadHidlV1_1();
54}
55
Matt Buckleyc3894a42022-09-01 21:17:15 +000056template <>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070057sp<IPowerV1_2> loadHal<sp<IPowerV1_2>>() {
Matt Buckleyc3894a42022-09-01 21:17:15 +000058 return PowerHalLoader::loadHidlV1_2();
59}
60
61template <>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070062sp<IPowerV1_3> loadHal<sp<IPowerV1_3>>() {
Matt Buckleyc3894a42022-09-01 21:17:15 +000063 return PowerHalLoader::loadHidlV1_3();
64}
65
Lais Andradec86c1d22020-03-30 20:17:42 +010066// -------------------------------------------------------------------------------------------------
67
68template <typename T>
Lais Andradeb59a9b52020-05-07 17:23:42 +010069class PowerHalLoaderTest : public Test {
Lais Andradec86c1d22020-03-30 20:17:42 +010070public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -070071 T load() { return ::loadHal<T>(); }
Lais Andradeb59a9b52020-05-07 17:23:42 +010072 void unload() { PowerHalLoader::unloadAll(); }
Lais Andradec86c1d22020-03-30 20:17:42 +010073};
74
75// -------------------------------------------------------------------------------------------------
Xiang Wang99f6f3c2023-05-22 13:12:16 -070076typedef ::testing::Types<std::shared_ptr<IPowerAidl>, sp<IPowerV1_0>, sp<IPowerV1_1>,
77 sp<IPowerV1_2>, sp<IPowerV1_3>>
78 PowerHalTypes;
Lais Andradec86c1d22020-03-30 20:17:42 +010079TYPED_TEST_SUITE(PowerHalLoaderTest, PowerHalTypes);
80
81TYPED_TEST(PowerHalLoaderTest, TestLoadsOnlyOnce) {
Xiang Wang99f6f3c2023-05-22 13:12:16 -070082 TypeParam firstHal = this->load();
Lais Andradec86c1d22020-03-30 20:17:42 +010083 if (firstHal == nullptr) {
84 ALOGE("Power HAL not available. Skipping test.");
85 return;
86 }
Xiang Wang99f6f3c2023-05-22 13:12:16 -070087 TypeParam secondHal = this->load();
Lais Andradec86c1d22020-03-30 20:17:42 +010088 ASSERT_EQ(firstHal, secondHal);
89}
90
91TYPED_TEST(PowerHalLoaderTest, TestUnload) {
Xiang Wang99f6f3c2023-05-22 13:12:16 -070092 TypeParam firstHal = this->load();
Lais Andradec86c1d22020-03-30 20:17:42 +010093 if (firstHal == nullptr) {
94 ALOGE("Power HAL not available. Skipping test.");
95 return;
96 }
97 this->unload();
Xiang Wang99f6f3c2023-05-22 13:12:16 -070098 TypeParam secondHal = this->load();
Lais Andradec86c1d22020-03-30 20:17:42 +010099 ASSERT_NE(secondHal, nullptr);
100 ASSERT_NE(firstHal, secondHal);
101}
102
103TYPED_TEST(PowerHalLoaderTest, TestLoadMultiThreadLoadsOnlyOnce) {
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700104 std::vector<std::future<TypeParam>> futures;
Lais Andradec86c1d22020-03-30 20:17:42 +0100105 for (int i = 0; i < 10; i++) {
106 futures.push_back(
Lais Andradeb59a9b52020-05-07 17:23:42 +0100107 std::async(std::launch::async, &PowerHalLoaderTest<TypeParam>::load, this));
Lais Andradec86c1d22020-03-30 20:17:42 +0100108 }
109
110 futures[0].wait();
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700111 TypeParam firstHal = futures[0].get();
Lais Andradec86c1d22020-03-30 20:17:42 +0100112 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();
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700119 TypeParam currentHal = futures[i].get();
Lais Andradec86c1d22020-03-30 20:17:42 +0100120 ASSERT_EQ(firstHal, currentHal);
121 }
122}