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 namespace OHOS {
19 namespace Rosen {
20 namespace {
GetTestName(const char * testCaseName,const char * testName)21 std::string GetTestName(const char* testCaseName, const char* testName)
22 {
23     return std::string(testCaseName) + "_" + testName;
24 }
25 }
26 
Instance()27 TestDefManager& TestDefManager::Instance()
28 {
29     static TestDefManager instance;
30     return instance;
31 }
32 
Regist(const char * testCaseName,const char * testName,RSGraphicTestType type,RSGraphicTestMode mode,const char * filePath)33 bool TestDefManager::Regist(const char* testCaseName, const char* testName,
34     RSGraphicTestType type, RSGraphicTestMode mode, const char* filePath)
35 {
36     const auto& name = GetTestName(testCaseName, testName);
37     if (testInfos_.find(name) != testInfos_.end()) {
38         LOGE("TestDefManager::Regist duplicate name %{public}s", name.c_str());
39         return false;
40     }
41 
42     std::string startPath = "graphic_test/";
43     size_t startPos = std::string(filePath).find(startPath) + startPath.length();
44     if (startPos == std::string::npos) {
45         LOGE("TestDefManager::Regist fail filePath %{public}s", filePath);
46         return false;
47     }
48 
49     std::string savePath = std::string(filePath).substr(startPos);
50     testInfos_.emplace(name, TestDefInfo {testCaseName, testName, type, mode, savePath});
51     return true;
52 }
53 
GetTestInfo(const char * testCaseName,const char * testName) const54 const TestDefInfo* TestDefManager::GetTestInfo(const char* testCaseName, const char* testName) const
55 {
56     const auto& it = testInfos_.find(GetTestName(testCaseName, testName));
57     if (it != testInfos_.end()) {
58         return &it->second;
59     }
60 
61     return nullptr;
62 }
63 
GetTestInfosByType(RSGraphicTestType type) const64 std::vector<const TestDefInfo*> TestDefManager::GetTestInfosByType(RSGraphicTestType type) const
65 {
66     std::vector<const TestDefInfo*> infos;
67     for (const auto& it: testInfos_) {
68         if (it.second.testType == type) {
69             infos.push_back(&it.second);
70         }
71     }
72 
73     return infos;
74 }
75 
Cmp(const TestDefInfo * info1,const TestDefInfo * info2)76 static inline bool Cmp(const TestDefInfo* info1, const TestDefInfo* info2)
77 {
78     std::string s1 = info1->filePath.substr(0, info1->filePath.rfind("/") + 1) + info1->testCaseName + info1->testName;
79     std::string s2 = info2->filePath.substr(0, info2->filePath.rfind("/") + 1) + info2->testCaseName + info2->testName;
80     return s1 < s2;
81 }
82 
GetAllTestInfos() const83 std::vector<const TestDefInfo*> TestDefManager::GetAllTestInfos() const
84 {
85     std::vector<const TestDefInfo*> infos;
86     for (const auto& it: testInfos_) {
87         infos.push_back(&it.second);
88     }
89 
90     sort(infos.begin(), infos.end(), Cmp);
91     return infos;
92 }
93 
94 } // namespace Rosen
95 } // namespace OHOS