lowlevellibsandfws/pluginfw/Test_Bed/test_bed/TestManager.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TestManager.h"
       
    17 
       
    18 
       
    19 CTestManager::CTestManager(RPointerArray<CComponentInfo>* aTestList, 
       
    20 						   CDataLogger& aDataLogger,
       
    21 						   MManagerObserver& aObserver,
       
    22 						   RTest* aRTest)
       
    23 : CActive(CActive::EPriorityStandard),
       
    24 iObserver(aObserver),
       
    25 iTestList(aTestList),
       
    26 iDataLogger(aDataLogger),
       
    27 iRTest(aRTest)
       
    28 	{
       
    29 	}
       
    30 
       
    31 
       
    32 CTestManager* CTestManager::NewL(RPointerArray<CComponentInfo>* aTestList, 
       
    33 								 CDataLogger& aDataLogger,
       
    34 								 MManagerObserver& aObserver,
       
    35 								 RTest* aRTest)
       
    36 	{
       
    37 	CTestManager* self = new (ELeave) CTestManager(aTestList, aDataLogger, aObserver, aRTest);
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop();
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 
       
    45 void CTestManager::ConstructL()
       
    46 	{
       
    47 	CActiveScheduler::Add(this);
       
    48 
       
    49 	// Say we are stopping just in case RunTests never gets called
       
    50 	// - if it does get called this will get unset
       
    51 	iAmStopping = ETrue;
       
    52 	}
       
    53 
       
    54 
       
    55 CTestManager::~CTestManager()
       
    56 	{
       
    57 	Cancel();
       
    58 
       
    59 	delete iCurrentTester;
       
    60 	}
       
    61 
       
    62 
       
    63 void CTestManager::RunL()
       
    64 	{
       
    65 	delete iCurrentTester;
       
    66 	iCurrentTester = NULL;
       
    67 
       
    68 	if((iCurrentTestLoad < iTestList->Count()) && !iAmStopping)
       
    69 		{
       
    70 		iStatus = KRequestPending;
       
    71 		SetActive();
       
    72 
       
    73 		TestComponentL(iCurrentTestLoad);
       
    74 		
       
    75 		// Next time run the next test
       
    76 		++iCurrentTestLoad;
       
    77 		// Set the flag for the next state.
       
    78 		iAmStopping = iCurrentTestLoad == iTestList->Count();
       
    79 		}
       
    80 	else if(iAmStopping)
       
    81 		iObserver.TestsComplete();
       
    82 	}
       
    83 
       
    84 TInt CTestManager::RunError(TInt /*aErrorCode*/)
       
    85 	{
       
    86 	// Do nothing because anything that needs to be cleaned up should be on the cleanup
       
    87 	// stack. We want any remaining tests to carry on.
       
    88 	return KErrNone;
       
    89 	}
       
    90 
       
    91 
       
    92 void CTestManager::DoCancel()
       
    93 	{
       
    94 	_LIT(KTestsCancelled,"TestBed cancelled at user request.");
       
    95 	iDataLogger.LogInformation(KTestsCancelled());
       
    96 	iDataLogger.ReportInformation(KTestsCancelled());
       
    97 
       
    98 	delete iCurrentTester;
       
    99 	iCurrentTester = NULL;
       
   100 
       
   101 	iObserver.TestsComplete();
       
   102 	}
       
   103 
       
   104 
       
   105 void CTestManager::RunTests(RPointerArray<TTestInfo>* aTests)
       
   106 	{
       
   107 	iTestsToRun = aTests;
       
   108 
       
   109 	if(iTestList->Count() >0)
       
   110 		iAmStopping = EFalse;
       
   111 	else
       
   112 		{
       
   113 		// If someone tried to call RunTests when there are no tests
       
   114 		// complete immediately
       
   115 		TRequestStatus* status = &iStatus;
       
   116 		User::RequestComplete(status, KErrNone);
       
   117 		}
       
   118 
       
   119 	if(!IsActive())
       
   120 		{
       
   121 		SetActive();
       
   122 		if(!iAmStopping)
       
   123 			{
       
   124 			TRequestStatus* status = &iStatus;
       
   125 			User::RequestComplete(status, KErrNone);
       
   126 			}
       
   127 		else
       
   128 			iStatus = KRequestPending;
       
   129 		}
       
   130 	}
       
   131 
       
   132 /**
       
   133 	@fn				CleanupTestArray(TAny* aArray)
       
   134 	Intended Useage:The CleanupTestArray method is used for cleanup support 
       
   135 					of locally declared arrays
       
   136 	@internalComponent
       
   137 	@since			7.0
       
   138 	@param			aArray is the array whose contents should be destroyed
       
   139 */
       
   140 static void CleanupTestArray(TAny* aArray)
       
   141 	{
       
   142 	// Whilst this array is an RPointerArray, it does not own the pointers
       
   143 	// and therefor should not destroy them
       
   144 	// This should be changed to an RArray
       
   145 	RPointerArray<TTestInfo>* array = REINTERPRET_CAST(RPointerArray<TTestInfo>*, aArray);
       
   146 	array->Reset();
       
   147 	delete array;
       
   148 	}
       
   149 
       
   150 
       
   151 void CTestManager::Complete(CComponentTester* /*aTester*/, TInt /*aUnitTestId*/)
       
   152 	{
       
   153 	TRequestStatus* status = &iStatus;
       
   154 	User::RequestComplete(status, KErrNone);
       
   155 	}
       
   156 
       
   157 void CTestManager::TestComponentL(TInt aComponentIndex)
       
   158 	{
       
   159 	// This should be changed to an RArray<TTestInfo*> and be typedefd
       
   160 	RPointerArray<TTestInfo>* tests = NULL;
       
   161 	if(iTestsToRun != NULL)
       
   162 		{
       
   163 		tests = new(ELeave) RPointerArray<TTestInfo>;
       
   164 		TCleanupItem cleanup(CleanupTestArray, tests);
       
   165 		CleanupStack::PushL(cleanup);
       
   166 		// Work out which tests to run
       
   167 		for(TInt index = 0; index < iTestsToRun->Count(); ++index)
       
   168 			{
       
   169 			if((*iTestsToRun)[index]->iComponentId == aComponentIndex)
       
   170 				User::LeaveIfError(tests->Append((*iTestsToRun)[index]));
       
   171 			}
       
   172 		if(tests->Count() == 0)
       
   173 			{
       
   174 			CleanupStack::PopAndDestroy();	// cleanup
       
   175 			Complete(NULL, 0);
       
   176 			return;
       
   177 			}
       
   178 		}
       
   179 	// Create the EXEs derived CComponentTester for this test iteration.
       
   180 	ComponentTesterInitialiserLC createLC = (*iTestList)[aComponentIndex]->GlobalEntryFunc();
       
   181 	iCurrentTester= createLC(iDataLogger, *this);
       
   182 	CleanupStack::Pop(iCurrentTester);
       
   183 	iCurrentTester->SetRTest(iRTest);
       
   184 
       
   185 	if(iTestsToRun != NULL)
       
   186 		CleanupStack::Pop();				// cleanup
       
   187 	// Execute unit tests for the current component
       
   188 	iCurrentTester->TestComponent(tests);
       
   189 	}
       
   190 
       
   191 TBool CTestManager::StartedTests() const
       
   192 	{
       
   193 	return iCurrentTestLoad > 0;
       
   194 	}
       
   195