kerneltest/e32test/usbho/t_usbdi/inc/TestCaseFactory.h
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 #ifndef __TEST_FACTORY_H
       
     2 #define __TEST_FACTORY_H
       
     3 
       
     4 /*
       
     5 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     6 * All rights reserved.
       
     7 * This component and the accompanying materials are made available
       
     8 * under the terms of the License "Eclipse Public License v1.0"
       
     9 * which accompanies this distribution, and is available
       
    10 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    11 *
       
    12 * Initial Contributors:
       
    13 * Nokia Corporation - initial contribution.
       
    14 *
       
    15 * Contributors:
       
    16 *
       
    17 * Description:
       
    18 * @file TestCaseFactory.h
       
    19 * @internalComponent
       
    20 * 
       
    21 *
       
    22 */
       
    23 
       
    24 
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <e32hashtab.h> 
       
    28 #include <e32test.h>
       
    29 #include "testdebug.h"
       
    30 #include "basetestcase.h"
       
    31 
       
    32 const TInt KTestCaseIdLength(18);
       
    33 
       
    34 // Test object
       
    35 
       
    36 extern RTest gtest;
       
    37  
       
    38 /**
       
    39 */
       
    40 namespace NUnitTesting_USBDI
       
    41 	{
       
    42 	
       
    43 /**
       
    44 This class describes an identity that consists of a string and will
       
    45 be used for the key for the association map in the test factory
       
    46 */
       
    47 class TStringIdentity
       
    48 	{
       
    49 public:
       
    50 	/**
       
    51 	Generate a unique has value from the key
       
    52 	@param aKey the key i.e. string identity
       
    53 	@return the 32bit unique hash value
       
    54 	*/
       
    55 	static TUint32 Hash(const TStringIdentity& aKey)
       
    56 		{
       
    57 		return DefaultHash::Des16(aKey.iIdentity);
       
    58 		}
       
    59 		
       
    60 	/**
       
    61 	Compare the string identity of a target key with the element key
       
    62 	@param aKeyTarget the target string identity 
       
    63 	@param aKeyElement the current element key from the association map
       
    64 	@return the boolean result
       
    65 	*/
       
    66 	static TBool Id(const TStringIdentity& aKeyTarget,const TStringIdentity& aKeyElement)
       
    67 		{
       
    68 		return aKeyElement.iIdentity == aKeyTarget.iIdentity;
       
    69 		}
       
    70 
       
    71 public:
       
    72 
       
    73 	/**
       
    74 	Constructor, build the identity from a string
       
    75 	@param anIdentity the string that is used for a unique identity
       
    76 	*/
       
    77 	explicit TStringIdentity(const TDesC& anIdentity)
       
    78 		{
       
    79 		iIdentity.Copy(anIdentity);
       
    80 		iIdentity.UpperCase();
       
    81 		}
       
    82 	
       
    83 private:
       
    84 	/**
       
    85 	The identity as a string symbian descriptor
       
    86 	*/
       
    87 	TBuf<64> iIdentity;
       
    88 	};
       
    89 
       
    90 
       
    91 /**
       
    92 */	
       
    93 class TBaseTestCaseFunctor
       
    94 	{
       
    95 public:
       
    96 	virtual CBaseTestCase* operator()(TBool) const = 0;
       
    97 	};
       
    98 
       
    99 	
       
   100 /**
       
   101 This class represents the test case factory
       
   102 Design pattern used: Singleton,Factory
       
   103 */
       
   104 class RTestFactory
       
   105 	{
       
   106 private:
       
   107 	// The signature for the container of test case associations
       
   108 
       
   109 	typedef RHashMap<TStringIdentity,TBaseTestCaseFunctor const*> RFactoryMap;
       
   110 	
       
   111 public:
       
   112 
       
   113 	/**
       
   114 	Destructor, Destroy the test factory
       
   115 	*/	
       
   116 	~RTestFactory();
       
   117 
       
   118 public:
       
   119 	
       
   120 	/**
       
   121 	Register a test case with this factory.  If the test case could not be registered, the resultant
       
   122 	error will be logged and when requested to be created the factory should state that the test case could
       
   123 	not be supported.
       
   124 	@param aTestCaseId the identity of the test case
       
   125 	@param aCreationMethod the method used to create the test case
       
   126 	*/
       
   127 	static void RegisterTestCase(const TDesC& aTestCaseId,TBaseTestCaseFunctor const* aFunctor);	
       
   128 
       
   129 	/**
       
   130 	Obtain a test case object that is for the given test case identity
       
   131 	@param aTestCaseId the identity of the test case
       
   132 	@return the test case object for the given identity
       
   133 	@leave KErrNotSupported if the test case object was not found
       
   134 	*/
       
   135 	static CBaseTestCase* CreateTestCaseL(const TDesC& aTestCaseId,TBool aHostRole);
       
   136 	
       
   137 	/**
       
   138 	Display through the use of the debug port, a list of all the test cases that 
       
   139 	have registered themselves with the factory
       
   140 	*/
       
   141 	static void ListRegisteredTestCases();
       
   142 
       
   143 private:
       
   144 	/**
       
   145 	Constructor
       
   146 	*/
       
   147 	RTestFactory();
       
   148 	
       
   149 	/**
       
   150 	Disable copy constructor
       
   151 	*/
       
   152 	RTestFactory(const RTestFactory& aRef);
       
   153 		
       
   154 	/**
       
   155 	Disable assignment operator
       
   156 	*/
       
   157 	RTestFactory& operator=(const RTestFactory& aRhs);
       
   158 		
       
   159 	/**
       
   160 	Retrieve the factory singleton instance
       
   161 	@return the only instance of this class
       
   162 	*/
       
   163 	static RTestFactory& Instance();	
       
   164 	
       
   165 private:
       
   166 	/**
       
   167 	The association between the test cases identities and the test case objects
       
   168 	that have registered themselves with the factory (i.e. that are available)	
       
   169 	*/
       
   170 	RFactoryMap iTestCases;
       
   171 	};
       
   172 	
       
   173 	
       
   174 	
       
   175 	
       
   176 	
       
   177 /**
       
   178 This functor class represents the receipt object that when instantiated registers a test case class
       
   179 in the Test case factory under its test case identity.
       
   180 */
       
   181 template<typename TestCase,typename Parameter>
       
   182 class TFunctorTestCase : public TBaseTestCaseFunctor
       
   183 	{
       
   184 private:
       
   185 	/**
       
   186 	The signature of the method to create the test case.  All test cases that have a receipt will have a method of being 
       
   187 	created by this factory.
       
   188 	*/
       
   189 	typedef TestCase* (*TSymbianTwoPhaseCreationMethod)(Parameter);
       
   190 
       
   191 public:
       
   192 	/**
       
   193 	Constructor, builds on instantiation a factory receipt object for a test case
       
   194 	@param aTestCaseId the identity of the test case for which this is a receipt for
       
   195 	*/
       
   196 	explicit TFunctorTestCase(const TDesC& aTestCaseId)
       
   197 		{
       
   198 		iCreationMethod = TestCase::NewL;
       
   199 		RTestFactory::RegisterTestCase(aTestCaseId,this);		
       
   200 		}
       
   201 	
       
   202 	/**
       
   203 	The invoker function to create a test case
       
   204 	@param aHostFlag the flag to indicate at construction time a host or client test case
       
   205 	@return the test case
       
   206 	*/
       
   207 	CBaseTestCase* operator()(TBool aHostFlag) const
       
   208 		{
       
   209 		return iCreationMethod(aHostFlag);
       
   210 		}
       
   211 	
       
   212 private:
       
   213 	/**
       
   214 	The creation method that will creation the test case when instructed to by the factory
       
   215 	*/
       
   216 	TSymbianTwoPhaseCreationMethod iCreationMethod;
       
   217 	};	
       
   218 	
       
   219 	
       
   220 	
       
   221 	
       
   222 	
       
   223 
       
   224 	}
       
   225 
       
   226 #endif
       
   227