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