• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <IVehicleNetwork.h>
27 
28 #include "IVehicleNetworkTestListener.h"
29 
30 namespace android {
31 
32 class IVehicleNetworkTest : public testing::Test {
33 public:
IVehicleNetworkTest()34     IVehicleNetworkTest() {}
35 
~IVehicleNetworkTest()36     ~IVehicleNetworkTest() {}
37 
connectToService()38     sp<IVehicleNetwork> connectToService() {
39         sp<IBinder> binder = defaultServiceManager()->getService(
40                 String16(IVehicleNetwork::SERVICE_NAME));
41         if (binder != NULL) {
42             sp<IVehicleNetwork> vn(interface_cast<IVehicleNetwork>(binder));
43             return vn;
44         }
45         sp<IVehicleNetwork> dummy;
46         return dummy;
47     }
48 
getDefaultVN()49     sp<IVehicleNetwork> getDefaultVN() {
50         return mDefaultVN;
51     }
52 protected:
SetUp()53     virtual void SetUp() {
54         ProcessState::self()->startThreadPool();
55         mDefaultVN = connectToService();
56         ASSERT_TRUE(mDefaultVN.get() != NULL);
57     }
58 private:
59     sp<IVehicleNetwork> mDefaultVN;
60 };
61 
TEST_F(IVehicleNetworkTest,connect)62 TEST_F(IVehicleNetworkTest, connect) {
63     sp<IVehicleNetwork> vn = connectToService();
64     ASSERT_TRUE(vn.get() != NULL);
65 }
66 
TEST_F(IVehicleNetworkTest,listProperties)67 TEST_F(IVehicleNetworkTest, listProperties) {
68     sp<IVehicleNetwork> vn = getDefaultVN();
69     sp<VehiclePropertiesHolder> properties = vn->listProperties();
70     ASSERT_TRUE(properties.get() != NULL);
71     int32_t numConfigs = properties->getList().size();
72     ASSERT_TRUE(numConfigs > 0);
73     for (auto& config : properties->getList()) {
74         String8 msg = String8::format("prop 0x%x\n", config->prop);
75         std::cout<<msg.string();
76     }
77     sp<VehiclePropertiesHolder> propertiesIvalid  = vn->listProperties(-1); // no such property
78     ASSERT_TRUE(propertiesIvalid.get() == NULL);
79     for (auto& config : properties->getList()) {
80         String8 msg = String8::format("query single prop 0x%x\n", config->prop);
81         std::cout<<msg.string();
82         sp<VehiclePropertiesHolder> singleProperty = vn->listProperties(config->prop);
83         ASSERT_EQ((size_t) 1, singleProperty->getList().size());
84         vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin();
85         ASSERT_EQ(config->prop, newConfig->prop);
86         ASSERT_EQ(config->access, newConfig->access);
87         ASSERT_EQ(config->change_mode, newConfig->change_mode);
88         //TODO add more check
89     }
90 }
91 
TEST_F(IVehicleNetworkTest,getProperty)92 TEST_F(IVehicleNetworkTest, getProperty) {
93     sp<IVehicleNetwork> vn = getDefaultVN();
94     sp<VehiclePropertiesHolder> properties = vn->listProperties();
95     ASSERT_TRUE(properties.get() != NULL);
96     int32_t numConfigs = properties->getList().size();
97     ASSERT_TRUE(numConfigs > 0);
98     for (auto& config : properties->getList()) {
99         if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
100             continue;
101         }
102         String8 msg = String8::format("getting prop 0x%x\n", config->prop);
103         std::cout<<msg.string();
104         if ((config->prop >= (int32_t)VEHICLE_PROPERTY_INTERNAL_START) &&
105                 (config->prop <= (int32_t)VEHICLE_PROPERTY_INTERNAL_END)) {
106             // internal property requires write to get anything.
107             ScopedVehiclePropValue value;
108             value.value.prop = config->prop;
109             value.value.value_type = config->value_type;
110             status_t r = vn->setProperty(value.value);
111             ASSERT_EQ(NO_ERROR, r);
112         }
113         ScopedVehiclePropValue value;
114         value.value.prop = config->prop;
115         value.value.value_type = config->value_type;
116         status_t r = vn->getProperty(&value.value);
117         if ((config->access & VEHICLE_PROP_ACCESS_READ) == 0) { // cannot read
118             ASSERT_TRUE(r != NO_ERROR);
119         } else {
120             ASSERT_EQ(NO_ERROR, r);
121             ASSERT_EQ(config->value_type, value.value.value_type);
122         }
123     }
124 }
125 
126 //TODO change this test to to safe write
TEST_F(IVehicleNetworkTest,setProperty)127 TEST_F(IVehicleNetworkTest, setProperty) {
128     sp<IVehicleNetwork> vn = getDefaultVN();
129     sp<VehiclePropertiesHolder> properties = vn->listProperties();
130     ASSERT_TRUE(properties.get() != NULL);
131     int32_t numConfigs = properties->getList().size();
132     ASSERT_TRUE(numConfigs > 0);
133     for (auto& config : properties->getList()) {
134         if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
135             continue;
136         }
137         String8 msg = String8::format("setting prop 0x%x\n", config->prop);
138         std::cout<<msg.string();
139         ScopedVehiclePropValue value;
140         value.value.prop = config->prop;
141         value.value.value_type = config->value_type;
142         status_t r = vn->setProperty(value.value);
143         if ((config->access & VEHICLE_PROP_ACCESS_WRITE) == 0) { // cannot write
144             ASSERT_TRUE(r != NO_ERROR);
145         } else {
146             ASSERT_EQ(NO_ERROR, r);
147         }
148     }
149 }
150 
TEST_F(IVehicleNetworkTest,setSubscribe)151 TEST_F(IVehicleNetworkTest, setSubscribe) {
152     sp<IVehicleNetwork> vn = getDefaultVN();
153     sp<VehiclePropertiesHolder> properties = vn->listProperties();
154     ASSERT_TRUE(properties.get() != NULL);
155     int32_t numConfigs = properties->getList().size();
156     ASSERT_TRUE(numConfigs > 0);
157     sp<IVehicleNetworkTestListener> listener(new IVehicleNetworkTestListener());
158     for (auto& config : properties->getList()) {
159         String8 msg = String8::format("subscribing property 0x%x\n", config->prop);
160         std::cout<<msg.string();
161         status_t r = vn->subscribe(listener, config->prop, config->max_sample_rate, 0);
162         if (((config->access & VEHICLE_PROP_ACCESS_READ) == 0) ||
163                 (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC)) { // cannot subsctibe
164             ASSERT_TRUE(r != NO_ERROR);
165         } else {
166             if ((config->prop >= (int32_t)VEHICLE_PROPERTY_INTERNAL_START) &&
167                     (config->prop <= (int32_t)VEHICLE_PROPERTY_INTERNAL_END)) {
168                 // internal property requires write for event notification.
169                 ScopedVehiclePropValue value;
170                 value.value.prop = config->prop;
171                 value.value.value_type = config->value_type;
172                 status_t r = vn->setProperty(value.value);
173                 ASSERT_EQ(NO_ERROR, r);
174             }
175             ASSERT_EQ(NO_ERROR, r);
176             ASSERT_TRUE(listener->waitForEvent(config->prop, 2000000000));
177         }
178     }
179     for (auto& config : properties->getList()) {
180         vn->unsubscribe(listener, config->prop);
181     }
182     usleep(1000000);
183     //TODO improve this as this will wait for too long
184     for (auto& config : properties->getList()) {
185         ASSERT_TRUE(!listener->waitForEvent(config->prop, 1000000000));
186     }
187 }
188 
189 }; // namespace android
190