1 #ifndef _TCUTESTCASE_HPP
2 #define _TCUTESTCASE_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Base class for a test case.
24 *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "tcuTestContext.hpp"
28
29 #include <string>
30 #include <vector>
31
32 namespace tcu
33 {
34
35 enum TestNodeType
36 {
37 NODETYPE_ROOT = 0, //!< Root for all test packages.
38 NODETYPE_PACKAGE, //!< Test case package -- same as group, but is omitted from XML dump.
39 NODETYPE_GROUP, //!< Test case container -- cannot be executed.
40 NODETYPE_SELF_VALIDATE, //!< Self-validating test case -- can be executed
41 NODETYPE_PERFORMANCE, //!< Performace test case -- can be executed
42 NODETYPE_CAPABILITY, //!< Capability score case -- can be executed
43 NODETYPE_ACCURACY //!< Accuracy test case -- can be executed
44 };
45
46 enum TestNodeClass
47 {
48 NODECLASS_GROUP = 0, //!< Root or non-leaf in the test hierarchy tree
49 NODECLASS_EXECUTABLE, //!< Non-root leaf in the test hierarchy tree
50
51 NODECLASS_LAST
52 };
53
54 enum TestRunnerType
55 {
56 RUNNERTYPE_ANY = 0u,
57 RUNNERTYPE_NONE = (1u << 0),
58 RUNNERTYPE_AMBER = (1u << 1)
59 };
60
getTestNodeTypeClass(TestNodeType type)61 inline TestNodeClass getTestNodeTypeClass (TestNodeType type)
62 {
63 switch (type)
64 {
65 case NODETYPE_ROOT: return NODECLASS_GROUP;
66 case NODETYPE_PACKAGE: return NODECLASS_GROUP;
67 case NODETYPE_GROUP: return NODECLASS_GROUP;
68 case NODETYPE_SELF_VALIDATE: return NODECLASS_EXECUTABLE;
69 case NODETYPE_PERFORMANCE: return NODECLASS_EXECUTABLE;
70 case NODETYPE_CAPABILITY: return NODECLASS_EXECUTABLE;
71 case NODETYPE_ACCURACY: return NODECLASS_EXECUTABLE;
72 default:
73 DE_ASSERT(false);
74 return NODECLASS_LAST;
75 }
76 }
77
isTestNodeTypeExecutable(TestNodeType type)78 inline bool isTestNodeTypeExecutable (TestNodeType type)
79 {
80 return getTestNodeTypeClass(type) == NODECLASS_EXECUTABLE;
81 }
82
isValidTestCaseNameChar(char c)83 inline bool isValidTestCaseNameChar (char c)
84 {
85 return de::inRange(c, 'a', 'z') ||
86 de::inRange(c, 'A', 'Z') ||
87 de::inRange(c, '0', '9') ||
88 c == '_' || c == '-';
89 }
90
91 class TestCaseGroup;
92 class CaseListFilter;
93
94 /*--------------------------------------------------------------------*//*!
95 * \brief Test case hierarchy node
96 *
97 * Test node forms the backbone of the test case hierarchy. All objects
98 * in the hierarchy are derived from this class.
99 *
100 * Each test node has a type and all except the root node have name and
101 * description. Root and test group nodes have a list of children.
102 *
103 * During test execution TestExecutor iterates the hierarchy. Upon entering
104 * the node (both groups and test cases) init() is called. When exiting the
105 * node deinit() is called respectively.
106 *//*--------------------------------------------------------------------*/
107 class TestNode
108 {
109 public:
110 enum IterateResult
111 {
112 STOP = 0,
113 CONTINUE = 1
114 };
115
116 // Methods.
117 TestNode (TestContext& testCtx, TestNodeType nodeType, const char* name);
118 TestNode (TestContext& testCtx, TestNodeType nodeType, const char* name, const std::vector<TestNode*>& children);
119 virtual ~TestNode (void);
120
getNodeType(void) const121 TestNodeType getNodeType (void) const { return m_nodeType; }
getTestContext(void) const122 TestContext& getTestContext (void) const { return m_testCtx; }
getName(void) const123 const char* getName (void) const { return m_name.c_str(); }
124 void getChildren (std::vector<TestNode*>& children) const;
125 void addRootChild (const std::string& groupName, const CaseListFilter* caseListFilter, TestCaseGroup* (*createTestGroup)(tcu::TestContext& testCtx, const std::string& name));
126 void addChild (TestNode* node);
empty() const127 bool empty () const { return m_children.empty(); }
128
129 virtual void init (void);
130 virtual void deinit (void);
131 virtual IterateResult iterate (void) = 0;
getRunnerType(void) const132 virtual TestRunnerType getRunnerType (void) const { return RUNNERTYPE_NONE; }
validateRequirements()133 virtual bool validateRequirements () { return true; }
134 protected:
135 TestContext& m_testCtx;
136 std::string m_name;
137
138 private:
139 const TestNodeType m_nodeType;
140 std::vector<TestNode*> m_children;
141 };
142
143 /*--------------------------------------------------------------------*//*!
144 * \brief Test case group node
145 *
146 * Test case group implementations must inherit this class. To save resources
147 * during test execution the group must delay creation of any child groups
148 * until init() is called.
149 *
150 * Default deinit() for test group will destroy all child nodes.
151 *//*--------------------------------------------------------------------*/
152 class TestCaseGroup : public TestNode
153 {
154 public:
155 TestCaseGroup (TestContext& testCtx, const char* name);
156 TestCaseGroup (TestContext& testCtx, const char* name, const std::vector<TestNode*>& children);
157
158 // Deprecated constructors
159 TestCaseGroup (TestContext& testCtx, const char* name, const char* description);
160 TestCaseGroup (TestContext& testCtx, const char* name, const char* description, const std::vector<TestNode*>& children);
161
162 virtual ~TestCaseGroup (void);
163
164 virtual IterateResult iterate (void);
165 };
166
167 /*--------------------------------------------------------------------*//*!
168 * \brief Test case class
169 *
170 * Test case implementations must inherit this class.
171 *
172 * Test case objects are usually constructed when TestExecutor enters parent
173 * group. Allocating any non-parameter resources, especially target API objects
174 * must be delayed to init().
175 *
176 * Upon entering the test case TestExecutor calls init(). If initialization
177 * is successful (no exception is thrown) the executor will then call iterate()
178 * until test case returns STOP. After that deinit() will be called.
179 *
180 * Before exiting the execution phase (i.e. at returning STOP from iterate())
181 * the test case must set valid status code to test context (m_testCtx).
182 *
183 * Test case can also signal error condition by throwing an exception. In
184 * that case the framework will set result code and details based on the
185 * exception.
186 *//*--------------------------------------------------------------------*/
187 class TestCase : public TestNode
188 {
189 public:
190 TestCase (TestContext& testCtx, const char* name);
191 TestCase (TestContext& testCtx, TestNodeType nodeType, const char* name);
192
193 // Deprecated constructors
194 TestCase (TestContext& testCtx, const char* name, const char* description);
195 TestCase (TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description);
196
197 virtual ~TestCase (void);
198 };
199
200 class TestStatus
201 {
202 public:
TestStatus(qpTestResult code,const std::string & description)203 TestStatus (qpTestResult code, const std::string& description) : m_code(code), m_description(description) {}
204
isComplete(void) const205 bool isComplete (void) const { return m_code != QP_TEST_RESULT_LAST; }
isFail(void) const206 bool isFail (void) const { return m_code == QP_TEST_RESULT_FAIL; }
getCode(void) const207 qpTestResult getCode (void) const { DE_ASSERT(isComplete()); return m_code; }
getDescription(void) const208 const std::string& getDescription (void) const { DE_ASSERT(isComplete()); return m_description; }
209
pass(const std::string & description)210 static TestStatus pass (const std::string& description) { return TestStatus(QP_TEST_RESULT_PASS, description); }
fail(const std::string & description)211 static TestStatus fail (const std::string& description) { return TestStatus(QP_TEST_RESULT_FAIL, description); }
incomplete(void)212 static TestStatus incomplete (void) { return TestStatus(QP_TEST_RESULT_LAST, ""); }
213
214 private:
215 qpTestResult m_code;
216 std::string m_description;
217 } DE_WARN_UNUSED_TYPE;
218
219 } // tcu
220
221 #endif // _TCUTESTCASE_HPP
222