sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #ifndef TESTFACTORY_H sl@0: #define TESTFACTORY_H sl@0: sl@0: #include sl@0: #include sl@0: #include // RTest headder sl@0: #include "testengine.h" sl@0: sl@0: // Test object sl@0: extern RTest test; sl@0: sl@0: sl@0: sl@0: const TInt KTestCaseIdLength(35); // current longest is 30 sl@0: sl@0: // Forward declarations sl@0: class CTestCaseRoot; sl@0: sl@0: /** sl@0: This class describes an identity that consists of a string and will sl@0: be used for the key for the association map in the test factory sl@0: */ sl@0: class TStringIdentity sl@0: { sl@0: public: sl@0: sl@0: /** sl@0: Constructor, build the identity from a string sl@0: @param anIdentity the string that is used for a unique identity sl@0: */ sl@0: explicit TStringIdentity(const TDesC& anIdentity) sl@0: { sl@0: iIdentity.Copy(anIdentity); sl@0: iIdentity.UpperCase(); sl@0: } sl@0: sl@0: /** sl@0: Generate a unique hash value from the key sl@0: @param aKey the key i.e. string identity sl@0: @return the 32bit unique hash value sl@0: */ sl@0: static TUint32 Hash(const TStringIdentity& aKey) sl@0: { sl@0: return DefaultHash::Des16(aKey.iIdentity); sl@0: } sl@0: sl@0: /** sl@0: Compare the string identity of a target key with the element key sl@0: @param aKeyTarget the target string identity sl@0: @param aKeyElement the current element key from the association map sl@0: @return the boolean result sl@0: */ sl@0: static TBool Id(const TStringIdentity& aKeyTarget,const TStringIdentity& aKeyElement) sl@0: { sl@0: return aKeyElement.iIdentity == aKeyTarget.iIdentity; sl@0: } sl@0: sl@0: private: sl@0: sl@0: // The identity as a string symbian descriptor sl@0: TBuf<64> iIdentity; sl@0: sl@0: }; sl@0: sl@0: /** sl@0: This class represents the test case factory sl@0: Design pattern used: Singleton,Factory sl@0: */ sl@0: class RTestFactory sl@0: { sl@0: public: sl@0: sl@0: /** sl@0: 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: created by this factory. sl@0: */ sl@0: typedef CTestCaseRoot* (*TCreationMethod)(TBool); sl@0: typedef RHashMap RFactoryMap; sl@0: sl@0: sl@0: /** sl@0: Destructor, Destroy the test factory sl@0: */ sl@0: ~RTestFactory(); sl@0: sl@0: /** sl@0: Register a test case with this factory. If the test case could not be registered, the resultant sl@0: error will be logged and when requested to be created the factory should state that the test case could sl@0: not be supported. sl@0: @param aTestCaseId the identity of the test case sl@0: @param aCreationMethod the method used to create the test case sl@0: */ sl@0: static void RegisterTestCase(const TDesC& aTestCaseId, sl@0: TCreationMethod aTestCreationMethod); sl@0: sl@0: /** sl@0: Obtain a test case object that is for the given test case identity sl@0: @param aTestCaseId the identity of the test case sl@0: @return the test case object for the given identity sl@0: @leave KErrNotSupported if the test case object was not found sl@0: */ sl@0: static CTestCaseRoot* CreateTestCaseL(const TDesC& aTestCaseId); sl@0: sl@0: /** sl@0: Display through the use of the debug port, a list of all the test cases that sl@0: have registered themselves with the factory sl@0: */ sl@0: static void ListRegisteredTestCases(RPointerArray & testCaseNames); sl@0: static void ListRegisteredTestCases() { RPointerArray test;ListRegisteredTestCases(test);test.ResetAndDestroy();}; sl@0: sl@0: static TInt TestCaseCount() { return(Instance().iTestCases.Count()); } sl@0: sl@0: static void GetTestID(TInt aIndex, TBuf &aTestID); sl@0: sl@0: static TBool TestCaseExists(const TDesC& aTestCaseId); sl@0: sl@0: private: sl@0: sl@0: /** sl@0: Constructor sl@0: */ sl@0: RTestFactory(); sl@0: sl@0: /** sl@0: Disable copy constructor sl@0: */ sl@0: RTestFactory(const RTestFactory& aRef); sl@0: sl@0: /** sl@0: Disable assignment operator sl@0: */ sl@0: RTestFactory& operator=(const RTestFactory& aRhs); sl@0: sl@0: /** sl@0: Retrieve the factory singleton instance sl@0: @return the only instance of this class sl@0: */ sl@0: static RTestFactory& Instance(); sl@0: sl@0: private: sl@0: sl@0: /** sl@0: The association between the test cases identities and the test case objects sl@0: that have registered themselves with the factory (i.e. that are available) sl@0: */ sl@0: RFactoryMap iTestCases; sl@0: sl@0: }; sl@0: sl@0: /** sl@0: This class represents the receipt object that when instantiated registers a test case class sl@0: in the Test case factory under its test case identity. sl@0: */ sl@0: template // T: The test case class sl@0: class TTestCaseFactoryReceipt sl@0: { sl@0: public: sl@0: /** sl@0: Called by the factory when the _TestCaseClass needs to be instantiated sl@0: @param aAutomation whether to bypass manual actions sl@0: @return a pointer to the required test case object sl@0: */ sl@0: static CTestCaseRoot* CreateTestCaseL(TBool aAutomation) sl@0: { sl@0: // Use the symbian factory two phase constructor sl@0: return T::NewL(aAutomation); sl@0: } sl@0: sl@0: /** sl@0: Constructor, builds on instantiation a factory receipt object for a test case sl@0: @param aTestCaseId the identity of the test case for which this is a receipt for sl@0: */ sl@0: explicit TTestCaseFactoryReceipt(const TDesC& aTestCaseId) sl@0: { sl@0: RTestFactory::RegisterTestCase(aTestCaseId, CreateTestCaseL); sl@0: } sl@0: }; sl@0: sl@0: sl@0: sl@0: #endif // TESTFACTORY_H sl@0: