os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/inc/testcasefactory.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 #ifndef TESTFACTORY_H
    19 #define TESTFACTORY_H
    20 
    21 #include <e32base.h>
    22 #include <e32hashtab.h>
    23 #include <e32test.h>     // RTest headder
    24 #include "testengine.h"
    25 
    26 // Test object
    27 extern RTest test;
    28 
    29 
    30 
    31 const TInt KTestCaseIdLength(35); // current longest is 30
    32 
    33 // Forward declarations
    34 class CTestCaseRoot;
    35 
    36 /**
    37 This class describes an identity that consists of a string and will
    38 be used for the key for the association map in the test factory
    39 */
    40 class TStringIdentity
    41 	{
    42 public:
    43 
    44 	/**
    45 	Constructor, build the identity from a string
    46 	@param anIdentity the string that is used for a unique identity
    47 	*/
    48 	explicit TStringIdentity(const TDesC& anIdentity)
    49 		{
    50 		iIdentity.Copy(anIdentity);
    51 		iIdentity.UpperCase();
    52 		}
    53 
    54 	/**
    55 	Generate a unique hash value from the key
    56 	@param aKey the key i.e. string identity
    57 	@return the 32bit unique hash value
    58 	*/
    59 	static TUint32 Hash(const TStringIdentity& aKey)
    60 		{
    61 		return DefaultHash::Des16(aKey.iIdentity);
    62 		}
    63 
    64 	/**
    65 	Compare the string identity of a target key with the element key
    66 	@param aKeyTarget the target string identity 
    67 	@param aKeyElement the current element key from the association map
    68 	@return the boolean result
    69 	*/
    70 	static TBool Id(const TStringIdentity& aKeyTarget,const TStringIdentity& aKeyElement)
    71 		{
    72 		return aKeyElement.iIdentity == aKeyTarget.iIdentity;
    73 		}
    74 	
    75 private:
    76 
    77 	// The identity as a string symbian descriptor
    78 	TBuf<64> iIdentity;
    79 	
    80 	};
    81 
    82 /**
    83 This class represents the test case factory
    84 Design pattern used: Singleton,Factory
    85 */
    86 class RTestFactory
    87 	{
    88 public:
    89 
    90 	/**
    91 	The signature of the method to create the test case.  All test cases that have a receipt will have a method of being 
    92 	created by this factory.
    93 	*/
    94 	typedef CTestCaseRoot* (*TCreationMethod)(TBool);
    95 	typedef RHashMap<TStringIdentity,TCreationMethod> RFactoryMap;
    96 
    97 
    98 	/**
    99 	Destructor, Destroy the test factory
   100 	*/
   101 	~RTestFactory();
   102 
   103 	/**
   104 	Register a test case with this factory.  If the test case could not be registered, the resultant
   105 	error will be logged and when requested to be created the factory should state that the test case could
   106 	not be supported.
   107 	@param aTestCaseId the identity of the test case
   108 	@param aCreationMethod the method used to create the test case
   109 	*/
   110 	static void RegisterTestCase(const TDesC& aTestCaseId,
   111 	  TCreationMethod aTestCreationMethod);	
   112 	
   113 	/**
   114 	Obtain a test case object that is for the given test case identity
   115 	@param aTestCaseId the identity of the test case
   116 	@return the test case object for the given identity
   117 	@leave KErrNotSupported if the test case object was not found
   118 	*/
   119 	static CTestCaseRoot* CreateTestCaseL(const TDesC& aTestCaseId);
   120 	
   121 	/**
   122 	Display through the use of the debug port, a list of all the test cases that 
   123 	have registered themselves with the factory
   124 	*/
   125 	static void ListRegisteredTestCases(RPointerArray<HBufC> & testCaseNames);
   126 	static void ListRegisteredTestCases() { RPointerArray<HBufC> test;ListRegisteredTestCases(test);test.ResetAndDestroy();};
   127 	
   128 	static TInt TestCaseCount() {	return(Instance().iTestCases.Count());	}
   129 	
   130 	static void GetTestID(TInt aIndex, TBuf<KTestCaseIdLength> &aTestID);
   131 	
   132 	static TBool TestCaseExists(const TDesC& aTestCaseId);
   133 
   134 private:
   135 
   136 	/**
   137 	Constructor
   138 	*/
   139 	RTestFactory();
   140 	
   141 	/**
   142 	Disable copy constructor
   143 	*/
   144 	RTestFactory(const RTestFactory& aRef);
   145 		
   146 	/**
   147 	Disable assignment operator
   148 	*/
   149 	RTestFactory& operator=(const RTestFactory& aRhs);
   150 		
   151 	/**
   152 	Retrieve the factory singleton instance
   153 	@return the only instance of this class
   154 	*/
   155 	static RTestFactory& Instance();	
   156 	
   157 private:
   158 
   159 	/**
   160 	The association between the test cases identities and the test case objects
   161 	that have registered themselves with the factory (i.e. that are available)	
   162 	*/
   163 	RFactoryMap iTestCases;
   164 	
   165 	};
   166 	
   167 /**
   168 This class represents the receipt object that when instantiated registers a test case class
   169 in the Test case factory under its test case identity.
   170 */
   171 template<typename T> // T: The test case class	
   172 class TTestCaseFactoryReceipt
   173 	{
   174 public:
   175 	/**
   176 	Called by the factory when the _TestCaseClass needs to be instantiated
   177 	@param aAutomation whether to bypass manual actions
   178 	@return a pointer to the required test case object
   179 	*/
   180 	static CTestCaseRoot* CreateTestCaseL(TBool aAutomation)
   181 		{
   182 		// Use the symbian factory two phase constructor
   183 		return T::NewL(aAutomation);
   184 		}
   185 	
   186 	/**
   187 	Constructor, builds on instantiation a factory receipt object for a test case
   188 	@param aTestCaseId the identity of the test case for which this is a receipt for
   189 	*/
   190 	explicit TTestCaseFactoryReceipt(const TDesC& aTestCaseId)
   191 		{
   192 		RTestFactory::RegisterTestCase(aTestCaseId, CreateTestCaseL);		
   193 		}
   194 	};
   195 
   196 
   197 
   198 #endif // TESTFACTORY_H
   199