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