blob: 2d13e26f3bee51d70fb90a2deaca3f89e5aeac84 [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -07001/*
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
Keun-young Park28dd4702015-11-19 18:06:04 -080028#include "IVehicleNetworkTestListener.h"
29
keunyounge18e25d2015-08-28 15:57:19 -070030namespace android {
31
32class IVehicleNetworkTest : public testing::Test {
33public:
34 IVehicleNetworkTest() {}
35
36 ~IVehicleNetworkTest() {}
37
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
49 sp<IVehicleNetwork> getDefaultVN() {
50 return mDefaultVN;
51 }
52protected:
53 virtual void SetUp() {
54 ProcessState::self()->startThreadPool();
55 mDefaultVN = connectToService();
56 ASSERT_TRUE(mDefaultVN.get() != NULL);
57 }
58private:
59 sp<IVehicleNetwork> mDefaultVN;
60};
61
keunyounge18e25d2015-08-28 15:57:19 -070062TEST_F(IVehicleNetworkTest, connect) {
63 sp<IVehicleNetwork> vn = connectToService();
64 ASSERT_TRUE(vn.get() != NULL);
65}
66
67TEST_F(IVehicleNetworkTest, listProperties) {
68 sp<IVehicleNetwork> vn = getDefaultVN();
69 sp<VehiclePropertiesHolder> properties = vn->listProperties();
70 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -070071 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -070072 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -070073 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -070074 String8 msg = String8::format("prop 0x%x\n", config->prop);
75 std::cout<<msg.string();
keunyounge18e25d2015-08-28 15:57:19 -070076 }
77 sp<VehiclePropertiesHolder> propertiesIvalid = vn->listProperties(-1); // no such property
78 ASSERT_TRUE(propertiesIvalid.get() == NULL);
keunyoungd32f4e62015-09-21 11:33:06 -070079 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -070080 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);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080083 ASSERT_EQ((size_t) 1, singleProperty->getList().size());
keunyoungd32f4e62015-09-21 11:33:06 -070084 vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin();
keunyounge18e25d2015-08-28 15:57:19 -070085 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
keunyounge18e25d2015-08-28 15:57:19 -070089 }
90}
91
92TEST_F(IVehicleNetworkTest, getProperty) {
93 sp<IVehicleNetwork> vn = getDefaultVN();
94 sp<VehiclePropertiesHolder> properties = vn->listProperties();
95 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -070096 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -070097 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -070098 for (auto& config : properties->getList()) {
Keun-young Park28dd4702015-11-19 18:06:04 -080099 if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
100 continue;
101 }
keunyounge18e25d2015-08-28 15:57:19 -0700102 String8 msg = String8::format("getting prop 0x%x\n", config->prop);
103 std::cout<<msg.string();
keunyoungd32f4e62015-09-21 11:33:06 -0700104 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700113 ScopedVehiclePropValue value;
114 value.value.prop = config->prop;
keunyoung7d74e6d2015-10-14 15:43:10 -0700115 value.value.value_type = config->value_type;
keunyounge18e25d2015-08-28 15:57:19 -0700116 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700123 }
124}
125
126//TODO change this test to to safe write
127TEST_F(IVehicleNetworkTest, setProperty) {
128 sp<IVehicleNetwork> vn = getDefaultVN();
129 sp<VehiclePropertiesHolder> properties = vn->listProperties();
130 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -0700131 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -0700132 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -0700133 for (auto& config : properties->getList()) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800134 if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
135 continue;
136 }
keunyounge18e25d2015-08-28 15:57:19 -0700137 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700148 }
149}
150
151TEST_F(IVehicleNetworkTest, setSubscribe) {
152 sp<IVehicleNetwork> vn = getDefaultVN();
153 sp<VehiclePropertiesHolder> properties = vn->listProperties();
154 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -0700155 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -0700156 ASSERT_TRUE(numConfigs > 0);
Keun-young Park28dd4702015-11-19 18:06:04 -0800157 sp<IVehicleNetworkTestListener> listener(new IVehicleNetworkTestListener());
keunyoungd32f4e62015-09-21 11:33:06 -0700158 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -0700159 String8 msg = String8::format("subscribing property 0x%x\n", config->prop);
160 std::cout<<msg.string();
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700161 status_t r = vn->subscribe(listener, config->prop, config->max_sample_rate,
162 0, /* zones */
163 SubscribeFlags::HAL_EVENT);
keunyounge18e25d2015-08-28 15:57:19 -0700164 if (((config->access & VEHICLE_PROP_ACCESS_READ) == 0) ||
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700165 (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC)) { // cannot subscribe
keunyounge18e25d2015-08-28 15:57:19 -0700166 ASSERT_TRUE(r != NO_ERROR);
167 } else {
keunyoungd32f4e62015-09-21 11:33:06 -0700168 if ((config->prop >= (int32_t)VEHICLE_PROPERTY_INTERNAL_START) &&
169 (config->prop <= (int32_t)VEHICLE_PROPERTY_INTERNAL_END)) {
170 // internal property requires write for event notification.
171 ScopedVehiclePropValue value;
172 value.value.prop = config->prop;
173 value.value.value_type = config->value_type;
174 status_t r = vn->setProperty(value.value);
175 ASSERT_EQ(NO_ERROR, r);
176 }
keunyounge18e25d2015-08-28 15:57:19 -0700177 ASSERT_EQ(NO_ERROR, r);
178 ASSERT_TRUE(listener->waitForEvent(config->prop, 2000000000));
179 }
keunyounge18e25d2015-08-28 15:57:19 -0700180 }
keunyoungd32f4e62015-09-21 11:33:06 -0700181 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -0700182 vn->unsubscribe(listener, config->prop);
keunyounge18e25d2015-08-28 15:57:19 -0700183 }
184 usleep(1000000);
keunyounge18e25d2015-08-28 15:57:19 -0700185 //TODO improve this as this will wait for too long
keunyoungd32f4e62015-09-21 11:33:06 -0700186 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -0700187 ASSERT_TRUE(!listener->waitForEvent(config->prop, 1000000000));
keunyounge18e25d2015-08-28 15:57:19 -0700188 }
189}
190
191}; // namespace android