os/ossrv/lowlevellibsandfws/pluginfw/Test_Bed/test_bed/ComponentTester.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) 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
//
sl@0
    15
sl@0
    16
#include "ComponentTester.h"
sl@0
    17
#include <ecom/test_bed/datalogger.h>
sl@0
    18
#include <ecom/test_bed/testbeddefinitions.h>
sl@0
    19
sl@0
    20
EXPORT_C CComponentTester::CComponentTester(CDataLogger&			aDataLogger, 
sl@0
    21
											MComponentTestObserver& aObserver)
sl@0
    22
: CActive(CActive::EPriorityStandard),
sl@0
    23
iDataLogger(aDataLogger),
sl@0
    24
iObserver(aObserver)
sl@0
    25
	{
sl@0
    26
	CActiveScheduler::Add(this);
sl@0
    27
	}
sl@0
    28
sl@0
    29
sl@0
    30
EXPORT_C CComponentTester::~CComponentTester()
sl@0
    31
	{
sl@0
    32
	Cancel();
sl@0
    33
sl@0
    34
	// This should only be true during if TransitionSetsL() left and we are being
sl@0
    35
	// destroyed as part of cleanup
sl@0
    36
	if(iTransitionSets)
sl@0
    37
		{
sl@0
    38
		iTransitionSets->ResetAndDestroy();
sl@0
    39
		delete iTransitionSets;
sl@0
    40
		}
sl@0
    41
	iParameterizedTests.Reset();
sl@0
    42
	if(iUnitTests)
sl@0
    43
		{
sl@0
    44
		iUnitTests->ResetAndDestroy();
sl@0
    45
		delete iUnitTests;
sl@0
    46
		}
sl@0
    47
	if(iUnitTestsToRun)
sl@0
    48
		{
sl@0
    49
		// We own the list but not the things on it.  So reset the list and delete it
sl@0
    50
		iUnitTestsToRun->Reset();
sl@0
    51
		delete iUnitTestsToRun;
sl@0
    52
		}
sl@0
    53
	}
sl@0
    54
sl@0
    55
EXPORT_C void CComponentTester::ComponentTesterConstructL()
sl@0
    56
	{
sl@0
    57
	iUnitTests = new(ELeave) RPointerArray<CUnitTest>;
sl@0
    58
	}
sl@0
    59
sl@0
    60
sl@0
    61
EXPORT_C RPointerArray<CUnitTestInfo>* CComponentTester::TransitionSetsL() const
sl@0
    62
	{
sl@0
    63
	// Create the array in a member variable to ensure correct cleanup but we do not
sl@0
    64
	// own this object.  Ownership is passed at the return
sl@0
    65
	iTransitionSets = new(ELeave) RPointerArray<CUnitTestInfo>;
sl@0
    66
sl@0
    67
	if(iUnitTests)
sl@0
    68
		{
sl@0
    69
		TInt numTests = iUnitTests->Count();
sl@0
    70
		for(TInt index = 0; index < numTests; index++)
sl@0
    71
			{
sl@0
    72
			CUnitTestInfo* newSet = (*iUnitTests)[index]->TransitionSetL();
sl@0
    73
			CleanupStack::PushL(newSet);
sl@0
    74
			User::LeaveIfError(iTransitionSets->Append(newSet));
sl@0
    75
			CleanupStack::Pop(newSet); // now owned by iTransitionSets
sl@0
    76
			}
sl@0
    77
		}
sl@0
    78
sl@0
    79
	// Return the pointer and null our member variable because we don't own it
sl@0
    80
	RPointerArray<CUnitTestInfo>* transitionSets = iTransitionSets;
sl@0
    81
	iTransitionSets = 0;
sl@0
    82
	return transitionSets;
sl@0
    83
	}
sl@0
    84
sl@0
    85
EXPORT_C void CComponentTester::Complete(CUnitTest* aUnitTest)
sl@0
    86
	{
sl@0
    87
	if(iUnitTestsToRun != 0)
sl@0
    88
		{
sl@0
    89
		if(iCurrentUnitTest == iUnitTestsToRun->Count())
sl@0
    90
			{
sl@0
    91
			iObserver.Complete(this, iUnitTests->Find(aUnitTest));
sl@0
    92
			}
sl@0
    93
		else
sl@0
    94
			{
sl@0
    95
			TRequestStatus* status = &iStatus;
sl@0
    96
			User::RequestComplete(status, KErrNone);
sl@0
    97
			}
sl@0
    98
		}
sl@0
    99
	else
sl@0
   100
		{
sl@0
   101
		if(iCurrentUnitTest == iUnitTests->Count())
sl@0
   102
			{
sl@0
   103
			iObserver.Complete(this, iUnitTests->Find(aUnitTest));
sl@0
   104
			}
sl@0
   105
		else
sl@0
   106
			{
sl@0
   107
			TRequestStatus* status = &iStatus;
sl@0
   108
			User::RequestComplete(status, KErrNone);
sl@0
   109
			}
sl@0
   110
		}
sl@0
   111
	}
sl@0
   112
sl@0
   113
EXPORT_C void CComponentTester::AddUnitTestL(const CUnitTest* aUnitTest)
sl@0
   114
	{
sl@0
   115
	CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest);
sl@0
   116
	CleanupStack::PushL(thisTest);
sl@0
   117
	User::LeaveIfError(iUnitTests->Append(thisTest));
sl@0
   118
	CleanupStack::Pop(thisTest);
sl@0
   119
	}
sl@0
   120
sl@0
   121
EXPORT_C void CComponentTester::AddParamUnitTestL(const CUnitTest* aUnitTest)
sl@0
   122
	{
sl@0
   123
	CUnitTest* thisTest = CONST_CAST(CUnitTest*, aUnitTest);
sl@0
   124
	CleanupStack::PushL(thisTest);
sl@0
   125
	User::LeaveIfError(iUnitTests->Append(thisTest));
sl@0
   126
	CleanupStack::Pop(thisTest);
sl@0
   127
	TInt testId = iUnitTests->Find(thisTest);
sl@0
   128
	User::LeaveIfError(iParameterizedTests.Append(testId));
sl@0
   129
	}
sl@0
   130
sl@0
   131
EXPORT_C void CComponentTester::TestComponent(RPointerArray<TTestInfo>* aTests)
sl@0
   132
	{
sl@0
   133
	_LIT(KStartingTestMessage, "Starting CComponentTester::TestComponent()...");
sl@0
   134
	iDataLogger.LogInformation(KStartingTestMessage);
sl@0
   135
sl@0
   136
	iUnitTestsToRun = aTests;
sl@0
   137
sl@0
   138
	SetActive();
sl@0
   139
	TRequestStatus* status = &iStatus;
sl@0
   140
	User::RequestComplete(status, KErrNone);
sl@0
   141
	}
sl@0
   142
sl@0
   143
EXPORT_C void CComponentTester::RunL()
sl@0
   144
	{
sl@0
   145
	TBool lastTestRun = EFalse;
sl@0
   146
	if(iUnitTestsToRun == 0)
sl@0
   147
		{
sl@0
   148
		TBool startFromFirst = iCurrentUnitTest == 0;
sl@0
   149
		TBool haveRunTest = EFalse;
sl@0
   150
		while((iCurrentUnitTest < iUnitTests->Count()) && !haveRunTest)
sl@0
   151
			{
sl@0
   152
			if(iParameterizedTests.Find(iCurrentUnitTest) == -1)
sl@0
   153
				{
sl@0
   154
				(*iUnitTests)[iCurrentUnitTest]->PrepareUnitTestL();
sl@0
   155
				(*iUnitTests)[iCurrentUnitTest]->RunTest(0);
sl@0
   156
				haveRunTest = ETrue;
sl@0
   157
				}
sl@0
   158
			++iCurrentUnitTest;
sl@0
   159
			lastTestRun = iCurrentUnitTest == iUnitTests->Count();
sl@0
   160
			}
sl@0
   161
		if(startFromFirst && !haveRunTest)
sl@0
   162
			iObserver.Complete(this, KErrNotFound);
sl@0
   163
		}
sl@0
   164
	else
sl@0
   165
		{
sl@0
   166
		if(iCurrentUnitTest < iUnitTestsToRun->Count())
sl@0
   167
			{
sl@0
   168
			TInt testToRun = (*iUnitTestsToRun)[iCurrentUnitTest]->iUnitTestId;
sl@0
   169
			TTimeIntervalMicroSeconds32 time = (*iUnitTestsToRun)[iCurrentUnitTest]->iRunTime;
sl@0
   170
			(*iUnitTests)[testToRun]->SetParametersL((*iUnitTestsToRun)[iCurrentUnitTest]->iParameters);
sl@0
   171
			(*iUnitTests)[testToRun]->PrepareUnitTestL();
sl@0
   172
			(*iUnitTests)[testToRun]->RunTest(time);
sl@0
   173
			++iCurrentUnitTest;
sl@0
   174
			lastTestRun = iCurrentUnitTest == iUnitTestsToRun->Count();
sl@0
   175
			}
sl@0
   176
		}
sl@0
   177
sl@0
   178
	// We don't need to be active for the last test because we don't RunL again
sl@0
   179
	if(!lastTestRun)
sl@0
   180
		{
sl@0
   181
		iStatus = KRequestPending;
sl@0
   182
		SetActive();
sl@0
   183
		}
sl@0
   184
	}
sl@0
   185
sl@0
   186
EXPORT_C void CComponentTester::DoCancel()
sl@0
   187
	{
sl@0
   188
	// If we have started a test then we have already advanced iCurrentUnitTest so cancel 
sl@0
   189
	// the previous test
sl@0
   190
	if(iCurrentUnitTest > 0)
sl@0
   191
		(*iUnitTests)[iCurrentUnitTest - 1]->Cancel();
sl@0
   192
sl@0
   193
	iObserver.Complete(this, KTestBedTestCancel);
sl@0
   194
	}
sl@0
   195
sl@0
   196
EXPORT_C void CComponentTester::SetRTest(RTest* aRTest)
sl@0
   197
	{
sl@0
   198
	// Record a handle on the RTest object to use in component testing.
sl@0
   199
	iRTest = aRTest;
sl@0
   200
sl@0
   201
	// We have a new RTest, best tell the unit tests we know about
sl@0
   202
	for (int ut=0; ut < iUnitTests->Count(); ut++)
sl@0
   203
		(*iUnitTests)[ut]->SetRTest(aRTest);
sl@0
   204
	}
sl@0
   205