1 /*
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>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25
26 #include <unistd.h>
27 #include <set>
28
29 using ::android::hardware::light::V2_0::Brightness;
30 using ::android::hardware::light::V2_0::Flash;
31 using ::android::hardware::light::V2_0::ILight;
32 using ::android::hardware::light::V2_0::LightState;
33 using ::android::hardware::light::V2_0::Status;
34 using ::android::hardware::light::V2_0::Type;
35 using ::android::hardware::hidl_vec;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38 using ::android::sp;
39
40 #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
41 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
42
43 const static LightState kWhite = {
44 .color = 0xFFFFFFFF,
45 .flashMode = Flash::TIMED,
46 .flashOnMs = 100,
47 .flashOffMs = 50,
48 .brightnessMode = Brightness::USER,
49 };
50
51 const static LightState kLowPersistance = {
52 .color = 0xFF123456,
53 .flashMode = Flash::TIMED,
54 .flashOnMs = 100,
55 .flashOffMs = 50,
56 .brightnessMode = Brightness::LOW_PERSISTENCE,
57 };
58
59 const static LightState kOff = {
60 .color = 0x00000000,
61 .flashMode = Flash::NONE,
62 .flashOnMs = 0,
63 .flashOffMs = 0,
64 .brightnessMode = Brightness::USER,
65 };
66
67 const 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
78 class LightHidlTest : public testing::TestWithParam<std::string> {
79 public:
SetUp()80 virtual void SetUp() override {
81 light = ILight::getService(GetParam());
82
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
TearDown()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 }
110 };
111
112 /**
113 * Ensure all lights which are reported as supported work.
114 */
TEST_P(LightHidlTest,TestSupported)115 TEST_P(LightHidlTest, TestSupported) {
116 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 }
121 }
122
123 /**
124 * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported.
125 */
TEST_P(LightHidlTest,TestLowPersistance)126 TEST_P(LightHidlTest, TestLowPersistance) {
127 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 }
135 }
136
137 /**
138 * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED
139 */
TEST_P(LightHidlTest,TestUnsupported)140 TEST_P(LightHidlTest, TestUnsupported) {
141 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 }
151 }
152
153 INSTANTIATE_TEST_SUITE_P(
154 PerInstance, LightHidlTest,
155 testing::ValuesIn(android::hardware::getAllHalInstanceNames(ILight::descriptor)),
156 android::hardware::PrintInstanceNameToString);
157