1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "rs_graphic_test.h"
17 #include "rs_graphic_test_director.h"
18 #include "rs_parameter_parse.h"
19 #include "rs_graphic_test_ext.h"
20 
21 #include <iostream>
22 #include <string>
23 
24 using namespace OHOS;
25 using namespace OHOS::Rosen;
26 using namespace std;
27 
28 constexpr size_t ARGS_ONE = 1;
29 constexpr size_t ARGS_TWO = 2;
30 constexpr size_t ARGS_THREE = 3;
31 constexpr size_t ARGS_FOUR = 4;
32 
33 typedef int (*ProcFunc)(int argc, char **argv);
34 typedef struct {
35     string oid;
36     ProcFunc procFunc;
37 } GraphicTestCommandTb;
38 
SplitString(const string & str,vector<string> & vec,const string & pattern)39 static void SplitString(const string& str, vector<string>& vec, const string& pattern)
40 {
41     string::size_type pos1 = 0;
42     string::size_type pos2 = str.find(pattern);
43     while (pos2 != string::npos) {
44         vec.push_back(str.substr(pos1, pos2 - pos1));
45         pos1 = pos2 + pattern.size();
46         pos2 = str.find(pattern, pos1);
47     }
48 
49     if (pos1 != str.length()) {
50         vec.push_back(str.substr(pos1));
51     }
52 }
53 
DisplayCaseLayer(vector<string> & curlayerInfo,vector<string> & layerInfo)54 static void DisplayCaseLayer(vector<string>& curlayerInfo, vector<string>& layerInfo)
55 {
56     for (uint32_t loop = 0; loop < curlayerInfo.size(); loop++) {
57         if (loop >= layerInfo.size()) {
58             layerInfo.push_back(curlayerInfo[loop]);
59         } else if (curlayerInfo[loop] == layerInfo[loop]) {
60             continue;
61         } else {
62             layerInfo[loop] = curlayerInfo[loop];
63         }
64 
65         string out {};
66         for (uint32_t idx = 0; idx < loop; idx++) {
67             out.append("|  ");
68         }
69         out.append("|--").append(curlayerInfo[loop]);
70         cout << out << endl;
71     }
72 }
73 
DisplayAllCaseInfo(int argc,char ** argv)74 static int DisplayAllCaseInfo(int argc, char **argv)
75 {
76     vector<const TestDefInfo*> info = ::OHOS::Rosen::TestDefManager::Instance().GetAllTestInfos();
77     vector<string> layerInfo {};
78     vector<string> curlayerInfo {};
79     string findPath = "graphic_test";
80     if (argc == ARGS_THREE) {
81         findPath = string(argv[ARGS_TWO]);
82     }
83     cout << findPath << endl;
84     findPath.append("/");
85 
86     for (uint32_t loop = 0; loop < info.size(); loop++) {
87         string filePath = info[loop]->filePath;
88         if (filePath.find(findPath) == string::npos) {
89             continue;
90         }
91 
92         size_t startPos = filePath.find(findPath) + findPath.length();
93         if (filePath.rfind("/") > startPos) {
94             string subPath = filePath.substr(startPos, filePath.rfind("/") - startPos);
95             SplitString(subPath, curlayerInfo, "/");
96         }
97 
98         curlayerInfo.push_back(info[loop]->testCaseName);
99         DisplayCaseLayer(curlayerInfo, layerInfo);
100 
101         string out {};
102         for (uint32_t idx = 0; idx < curlayerInfo.size() - 1; idx++) {
103             out.append("|  ");
104         }
105 
106         out.append("   |--").append(info[loop]->testName);
107         cout << out << endl;
108         curlayerInfo.clear();
109     }
110     return 0;
111 }
112 
FilterTestUnit(int argc,char ** argv)113 static int FilterTestUnit(int argc, char **argv)
114 {
115     string unitName;
116     string caseName = "*";
117     switch (argc) {
118         case ARGS_THREE:
119             unitName = argv[ARGS_TWO];
120             break;
121         case ARGS_FOUR:
122             unitName = argv[ARGS_TWO];
123             caseName = argv[ARGS_THREE];
124             break;
125         default:
126             cout << "format fail [-unit unitName [caseName]]" << endl;
127             return 0;
128     }
129 
130     int argcTemp = ARGS_TWO;
131     string filter = "--gtest_filter=";
132     filter.append(unitName).append(".").append(caseName);
133     argv[ARGS_ONE] = filter.data();
134     RSGraphicTestDirector::Instance().Run();
135     testing::InitGoogleTest(&argcTemp, argv);
136     return RUN_ALL_TESTS();
137 }
138 
main(int argc,char ** argv)139 int main(int argc, char **argv)
140 {
141     GraphicTestCommandTb funcTbl[] = {
142         { "-list", DisplayAllCaseInfo },
143         { "-unit", FilterTestUnit }
144     };
145 
146     if (argc >= ARGS_TWO) {
147         size_t tblCnt = sizeof(funcTbl) / sizeof(funcTbl[0]);
148         for (uint32_t paraNo = 0; paraNo < tblCnt; paraNo++) {
149             if (funcTbl[paraNo].oid == string(argv[ARGS_ONE])) {
150                 return funcTbl[paraNo].procFunc(argc, argv);
151             }
152         }
153     }
154 
155     RSGraphicTestDirector::Instance().Run();
156     testing::GTEST_FLAG(output) = "xml:./";
157     testing::InitGoogleTest(&argc, argv);
158     return RUN_ALL_TESTS();
159 }
160