os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcasefactory.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// @internalComponent
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include <e32std_private.h>
sl@0
    20
#include <u32std.h> 	// unicode builds
sl@0
    21
#include <e32base.h>
sl@0
    22
#include <e32base_private.h>
sl@0
    23
#include <e32cons.h>
sl@0
    24
#include <e32Test.h>	// RTest headder
sl@0
    25
#include <e32def.h>
sl@0
    26
#include <e32def_private.h>
sl@0
    27
#include "TestCaseFactory.h"
sl@0
    28
#include "debugmacros.h"
sl@0
    29
sl@0
    30
sl@0
    31
	
sl@0
    32
RTestFactory& RTestFactory::Instance()
sl@0
    33
	{
sl@0
    34
	static RTestFactory singleton;
sl@0
    35
	return singleton;
sl@0
    36
	}
sl@0
    37
	
sl@0
    38
	
sl@0
    39
RTestFactory::~RTestFactory()
sl@0
    40
	{
sl@0
    41
	}
sl@0
    42
	
sl@0
    43
	
sl@0
    44
RTestFactory::RTestFactory()
sl@0
    45
:	iTestCases(TStringIdentity::Hash,TStringIdentity::Id)
sl@0
    46
	{
sl@0
    47
	}
sl@0
    48
	
sl@0
    49
	
sl@0
    50
void RTestFactory::RegisterTestCase(const TDesC& aTestCaseId,TCreationMethod aCreationMethod)
sl@0
    51
	{
sl@0
    52
	//LOG_FUNC
sl@0
    53
	TStringIdentity key(aTestCaseId);
sl@0
    54
	TInt err(Instance().iTestCases.Insert(key,aCreationMethod));
sl@0
    55
	if (err != KErrNone)
sl@0
    56
		{
sl@0
    57
		// Log that a test case could not be registered due to err
sl@0
    58
		RDebug::Print(_L("Test case '%S' could not be registered with test case factory"),&aTestCaseId);
sl@0
    59
		}
sl@0
    60
	else
sl@0
    61
		{
sl@0
    62
		RTestFactory::TCreationMethod* creatorFunction = Instance().iTestCases.Find(key);
sl@0
    63
		if (creatorFunction == NULL)
sl@0
    64
			{
sl@0
    65
			RDebug::Print(_L("<Error> Test case '%S' did not register"),&aTestCaseId);
sl@0
    66
			ListRegisteredTestCases();
sl@0
    67
			}
sl@0
    68
		else
sl@0
    69
			{
sl@0
    70
			RDebug::Print(_L("Test case '%S' registered in factory"),&aTestCaseId);
sl@0
    71
			}
sl@0
    72
		}
sl@0
    73
	}
sl@0
    74
sl@0
    75
sl@0
    76
TBool RTestFactory::TestCaseExists(const TDesC& aTestCaseId)
sl@0
    77
	{
sl@0
    78
	RTestFactory::TCreationMethod creatorFunction;
sl@0
    79
	TStringIdentity key(aTestCaseId);
sl@0
    80
	
sl@0
    81
	creatorFunction = REINTERPRET_CAST(RTestFactory::TCreationMethod, Instance().iTestCases.Find(key));
sl@0
    82
	return (NULL != creatorFunction);
sl@0
    83
	}
sl@0
    84
sl@0
    85
sl@0
    86
/* Returns the test ID (name) of test at offset aIndex in the MAP
sl@0
    87
 */
sl@0
    88
void RTestFactory::GetTestID(TInt aIndex, TBuf<KTestCaseIdLength> &aTestID)
sl@0
    89
	{
sl@0
    90
	LOG_FUNC
sl@0
    91
sl@0
    92
	RFactoryMap::TIter it(Instance().iTestCases);
sl@0
    93
	
sl@0
    94
	TInt count(0);
sl@0
    95
	for (count=0; count<Instance().iTestCases.Count(); count++)
sl@0
    96
		{
sl@0
    97
		it.NextKey();
sl@0
    98
		if (count == aIndex)
sl@0
    99
			{
sl@0
   100
			TStringIdentity k(*it.CurrentKey());
sl@0
   101
			TBuf<64> *p = REINTERPRET_CAST(TBuf<64>*, &k);
sl@0
   102
			
sl@0
   103
			aTestID.Copy( *p);	
sl@0
   104
			return;
sl@0
   105
			}
sl@0
   106
		}
sl@0
   107
	// Error: Case not found!
sl@0
   108
	User::Leave(-2);
sl@0
   109
	}
sl@0
   110
	
sl@0
   111
sl@0
   112
/* Return the ordinal value of the Test ID (numeric portion)
sl@0
   113
 */
sl@0
   114
TInt TestIDValue(const TDesC & aTestID)
sl@0
   115
	{
sl@0
   116
	TUint16 value;
sl@0
   117
	TBuf<KTestCaseIdLength> id;
sl@0
   118
	id = aTestID.Right(4);
sl@0
   119
	TLex  lex(id);
sl@0
   120
	if (KErrNone == lex.Val(value, EDecimal))
sl@0
   121
		return(value);
sl@0
   122
	return(-1);	
sl@0
   123
	}
sl@0
   124
sl@0
   125
/* Print the test IDs in numerical order
sl@0
   126
 * Returns a sorted array of strings containing all of the test IDs
sl@0
   127
 */	
sl@0
   128
void RTestFactory::ListRegisteredTestCases(RPointerArray<HBufC> & aTestCaseNameArr)
sl@0
   129
	{
sl@0
   130
	LOG_FUNC
sl@0
   131
	RFactoryMap::TIter it(Instance().iTestCases);
sl@0
   132
	TInt count(0);
sl@0
   133
	TInt cases(Instance().iTestCases.Count());
sl@0
   134
	
sl@0
   135
	test.Printf(_L("------ F A C T O R Y -------\n"));
sl@0
   136
	
sl@0
   137
	it.Reset();
sl@0
   138
	for (count=0; count<Instance().iTestCases.Count(); count++)
sl@0
   139
		{
sl@0
   140
		TStringIdentity k(*it.NextKey());
sl@0
   141
		TBuf<KTestCaseIdLength> *p = REINTERPRET_CAST(TBuf<KTestCaseIdLength>*, &k); // pointer to the test ID
sl@0
   142
		TBool placed(EFalse);
sl@0
   143
		TInt  pos(0);
sl@0
   144
		TInt  val(0);
sl@0
   145
		
sl@0
   146
		// build the sorted list
sl@0
   147
		while (!placed)
sl@0
   148
			{
sl@0
   149
			val = TestIDValue(*p);
sl@0
   150
			if (aTestCaseNameArr.Count()==pos) 
sl@0
   151
				{ //array empty or reached end
sl@0
   152
				HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
sl@0
   153
				*testIdentity = *p;
sl@0
   154
				aTestCaseNameArr.Append(testIdentity);
sl@0
   155
				CleanupStack::Pop(testIdentity);
sl@0
   156
				placed = ETrue;
sl@0
   157
				}
sl@0
   158
			else
sl@0
   159
				{
sl@0
   160
				if ( val < TestIDValue(*aTestCaseNameArr[pos]) )
sl@0
   161
					{
sl@0
   162
					HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
sl@0
   163
					*testIdentity = *p;
sl@0
   164
					aTestCaseNameArr.Insert(testIdentity, pos);
sl@0
   165
					placed = ETrue;
sl@0
   166
					CleanupStack::Pop(testIdentity);
sl@0
   167
					}
sl@0
   168
				else
sl@0
   169
					{
sl@0
   170
					pos++;
sl@0
   171
					}
sl@0
   172
				}
sl@0
   173
			}
sl@0
   174
		}
sl@0
   175
	// print it
sl@0
   176
	for (count=0; count<aTestCaseNameArr.Count(); count++)
sl@0
   177
		{
sl@0
   178
		test.Printf(_L("% 2d: %S\n"), count, aTestCaseNameArr[count]);
sl@0
   179
		}
sl@0
   180
	
sl@0
   181
	test.Printf(_L("----------------------------\n"));
sl@0
   182
	}
sl@0
   183
sl@0
   184
sl@0
   185
CTestCaseRoot* RTestFactory::CreateTestCaseL(const TDesC& aTestCaseId)
sl@0
   186
	{
sl@0
   187
	LOG_FUNC
sl@0
   188
	RTestFactory::TCreationMethod creatorFunction = NULL;
sl@0
   189
	TInt err(KErrNone);
sl@0
   190
	TStringIdentity key(aTestCaseId);
sl@0
   191
	
sl@0
   192
	TRAP(err,creatorFunction = Instance().iTestCases.FindL(key));
sl@0
   193
	if (err != KErrNone)
sl@0
   194
		{
sl@0
   195
		// Test case is not present in the factory therefore test cannot support specified test case
sl@0
   196
		RDebug::Print(_L("<Error %d> Test case '%S' not supported"),err,&aTestCaseId);
sl@0
   197
		ListRegisteredTestCases();
sl@0
   198
		User::Leave(err);
sl@0
   199
		}
sl@0
   200
sl@0
   201
	RDebug::Print(_L("Creating test case '%S'"),&aTestCaseId);
sl@0
   202
		
sl@0
   203
	// Call the creator function to create the test case object
sl@0
   204
	return creatorFunction(gSemiAutomated);
sl@0
   205
	}
sl@0
   206
	
sl@0
   207