• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_graphic_test_profiler.h"
19 #include "rs_parameter_parse.h"
20 #include "rs_graphic_test_ext.h"
21 
22 #include <iostream>
23 #include <string>
24 
25 using namespace OHOS;
26 using namespace OHOS::Rosen;
27 using namespace std;
28 
29 constexpr size_t ARGS_ONE = 1;
30 constexpr size_t ARGS_TWO = 2;
31 constexpr size_t ARGS_THREE = 3;
32 constexpr size_t ARGS_FOUR = 4;
33 
34 typedef int (*ProcFunc)(int argc, char **argv);
35 typedef struct {
36     string oid;
37     ProcFunc procFunc;
38 } GraphicTestCommandTb;
39 
SplitString(const string & str,vector<string> & vec,const string & pattern)40 static void SplitString(const string& str, vector<string>& vec, const string& pattern)
41 {
42     string::size_type pos1 = 0;
43     string::size_type pos2 = str.find(pattern);
44     while (pos2 != string::npos) {
45         vec.push_back(str.substr(pos1, pos2 - pos1));
46         pos1 = pos2 + pattern.size();
47         pos2 = str.find(pattern, pos1);
48     }
49 
50     if (pos1 != str.length()) {
51         vec.push_back(str.substr(pos1));
52     }
53 }
54 
DisplayCaseLayer(vector<string> & curlayerInfo,vector<string> & layerInfo)55 static void DisplayCaseLayer(vector<string>& curlayerInfo, vector<string>& layerInfo)
56 {
57     for (uint32_t loop = 0; loop < curlayerInfo.size(); loop++) {
58         if (loop >= layerInfo.size()) {
59             layerInfo.push_back(curlayerInfo[loop]);
60         } else if (curlayerInfo[loop] == layerInfo[loop]) {
61             continue;
62         } else {
63             layerInfo[loop] = curlayerInfo[loop];
64         }
65 
66         string out {};
67         for (uint32_t idx = 0; idx < loop; idx++) {
68             out.append("|  ");
69         }
70         out.append("|--").append(curlayerInfo[loop]);
71         cout << out << endl;
72     }
73 }
74 
DisplayAllCaseInfo(int argc,char ** argv)75 static int DisplayAllCaseInfo(int argc, char **argv)
76 {
77     vector<const TestDefInfo*> info = ::OHOS::Rosen::TestDefManager::Instance().GetAllTestInfos();
78     vector<string> layerInfo {};
79     vector<string> curlayerInfo {};
80     string findPath = "graphic_test";
81     if (argc == ARGS_THREE) {
82         findPath = string(argv[ARGS_TWO]);
83     }
84     cout << findPath << endl;
85     findPath.append("/");
86 
87     for (uint32_t loop = 0; loop < info.size(); loop++) {
88         string filePath = info[loop]->filePath;
89         if (filePath.find(findPath) == string::npos) {
90             continue;
91         }
92 
93         size_t startPos = filePath.find(findPath) + findPath.length();
94         if (filePath.rfind("/") > startPos) {
95             string subPath = filePath.substr(startPos, filePath.rfind("/") - startPos);
96             SplitString(subPath, curlayerInfo, "/");
97         }
98 
99         curlayerInfo.push_back(info[loop]->testCaseName);
100         DisplayCaseLayer(curlayerInfo, layerInfo);
101 
102         string out {};
103         for (uint32_t idx = 0; idx < curlayerInfo.size() - 1; idx++) {
104             out.append("|  ");
105         }
106 
107         out.append("   |--").append(info[loop]->testName);
108         cout << out << endl;
109         curlayerInfo.clear();
110     }
111     return 0;
112 }
113 
FilterTestUnit(int argc,char ** argv)114 static int FilterTestUnit(int argc, char **argv)
115 {
116     string unitName;
117     string caseName = "*";
118     switch (argc) {
119         case ARGS_THREE:
120             unitName = argv[ARGS_TWO];
121             break;
122         case ARGS_FOUR:
123             unitName = argv[ARGS_TWO];
124             caseName = argv[ARGS_THREE];
125             RSGraphicTestDirector::Instance().SetSingleTest(true);
126             break;
127         default:
128             cout << "format fail [-unit unitName [caseName]]" << endl;
129             return 0;
130     }
131 
132     int argcTemp = ARGS_TWO;
133     string filter = "--gtest_filter=";
134     filter.append(unitName).append(".").append(caseName);
135     argv[ARGS_ONE] = filter.data();
136     RSGraphicTestDirector::Instance().Run();
137     testing::InitGoogleTest(&argcTemp, argv);
138     return RUN_ALL_TESTS();
139 }
140 
RunAllTest(int argc,char ** argv)141 static int RunAllTest(int argc, char **argv)
142 {
143     int argcTemp = ARGS_ONE;
144     RSGraphicTestDirector::Instance().Run();
145     testing::GTEST_FLAG(output) = "xml:./";
146     testing::InitGoogleTest(&argcTemp, argv);
147     return RUN_ALL_TESTS();
148 }
149 
RunNodeTreeProfilerTest(int argc,char ** argv)150 static int RunNodeTreeProfilerTest(int argc, char **argv)
151 {
152     if (argc != ARGS_THREE && argc != ARGS_FOUR) {
153         cout << "nodetree failed : wrong param number" << endl;
154         return 0;
155     }
156     RSGraphicTestDirector::Instance().SetProfilerTest(true);
157     RSGraphicTestDirector::Instance().Run();
158     std::string path = argv[ARGS_TWO];
159     bool useBufferDump = false;
160     if (argc == ARGS_FOUR) {
161         useBufferDump = (string(argv[ARGS_THREE]) == "-dumpbuffer");
162     }
163     RSGraphicTestProfiler engine;
164     engine.SetUseBufferDump(useBufferDump);
165     return engine.RunNodeTreeTest(path);
166 }
167 
RunPlaybackProfilerTest(int argc,char ** argv)168 static int RunPlaybackProfilerTest(int argc, char **argv)
169 {
170     if (argc != ARGS_THREE && argc != ARGS_FOUR) {
171         cout << "nodetree failed : wrong param number" << endl;
172         return 0;
173     }
174     RSGraphicTestDirector::Instance().SetProfilerTest(true);
175     RSGraphicTestDirector::Instance().Run();
176     std::string path = argv[ARGS_TWO];
177     bool useBufferDump = false;
178     if (argc == ARGS_FOUR) {
179         useBufferDump = (string(argv[ARGS_THREE]) == "-dumpbuffer");
180     }
181     RSGraphicTestProfiler engine;
182     engine.SetUseBufferDump(useBufferDump);
183     return engine.RunPlaybackTest(path);
184 }
185 
main(int argc,char ** argv)186 int main(int argc, char **argv)
187 {
188     GraphicTestCommandTb funcTbl[] = {
189         { "-list", DisplayAllCaseInfo },
190         { "-unit", FilterTestUnit },
191         { "-all", RunAllTest },
192         { "-nodetree", RunNodeTreeProfilerTest },
193         { "-playback", RunPlaybackProfilerTest }
194     };
195 
196     if (argc >= ARGS_TWO) {
197         size_t tblCnt = sizeof(funcTbl) / sizeof(funcTbl[0]);
198         for (uint32_t paraNo = 0; paraNo < tblCnt; paraNo++) {
199             if (funcTbl[paraNo].oid == string(argv[ARGS_ONE])) {
200                 return funcTbl[paraNo].procFunc(argc, argv);
201             }
202         }
203     }
204 
205     if (argc == ARGS_ONE) {
206         RSParameterParse::Instance().SetSkipCapture(true);
207     }
208 
209     RSGraphicTestDirector::Instance().Run();
210     testing::GTEST_FLAG(output) = "xml:./";
211     testing::InitGoogleTest(&argc, argv);
212     return RUN_ALL_TESTS();
213 }
214