keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 28 | #include "IVehicleNetworkTestListener.h" |
| 29 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | class IVehicleNetworkTest : public testing::Test { |
| 33 | public: |
| 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 | } |
| 52 | protected: |
| 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 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 62 | TEST_F(IVehicleNetworkTest, connect) { |
| 63 | sp<IVehicleNetwork> vn = connectToService(); |
| 64 | ASSERT_TRUE(vn.get() != NULL); |
| 65 | } |
| 66 | |
| 67 | TEST_F(IVehicleNetworkTest, listProperties) { |
| 68 | sp<IVehicleNetwork> vn = getDefaultVN(); |
| 69 | sp<VehiclePropertiesHolder> properties = vn->listProperties(); |
| 70 | ASSERT_TRUE(properties.get() != NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 71 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 73 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 74 | String8 msg = String8::format("prop 0x%x\n", config->prop); |
| 75 | std::cout<<msg.string(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 76 | } |
| 77 | sp<VehiclePropertiesHolder> propertiesIvalid = vn->listProperties(-1); // no such property |
| 78 | ASSERT_TRUE(propertiesIvalid.get() == NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 79 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 83 | ASSERT_EQ((size_t) 1, singleProperty->getList().size()); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 84 | vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | TEST_F(IVehicleNetworkTest, getProperty) { |
| 93 | sp<IVehicleNetwork> vn = getDefaultVN(); |
| 94 | sp<VehiclePropertiesHolder> properties = vn->listProperties(); |
| 95 | ASSERT_TRUE(properties.get() != NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 96 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 98 | for (auto& config : properties->getList()) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 99 | if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) { |
| 100 | continue; |
| 101 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 102 | String8 msg = String8::format("getting prop 0x%x\n", config->prop); |
| 103 | std::cout<<msg.string(); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 113 | ScopedVehiclePropValue value; |
| 114 | value.value.prop = config->prop; |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 115 | value.value.value_type = config->value_type; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | //TODO change this test to to safe write |
| 127 | TEST_F(IVehicleNetworkTest, setProperty) { |
| 128 | sp<IVehicleNetwork> vn = getDefaultVN(); |
| 129 | sp<VehiclePropertiesHolder> properties = vn->listProperties(); |
| 130 | ASSERT_TRUE(properties.get() != NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 131 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 132 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 133 | for (auto& config : properties->getList()) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 134 | if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) { |
| 135 | continue; |
| 136 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | TEST_F(IVehicleNetworkTest, setSubscribe) { |
| 152 | sp<IVehicleNetwork> vn = getDefaultVN(); |
| 153 | sp<VehiclePropertiesHolder> properties = vn->listProperties(); |
| 154 | ASSERT_TRUE(properties.get() != NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 155 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 156 | ASSERT_TRUE(numConfigs > 0); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 157 | sp<IVehicleNetworkTestListener> listener(new IVehicleNetworkTestListener()); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 158 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 159 | String8 msg = String8::format("subscribing property 0x%x\n", config->prop); |
| 160 | std::cout<<msg.string(); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 161 | status_t r = vn->subscribe(listener, config->prop, config->max_sample_rate, |
| 162 | 0, /* zones */ |
| 163 | SubscribeFlags::HAL_EVENT); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 164 | if (((config->access & VEHICLE_PROP_ACCESS_READ) == 0) || |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 165 | (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC)) { // cannot subscribe |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 166 | ASSERT_TRUE(r != NO_ERROR); |
| 167 | } else { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 168 | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 177 | ASSERT_EQ(NO_ERROR, r); |
| 178 | ASSERT_TRUE(listener->waitForEvent(config->prop, 2000000000)); |
| 179 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 180 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 181 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 182 | vn->unsubscribe(listener, config->prop); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 183 | } |
| 184 | usleep(1000000); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 185 | //TODO improve this as this will wait for too long |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 186 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 187 | ASSERT_TRUE(!listener->waitForEvent(config->prop, 1000000000)); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | }; // namespace android |