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