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: #include sl@0: #include sl@0: #include // unicode builds sl@0: #include sl@0: #include sl@0: #include sl@0: #include // RTest headder sl@0: #include sl@0: #include sl@0: #include "TestCaseFactory.h" sl@0: #include "debugmacros.h" sl@0: sl@0: sl@0: sl@0: RTestFactory& RTestFactory::Instance() sl@0: { sl@0: static RTestFactory singleton; sl@0: return singleton; sl@0: } sl@0: sl@0: sl@0: RTestFactory::~RTestFactory() sl@0: { sl@0: } sl@0: sl@0: sl@0: RTestFactory::RTestFactory() sl@0: : iTestCases(TStringIdentity::Hash,TStringIdentity::Id) sl@0: { sl@0: } sl@0: sl@0: sl@0: void RTestFactory::RegisterTestCase(const TDesC& aTestCaseId,TCreationMethod aCreationMethod) sl@0: { sl@0: //LOG_FUNC sl@0: TStringIdentity key(aTestCaseId); sl@0: TInt err(Instance().iTestCases.Insert(key,aCreationMethod)); sl@0: if (err != KErrNone) sl@0: { sl@0: // Log that a test case could not be registered due to err sl@0: RDebug::Print(_L("Test case '%S' could not be registered with test case factory"),&aTestCaseId); sl@0: } sl@0: else sl@0: { sl@0: RTestFactory::TCreationMethod* creatorFunction = Instance().iTestCases.Find(key); sl@0: if (creatorFunction == NULL) sl@0: { sl@0: RDebug::Print(_L(" Test case '%S' did not register"),&aTestCaseId); sl@0: ListRegisteredTestCases(); sl@0: } sl@0: else sl@0: { sl@0: RDebug::Print(_L("Test case '%S' registered in factory"),&aTestCaseId); sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: TBool RTestFactory::TestCaseExists(const TDesC& aTestCaseId) sl@0: { sl@0: RTestFactory::TCreationMethod creatorFunction; sl@0: TStringIdentity key(aTestCaseId); sl@0: sl@0: creatorFunction = REINTERPRET_CAST(RTestFactory::TCreationMethod, Instance().iTestCases.Find(key)); sl@0: return (NULL != creatorFunction); sl@0: } sl@0: sl@0: sl@0: /* Returns the test ID (name) of test at offset aIndex in the MAP sl@0: */ sl@0: void RTestFactory::GetTestID(TInt aIndex, TBuf &aTestID) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RFactoryMap::TIter it(Instance().iTestCases); sl@0: sl@0: TInt count(0); sl@0: for (count=0; count *p = REINTERPRET_CAST(TBuf<64>*, &k); sl@0: sl@0: aTestID.Copy( *p); sl@0: return; sl@0: } sl@0: } sl@0: // Error: Case not found! sl@0: User::Leave(-2); sl@0: } sl@0: sl@0: sl@0: /* Return the ordinal value of the Test ID (numeric portion) sl@0: */ sl@0: TInt TestIDValue(const TDesC & aTestID) sl@0: { sl@0: TUint16 value; sl@0: TBuf id; sl@0: id = aTestID.Right(4); sl@0: TLex lex(id); sl@0: if (KErrNone == lex.Val(value, EDecimal)) sl@0: return(value); sl@0: return(-1); sl@0: } sl@0: sl@0: /* Print the test IDs in numerical order sl@0: * Returns a sorted array of strings containing all of the test IDs sl@0: */ sl@0: void RTestFactory::ListRegisteredTestCases(RPointerArray & aTestCaseNameArr) sl@0: { sl@0: LOG_FUNC sl@0: RFactoryMap::TIter it(Instance().iTestCases); sl@0: TInt count(0); sl@0: TInt cases(Instance().iTestCases.Count()); sl@0: sl@0: test.Printf(_L("------ F A C T O R Y -------\n")); sl@0: sl@0: it.Reset(); sl@0: for (count=0; count *p = REINTERPRET_CAST(TBuf*, &k); // pointer to the test ID sl@0: TBool placed(EFalse); sl@0: TInt pos(0); sl@0: TInt val(0); sl@0: sl@0: // build the sorted list sl@0: while (!placed) sl@0: { sl@0: val = TestIDValue(*p); sl@0: if (aTestCaseNameArr.Count()==pos) sl@0: { //array empty or reached end sl@0: HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength); sl@0: *testIdentity = *p; sl@0: aTestCaseNameArr.Append(testIdentity); sl@0: CleanupStack::Pop(testIdentity); sl@0: placed = ETrue; sl@0: } sl@0: else sl@0: { sl@0: if ( val < TestIDValue(*aTestCaseNameArr[pos]) ) sl@0: { sl@0: HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength); sl@0: *testIdentity = *p; sl@0: aTestCaseNameArr.Insert(testIdentity, pos); sl@0: placed = ETrue; sl@0: CleanupStack::Pop(testIdentity); sl@0: } sl@0: else sl@0: { sl@0: pos++; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: // print it sl@0: for (count=0; count Test case '%S' not supported"),err,&aTestCaseId); sl@0: ListRegisteredTestCases(); sl@0: User::Leave(err); sl@0: } sl@0: sl@0: RDebug::Print(_L("Creating test case '%S'"),&aTestCaseId); sl@0: sl@0: // Call the creator function to create the test case object sl@0: return creatorFunction(gSemiAutomated); sl@0: } sl@0: sl@0: