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 <gtest/gtest.h>
18 #include "vehicle_test_fixtures.h"
19 #include "hardware/vehicle.h"
20
21 namespace tests {
22
23 // Check if list_properties command exists.
TEST_F(VehicleDevice,isThereListProperties)24 TEST_F(VehicleDevice, isThereListProperties) {
25 ASSERT_TRUE(NULL != vehicle_device()->list_properties)
26 << "list_properties() function is not implemented";
27 std::cout << "Test succeeds.\n";
28 }
29
30 // HAL should provide atleast one property. The output of this command should be
31 // used to verify the vailidity of the function.
TEST_F(VehicleDevice,listPropertiesMoreThanOne)32 TEST_F(VehicleDevice, listPropertiesMoreThanOne) {
33 vehicle_prop_config_t const* config;
34 int num_configs = -1;
35 config = vehicle_device()->list_properties(vehicle_device(), &num_configs);
36 ASSERT_TRUE(num_configs > -1) << "list_properties() call failed.";
37 ASSERT_TRUE(num_configs > 0) << "list_properties() returned zero items.";
38 std::cout << "Number of properties reported: " << num_configs << "\n";
39 for (int i = 0; i < num_configs; i++) {
40 // Print each of the properties.
41 const vehicle_prop_config_t& config_temp = config[i];
42 std::cout << "Property ID: " << config_temp.prop << "\n";
43 std::cout << "Property flags: " << config_temp.config_flags << "\n";
44 std::cout << "Property change mode: " << config_temp.change_mode << "\n";
45 std::cout << "Property min sample rate: " << config_temp.min_sample_rate << "\n";
46 std::cout << "Property max sample rate: " << config_temp.max_sample_rate << "\n\n";
47 }
48 }
49
50 // Test get() command.
51 // The fields are hardcoded in the dummy implementation and here.
TEST_F(VehicleDevice,getDriveState)52 TEST_F(VehicleDevice, getDriveState) {
53 vehicle_prop_value_t data;
54 data.prop = VEHICLE_PROPERTY_DRIVING_STATUS;
55 // Set drive_state field to EINVAL so that we can check that its valid when
56 // it comes back.
57 data.value_type = -EINVAL;
58 data.value.driving_status = -EINVAL;
59 vehicle_device()->get(vehicle_device(), &data);
60
61 // Check that retured values are not invalid.
62 ASSERT_NE(data.value_type, -EINVAL) << "Drive state value type should be integer.";
63 ASSERT_NE(data.value.driving_status, -EINVAL) << "Driving status should be positive.";
64
65 std::cout << "Driving status value type: " << data.value_type << "\n"
66 << "Driving status: " << data.value.driving_status << "\n";
67 }
68
69 // Test the workflows for subscribe and init/release.
70 // Subscribe will return error before init() is called or after release() is
71 // called.
TEST_F(VehicleDevice,initTest)72 TEST_F(VehicleDevice, initTest) {
73 // Test that init on a new device works. When getting an instance, we are
74 // already calling 'open' on the device.
75 int ret_code =
76 vehicle_device()->init(vehicle_device(), callback_fn(), error_fn());
77 ASSERT_EQ(ret_code, 0) << "ret code: " << ret_code;
78
79 // Trying to init again should return an error.
80 ret_code = vehicle_device()->init(vehicle_device(), callback_fn(), error_fn());
81 ASSERT_EQ(ret_code, -EEXIST) << "ret code: " << ret_code;
82
83 // Uninit should always return 0.
84 ret_code = vehicle_device()->release(vehicle_device());
85 ASSERT_EQ(ret_code, 0) << "ret code: " << ret_code;
86
87 // We should be able to init again.
88 ret_code = vehicle_device()->init(vehicle_device(), callback_fn(), error_fn());
89 ASSERT_EQ(ret_code, 0) << "ret code: " << ret_code;
90
91 // Finally release.
92 ret_code = vehicle_device()->release(vehicle_device());
93 ASSERT_EQ(ret_code, 0) << "ret_code: " << ret_code;
94 }
95
96 // Test that subscribe works.
97 // We wait for 10 seconds while which the vehicle.c can post messages from
98 // within it's own thread.
TEST_F(VehicleDevice,subscribeTest)99 TEST_F(VehicleDevice, subscribeTest) {
100 // If the device is not init subscribe should fail off the bat.
101 int ret_code = vehicle_device()->subscribe(vehicle_device(), VEHICLE_PROPERTY_DRIVING_STATUS,
102 0, 0);
103 ASSERT_EQ(ret_code, -EINVAL) << "Return code is: " << ret_code;
104
105 // Let's init the device.
106 ret_code = vehicle_device()->init(vehicle_device(), callback_fn(), error_fn());
107 ASSERT_EQ(ret_code, 0) << "Return code is: " << ret_code;
108
109 // Subscribe should now go through.
110 ret_code = vehicle_device()->subscribe(vehicle_device(), VEHICLE_PROPERTY_DRIVING_STATUS, 0, 0);
111 ASSERT_EQ(ret_code, 0) << "Return code is: " << ret_code;
112
113 // We should start getting some messages thrown from the callback. Let's
114 // wait for 20 seconds before unsubscribing.
115 std::cout << "Sleeping for 20 seconds.";
116 sleep(20);
117 std::cout << "Waking from sleep.";
118
119 // This property does not exist, so we should get -EINVAL.
120 ret_code = vehicle_device()->unsubscribe(vehicle_device(), VEHICLE_PROPERTY_INFO_VIN);
121 ASSERT_EQ(ret_code, -EINVAL) << "Return code is: " << ret_code;
122
123 // This property exists, so we should get a success return code - also this
124 // will be a blocking call.
125 ret_code = vehicle_device()->unsubscribe(vehicle_device(), VEHICLE_PROPERTY_DRIVING_STATUS);
126 ASSERT_EQ(ret_code, 0) << "Return code is: " << ret_code;
127 }
128
129 } // namespace tests
130