Home | History | Annotate | Download | only in functional
      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 <VtsHalHidlTargetTestBase.h>
     20 #include <VtsHalHidlTargetTestEnvBase.h>
     21 #include <android-base/logging.h>
     22 #include <android/hardware/light/2.0/ILight.h>
     23 #include <android/hardware/light/2.0/types.h>
     24 #include <unistd.h>
     25 #include <set>
     26 
     27 using ::android::hardware::light::V2_0::Brightness;
     28 using ::android::hardware::light::V2_0::Flash;
     29 using ::android::hardware::light::V2_0::ILight;
     30 using ::android::hardware::light::V2_0::LightState;
     31 using ::android::hardware::light::V2_0::Status;
     32 using ::android::hardware::light::V2_0::Type;
     33 using ::android::hardware::hidl_vec;
     34 using ::android::hardware::Return;
     35 using ::android::hardware::Void;
     36 using ::android::sp;
     37 
     38 #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
     39 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk())
     40 
     41 const static LightState kWhite = {
     42     .color = 0xFFFFFFFF,
     43     .flashMode = Flash::TIMED,
     44     .flashOnMs = 100,
     45     .flashOffMs = 50,
     46     .brightnessMode = Brightness::USER,
     47 };
     48 
     49 const static LightState kLowPersistance = {
     50     .color = 0xFF123456,
     51     .flashMode = Flash::TIMED,
     52     .flashOnMs = 100,
     53     .flashOffMs = 50,
     54     .brightnessMode = Brightness::LOW_PERSISTENCE,
     55 };
     56 
     57 const static LightState kOff = {
     58     .color = 0x00000000,
     59     .flashMode = Flash::NONE,
     60     .flashOnMs = 0,
     61     .flashOffMs = 0,
     62     .brightnessMode = Brightness::USER,
     63 };
     64 
     65 const static std::set<Type> kAllTypes = {
     66     Type::BACKLIGHT,
     67     Type::KEYBOARD,
     68     Type::BUTTONS,
     69     Type::BATTERY,
     70     Type::NOTIFICATIONS,
     71     Type::ATTENTION,
     72     Type::BLUETOOTH,
     73     Type::WIFI
     74 };
     75 
     76 // Test environment for Light HIDL HAL.
     77 class LightHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
     78    public:
     79     // get the test environment singleton
     80     static LightHidlEnvironment* Instance() {
     81         static LightHidlEnvironment* instance = new LightHidlEnvironment;
     82         return instance;
     83     }
     84 
     85     virtual void registerTestServices() override { registerTestService<ILight>(); }
     86    private:
     87     LightHidlEnvironment() {}
     88 };
     89 
     90 class LightHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     91 public:
     92     virtual void SetUp() override {
     93         light = ::testing::VtsHalHidlTargetTestBase::getService<ILight>(
     94             LightHidlEnvironment::Instance()->getServiceName<ILight>());
     95 
     96         ASSERT_NE(light, nullptr);
     97         LOG(INFO) << "Test is remote " << light->isRemote();
     98 
     99         ASSERT_OK(light->getSupportedTypes([this](const hidl_vec<Type> &types) {
    100             supportedTypes = types;
    101         }));
    102     }
    103 
    104     sp<ILight> light;
    105     std::vector<Type> supportedTypes;
    106 
    107     virtual void TearDown() override {
    108         for (const Type& type: supportedTypes) {
    109             Return<Status> ret = light->setLight(type, kOff);
    110             EXPECT_OK(ret);
    111             EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
    112         }
    113 
    114         // must leave the device in a useable condition
    115         if (std::find(supportedTypes.begin(),
    116                       supportedTypes.end(),
    117                       Type::BACKLIGHT) != supportedTypes.end()) {
    118             Return<Status> ret = light->setLight(Type::BACKLIGHT, kWhite);
    119             EXPECT_OK(ret);
    120             EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
    121         }
    122     }
    123 
    124 };
    125 
    126 /**
    127  * Ensure all lights which are reported as supported work.
    128  */
    129 TEST_F(LightHidlTest, TestSupported) {
    130     for (const Type& type: supportedTypes) {
    131         Return<Status> ret = light->setLight(type, kWhite);
    132         EXPECT_OK(ret);
    133         EXPECT_EQ(Status::SUCCESS, static_cast<Status>(ret));
    134     }
    135 }
    136 
    137 /**
    138  * Ensure BRIGHTNESS_NOT_SUPPORTED is returned if LOW_PERSISTANCE is not supported.
    139  */
    140 TEST_F(LightHidlTest, TestLowPersistance) {
    141     for (const Type& type: supportedTypes) {
    142         Return<Status> ret = light->setLight(type, kLowPersistance);
    143         EXPECT_OK(ret);
    144 
    145         Status status = ret;
    146         EXPECT_TRUE(Status::SUCCESS == status ||
    147                     Status::BRIGHTNESS_NOT_SUPPORTED == status);
    148     }
    149 }
    150 
    151 /**
    152  * Ensure lights which are not supported return LIGHT_NOT_SUPPORTED
    153  */
    154 TEST_F(LightHidlTest, TestUnsupported) {
    155     std::set<Type> unsupportedTypes = kAllTypes;
    156     for (const Type& type: supportedTypes) {
    157         unsupportedTypes.erase(type);
    158     }
    159 
    160     for (const Type& type: unsupportedTypes) {
    161         Return<Status> ret = light->setLight(type, kWhite);
    162         EXPECT_OK(ret);
    163         EXPECT_EQ(Status::LIGHT_NOT_SUPPORTED, static_cast<Status>(ret));
    164     }
    165 }
    166 
    167 int main(int argc, char **argv) {
    168     ::testing::AddGlobalTestEnvironment(LightHidlEnvironment::Instance());
    169     ::testing::InitGoogleTest(&argc, argv);
    170     LightHidlEnvironment::Instance()->init(&argc, argv);
    171     int status = RUN_ALL_TESTS();
    172     LOG(INFO) << "Test result = " << status;
    173     return status;
    174 }
    175