sl@0: // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\include\e32test.h sl@0: // sl@0: // sl@0: sl@0: /** sl@0: @file e32test.h sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: sl@0: #ifndef __E32TEST_H__ sl@0: #define __E32TEST_H__ sl@0: #include sl@0: #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS sl@0: #include sl@0: #include sl@0: #endif sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: sl@0: /** sl@0: Test console. sl@0: sl@0: The class creates a console window to which test results can be logged sl@0: through the various overloads of the operator(). sl@0: */ sl@0: class RTest sl@0: { sl@0: public: sl@0: IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway,const TText* anOtherThrowaway); sl@0: IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway); sl@0: IMPORT_C RTest(const TDesC &aTitle); sl@0: IMPORT_C void Close(); sl@0: IMPORT_C void Title(); sl@0: IMPORT_C void Start(const TDesC &aHeading); sl@0: IMPORT_C void Next(const TDesC &aHeading); sl@0: IMPORT_C void End(); sl@0: IMPORT_C void operator()(TInt aResult,TInt aLineNum,const TText* aFileName); sl@0: IMPORT_C void operator()(TInt aResult,TInt aLineNum); sl@0: IMPORT_C void operator()(TInt aResult); sl@0: IMPORT_C void Panic(TInt anError,TRefByValue aFmt,...); sl@0: IMPORT_C void Panic(TRefByValue aFmt,...); sl@0: IMPORT_C void Printf(TRefByValue aFmt,...); sl@0: IMPORT_C TKeyCode Getch(); sl@0: inline static const TAny* String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2); sl@0: inline CConsoleBase* Console() const; sl@0: inline void SetConsole(CConsoleBase* aConsole); sl@0: inline TBool Logged() const; sl@0: inline void SetLogged(TBool aToLog); sl@0: inline void HandleError(TInt aError, TInt aLine, const TText* aFileName); sl@0: inline void HandleNull(TInt aLine, const TText* aFileName); sl@0: inline void HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName); sl@0: inline void HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName); sl@0: inline void HandleValue(TInt aValue, TInt aLine, const TText* aFileName); sl@0: sl@0: IMPORT_C static TInt CloseHandleAndWaitForDestruction(RHandleBase& aH); /**< @internalTechnology */ sl@0: sl@0: protected: sl@0: void CheckConsoleCreated(); sl@0: void DisplayLevel(); sl@0: inline void Push(); sl@0: inline void Pop(); sl@0: private: sl@0: enum {EMaxStack=0x100,EMaxBuffer=0x100}; sl@0: private: sl@0: TInt iTest; sl@0: TInt iCheck; sl@0: TInt iLevel; sl@0: TBool iLogging; sl@0: CConsoleBase *iConsole; sl@0: TBuf<0x40> iTitle; sl@0: TInt iStack[EMaxStack]; sl@0: TText iBuf[EMaxBuffer]; sl@0: }; sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Gets the console. sl@0: sl@0: @return A pointer to the console object. sl@0: */ sl@0: inline CConsoleBase* RTest::Console() const sl@0: { return(iConsole); } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Utility function that returns a pointer to the specified TText8* argument sl@0: or the TText16* argument depending on the value of the aSel argument. sl@0: sl@0: @param aSel An integer containing the size of a TText8 type or TText16 type. sl@0: @param aBuf1 A pointer to 8-bit text. sl@0: @param aBuf2 A pointer to 16-bit text. sl@0: sl@0: @return A pointer to aBuf1, if the value of aSel is the size of a TText8 type, sl@0: otherwise a pointer to aBuf2. sl@0: */ sl@0: inline const TAny *RTest::String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2) sl@0: { return(aSel == sizeof(TText8) ? (TAny *)aBuf1 : (TAny *)aBuf2); } sl@0: sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline void RTest::Push() sl@0: { iStack[iLevel++] = iTest; iTest = 0; } sl@0: sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline void RTest::Pop() sl@0: { iTest = iStack[--iLevel]; } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Sets the console. sl@0: sl@0: @param aConsole A pointer to the console object to be used. sl@0: */ sl@0: inline void RTest::SetConsole(CConsoleBase* aConsole) sl@0: { iConsole = aConsole; } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Tests whether the logging flag is set. sl@0: sl@0: If the logging flag is set, console output is also written to sl@0: the debug output as represented by a RDebug object. sl@0: sl@0: @return True, if the logging flag is set, false otherwise. sl@0: */ sl@0: inline TBool RTest::Logged() const sl@0: { return(iLogging); } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Sets the logging flag. sl@0: sl@0: If the logging flag is set, console output is also written to sl@0: the debug output as represented by a RDebug object. sl@0: sl@0: @param aToLog ETrue, if the logging flag is to be set, EFalse, otherwise. sl@0: */ sl@0: inline void RTest::SetLogged(TBool aToLog) sl@0: { iLogging = aToLog; } sl@0: sl@0: sl@0: sl@0: sl@0: // test equivalent of _L sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: #define _TL(a) (S*)RTest::String(sizeof(S),(TText8*)a,(TText16*)L ## a) sl@0: sl@0: // the next two, slightly confusing, macros are necessary in order sl@0: // to enable proper string merging with certain compilers. sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: #define __test(x,l,f) test(x,l,_S(f)) sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: #define test(x) __test(x,__LINE__,__FILE__) sl@0: sl@0: sl@0: #ifdef __E32TEST_EXTENSION__ sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: #define __S(f) _S(f) sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if x is less then zero (Indicating an error code). sl@0: */ sl@0: #define test_NotNegative(x) { TInt _r = (x); if (_r < 0) test.HandleError(_r, __LINE__,__S(__FILE__)); } sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if x is not equal to KErrNone. sl@0: */ sl@0: #define test_KErrNone(x) { TInt _r = (x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); } sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if the trapped statement/block x leaves. sl@0: */ sl@0: #define test_TRAP(x) { TRAPD(_r, x); if (_r != KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); } sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if x is not equal to NULL. sl@0: */ sl@0: #define test_NotNull(x) { TAny* _a = (TAny*)(x); if (_a == NULL) test.HandleNull(__LINE__,__S(__FILE__)); } sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if e (expected) is not equal to a (actual). sl@0: */ sl@0: #define test_Equal(e, a) { TInt _e = TInt(e); TInt _a = TInt(a); if (_e != _a) test.HandleNotEqual(_e, _a, __LINE__,__S(__FILE__)); } sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message if the comparison specified with operator b, between a and c, is EFalse. sl@0: */ sl@0: #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__)); } sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Panics and displays an appropriate error message displaying v, if the expression e is false. sl@0: */ sl@0: #define test_Value(v, e) if (!(e)) test.HandleValue(v, __LINE__,__S(__FILE__)); sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: If expression e is false, statement s is executed then a Panic is raised. sl@0: */ sl@0: #define test_Assert(e,s) if(!(e)) {s; test.operator()(EFalse, __LINE__,__S(__FILE__)); } sl@0: sl@0: sl@0: sl@0: #endif sl@0: sl@0: sl@0: /** sl@0: Prints a failure message, including an error code at the console and raises a panic. sl@0: sl@0: sl@0: @param aError The error code to be printed in the failure massage. sl@0: @param aLineNum A line number that is printed in the failure message. sl@0: @param aFileName A file name that is printed in the failure message. sl@0: sl@0: @panic USER 84 Always. sl@0: */ sl@0: inline void RTest::HandleError(TInt aError, TInt aLine, const TText* aFileName) sl@0: { sl@0: RDebug::Printf("RTEST: Error %d at line %d", aError,aLine); sl@0: Printf(_L("RTEST: Error %d\n"), aError); sl@0: operator()(EFalse, aLine, aFileName); sl@0: } sl@0: /** sl@0: Prints a failure message indicating null was encountered, at the console and raises a panic. sl@0: sl@0: @param aLineNum A line number that is printed in the failure message. sl@0: @param aFileName A file name that is printed in the failure message. sl@0: sl@0: @panic USER 84 Always. sl@0: */ sl@0: sl@0: inline void RTest::HandleNull(TInt aLine, const TText* aFileName) sl@0: { sl@0: RDebug::Printf("RTEST: Null value at line %d", aLine); sl@0: Printf(_L("RTEST: Null value\n")); sl@0: operator()(EFalse, aLine, aFileName); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Prints a failure message indicating that two value (also printed) where not equal, at the console and raises a panic. sl@0: sl@0: @param aExpected The value that is to be printed as expected. sl@0: @param aActual The value that is to be printed as being actually received. sl@0: @param aLineNum A line number that is printed in the failure message. sl@0: @param aFileName A file name that is printed in the failure message. sl@0: sl@0: @panic USER 84 Always. sl@0: */ sl@0: sl@0: inline void RTest::HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName) sl@0: { sl@0: RDebug::Printf("RTEST: Expected 0x%x (%d) but got 0x%x (%d) at line %d", aExpected,aExpected,aActual,aActual,aLine); sl@0: Printf(_L("RTEST: Expected 0x%x (%d) but got 0x%x (%d)\n"), aExpected,aExpected,aActual,aActual); sl@0: operator()(EFalse, aLine, aFileName); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Prints a failure message indicating that a comparison between two values (also printed) resulted in EFalse, sl@0: at the console and raises a panic. sl@0: sl@0: @param aLeft The left value of the comparison. sl@0: @param aComp A string representing the comparison operator. sl@0: @param aRight The right value of the comparison. sl@0: @param aLineNum A line number that is printed in the failure message. sl@0: @param aFileName A file name that is printed in the failure message. sl@0: sl@0: @panic USER 84 Always. sl@0: */ sl@0: inline void RTest::HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName) sl@0: { sl@0: RDebug::Printf("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse at line %d", aLeft,aLeft,aComp,aRight,aRight,aLine); sl@0: Printf(_L("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse\n"), aLeft,aLeft,aComp, aRight,aRight); sl@0: operator()(EFalse, aLine, aFileName); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Prints a failure message indicating that aValue was not an expected value, at the console and raises a panic. sl@0: sl@0: @param aValue The value that is to be printed as not being an expected value. sl@0: @param aLineNum A line number that is printed in the failure message. sl@0: @param aFileName A file name that is printed in the failure message. sl@0: sl@0: @panic USER 84 Always. sl@0: */ sl@0: inline void RTest::HandleValue(TInt aValue, TInt aLine, const TText* aFileName) sl@0: { sl@0: Printf(_L("RTEST: %d (0x%x) was not an expected value.\n"), aValue, aValue); sl@0: operator()(EFalse, aLine, aFileName); sl@0: } sl@0: sl@0: sl@0: /** sl@0: @internalTechnology sl@0: */ sl@0: _LIT(KLitCloseAndWait,"Close&Wait"); sl@0: sl@0: /** sl@0: @internalTechnology sl@0: */ sl@0: #define CLOSE_AND_WAIT(h) ((void)(RTest::CloseHandleAndWaitForDestruction(h) && (User::Panic(KLitCloseAndWait,__LINE__),1))) sl@0: sl@0: sl@0: sl@0: #endif sl@0: