os/ossrv/lowlevellibsandfws/pluginfw/Test_Bed/test_bed/ComponentTester.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/pluginfw/Test_Bed/test_bed/ComponentTester.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,205 @@
     1.4 +// Copyright (c) 1997-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 "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 +//
    1.18 +
    1.19 +#include "ComponentTester.h"
    1.20 +#include <ecom/test_bed/datalogger.h>
    1.21 +#include <ecom/test_bed/testbeddefinitions.h>
    1.22 +
    1.23 +EXPORT_C CComponentTester::CComponentTester(CDataLogger&			aDataLogger, 
    1.24 +											MComponentTestObserver& aObserver)
    1.25 +: CActive(CActive::EPriorityStandard),
    1.26 +iDataLogger(aDataLogger),
    1.27 +iObserver(aObserver)
    1.28 +	{
    1.29 +	CActiveScheduler::Add(this);
    1.30 +	}
    1.31 +
    1.32 +
    1.33 +EXPORT_C CComponentTester::~CComponentTester()
    1.34 +	{
    1.35 +	Cancel();
    1.36 +
    1.37 +	// This should only be true during if TransitionSetsL() left and we are being
    1.38 +	// destroyed as part of cleanup
    1.39 +	if(iTransitionSets)
    1.40 +		{
    1.41 +		iTransitionSets->ResetAndDestroy();
    1.42 +		delete iTransitionSets;
    1.43 +		}
    1.44 +	iParameterizedTests.Reset();
    1.45 +	if(iUnitTests)
    1.46 +		{
    1.47 +		iUnitTests->ResetAndDestroy();
    1.48 +		delete iUnitTests;
    1.49 +		}
    1.50 +	if(iUnitTestsToRun)
    1.51 +		{
    1.52 +		// We own the list but not the things on it.  So reset the list and delete it
    1.53 +		iUnitTestsToRun->Reset();
    1.54 +		delete iUnitTestsToRun;
    1.55 +		}
    1.56 +	}
    1.57 +
    1.58 +EXPORT_C void CComponentTester::ComponentTesterConstructL()
    1.59 +	{
    1.60 +	iUnitTests = new(ELeave) RPointerArray<CUnitTest>;
    1.61 +	}
    1.62 +
    1.63 +
    1.64 +EXPORT_C RPointerArray<CUnitTestInfo>* CComponentTester::TransitionSetsL() const
    1.65 +	{
    1.66 +	// Create the array in a member variable to ensure correct cleanup but we do not
    1.67 +	// own this object.  Ownership is passed at the return
    1.68 +	iTransitionSets = new(ELeave) RPointerArray<CUnitTestInfo>;
    1.69 +
    1.70 +	if(iUnitTests)
    1.71 +		{
    1.72 +		TInt numTests = iUnitTests->Count();
    1.73 +		for(TInt index = 0; index < numTests; index++)
    1.74 +			{
    1.75 +			CUnitTestInfo* newSet = (*iUnitTests)[index]->TransitionSetL();
    1.76 +			CleanupStack::PushL(newSet);
    1.77 +			User::LeaveIfError(iTransitionSets->Append(newSet));
    1.78 +			CleanupStack::Pop(newSet); // now owned by iTransitionSets
    1.79 +			}
    1.80 +		}
    1.81 +
    1.82 +	// Return the pointer and null our member variable because we don't own it
    1.83 +	RPointerArray<CUnitTestInfo>* transitionSets = iTransitionSets;
    1.84 +	iTransitionSets = 0;
    1.85 +	return transitionSets;
    1.86 +	}
    1.87 +
    1.88 +EXPORT_C void CComponentTester::Complete(CUnitTest* aUnitTest)
    1.89 +	{
    1.90 +	if(iUnitTestsToRun != 0)
    1.91 +		{
    1.92 +		if(iCurrentUnitTest == iUnitTestsToRun->Count())
    1.93 +			{
    1.94 +			iObserver.Complete(this, iUnitTests->Find(aUnitTest));
    1.95 +			}
    1.96 +		else
    1.97 +			{
    1.98 +			TRequestStatus* status = &iStatus;
    1.99 +			User::RequestComplete(status, KErrNone);
   1.100 +			}
   1.101 +		}
   1.102 +	else
   1.103 +		{
   1.104 +		if(iCurrentUnitTest == iUnitTests->Count())
   1.105 +			{
   1.106 +			iObserver.Complete(this, iUnitTests->Find(aUnitTest));
   1.107 +			}
   1.108 +		else
   1.109 +			{
   1.110 +			TRequestStatus* status = &iStatus;
   1.111 +			User::RequestComplete(status, KErrNone);
   1.112 +			}
   1.113 +		}
   1.114 +	}
   1.115 +
   1.116 +EXPORT_C void CComponentTester::AddUnitTestL(const CUnitTest* aUnitTest)
   1.117 +	{
   1.118 +	CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest);
   1.119 +	CleanupStack::PushL(thisTest);
   1.120 +	User::LeaveIfError(iUnitTests->Append(thisTest));
   1.121 +	CleanupStack::Pop(thisTest);
   1.122 +	}
   1.123 +
   1.124 +EXPORT_C void CComponentTester::AddParamUnitTestL(const CUnitTest* aUnitTest)
   1.125 +	{
   1.126 +	CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest);
   1.127 +	CleanupStack::PushL(thisTest);
   1.128 +	User::LeaveIfError(iUnitTests->Append(thisTest));
   1.129 +	CleanupStack::Pop(thisTest);
   1.130 +	TInt testId = iUnitTests->Find(thisTest);
   1.131 +	User::LeaveIfError(iParameterizedTests.Append(testId));
   1.132 +	}
   1.133 +
   1.134 +EXPORT_C void CComponentTester::TestComponent(RPointerArray<TTestInfo>* aTests)
   1.135 +	{
   1.136 +	_LIT(KStartingTestMessage, "Starting CComponentTester::TestComponent()...");
   1.137 +	iDataLogger.LogInformation(KStartingTestMessage);
   1.138 +
   1.139 +	iUnitTestsToRun = aTests;
   1.140 +
   1.141 +	SetActive();
   1.142 +	TRequestStatus* status = &iStatus;
   1.143 +	User::RequestComplete(status, KErrNone);
   1.144 +	}
   1.145 +
   1.146 +EXPORT_C void CComponentTester::RunL()
   1.147 +	{
   1.148 +	TBool lastTestRun = EFalse;
   1.149 +	if(iUnitTestsToRun == 0)
   1.150 +		{
   1.151 +		TBool startFromFirst = iCurrentUnitTest == 0;
   1.152 +		TBool haveRunTest = EFalse;
   1.153 +		while((iCurrentUnitTest < iUnitTests->Count()) && !haveRunTest)
   1.154 +			{
   1.155 +			if(iParameterizedTests.Find(iCurrentUnitTest) == -1)
   1.156 +				{
   1.157 +				(*iUnitTests)[iCurrentUnitTest]->PrepareUnitTestL();
   1.158 +				(*iUnitTests)[iCurrentUnitTest]->RunTest(0);
   1.159 +				haveRunTest = ETrue;
   1.160 +				}
   1.161 +			++iCurrentUnitTest;
   1.162 +			lastTestRun = iCurrentUnitTest == iUnitTests->Count();
   1.163 +			}
   1.164 +		if(startFromFirst && !haveRunTest)
   1.165 +			iObserver.Complete(this, KErrNotFound);
   1.166 +		}
   1.167 +	else
   1.168 +		{
   1.169 +		if(iCurrentUnitTest < iUnitTestsToRun->Count())
   1.170 +			{
   1.171 +			TInt testToRun = (*iUnitTestsToRun)[iCurrentUnitTest]->iUnitTestId;
   1.172 +			TTimeIntervalMicroSeconds32 time = (*iUnitTestsToRun)[iCurrentUnitTest]->iRunTime;
   1.173 +			(*iUnitTests)[testToRun]->SetParametersL((*iUnitTestsToRun)[iCurrentUnitTest]->iParameters);
   1.174 +			(*iUnitTests)[testToRun]->PrepareUnitTestL();
   1.175 +			(*iUnitTests)[testToRun]->RunTest(time);
   1.176 +			++iCurrentUnitTest;
   1.177 +			lastTestRun = iCurrentUnitTest == iUnitTestsToRun->Count();
   1.178 +			}
   1.179 +		}
   1.180 +
   1.181 +	// We don't need to be active for the last test because we don't RunL again
   1.182 +	if(!lastTestRun)
   1.183 +		{
   1.184 +		iStatus = KRequestPending;
   1.185 +		SetActive();
   1.186 +		}
   1.187 +	}
   1.188 +
   1.189 +EXPORT_C void CComponentTester::DoCancel()
   1.190 +	{
   1.191 +	// If we have started a test then we have already advanced iCurrentUnitTest so cancel 
   1.192 +	// the previous test
   1.193 +	if(iCurrentUnitTest > 0)
   1.194 +		(*iUnitTests)[iCurrentUnitTest - 1]->Cancel();
   1.195 +
   1.196 +	iObserver.Complete(this, KTestBedTestCancel);
   1.197 +	}
   1.198 +
   1.199 +EXPORT_C void CComponentTester::SetRTest(RTest* aRTest)
   1.200 +	{
   1.201 +	// Record a handle on the RTest object to use in component testing.
   1.202 +	iRTest = aRTest;
   1.203 +
   1.204 +	// We have a new RTest, best tell the unit tests we know about
   1.205 +	for (int ut=0; ut < iUnitTests->Count(); ut++)
   1.206 +		(*iUnitTests)[ut]->SetRTest(aRTest);
   1.207 +	}
   1.208 +