Home | History | Annotate | Download | only in libvehiclenetwork-native-test
      1 /*
      2  * Copyright (C) 2015 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 #include <unistd.h>
     18 
     19 #include <gtest/gtest.h>
     20 #include <binder/IServiceManager.h>
     21 #include <binder/ProcessState.h>
     22 #include <utils/threads.h>
     23 #include <utils/KeyedVector.h>
     24 #include <utils/String8.h>
     25 #include <utils/SystemClock.h>
     26 #include <VehicleNetwork.h>
     27 
     28 #include "VehicleNetworkTestListener.h"
     29 
     30 namespace android {
     31 
     32 // Be careful with name conflict with other tests!. It can lead into wrong virtual function table
     33 // , leading into mysterious crash. Always add test name in front for any class name.
     34 class VehicleNetworkTest : public testing::Test {
     35 public:
     36     VehicleNetworkTest() :
     37         mVN(NULL),
     38         mListener(new VehicleNetworkTestListener()) {
     39         String8 msg;
     40         msg.appendFormat("Creating VehicleNetworkTest 0x%p %p %p\n", this, mVN.get(),
     41                 mListener.get());
     42         std::cout<<msg.string();
     43     }
     44 
     45     virtual ~VehicleNetworkTest() { }
     46 
     47     sp<VehicleNetwork> getDefaultVN() {
     48         return mVN;
     49     }
     50 
     51     VehicleNetworkTestListener& getTestListener() {
     52         return *mListener.get();
     53     }
     54 
     55 protected:
     56     void SetUp() {
     57         String8 msg;
     58         msg.appendFormat("setUp starts %p %p %p\n", this, mVN.get(),
     59                 mListener.get());
     60         std::cout<<msg.string();
     61         ASSERT_TRUE(mListener.get() != NULL);
     62         sp<VehicleNetworkListener> listener(mListener.get());
     63         mVN = VehicleNetwork::createVehicleNetwork(listener);
     64         ASSERT_TRUE(mVN.get() != NULL);
     65         std::cout<<"setUp ends\n";
     66     }
     67 
     68 protected:
     69     sp<VehicleNetwork> mVN;
     70     sp<VehicleNetworkTestListener> mListener;
     71 };
     72 
     73 
     74 TEST_F(VehicleNetworkTest, listProperties) {
     75     sp<VehicleNetwork> vn = getDefaultVN();
     76     sp<VehiclePropertiesHolder> properties = vn->listProperties();
     77     ASSERT_TRUE(properties.get() != NULL);
     78     int32_t numConfigs = properties->getList().size();
     79     ASSERT_TRUE(numConfigs > 0);
     80     for (auto& config : properties->getList()) {
     81         String8 msg = String8::format("prop 0x%x\n", config->prop);
     82         std::cout<<msg.string();
     83     }
     84     sp<VehiclePropertiesHolder> propertiesIvalid  = vn->listProperties(-1); // no such property
     85     ASSERT_TRUE(propertiesIvalid.get() == NULL);
     86     for (auto& config : properties->getList()) {
     87         String8 msg = String8::format("query single prop 0x%x\n", config->prop);
     88         std::cout<<msg.string();
     89         sp<VehiclePropertiesHolder> singleProperty = vn->listProperties(config->prop);
     90         ASSERT_EQ((size_t) 1, singleProperty->getList().size());
     91         vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin();
     92         ASSERT_EQ(config->prop, newConfig->prop);
     93         ASSERT_EQ(config->access, newConfig->access);
     94         ASSERT_EQ(config->change_mode, newConfig->change_mode);
     95         //TODO add more check
     96     }
     97 }
     98 
     99 TEST_F(VehicleNetworkTest, getProperty) {
    100     sp<VehicleNetwork> vn = getDefaultVN();
    101     sp<VehiclePropertiesHolder> properties = vn->listProperties();
    102     ASSERT_TRUE(properties.get() != NULL);
    103     int32_t numConfigs = properties->getList().size();
    104     ASSERT_TRUE(numConfigs > 0);
    105     for (auto& config : properties->getList()) {
    106         if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
    107             continue;
    108         }
    109         String8 msg = String8::format("getting prop 0x%x\n", config->prop);
    110         std::cout<<msg.string();
    111         ScopedVehiclePropValue value;
    112         value.value.prop = config->prop;
    113         value.value.value_type = config->value_type;
    114         status_t r = vn->getProperty(&value.value);
    115         if ((config->access & VEHICLE_PROP_ACCESS_READ) == 0) { // cannot read
    116             ASSERT_TRUE(r != NO_ERROR);
    117         } else {
    118             ASSERT_EQ(NO_ERROR, r);
    119             ASSERT_EQ(config->value_type, value.value.value_type);
    120         }
    121     }
    122 }
    123 
    124 //TODO change this test to to safe write
    125 TEST_F(VehicleNetworkTest, setProperty) {
    126     sp<VehicleNetwork> vn = getDefaultVN();
    127     sp<VehiclePropertiesHolder> properties = vn->listProperties();
    128     ASSERT_TRUE(properties.get() != NULL);
    129     int32_t numConfigs = properties->getList().size();
    130     ASSERT_TRUE(numConfigs > 0);
    131     for (auto& config : properties->getList()) {
    132         if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
    133             continue;
    134         }
    135         String8 msg = String8::format("setting prop 0x%x\n", config->prop);
    136         std::cout<<msg.string();
    137         ScopedVehiclePropValue value;
    138         value.value.prop = config->prop;
    139         value.value.value_type = config->value_type;
    140         status_t r = vn->setProperty(value.value);
    141         if ((config->access & VEHICLE_PROP_ACCESS_WRITE) == 0) { // cannot write
    142             ASSERT_TRUE(r != NO_ERROR);
    143         } else {
    144             ASSERT_EQ(NO_ERROR, r);
    145         }
    146     }
    147 }
    148 
    149 TEST_F(VehicleNetworkTest, setSubscribe) {
    150     sp<VehicleNetwork> vn = getDefaultVN();
    151     sp<VehiclePropertiesHolder> properties = vn->listProperties();
    152     ASSERT_TRUE(properties.get() != NULL);
    153     int32_t numConfigs = properties->getList().size();
    154     ASSERT_TRUE(numConfigs > 0);
    155     for (auto& config : properties->getList()) {
    156         String8 msg = String8::format("subscribing property 0x%x\n", config->prop);
    157         std::cout<<msg.string();
    158         status_t r = vn->subscribe(config->prop, config->max_sample_rate);
    159         if (((config->access & VEHICLE_PROP_ACCESS_READ) == 0) ||
    160                 (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC)) { // cannot subsctibe
    161             ASSERT_TRUE(r != NO_ERROR);
    162         } else {
    163             if ((config->prop >= (int32_t)VEHICLE_PROPERTY_INTERNAL_START) &&
    164                     (config->prop <= (int32_t)VEHICLE_PROPERTY_INTERNAL_END)) {
    165                 // internal property requires write for event notification.
    166                 ScopedVehiclePropValue value;
    167                 value.value.prop = config->prop;
    168                 value.value.value_type = config->value_type;
    169                 status_t r = vn->setProperty(value.value);
    170                 ASSERT_EQ(NO_ERROR, r);
    171             }
    172             ASSERT_EQ(NO_ERROR, r);
    173             ASSERT_TRUE(getTestListener().waitForEvent(config->prop, 0, 2000000000));
    174         }
    175     }
    176     for (auto& config : properties->getList()) {
    177         vn->unsubscribe(config->prop);
    178     }
    179     usleep(1000000);
    180     //TODO improve this as this will wait for too long
    181     for (auto& config : properties->getList()) {
    182         int initialCount = getTestListener().getEventCount(config->prop);
    183         ASSERT_TRUE(!getTestListener().waitForEvent(config->prop, initialCount, 1000000000));
    184     }
    185 }
    186 
    187 }; // namespace android
    188