blob: f65005ef63613f7bde4417db41bcdeaf478932f6 [file] [log] [blame]
Steven Morelandcbf2fa02016-11-16 11:15:42 -08001/*
2 * Copyright (C) 2016 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_hidl_hal_test"
18
19#include <android-base/logging.h>
20#include <android/hardware/light/2.0/ILight.h>
21#include <android/hardware/light/2.0/types.h>
Steven Moreland022cda02019-08-21 15:44:07 -070022#include <gtest/gtest.h>
23#include <hidl/GtestPrinter.h>
24#include <hidl/ServiceManagement.h>
25
Steven Morelandcbf2fa02016-11-16 11:15:42 -080026#include <unistd.h>
Zhuoyao Zhang0f6e77c2018-02-08 20:58:54 -080027#include <set>
Steven Morelandcbf2fa02016-11-16 11:15:42 -080028
29using ::android::hardware::light::V2_0::Brightness;
30using ::android::hardware::light::V2_0::Flash;
31using ::android::hardware::light::V2_0::ILight;
32using ::android::hardware::light::V2_0::LightState;
33using ::android::hardware::light::V2_0::Status;
34using ::android::hardware::light::V2_0::Type;
35using ::android::hardware::hidl_vec;
36using ::android::hardware::Return;
37using ::android::hardware::Void;
38using ::android::sp;
39
Steven Morelandb6438422017-01-03 17:06:57 -080040#define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
41#define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
Steven Morelandcbf2fa02016-11-16 11:15:42 -080042
Steven Morelandcbf2fa02016-11-16 11:15:42 -080043const static LightState kWhite = {
44 .color = 0xFFFFFFFF,
45 .flashMode = Flash::TIMED,
46 .flashOnMs = 100,
47 .flashOffMs = 50,
48 .brightnessMode = Brightness::USER,
49};
50
Steven Moreland82401672016-12-28 14:42:16 -080051const static LightState kLowPersistance = {
52 .color = 0xFF123456,
53 .flashMode = Flash::TIMED,
54 .flashOnMs = 100,
55 .flashOffMs = 50,
56 .brightnessMode = Brightness::LOW_PERSISTENCE,
57};
58
Steven Morelandcbf2fa02016-11-16 11:15:42 -080059const static LightState kOff = {
60 .color = 0x00000000,
61 .flashMode = Flash::NONE,
62 .flashOnMs = 0,
63 .flashOffMs = 0,
64 .brightnessMode = Brightness::USER,
65};
66
Steven Moreland82401672016-12-28 14:42:16 -080067const static std::set<Type> kAllTypes = {
68 Type::BACKLIGHT,
69 Type::KEYBOARD,
70 Type::BUTTONS,
71 Type::BATTERY,
72 Type::NOTIFICATIONS,
73 Type::ATTENTION,
74 Type::BLUETOOTH,
75 Type::WIFI
76};
77
Steven Moreland022cda02019-08-21 15:44:07 -070078class LightHidlTest : public testing::TestWithParam<std::string> {
79 public:
Steven Moreland18fef4e2017-01-19 16:32:14 -080080 virtual void SetUp() override {
Steven Moreland022cda02019-08-21 15:44:07 -070081 light = ILight::getService(GetParam());
Steven Moreland18fef4e2017-01-19 16:32:14 -080082
83 ASSERT_NE(light, nullptr);
84 LOG(INFO) << "Test is remote " << light->isRemote();
85
86 ASSERT_OK(light->getSupportedTypes([this](const hidl_vec<Type> &types) {
87 supportedTypes = types;
88 }));
89 }
90
91 sp<ILight> light;
92 std::vector<Type> supportedTypes;
93
94 virtual void TearDown() override {
95 for (const Type& type: supportedTypes) {
96 Return<Status> ret = light->setLight(type, kOff);
97 EXPECT_OK(ret);
98 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
99 }
100
101 // must leave the device in a useable condition
102 if (std::find(supportedTypes.begin(),
103 supportedTypes.end(),
104 Type::BACKLIGHT) != supportedTypes.end()) {
105 Return<Status> ret = light->setLight(Type::BACKLIGHT, kWhite);
106 EXPECT_OK(ret);
107 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
108 }
109 }
Steven Moreland18fef4e2017-01-19 16:32:14 -0800110};
111
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800112/**
113 * Ensure all lights which are reported as supported work.
114 */
Steven Moreland022cda02019-08-21 15:44:07 -0700115TEST_P(LightHidlTest, TestSupported) {
Steven Moreland82401672016-12-28 14:42:16 -0800116 for (const Type& type: supportedTypes) {
117 Return<Status> ret = light->setLight(type, kWhite);
118 EXPECT_OK(ret);
119 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
120 }
Steven Moreland82401672016-12-28 14:42:16 -0800121}
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800122
Steven Moreland82401672016-12-28 14:42:16 -0800123/**
124 * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported.
125 */
Steven Moreland022cda02019-08-21 15:44:07 -0700126TEST_P(LightHidlTest, TestLowPersistance) {
Steven Moreland82401672016-12-28 14:42:16 -0800127 for (const Type& type: supportedTypes) {
128 Return<Status> ret = light->setLight(type, kLowPersistance);
129 EXPECT_OK(ret);
130
131 Status status = ret;
132 EXPECT_TRUE(Status::SUCCESS == status ||
133 Status::BRIGHTNESS_NOT_SUPPORTED == status);
134 }
Steven Moreland82401672016-12-28 14:42:16 -0800135}
136
137/**
138 * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED
139 */
Steven Moreland022cda02019-08-21 15:44:07 -0700140TEST_P(LightHidlTest, TestUnsupported) {
Steven Moreland82401672016-12-28 14:42:16 -0800141 std::set<Type> unsupportedTypes = kAllTypes;
142 for (const Type& type: supportedTypes) {
143 unsupportedTypes.erase(type);
144 }
145
146 for (const Type& type: unsupportedTypes) {
147 Return<Status> ret = light->setLight(type, kWhite);
148 EXPECT_OK(ret);
149 EXPECT_EQ(Status::LIGHT_NOT_SUPPORTED, static_cast<Status>(ret));
150 }
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800151}
152
Dan Shiba4d5322020-07-28 13:09:30 -0700153GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LightHidlTest);
Steven Moreland022cda02019-08-21 15:44:07 -0700154INSTANTIATE_TEST_SUITE_P(
155 PerInstance, LightHidlTest,
156 testing::ValuesIn(android::hardware::getAllHalInstanceNames(ILight::descriptor)),
157 android::hardware::PrintInstanceNameToString);