os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcasefactory.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 the License "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 // @internalComponent
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32std_private.h>
    20 #include <u32std.h> 	// unicode builds
    21 #include <e32base.h>
    22 #include <e32base_private.h>
    23 #include <e32cons.h>
    24 #include <e32Test.h>	// RTest headder
    25 #include <e32def.h>
    26 #include <e32def_private.h>
    27 #include "TestCaseFactory.h"
    28 #include "debugmacros.h"
    29 
    30 
    31 	
    32 RTestFactory& RTestFactory::Instance()
    33 	{
    34 	static RTestFactory singleton;
    35 	return singleton;
    36 	}
    37 	
    38 	
    39 RTestFactory::~RTestFactory()
    40 	{
    41 	}
    42 	
    43 	
    44 RTestFactory::RTestFactory()
    45 :	iTestCases(TStringIdentity::Hash,TStringIdentity::Id)
    46 	{
    47 	}
    48 	
    49 	
    50 void RTestFactory::RegisterTestCase(const TDesC& aTestCaseId,TCreationMethod aCreationMethod)
    51 	{
    52 	//LOG_FUNC
    53 	TStringIdentity key(aTestCaseId);
    54 	TInt err(Instance().iTestCases.Insert(key,aCreationMethod));
    55 	if (err != KErrNone)
    56 		{
    57 		// Log that a test case could not be registered due to err
    58 		RDebug::Print(_L("Test case '%S' could not be registered with test case factory"),&aTestCaseId);
    59 		}
    60 	else
    61 		{
    62 		RTestFactory::TCreationMethod* creatorFunction = Instance().iTestCases.Find(key);
    63 		if (creatorFunction == NULL)
    64 			{
    65 			RDebug::Print(_L("<Error> Test case '%S' did not register"),&aTestCaseId);
    66 			ListRegisteredTestCases();
    67 			}
    68 		else
    69 			{
    70 			RDebug::Print(_L("Test case '%S' registered in factory"),&aTestCaseId);
    71 			}
    72 		}
    73 	}
    74 
    75 
    76 TBool RTestFactory::TestCaseExists(const TDesC& aTestCaseId)
    77 	{
    78 	RTestFactory::TCreationMethod creatorFunction;
    79 	TStringIdentity key(aTestCaseId);
    80 	
    81 	creatorFunction = REINTERPRET_CAST(RTestFactory::TCreationMethod, Instance().iTestCases.Find(key));
    82 	return (NULL != creatorFunction);
    83 	}
    84 
    85 
    86 /* Returns the test ID (name) of test at offset aIndex in the MAP
    87  */
    88 void RTestFactory::GetTestID(TInt aIndex, TBuf<KTestCaseIdLength> &aTestID)
    89 	{
    90 	LOG_FUNC
    91 
    92 	RFactoryMap::TIter it(Instance().iTestCases);
    93 	
    94 	TInt count(0);
    95 	for (count=0; count<Instance().iTestCases.Count(); count++)
    96 		{
    97 		it.NextKey();
    98 		if (count == aIndex)
    99 			{
   100 			TStringIdentity k(*it.CurrentKey());
   101 			TBuf<64> *p = REINTERPRET_CAST(TBuf<64>*, &k);
   102 			
   103 			aTestID.Copy( *p);	
   104 			return;
   105 			}
   106 		}
   107 	// Error: Case not found!
   108 	User::Leave(-2);
   109 	}
   110 	
   111 
   112 /* Return the ordinal value of the Test ID (numeric portion)
   113  */
   114 TInt TestIDValue(const TDesC & aTestID)
   115 	{
   116 	TUint16 value;
   117 	TBuf<KTestCaseIdLength> id;
   118 	id = aTestID.Right(4);
   119 	TLex  lex(id);
   120 	if (KErrNone == lex.Val(value, EDecimal))
   121 		return(value);
   122 	return(-1);	
   123 	}
   124 
   125 /* Print the test IDs in numerical order
   126  * Returns a sorted array of strings containing all of the test IDs
   127  */	
   128 void RTestFactory::ListRegisteredTestCases(RPointerArray<HBufC> & aTestCaseNameArr)
   129 	{
   130 	LOG_FUNC
   131 	RFactoryMap::TIter it(Instance().iTestCases);
   132 	TInt count(0);
   133 	TInt cases(Instance().iTestCases.Count());
   134 	
   135 	test.Printf(_L("------ F A C T O R Y -------\n"));
   136 	
   137 	it.Reset();
   138 	for (count=0; count<Instance().iTestCases.Count(); count++)
   139 		{
   140 		TStringIdentity k(*it.NextKey());
   141 		TBuf<KTestCaseIdLength> *p = REINTERPRET_CAST(TBuf<KTestCaseIdLength>*, &k); // pointer to the test ID
   142 		TBool placed(EFalse);
   143 		TInt  pos(0);
   144 		TInt  val(0);
   145 		
   146 		// build the sorted list
   147 		while (!placed)
   148 			{
   149 			val = TestIDValue(*p);
   150 			if (aTestCaseNameArr.Count()==pos) 
   151 				{ //array empty or reached end
   152 				HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
   153 				*testIdentity = *p;
   154 				aTestCaseNameArr.Append(testIdentity);
   155 				CleanupStack::Pop(testIdentity);
   156 				placed = ETrue;
   157 				}
   158 			else
   159 				{
   160 				if ( val < TestIDValue(*aTestCaseNameArr[pos]) )
   161 					{
   162 					HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
   163 					*testIdentity = *p;
   164 					aTestCaseNameArr.Insert(testIdentity, pos);
   165 					placed = ETrue;
   166 					CleanupStack::Pop(testIdentity);
   167 					}
   168 				else
   169 					{
   170 					pos++;
   171 					}
   172 				}
   173 			}
   174 		}
   175 	// print it
   176 	for (count=0; count<aTestCaseNameArr.Count(); count++)
   177 		{
   178 		test.Printf(_L("% 2d: %S\n"), count, aTestCaseNameArr[count]);
   179 		}
   180 	
   181 	test.Printf(_L("----------------------------\n"));
   182 	}
   183 
   184 
   185 CTestCaseRoot* RTestFactory::CreateTestCaseL(const TDesC& aTestCaseId)
   186 	{
   187 	LOG_FUNC
   188 	RTestFactory::TCreationMethod creatorFunction = NULL;
   189 	TInt err(KErrNone);
   190 	TStringIdentity key(aTestCaseId);
   191 	
   192 	TRAP(err,creatorFunction = Instance().iTestCases.FindL(key));
   193 	if (err != KErrNone)
   194 		{
   195 		// Test case is not present in the factory therefore test cannot support specified test case
   196 		RDebug::Print(_L("<Error %d> Test case '%S' not supported"),err,&aTestCaseId);
   197 		ListRegisteredTestCases();
   198 		User::Leave(err);
   199 		}
   200 
   201 	RDebug::Print(_L("Creating test case '%S'"),&aTestCaseId);
   202 		
   203 	// Call the creator function to create the test case object
   204 	return creatorFunction(gSemiAutomated);
   205 	}
   206 	
   207