Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 #include <e32std_private.h>
20 #include <u32std.h> // unicode builds
22 #include <e32base_private.h>
24 #include <e32Test.h> // RTest headder
26 #include <e32def_private.h>
27 #include "TestCaseFactory.h"
28 #include "debugmacros.h"
32 RTestFactory& RTestFactory::Instance()
34 static RTestFactory singleton;
39 RTestFactory::~RTestFactory()
44 RTestFactory::RTestFactory()
45 : iTestCases(TStringIdentity::Hash,TStringIdentity::Id)
50 void RTestFactory::RegisterTestCase(const TDesC& aTestCaseId,TCreationMethod aCreationMethod)
53 TStringIdentity key(aTestCaseId);
54 TInt err(Instance().iTestCases.Insert(key,aCreationMethod));
57 // Log that a test case could not be registered due to err
58 RDebug::Print(_L("Test case '%S' could not be registered with test case factory"),&aTestCaseId);
62 RTestFactory::TCreationMethod* creatorFunction = Instance().iTestCases.Find(key);
63 if (creatorFunction == NULL)
65 RDebug::Print(_L("<Error> Test case '%S' did not register"),&aTestCaseId);
66 ListRegisteredTestCases();
70 RDebug::Print(_L("Test case '%S' registered in factory"),&aTestCaseId);
76 TBool RTestFactory::TestCaseExists(const TDesC& aTestCaseId)
78 RTestFactory::TCreationMethod creatorFunction;
79 TStringIdentity key(aTestCaseId);
81 creatorFunction = REINTERPRET_CAST(RTestFactory::TCreationMethod, Instance().iTestCases.Find(key));
82 return (NULL != creatorFunction);
86 /* Returns the test ID (name) of test at offset aIndex in the MAP
88 void RTestFactory::GetTestID(TInt aIndex, TBuf<KTestCaseIdLength> &aTestID)
92 RFactoryMap::TIter it(Instance().iTestCases);
95 for (count=0; count<Instance().iTestCases.Count(); count++)
100 TStringIdentity k(*it.CurrentKey());
101 TBuf<64> *p = REINTERPRET_CAST(TBuf<64>*, &k);
107 // Error: Case not found!
112 /* Return the ordinal value of the Test ID (numeric portion)
114 TInt TestIDValue(const TDesC & aTestID)
117 TBuf<KTestCaseIdLength> id;
118 id = aTestID.Right(4);
120 if (KErrNone == lex.Val(value, EDecimal))
125 /* Print the test IDs in numerical order
126 * Returns a sorted array of strings containing all of the test IDs
128 void RTestFactory::ListRegisteredTestCases(RPointerArray<HBufC> & aTestCaseNameArr)
131 RFactoryMap::TIter it(Instance().iTestCases);
133 TInt cases(Instance().iTestCases.Count());
135 test.Printf(_L("------ F A C T O R Y -------\n"));
138 for (count=0; count<Instance().iTestCases.Count(); count++)
140 TStringIdentity k(*it.NextKey());
141 TBuf<KTestCaseIdLength> *p = REINTERPRET_CAST(TBuf<KTestCaseIdLength>*, &k); // pointer to the test ID
142 TBool placed(EFalse);
146 // build the sorted list
149 val = TestIDValue(*p);
150 if (aTestCaseNameArr.Count()==pos)
151 { //array empty or reached end
152 HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
154 aTestCaseNameArr.Append(testIdentity);
155 CleanupStack::Pop(testIdentity);
160 if ( val < TestIDValue(*aTestCaseNameArr[pos]) )
162 HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
164 aTestCaseNameArr.Insert(testIdentity, pos);
166 CleanupStack::Pop(testIdentity);
176 for (count=0; count<aTestCaseNameArr.Count(); count++)
178 test.Printf(_L("% 2d: %S\n"), count, aTestCaseNameArr[count]);
181 test.Printf(_L("----------------------------\n"));
185 CTestCaseRoot* RTestFactory::CreateTestCaseL(const TDesC& aTestCaseId)
188 RTestFactory::TCreationMethod creatorFunction = NULL;
190 TStringIdentity key(aTestCaseId);
192 TRAP(err,creatorFunction = Instance().iTestCases.FindL(key));
195 // Test case is not present in the factory therefore test cannot support specified test case
196 RDebug::Print(_L("<Error %d> Test case '%S' not supported"),err,&aTestCaseId);
197 ListRegisteredTestCases();
201 RDebug::Print(_L("Creating test case '%S'"),&aTestCaseId);
203 // Call the creator function to create the test case object
204 return creatorFunction(gSemiAutomated);