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 #ifndef OHOS_ACE_COMPONENT_TEST_RESULT_RECORDER_H 17 #define OHOS_ACE_COMPONENT_TEST_RESULT_RECORDER_H 18 19 #include <string> 20 #include <vector> 21 22 #include "core/common/task_runner_adapter.h" 23 24 namespace OHOS::Ace::ComponentTest { 25 enum Result : int { 26 PASS = 0, 27 FAIL, 28 ERROR, 29 }; 30 31 const std::string TEST_CASE_RESULTS[] = { 32 "Pass", 33 "Fail", 34 "Error", 35 }; 36 37 enum TestCaseType : int { 38 FUNCTION = 0, 39 PERFORMANCE, 40 POWER, 41 RELIABILITY, 42 SECURITY, 43 GLOBAL, 44 COMPATIBILITY, 45 USER, 46 STANDARD, 47 SAFETY, 48 RESILIENCE, 49 INVALID_TYPE, 50 }; 51 52 const std::string TEST_CASE_TYPE_STRINGS[] = { 53 "FUNCTION", 54 "PERFORMANCE", 55 "POWER", 56 "RELIABILITY", 57 "SECURITY", 58 "GLOBAL", 59 "COMPATIBILITY", 60 "USER", 61 "STANDARD", 62 "SAFETY", 63 "RESILIENCE", 64 }; 65 66 enum TestCaseSize : int { 67 SMALLTEST = 0, 68 MEDIUMTEST, 69 LARGETEST, 70 INVALID_SIZE, 71 }; 72 73 const std::string TEST_CASE_SIZE_STRINGS[] = { 74 "SMALLTEST", 75 "MEDIUMTEST", 76 "LARGETEST", 77 }; 78 79 enum TestCaseLevel : int { 80 LEVEL0 = 0, 81 LEVEL1, 82 LEVEL2, 83 LEVEL3, 84 LEVEL4, 85 INVALID_LEVEL, 86 }; 87 88 const std::string TEST_CASE_LEVEL_STRINGS[] = { 89 "LEVEL0", 90 "LEVEL1", 91 "LEVEL2", 92 "LEVEL3", 93 "LEVEL4", 94 }; 95 96 class TestCaseAttribute { 97 public: 98 TestCaseAttribute() = default; TestCaseAttribute(std::string testCaseName,TestCaseType testCaseType,TestCaseSize testCaseSize,TestCaseLevel testCaseLevel)99 TestCaseAttribute( 100 std::string testCaseName, TestCaseType testCaseType, TestCaseSize testCaseSize, TestCaseLevel testCaseLevel) 101 : testCaseName_(testCaseName), testCaseType_(testCaseType), testCaseSize_(testCaseSize), 102 testCaseLevel_(testCaseLevel) 103 {} 104 GetTestCaseName()105 std::string GetTestCaseName() const 106 { 107 return testCaseName_; 108 } 109 GetTestCaseTypeString()110 std::string GetTestCaseTypeString() const 111 { 112 return TEST_CASE_TYPE_STRINGS[testCaseType_]; 113 } 114 GetTestCaseSizeString()115 std::string GetTestCaseSizeString() const 116 { 117 return TEST_CASE_SIZE_STRINGS[testCaseSize_]; 118 } 119 GetTestCaseLevelString()120 std::string GetTestCaseLevelString() const 121 { 122 return TEST_CASE_LEVEL_STRINGS[testCaseLevel_]; 123 } 124 125 private: 126 std::string testCaseName_; 127 TestCaseType testCaseType_; 128 TestCaseSize testCaseSize_; 129 TestCaseLevel testCaseLevel_; 130 }; 131 132 class AssertResult { 133 public: 134 std::string position; 135 Result result; 136 std::string info; GetTestCaseResultString()137 std::string GetTestCaseResultString() const 138 { 139 return TEST_CASE_RESULTS[result]; 140 } 141 }; 142 143 class TestRecordParam { 144 public: 145 std::string info; 146 AssertResult assertResult; 147 }; 148 149 class TestRecord { 150 public: 151 std::string info; 152 Result result; 153 std::string position; 154 std::vector<AssertResult> assertResults; 155 }; 156 157 class TestResultRecorder { 158 public: 159 void Initialize(const std::string& out); 160 void Record(const std::string& info, const std::string& position, Result result); 161 void Finish(); SetTestCaseAttribute(TestCaseAttribute attribute)162 void SetTestCaseAttribute(TestCaseAttribute attribute) 163 { 164 attr_ = attribute; 165 WriteJson(); 166 } 167 168 private: 169 TestCaseAttribute attr_; 170 TestRecord testRecord_; 171 std::string outPath_; 172 void WriteJson(); 173 }; 174 } // namespace OHOS::Ace::ComponentTest 175 176 #endif // OHOS_ACE_COMPONENT_TEST_RESULT_RECORDER_H 177