First public contribution.
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.
14 // @file testpolicy.cpp
19 #include "TestPolicy.h"
20 #include "TestCaseFactory.h"
21 #include "BaseTestCase.h"
22 #include "TestCaseFactory.h"
23 #include "testdebug.h"
25 namespace NUnitTesting_USBDI
28 CBasicTestPolicy* CBasicTestPolicy::NewL()
30 CBasicTestPolicy* self = new (ELeave) CBasicTestPolicy;
31 CleanupStack::PushL(self);
33 CleanupStack::Pop(self);
38 CBasicTestPolicy::CBasicTestPolicy()
39 : CActive(EPriorityStandard)
41 CActiveScheduler::Add(this);
45 CBasicTestPolicy::~CBasicTestPolicy()
53 void CBasicTestPolicy::ConstructL()
58 void CBasicTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus)
62 iNotifierStatus = &aNotifierStatus;
64 // Create the specified test case
66 iTestCase = RTestFactory::CreateTestCaseL(aTestCaseId,EFalse);
67 iTestCase->SetTestPolicy(this);
68 iTestCase->PerformTestL();
70 // Set the request of the notifier to pending
72 *iNotifierStatus = iStatus = KRequestPending;
77 void CBasicTestPolicy::DoCancel()
81 // Cancel running the test cases
85 // Notify the test case controller that test case execution was cancelled
87 User::RequestComplete(iNotifierStatus,KErrCancel);
91 void CBasicTestPolicy::SignalTestComplete(TInt aCompletionCode)
95 // Complete the test policy request with the test case completion code
96 // (Basically self completion)
98 TRequestStatus* s = &iStatus;
99 User::RequestComplete(s,aCompletionCode);
103 void CBasicTestPolicy::RunL()
107 // Complete the request of the notifier with the test case
110 User::RequestComplete(iNotifierStatus,iStatus.Int());
112 // Destroy the test case
118 TInt CBasicTestPolicy::RunError(TInt aError)
126 CThreadTestPolicy* CThreadTestPolicy::NewL()
128 CThreadTestPolicy* self = new (ELeave) CThreadTestPolicy;
129 CleanupStack::PushL(self);
131 CleanupStack::Pop(self);
136 CThreadTestPolicy::CThreadTestPolicy()
141 CThreadTestPolicy::~CThreadTestPolicy()
151 void CThreadTestPolicy::ConstructL()
155 void CThreadTestPolicy::RunTestCaseL(const TDesC& aTestCaseId,TRequestStatus& aNotifierStatus)
159 iNotifierStatus = &aNotifierStatus;
160 RDebug::Printf("Creating thread for test case '%S'",&aTestCaseId);
167 iTestCaseId = HBufC::NewL(aTestCaseId.Length());
168 *iTestCaseId = aTestCaseId;
170 // Create the thread to run the test case in
171 TInt err(iTestThread.Create(aTestCaseId,CThreadTestPolicy::ThreadFunction,
172 KDefaultStackSize,NULL,reinterpret_cast<TAny*>(iTestCaseId)));
176 RDebug::Printf("Test thread creation unsuccessful: %d",err);
180 RDebug::Printf("Test thread '%S' created",&aTestCaseId);
181 // Start the test case in the thread
182 iTestThread.Logon(iStatus);
184 iTestThread.Resume();
185 *iNotifierStatus = KRequestPending;
189 void CThreadTestPolicy::SignalTestComplete(TInt aCompletionCode)
193 if(aCompletionCode == KErrNone)
195 RDebug::Printf("CActiveScheduler::Stop CThreadTestPolicy::SignalTestComplete");
196 CActiveScheduler::Stop();
200 RDebug::Printf("Killing thread with: %d",aCompletionCode);
201 iTestThread.Kill(aCompletionCode);
205 void CThreadTestPolicy::DoCancel()
211 TInt err(iTestThread.LogonCancel(iStatus));
214 RDebug::Printf("Unable to cancel thread logon: %d",err);
217 TRequestStatus cancelStatus;
218 iTestThread.Logon(cancelStatus);
220 iTestThread.Kill(KErrCancel);
222 User::RequestComplete(iNotifierStatus,cancelStatus.Int());
225 TInt CThreadTestPolicy::ThreadFunction(TAny* aThreadParameter)
230 TInt leaveCode(KErrNone);
232 HBufC* testCaseId = reinterpret_cast<HBufC*>(aThreadParameter);
234 // Create the cleanup stack for this thread
235 CTrapCleanup* cleanup = CTrapCleanup::New();
241 TRAP(leaveCode,err = CThreadTestPolicy::DoTestL(*testCaseId));
242 if(leaveCode != KErrNone)
244 RDebug::Printf("<Error %d> Thread '%S' DoTest",leaveCode,&testCaseId);
253 TInt CThreadTestPolicy::DoTestL(const TDesC& aTestCaseId)
258 // Create a new active scheduler for this thread
259 CActiveScheduler* sched = new (ELeave) CActiveScheduler;
260 CleanupStack::PushL(sched);
261 CActiveScheduler::Install(sched);
263 CBaseTestCase* testCase = RTestFactory::CreateTestCaseL(aTestCaseId,ETrue);
264 CleanupStack::PushL(testCase);
267 testCase->PerformTestL();
269 if(!testCase->IsHostOnly())
271 // Loop for active objects
272 RDebug::Printf("CActiveScheduler::Start in CThreadTestPolicy::DoTestL");
273 CActiveScheduler::Start();
275 // Get the test case execution result
276 err = testCase->TestResult();
279 CleanupStack::PopAndDestroy(testCase);
281 // Destroy the active scheduler
282 CleanupStack::PopAndDestroy(sched);
287 void CThreadTestPolicy::RunL()
290 TInt completionCode(iStatus.Int());
292 TExitType exitType(iTestThread.ExitType());
293 TExitCategoryName exitName(iTestThread.ExitCategory());
294 TInt exitReason(iTestThread.ExitReason());
295 RDebug::Printf("Test thread '%S' completed with completion code %d",&iTestThread.Name(),completionCode);
299 // The test thread has panicked
300 // This will occur if test API panics or RTest expression is false (i.e. test case fails)
303 RDebug::Printf("Test thread '%S' has panicked with category '%S' reason %d",
304 &iTestThread.Name(),&exitName,exitReason);
305 // May require to stop and start host/client USB depending on what panic category it is
306 // can no longer trust RUsbHubDriver/RDevUsbcClient to be in good state
307 completionCode = KErrAbort;
311 // The thread has been terminated
314 RDebug::Printf("Test thread '%S' terminated with category %s reason %d",
315 &iTestThread.Name(),&exitName,exitReason);
319 // The thread has been killed
320 // This will occur when the test thread executes normally or is cancelled
323 RDebug::Printf("Test thread '%S' has been killed with reason %d",&iTestThread.Name(),exitReason);
327 // These cases should not occur at this stage
328 case EExitPending: //follow through
330 gtest(EFalse); // Panic main thread
334 // Close the just executed test thread
337 // Complete the notifier's request status
338 User::RequestComplete(iNotifierStatus,completionCode);
341 TInt CThreadTestPolicy::RunError(TInt aError)
343 RDebug::Printf("<Error %d><Test Policy> RunError",aError);