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> |
| 22 | |
| 23 | #include <future> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <powermanager/PowerHalLoader.h> |
| 27 | |
| 28 | using IPowerV1_0 = android::hardware::power::V1_0::IPower; |
| 29 | using IPowerV1_1 = android::hardware::power::V1_1::IPower; |
| 30 | using IPowerAidl = android::hardware::power::IPower; |
| 31 | |
| 32 | using namespace android; |
| 33 | |
| 34 | // ------------------------------------------------------------------------------------------------- |
| 35 | |
| 36 | template <typename T> |
| 37 | sp<T> loadHal(); |
| 38 | |
| 39 | template <> |
| 40 | sp<IPowerAidl> loadHal<IPowerAidl>() { |
| 41 | return PowerHalLoader::loadAidl(); |
| 42 | } |
| 43 | |
| 44 | template <> |
| 45 | sp<IPowerV1_0> loadHal<IPowerV1_0>() { |
| 46 | return PowerHalLoader::loadHidlV1_0(); |
| 47 | } |
| 48 | |
| 49 | template <> |
| 50 | sp<IPowerV1_1> loadHal<IPowerV1_1>() { |
| 51 | return PowerHalLoader::loadHidlV1_1(); |
| 52 | } |
| 53 | |
| 54 | // ------------------------------------------------------------------------------------------------- |
| 55 | |
| 56 | template <typename T> |
| 57 | class PowerHalLoaderTest : public testing::Test { |
| 58 | public: |
| 59 | sp<T> load() { |
| 60 | return ::loadHal<T>(); |
| 61 | } |
| 62 | void unload() { |
| 63 | PowerHalLoader::unloadAll(); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | // ------------------------------------------------------------------------------------------------- |
| 68 | |
| 69 | typedef ::testing::Types<IPowerAidl, IPowerV1_0, IPowerV1_1> PowerHalTypes; |
| 70 | TYPED_TEST_SUITE(PowerHalLoaderTest, PowerHalTypes); |
| 71 | |
| 72 | TYPED_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 | |
| 82 | TYPED_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 | |
| 94 | TYPED_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 | } |