xref: /aosp_15_r20/hardware/interfaces/light/aidl/vts/functional/VtsHalLightTargetTest.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker  *
4*4d7e907cSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker  *
8*4d7e907cSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker  *
10*4d7e907cSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker  * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker  */
16*4d7e907cSAndroid Build Coastguard Worker 
17*4d7e907cSAndroid Build Coastguard Worker #define LOG_TAG "light_aidl_hal_test"
18*4d7e907cSAndroid Build Coastguard Worker 
19*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Gtest.h>
20*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Vintf.h>
21*4d7e907cSAndroid Build Coastguard Worker 
22*4d7e907cSAndroid Build Coastguard Worker #include <android-base/logging.h>
23*4d7e907cSAndroid Build Coastguard Worker #include <android/hardware/light/ILights.h>
24*4d7e907cSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
25*4d7e907cSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
26*4d7e907cSAndroid Build Coastguard Worker #include <gtest/gtest.h>
27*4d7e907cSAndroid Build Coastguard Worker #include <hidl/GtestPrinter.h>
28*4d7e907cSAndroid Build Coastguard Worker #include <hidl/ServiceManagement.h>
29*4d7e907cSAndroid Build Coastguard Worker 
30*4d7e907cSAndroid Build Coastguard Worker #include <unistd.h>
31*4d7e907cSAndroid Build Coastguard Worker #include <set>
32*4d7e907cSAndroid Build Coastguard Worker 
33*4d7e907cSAndroid Build Coastguard Worker using android::ProcessState;
34*4d7e907cSAndroid Build Coastguard Worker using android::sp;
35*4d7e907cSAndroid Build Coastguard Worker using android::String16;
36*4d7e907cSAndroid Build Coastguard Worker using android::binder::Status;
37*4d7e907cSAndroid Build Coastguard Worker using android::hardware::hidl_vec;
38*4d7e907cSAndroid Build Coastguard Worker using android::hardware::Return;
39*4d7e907cSAndroid Build Coastguard Worker using android::hardware::Void;
40*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::BrightnessMode;
41*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::FlashMode;
42*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::HwLight;
43*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::HwLightState;
44*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::ILights;
45*4d7e907cSAndroid Build Coastguard Worker using android::hardware::light::LightType;
46*4d7e907cSAndroid Build Coastguard Worker 
47*4d7e907cSAndroid Build Coastguard Worker #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
48*4d7e907cSAndroid Build Coastguard Worker #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
49*4d7e907cSAndroid Build Coastguard Worker 
50*4d7e907cSAndroid Build Coastguard Worker const std::set<LightType> kAllTypes{android::enum_range<LightType>().begin(),
51*4d7e907cSAndroid Build Coastguard Worker                                     android::enum_range<LightType>().end()};
52*4d7e907cSAndroid Build Coastguard Worker 
53*4d7e907cSAndroid Build Coastguard Worker class LightsAidl : public testing::TestWithParam<std::string> {
54*4d7e907cSAndroid Build Coastguard Worker   public:
SetUp()55*4d7e907cSAndroid Build Coastguard Worker     virtual void SetUp() override {
56*4d7e907cSAndroid Build Coastguard Worker         lights = android::waitForDeclaredService<ILights>(String16(GetParam().c_str()));
57*4d7e907cSAndroid Build Coastguard Worker         ASSERT_NE(lights, nullptr);
58*4d7e907cSAndroid Build Coastguard Worker         ASSERT_TRUE(lights->getLights(&supportedLights).isOk());
59*4d7e907cSAndroid Build Coastguard Worker     }
60*4d7e907cSAndroid Build Coastguard Worker 
61*4d7e907cSAndroid Build Coastguard Worker     sp<ILights> lights;
62*4d7e907cSAndroid Build Coastguard Worker     std::vector<HwLight> supportedLights;
63*4d7e907cSAndroid Build Coastguard Worker 
TearDown()64*4d7e907cSAndroid Build Coastguard Worker     virtual void TearDown() override {
65*4d7e907cSAndroid Build Coastguard Worker         for (const HwLight& light : supportedLights) {
66*4d7e907cSAndroid Build Coastguard Worker             HwLightState off;
67*4d7e907cSAndroid Build Coastguard Worker             off.color = 0x00000000;
68*4d7e907cSAndroid Build Coastguard Worker             off.flashMode = FlashMode::NONE;
69*4d7e907cSAndroid Build Coastguard Worker             off.brightnessMode = BrightnessMode::USER;
70*4d7e907cSAndroid Build Coastguard Worker             EXPECT_TRUE(lights->setLightState(light.id, off).isOk());
71*4d7e907cSAndroid Build Coastguard Worker         }
72*4d7e907cSAndroid Build Coastguard Worker 
73*4d7e907cSAndroid Build Coastguard Worker         // must leave the device in a useable condition
74*4d7e907cSAndroid Build Coastguard Worker         for (const HwLight& light : supportedLights) {
75*4d7e907cSAndroid Build Coastguard Worker             if (light.type == LightType::BACKLIGHT) {
76*4d7e907cSAndroid Build Coastguard Worker                 HwLightState backlightOn;
77*4d7e907cSAndroid Build Coastguard Worker                 backlightOn.color = 0xFFFFFFFF;
78*4d7e907cSAndroid Build Coastguard Worker                 backlightOn.flashMode = FlashMode::TIMED;
79*4d7e907cSAndroid Build Coastguard Worker                 backlightOn.brightnessMode = BrightnessMode::USER;
80*4d7e907cSAndroid Build Coastguard Worker                 EXPECT_TRUE(lights->setLightState(light.id, backlightOn).isOk());
81*4d7e907cSAndroid Build Coastguard Worker             }
82*4d7e907cSAndroid Build Coastguard Worker         }
83*4d7e907cSAndroid Build Coastguard Worker     }
84*4d7e907cSAndroid Build Coastguard Worker };
85*4d7e907cSAndroid Build Coastguard Worker 
86*4d7e907cSAndroid Build Coastguard Worker /**
87*4d7e907cSAndroid Build Coastguard Worker  * Ensure all reported lights actually work.
88*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestSupported)89*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestSupported) {
90*4d7e907cSAndroid Build Coastguard Worker     HwLightState whiteFlashing;
91*4d7e907cSAndroid Build Coastguard Worker     whiteFlashing.color = 0xFFFFFFFF;
92*4d7e907cSAndroid Build Coastguard Worker     whiteFlashing.flashMode = FlashMode::TIMED;
93*4d7e907cSAndroid Build Coastguard Worker     whiteFlashing.flashOnMs = 100;
94*4d7e907cSAndroid Build Coastguard Worker     whiteFlashing.flashOffMs = 50;
95*4d7e907cSAndroid Build Coastguard Worker     whiteFlashing.brightnessMode = BrightnessMode::USER;
96*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
97*4d7e907cSAndroid Build Coastguard Worker         EXPECT_TRUE(lights->setLightState(light.id, whiteFlashing).isOk());
98*4d7e907cSAndroid Build Coastguard Worker     }
99*4d7e907cSAndroid Build Coastguard Worker }
100*4d7e907cSAndroid Build Coastguard Worker 
101*4d7e907cSAndroid Build Coastguard Worker /**
102*4d7e907cSAndroid Build Coastguard Worker  * Ensure all reported lights have one of the supported types.
103*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestSupportedLightTypes)104*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestSupportedLightTypes) {
105*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
106*4d7e907cSAndroid Build Coastguard Worker         EXPECT_TRUE(kAllTypes.find(light.type) != kAllTypes.end());
107*4d7e907cSAndroid Build Coastguard Worker     }
108*4d7e907cSAndroid Build Coastguard Worker }
109*4d7e907cSAndroid Build Coastguard Worker 
110*4d7e907cSAndroid Build Coastguard Worker /**
111*4d7e907cSAndroid Build Coastguard Worker  * Ensure all lights have a unique id.
112*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestUniqueIds)113*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestUniqueIds) {
114*4d7e907cSAndroid Build Coastguard Worker     std::set<int> ids;
115*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
116*4d7e907cSAndroid Build Coastguard Worker         EXPECT_TRUE(ids.find(light.id) == ids.end());
117*4d7e907cSAndroid Build Coastguard Worker         ids.insert(light.id);
118*4d7e907cSAndroid Build Coastguard Worker     }
119*4d7e907cSAndroid Build Coastguard Worker }
120*4d7e907cSAndroid Build Coastguard Worker 
121*4d7e907cSAndroid Build Coastguard Worker /**
122*4d7e907cSAndroid Build Coastguard Worker  * Ensure all lights have a unique ordinal for a given type.
123*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestUniqueOrdinalsForType)124*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestUniqueOrdinalsForType) {
125*4d7e907cSAndroid Build Coastguard Worker     std::map<int, std::set<int>> ordinalsByType;
126*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
127*4d7e907cSAndroid Build Coastguard Worker         auto& ordinals = ordinalsByType[(int)light.type];
128*4d7e907cSAndroid Build Coastguard Worker         EXPECT_TRUE(ordinals.find(light.ordinal) == ordinals.end());
129*4d7e907cSAndroid Build Coastguard Worker         ordinals.insert(light.ordinal);
130*4d7e907cSAndroid Build Coastguard Worker     }
131*4d7e907cSAndroid Build Coastguard Worker }
132*4d7e907cSAndroid Build Coastguard Worker 
133*4d7e907cSAndroid Build Coastguard Worker /**
134*4d7e907cSAndroid Build Coastguard Worker  * Ensure EX_UNSUPPORTED_OPERATION is returned if LOW_PERSISTENCE is not supported.
135*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestLowPersistence)136*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestLowPersistence) {
137*4d7e907cSAndroid Build Coastguard Worker     HwLightState lowPersistence;
138*4d7e907cSAndroid Build Coastguard Worker     lowPersistence.color = 0xFF123456;
139*4d7e907cSAndroid Build Coastguard Worker     lowPersistence.flashMode = FlashMode::TIMED;
140*4d7e907cSAndroid Build Coastguard Worker     lowPersistence.flashOnMs = 100;
141*4d7e907cSAndroid Build Coastguard Worker     lowPersistence.flashOffMs = 50;
142*4d7e907cSAndroid Build Coastguard Worker     lowPersistence.brightnessMode = BrightnessMode::LOW_PERSISTENCE;
143*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
144*4d7e907cSAndroid Build Coastguard Worker         Status status = lights->setLightState(light.id, lowPersistence);
145*4d7e907cSAndroid Build Coastguard Worker         EXPECT_TRUE(status.isOk() || Status::EX_UNSUPPORTED_OPERATION == status.exceptionCode());
146*4d7e907cSAndroid Build Coastguard Worker     }
147*4d7e907cSAndroid Build Coastguard Worker }
148*4d7e907cSAndroid Build Coastguard Worker 
149*4d7e907cSAndroid Build Coastguard Worker /**
150*4d7e907cSAndroid Build Coastguard Worker  * Ensure EX_UNSUPPORTED_OPERATION is returns for an invalid light id.
151*4d7e907cSAndroid Build Coastguard Worker  */
TEST_P(LightsAidl,TestInvalidLightIdUnsupported)152*4d7e907cSAndroid Build Coastguard Worker TEST_P(LightsAidl, TestInvalidLightIdUnsupported) {
153*4d7e907cSAndroid Build Coastguard Worker     int maxId = INT_MIN;
154*4d7e907cSAndroid Build Coastguard Worker     for (const HwLight& light : supportedLights) {
155*4d7e907cSAndroid Build Coastguard Worker         maxId = std::max(maxId, light.id);
156*4d7e907cSAndroid Build Coastguard Worker     }
157*4d7e907cSAndroid Build Coastguard Worker 
158*4d7e907cSAndroid Build Coastguard Worker     Status status = lights->setLightState(maxId + 1, HwLightState());
159*4d7e907cSAndroid Build Coastguard Worker     EXPECT_TRUE(status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION);
160*4d7e907cSAndroid Build Coastguard Worker }
161*4d7e907cSAndroid Build Coastguard Worker 
162*4d7e907cSAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LightsAidl);
163*4d7e907cSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(Lights, LightsAidl,
164*4d7e907cSAndroid Build Coastguard Worker                          testing::ValuesIn(android::getAidlHalInstanceNames(ILights::descriptor)),
165*4d7e907cSAndroid Build Coastguard Worker                          android::PrintInstanceNameToString);
166*4d7e907cSAndroid Build Coastguard Worker 
main(int argc,char ** argv)167*4d7e907cSAndroid Build Coastguard Worker int main(int argc, char** argv) {
168*4d7e907cSAndroid Build Coastguard Worker     ::testing::InitGoogleTest(&argc, argv);
169*4d7e907cSAndroid Build Coastguard Worker     ProcessState::self()->setThreadPoolMaxThreadCount(1);
170*4d7e907cSAndroid Build Coastguard Worker     ProcessState::self()->startThreadPool();
171*4d7e907cSAndroid Build Coastguard Worker     return RUN_ALL_TESTS();
172*4d7e907cSAndroid Build Coastguard Worker }
173