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 
getTestNodeTypeClass(TestNodeType type)54 inline TestNodeClass getTestNodeTypeClass (TestNodeType type)
55 {
56 	switch (type)
57 	{
58 		case NODETYPE_ROOT:				return NODECLASS_GROUP;
59 		case NODETYPE_PACKAGE:			return NODECLASS_GROUP;
60 		case NODETYPE_GROUP:			return NODECLASS_GROUP;
61 		case NODETYPE_SELF_VALIDATE:	return NODECLASS_EXECUTABLE;
62 		case NODETYPE_PERFORMANCE:		return NODECLASS_EXECUTABLE;
63 		case NODETYPE_CAPABILITY:		return NODECLASS_EXECUTABLE;
64 		case NODETYPE_ACCURACY:			return NODECLASS_EXECUTABLE;
65 		default:
66 			DE_ASSERT(false);
67 			return NODECLASS_LAST;
68 	}
69 }
70 
isTestNodeTypeExecutable(TestNodeType type)71 inline bool isTestNodeTypeExecutable (TestNodeType type)
72 {
73 	return getTestNodeTypeClass(type) == NODECLASS_EXECUTABLE;
74 }
75 
isValidTestCaseNameChar(char c)76 inline bool isValidTestCaseNameChar (char c)
77 {
78 	return de::inRange(c, 'a', 'z') ||
79 		   de::inRange(c, 'A', 'Z') ||
80 		   de::inRange(c, '0', '9') ||
81 		   c == '_' || c == '-';
82 }
83 
84 /*--------------------------------------------------------------------*//*!
85  * \brief Test case hierarchy node
86  *
87  * Test node forms the backbone of the test case hierarchy. All objects
88  * in the hierarchy are derived from this class.
89  *
90  * Each test node has a type and all except the root node have name and
91  * description. Root and test group nodes have a list of children.
92  *
93  * During test execution TestExecutor iterates the hierarchy. Upon entering
94  * the node (both groups and test cases) init() is called. When exiting the
95  * node deinit() is called respectively.
96  *//*--------------------------------------------------------------------*/
97 class TestNode
98 {
99 public:
100 	enum IterateResult
101 	{
102 		STOP		= 0,
103 		CONTINUE	= 1
104 	};
105 
106 	// Methods.
107 							TestNode		(TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description);
108 							TestNode		(TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description, const std::vector<TestNode*>& children);
109 	virtual					~TestNode		(void);
110 
getNodeType(void) const111 	TestNodeType			getNodeType		(void) const	{ return m_nodeType;			}
getTestContext(void) const112 	TestContext&			getTestContext	(void) const	{ return m_testCtx;				}
getName(void) const113 	const char*				getName			(void) const	{ return m_name.c_str();		}
getDescription(void) const114 	const char*				getDescription	(void) const	{ return m_description.c_str(); }
115 	void					getChildren		(std::vector<TestNode*>& children);
116 	void					addChild		(TestNode* node);
117 
118 	virtual void			init			(void);
119 	virtual void			deinit			(void);
120 	virtual IterateResult	iterate			(void) = 0;
121 
122 protected:
123 	TestContext&			m_testCtx;
124 	std::string				m_name;
125 	std::string				m_description;
126 
127 private:
128 	const TestNodeType		m_nodeType;
129 	std::vector<TestNode*>	m_children;
130 };
131 
132 /*--------------------------------------------------------------------*//*!
133  * \brief Test case group node
134  *
135  * Test case group implementations must inherit this class. To save resources
136  * during test execution the group must delay creation of any child groups
137  * until init() is called.
138  *
139  * Default deinit() for test group will destroy all child nodes.
140  *//*--------------------------------------------------------------------*/
141 class TestCaseGroup : public TestNode
142 {
143 public:
144 							TestCaseGroup	(TestContext& testCtx, const char* name, const char* description);
145 							TestCaseGroup	(TestContext& testCtx, const char* name, const char* description, const std::vector<TestNode*>& children);
146 	virtual					~TestCaseGroup	(void);
147 
148 	virtual IterateResult	iterate			(void);
149 };
150 
151 /*--------------------------------------------------------------------*//*!
152  * \brief Test case class
153  *
154  * Test case implementations must inherit this class.
155  *
156  * Test case objects are usually constructed when TestExecutor enters parent
157  * group. Allocating any non-parameter resources, especially target API objects
158  * must be delayed to init().
159  *
160  * Upon entering the test case TestExecutor calls init(). If initialization
161  * is successful (no exception is thrown) the executor will then call iterate()
162  * until test case returns STOP. After that deinit() will be called.
163  *
164  * Before exiting the execution phase (i.e. at returning STOP from iterate())
165  * the test case must set valid status code to test context (m_testCtx).
166  *
167  * Test case can also signal error condition by throwing an exception. In
168  * that case the framework will set result code and details based on the
169  * exception.
170  *//*--------------------------------------------------------------------*/
171 class TestCase : public TestNode
172 {
173 public:
174 					TestCase			(TestContext& testCtx, const char* name, const char* description);
175 					TestCase			(TestContext& testCtx, TestNodeType nodeType, const char* name, const char* description);
176 	virtual			~TestCase			(void);
177 };
178 
179 class TestStatus
180 {
181 public:
TestStatus(qpTestResult code,const std::string & description)182 						TestStatus		(qpTestResult code, const std::string& description) : m_code(code), m_description(description) {}
183 
isComplete(void) const184 	bool				isComplete		(void) const { return m_code != QP_TEST_RESULT_LAST;			}
getCode(void) const185 	qpTestResult		getCode			(void) const { DE_ASSERT(isComplete()); return m_code;			}
getDescription(void) const186 	const std::string&	getDescription	(void) const { DE_ASSERT(isComplete()); return m_description;	}
187 
pass(const std::string & description)188 	static TestStatus	pass			(const std::string& description)	{ return TestStatus(QP_TEST_RESULT_PASS,	description);	}
fail(const std::string & description)189 	static TestStatus	fail			(const std::string& description)	{ return TestStatus(QP_TEST_RESULT_FAIL,	description);	}
incomplete(void)190 	static TestStatus	incomplete		(void)								{ return TestStatus(QP_TEST_RESULT_LAST,	"");			}
191 
192 private:
193 	qpTestResult		m_code;
194 	std::string			m_description;
195 } DE_WARN_UNUSED_TYPE;
196 
197 } // tcu
198 
199 #endif // _TCUTESTCASE_HPP
200