• 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_ext.h"
17 
18 using namespace std;
19 namespace OHOS {
20 namespace Rosen {
21 namespace {
GetTestName(const char * testCaseName,const char * testName)22 string GetTestName(const char* testCaseName, const char* testName)
23 {
24     return string(testCaseName) + "_" + testName;
25 }
26 }
27 
Instance()28 TestDefManager& TestDefManager::Instance()
29 {
30     static TestDefManager instance;
31     return instance;
32 }
33 
Regist(const char * testCaseName,const char * testName,RSGraphicTestType type,RSGraphicTestMode mode,const char * filePath,const bool isMultiple)34 bool TestDefManager::Regist(const char* testCaseName, const char* testName,
35     RSGraphicTestType type, RSGraphicTestMode mode, const char* filePath, const bool isMultiple)
36 {
37     const auto& name = GetTestName(testCaseName, testName);
38     if (testInfos_.find(name) != testInfos_.end()) {
39         LOGE("TestDefManager::Regist duplicate name %{public}s", name.c_str());
40         return false;
41     }
42 
43     string startPath = "graphic_test/";
44     size_t startPos = string(filePath).find(startPath) + startPath.length();
45     if (startPos == string::npos) {
46         LOGE("TestDefManager::Regist fail filePath %{public}s", filePath);
47         return false;
48     }
49     string strName = string(testCaseName);
50     string savePath = string(filePath).substr(startPos);
51     testInfos_.emplace(name,
52         TestDefInfo {testCaseName, testName, type, mode, savePath, isMultiple, GetTestCaseCnt(strName)});
53 
54     if (testCaseInfos_.find(strName) != testCaseInfos_.end()) {
55         testCaseInfos_[strName]++;
56     } else {
57         testCaseInfos_.emplace(strName, 1);
58     }
59     return true;
60 }
61 
GetTestInfo(const char * testCaseName,const char * testName) const62 const TestDefInfo* TestDefManager::GetTestInfo(const char* testCaseName, const char* testName) const
63 {
64     const auto& it = testInfos_.find(GetTestName(testCaseName, testName));
65     if (it != testInfos_.end()) {
66         return &it->second;
67     }
68 
69     return nullptr;
70 }
71 
GetTestInfosByType(RSGraphicTestType type) const72 vector<const TestDefInfo*> TestDefManager::GetTestInfosByType(RSGraphicTestType type) const
73 {
74     vector<const TestDefInfo*> infos;
75     for (const auto& it: testInfos_) {
76         if (it.second.testType == type) {
77             infos.push_back(&it.second);
78         }
79     }
80 
81     return infos;
82 }
83 
Cmp(const TestDefInfo * info1,const TestDefInfo * info2)84 static inline bool Cmp(const TestDefInfo* info1, const TestDefInfo* info2)
85 {
86     string s1 = info1->filePath.substr(0, info1->filePath.rfind("/") + 1) + info1->testCaseName + info1->testName;
87     string s2 = info2->filePath.substr(0, info2->filePath.rfind("/") + 1) + info2->testCaseName + info2->testName;
88     return s1 < s2;
89 }
90 
GetAllTestInfos() const91 vector<const TestDefInfo*> TestDefManager::GetAllTestInfos() const
92 {
93     vector<const TestDefInfo*> infos;
94     for (const auto& it: testInfos_) {
95         infos.push_back(&it.second);
96     }
97 
98     sort(infos.begin(), infos.end(), Cmp);
99     return infos;
100 }
101 
GetTestCaseCnt(string testCaseName) const102 int TestDefManager::GetTestCaseCnt(string testCaseName) const
103 {
104     int cnt = 0;
105     for (const auto& it: testInfos_) {
106         if ((it.second.testCaseName == testCaseName) && it.second.isMultiple) {
107             cnt++;
108         }
109     }
110     return cnt;
111 }
112 
113 } // namespace Rosen
114 } // namespace OHOS