Steven Moreland | 84dd3b7 | 2019-07-25 17:13:52 -0700 | [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 "light_aidl_hal_test" |
| 18 | |
| 19 | #include <aidl/Gtest.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android/hardware/light/ILights.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <binder/ProcessState.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <hidl/GtestPrinter.h> |
| 28 | #include <hidl/ServiceManagement.h> |
| 29 | |
| 30 | #include <unistd.h> |
| 31 | #include <set> |
| 32 | |
| 33 | using android::ProcessState; |
| 34 | using android::sp; |
| 35 | using android::String16; |
| 36 | using android::binder::Status; |
| 37 | using android::hardware::hidl_vec; |
| 38 | using android::hardware::Return; |
| 39 | using android::hardware::Void; |
| 40 | using android::hardware::light::BrightnessMode; |
| 41 | using android::hardware::light::FlashMode; |
| 42 | using android::hardware::light::HwLight; |
| 43 | using android::hardware::light::HwLightState; |
| 44 | using android::hardware::light::ILights; |
| 45 | using android::hardware::light::LightType; |
| 46 | |
| 47 | #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk()) |
| 48 | #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk()) |
| 49 | |
| 50 | const std::set<LightType> kAllTypes{android::enum_range<LightType>().begin(), |
| 51 | android::enum_range<LightType>().end()}; |
| 52 | |
| 53 | class LightsAidl : public testing::TestWithParam<std::string> { |
| 54 | public: |
| 55 | virtual void SetUp() override { |
| 56 | lights = android::waitForDeclaredService<ILights>(String16(GetParam().c_str())); |
| 57 | ASSERT_NE(lights, nullptr); |
| 58 | ASSERT_TRUE(lights->getLights(&supportedLights).isOk()); |
| 59 | } |
| 60 | |
| 61 | sp<ILights> lights; |
| 62 | std::vector<HwLight> supportedLights; |
| 63 | |
| 64 | virtual void TearDown() override { |
| 65 | for (const HwLight& light : supportedLights) { |
| 66 | HwLightState off; |
| 67 | off.color = 0x00000000; |
| 68 | off.flashMode = FlashMode::NONE; |
| 69 | off.brightnessMode = BrightnessMode::USER; |
| 70 | EXPECT_TRUE(lights->setLightState(light.id, off).isOk()); |
| 71 | } |
| 72 | |
| 73 | // must leave the device in a useable condition |
| 74 | for (const HwLight& light : supportedLights) { |
| 75 | if (light.type == LightType::BACKLIGHT) { |
| 76 | HwLightState backlightOn; |
| 77 | backlightOn.color = 0xFFFFFFFF; |
| 78 | backlightOn.flashMode = FlashMode::TIMED; |
| 79 | backlightOn.brightnessMode = BrightnessMode::USER; |
| 80 | EXPECT_TRUE(lights->setLightState(light.id, backlightOn).isOk()); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Ensure all reported lights actually work. |
| 88 | */ |
| 89 | TEST_P(LightsAidl, TestSupported) { |
| 90 | HwLightState whiteFlashing; |
| 91 | whiteFlashing.color = 0xFFFFFFFF; |
| 92 | whiteFlashing.flashMode = FlashMode::TIMED; |
| 93 | whiteFlashing.flashOnMs = 100; |
| 94 | whiteFlashing.flashOffMs = 50; |
| 95 | whiteFlashing.brightnessMode = BrightnessMode::USER; |
| 96 | for (const HwLight& light : supportedLights) { |
| 97 | EXPECT_TRUE(lights->setLightState(light.id, whiteFlashing).isOk()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Ensure all reported lights have one of the supported types. |
| 103 | */ |
| 104 | TEST_P(LightsAidl, TestSupportedLightTypes) { |
| 105 | for (const HwLight& light : supportedLights) { |
| 106 | EXPECT_TRUE(kAllTypes.find(light.type) != kAllTypes.end()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Ensure all lights have a unique id. |
| 112 | */ |
| 113 | TEST_P(LightsAidl, TestUniqueIds) { |
| 114 | std::set<int> ids; |
| 115 | for (const HwLight& light : supportedLights) { |
| 116 | EXPECT_TRUE(ids.find(light.id) == ids.end()); |
| 117 | ids.insert(light.id); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Ensure all lights have a unique ordinal for a given type. |
| 123 | */ |
| 124 | TEST_P(LightsAidl, TestUniqueOrdinalsForType) { |
| 125 | std::map<int, std::set<int>> ordinalsByType; |
| 126 | for (const HwLight& light : supportedLights) { |
| 127 | auto& ordinals = ordinalsByType[(int)light.type]; |
| 128 | EXPECT_TRUE(ordinals.find(light.ordinal) == ordinals.end()); |
| 129 | ordinals.insert(light.ordinal); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Ensure EX_UNSUPPORTED_OPERATION is returned if LOW_PERSISTENCE is not supported. |
| 135 | */ |
| 136 | TEST_P(LightsAidl, TestLowPersistence) { |
| 137 | HwLightState lowPersistence; |
| 138 | lowPersistence.color = 0xFF123456; |
| 139 | lowPersistence.flashMode = FlashMode::TIMED; |
| 140 | lowPersistence.flashOnMs = 100; |
| 141 | lowPersistence.flashOffMs = 50; |
| 142 | lowPersistence.brightnessMode = BrightnessMode::LOW_PERSISTENCE; |
| 143 | for (const HwLight& light : supportedLights) { |
| 144 | Status status = lights->setLightState(light.id, lowPersistence); |
| 145 | EXPECT_TRUE(status.isOk() || Status::EX_UNSUPPORTED_OPERATION == status.exceptionCode()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Ensure EX_UNSUPPORTED_OPERATION is returns for an invalid light id. |
| 151 | */ |
| 152 | TEST_P(LightsAidl, TestInvalidLightIdUnsupported) { |
| 153 | int maxId = INT_MIN; |
| 154 | for (const HwLight& light : supportedLights) { |
| 155 | maxId = std::max(maxId, light.id); |
| 156 | } |
| 157 | |
| 158 | Status status = lights->setLightState(maxId + 1, HwLightState()); |
| 159 | EXPECT_TRUE(status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION); |
| 160 | } |
| 161 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 162 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LightsAidl); |
Steven Moreland | 84dd3b7 | 2019-07-25 17:13:52 -0700 | [diff] [blame] | 163 | INSTANTIATE_TEST_SUITE_P(Lights, LightsAidl, |
| 164 | testing::ValuesIn(android::getAidlHalInstanceNames(ILights::descriptor)), |
| 165 | android::PrintInstanceNameToString); |
| 166 | |
| 167 | int main(int argc, char** argv) { |
| 168 | ::testing::InitGoogleTest(&argc, argv); |
| 169 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 170 | ProcessState::self()->startThreadPool(); |
| 171 | return RUN_ALL_TESTS(); |
| 172 | } |