1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/euser/us_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,555 @@
1.4 +// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32\euser\us_test.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include "us_std.h"
1.22 +#include <e32test.h>
1.23 +
1.24 +#if defined(test)
1.25 +#undef test
1.26 +#endif
1.27 +
1.28 +#include <e32svr.h>
1.29 +
1.30 +void RTest::CheckConsoleCreated()
1.31 + {
1.32 + // Check that the console has been created.
1.33 + if (iConsole == NULL)
1.34 + {
1.35 + TRAPD(r, iConsole = Console::NewL(iTitle, TSize(KConsFullScreen, KConsFullScreen)))
1.36 + __ASSERT_ALWAYS(r == KErrNone, ::Panic(ERTestCreateConsole));
1.37 + }
1.38 + }
1.39 +
1.40 +void RTest::DisplayLevel()
1.41 + {
1.42 + // Display the current level string.
1.43 + TBuf<0x100> aBuf(_L("RTEST: Level "));
1.44 + for (TInt ii = 1; ii < iLevel; ii++)
1.45 + {
1.46 + if (ii > 1)
1.47 + {
1.48 + aBuf.AppendFormat(_L(".%02d"), iStack[ii]);
1.49 + }
1.50 + else
1.51 + {
1.52 + aBuf.AppendFormat(_L(" %03d"), iStack[ii]);
1.53 + }
1.54 + }
1.55 + if (iLevel > 1)
1.56 + {
1.57 + aBuf.AppendFormat(_L(".%02d "), iTest);
1.58 + }
1.59 + else
1.60 + {
1.61 + aBuf.AppendFormat(_L(" %03d "), iTest);
1.62 + }
1.63 +
1.64 + Printf(aBuf);
1.65 + }
1.66 +
1.67 +
1.68 +
1.69 +
1.70 +/**
1.71 +Constructor.
1.72 +
1.73 +@param aTitle A title describing this use of RTest.
1.74 + This is also referred to as the console title.
1.75 +@param aThrowaway Not used.
1.76 +@param anOtherThrowaway Not used.
1.77 +*/
1.78 +EXPORT_C RTest::RTest(const TDesC &aTitle,TInt /* aThrowaway */,const TText* /* anOtherThrowaway */)
1.79 + : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
1.80 + // Constructor
1.81 + // There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
1.82 + // This depends upon the user naming their RTest object test, but if they do this then an extra
1.83 + // parameter aThrowaway must be added to the constructor
1.84 + {}
1.85 +
1.86 +
1.87 +
1.88 +/**
1.89 +Constructor.
1.90 +
1.91 +@param aTitle A title describing this use of RTest.
1.92 + This is also referred to as the console title.
1.93 +@param aThrowaway Not used.
1.94 +*/
1.95 +EXPORT_C RTest::RTest(const TDesC &aTitle, TInt /* athrowaway */)
1.96 + : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
1.97 + // Constructor
1.98 + // There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
1.99 + // This depends upon the user naming their RTest object test, but if they do this then an extra
1.100 + // parameter aThrowaway must be added to the constructor
1.101 + {}
1.102 +
1.103 +
1.104 +
1.105 +
1.106 +/**
1.107 +Constructor.
1.108 +
1.109 +@param aTitle A title describing this use of RTest.
1.110 + This is also referred to as the console title.
1.111 +*/
1.112 +EXPORT_C RTest::RTest(const TDesC &aTitle)
1.113 + : iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
1.114 + // Constructor
1.115 + {}
1.116 +
1.117 +
1.118 +
1.119 +
1.120 +/**
1.121 +Closes the console and frees any resources acquired.
1.122 +*/
1.123 +EXPORT_C void RTest::Close()
1.124 + {
1.125 + // Close the console.
1.126 + delete iConsole;
1.127 + iConsole=NULL;
1.128 + }
1.129 +
1.130 +
1.131 +
1.132 +
1.133 +/**
1.134 +Prints out the console title and version number.
1.135 +
1.136 +The format of the text is:
1.137 +
1.138 +@code
1.139 +RTEST TITLE: XXX YYY
1.140 +Epoc/32 YYY
1.141 +@endcode
1.142 +
1.143 +where XXX is the console title, and YYY is the version number,
1.144 +formatted as described by TVersion::Name().
1.145 +
1.146 +@see TVersion::Name()
1.147 +@see RTest::Printf()
1.148 +*/
1.149 +EXPORT_C void RTest::Title()
1.150 + {
1.151 + // Print out the program title and version number.
1.152 + TVersion v(KE32MajorVersionNumber, KE32MinorVersionNumber, KE32BuildVersionNumber);
1.153 + TBuf<16> vName=v.Name();
1.154 + Printf(_L("RTEST TITLE: %S %S\n"), &iTitle, &vName);
1.155 + vName=User::Version().Name();
1.156 + Printf(_L("Epoc/32 %S\n"), &vName);
1.157 + }
1.158 +
1.159 +
1.160 +
1.161 +
1.162 +/**
1.163 +Marks the start of a set of tests.
1.164 +
1.165 +Note that sets of tests can be nested.
1.166 +
1.167 +A call to this function must be matched by a call to RTest::End() to mark
1.168 +the end of this set of tests.
1.169 +
1.170 +@param aHeading A heading describing the set of tests; this is
1.171 + printed at the console.
1.172 +
1.173 +@see RTest::End()
1.174 +*/
1.175 +EXPORT_C void RTest::Start(const TDesC &aHeading)
1.176 + {
1.177 + // Print out the heading and nest the level.
1.178 + Push();
1.179 + Next(aHeading);
1.180 + }
1.181 +
1.182 +
1.183 +
1.184 +
1.185 +/**
1.186 +Marks the start of the next test.
1.187 +
1.188 +@param aHeading A heading describing the test; this
1.189 + is printed at the console. This function is also
1.190 + called by Start(), which passes the text that describes
1.191 + the set of tests.
1.192 +
1.193 +@see RTest::Start()
1.194 +*/
1.195 +EXPORT_C void RTest::Next(const TDesC &aHeading)
1.196 + {
1.197 + // Print out the heading and nest the level.
1.198 + iTest++;
1.199 + iCheck = 0;
1.200 + DisplayLevel();
1.201 + Printf(_L("Next test - %S\n"), &aHeading);
1.202 + }
1.203 +
1.204 +
1.205 +
1.206 +
1.207 +/**
1.208 +Checks the result of a condition and, if this is false, prints
1.209 +a failure message at the console and raises a panic.
1.210 +
1.211 +Before checking the condition passed in, the operator increments
1.212 +a check number. This is a value that is set to zero at the start of a test
1.213 +and is incremented by this operator (and by all variants of it). It identifies
1.214 +the check being made within the current test.
1.215 +This value is printed on a failure message.
1.216 +
1.217 +Typically, the operator is called, passing a test condition, for example:
1.218 +
1.219 +@code
1.220 +RTest test(... heading text...,line number... file name)
1.221 +TInt r;
1.222 +...some operation to be tested that returns a value in r...
1.223 +test(r==KErrNone);
1.224 +@endcode
1.225 +
1.226 +The failure message has the format:
1.227 +
1.228 +@code
1.229 +: FAIL : XXX failed check N in FFF at line Number: M
1.230 +RTEST: Checkpoint-fail
1.231 +@endcode
1.232 +
1.233 +where XXX is the console title, N is the check number, FFF is the filename,
1.234 +and M is the line number passed in.
1.235 +
1.236 +@param aResult The condition being tested.
1.237 + This is interpreted as a true or false value.
1.238 +@param aLineNum A line number that is printed in the failure message if
1.239 + the condition being tested is false.
1.240 +@param aFileName A file name that is printed in the failure message if
1.241 + the condition being tested is false.
1.242 +
1.243 +@panic USER 84 if the condition being tested is false.
1.244 +
1.245 +@see RTest::Next()
1.246 +@see RTest::Start()
1.247 +*/
1.248 +EXPORT_C void RTest::operator()(TInt aResult, TInt aLineNum, const TText* aFileName)
1.249 + {
1.250 + // Test a condition.
1.251 + iCheck++;
1.252 + if (!aResult)
1.253 + {
1.254 + RDebug::Printf(": FAILING : failed check at line number %d", aLineNum);
1.255 + DisplayLevel();
1.256 + Printf(_L(": FAIL : %S failed check %d in %s at line number %d\n"),
1.257 + &iTitle, iCheck, aFileName, aLineNum);
1.258 + Panic(_L("Checkpoint-fail\n"));
1.259 + if (!iLogging)
1.260 + Getch();
1.261 + }
1.262 + }
1.263 +
1.264 +
1.265 +
1.266 +
1.267 +/**
1.268 +Checks the result of a condition and, if this is false, prints
1.269 +a failure message at the console and raises a panic.
1.270 +
1.271 +Before checking the condition passed in, the operator increments
1.272 +a check number. This is a value that is set to zero at the start of a test
1.273 +and is incremented by this operator (and by all variants of it). It identifies
1.274 +the check being made within the current test.
1.275 +This value is printed on the failure message.
1.276 +
1.277 +Typically, the operator is called, passing a test condition, for example:
1.278 +
1.279 +@code
1.280 +RTest test(... heading text...,line number)
1.281 +TInt r;
1.282 +...some operation to be tested that returns a value in r...
1.283 +test(r==KErrNone);
1.284 +@endcode
1.285 +
1.286 +The failure message has the format:
1.287 +
1.288 +@code
1.289 +: FAIL : XXX failed check N at line Number: M
1.290 +RTEST: Checkpoint-fail
1.291 +@endcode
1.292 +
1.293 +where XXX is the console title, N is the check number, and M is
1.294 +the line number passed in.
1.295 +
1.296 +@param aResult The condition being tested.
1.297 + This is interpreted as a true or false value.
1.298 +@param aLineNum A line number that is printed in the failure message if
1.299 + the condition being tested is false.
1.300 +
1.301 +@panic USER 84 if the condition being tested is false.
1.302 +
1.303 +@see RTest::Next()
1.304 +@see RTest::Start()
1.305 +*/
1.306 +EXPORT_C void RTest::operator()(TInt aResult,TInt aLineNum)
1.307 + {
1.308 + // Test a condition.
1.309 + iCheck++;
1.310 + if (!aResult)
1.311 + {
1.312 + RDebug::Printf(": FAILING : failed check at line Number: %d", aLineNum);
1.313 + DisplayLevel();
1.314 + Printf(_L(": FAIL : %S failed check %d at line Number: %d\n"), &iTitle, iCheck, aLineNum);
1.315 + Panic(_L("Checkpoint-fail\n"));
1.316 + if (!iLogging)
1.317 + Getch();
1.318 + }
1.319 + }
1.320 +
1.321 +
1.322 +
1.323 +
1.324 +/**
1.325 +Checks the result of a condition and, if this is false, prints
1.326 +a failure message at the console and raises a panic.
1.327 +
1.328 +Before checking the condition passed in, the operator increments
1.329 +a check number. This is a value that is set to zero at the start of a test
1.330 +and is incremented by this operator (and by all variants of it). It identifies
1.331 +the check being made within the current test.
1.332 +This value is printed on the failure message.
1.333 +
1.334 +Typically, the operator is called, passing a test condition, for example:
1.335 +
1.336 +@code
1.337 +RTest test(... heading text...)
1.338 +TInt r;
1.339 +...some operation to be tested that returns a value in r...
1.340 +test(r==KErrNone);
1.341 +@endcode
1.342 +
1.343 +The failure message has the format:
1.344 +
1.345 +@code
1.346 +: FAIL : XXX failed check N
1.347 +RTEST: Checkpoint-fail
1.348 +@endcode
1.349 +
1.350 +where XXX is the console title, and N is the check number.
1.351 +
1.352 +@param aResult The condition being tested.
1.353 + This is interpreted as a true or false value.
1.354 +
1.355 +@panic USER 84 if the condition being tested is false.
1.356 +
1.357 +@see RTest::Next()
1.358 +@see RTest::Start()
1.359 +*/
1.360 +EXPORT_C void RTest::operator()(TInt aResult)
1.361 + {
1.362 + // Test a condition.
1.363 + iCheck++;
1.364 + if (!aResult)
1.365 + {
1.366 + RDebug::Printf(": FAILING : failed check\n");
1.367 + DisplayLevel();
1.368 + Printf(_L(": FAIL : %S failed check %d\n"), &iTitle, iCheck);
1.369 + Panic(_L("Checkpoint-fail\n"));
1.370 + if (!iLogging)
1.371 + Getch();
1.372 + }
1.373 + }
1.374 +
1.375 +
1.376 +
1.377 +
1.378 +/**
1.379 +Ends the current set of tests.
1.380 +
1.381 +If this set of tests is not nested within another set,
1.382 +then a message reporting success is written to
1.383 +the console.
1.384 +
1.385 +@panic USER 84 if there was no matching call to RTest::Start(),
1.386 + i.e. more calls to End() have been made than calls to Start().
1.387 +
1.388 +@see RTest::Start()
1.389 +*/
1.390 +EXPORT_C void RTest::End()
1.391 + {
1.392 + // End the current level of tests.
1.393 + if (TInt(iLevel-1) < 0)
1.394 + {
1.395 + Panic(_L("End() without matching Start()\n"));
1.396 + }
1.397 +
1.398 + Pop();
1.399 +
1.400 + if (iLevel == 0)
1.401 + {
1.402 + Printf(_L("RTEST: SUCCESS : %S test completed O.K.\n"), &iTitle);
1.403 + if (!iLogging)
1.404 + Getch();
1.405 + }
1.406 + }
1.407 +
1.408 +
1.409 +
1.410 +
1.411 +/**
1.412 +Prints an error message and an error code,
1.413 +and raises a USER 84 panic.
1.414 +
1.415 +@param anError The error code.
1.416 +@param aFmt A format list.
1.417 +@param ... A variable number of parameters.
1.418 +*/
1.419 +EXPORT_C void RTest::Panic(TInt anError,TRefByValue<const TDesC> aFmt,...)
1.420 + {
1.421 + // Print an error message, an error and then panic.
1.422 + TestOverflowTruncate overflow;
1.423 + VA_LIST list;
1.424 + VA_START(list, aFmt);
1.425 + TBuf<0x100> aBuf;
1.426 + aBuf.AppendFormat(_L("RTEST: "));
1.427 + // coverity[uninit_use_in_call]
1.428 + aBuf.AppendFormatList(aFmt, list, &overflow);
1.429 + aBuf.AppendFormat(_L(" Failed with error %d\n"), anError);
1.430 + Printf(aBuf);
1.431 + if (!iLogging)
1.432 + Getch();
1.433 + ::Panic(ERTestFailed);
1.434 + }
1.435 +
1.436 +
1.437 +
1.438 +
1.439 +/**
1.440 +Prints an error message, and raises a USER 84 panic.
1.441 +
1.442 +@param aFmt A format list.
1.443 +@param ... A variable number of parameters.
1.444 +*/
1.445 +EXPORT_C void RTest::Panic(TRefByValue<const TDesC> aFmt,...)
1.446 + {
1.447 + // Print an error message and then panic.
1.448 + TestOverflowTruncate overflow;
1.449 + VA_LIST list;
1.450 + VA_START(list, aFmt);
1.451 + TBuf<0x100> aBuf;
1.452 + aBuf.AppendFormat(_L("RTEST: "));
1.453 + // coverity[uninit_use_in_call]
1.454 + aBuf.AppendFormatList(aFmt, list, &overflow);
1.455 + Printf(aBuf);
1.456 + if (!iLogging)
1.457 + Getch();
1.458 + ::Panic(ERTestFailed);
1.459 + }
1.460 +
1.461 +
1.462 +
1.463 +_LIT(KLitNL, "\n");
1.464 +_LIT(KLitCRNL, "\r\n");
1.465 +/**
1.466 +Prints text to the console.
1.467 +
1.468 +If the logging flag is set, the string
1.469 +is also written to the debug output as represented by an RDebug object.
1.470 +
1.471 +@param aFmt A format list.
1.472 +@param ... A variable number of parameters.
1.473 +
1.474 +@see RTest::SetLogged()
1.475 +@see Rtest::Logged()
1.476 +@see RDebug
1.477 +*/
1.478 +EXPORT_C void RTest::Printf(TRefByValue<const TDesC> aFmt,...)
1.479 + {
1.480 + // Print to a console screen.
1.481 + TestOverflowTruncate overflow;
1.482 + VA_LIST list;
1.483 + VA_START(list, aFmt);
1.484 + TBuf<0x100> buf;
1.485 + // coverity[uninit_use_in_call]
1.486 + buf.AppendFormatList(aFmt, list, &overflow);
1.487 + CheckConsoleCreated();
1.488 + iConsole->Write(buf);
1.489 +
1.490 + if (iLogging)
1.491 + {
1.492 + TPtrC ptr(buf);
1.493 + TInt newline;
1.494 + while ((newline = ptr.Locate('\n')) != KErrNotFound)
1.495 + {
1.496 + RDebug::RawPrint(ptr.Left(newline));
1.497 + if (newline==0 || ptr[newline-1]!='\r')
1.498 + RDebug::RawPrint(KLitCRNL); // bare nl, replace with crnl
1.499 + else
1.500 + RDebug::RawPrint(KLitNL); // crnl, already printed cr
1.501 + if (newline+1<ptr.Length())
1.502 + ptr.Set(ptr.Mid(newline+1));
1.503 + else
1.504 + return; // newline was end of string
1.505 + }
1.506 + RDebug::RawPrint(ptr);
1.507 + }
1.508 + }
1.509 +
1.510 +
1.511 +
1.512 +
1.513 +/**
1.514 +Gets an input key stroke.
1.515 +
1.516 +@return The input key code.
1.517 +*/
1.518 +EXPORT_C TKeyCode RTest::Getch()
1.519 + {
1.520 + // Get a key from the console.
1.521 + CheckConsoleCreated();
1.522 + return(iConsole->Getch());
1.523 + }
1.524 +
1.525 +
1.526 +
1.527 +EXPORT_C TInt RTest::CloseHandleAndWaitForDestruction(RHandleBase& aH)
1.528 + {
1.529 + TRequestStatus s;
1.530 + aH.NotifyDestruction(s);
1.531 + aH.Close();
1.532 + TUint32 initial = User::NTickCount();
1.533 + TInt r = KErrNone;
1.534 + if (s == KErrNoMemory)
1.535 + r = KErrNoMemory;
1.536 + TInt factor = UserSvr::HalFunction(EHalGroupVariant, EVariantHalTimeoutExpansion, 0, 0);
1.537 + if (factor<=0)
1.538 + factor = 1;
1.539 + if (factor>1024)
1.540 + factor = 1024;
1.541 + TUint32 timeout = 5000 * (TUint32)factor;
1.542 + TUint32 period = 1000 * (TUint32)factor;
1.543 + while (s == KRequestPending)
1.544 + {
1.545 + TUint32 now = User::NTickCount();
1.546 + if ((now - initial) > timeout)
1.547 + {
1.548 + User::CancelMiscNotifier(s);
1.549 + r = KErrTimedOut;
1.550 + break;
1.551 + }
1.552 + User::AfterHighRes(period);
1.553 + }
1.554 + User::WaitForRequest(s);
1.555 + return r;
1.556 + }
1.557 +
1.558 +