os/ossrv/lowlevellibsandfws/pluginfw/Test_Bed/test_bed/UnitTest.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1997-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 "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
// Implementation of the CUnitTest base class.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <ecom/test_bed/unittest.h>
sl@0
    19
#include <ecom/test_bed/transition.h>
sl@0
    20
#include <ecom/test_bed/datalogger.h>
sl@0
    21
#include <ecom/test_bed/testbeddefinitions.h>
sl@0
    22
sl@0
    23
sl@0
    24
EXPORT_C CUnitTest::~CUnitTest()
sl@0
    25
	{
sl@0
    26
	Cancel();
sl@0
    27
sl@0
    28
	// Delete any outstanding asynchronous transitions
sl@0
    29
	if(iOutstandingTransitions)
sl@0
    30
		{
sl@0
    31
		iOutstandingTransitions->Reset();
sl@0
    32
		delete iOutstandingTransitions;
sl@0
    33
		}
sl@0
    34
sl@0
    35
	if(iTransitions)
sl@0
    36
		{
sl@0
    37
		iTransitions->ResetAndDestroy();
sl@0
    38
		delete iTransitions;
sl@0
    39
		}
sl@0
    40
sl@0
    41
	iLeaveErrorArray.Reset();
sl@0
    42
sl@0
    43
	delete iFileMan;
sl@0
    44
	iFs.Close();
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C void CUnitTest::UnitTestConstructL()
sl@0
    48
	{
sl@0
    49
	User::LeaveIfError(iFs.Connect());
sl@0
    50
	iFileMan = CFileMan::NewL(iFs);
sl@0
    51
	CTimer::ConstructL();
sl@0
    52
	iTransitions = new(ELeave) RPointerArray<CTransition>;
sl@0
    53
	iOutstandingTransitions = new(ELeave) RPointerArray<CTransition>;
sl@0
    54
	_LIT(KConstructingUnitTestMsg, "Constructed Unit Test named %S");
sl@0
    55
	iDataLogger.LogInformationWithParameters(KConstructingUnitTestMsg, &iUnitTestName);
sl@0
    56
	}
sl@0
    57
sl@0
    58
sl@0
    59
CUnitTestInfo* CUnitTest::TransitionSetL() const
sl@0
    60
	{
sl@0
    61
	CUnitTestInfo* transitionSet = CUnitTestInfo::NewL(iUnitTestName);
sl@0
    62
	return transitionSet;
sl@0
    63
	}
sl@0
    64
sl@0
    65
sl@0
    66
EXPORT_C void CUnitTest::RunTest(TTimeIntervalMicroSeconds32 aTimeAfter /*= 0*/)
sl@0
    67
	{
sl@0
    68
	After(aTimeAfter);
sl@0
    69
	_LIT(KTxtSeparator, "-----------------------------------------------------------------------------------");
sl@0
    70
	_LIT(KStartingUnitTest, "Beginning Unit Test named %S");
sl@0
    71
	iDataLogger.LogInformation(KTxtSeparator);
sl@0
    72
	iDataLogger.LogInformationWithParameters(KStartingUnitTest, &iUnitTestName);
sl@0
    73
	iDataLogger.ReportInformationWithParameters(KStartingUnitTest, &iUnitTestName);
sl@0
    74
	}
sl@0
    75
sl@0
    76
EXPORT_C void CUnitTest::RunL()
sl@0
    77
	{
sl@0
    78
	_LIT(KUnitTestRunLPanic, "CUnitTest::RunL");
sl@0
    79
sl@0
    80
	TInt status = iStatus.Int();
sl@0
    81
	switch(status)
sl@0
    82
		{
sl@0
    83
		case (KTestBedRepeatTest):			/* A stub has requested repeat of the last test */
sl@0
    84
			// Go back one so that we repeat the last test
sl@0
    85
			--iNextTransitionIndex;
sl@0
    86
			break;
sl@0
    87
sl@0
    88
		case (KTestBedTestLeft):				/* The last transition's RunL left */
sl@0
    89
		case (KTestBedTestCancel):				/* The last transition was cancelled */
sl@0
    90
		case (KTestBedLeakTestLoopDetected):	/* A leak test detected an infinite loop */
sl@0
    91
		case (KTestBedFailedPreConditions):		/* The last transition failed it's pre conditions */
sl@0
    92
		case (KTestBedFailedPostConditions):	/* The last transition failed it's post conditions */
sl@0
    93
			// Go to the end of the test so that it finishes
sl@0
    94
			iNextTransitionIndex = iTransitions->Count();
sl@0
    95
			break;
sl@0
    96
sl@0
    97
		case (KTestBedAsynchronousTransition):	/* The last transition started an async request */
sl@0
    98
			// Remember that we have an outstanding request and then carry on
sl@0
    99
			iOutstandingTransitions->Append((*iTransitions)[iNextTransitionIndex - 1]);
sl@0
   100
			break;
sl@0
   101
sl@0
   102
		case (KErrNone):
sl@0
   103
			break;
sl@0
   104
sl@0
   105
		default:
sl@0
   106
			User::Panic(KUnitTestRunLPanic, KTestBedInvalidStatus);
sl@0
   107
		}
sl@0
   108
sl@0
   109
	// If we still have more transitions to run
sl@0
   110
	if(iNextTransitionIndex < iTransitions->Count())
sl@0
   111
		{
sl@0
   112
		iStatus = KRequestPending;
sl@0
   113
		SetActive();
sl@0
   114
sl@0
   115
		// If the next transition is a blocking one then wait for all outstanding async
sl@0
   116
		// requests to complete.  Otherwise just run the next transition
sl@0
   117
		if(((*iTransitions)[iNextTransitionIndex]->IsBlockingTransition()) && 
sl@0
   118
			(iOutstandingTransitions->Count() > 0))
sl@0
   119
			{
sl@0
   120
			iWaitingForCompletion = ETrue;
sl@0
   121
			}
sl@0
   122
		else
sl@0
   123
			{
sl@0
   124
			(*iTransitions)[iNextTransitionIndex]->RunTransition(&iStatus);
sl@0
   125
			++iNextTransitionIndex;
sl@0
   126
			}
sl@0
   127
		}
sl@0
   128
	else
sl@0
   129
		{
sl@0
   130
		// If we still have outstanding async requests then wait for these to complete
sl@0
   131
		// otherwise we have finished this test
sl@0
   132
		if(iOutstandingTransitions->Count() > 0)
sl@0
   133
			{
sl@0
   134
			iWaitingForCompletion = ETrue;
sl@0
   135
			iStatus = KRequestPending;
sl@0
   136
			SetActive();
sl@0
   137
			}
sl@0
   138
		else
sl@0
   139
			{
sl@0
   140
			iUnitTestObserver.Complete(this);
sl@0
   141
sl@0
   142
			_LIT(KInfoPrintFailed, "Failed: Unit Test");
sl@0
   143
			_LIT(KTestLeft, "Failed: Unit Test %S left");
sl@0
   144
			_LIT(KTestLeftWithUnexpectedError, "Failed: Test %S left with unexpected error");
sl@0
   145
			_LIT(KTestFailed, "Failed: Unit Test %S failed a pre/post condition validation check");
sl@0
   146
			_LIT(KTestLeftWithExpectedError, "Test %S left with an anticipated error");
sl@0
   147
			_LIT(KTestCancelled, "Cancelled: Unit Test Transition %S was cancelled");
sl@0
   148
			_LIT(KTestEnteredInfiniteLoop, "Unit Test Transition %S aborted (infinitely looping)");
sl@0
   149
			_LIT(KEndingUnitTest, "Successfully completed Unit Test %S");
sl@0
   150
			// We use RTest if it is present in the framework to validate
sl@0
   151
			// status codes for errors. Note: not all non KErrNone code mean 
sl@0
   152
			// there was an error and so we need to selective over which 
sl@0
   153
			// cases below we use RTest().
sl@0
   154
			switch(status)
sl@0
   155
				{
sl@0
   156
				case (KTestBedTestLeft):
sl@0
   157
					{
sl@0
   158
					TInt leaveCode = iCurrentlyExecutingTransition->GetErrorCode();
sl@0
   159
					//Check to see if the leave code is NOT on the list of known leaving codes
sl@0
   160
					if(iLeaveErrorArray.Find(leaveCode) == KErrNotFound)
sl@0
   161
						{
sl@0
   162
						iDataLogger.LogInformationWithParameters(KTestLeft, &iUnitTestName);
sl@0
   163
						iDataLogger.ReportInformationWithParameters(KTestLeft, &iUnitTestName);
sl@0
   164
						if(iRTest) 
sl@0
   165
							{
sl@0
   166
							(*iRTest)(status==KErrNone);
sl@0
   167
							}
sl@0
   168
						}
sl@0
   169
					else	//Leave code is on the list 
sl@0
   170
						{
sl@0
   171
						TInt count = iTransitions->Count();
sl@0
   172
						//Check transition number and if it is the last transition then this is an expected error
sl@0
   173
						CTransition* lastTransition = (*iTransitions)[count-1];
sl@0
   174
						if(iCurrentlyExecutingTransition == lastTransition)
sl@0
   175
							{
sl@0
   176
							iDataLogger.LogInformationWithParameters(KTestLeftWithExpectedError, &iUnitTestName);
sl@0
   177
							iDataLogger.ReportInformationWithParameters(KEndingUnitTest, &iUnitTestName);
sl@0
   178
							}
sl@0
   179
						else	//Otherwise, if not the last transition, the test failed with an unexpected error
sl@0
   180
							{
sl@0
   181
							User::InfoPrint(KInfoPrintFailed);
sl@0
   182
							User::InfoPrint(iUnitTestName);
sl@0
   183
							iDataLogger.LogInformationWithParameters(KTestLeftWithUnexpectedError, &iUnitTestName);
sl@0
   184
							iDataLogger.ReportInformationWithParameters(KTestLeftWithUnexpectedError, &iUnitTestName);
sl@0
   185
							if(iRTest) 
sl@0
   186
								{
sl@0
   187
								(*iRTest)(status==KErrNone);
sl@0
   188
								}
sl@0
   189
							}
sl@0
   190
						}
sl@0
   191
					}
sl@0
   192
					break;
sl@0
   193
sl@0
   194
				case (KTestBedFailedPreConditions):
sl@0
   195
				case (KTestBedFailedPostConditions):
sl@0
   196
					{
sl@0
   197
					User::InfoPrint(KInfoPrintFailed);
sl@0
   198
					User::InfoPrint(iUnitTestName);
sl@0
   199
					iDataLogger.LogInformationWithParameters(KTestFailed, &iUnitTestName);
sl@0
   200
					iDataLogger.ReportInformationWithParameters(KTestFailed, &iUnitTestName);
sl@0
   201
					if(iRTest) 
sl@0
   202
						{
sl@0
   203
						(*iRTest)(status==KErrNone);
sl@0
   204
						}
sl@0
   205
					}
sl@0
   206
					break;
sl@0
   207
sl@0
   208
				case (KTestBedTestCancel):
sl@0
   209
					iDataLogger.LogInformationWithParameters(KTestCancelled, &iUnitTestName);
sl@0
   210
					iDataLogger.ReportInformationWithParameters(KTestCancelled, &iUnitTestName);
sl@0
   211
					if(iRTest) 
sl@0
   212
						{
sl@0
   213
						(*iRTest)(status==KErrNone);
sl@0
   214
						}
sl@0
   215
					break;
sl@0
   216
sl@0
   217
				case (KTestBedLeakTestLoopDetected):
sl@0
   218
					iDataLogger.LogInformationWithParameters(KTestEnteredInfiniteLoop, &iUnitTestName);
sl@0
   219
					iDataLogger.ReportInformationWithParameters(KTestEnteredInfiniteLoop, &iUnitTestName);
sl@0
   220
					if(iRTest) 
sl@0
   221
						{
sl@0
   222
						(*iRTest)(status==KErrNone);
sl@0
   223
						}
sl@0
   224
					break;
sl@0
   225
sl@0
   226
				case (KErrNone):
sl@0
   227
					iDataLogger.LogInformationWithParameters(KEndingUnitTest, &iUnitTestName);
sl@0
   228
					iDataLogger.ReportInformationWithParameters(KEndingUnitTest, &iUnitTestName);
sl@0
   229
					break;
sl@0
   230
sl@0
   231
				default:
sl@0
   232
					User::Panic(KUnitTestRunLPanic, KTestBedInvalidStatus);
sl@0
   233
				}
sl@0
   234
			}
sl@0
   235
		}
sl@0
   236
	}
sl@0
   237
sl@0
   238
EXPORT_C void CUnitTest::AddTransitionL(CTransition* aTransition)
sl@0
   239
	{
sl@0
   240
	__ASSERT_DEBUG(aTransition, User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
sl@0
   241
	CleanupStack::PushL(aTransition);
sl@0
   242
	User::LeaveIfError(iTransitions->Append(aTransition));
sl@0
   243
	CleanupStack::Pop(aTransition);
sl@0
   244
	}
sl@0
   245
sl@0
   246
EXPORT_C void CUnitTest::AddBlockingTransitionL(CTransition* aTransition)
sl@0
   247
	{
sl@0
   248
	__ASSERT_DEBUG(aTransition, User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
sl@0
   249
	aTransition->SetBlockingTransition(ETrue);
sl@0
   250
	CleanupStack::PushL(aTransition);
sl@0
   251
	User::LeaveIfError(iTransitions->Append(aTransition));
sl@0
   252
	CleanupStack::Pop(aTransition);
sl@0
   253
	}
sl@0
   254
sl@0
   255
EXPORT_C void CUnitTest::AddLeaveErrorCodeL(TInt aLeaveErrorCode)
sl@0
   256
	{
sl@0
   257
	User::LeaveIfError(iLeaveErrorArray.Append(aLeaveErrorCode));
sl@0
   258
	}
sl@0
   259
sl@0
   260
EXPORT_C CTransition& CUnitTest::GetCurrentTransition() const
sl@0
   261
	{
sl@0
   262
	// Check fror a stray stub call
sl@0
   263
	// We will always have a valid pointer here IF called
sl@0
   264
	// from a stub in response to that transition's call 
sl@0
   265
	// on the stub's methods.
sl@0
   266
	__ASSERT_DEBUG(iCurrentlyExecutingTransition, User::Invariant());
sl@0
   267
	return *iCurrentlyExecutingTransition;
sl@0
   268
	}
sl@0
   269
sl@0
   270
EXPORT_C void CUnitTest::SetCurrentTransition(CTransition& aTransition)
sl@0
   271
	{
sl@0
   272
	iCurrentlyExecutingTransition = &aTransition;
sl@0
   273
	}
sl@0
   274
sl@0
   275
EXPORT_C void CUnitTest::Complete(CTransition& aTransition, TInt aAsyncPostCheckError)
sl@0
   276
	{
sl@0
   277
	// Should never be NULL at this point
sl@0
   278
	__ASSERT_DEBUG(iCurrentlyExecutingTransition, User::Invariant());
sl@0
   279
	if(iCurrentlyExecutingTransition == &aTransition)
sl@0
   280
		iCurrentlyExecutingTransition = NULL;	// Clear the current transition
sl@0
   281
sl@0
   282
	// Oops the code will crash if this is ever false
sl@0
   283
	__ASSERT_DEBUG(iOutstandingTransitions, User::Invariant());
sl@0
   284
sl@0
   285
	// Look-up the transition passed in...
sl@0
   286
	TInt index = iOutstandingTransitions->Find(&aTransition);
sl@0
   287
	if(index != KErrNotFound)
sl@0
   288
		{
sl@0
   289
		// ... and remove from the set of outstanding ones.
sl@0
   290
		iOutstandingTransitions->Remove(index);
sl@0
   291
sl@0
   292
		// Did we fail a second-phase post-condition validation on an asynchronous transition?
sl@0
   293
		// Or was it a normal transition completion?  Regardless, we go for another iteration 
sl@0
   294
		// of the AO, passing through the error code.
sl@0
   295
		TBool completeIt = (aAsyncPostCheckError != KErrNone);
sl@0
   296
		if(iWaitingForCompletion && (iOutstandingTransitions->Count() == 0))
sl@0
   297
			completeIt = ETrue;
sl@0
   298
sl@0
   299
		if (completeIt)
sl@0
   300
			{
sl@0
   301
			TRequestStatus* status = &iStatus;
sl@0
   302
			User::RequestComplete(status, aAsyncPostCheckError);
sl@0
   303
			}
sl@0
   304
		}
sl@0
   305
	else
sl@0
   306
		{
sl@0
   307
		__ASSERT_DEBUG(ETrue, 
sl@0
   308
			User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
sl@0
   309
		}
sl@0
   310
	}
sl@0
   311
sl@0
   312
EXPORT_C void CUnitTest::SetParametersL(TAny* /*aParams*/)
sl@0
   313
	{
sl@0
   314
	// Do nothing
sl@0
   315
	}
sl@0
   316
sl@0
   317
EXPORT_C void CUnitTest::DoCancel()
sl@0
   318
	{
sl@0
   319
	CTimer::DoCancel();
sl@0
   320
sl@0
   321
	if(iCurrentlyExecutingTransition)
sl@0
   322
		iCurrentlyExecutingTransition->Cancel();
sl@0
   323
sl@0
   324
	// Cancel any outstanding asynchronous transitions
sl@0
   325
	if(iOutstandingTransitions)
sl@0
   326
		{
sl@0
   327
		TInt count = iOutstandingTransitions->Count();
sl@0
   328
		for(TInt index = 0; index < count; ++index)
sl@0
   329
			{
sl@0
   330
			(*iOutstandingTransitions)[index]->Cancel();
sl@0
   331
			}
sl@0
   332
		}
sl@0
   333
sl@0
   334
	iUnitTestObserver.Complete(this);
sl@0
   335
	}
sl@0
   336