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 <VtsHalHidlTargetTestBase.h> 23 #include <set> 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 37 #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk()) 38 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk()) 39 40 const static LightState kWhite = { 41 .color = 0xFFFFFFFF, 42 .flashMode = Flash::TIMED, 43 .flashOnMs = 100, 44 .flashOffMs = 50, 45 .brightnessMode = Brightness::USER, 46 }; 47 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 56 const static LightState kOff = { 57 .color = 0x00000000, 58 .flashMode = Flash::NONE, 59 .flashOnMs = 0, 60 .flashOffMs = 0, 61 .brightnessMode = Brightness::USER, 62 }; 63 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 75 class LightHidlTest : public ::testing::VtsHalHidlTargetTestBase { 76 public: 77 virtual void SetUp() override { 78 light = ::testing::VtsHalHidlTargetTestBase::getService<ILight>(); 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 110 /** 111 * Ensure all lights which are reported as supported work. 112 */ 113 TEST_F(LightHidlTest, TestSupported) { 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 } 119 } 120 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 } 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 } 149 } 150 151 int main(int argc, char **argv) { 152 ::testing::InitGoogleTest(&argc, argv); 153 int status = RUN_ALL_TESTS(); 154 LOG(INFO) << "Test result = " << status; 155 return status; 156 } 157