First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\us_test.cpp
27 void RTest::CheckConsoleCreated()
29 // Check that the console has been created.
32 TRAPD(r, iConsole = Console::NewL(iTitle, TSize(KConsFullScreen, KConsFullScreen)))
33 __ASSERT_ALWAYS(r == KErrNone, ::Panic(ERTestCreateConsole));
37 void RTest::DisplayLevel()
39 // Display the current level string.
40 TBuf<0x100> aBuf(_L("RTEST: Level "));
41 for (TInt ii = 1; ii < iLevel; ii++)
45 aBuf.AppendFormat(_L(".%02d"), iStack[ii]);
49 aBuf.AppendFormat(_L(" %03d"), iStack[ii]);
54 aBuf.AppendFormat(_L(".%02d "), iTest);
58 aBuf.AppendFormat(_L(" %03d "), iTest);
70 @param aTitle A title describing this use of RTest.
71 This is also referred to as the console title.
72 @param aThrowaway Not used.
73 @param anOtherThrowaway Not used.
75 EXPORT_C RTest::RTest(const TDesC &aTitle,TInt /* aThrowaway */,const TText* /* anOtherThrowaway */)
76 : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
78 // There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
79 // This depends upon the user naming their RTest object test, but if they do this then an extra
80 // parameter aThrowaway must be added to the constructor
88 @param aTitle A title describing this use of RTest.
89 This is also referred to as the console title.
90 @param aThrowaway Not used.
92 EXPORT_C RTest::RTest(const TDesC &aTitle, TInt /* athrowaway */)
93 : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
95 // There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
96 // This depends upon the user naming their RTest object test, but if they do this then an extra
97 // parameter aThrowaway must be added to the constructor
106 @param aTitle A title describing this use of RTest.
107 This is also referred to as the console title.
109 EXPORT_C RTest::RTest(const TDesC &aTitle)
110 : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
118 Closes the console and frees any resources acquired.
120 EXPORT_C void RTest::Close()
122 // Close the console.
131 Prints out the console title and version number.
133 The format of the text is:
140 where XXX is the console title, and YYY is the version number,
141 formatted as described by TVersion::Name().
143 @see TVersion::Name()
146 EXPORT_C void RTest::Title()
148 // Print out the program title and version number.
149 TVersion v(KE32MajorVersionNumber, KE32MinorVersionNumber, KE32BuildVersionNumber);
150 TBuf<16> vName=v.Name();
151 Printf(_L("RTEST TITLE: %S %S\n"), &iTitle, &vName);
152 vName=User::Version().Name();
153 Printf(_L("Epoc/32 %S\n"), &vName);
160 Marks the start of a set of tests.
162 Note that sets of tests can be nested.
164 A call to this function must be matched by a call to RTest::End() to mark
165 the end of this set of tests.
167 @param aHeading A heading describing the set of tests; this is
168 printed at the console.
172 EXPORT_C void RTest::Start(const TDesC &aHeading)
174 // Print out the heading and nest the level.
183 Marks the start of the next test.
185 @param aHeading A heading describing the test; this
186 is printed at the console. This function is also
187 called by Start(), which passes the text that describes
192 EXPORT_C void RTest::Next(const TDesC &aHeading)
194 // Print out the heading and nest the level.
198 Printf(_L("Next test - %S\n"), &aHeading);
205 Checks the result of a condition and, if this is false, prints
206 a failure message at the console and raises a panic.
208 Before checking the condition passed in, the operator increments
209 a check number. This is a value that is set to zero at the start of a test
210 and is incremented by this operator (and by all variants of it). It identifies
211 the check being made within the current test.
212 This value is printed on a failure message.
214 Typically, the operator is called, passing a test condition, for example:
217 RTest test(... heading text...,line number... file name)
219 ...some operation to be tested that returns a value in r...
223 The failure message has the format:
226 : FAIL : XXX failed check N in FFF at line Number: M
227 RTEST: Checkpoint-fail
230 where XXX is the console title, N is the check number, FFF is the filename,
231 and M is the line number passed in.
233 @param aResult The condition being tested.
234 This is interpreted as a true or false value.
235 @param aLineNum A line number that is printed in the failure message if
236 the condition being tested is false.
237 @param aFileName A file name that is printed in the failure message if
238 the condition being tested is false.
240 @panic USER 84 if the condition being tested is false.
245 EXPORT_C void RTest::operator()(TInt aResult, TInt aLineNum, const TText* aFileName)
251 RDebug::Printf(": FAILING : failed check at line number %d", aLineNum);
253 Printf(_L(": FAIL : %S failed check %d in %s at line number %d\n"),
254 &iTitle, iCheck, aFileName, aLineNum);
255 Panic(_L("Checkpoint-fail\n"));
265 Checks the result of a condition and, if this is false, prints
266 a failure message at the console and raises a panic.
268 Before checking the condition passed in, the operator increments
269 a check number. This is a value that is set to zero at the start of a test
270 and is incremented by this operator (and by all variants of it). It identifies
271 the check being made within the current test.
272 This value is printed on the failure message.
274 Typically, the operator is called, passing a test condition, for example:
277 RTest test(... heading text...,line number)
279 ...some operation to be tested that returns a value in r...
283 The failure message has the format:
286 : FAIL : XXX failed check N at line Number: M
287 RTEST: Checkpoint-fail
290 where XXX is the console title, N is the check number, and M is
291 the line number passed in.
293 @param aResult The condition being tested.
294 This is interpreted as a true or false value.
295 @param aLineNum A line number that is printed in the failure message if
296 the condition being tested is false.
298 @panic USER 84 if the condition being tested is false.
303 EXPORT_C void RTest::operator()(TInt aResult,TInt aLineNum)
309 RDebug::Printf(": FAILING : failed check at line Number: %d", aLineNum);
311 Printf(_L(": FAIL : %S failed check %d at line Number: %d\n"), &iTitle, iCheck, aLineNum);
312 Panic(_L("Checkpoint-fail\n"));
322 Checks the result of a condition and, if this is false, prints
323 a failure message at the console and raises a panic.
325 Before checking the condition passed in, the operator increments
326 a check number. This is a value that is set to zero at the start of a test
327 and is incremented by this operator (and by all variants of it). It identifies
328 the check being made within the current test.
329 This value is printed on the failure message.
331 Typically, the operator is called, passing a test condition, for example:
334 RTest test(... heading text...)
336 ...some operation to be tested that returns a value in r...
340 The failure message has the format:
343 : FAIL : XXX failed check N
344 RTEST: Checkpoint-fail
347 where XXX is the console title, and N is the check number.
349 @param aResult The condition being tested.
350 This is interpreted as a true or false value.
352 @panic USER 84 if the condition being tested is false.
357 EXPORT_C void RTest::operator()(TInt aResult)
363 RDebug::Printf(": FAILING : failed check\n");
365 Printf(_L(": FAIL : %S failed check %d\n"), &iTitle, iCheck);
366 Panic(_L("Checkpoint-fail\n"));
376 Ends the current set of tests.
378 If this set of tests is not nested within another set,
379 then a message reporting success is written to
382 @panic USER 84 if there was no matching call to RTest::Start(),
383 i.e. more calls to End() have been made than calls to Start().
387 EXPORT_C void RTest::End()
389 // End the current level of tests.
390 if (TInt(iLevel-1) < 0)
392 Panic(_L("End() without matching Start()\n"));
399 Printf(_L("RTEST: SUCCESS : %S test completed O.K.\n"), &iTitle);
409 Prints an error message and an error code,
410 and raises a USER 84 panic.
412 @param anError The error code.
413 @param aFmt A format list.
414 @param ... A variable number of parameters.
416 EXPORT_C void RTest::Panic(TInt anError,TRefByValue<const TDesC> aFmt,...)
418 // Print an error message, an error and then panic.
419 TestOverflowTruncate overflow;
421 VA_START(list, aFmt);
423 aBuf.AppendFormat(_L("RTEST: "));
424 // coverity[uninit_use_in_call]
425 aBuf.AppendFormatList(aFmt, list, &overflow);
426 aBuf.AppendFormat(_L(" Failed with error %d\n"), anError);
430 ::Panic(ERTestFailed);
437 Prints an error message, and raises a USER 84 panic.
439 @param aFmt A format list.
440 @param ... A variable number of parameters.
442 EXPORT_C void RTest::Panic(TRefByValue<const TDesC> aFmt,...)
444 // Print an error message and then panic.
445 TestOverflowTruncate overflow;
447 VA_START(list, aFmt);
449 aBuf.AppendFormat(_L("RTEST: "));
450 // coverity[uninit_use_in_call]
451 aBuf.AppendFormatList(aFmt, list, &overflow);
455 ::Panic(ERTestFailed);
461 _LIT(KLitCRNL, "\r\n");
463 Prints text to the console.
465 If the logging flag is set, the string
466 is also written to the debug output as represented by an RDebug object.
468 @param aFmt A format list.
469 @param ... A variable number of parameters.
471 @see RTest::SetLogged()
475 EXPORT_C void RTest::Printf(TRefByValue<const TDesC> aFmt,...)
477 // Print to a console screen.
478 TestOverflowTruncate overflow;
480 VA_START(list, aFmt);
482 // coverity[uninit_use_in_call]
483 buf.AppendFormatList(aFmt, list, &overflow);
484 CheckConsoleCreated();
485 iConsole->Write(buf);
491 while ((newline = ptr.Locate('\n')) != KErrNotFound)
493 RDebug::RawPrint(ptr.Left(newline));
494 if (newline==0 || ptr[newline-1]!='\r')
495 RDebug::RawPrint(KLitCRNL); // bare nl, replace with crnl
497 RDebug::RawPrint(KLitNL); // crnl, already printed cr
498 if (newline+1<ptr.Length())
499 ptr.Set(ptr.Mid(newline+1));
501 return; // newline was end of string
503 RDebug::RawPrint(ptr);
511 Gets an input key stroke.
513 @return The input key code.
515 EXPORT_C TKeyCode RTest::Getch()
517 // Get a key from the console.
518 CheckConsoleCreated();
519 return(iConsole->Getch());
524 EXPORT_C TInt RTest::CloseHandleAndWaitForDestruction(RHandleBase& aH)
527 aH.NotifyDestruction(s);
529 TUint32 initial = User::NTickCount();
531 if (s == KErrNoMemory)
533 TInt factor = UserSvr::HalFunction(EHalGroupVariant, EVariantHalTimeoutExpansion, 0, 0);
538 TUint32 timeout = 5000 * (TUint32)factor;
539 TUint32 period = 1000 * (TUint32)factor;
540 while (s == KRequestPending)
542 TUint32 now = User::NTickCount();
543 if ((now - initial) > timeout)
545 User::CancelMiscNotifier(s);
549 User::AfterHighRes(period);
551 User::WaitForRequest(s);