Home | History | Annotate | Download | only in master
      1 /*
      2  * Copyright (C) 2017 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 "media_omx_hidl_master_test"
     18 #include <android-base/logging.h>
     19 
     20 #include <android/hardware/media/omx/1.0/IOmx.h>
     21 #include <android/hardware/media/omx/1.0/IOmxNode.h>
     22 #include <android/hardware/media/omx/1.0/IOmxObserver.h>
     23 #include <android/hardware/media/omx/1.0/IOmxStore.h>
     24 #include <android/hardware/media/omx/1.0/types.h>
     25 #include <android/hidl/allocator/1.0/IAllocator.h>
     26 #include <android/hidl/memory/1.0/IMapper.h>
     27 #include <android/hidl/memory/1.0/IMemory.h>
     28 
     29 using ::android::hardware::media::omx::V1_0::IOmx;
     30 using ::android::hardware::media::omx::V1_0::IOmxObserver;
     31 using ::android::hardware::media::omx::V1_0::IOmxNode;
     32 using ::android::hardware::media::omx::V1_0::IOmxStore;
     33 using ::android::hardware::media::omx::V1_0::Message;
     34 using ::android::hardware::media::omx::V1_0::CodecBuffer;
     35 using ::android::hardware::media::omx::V1_0::PortMode;
     36 using ::android::hidl::allocator::V1_0::IAllocator;
     37 using ::android::hidl::memory::V1_0::IMemory;
     38 using ::android::hidl::memory::V1_0::IMapper;
     39 using ::android::hardware::Return;
     40 using ::android::hardware::Void;
     41 using ::android::hardware::hidl_vec;
     42 using ::android::hardware::hidl_string;
     43 using ::android::sp;
     44 
     45 #include <VtsHalHidlTargetTestBase.h>
     46 #include <getopt.h>
     47 #include <media_hidl_test_common.h>
     48 
     49 // A class for test environment setup
     50 class ComponentTestEnvironment : public ::testing::Environment {
     51    public:
     52     virtual void SetUp() {}
     53     virtual void TearDown() {}
     54 
     55     ComponentTestEnvironment() : instance("default") {}
     56 
     57     void setInstance(const char* _instance) { instance = _instance; }
     58 
     59     const hidl_string getInstance() const { return instance; }
     60 
     61     int initFromOptions(int argc, char** argv) {
     62         static struct option options[] = {
     63             {"instance", required_argument, 0, 'I'}, {0, 0, 0, 0}};
     64 
     65         while (true) {
     66             int index = 0;
     67             int c = getopt_long(argc, argv, "I:", options, &index);
     68             if (c == -1) {
     69                 break;
     70             }
     71 
     72             switch (c) {
     73                 case 'I':
     74                     setInstance(optarg);
     75                     break;
     76                 case '?':
     77                     break;
     78             }
     79         }
     80 
     81         if (optind < argc) {
     82             fprintf(stderr,
     83                     "unrecognized option: %s\n\n"
     84                     "usage: %s <gtest options> <test options>\n\n"
     85                     "test options are:\n\n"
     86                     "-I, --instance: HAL instance to test\n",
     87                     argv[optind ?: 1], argv[0]);
     88             return 2;
     89         }
     90         return 0;
     91     }
     92 
     93    private:
     94     hidl_string instance;
     95 };
     96 
     97 static ComponentTestEnvironment* gEnv = nullptr;
     98 
     99 class MasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
    100    public:
    101     virtual void SetUp() override {
    102         omxStore = nullptr;
    103         omxStore = ::testing::VtsHalHidlTargetTestBase::getService<IOmxStore>();
    104         ASSERT_NE(omxStore, nullptr);
    105         omx = nullptr;
    106         omx = omxStore->getOmx(gEnv->getInstance());
    107         ASSERT_NE(omx, nullptr);
    108     }
    109 
    110     virtual void TearDown() override {}
    111 
    112     sp<IOmxStore> omxStore;
    113     sp<IOmx> omx;
    114 
    115    protected:
    116     static void description(const std::string& description) {
    117         RecordProperty("description", description);
    118     }
    119 };
    120 
    121 void displayComponentInfo(hidl_vec<IOmx::ComponentInfo>& nodeList) {
    122     for (size_t i = 0; i < nodeList.size(); i++) {
    123         printf("%s | ", nodeList[i].mName.c_str());
    124         for (size_t j = 0; j < ((nodeList[i]).mRoles).size(); j++) {
    125             printf("%s ", nodeList[i].mRoles[j].c_str());
    126         }
    127         printf("\n");
    128     }
    129 }
    130 
    131 // list service attributes
    132 TEST_F(MasterHidlTest, ListServiceAttr) {
    133     description("list service attributes");
    134     android::hardware::media::omx::V1_0::Status status;
    135     hidl_vec<IOmxStore::Attribute> attributes;
    136     EXPECT_TRUE(omxStore
    137                     ->listServiceAttributes([&status, &attributes](
    138                         android::hardware::media::omx::V1_0::Status _s,
    139                         hidl_vec<IOmxStore::Attribute> const& _nl) {
    140                         status = _s;
    141                         attributes = _nl;
    142                     })
    143                     .isOk());
    144     if (attributes.size() == 0) ALOGV("Warning, Attribute list empty");
    145 }
    146 
    147 // get node prefix
    148 TEST_F(MasterHidlTest, getNodePrefix) {
    149     description("get node prefix");
    150     hidl_string prefix;
    151     omxStore->getNodePrefix(
    152         [&prefix](hidl_string const& _nl) { prefix = _nl; });
    153     if (prefix.empty()) ALOGV("Warning, Node Prefix empty");
    154 }
    155 
    156 // list roles
    157 TEST_F(MasterHidlTest, ListRoles) {
    158     description("list roles");
    159     hidl_vec<IOmxStore::RoleInfo> roleList;
    160     omxStore->listRoles([&roleList](hidl_vec<IOmxStore::RoleInfo> const& _nl) {
    161         roleList = _nl;
    162     });
    163     if (roleList.size() == 0) ALOGV("Warning, RoleInfo list empty");
    164 }
    165 
    166 // list components and roles.
    167 TEST_F(MasterHidlTest, ListNodes) {
    168     description("enumerate component and roles");
    169     android::hardware::media::omx::V1_0::Status status;
    170     hidl_vec<IOmx::ComponentInfo> nodeList;
    171     bool isPass = true;
    172     EXPECT_TRUE(
    173         omx->listNodes([&status, &nodeList](
    174                            android::hardware::media::omx::V1_0::Status _s,
    175                            hidl_vec<IOmx::ComponentInfo> const& _nl) {
    176                status = _s;
    177                nodeList = _nl;
    178            })
    179             .isOk());
    180     if (nodeList.size() == 0)
    181         ALOGV("Warning, ComponentInfo list empty");
    182     else {
    183         // displayComponentInfo(nodeList);
    184         for (size_t i = 0; i < nodeList.size(); i++) {
    185             sp<CodecObserver> observer = nullptr;
    186             sp<IOmxNode> omxNode = nullptr;
    187             observer = new CodecObserver(nullptr);
    188             ASSERT_NE(observer, nullptr);
    189             EXPECT_TRUE(
    190                 omx->allocateNode(
    191                        nodeList[i].mName, observer,
    192                        [&](android::hardware::media::omx::V1_0::Status _s,
    193                            sp<IOmxNode> const& _nl) {
    194                            status = _s;
    195                            omxNode = _nl;
    196                        })
    197                     .isOk());
    198             if (omxNode == nullptr) {
    199                 isPass = false;
    200                 std::cerr << "[    !OK   ] " << nodeList[i].mName.c_str()
    201                           << "\n";
    202             } else {
    203                 EXPECT_TRUE((omxNode->freeNode()).isOk());
    204                 omxNode = nullptr;
    205                 // std::cout << "[     OK   ] " << nodeList[i].mName.c_str() <<
    206                 // "\n";
    207             }
    208         }
    209     }
    210     EXPECT_TRUE(isPass);
    211 }
    212 
    213 int main(int argc, char** argv) {
    214     gEnv = new ComponentTestEnvironment();
    215     ::testing::AddGlobalTestEnvironment(gEnv);
    216     ::testing::InitGoogleTest(&argc, argv);
    217     int status = gEnv->initFromOptions(argc, argv);
    218     if (status == 0) {
    219         status = RUN_ALL_TESTS();
    220         ALOGI("Test result = %d", status);
    221     }
    222     return status;
    223 }
    224