1 /*
2  * Copyright (C) 2019 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 
19 #include <utils/SystemClock.h>
20 #include <vhal_v2_0/ProtoMessageConverter.h>
21 
22 #include "vhal_v2_0/DefaultConfig.h"
23 #include "vhal_v2_0/VehicleUtils.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace vehicle {
29 namespace V2_0 {
30 namespace impl {
31 namespace proto_msg_converter {
32 
33 namespace {
34 
CheckPropConfigConversion(const VehiclePropConfig & config)35 void CheckPropConfigConversion(const VehiclePropConfig& config) {
36     vhal_proto::VehiclePropConfig protoCfg;
37     VehiclePropConfig tmpConfig;
38 
39     toProto(&protoCfg, config);
40     fromProto(&tmpConfig, protoCfg);
41 
42     EXPECT_EQ(config.prop, tmpConfig.prop);
43     EXPECT_EQ(config.access, tmpConfig.access);
44     EXPECT_EQ(config.changeMode, tmpConfig.changeMode);
45     EXPECT_EQ(config.configString, tmpConfig.configString);
46     EXPECT_EQ(config.minSampleRate, tmpConfig.minSampleRate);
47     EXPECT_EQ(config.maxSampleRate, tmpConfig.maxSampleRate);
48     EXPECT_EQ(config.configArray, tmpConfig.configArray);
49 
50     EXPECT_EQ(config.areaConfigs.size(), tmpConfig.areaConfigs.size());
51 
52     auto cfgType = getPropType(config.prop);
53     for (size_t idx = 0; idx < std::min(config.areaConfigs.size(), tmpConfig.areaConfigs.size());
54          ++idx) {
55         auto& lhs = config.areaConfigs[idx];
56         auto& rhs = tmpConfig.areaConfigs[idx];
57         EXPECT_EQ(lhs.areaId, rhs.areaId);
58         switch (cfgType) {
59             case VehiclePropertyType::INT64:
60                 EXPECT_EQ(lhs.minInt64Value, rhs.minInt64Value);
61                 EXPECT_EQ(lhs.maxInt64Value, rhs.maxInt64Value);
62                 break;
63             case VehiclePropertyType::FLOAT:
64                 EXPECT_EQ(lhs.minFloatValue, rhs.minFloatValue);
65                 EXPECT_EQ(lhs.maxFloatValue, rhs.maxFloatValue);
66                 break;
67             case VehiclePropertyType::INT32:
68                 EXPECT_EQ(lhs.minInt32Value, rhs.minInt32Value);
69                 EXPECT_EQ(lhs.maxInt32Value, rhs.maxInt32Value);
70                 break;
71             default:
72                 // ignore min/max values
73                 break;
74         }
75     }
76 }
77 
CheckPropValueConversion(const VehiclePropValue & val)78 void CheckPropValueConversion(const VehiclePropValue& val) {
79     vhal_proto::VehiclePropValue protoVal;
80     VehiclePropValue tmpVal;
81 
82     toProto(&protoVal, val);
83     fromProto(&tmpVal, protoVal);
84 
85     EXPECT_EQ(val, tmpVal);
86 }
87 
TEST(ProtoMessageConverterTest,basic)88 TEST(ProtoMessageConverterTest, basic) {
89     for (auto& property : impl::kVehicleProperties) {
90         CheckPropConfigConversion(property.config);
91 
92         VehiclePropValue prop;
93         prop.timestamp = elapsedRealtimeNano();
94         prop.areaId = 123;
95         prop.prop = property.config.prop;
96         prop.value = property.initialValue;
97         prop.status = VehiclePropertyStatus::ERROR;
98         CheckPropValueConversion(prop);
99     }
100 }
101 
102 }  // namespace
103 
104 }  // namespace proto_msg_converter
105 }  // namespace impl
106 }  // namespace V2_0
107 }  // namespace vehicle
108 }  // namespace automotive
109 }  // namespace hardware
110 }  // namespace android
111