os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/TestCaseController.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // @file testcasecontroller.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "basetestcase.h"
    20 #include "testcasecontroller.h"
    21 #include "testcasefactory.h"
    22 #include "testengine.h"
    23 #include "testpolicy.h"
    24 #include "testdebug.h"
    25 
    26 _LIT(KClientDeviceDriverName,"EUSBC");
    27 _LIT(KHostDeviceDriverName,"usbhubdriver");
    28 _LIT(KHostDeviceInterfaceDriverName,"usbdi");
    29 _LIT(KOtgdiLddFileName, "otgdi");
    30 
    31 namespace NUnitTesting_USBDI
    32 	{
    33 	
    34 CTestCaseController* CTestCaseController::NewL(CTestEngine& aTestEngine,TBool aHostRole)
    35 	{
    36 	CTestCaseController* self = new (ELeave) CTestCaseController(aTestEngine,aHostRole);
    37 	CleanupStack::PushL(self);
    38 	self->ConstructL();
    39 	CleanupStack::Pop(self);
    40 	return self;
    41 	}
    42 	
    43 	
    44 CTestCaseController::CTestCaseController(CTestEngine& aTestEngine,TBool aHostRole)
    45 :	CActive(EPriorityStandard),
    46 	iTestEngine(aTestEngine),
    47 	iHostRole(aHostRole)
    48 	{
    49 	// Add to current threads active scheduler
    50 	CActiveScheduler::Add(this);
    51 	}
    52 	
    53 	
    54 CTestCaseController::~CTestCaseController()
    55 	{
    56 	LOG_FUNC 
    57 
    58 	Cancel(); // Cancels any oustanding test cases
    59 
    60 	delete iTestPolicy;
    61 	
    62 	if(iHostRole)
    63 		{	
    64 		TInt err = User::FreeLogicalDevice(KHostDeviceInterfaceDriverName);
    65 		if(err != KErrNone)
    66 			{
    67 			RDebug::Printf("<Error %d> Unable to unload driver: %S",err,&KHostDeviceInterfaceDriverName);
    68 			}
    69 		
    70 		err = User::FreeLogicalDevice(KHostDeviceDriverName);
    71 		if(err != KErrNone)
    72 			{
    73 			RDebug::Printf("<Error %d> Unable to unload driver: %S",err,&KHostDeviceDriverName);
    74 			}
    75 			
    76 		err = User::FreeLogicalDevice(KOtgdiLddFileName);
    77 		if(err != KErrNone)
    78 			{
    79 			RDebug::Printf("<Error %d> Unable to unload driver: %S",err,&KHostDeviceDriverName);
    80 			}			
    81 		}
    82 	else
    83 		{
    84 		TInt err(User::FreeLogicalDevice(KClientDeviceDriverName));
    85 		if(err != KErrNone)
    86 			{
    87 			RDebug::Printf("<Error %d> Unable to unload driver: %S",err,&KClientDeviceDriverName);
    88 			}		
    89 		}
    90 	}
    91 
    92 void CTestCaseController::ConstructL()
    93 	{
    94 	LOG_FUNC
    95 	TInt err = KErrNone;
    96 	
    97 	_LIT(KLoadingNamedDriverString,"loading driver: %S\n");
    98 	_LIT(KLoadedNamedDriverString,"loaded driver: %S\n");
    99 		
   100 	// loading drivers
   101 	if(iHostRole)
   102 		{
   103 		gtest.Printf(KLoadingNamedDriverString,&KHostDeviceDriverName);		
   104 		// Load both Host USB device drivers
   105 		err = User::LoadLogicalDevice(KHostDeviceDriverName);
   106 		gtest((err == KErrNone) || (err == KErrAlreadyExists));
   107 		gtest.Printf(KLoadedNamedDriverString,&KHostDeviceDriverName);
   108 		
   109 		RDebug::Print(KLoadingNamedDriverString,&KHostDeviceInterfaceDriverName);
   110 		err = User::LoadLogicalDevice(KHostDeviceInterfaceDriverName);
   111 		gtest((err == KErrNone) || (err == KErrAlreadyExists));
   112 		gtest.Printf(KLoadedNamedDriverString,&KHostDeviceInterfaceDriverName);
   113 		  												 
   114 		// If test cases are running USB host side actions
   115 		// then run each test case in its own thread		
   116 		iTestPolicy = CThreadTestPolicy::NewL();
   117 		}
   118 	else
   119 		{
   120 				  		
   121 		// Load the USB client driver	
   122 		gtest.Printf(KLoadingNamedDriverString,&KClientDeviceDriverName);
   123 		err = User::LoadLogicalDevice(KClientDeviceDriverName);
   124 		gtest((err == KErrNone) || (err == KErrAlreadyExists));
   125 		gtest.Printf(KLoadedNamedDriverString,&KClientDeviceDriverName);
   126 		
   127 		// Run each test case in the main thread as its not new API 
   128 		// and not expected to panic
   129 		iTestPolicy = CBasicTestPolicy::NewL();		
   130 		}
   131 	
   132 	// Get the identity of the next test case to run	
   133 	err = iTestEngine.NextTestCaseId(iTestCaseId);
   134 	gtest.Next(iTestCaseId);
   135 	
   136 	// Run the test case	
   137 	iTestPolicy->RunTestCaseL(iTestCaseId,iStatus);
   138 	SetActive();
   139 	}
   140 	
   141 
   142 void CTestCaseController::DoCancel()
   143 	{
   144 	// Cancel the outstanding test case running
   145 
   146 	iTestPolicy->Cancel();
   147 	}
   148 	
   149 	
   150 void CTestCaseController::RunL()
   151 	{
   152 	LOG_FUNC
   153 
   154 	// Retrieve the completion code of the last test case run
   155 	TInt err(iStatus.Int());
   156 	
   157 	TBuf<64> log;
   158 	if(err != KErrNone)
   159 		{
   160 		iTestCasesResults.Append(EFalse);
   161 		gtest.Printf(_L("FAILED err=%d\n"),err);
   162 		}
   163 	else
   164 		{
   165 		iTestCasesResults.Append(ETrue);
   166 		gtest.Printf(_L("PASSED\n"));
   167 		}
   168 		
   169 	// Get the identity of the next test case to run
   170 		
   171 	err = iTestEngine.NextTestCaseId(iTestCaseId);
   172 	if(err == KErrNone)
   173 		{
   174 		RDebug::Printf("\n");
   175 		RDebug::Printf("\n");
   176 		RDebug::Printf("\n");
   177 		gtest.Next(iTestCaseId);
   178 		RDebug::Printf("                              --------------------");
   179 		RDebug::Printf("\n");
   180 		RDebug::Printf("\n");
   181 		RDebug::Printf("\n");
   182 		
   183 		// Run the next test case
   184 		
   185 		iTestPolicy->RunTestCaseL(iTestCaseId,iStatus);
   186 		SetActive();
   187 		}
   188 	else if(err == KErrNotFound)
   189 		{
   190 		RDebug::Printf("All specified test cases performed");
   191 		RDebug::Printf("----------------------------------");
   192 		
   193 		
   194 		// count nb failures
   195 		TUint nbFailures = 0;
   196 		for(TInt test = 0; test < iTestCasesResults.Count() ; test++)
   197 			{
   198 			if(!iTestCasesResults[test])
   199 				//NB iTestCasesResults is a boolean array added to each time a test is run...
   200 				//   ...even if it is a repeat.
   201 				{
   202 				nbFailures++;
   203 				}
   204 			}
   205 		RDebug::Printf("There are %d test case results, %d failures", iTestCasesResults.Count(), nbFailures);
   206 
   207 		// Number of tests that should have been run (including repeats)
   208 		TUint nbTests = iTestEngine.TestCasesIdentities().Count() * iTestEngine.NumRepeats();
   209 		if(nbTests!=iTestCasesResults.Count())
   210 			{
   211 			RDebug::Printf("The number of tests that should have been run (%d) DOES NOT EQUAL the actual number of tests run (%d).", 
   212 						  nbTests, iTestCasesResults.Count());
   213 			RDebug::Printf("This test suite will now PANIC!");
   214 			}
   215 		ASSERT((nbTests==iTestCasesResults.Count()));
   216 
   217 		
   218 		for(TInt repeat = 0; repeat < iTestEngine.NumRepeats() ; repeat++)
   219 			{
   220 			if(iTestEngine.NumRepeats() > 1)
   221 				{
   222 				RDebug::Printf("Test Case Loop %d..........",	repeat+1);			
   223 				}
   224 			for(TInt testIndex = 0; testIndex < iTestEngine.TestCasesIdentities().Count() ; testIndex++)
   225 				{
   226 				if(iTestCasesResults[testIndex])
   227 					{
   228 					RDebug::Print(_L("Test Case: %S : PASSED"),	(iTestEngine.TestCasesIdentities())[testIndex]);			
   229 					}
   230 				else
   231 					{
   232 					RDebug::Print(_L("Test Case: %S : FAILED"),	(iTestEngine.TestCasesIdentities())[testIndex]);
   233 					}
   234 				}
   235 			}
   236 
   237 		RDebug::Printf("CActiveScheduler::Stop CTestCaseController::RunL");
   238 		CActiveScheduler::Stop();
   239 		}
   240 	else
   241 		{
   242 		RDebug::Printf("<Error %d> Unknown error from CTestEngine::NextTestCaseId",err);
   243 		User::Leave(err);
   244 		}
   245 	}
   246 	
   247 	
   248 TInt CTestCaseController::RunError(TInt aError)
   249 	{
   250 	LOG_FUNC
   251 	
   252 	switch(aError)
   253 		{
   254 		case KErrNoMemory: //follow through
   255 		default:
   256 			// Panic the test module
   257 			gtest(EFalse);
   258 			break;
   259 		}
   260 	return KErrNone;
   261 	}
   262 
   263 
   264 	}
   265 
   266 
   267 
   268