blob: 71a8b4e5f2979b692184037de49ec9bb6bb8253e [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>
22#include <gtest/gtest.h>
Steven Moreland82401672016-12-28 14:42:16 -080023#include <set>
Steven Morelandcbf2fa02016-11-16 11:15:42 -080024#include <unistd.h>
25
26using ::android::hardware::light::V2_0::Brightness;
27using ::android::hardware::light::V2_0::Flash;
28using ::android::hardware::light::V2_0::ILight;
29using ::android::hardware::light::V2_0::LightState;
30using ::android::hardware::light::V2_0::Status;
31using ::android::hardware::light::V2_0::Type;
32using ::android::hardware::hidl_vec;
33using ::android::hardware::Return;
34using ::android::hardware::Void;
35using ::android::sp;
36
Steven Morelandb6438422017-01-03 17:06:57 -080037#define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
38#define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
Steven Morelandcbf2fa02016-11-16 11:15:42 -080039
Steven Morelandcbf2fa02016-11-16 11:15:42 -080040class LightHidlTest : public ::testing::Test {
41public:
42 virtual void SetUp() override {
Chris Phoenix6d0a0912017-01-23 14:17:23 -080043 light = ILight::getService();
Steven Morelandcbf2fa02016-11-16 11:15:42 -080044
45 ASSERT_NE(light, nullptr);
Yifan Hongf9d30342016-11-30 13:45:34 -080046 LOG(INFO) << "Test is remote " << light->isRemote();
Steven Moreland82401672016-12-28 14:42:16 -080047
48 ASSERT_OK(light->getSupportedTypes([this](const hidl_vec<Type> &types) {
49 supportedTypes = types;
50 }));
Steven Morelandcbf2fa02016-11-16 11:15:42 -080051 }
52
53 virtual void TearDown() override {}
54
55 sp<ILight> light;
Steven Moreland82401672016-12-28 14:42:16 -080056 std::vector<Type> supportedTypes;
Steven Morelandcbf2fa02016-11-16 11:15:42 -080057};
58
Steven Morelandcbf2fa02016-11-16 11:15:42 -080059class LightHidlEnvironment : public ::testing::Environment {
60public:
61 virtual void SetUp() {}
62 virtual void TearDown() {}
Steven Morelandcbf2fa02016-11-16 11:15:42 -080063};
64
65const static LightState kWhite = {
66 .color = 0xFFFFFFFF,
67 .flashMode = Flash::TIMED,
68 .flashOnMs = 100,
69 .flashOffMs = 50,
70 .brightnessMode = Brightness::USER,
71};
72
Steven Moreland82401672016-12-28 14:42:16 -080073const static LightState kLowPersistance = {
74 .color = 0xFF123456,
75 .flashMode = Flash::TIMED,
76 .flashOnMs = 100,
77 .flashOffMs = 50,
78 .brightnessMode = Brightness::LOW_PERSISTENCE,
79};
80
Steven Morelandcbf2fa02016-11-16 11:15:42 -080081const static LightState kOff = {
82 .color = 0x00000000,
83 .flashMode = Flash::NONE,
84 .flashOnMs = 0,
85 .flashOffMs = 0,
86 .brightnessMode = Brightness::USER,
87};
88
Steven Moreland82401672016-12-28 14:42:16 -080089const static std::set<Type> kAllTypes = {
90 Type::BACKLIGHT,
91 Type::KEYBOARD,
92 Type::BUTTONS,
93 Type::BATTERY,
94 Type::NOTIFICATIONS,
95 Type::ATTENTION,
96 Type::BLUETOOTH,
97 Type::WIFI
98};
99
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800100/**
101 * Ensure all lights which are reported as supported work.
102 */
103TEST_F(LightHidlTest, TestSupported) {
Steven Moreland82401672016-12-28 14:42:16 -0800104 for (const Type& type: supportedTypes) {
105 Return<Status> ret = light->setLight(type, kWhite);
106 EXPECT_OK(ret);
107 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
108 }
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800109
Steven Moreland82401672016-12-28 14:42:16 -0800110 for (const Type& type: supportedTypes) {
111 Return<Status> ret = light->setLight(type, kOff);
112 EXPECT_OK(ret);
113 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
114 }
115}
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800116
Steven Moreland82401672016-12-28 14:42:16 -0800117/**
118 * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported.
119 */
120TEST_F(LightHidlTest, TestLowPersistance) {
121 for (const Type& type: supportedTypes) {
122 Return<Status> ret = light->setLight(type, kLowPersistance);
123 EXPECT_OK(ret);
124
125 Status status = ret;
126 EXPECT_TRUE(Status::SUCCESS == status ||
127 Status::BRIGHTNESS_NOT_SUPPORTED == status);
128 }
129
130 for (const Type& type: supportedTypes) {
131 Return<Status> ret = light->setLight(type, kOff);
132 EXPECT_OK(ret);
133 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
134 }
135}
136
137/**
138 * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED
139 */
140TEST_F(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 }
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800151}
152
153int main(int argc, char **argv) {
154 ::testing::AddGlobalTestEnvironment(new LightHidlEnvironment);
155 ::testing::InitGoogleTest(&argc, argv);
156 int status = RUN_ALL_TESTS();
Yifan Hongf9d30342016-11-30 13:45:34 -0800157 LOG(INFO) << "Test result = " << status;
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800158 return status;
159}