os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/TestPolicy.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
// @file testpolicy.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "TestPolicy.h"
sl@0
    20
#include "TestCaseFactory.h"
sl@0
    21
#include "BaseTestCase.h"
sl@0
    22
#include "TestCaseFactory.h"
sl@0
    23
#include "testdebug.h"
sl@0
    24
sl@0
    25
namespace NUnitTesting_USBDI
sl@0
    26
	{
sl@0
    27
	
sl@0
    28
CBasicTestPolicy* CBasicTestPolicy::NewL()
sl@0
    29
	{
sl@0
    30
	CBasicTestPolicy* self = new (ELeave) CBasicTestPolicy;
sl@0
    31
	CleanupStack::PushL(self);
sl@0
    32
	self->ConstructL();
sl@0
    33
	CleanupStack::Pop(self);
sl@0
    34
	return self;
sl@0
    35
	}
sl@0
    36
sl@0
    37
	
sl@0
    38
CBasicTestPolicy::CBasicTestPolicy()
sl@0
    39
:	CActive(EPriorityStandard)
sl@0
    40
	{
sl@0
    41
	CActiveScheduler::Add(this);
sl@0
    42
	}
sl@0
    43
sl@0
    44
	
sl@0
    45
CBasicTestPolicy::~CBasicTestPolicy()
sl@0
    46
	{
sl@0
    47
	LOG_FUNC
sl@0
    48
sl@0
    49
	Cancel();
sl@0
    50
	}
sl@0
    51
	
sl@0
    52
sl@0
    53
void CBasicTestPolicy::ConstructL()
sl@0
    54
	{
sl@0
    55
	}
sl@0
    56
sl@0
    57
	
sl@0
    58
void CBasicTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus)
sl@0
    59
	{
sl@0
    60
	LOG_FUNC
sl@0
    61
sl@0
    62
	iNotifierStatus = &aNotifierStatus;
sl@0
    63
sl@0
    64
	// Create the specified test case
sl@0
    65
sl@0
    66
	iTestCase = RTestFactory::CreateTestCaseL(aTestCaseId,EFalse);
sl@0
    67
	iTestCase->SetTestPolicy(this);
sl@0
    68
	iTestCase->PerformTestL();
sl@0
    69
sl@0
    70
	// Set the request of the notifier to pending
sl@0
    71
	
sl@0
    72
	*iNotifierStatus = iStatus = KRequestPending;
sl@0
    73
	SetActive();
sl@0
    74
	}
sl@0
    75
sl@0
    76
sl@0
    77
void CBasicTestPolicy::DoCancel()
sl@0
    78
	{
sl@0
    79
	LOG_FUNC
sl@0
    80
sl@0
    81
	// Cancel running the test cases
sl@0
    82
	
sl@0
    83
	iTestCase->Cancel();
sl@0
    84
sl@0
    85
	// Notify the test case controller that test case execution was cancelled
sl@0
    86
	
sl@0
    87
	User::RequestComplete(iNotifierStatus,KErrCancel);
sl@0
    88
	}
sl@0
    89
sl@0
    90
sl@0
    91
void CBasicTestPolicy::SignalTestComplete(TInt aCompletionCode)
sl@0
    92
	{
sl@0
    93
	LOG_FUNC
sl@0
    94
	
sl@0
    95
	// Complete the test policy request with the test case completion code
sl@0
    96
	// (Basically self completion)
sl@0
    97
	
sl@0
    98
	TRequestStatus* s = &iStatus;	
sl@0
    99
	User::RequestComplete(s,aCompletionCode);
sl@0
   100
	}
sl@0
   101
sl@0
   102
	
sl@0
   103
void CBasicTestPolicy::RunL()
sl@0
   104
	{
sl@0
   105
	LOG_FUNC
sl@0
   106
	
sl@0
   107
	// Complete the request of the notifier with the test case 
sl@0
   108
	// completion code
sl@0
   109
	
sl@0
   110
	User::RequestComplete(iNotifierStatus,iStatus.Int());
sl@0
   111
	
sl@0
   112
	// Destroy the test case
sl@0
   113
	
sl@0
   114
	delete iTestCase;
sl@0
   115
	}
sl@0
   116
sl@0
   117
sl@0
   118
TInt CBasicTestPolicy::RunError(TInt aError)
sl@0
   119
	{
sl@0
   120
	LOG_FUNC
sl@0
   121
	
sl@0
   122
	aError = KErrNone;
sl@0
   123
	return aError;
sl@0
   124
	}
sl@0
   125
	
sl@0
   126
CThreadTestPolicy* CThreadTestPolicy::NewL()
sl@0
   127
	{
sl@0
   128
	CThreadTestPolicy* self = new (ELeave) CThreadTestPolicy;
sl@0
   129
	CleanupStack::PushL(self);
sl@0
   130
	self->ConstructL();
sl@0
   131
	CleanupStack::Pop(self);
sl@0
   132
	return self;
sl@0
   133
	}
sl@0
   134
	
sl@0
   135
	
sl@0
   136
CThreadTestPolicy::CThreadTestPolicy()
sl@0
   137
	{
sl@0
   138
	}
sl@0
   139
	
sl@0
   140
	
sl@0
   141
CThreadTestPolicy::~CThreadTestPolicy()
sl@0
   142
	{
sl@0
   143
	iTestThread.Close();
sl@0
   144
	if(iTestCaseId)
sl@0
   145
		{
sl@0
   146
		delete iTestCaseId;
sl@0
   147
		iTestCaseId = NULL;
sl@0
   148
		}
sl@0
   149
	}
sl@0
   150
sl@0
   151
void CThreadTestPolicy::ConstructL()
sl@0
   152
	{
sl@0
   153
	}
sl@0
   154
sl@0
   155
void CThreadTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus)
sl@0
   156
	{
sl@0
   157
	LOG_FUNC
sl@0
   158
sl@0
   159
	iNotifierStatus = &aNotifierStatus;
sl@0
   160
	RDebug::Printf("Creating thread for test case '%S'",&aTestCaseId);
sl@0
   161
		
sl@0
   162
	if(iTestCaseId)
sl@0
   163
		{
sl@0
   164
		delete iTestCaseId;
sl@0
   165
		iTestCaseId = NULL;
sl@0
   166
		}
sl@0
   167
	iTestCaseId = HBufC::NewL(aTestCaseId.Length()); 
sl@0
   168
	*iTestCaseId = aTestCaseId;
sl@0
   169
		
sl@0
   170
	// Create the thread to run the test case in
sl@0
   171
	TInt err(iTestThread.Create(aTestCaseId,CThreadTestPolicy::ThreadFunction,
sl@0
   172
			KDefaultStackSize,NULL,reinterpret_cast<TAny*>(iTestCaseId)));
sl@0
   173
	
sl@0
   174
	if(err != KErrNone)
sl@0
   175
		{
sl@0
   176
		RDebug::Printf("Test thread creation unsuccessful: %d",err);
sl@0
   177
		User::Leave(err);
sl@0
   178
		}
sl@0
   179
sl@0
   180
	RDebug::Printf("Test thread '%S' created",&aTestCaseId);
sl@0
   181
	// Start the test case in the thread
sl@0
   182
	iTestThread.Logon(iStatus);
sl@0
   183
	SetActive();
sl@0
   184
	iTestThread.Resume();
sl@0
   185
	*iNotifierStatus = KRequestPending;
sl@0
   186
	}
sl@0
   187
sl@0
   188
sl@0
   189
void CThreadTestPolicy::SignalTestComplete(TInt aCompletionCode)
sl@0
   190
	{
sl@0
   191
	LOG_FUNC
sl@0
   192
sl@0
   193
	if(aCompletionCode == KErrNone)
sl@0
   194
		{
sl@0
   195
		RDebug::Printf("CActiveScheduler::Stop CThreadTestPolicy::SignalTestComplete");
sl@0
   196
		CActiveScheduler::Stop();
sl@0
   197
		}
sl@0
   198
	else
sl@0
   199
		{
sl@0
   200
		RDebug::Printf("Killing thread with: %d",aCompletionCode);
sl@0
   201
		iTestThread.Kill(aCompletionCode);
sl@0
   202
		}
sl@0
   203
	}
sl@0
   204
sl@0
   205
void CThreadTestPolicy::DoCancel()
sl@0
   206
	{
sl@0
   207
	LOG_FUNC
sl@0
   208
sl@0
   209
	iTestCase->Cancel();
sl@0
   210
	LOG_POINT(1)
sl@0
   211
	TInt err(iTestThread.LogonCancel(iStatus));
sl@0
   212
	if(err != KErrNone)
sl@0
   213
		{
sl@0
   214
		RDebug::Printf("Unable to cancel thread logon: %d",err);
sl@0
   215
		}
sl@0
   216
	LOG_POINT(2)
sl@0
   217
	TRequestStatus cancelStatus;
sl@0
   218
	iTestThread.Logon(cancelStatus);
sl@0
   219
	LOG_POINT(3)
sl@0
   220
	iTestThread.Kill(KErrCancel);		
sl@0
   221
	LOG_POINT(4)
sl@0
   222
	User::RequestComplete(iNotifierStatus,cancelStatus.Int());
sl@0
   223
	}
sl@0
   224
	
sl@0
   225
TInt CThreadTestPolicy::ThreadFunction(TAny* aThreadParameter)
sl@0
   226
	{
sl@0
   227
	LOG_CFUNC
sl@0
   228
sl@0
   229
	TInt err(KErrNone);
sl@0
   230
	TInt leaveCode(KErrNone);
sl@0
   231
	
sl@0
   232
	HBufC* testCaseId = reinterpret_cast<HBufC*>(aThreadParameter);
sl@0
   233
	
sl@0
   234
	// Create the cleanup stack for this thread
sl@0
   235
	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0
   236
	if(cleanup == NULL)
sl@0
   237
		{
sl@0
   238
		return KErrNoMemory;
sl@0
   239
		}
sl@0
   240
		
sl@0
   241
	TRAP(leaveCode,err = CThreadTestPolicy::DoTestL(*testCaseId));
sl@0
   242
	if(leaveCode != KErrNone)
sl@0
   243
		{
sl@0
   244
		RDebug::Printf("<Error %d> Thread '%S' DoTest",leaveCode,&testCaseId);
sl@0
   245
		err = leaveCode;
sl@0
   246
		}
sl@0
   247
	
sl@0
   248
	delete cleanup;
sl@0
   249
	return err;
sl@0
   250
	}
sl@0
   251
	
sl@0
   252
	
sl@0
   253
TInt CThreadTestPolicy::DoTestL(const TDesC& aTestCaseId)
sl@0
   254
	{
sl@0
   255
	LOG_CFUNC
sl@0
   256
	TInt err(KErrNone);
sl@0
   257
	
sl@0
   258
	// Create a new active scheduler for this thread
sl@0
   259
	CActiveScheduler* sched = new (ELeave) CActiveScheduler;
sl@0
   260
	CleanupStack::PushL(sched);
sl@0
   261
	CActiveScheduler::Install(sched);
sl@0
   262
sl@0
   263
	CBaseTestCase* testCase = RTestFactory::CreateTestCaseL(aTestCaseId,ETrue);
sl@0
   264
	CleanupStack::PushL(testCase);
sl@0
   265
		
sl@0
   266
	// Do the test
sl@0
   267
	testCase->PerformTestL();
sl@0
   268
	
sl@0
   269
	if(!testCase->IsHostOnly())
sl@0
   270
		{
sl@0
   271
		// Loop for active objects
sl@0
   272
		RDebug::Printf("CActiveScheduler::Start in CThreadTestPolicy::DoTestL");	
sl@0
   273
		CActiveScheduler::Start();
sl@0
   274
		}	
sl@0
   275
	// Get the test case execution result 
sl@0
   276
	err = testCase->TestResult();
sl@0
   277
	
sl@0
   278
	// Destroy test case
sl@0
   279
	CleanupStack::PopAndDestroy(testCase);
sl@0
   280
	
sl@0
   281
	// Destroy the active scheduler	
sl@0
   282
	CleanupStack::PopAndDestroy(sched);
sl@0
   283
	
sl@0
   284
	return err;
sl@0
   285
	}
sl@0
   286
sl@0
   287
void CThreadTestPolicy::RunL()
sl@0
   288
	{
sl@0
   289
	LOG_FUNC
sl@0
   290
	TInt completionCode(iStatus.Int());
sl@0
   291
	
sl@0
   292
	TExitType exitType(iTestThread.ExitType());
sl@0
   293
	TExitCategoryName exitName(iTestThread.ExitCategory());
sl@0
   294
	TInt exitReason(iTestThread.ExitReason());
sl@0
   295
	RDebug::Printf("Test thread '%S' completed with completion code %d",&iTestThread.Name(),completionCode);
sl@0
   296
		
sl@0
   297
	switch(exitType)
sl@0
   298
		{
sl@0
   299
		// The test thread has panicked
sl@0
   300
		// This will occur if test API panics or RTest expression is false (i.e. test case fails)
sl@0
   301
		case EExitPanic:
sl@0
   302
			{
sl@0
   303
			RDebug::Printf("Test thread '%S' has panicked with category '%S' reason %d",
sl@0
   304
						&iTestThread.Name(),&exitName,exitReason);
sl@0
   305
			// May require to stop and start host/client USB depending on what panic category it is
sl@0
   306
			// can no longer trust RUsbHubDriver/RDevUsbcClient to be in good state
sl@0
   307
			completionCode = KErrAbort;
sl@0
   308
			}
sl@0
   309
			break;
sl@0
   310
				
sl@0
   311
		// The thread has been terminated
sl@0
   312
		case EExitTerminate:
sl@0
   313
			{
sl@0
   314
			RDebug::Printf("Test thread '%S' terminated with category %s reason %d",
sl@0
   315
				&iTestThread.Name(),&exitName,exitReason);
sl@0
   316
			}
sl@0
   317
			break;
sl@0
   318
				
sl@0
   319
		// The thread has been killed
sl@0
   320
		// This will occur when the test thread executes normally or is cancelled
sl@0
   321
		case EExitKill:
sl@0
   322
			{
sl@0
   323
			RDebug::Printf("Test thread '%S' has been killed with reason %d",&iTestThread.Name(),exitReason);
sl@0
   324
			}
sl@0
   325
			break;
sl@0
   326
				
sl@0
   327
		// These cases should not occur at this stage
sl@0
   328
		case EExitPending: //follow through
sl@0
   329
		default:
sl@0
   330
			gtest(EFalse); // Panic main thread
sl@0
   331
			break;
sl@0
   332
		}
sl@0
   333
sl@0
   334
	// Close the just executed test thread
sl@0
   335
	iTestThread.Close();
sl@0
   336
		
sl@0
   337
	// Complete the notifier's request status
sl@0
   338
	User::RequestComplete(iNotifierStatus,completionCode);
sl@0
   339
	}
sl@0
   340
sl@0
   341
TInt CThreadTestPolicy::RunError(TInt aError)
sl@0
   342
	{
sl@0
   343
	RDebug::Printf("<Error %d><Test Policy> RunError",aError);
sl@0
   344
	return KErrNone;
sl@0
   345
	}
sl@0
   346
	
sl@0
   347
	}