|
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32\include\e32test.h |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file e32test.h |
|
20 @publishedAll |
|
21 @released |
|
22 */ |
|
23 |
|
24 #ifndef __E32TEST_H__ |
|
25 #define __E32TEST_H__ |
|
26 #include <e32std.h> |
|
27 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
28 #include <e32std_private.h> |
|
29 #include <e32base_private.h> |
|
30 #endif |
|
31 #include <e32base.h> |
|
32 #include <e32cons.h> |
|
33 #include <e32ver.h> |
|
34 #include <e32kpan.h> |
|
35 #include <cpudefs.h> |
|
36 #include <e32debug.h> |
|
37 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
38 #include <e32def_private.h> |
|
39 #include <e32event_private.h> |
|
40 #endif |
|
41 |
|
42 |
|
43 /** |
|
44 Test console. |
|
45 |
|
46 The class creates a console window to which test results can be logged |
|
47 through the various overloads of the operator(). |
|
48 */ |
|
49 class RTest |
|
50 { |
|
51 public: |
|
52 IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway,const TText* anOtherThrowaway); |
|
53 IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway); |
|
54 IMPORT_C RTest(const TDesC &aTitle); |
|
55 IMPORT_C void Close(); |
|
56 IMPORT_C void Title(); |
|
57 IMPORT_C void Start(const TDesC &aHeading); |
|
58 IMPORT_C void Next(const TDesC &aHeading); |
|
59 IMPORT_C void End(); |
|
60 IMPORT_C void operator()(TInt aResult,TInt aLineNum,const TText* aFileName); |
|
61 IMPORT_C void operator()(TInt aResult,TInt aLineNum); |
|
62 IMPORT_C void operator()(TInt aResult); |
|
63 IMPORT_C void Panic(TInt anError,TRefByValue<const TDesC> aFmt,...); |
|
64 IMPORT_C void Panic(TRefByValue<const TDesC> aFmt,...); |
|
65 IMPORT_C void Printf(TRefByValue<const TDesC> aFmt,...); |
|
66 IMPORT_C TKeyCode Getch(); |
|
67 inline static const TAny* String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2); |
|
68 inline CConsoleBase* Console() const; |
|
69 inline void SetConsole(CConsoleBase* aConsole); |
|
70 inline TBool Logged() const; |
|
71 inline void SetLogged(TBool aToLog); |
|
72 inline void HandleError(TInt aError, TInt aLine, const TText* aFileName); |
|
73 inline void HandleNull(TInt aLine, const TText* aFileName); |
|
74 inline void HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName); |
|
75 inline void HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName); |
|
76 inline void HandleValue(TInt aValue, TInt aLine, const TText* aFileName); |
|
77 |
|
78 IMPORT_C static TInt CloseHandleAndWaitForDestruction(RHandleBase& aH); /**< @internalTechnology */ |
|
79 |
|
80 protected: |
|
81 void CheckConsoleCreated(); |
|
82 void DisplayLevel(); |
|
83 inline void Push(); |
|
84 inline void Pop(); |
|
85 private: |
|
86 enum {EMaxStack=0x100,EMaxBuffer=0x100}; |
|
87 private: |
|
88 TInt iTest; |
|
89 TInt iCheck; |
|
90 TInt iLevel; |
|
91 TBool iLogging; |
|
92 CConsoleBase *iConsole; |
|
93 TBuf<0x40> iTitle; |
|
94 TInt iStack[EMaxStack]; |
|
95 TText iBuf[EMaxBuffer]; |
|
96 }; |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 /** |
|
102 Gets the console. |
|
103 |
|
104 @return A pointer to the console object. |
|
105 */ |
|
106 inline CConsoleBase* RTest::Console() const |
|
107 { return(iConsole); } |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 /** |
|
113 Utility function that returns a pointer to the specified TText8* argument |
|
114 or the TText16* argument depending on the value of the aSel argument. |
|
115 |
|
116 @param aSel An integer containing the size of a TText8 type or TText16 type. |
|
117 @param aBuf1 A pointer to 8-bit text. |
|
118 @param aBuf2 A pointer to 16-bit text. |
|
119 |
|
120 @return A pointer to aBuf1, if the value of aSel is the size of a TText8 type, |
|
121 otherwise a pointer to aBuf2. |
|
122 */ |
|
123 inline const TAny *RTest::String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2) |
|
124 { return(aSel == sizeof(TText8) ? (TAny *)aBuf1 : (TAny *)aBuf2); } |
|
125 |
|
126 |
|
127 |
|
128 /** |
|
129 @internalComponent |
|
130 */ |
|
131 inline void RTest::Push() |
|
132 { iStack[iLevel++] = iTest; iTest = 0; } |
|
133 |
|
134 |
|
135 |
|
136 /** |
|
137 @internalComponent |
|
138 */ |
|
139 inline void RTest::Pop() |
|
140 { iTest = iStack[--iLevel]; } |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 /** |
|
146 Sets the console. |
|
147 |
|
148 @param aConsole A pointer to the console object to be used. |
|
149 */ |
|
150 inline void RTest::SetConsole(CConsoleBase* aConsole) |
|
151 { iConsole = aConsole; } |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 /** |
|
157 Tests whether the logging flag is set. |
|
158 |
|
159 If the logging flag is set, console output is also written to |
|
160 the debug output as represented by a RDebug object. |
|
161 |
|
162 @return True, if the logging flag is set, false otherwise. |
|
163 */ |
|
164 inline TBool RTest::Logged() const |
|
165 { return(iLogging); } |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 /** |
|
171 Sets the logging flag. |
|
172 |
|
173 If the logging flag is set, console output is also written to |
|
174 the debug output as represented by a RDebug object. |
|
175 |
|
176 @param aToLog ETrue, if the logging flag is to be set, EFalse, otherwise. |
|
177 */ |
|
178 inline void RTest::SetLogged(TBool aToLog) |
|
179 { iLogging = aToLog; } |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 // test equivalent of _L |
|
185 /** |
|
186 @internalComponent |
|
187 */ |
|
188 #define _TL(a) (S*)RTest::String(sizeof(S),(TText8*)a,(TText16*)L ## a) |
|
189 |
|
190 // the next two, slightly confusing, macros are necessary in order |
|
191 // to enable proper string merging with certain compilers. |
|
192 |
|
193 /** |
|
194 @internalComponent |
|
195 */ |
|
196 #define __test(x,l,f) test(x,l,_S(f)) |
|
197 |
|
198 /** |
|
199 @internalComponent |
|
200 */ |
|
201 #define test(x) __test(x,__LINE__,__FILE__) |
|
202 |
|
203 |
|
204 #ifdef __E32TEST_EXTENSION__ |
|
205 |
|
206 /** |
|
207 @internalComponent |
|
208 */ |
|
209 #define __S(f) _S(f) |
|
210 |
|
211 /** |
|
212 @internalComponent |
|
213 |
|
214 Panics and displays an appropriate error message if x is less then zero (Indicating an error code). |
|
215 */ |
|
216 #define test_NotNegative(x) { TInt _r = (x); if (_r < 0) test.HandleError(_r, __LINE__,__S(__FILE__)); } |
|
217 |
|
218 /** |
|
219 @internalComponent |
|
220 |
|
221 Panics and displays an appropriate error message if x is not equal to KErrNone. |
|
222 */ |
|
223 #define test_KErrNone(x) { TInt _r = (x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); } |
|
224 |
|
225 /** |
|
226 @internalComponent |
|
227 |
|
228 Panics and displays an appropriate error message if the trapped statement/block x leaves. |
|
229 */ |
|
230 #define test_TRAP(x) { TRAPD(_r, x); if (_r != KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); } |
|
231 |
|
232 /** |
|
233 @internalComponent |
|
234 |
|
235 Panics and displays an appropriate error message if x is not equal to NULL. |
|
236 */ |
|
237 #define test_NotNull(x) { TAny* _a = (TAny*)(x); if (_a == NULL) test.HandleNull(__LINE__,__S(__FILE__)); } |
|
238 /** |
|
239 @internalComponent |
|
240 |
|
241 Panics and displays an appropriate error message if e (expected) is not equal to a (actual). |
|
242 */ |
|
243 #define test_Equal(e, a) { TInt _e = TInt(e); TInt _a = TInt(a); if (_e != _a) test.HandleNotEqual(_e, _a, __LINE__,__S(__FILE__)); } |
|
244 |
|
245 /** |
|
246 @internalComponent |
|
247 |
|
248 Panics and displays an appropriate error message if the comparison specified with operator b, between a and c, is EFalse. |
|
249 */ |
|
250 #define test_Compare(a,b,c) {TInt _a = TInt(a); TInt _c = TInt(c); if (!(_a b _c)) test.HandleFailedCompare(_a, __S(#b), _c, __LINE__,__S(__FILE__)); } |
|
251 |
|
252 |
|
253 /** |
|
254 @internalComponent |
|
255 |
|
256 Panics and displays an appropriate error message displaying v, if the expression e is false. |
|
257 */ |
|
258 #define test_Value(v, e) if (!(e)) test.HandleValue(v, __LINE__,__S(__FILE__)); |
|
259 |
|
260 /** |
|
261 @internalComponent |
|
262 |
|
263 If expression e is false, statement s is executed then a Panic is raised. |
|
264 */ |
|
265 #define test_Assert(e,s) if(!(e)) {s; test.operator()(EFalse, __LINE__,__S(__FILE__)); } |
|
266 |
|
267 |
|
268 |
|
269 #endif |
|
270 |
|
271 |
|
272 /** |
|
273 Prints a failure message, including an error code at the console and raises a panic. |
|
274 |
|
275 |
|
276 @param aError The error code to be printed in the failure massage. |
|
277 @param aLineNum A line number that is printed in the failure message. |
|
278 @param aFileName A file name that is printed in the failure message. |
|
279 |
|
280 @panic USER 84 Always. |
|
281 */ |
|
282 inline void RTest::HandleError(TInt aError, TInt aLine, const TText* aFileName) |
|
283 { |
|
284 RDebug::Printf("RTEST: Error %d at line %d", aError,aLine); |
|
285 Printf(_L("RTEST: Error %d\n"), aError); |
|
286 operator()(EFalse, aLine, aFileName); |
|
287 } |
|
288 /** |
|
289 Prints a failure message indicating null was encountered, at the console and raises a panic. |
|
290 |
|
291 @param aLineNum A line number that is printed in the failure message. |
|
292 @param aFileName A file name that is printed in the failure message. |
|
293 |
|
294 @panic USER 84 Always. |
|
295 */ |
|
296 |
|
297 inline void RTest::HandleNull(TInt aLine, const TText* aFileName) |
|
298 { |
|
299 RDebug::Printf("RTEST: Null value at line %d", aLine); |
|
300 Printf(_L("RTEST: Null value\n")); |
|
301 operator()(EFalse, aLine, aFileName); |
|
302 } |
|
303 |
|
304 |
|
305 /** |
|
306 Prints a failure message indicating that two value (also printed) where not equal, at the console and raises a panic. |
|
307 |
|
308 @param aExpected The value that is to be printed as expected. |
|
309 @param aActual The value that is to be printed as being actually received. |
|
310 @param aLineNum A line number that is printed in the failure message. |
|
311 @param aFileName A file name that is printed in the failure message. |
|
312 |
|
313 @panic USER 84 Always. |
|
314 */ |
|
315 |
|
316 inline void RTest::HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName) |
|
317 { |
|
318 RDebug::Printf("RTEST: Expected 0x%x (%d) but got 0x%x (%d) at line %d", aExpected,aExpected,aActual,aActual,aLine); |
|
319 Printf(_L("RTEST: Expected 0x%x (%d) but got 0x%x (%d)\n"), aExpected,aExpected,aActual,aActual); |
|
320 operator()(EFalse, aLine, aFileName); |
|
321 } |
|
322 |
|
323 |
|
324 /** |
|
325 Prints a failure message indicating that a comparison between two values (also printed) resulted in EFalse, |
|
326 at the console and raises a panic. |
|
327 |
|
328 @param aLeft The left value of the comparison. |
|
329 @param aComp A string representing the comparison operator. |
|
330 @param aRight The right value of the comparison. |
|
331 @param aLineNum A line number that is printed in the failure message. |
|
332 @param aFileName A file name that is printed in the failure message. |
|
333 |
|
334 @panic USER 84 Always. |
|
335 */ |
|
336 inline void RTest::HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName) |
|
337 { |
|
338 RDebug::Printf("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse at line %d", aLeft,aLeft,aComp,aRight,aRight,aLine); |
|
339 Printf(_L("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse\n"), aLeft,aLeft,aComp, aRight,aRight); |
|
340 operator()(EFalse, aLine, aFileName); |
|
341 } |
|
342 |
|
343 |
|
344 /** |
|
345 Prints a failure message indicating that aValue was not an expected value, at the console and raises a panic. |
|
346 |
|
347 @param aValue The value that is to be printed as not being an expected value. |
|
348 @param aLineNum A line number that is printed in the failure message. |
|
349 @param aFileName A file name that is printed in the failure message. |
|
350 |
|
351 @panic USER 84 Always. |
|
352 */ |
|
353 inline void RTest::HandleValue(TInt aValue, TInt aLine, const TText* aFileName) |
|
354 { |
|
355 Printf(_L("RTEST: %d (0x%x) was not an expected value.\n"), aValue, aValue); |
|
356 operator()(EFalse, aLine, aFileName); |
|
357 } |
|
358 |
|
359 |
|
360 /** |
|
361 @internalTechnology |
|
362 */ |
|
363 _LIT(KLitCloseAndWait,"Close&Wait"); |
|
364 |
|
365 /** |
|
366 @internalTechnology |
|
367 */ |
|
368 #define CLOSE_AND_WAIT(h) ((void)(RTest::CloseHandleAndWaitForDestruction(h) && (User::Panic(KLitCloseAndWait,__LINE__),1))) |
|
369 |
|
370 |
|
371 |
|
372 #endif |
|
373 |