1 /*
2  * Copyright (C) 2021 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 <JsonConfigLoader.h>
18 #include <VehicleUtils.h>
19 #include <android-base/file.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <fstream>
23 #include <unordered_map>
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace vehicle {
29 
30 namespace test {
31 
32 using ::android::base::Error;
33 using ::android::base::Result;
34 using ::testing::UnorderedElementsAreArray;
35 
36 constexpr char kDefaultPropertiesConfigFile[] = "DefaultProperties.json";
37 
38 #ifdef ENABLE_VEHICLE_HAL_TEST_PROPERTIES
39 constexpr char kTestPropertiesConfigFile[] = "TestProperties.json";
40 constexpr char kVendorClusterTestPropertiesConfigFile[] = "VendorClusterTestProperties.json";
41 #endif  // ENABLE_VEHICLE_HAL_TEST_PROPERTIES
42 
getTestFilePath(const char * filename)43 std::string getTestFilePath(const char* filename) {
44     static std::string baseDir = android::base::GetExecutableDirectory();
45     return baseDir + "/" + filename;
46 }
47 
loadConfig(JsonConfigLoader & loader,const char * path)48 Result<std::unordered_map<int32_t, ConfigDeclaration>> loadConfig(JsonConfigLoader& loader,
49                                                                   const char* path) {
50     std::string configPath = getTestFilePath(path);
51     std::ifstream ifs(configPath.c_str());
52     if (!ifs) {
53         return Error() << "couldn't open %s for parsing." << configPath;
54     }
55 
56     return loader.loadPropConfig(ifs);
57 }
58 
TEST(DefaultConfigTest,TestloadDefaultProperties)59 TEST(DefaultConfigTest, TestloadDefaultProperties) {
60     JsonConfigLoader loader;
61     auto result = loadConfig(loader, kDefaultPropertiesConfigFile);
62 
63     ASSERT_TRUE(result.ok()) << result.error().message();
64 }
65 
66 #ifdef ENABLE_VEHICLE_HAL_TEST_PROPERTIES
67 
TEST(DefaultConfigTest,TestloadTestProperties)68 TEST(DefaultConfigTest, TestloadTestProperties) {
69     JsonConfigLoader loader;
70     auto result = loadConfig(loader, kTestPropertiesConfigFile);
71 
72     ASSERT_TRUE(result.ok()) << result.error().message();
73 }
74 
TEST(DefaultConfigTest,TestloadVendorClusterTestProperties)75 TEST(DefaultConfigTest, TestloadVendorClusterTestProperties) {
76     JsonConfigLoader loader;
77     auto result = loadConfig(loader, kVendorClusterTestPropertiesConfigFile);
78 
79     ASSERT_TRUE(result.ok()) << result.error().message();
80 }
81 
82 #endif  // ENABLE_VEHICLE_HAL_TEST_PROPERTIES
83 
84 }  // namespace test
85 
86 }  // namespace vehicle
87 }  // namespace automotive
88 }  // namespace hardware
89 }  // namespace android
90