blob: 50b6d5f4574c40684e9d97ba16dcba748e3739af [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>
Yuexi Ma50d7e272017-02-28 01:46:51 -080022#include <VtsHalHidlTargetBaseTest.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 -080040const static LightState kWhite = {
41 .color = 0xFFFFFFFF,
42 .flashMode = Flash::TIMED,
43 .flashOnMs = 100,
44 .flashOffMs = 50,
45 .brightnessMode = Brightness::USER,
46};
47
Steven Moreland82401672016-12-28 14:42:16 -080048const static LightState kLowPersistance = {
49 .color = 0xFF123456,
50 .flashMode = Flash::TIMED,
51 .flashOnMs = 100,
52 .flashOffMs = 50,
53 .brightnessMode = Brightness::LOW_PERSISTENCE,
54};
55
Steven Morelandcbf2fa02016-11-16 11:15:42 -080056const static LightState kOff = {
57 .color = 0x00000000,
58 .flashMode = Flash::NONE,
59 .flashOnMs = 0,
60 .flashOffMs = 0,
61 .brightnessMode = Brightness::USER,
62};
63
Steven Moreland82401672016-12-28 14:42:16 -080064const static std::set<Type> kAllTypes = {
65 Type::BACKLIGHT,
66 Type::KEYBOARD,
67 Type::BUTTONS,
68 Type::BATTERY,
69 Type::NOTIFICATIONS,
70 Type::ATTENTION,
71 Type::BLUETOOTH,
72 Type::WIFI
73};
74
Yuexi Ma50d7e272017-02-28 01:46:51 -080075class LightHidlTest : public ::testing::VtsHalHidlTargetBaseTest {
Steven Moreland18fef4e2017-01-19 16:32:14 -080076public:
77 virtual void SetUp() override {
Yuexi Ma50d7e272017-02-28 01:46:51 -080078 light = ::testing::VtsHalHidlTargetBaseTest::getService<ILight>();
Steven Moreland18fef4e2017-01-19 16:32:14 -080079
80 ASSERT_NE(light, nullptr);
81 LOG(INFO) << "Test is remote " << light->isRemote();
82
83 ASSERT_OK(light->getSupportedTypes([this](const hidl_vec<Type> &types) {
84 supportedTypes = types;
85 }));
86 }
87
88 sp<ILight> light;
89 std::vector<Type> supportedTypes;
90
91 virtual void TearDown() override {
92 for (const Type& type: supportedTypes) {
93 Return<Status> ret = light->setLight(type, kOff);
94 EXPECT_OK(ret);
95 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
96 }
97
98 // must leave the device in a useable condition
99 if (std::find(supportedTypes.begin(),
100 supportedTypes.end(),
101 Type::BACKLIGHT) != supportedTypes.end()) {
102 Return<Status> ret = light->setLight(Type::BACKLIGHT, kWhite);
103 EXPECT_OK(ret);
104 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
105 }
106 }
107
108};
109
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800110/**
111 * Ensure all lights which are reported as supported work.
112 */
113TEST_F(LightHidlTest, TestSupported) {
Steven Moreland82401672016-12-28 14:42:16 -0800114 for (const Type& type: supportedTypes) {
115 Return<Status> ret = light->setLight(type, kWhite);
116 EXPECT_OK(ret);
117 EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
118 }
Steven Moreland82401672016-12-28 14:42:16 -0800119}
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800120
Steven Moreland82401672016-12-28 14:42:16 -0800121/**
122 * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported.
123 */
124TEST_F(LightHidlTest, TestLowPersistance) {
125 for (const Type& type: supportedTypes) {
126 Return<Status> ret = light->setLight(type, kLowPersistance);
127 EXPECT_OK(ret);
128
129 Status status = ret;
130 EXPECT_TRUE(Status::SUCCESS == status ||
131 Status::BRIGHTNESS_NOT_SUPPORTED == status);
132 }
Steven Moreland82401672016-12-28 14:42:16 -0800133}
134
135/**
136 * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED
137 */
138TEST_F(LightHidlTest, TestUnsupported) {
139 std::set<Type> unsupportedTypes = kAllTypes;
140 for (const Type& type: supportedTypes) {
141 unsupportedTypes.erase(type);
142 }
143
144 for (const Type& type: unsupportedTypes) {
145 Return<Status> ret = light->setLight(type, kWhite);
146 EXPECT_OK(ret);
147 EXPECT_EQ(Status::LIGHT_NOT_SUPPORTED, static_cast<Status>(ret));
148 }
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800149}
150
151int main(int argc, char **argv) {
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800152 ::testing::InitGoogleTest(&argc, argv);
153 int status = RUN_ALL_TESTS();
Yifan Hongf9d30342016-11-30 13:45:34 -0800154 LOG(INFO) << "Test result = " << status;
Steven Morelandcbf2fa02016-11-16 11:15:42 -0800155 return status;
156}