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 <VehicleNetwork.h> |
| 27 | |
Keun-young Park | aaeffaf | 2015-11-25 17:24:10 -0800 | [diff] [blame] | 28 | #include "VehicleNetworkTestListener.h" |
| 29 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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. |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 78 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 79 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 80 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 81 | String8 msg = String8::format("prop 0x%x\n", config->prop); |
| 82 | std::cout<<msg.string(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 83 | } |
| 84 | sp<VehiclePropertiesHolder> propertiesIvalid = vn->listProperties(-1); // no such property |
| 85 | ASSERT_TRUE(propertiesIvalid.get() == NULL); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 86 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 90 | ASSERT_EQ((size_t) 1, singleProperty->getList().size()); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 91 | vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 103 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 104 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 105 | for (auto& config : properties->getList()) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 106 | if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) { |
| 107 | continue; |
| 108 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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; |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 113 | value.value.value_type = config->value_type; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 129 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 131 | for (auto& config : properties->getList()) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 132 | if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) { |
| 133 | continue; |
| 134 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 153 | int32_t numConfigs = properties->getList().size(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 154 | ASSERT_TRUE(numConfigs > 0); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 155 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 172 | ASSERT_EQ(NO_ERROR, r); |
Keun-young Park | aaeffaf | 2015-11-25 17:24:10 -0800 | [diff] [blame] | 173 | ASSERT_TRUE(getTestListener().waitForEvent(config->prop, 0, 2000000000)); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 174 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 175 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 176 | for (auto& config : properties->getList()) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 177 | vn->unsubscribe(config->prop); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 178 | } |
| 179 | usleep(1000000); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 180 | //TODO improve this as this will wait for too long |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 181 | for (auto& config : properties->getList()) { |
Keun-young Park | aaeffaf | 2015-11-25 17:24:10 -0800 | [diff] [blame] | 182 | int initialCount = getTestListener().getEventCount(config->prop); |
| 183 | ASSERT_TRUE(!getTestListener().waitForEvent(config->prop, initialCount, 1000000000)); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | }; // namespace android |