Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 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> |
Yuexi Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 22 | #include <VtsHalHidlTargetBaseTest.h> |
Steven Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 23 | #include <set> |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | using ::android::hardware::light::V2_0::Brightness; |
| 27 | using ::android::hardware::light::V2_0::Flash; |
| 28 | using ::android::hardware::light::V2_0::ILight; |
| 29 | using ::android::hardware::light::V2_0::LightState; |
| 30 | using ::android::hardware::light::V2_0::Status; |
| 31 | using ::android::hardware::light::V2_0::Type; |
| 32 | using ::android::hardware::hidl_vec; |
| 33 | using ::android::hardware::Return; |
| 34 | using ::android::hardware::Void; |
| 35 | using ::android::sp; |
| 36 | |
Steven Moreland | b643842 | 2017-01-03 17:06:57 -0800 | [diff] [blame] | 37 | #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk()) |
| 38 | #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk()) |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 39 | |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 40 | const static LightState kWhite = { |
| 41 | .color = 0xFFFFFFFF, |
| 42 | .flashMode = Flash::TIMED, |
| 43 | .flashOnMs = 100, |
| 44 | .flashOffMs = 50, |
| 45 | .brightnessMode = Brightness::USER, |
| 46 | }; |
| 47 | |
Steven Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 48 | const static LightState kLowPersistance = { |
| 49 | .color = 0xFF123456, |
| 50 | .flashMode = Flash::TIMED, |
| 51 | .flashOnMs = 100, |
| 52 | .flashOffMs = 50, |
| 53 | .brightnessMode = Brightness::LOW_PERSISTENCE, |
| 54 | }; |
| 55 | |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 56 | const static LightState kOff = { |
| 57 | .color = 0x00000000, |
| 58 | .flashMode = Flash::NONE, |
| 59 | .flashOnMs = 0, |
| 60 | .flashOffMs = 0, |
| 61 | .brightnessMode = Brightness::USER, |
| 62 | }; |
| 63 | |
Steven Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 64 | const 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 Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 75 | class LightHidlTest : public ::testing::VtsHalHidlTargetBaseTest { |
Steven Moreland | 18fef4e | 2017-01-19 16:32:14 -0800 | [diff] [blame] | 76 | public: |
| 77 | virtual void SetUp() override { |
Yuexi Ma | 50d7e27 | 2017-02-28 01:46:51 -0800 | [diff] [blame] | 78 | light = ::testing::VtsHalHidlTargetBaseTest::getService<ILight>(); |
Steven Moreland | 18fef4e | 2017-01-19 16:32:14 -0800 | [diff] [blame] | 79 | |
| 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 Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Ensure all lights which are reported as supported work. |
| 112 | */ |
| 113 | TEST_F(LightHidlTest, TestSupported) { |
Steven Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 114 | 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 Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 119 | } |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 120 | |
Steven Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported. |
| 123 | */ |
| 124 | TEST_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 Moreland | 8240167 | 2016-12-28 14:42:16 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED |
| 137 | */ |
| 138 | TEST_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 Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int main(int argc, char **argv) { |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 152 | ::testing::InitGoogleTest(&argc, argv); |
| 153 | int status = RUN_ALL_TESTS(); |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 154 | LOG(INFO) << "Test result = " << status; |
Steven Moreland | cbf2fa0 | 2016-11-16 11:15:42 -0800 | [diff] [blame] | 155 | return status; |
| 156 | } |