os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcasefactory.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcasefactory.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,207 @@
     1.4 +// Copyright (c) 2007-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 the License "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 +// @internalComponent
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32std.h>
    1.22 +#include <e32std_private.h>
    1.23 +#include <u32std.h> 	// unicode builds
    1.24 +#include <e32base.h>
    1.25 +#include <e32base_private.h>
    1.26 +#include <e32cons.h>
    1.27 +#include <e32Test.h>	// RTest headder
    1.28 +#include <e32def.h>
    1.29 +#include <e32def_private.h>
    1.30 +#include "TestCaseFactory.h"
    1.31 +#include "debugmacros.h"
    1.32 +
    1.33 +
    1.34 +	
    1.35 +RTestFactory& RTestFactory::Instance()
    1.36 +	{
    1.37 +	static RTestFactory singleton;
    1.38 +	return singleton;
    1.39 +	}
    1.40 +	
    1.41 +	
    1.42 +RTestFactory::~RTestFactory()
    1.43 +	{
    1.44 +	}
    1.45 +	
    1.46 +	
    1.47 +RTestFactory::RTestFactory()
    1.48 +:	iTestCases(TStringIdentity::Hash,TStringIdentity::Id)
    1.49 +	{
    1.50 +	}
    1.51 +	
    1.52 +	
    1.53 +void RTestFactory::RegisterTestCase(const TDesC& aTestCaseId,TCreationMethod aCreationMethod)
    1.54 +	{
    1.55 +	//LOG_FUNC
    1.56 +	TStringIdentity key(aTestCaseId);
    1.57 +	TInt err(Instance().iTestCases.Insert(key,aCreationMethod));
    1.58 +	if (err != KErrNone)
    1.59 +		{
    1.60 +		// Log that a test case could not be registered due to err
    1.61 +		RDebug::Print(_L("Test case '%S' could not be registered with test case factory"),&aTestCaseId);
    1.62 +		}
    1.63 +	else
    1.64 +		{
    1.65 +		RTestFactory::TCreationMethod* creatorFunction = Instance().iTestCases.Find(key);
    1.66 +		if (creatorFunction == NULL)
    1.67 +			{
    1.68 +			RDebug::Print(_L("<Error> Test case '%S' did not register"),&aTestCaseId);
    1.69 +			ListRegisteredTestCases();
    1.70 +			}
    1.71 +		else
    1.72 +			{
    1.73 +			RDebug::Print(_L("Test case '%S' registered in factory"),&aTestCaseId);
    1.74 +			}
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +
    1.79 +TBool RTestFactory::TestCaseExists(const TDesC& aTestCaseId)
    1.80 +	{
    1.81 +	RTestFactory::TCreationMethod creatorFunction;
    1.82 +	TStringIdentity key(aTestCaseId);
    1.83 +	
    1.84 +	creatorFunction = REINTERPRET_CAST(RTestFactory::TCreationMethod, Instance().iTestCases.Find(key));
    1.85 +	return (NULL != creatorFunction);
    1.86 +	}
    1.87 +
    1.88 +
    1.89 +/* Returns the test ID (name) of test at offset aIndex in the MAP
    1.90 + */
    1.91 +void RTestFactory::GetTestID(TInt aIndex, TBuf<KTestCaseIdLength> &aTestID)
    1.92 +	{
    1.93 +	LOG_FUNC
    1.94 +
    1.95 +	RFactoryMap::TIter it(Instance().iTestCases);
    1.96 +	
    1.97 +	TInt count(0);
    1.98 +	for (count=0; count<Instance().iTestCases.Count(); count++)
    1.99 +		{
   1.100 +		it.NextKey();
   1.101 +		if (count == aIndex)
   1.102 +			{
   1.103 +			TStringIdentity k(*it.CurrentKey());
   1.104 +			TBuf<64> *p = REINTERPRET_CAST(TBuf<64>*, &k);
   1.105 +			
   1.106 +			aTestID.Copy( *p);	
   1.107 +			return;
   1.108 +			}
   1.109 +		}
   1.110 +	// Error: Case not found!
   1.111 +	User::Leave(-2);
   1.112 +	}
   1.113 +	
   1.114 +
   1.115 +/* Return the ordinal value of the Test ID (numeric portion)
   1.116 + */
   1.117 +TInt TestIDValue(const TDesC & aTestID)
   1.118 +	{
   1.119 +	TUint16 value;
   1.120 +	TBuf<KTestCaseIdLength> id;
   1.121 +	id = aTestID.Right(4);
   1.122 +	TLex  lex(id);
   1.123 +	if (KErrNone == lex.Val(value, EDecimal))
   1.124 +		return(value);
   1.125 +	return(-1);	
   1.126 +	}
   1.127 +
   1.128 +/* Print the test IDs in numerical order
   1.129 + * Returns a sorted array of strings containing all of the test IDs
   1.130 + */	
   1.131 +void RTestFactory::ListRegisteredTestCases(RPointerArray<HBufC> & aTestCaseNameArr)
   1.132 +	{
   1.133 +	LOG_FUNC
   1.134 +	RFactoryMap::TIter it(Instance().iTestCases);
   1.135 +	TInt count(0);
   1.136 +	TInt cases(Instance().iTestCases.Count());
   1.137 +	
   1.138 +	test.Printf(_L("------ F A C T O R Y -------\n"));
   1.139 +	
   1.140 +	it.Reset();
   1.141 +	for (count=0; count<Instance().iTestCases.Count(); count++)
   1.142 +		{
   1.143 +		TStringIdentity k(*it.NextKey());
   1.144 +		TBuf<KTestCaseIdLength> *p = REINTERPRET_CAST(TBuf<KTestCaseIdLength>*, &k); // pointer to the test ID
   1.145 +		TBool placed(EFalse);
   1.146 +		TInt  pos(0);
   1.147 +		TInt  val(0);
   1.148 +		
   1.149 +		// build the sorted list
   1.150 +		while (!placed)
   1.151 +			{
   1.152 +			val = TestIDValue(*p);
   1.153 +			if (aTestCaseNameArr.Count()==pos) 
   1.154 +				{ //array empty or reached end
   1.155 +				HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
   1.156 +				*testIdentity = *p;
   1.157 +				aTestCaseNameArr.Append(testIdentity);
   1.158 +				CleanupStack::Pop(testIdentity);
   1.159 +				placed = ETrue;
   1.160 +				}
   1.161 +			else
   1.162 +				{
   1.163 +				if ( val < TestIDValue(*aTestCaseNameArr[pos]) )
   1.164 +					{
   1.165 +					HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
   1.166 +					*testIdentity = *p;
   1.167 +					aTestCaseNameArr.Insert(testIdentity, pos);
   1.168 +					placed = ETrue;
   1.169 +					CleanupStack::Pop(testIdentity);
   1.170 +					}
   1.171 +				else
   1.172 +					{
   1.173 +					pos++;
   1.174 +					}
   1.175 +				}
   1.176 +			}
   1.177 +		}
   1.178 +	// print it
   1.179 +	for (count=0; count<aTestCaseNameArr.Count(); count++)
   1.180 +		{
   1.181 +		test.Printf(_L("% 2d: %S\n"), count, aTestCaseNameArr[count]);
   1.182 +		}
   1.183 +	
   1.184 +	test.Printf(_L("----------------------------\n"));
   1.185 +	}
   1.186 +
   1.187 +
   1.188 +CTestCaseRoot* RTestFactory::CreateTestCaseL(const TDesC& aTestCaseId)
   1.189 +	{
   1.190 +	LOG_FUNC
   1.191 +	RTestFactory::TCreationMethod creatorFunction = NULL;
   1.192 +	TInt err(KErrNone);
   1.193 +	TStringIdentity key(aTestCaseId);
   1.194 +	
   1.195 +	TRAP(err,creatorFunction = Instance().iTestCases.FindL(key));
   1.196 +	if (err != KErrNone)
   1.197 +		{
   1.198 +		// Test case is not present in the factory therefore test cannot support specified test case
   1.199 +		RDebug::Print(_L("<Error %d> Test case '%S' not supported"),err,&aTestCaseId);
   1.200 +		ListRegisteredTestCases();
   1.201 +		User::Leave(err);
   1.202 +		}
   1.203 +
   1.204 +	RDebug::Print(_L("Creating test case '%S'"),&aTestCaseId);
   1.205 +		
   1.206 +	// Call the creator function to create the test case object
   1.207 +	return creatorFunction(gSemiAutomated);
   1.208 +	}
   1.209 +	
   1.210 +