os/kernelhwsrv/kernel/eka/euser/us_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\us_test.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "us_std.h"
sl@0
    19
#include <e32test.h>
sl@0
    20
sl@0
    21
#if defined(test)
sl@0
    22
#undef test
sl@0
    23
#endif
sl@0
    24
sl@0
    25
#include <e32svr.h> 
sl@0
    26
sl@0
    27
void RTest::CheckConsoleCreated()
sl@0
    28
	{
sl@0
    29
	// Check that the console has been created.
sl@0
    30
	if (iConsole == NULL)
sl@0
    31
		{
sl@0
    32
		TRAPD(r, iConsole = Console::NewL(iTitle, TSize(KConsFullScreen, KConsFullScreen)))
sl@0
    33
		__ASSERT_ALWAYS(r == KErrNone, ::Panic(ERTestCreateConsole));
sl@0
    34
		}
sl@0
    35
	}
sl@0
    36
sl@0
    37
void RTest::DisplayLevel()
sl@0
    38
	{
sl@0
    39
	// Display the current level string.
sl@0
    40
	TBuf<0x100> aBuf(_L("RTEST: Level "));
sl@0
    41
	for (TInt ii = 1; ii < iLevel; ii++)
sl@0
    42
		{
sl@0
    43
		if (ii > 1)
sl@0
    44
			{
sl@0
    45
			aBuf.AppendFormat(_L(".%02d"), iStack[ii]);
sl@0
    46
			}
sl@0
    47
		else
sl@0
    48
			{
sl@0
    49
			aBuf.AppendFormat(_L(" %03d"), iStack[ii]);
sl@0
    50
			}
sl@0
    51
		}
sl@0
    52
	if (iLevel > 1)
sl@0
    53
		{
sl@0
    54
		aBuf.AppendFormat(_L(".%02d "), iTest);
sl@0
    55
		}
sl@0
    56
	else
sl@0
    57
		{
sl@0
    58
		aBuf.AppendFormat(_L(" %03d "), iTest);
sl@0
    59
		}
sl@0
    60
sl@0
    61
	Printf(aBuf);
sl@0
    62
	}
sl@0
    63
sl@0
    64
sl@0
    65
sl@0
    66
sl@0
    67
/**
sl@0
    68
Constructor.
sl@0
    69
sl@0
    70
@param aTitle           A title describing this use of RTest.
sl@0
    71
                        This is also referred to as the console title.
sl@0
    72
@param aThrowaway       Not used.
sl@0
    73
@param anOtherThrowaway Not used.
sl@0
    74
*/
sl@0
    75
EXPORT_C RTest::RTest(const TDesC &aTitle,TInt /* aThrowaway */,const TText* /* anOtherThrowaway */)
sl@0
    76
	: iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
sl@0
    77
	// Constructor
sl@0
    78
	// There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
sl@0
    79
	// This depends upon the user naming their RTest object test, but if they do this then an extra
sl@0
    80
	// parameter aThrowaway must be added to the constructor
sl@0
    81
	{}
sl@0
    82
sl@0
    83
sl@0
    84
sl@0
    85
/**
sl@0
    86
Constructor.
sl@0
    87
sl@0
    88
@param aTitle     A title describing this use of RTest.
sl@0
    89
                  This is also referred to as the console title.
sl@0
    90
@param aThrowaway Not used.
sl@0
    91
*/
sl@0
    92
EXPORT_C RTest::RTest(const TDesC &aTitle, TInt /* athrowaway */)
sl@0
    93
	: iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
sl@0
    94
	// Constructor
sl@0
    95
	// There is a #define test(x) test(x, __LINE__) in e32test.h to pass on line info of failing tests,
sl@0
    96
	// This depends upon the user naming their RTest object test, but if they do this then an extra
sl@0
    97
	// parameter aThrowaway must be added to the constructor
sl@0
    98
	{}
sl@0
    99
sl@0
   100
sl@0
   101
sl@0
   102
sl@0
   103
/**
sl@0
   104
Constructor.
sl@0
   105
sl@0
   106
@param aTitle A title describing this use of RTest.
sl@0
   107
              This is also referred to as the console title.
sl@0
   108
*/
sl@0
   109
EXPORT_C RTest::RTest(const TDesC &aTitle)
sl@0
   110
	: iTest(0), iLevel(0), iLogging(ETrue), iConsole(NULL), iTitle(aTitle)
sl@0
   111
	// Constructor
sl@0
   112
	{}
sl@0
   113
sl@0
   114
sl@0
   115
sl@0
   116
sl@0
   117
/**
sl@0
   118
Closes the console and frees any resources acquired.
sl@0
   119
*/
sl@0
   120
EXPORT_C void RTest::Close()
sl@0
   121
	{
sl@0
   122
	// Close the console.
sl@0
   123
	delete iConsole;
sl@0
   124
	iConsole=NULL;
sl@0
   125
	}
sl@0
   126
sl@0
   127
sl@0
   128
sl@0
   129
sl@0
   130
/**
sl@0
   131
Prints out the console title and version number.
sl@0
   132
sl@0
   133
The format of the text is:
sl@0
   134
sl@0
   135
@code
sl@0
   136
RTEST TITLE: XXX YYY
sl@0
   137
Epoc/32 YYY
sl@0
   138
@endcode
sl@0
   139
sl@0
   140
where XXX is the console title, and YYY is the version number,
sl@0
   141
formatted as described by TVersion::Name().
sl@0
   142
sl@0
   143
@see TVersion::Name()
sl@0
   144
@see RTest::Printf() 
sl@0
   145
*/
sl@0
   146
EXPORT_C void RTest::Title()
sl@0
   147
	{
sl@0
   148
	// Print out the program title and version number.
sl@0
   149
	TVersion v(KE32MajorVersionNumber, KE32MinorVersionNumber, KE32BuildVersionNumber);
sl@0
   150
	TBuf<16> vName=v.Name();
sl@0
   151
	Printf(_L("RTEST TITLE: %S %S\n"), &iTitle, &vName);
sl@0
   152
	vName=User::Version().Name();
sl@0
   153
	Printf(_L("Epoc/32 %S\n"), &vName);
sl@0
   154
	}
sl@0
   155
sl@0
   156
sl@0
   157
sl@0
   158
sl@0
   159
/**
sl@0
   160
Marks the start of a set of tests.
sl@0
   161
sl@0
   162
Note that sets of tests can be nested.
sl@0
   163
sl@0
   164
A call to this function must be matched by a call to RTest::End() to mark
sl@0
   165
the end of this set of tests.
sl@0
   166
sl@0
   167
@param aHeading A heading describing the set of tests; this is
sl@0
   168
                printed at the console.
sl@0
   169
                
sl@0
   170
@see RTest::End()                
sl@0
   171
*/
sl@0
   172
EXPORT_C void RTest::Start(const TDesC &aHeading)
sl@0
   173
	{
sl@0
   174
	// Print out the heading and nest the level.
sl@0
   175
	Push();
sl@0
   176
	Next(aHeading);
sl@0
   177
	}
sl@0
   178
sl@0
   179
sl@0
   180
sl@0
   181
sl@0
   182
/**
sl@0
   183
Marks the start of the next test.
sl@0
   184
sl@0
   185
@param aHeading A heading describing the test; this
sl@0
   186
                is printed at the console. This function is also
sl@0
   187
                called by Start(), which passes the text that describes
sl@0
   188
                the set of tests.
sl@0
   189
                
sl@0
   190
@see RTest::Start()                
sl@0
   191
*/
sl@0
   192
EXPORT_C void RTest::Next(const TDesC &aHeading)
sl@0
   193
	{
sl@0
   194
	// Print out the heading and nest the level.
sl@0
   195
	iTest++;
sl@0
   196
	iCheck = 0;
sl@0
   197
	DisplayLevel();
sl@0
   198
	Printf(_L("Next test - %S\n"), &aHeading);
sl@0
   199
	}
sl@0
   200
sl@0
   201
sl@0
   202
sl@0
   203
sl@0
   204
/**
sl@0
   205
Checks the result of a condition and, if this is false, prints
sl@0
   206
a failure message at the console and raises a panic.
sl@0
   207
sl@0
   208
Before checking the condition passed in, the operator increments
sl@0
   209
a check number. This is a value that is set to zero at the start of a test
sl@0
   210
and is incremented by this operator (and by all variants of it). It identifies
sl@0
   211
the check being made within the current test.
sl@0
   212
This value is printed on a failure message.
sl@0
   213
sl@0
   214
Typically, the operator is called, passing a test condition, for example:
sl@0
   215
sl@0
   216
@code
sl@0
   217
RTest test(... heading text...,line number... file name)
sl@0
   218
TInt r;
sl@0
   219
...some operation to be tested that returns a value in r...
sl@0
   220
test(r==KErrNone);
sl@0
   221
@endcode
sl@0
   222
sl@0
   223
The failure message has the format:
sl@0
   224
sl@0
   225
@code
sl@0
   226
: FAIL : XXX failed check N in FFF at line Number: M
sl@0
   227
RTEST: Checkpoint-fail
sl@0
   228
@endcode
sl@0
   229
sl@0
   230
where XXX is the console title, N is the check number, FFF is the filename,
sl@0
   231
and M is the line number passed in.
sl@0
   232
sl@0
   233
@param aResult   The condition being tested.
sl@0
   234
                 This is interpreted as a true or false value.
sl@0
   235
@param aLineNum  A line number that is printed in the failure message if
sl@0
   236
                 the condition being tested is false.
sl@0
   237
@param aFileName A file name that is printed in the failure message if
sl@0
   238
                 the condition being tested is false.
sl@0
   239
                 
sl@0
   240
@panic USER 84 if the condition being tested is false.
sl@0
   241
sl@0
   242
@see RTest::Next()
sl@0
   243
@see RTest::Start()
sl@0
   244
*/
sl@0
   245
EXPORT_C void RTest::operator()(TInt aResult, TInt aLineNum, const TText* aFileName)
sl@0
   246
	{
sl@0
   247
	// Test a condition.
sl@0
   248
	iCheck++;
sl@0
   249
	if (!aResult)
sl@0
   250
		{
sl@0
   251
		RDebug::Printf(": FAILING : failed check at line number %d", aLineNum);
sl@0
   252
		DisplayLevel();
sl@0
   253
		Printf(_L(": FAIL : %S failed check %d in %s at line number %d\n"),
sl@0
   254
			   &iTitle, iCheck, aFileName, aLineNum);
sl@0
   255
		Panic(_L("Checkpoint-fail\n"));
sl@0
   256
		if (!iLogging)
sl@0
   257
			Getch();
sl@0
   258
		}
sl@0
   259
	}
sl@0
   260
sl@0
   261
sl@0
   262
sl@0
   263
sl@0
   264
/**
sl@0
   265
Checks the result of a condition and, if this is false, prints
sl@0
   266
a failure message at the console and raises a panic.
sl@0
   267
sl@0
   268
Before checking the condition passed in, the operator increments
sl@0
   269
a check number. This is a value that is set to zero at the start of a test
sl@0
   270
and is incremented by this operator (and by all variants of it). It identifies
sl@0
   271
the check being made within the current test.
sl@0
   272
This value is printed on the failure message.
sl@0
   273
sl@0
   274
Typically, the operator is called, passing a test condition, for example:
sl@0
   275
sl@0
   276
@code
sl@0
   277
RTest test(... heading text...,line number)
sl@0
   278
TInt r;
sl@0
   279
...some operation to be tested that returns a value in r...
sl@0
   280
test(r==KErrNone);
sl@0
   281
@endcode
sl@0
   282
sl@0
   283
The failure message has the format:
sl@0
   284
sl@0
   285
@code
sl@0
   286
: FAIL : XXX failed check N at line Number: M
sl@0
   287
RTEST: Checkpoint-fail
sl@0
   288
@endcode
sl@0
   289
sl@0
   290
where XXX is the console title, N is the check number, and M is
sl@0
   291
the line number passed in.
sl@0
   292
sl@0
   293
@param aResult  The condition being tested.
sl@0
   294
                This is interpreted as a true or false value.
sl@0
   295
@param aLineNum A line number that is printed in the failure message if
sl@0
   296
                the condition being tested is false.               
sl@0
   297
sl@0
   298
@panic USER 84 if the condition being tested is false.
sl@0
   299
sl@0
   300
@see RTest::Next()
sl@0
   301
@see RTest::Start()
sl@0
   302
*/
sl@0
   303
EXPORT_C void RTest::operator()(TInt aResult,TInt aLineNum)
sl@0
   304
	{
sl@0
   305
	// Test a condition.
sl@0
   306
	iCheck++;
sl@0
   307
	if (!aResult)
sl@0
   308
		{
sl@0
   309
		RDebug::Printf(": FAILING : failed check at line Number: %d", aLineNum);
sl@0
   310
		DisplayLevel();
sl@0
   311
		Printf(_L(": FAIL : %S failed check %d at line Number: %d\n"), &iTitle, iCheck, aLineNum);
sl@0
   312
		Panic(_L("Checkpoint-fail\n"));
sl@0
   313
		if (!iLogging)
sl@0
   314
			Getch();
sl@0
   315
		}
sl@0
   316
	}  
sl@0
   317
sl@0
   318
sl@0
   319
sl@0
   320
sl@0
   321
/**
sl@0
   322
Checks the result of a condition and, if this is false, prints
sl@0
   323
a failure message at the console and raises a panic.
sl@0
   324
sl@0
   325
Before checking the condition passed in, the operator increments
sl@0
   326
a check number. This is a value that is set to zero at the start of a test
sl@0
   327
and is incremented by this operator (and by all variants of it). It identifies
sl@0
   328
the check being made within the current test.
sl@0
   329
This value is printed on the failure message.
sl@0
   330
sl@0
   331
Typically, the operator is called, passing a test condition, for example:
sl@0
   332
sl@0
   333
@code
sl@0
   334
RTest test(... heading text...)
sl@0
   335
TInt r;
sl@0
   336
...some operation to be tested that returns a value in r...
sl@0
   337
test(r==KErrNone);
sl@0
   338
@endcode
sl@0
   339
sl@0
   340
The failure message has the format:
sl@0
   341
sl@0
   342
@code
sl@0
   343
: FAIL : XXX failed check N
sl@0
   344
RTEST: Checkpoint-fail
sl@0
   345
@endcode
sl@0
   346
sl@0
   347
where XXX is the console title, and N is the check number.
sl@0
   348
sl@0
   349
@param aResult The condition being tested.
sl@0
   350
               This is interpreted as a true or false value.
sl@0
   351
sl@0
   352
@panic USER 84 if the condition being tested is false.
sl@0
   353
sl@0
   354
@see RTest::Next()
sl@0
   355
@see RTest::Start()
sl@0
   356
*/
sl@0
   357
EXPORT_C void RTest::operator()(TInt aResult)
sl@0
   358
	{
sl@0
   359
	// Test a condition.
sl@0
   360
	iCheck++;
sl@0
   361
	if (!aResult)
sl@0
   362
		{
sl@0
   363
		RDebug::Printf(": FAILING : failed check\n");
sl@0
   364
		DisplayLevel();
sl@0
   365
		Printf(_L(": FAIL : %S failed check %d\n"), &iTitle, iCheck);
sl@0
   366
		Panic(_L("Checkpoint-fail\n"));
sl@0
   367
		if (!iLogging)
sl@0
   368
			Getch();
sl@0
   369
		}
sl@0
   370
	}
sl@0
   371
sl@0
   372
sl@0
   373
sl@0
   374
sl@0
   375
/**
sl@0
   376
Ends the current set of tests.
sl@0
   377
sl@0
   378
If this set of tests is not nested within another set,
sl@0
   379
then a message reporting success is written to
sl@0
   380
the console.
sl@0
   381
sl@0
   382
@panic USER 84 if there was no matching call to RTest::Start(),
sl@0
   383
               i.e. more calls to End() have been made than calls to Start().
sl@0
   384
sl@0
   385
@see RTest::Start()
sl@0
   386
*/
sl@0
   387
EXPORT_C void RTest::End()
sl@0
   388
	{
sl@0
   389
	// End the current level of tests.
sl@0
   390
	if (TInt(iLevel-1) < 0)
sl@0
   391
		{
sl@0
   392
		Panic(_L("End() without matching Start()\n"));
sl@0
   393
		}
sl@0
   394
sl@0
   395
	Pop();
sl@0
   396
sl@0
   397
	if (iLevel == 0)
sl@0
   398
		{
sl@0
   399
		Printf(_L("RTEST: SUCCESS : %S test completed O.K.\n"), &iTitle);
sl@0
   400
		if (!iLogging)
sl@0
   401
			Getch();
sl@0
   402
		}
sl@0
   403
	}
sl@0
   404
sl@0
   405
sl@0
   406
sl@0
   407
sl@0
   408
/**
sl@0
   409
Prints an error message and an error code,
sl@0
   410
and raises a USER 84 panic.
sl@0
   411
sl@0
   412
@param anError The error code.
sl@0
   413
@param aFmt    A format list.
sl@0
   414
@param ...     A variable number of parameters.
sl@0
   415
*/
sl@0
   416
EXPORT_C void RTest::Panic(TInt anError,TRefByValue<const TDesC> aFmt,...)
sl@0
   417
	{
sl@0
   418
	// Print an error message, an error and then panic.
sl@0
   419
	TestOverflowTruncate overflow;
sl@0
   420
	VA_LIST list;
sl@0
   421
	VA_START(list, aFmt);
sl@0
   422
	TBuf<0x100> aBuf;
sl@0
   423
	aBuf.AppendFormat(_L("RTEST: "));
sl@0
   424
	// coverity[uninit_use_in_call]
sl@0
   425
	aBuf.AppendFormatList(aFmt, list, &overflow);
sl@0
   426
	aBuf.AppendFormat(_L(" Failed with error %d\n"), anError);
sl@0
   427
	Printf(aBuf);
sl@0
   428
	if (!iLogging)
sl@0
   429
		Getch();
sl@0
   430
	::Panic(ERTestFailed);
sl@0
   431
	}
sl@0
   432
sl@0
   433
sl@0
   434
sl@0
   435
sl@0
   436
/**
sl@0
   437
Prints an error message, and raises a USER 84 panic.
sl@0
   438
sl@0
   439
@param aFmt    A format list.
sl@0
   440
@param ...     A variable number of parameters.
sl@0
   441
*/
sl@0
   442
EXPORT_C void RTest::Panic(TRefByValue<const TDesC> aFmt,...)
sl@0
   443
	{
sl@0
   444
	// Print an error message and then panic.
sl@0
   445
	TestOverflowTruncate overflow;
sl@0
   446
	VA_LIST list;
sl@0
   447
	VA_START(list, aFmt);
sl@0
   448
	TBuf<0x100> aBuf;
sl@0
   449
	aBuf.AppendFormat(_L("RTEST: "));
sl@0
   450
	// coverity[uninit_use_in_call]
sl@0
   451
	aBuf.AppendFormatList(aFmt, list, &overflow);
sl@0
   452
	Printf(aBuf);
sl@0
   453
	if (!iLogging)
sl@0
   454
		Getch();
sl@0
   455
	::Panic(ERTestFailed);
sl@0
   456
	}
sl@0
   457
sl@0
   458
sl@0
   459
sl@0
   460
_LIT(KLitNL, "\n");
sl@0
   461
_LIT(KLitCRNL, "\r\n");
sl@0
   462
/**
sl@0
   463
Prints text to the console.
sl@0
   464
sl@0
   465
If the logging flag is set, the string
sl@0
   466
is also written to the debug output as represented by an RDebug object.
sl@0
   467
sl@0
   468
@param aFmt    A format list.
sl@0
   469
@param ...     A variable number of parameters.
sl@0
   470
sl@0
   471
@see RTest::SetLogged()
sl@0
   472
@see Rtest::Logged()
sl@0
   473
@see RDebug
sl@0
   474
*/
sl@0
   475
EXPORT_C void RTest::Printf(TRefByValue<const TDesC> aFmt,...)
sl@0
   476
	{
sl@0
   477
	// Print to a console screen.
sl@0
   478
	TestOverflowTruncate overflow;
sl@0
   479
	VA_LIST list;
sl@0
   480
	VA_START(list, aFmt);
sl@0
   481
	TBuf<0x100> buf;
sl@0
   482
	// coverity[uninit_use_in_call]
sl@0
   483
	buf.AppendFormatList(aFmt, list, &overflow);
sl@0
   484
	CheckConsoleCreated();
sl@0
   485
	iConsole->Write(buf);
sl@0
   486
	
sl@0
   487
	if (iLogging)
sl@0
   488
		{
sl@0
   489
		TPtrC ptr(buf);
sl@0
   490
		TInt newline;
sl@0
   491
		while ((newline = ptr.Locate('\n')) != KErrNotFound)
sl@0
   492
			{
sl@0
   493
			RDebug::RawPrint(ptr.Left(newline));
sl@0
   494
			if (newline==0 || ptr[newline-1]!='\r')
sl@0
   495
				RDebug::RawPrint(KLitCRNL); // bare nl, replace with crnl
sl@0
   496
			else
sl@0
   497
				RDebug::RawPrint(KLitNL); // crnl, already printed cr
sl@0
   498
			if (newline+1<ptr.Length())
sl@0
   499
				ptr.Set(ptr.Mid(newline+1));
sl@0
   500
			else
sl@0
   501
				return; // newline was end of string
sl@0
   502
			}
sl@0
   503
		RDebug::RawPrint(ptr);
sl@0
   504
		}
sl@0
   505
	}
sl@0
   506
sl@0
   507
sl@0
   508
sl@0
   509
sl@0
   510
/**
sl@0
   511
Gets an input key stroke.
sl@0
   512
sl@0
   513
@return The input key code.
sl@0
   514
*/
sl@0
   515
EXPORT_C TKeyCode RTest::Getch()
sl@0
   516
	{
sl@0
   517
	// Get a key from the console.
sl@0
   518
	CheckConsoleCreated();
sl@0
   519
	return(iConsole->Getch());
sl@0
   520
	}
sl@0
   521
sl@0
   522
sl@0
   523
sl@0
   524
EXPORT_C TInt RTest::CloseHandleAndWaitForDestruction(RHandleBase& aH)
sl@0
   525
	{
sl@0
   526
	TRequestStatus s;
sl@0
   527
	aH.NotifyDestruction(s);
sl@0
   528
	aH.Close();
sl@0
   529
	TUint32 initial = User::NTickCount();
sl@0
   530
	TInt r = KErrNone;
sl@0
   531
	if (s == KErrNoMemory)
sl@0
   532
		r = KErrNoMemory;
sl@0
   533
	TInt factor = UserSvr::HalFunction(EHalGroupVariant, EVariantHalTimeoutExpansion, 0, 0);
sl@0
   534
	if (factor<=0)
sl@0
   535
		factor = 1;
sl@0
   536
	if (factor>1024)
sl@0
   537
		factor = 1024;
sl@0
   538
	TUint32 timeout = 5000 * (TUint32)factor;
sl@0
   539
	TUint32 period = 1000 * (TUint32)factor;
sl@0
   540
	while (s == KRequestPending)
sl@0
   541
		{
sl@0
   542
		TUint32 now = User::NTickCount();
sl@0
   543
		if ((now - initial) > timeout)
sl@0
   544
			{
sl@0
   545
			User::CancelMiscNotifier(s);
sl@0
   546
			r = KErrTimedOut;
sl@0
   547
			break;
sl@0
   548
			}
sl@0
   549
		User::AfterHighRes(period);
sl@0
   550
		}
sl@0
   551
	User::WaitForRequest(s);
sl@0
   552
	return r;
sl@0
   553
	}
sl@0
   554
sl@0
   555