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