blob: 6df76e369e77a33684063620eee729ba57a1e8bc [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 <VehicleNetwork.h>
27
Keun-young Parkaaeffaf2015-11-25 17:24:10 -080028#include "VehicleNetworkTestListener.h"
29
keunyounge18e25d2015-08-28 15:57:19 -070030namespace 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.
keunyounge18e25d2015-08-28 15:57:19 -070034class VehicleNetworkTest : public testing::Test {
35public:
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
55protected:
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
68protected:
69 sp<VehicleNetwork> mVN;
70 sp<VehicleNetworkTestListener> mListener;
71};
72
73
74TEST_F(VehicleNetworkTest, listProperties) {
75 sp<VehicleNetwork> vn = getDefaultVN();
76 sp<VehiclePropertiesHolder> properties = vn->listProperties();
77 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -070078 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -070079 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -070080 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -070081 String8 msg = String8::format("prop 0x%x\n", config->prop);
82 std::cout<<msg.string();
keunyounge18e25d2015-08-28 15:57:19 -070083 }
84 sp<VehiclePropertiesHolder> propertiesIvalid = vn->listProperties(-1); // no such property
85 ASSERT_TRUE(propertiesIvalid.get() == NULL);
keunyoungd32f4e62015-09-21 11:33:06 -070086 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -070087 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 Park4aeb4bf2015-12-08 18:31:33 -080090 ASSERT_EQ((size_t) 1, singleProperty->getList().size());
keunyoungd32f4e62015-09-21 11:33:06 -070091 vehicle_prop_config_t const * newConfig = *singleProperty->getList().begin();
keunyounge18e25d2015-08-28 15:57:19 -070092 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
keunyounge18e25d2015-08-28 15:57:19 -070096 }
97}
98
99TEST_F(VehicleNetworkTest, getProperty) {
100 sp<VehicleNetwork> vn = getDefaultVN();
101 sp<VehiclePropertiesHolder> properties = vn->listProperties();
102 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -0700103 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -0700104 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -0700105 for (auto& config : properties->getList()) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800106 if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
107 continue;
108 }
keunyounge18e25d2015-08-28 15:57:19 -0700109 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;
keunyoung7d74e6d2015-10-14 15:43:10 -0700113 value.value.value_type = config->value_type;
keunyounge18e25d2015-08-28 15:57:19 -0700114 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700121 }
122}
123
124//TODO change this test to to safe write
125TEST_F(VehicleNetworkTest, setProperty) {
126 sp<VehicleNetwork> vn = getDefaultVN();
127 sp<VehiclePropertiesHolder> properties = vn->listProperties();
128 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -0700129 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -0700130 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -0700131 for (auto& config : properties->getList()) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800132 if (config->prop == VEHICLE_PROPERTY_RADIO_PRESET) {
133 continue;
134 }
keunyounge18e25d2015-08-28 15:57:19 -0700135 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700146 }
147}
148
149TEST_F(VehicleNetworkTest, setSubscribe) {
150 sp<VehicleNetwork> vn = getDefaultVN();
151 sp<VehiclePropertiesHolder> properties = vn->listProperties();
152 ASSERT_TRUE(properties.get() != NULL);
keunyoungd32f4e62015-09-21 11:33:06 -0700153 int32_t numConfigs = properties->getList().size();
keunyounge18e25d2015-08-28 15:57:19 -0700154 ASSERT_TRUE(numConfigs > 0);
keunyoungd32f4e62015-09-21 11:33:06 -0700155 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -0700156 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 {
keunyoungd32f4e62015-09-21 11:33:06 -0700163 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 }
keunyounge18e25d2015-08-28 15:57:19 -0700172 ASSERT_EQ(NO_ERROR, r);
Keun-young Parkaaeffaf2015-11-25 17:24:10 -0800173 ASSERT_TRUE(getTestListener().waitForEvent(config->prop, 0, 2000000000));
keunyounge18e25d2015-08-28 15:57:19 -0700174 }
keunyounge18e25d2015-08-28 15:57:19 -0700175 }
keunyoungd32f4e62015-09-21 11:33:06 -0700176 for (auto& config : properties->getList()) {
keunyounge18e25d2015-08-28 15:57:19 -0700177 vn->unsubscribe(config->prop);
keunyounge18e25d2015-08-28 15:57:19 -0700178 }
179 usleep(1000000);
keunyounge18e25d2015-08-28 15:57:19 -0700180 //TODO improve this as this will wait for too long
keunyoungd32f4e62015-09-21 11:33:06 -0700181 for (auto& config : properties->getList()) {
Keun-young Parkaaeffaf2015-11-25 17:24:10 -0800182 int initialCount = getTestListener().getEventCount(config->prop);
183 ASSERT_TRUE(!getTestListener().waitForEvent(config->prop, initialCount, 1000000000));
keunyounge18e25d2015-08-28 15:57:19 -0700184 }
185}
186
187}; // namespace android