blob: 5e3eabdbe5ca2c5bdfb86e8b59f945098801f221 [file] [log] [blame]
Steven Moreland84dd3b72019-07-25 17:13:52 -07001/*
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
33using android::ProcessState;
34using android::sp;
35using android::String16;
36using android::binder::Status;
37using android::hardware::hidl_vec;
38using android::hardware::Return;
39using android::hardware::Void;
40using android::hardware::light::BrightnessMode;
41using android::hardware::light::FlashMode;
42using android::hardware::light::HwLight;
43using android::hardware::light::HwLightState;
44using android::hardware::light::ILights;
45using android::hardware::light::LightType;
46
47#define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
48#define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
49
50const std::set<LightType> kAllTypes{android::enum_range<LightType>().begin(),
51 android::enum_range<LightType>().end()};
52
53class 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 */
89TEST_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 */
104TEST_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 */
113TEST_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 */
124TEST_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 */
136TEST_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 */
152TEST_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 Shiba4d5322020-07-28 13:09:30 -0700162GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LightsAidl);
Steven Moreland84dd3b72019-07-25 17:13:52 -0700163INSTANTIATE_TEST_SUITE_P(Lights, LightsAidl,
164 testing::ValuesIn(android::getAidlHalInstanceNames(ILights::descriptor)),
165 android::PrintInstanceNameToString);
166
167int 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}