1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/inc/TestCaseFactory.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,227 @@
1.4 +#ifndef __TEST_FACTORY_H
1.5 +#define __TEST_FACTORY_H
1.6 +
1.7 +/*
1.8 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.9 +* All rights reserved.
1.10 +* This component and the accompanying materials are made available
1.11 +* under the terms of the License "Eclipse Public License v1.0"
1.12 +* which accompanies this distribution, and is available
1.13 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.14 +*
1.15 +* Initial Contributors:
1.16 +* Nokia Corporation - initial contribution.
1.17 +*
1.18 +* Contributors:
1.19 +*
1.20 +* Description:
1.21 +* @file TestCaseFactory.h
1.22 +* @internalComponent
1.23 +*
1.24 +*
1.25 +*/
1.26 +
1.27 +
1.28 +
1.29 +#include <e32base.h>
1.30 +#include <e32hashtab.h>
1.31 +#include <e32test.h>
1.32 +#include "testdebug.h"
1.33 +#include "basetestcase.h"
1.34 +
1.35 +const TInt KTestCaseIdLength(18);
1.36 +
1.37 +// Test object
1.38 +
1.39 +extern RTest gtest;
1.40 +
1.41 +/**
1.42 +*/
1.43 +namespace NUnitTesting_USBDI
1.44 + {
1.45 +
1.46 +/**
1.47 +This class describes an identity that consists of a string and will
1.48 +be used for the key for the association map in the test factory
1.49 +*/
1.50 +class TStringIdentity
1.51 + {
1.52 +public:
1.53 + /**
1.54 + Generate a unique has value from the key
1.55 + @param aKey the key i.e. string identity
1.56 + @return the 32bit unique hash value
1.57 + */
1.58 + static TUint32 Hash(const TStringIdentity& aKey)
1.59 + {
1.60 + return DefaultHash::Des16(aKey.iIdentity);
1.61 + }
1.62 +
1.63 + /**
1.64 + Compare the string identity of a target key with the element key
1.65 + @param aKeyTarget the target string identity
1.66 + @param aKeyElement the current element key from the association map
1.67 + @return the boolean result
1.68 + */
1.69 + static TBool Id(const TStringIdentity& aKeyTarget,const TStringIdentity& aKeyElement)
1.70 + {
1.71 + return aKeyElement.iIdentity == aKeyTarget.iIdentity;
1.72 + }
1.73 +
1.74 +public:
1.75 +
1.76 + /**
1.77 + Constructor, build the identity from a string
1.78 + @param anIdentity the string that is used for a unique identity
1.79 + */
1.80 + explicit TStringIdentity(const TDesC& anIdentity)
1.81 + {
1.82 + iIdentity.Copy(anIdentity);
1.83 + iIdentity.UpperCase();
1.84 + }
1.85 +
1.86 +private:
1.87 + /**
1.88 + The identity as a string symbian descriptor
1.89 + */
1.90 + TBuf<64> iIdentity;
1.91 + };
1.92 +
1.93 +
1.94 +/**
1.95 +*/
1.96 +class TBaseTestCaseFunctor
1.97 + {
1.98 +public:
1.99 + virtual CBaseTestCase* operator()(TBool) const = 0;
1.100 + };
1.101 +
1.102 +
1.103 +/**
1.104 +This class represents the test case factory
1.105 +Design pattern used: Singleton,Factory
1.106 +*/
1.107 +class RTestFactory
1.108 + {
1.109 +private:
1.110 + // The signature for the container of test case associations
1.111 +
1.112 + typedef RHashMap<TStringIdentity,TBaseTestCaseFunctor const*> RFactoryMap;
1.113 +
1.114 +public:
1.115 +
1.116 + /**
1.117 + Destructor, Destroy the test factory
1.118 + */
1.119 + ~RTestFactory();
1.120 +
1.121 +public:
1.122 +
1.123 + /**
1.124 + Register a test case with this factory. If the test case could not be registered, the resultant
1.125 + error will be logged and when requested to be created the factory should state that the test case could
1.126 + not be supported.
1.127 + @param aTestCaseId the identity of the test case
1.128 + @param aCreationMethod the method used to create the test case
1.129 + */
1.130 + static void RegisterTestCase(const TDesC& aTestCaseId,TBaseTestCaseFunctor const* aFunctor);
1.131 +
1.132 + /**
1.133 + Obtain a test case object that is for the given test case identity
1.134 + @param aTestCaseId the identity of the test case
1.135 + @return the test case object for the given identity
1.136 + @leave KErrNotSupported if the test case object was not found
1.137 + */
1.138 + static CBaseTestCase* CreateTestCaseL(const TDesC& aTestCaseId,TBool aHostRole);
1.139 +
1.140 + /**
1.141 + Display through the use of the debug port, a list of all the test cases that
1.142 + have registered themselves with the factory
1.143 + */
1.144 + static void ListRegisteredTestCases();
1.145 +
1.146 +private:
1.147 + /**
1.148 + Constructor
1.149 + */
1.150 + RTestFactory();
1.151 +
1.152 + /**
1.153 + Disable copy constructor
1.154 + */
1.155 + RTestFactory(const RTestFactory& aRef);
1.156 +
1.157 + /**
1.158 + Disable assignment operator
1.159 + */
1.160 + RTestFactory& operator=(const RTestFactory& aRhs);
1.161 +
1.162 + /**
1.163 + Retrieve the factory singleton instance
1.164 + @return the only instance of this class
1.165 + */
1.166 + static RTestFactory& Instance();
1.167 +
1.168 +private:
1.169 + /**
1.170 + The association between the test cases identities and the test case objects
1.171 + that have registered themselves with the factory (i.e. that are available)
1.172 + */
1.173 + RFactoryMap iTestCases;
1.174 + };
1.175 +
1.176 +
1.177 +
1.178 +
1.179 +
1.180 +/**
1.181 +This functor class represents the receipt object that when instantiated registers a test case class
1.182 +in the Test case factory under its test case identity.
1.183 +*/
1.184 +template<typename TestCase,typename Parameter>
1.185 +class TFunctorTestCase : public TBaseTestCaseFunctor
1.186 + {
1.187 +private:
1.188 + /**
1.189 + The signature of the method to create the test case. All test cases that have a receipt will have a method of being
1.190 + created by this factory.
1.191 + */
1.192 + typedef TestCase* (*TSymbianTwoPhaseCreationMethod)(Parameter);
1.193 +
1.194 +public:
1.195 + /**
1.196 + Constructor, builds on instantiation a factory receipt object for a test case
1.197 + @param aTestCaseId the identity of the test case for which this is a receipt for
1.198 + */
1.199 + explicit TFunctorTestCase(const TDesC& aTestCaseId)
1.200 + {
1.201 + iCreationMethod = TestCase::NewL;
1.202 + RTestFactory::RegisterTestCase(aTestCaseId,this);
1.203 + }
1.204 +
1.205 + /**
1.206 + The invoker function to create a test case
1.207 + @param aHostFlag the flag to indicate at construction time a host or client test case
1.208 + @return the test case
1.209 + */
1.210 + CBaseTestCase* operator()(TBool aHostFlag) const
1.211 + {
1.212 + return iCreationMethod(aHostFlag);
1.213 + }
1.214 +
1.215 +private:
1.216 + /**
1.217 + The creation method that will creation the test case when instructed to by the factory
1.218 + */
1.219 + TSymbianTwoPhaseCreationMethod iCreationMethod;
1.220 + };
1.221 +
1.222 +
1.223 +
1.224 +
1.225 +
1.226 +
1.227 + }
1.228 +
1.229 +#endif
1.230 +