First public contribution.
1 // Copyright (c) 2009-2010 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 "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.
17 @internalComponent - Internal Symbian test code
22 * This file contains the parts of CRemoteTestEnv and CRemoteTestStepBase
23 * that do not need editting when adding new tests.
25 * CRemoteTestEnv acts as the controller. It provides the means of communicating
26 * with the local side and it instantiates remote test steps and executes them in
27 * their own thread. It monitors this thread for panic / timeout. If this
28 * happens, it informs the local side.
32 #include "remotetestbase.h"
37 // Default timout for remote test steps. This MUST be smaller
38 // than any timeout passed to TEF for the local test step
39 // in the script file. Remote test steps can override this
40 // value by implementing the Timeout() function in their
42 const TInt KRemoteTestStepTimeout = 10 * 1000000;
45 //Active object used to generate a timeout if worker thread takes too long. ------
47 CTimeoutTimer* CTimeoutTimer::NewL(CRemoteTestEnv& aEnv, TInt aPriority)
49 CTimeoutTimer* obj = new (ELeave) CTimeoutTimer(aEnv, aPriority);
50 CleanupStack::PushL(obj);
52 CleanupStack::Pop(obj);
57 CTimeoutTimer::CTimeoutTimer(CRemoteTestEnv& aEnv, TInt aPriority) :
64 void CTimeoutTimer::ConstructL()
67 CActiveScheduler::Add(this);
71 CTimeoutTimer::~CTimeoutTimer()
77 void CTimeoutTimer::RunL()
79 ENDPOINT_ASSERT_DEBUG(iStatus.Int() == KErrNone, User::Invariant());
80 iEnv.TestCaseTimedOut();
83 //--------------------------------------------------------------------------------
86 //Active object used to listen to the worker thread to see if it panics. ---------
88 CWorkerListener* CWorkerListener::NewL(CRemoteTestEnv& aEnv, TInt aPriority)
90 CWorkerListener* obj = new (ELeave) CWorkerListener(aEnv, aPriority);
91 CleanupStack::PushL(obj);
93 CleanupStack::Pop(obj);
98 CWorkerListener::CWorkerListener(CRemoteTestEnv& aEnv, TInt aPriority) :
105 CWorkerListener::~CWorkerListener()
111 void CWorkerListener::ConstructL()
113 CActiveScheduler::Add(this);
117 void CWorkerListener::Listen(RThread& aThread)
119 ENDPOINT_ASSERT_DEBUG(!IsActive(), User::Invariant());
121 iThread->Logon(iStatus);
126 void CWorkerListener::RunL()
128 iEnv.WorkerExitted();
132 void CWorkerListener::DoCancel()
134 iThread->LogonCancel(iStatus);
137 //--------------------------------------------------------------------------------
140 //Active object used to listen for test case completion from the worker thread. --
142 CTestCaseListener* CTestCaseListener::NewL(CRemoteTestEnv& aEnv, TInt aPriority)
144 CTestCaseListener* obj = new (ELeave) CTestCaseListener(aEnv, aPriority);
145 CleanupStack::PushL(obj);
147 CleanupStack::Pop(obj);
152 CTestCaseListener::CTestCaseListener(CRemoteTestEnv& aEnv, TInt aPriority) :
159 CTestCaseListener::~CTestCaseListener()
165 void CTestCaseListener::ConstructL()
167 CActiveScheduler::Add(this);
171 void CTestCaseListener::Listen()
173 ENDPOINT_ASSERT_DEBUG(!IsActive(), User::Invariant());
174 iStatus = KRequestPending;
179 void CTestCaseListener::RunL()
181 ENDPOINT_ASSERT_DEBUG(iStatus.Int() == KErrNone, User::Invariant());
182 iEnv.TestCaseCompleted();
186 void CTestCaseListener::DoCancel()
188 //There is no way to actually cancel a test case,
189 //But this AO will only be cancelled when the thread
190 //has panicked or timed out - which is as good as a
191 //cancel. We still need to call canel on the AO though
192 //to set it as inactive.
194 //Also need to do a request complete if the worker has not already
195 //done it. There is no danger of the worker completing between our
196 //check (the if) and actually completing because the worker is dead.
197 if(iStatus.Int() == KRequestPending)
199 TRequestStatus* myStatus = &iStatus;
200 User::RequestComplete(myStatus, KErrCancel);
204 //--------------------------------------------------------------------------------
207 //CRemoteTestEnv -----------------------------------------------------------------
209 CRemoteTestEnv* CRemoteTestEnv::NewL()
211 CRemoteTestEnv* obj = new (ELeave) CRemoteTestEnv();
212 CleanupStack::PushL(obj);
214 CleanupStack::Pop(obj);
219 CRemoteTestEnv::CRemoteTestEnv() :
220 CActive(CActive::EPriorityStandard)
225 void CRemoteTestEnv::ConstructL()
227 //Create the message queues.
228 User::LeaveIfError(iResultOutQueue.CreateGlobal(KResultQueueName, 5));
229 User::LeaveIfError(iParamsInQueue.CreateGlobal(KParamsQueueName, 1));
231 iSupervisorId = RThread().Id();
233 //Create AOs that monitor for events.
234 //These priorities are important, since if, for example, the worker
235 //thread exits by returning from the thread entrypoint after
236 //successfully completing the EndTestStep() case, but before the
237 //supervisor runs, the supervisor's AS will find that both iWorkerListener
238 //and iTestCaseListener have completed. We use the priorities to determine
239 //which signal to run first. (The one we run will cancel the others).
240 iTimeoutTimer = CTimeoutTimer::NewL(*this, CActive::EPriorityLow);
241 iWorkerListener = CWorkerListener::NewL(*this, CActive::EPriorityStandard);
242 iTestCaseListener = CTestCaseListener::NewL(*this, CActive::EPriorityHigh);
244 //Add self to active scheduler.
245 CActiveScheduler::Add(this);
249 CRemoteTestEnv::~CRemoteTestEnv()
252 delete iTimeoutTimer;
253 delete iWorkerListener;
254 delete iTestCaseListener;
255 iParamsInQueue.Close();
256 iResultOutQueue.Close();
260 void CRemoteTestEnv::StartReceivingCmds()
263 CActiveScheduler::Start();
267 void CRemoteTestEnv::ReceiveCmd()
269 ENDPOINT_ASSERT_DEBUG(!IsActive(), User::Invariant());
270 iParamsInQueue.NotifyDataAvailable(iStatus);
275 //This is run when an packet arrives in the queue from the local side.
276 //It is not rearmed until the test step has run to completion.
277 void CRemoteTestEnv::RunL()
279 //Retrieve the packet from the queue.
280 TInt err = iParamsInQueue.Receive(iCurTestCaseParamsPacket);
281 ENDPOINT_ASSERT_DEBUG(err == KErrNone, User::Invariant());
283 //Create the appropriate TestStep and launch thread if this is a "StartTestStep".
284 if(iCurTestCaseParamsPacket.iTestCase == KStartTestStepCaseNumber)
286 //At this point in a well behaved system, iCurTestStep must be NULL.
287 //If it is not, comms has gone wrong (most likely the local side has
288 //panicked midway through the test step). If iCurTestStep is not NULL
289 //we can also guarantee that the thread is still running. Recover
290 //from this by getting the thread to do some special teardown,
291 //followed by tidying up in this thread.
294 //Logon to worker then signal to abort the test case
295 //and wait for completion.
296 TRequestStatus threadStat;
297 iCurWorker.Logon(threadStat);
298 TRequestStatus* notifyRunTestCase = &iNotifyRunTestCase;
299 iCurWorker.RequestComplete(notifyRunTestCase, KErrAbort);
300 User::WaitForRequest(threadStat);
308 TBool result = SetupTestStep();
310 //If we failed to setup the test step (invalid uid),
311 //just register for another command and return.
314 //Register to receive another packet from the message queue.
320 //Activate the TimoutTimer, TestCaseListener and WorkerListener.
321 iTimeoutTimer->After(iCurTestStep->Timeout());
322 iTestCaseListener->Listen();
323 iWorkerListener->Listen(iCurWorker);
325 //Signal the worker thread to start a test case.
326 TRequestStatus* notifyRunTestCase = &iNotifyRunTestCase;
327 iCurWorker.RequestComplete(notifyRunTestCase, KErrNone);
331 void CRemoteTestEnv::DoCancel()
333 iParamsInQueue.CancelDataAvailable();
337 //The test case can end in three ways:
338 // 1. Test Case compeletes normally (this includes failing).
339 // 2. The Test Case times out (in which case the thread is panicked).
340 // 3. The Test Case panics the worker thread.
341 //Three AOs listen for each of these possibilities and one of the functions below.
344 //This is called for case 1.
345 void CRemoteTestEnv::TestCaseCompleted()
347 //Cancel the TimeoutTimer and WorkerListener.
348 iTimeoutTimer->Cancel();
349 iWorkerListener->Cancel();
351 //Test case completed correctly, so send test result.
352 SendResult(iCurTestCaseVerdict);
354 //Tidy up if this is the end of the test step.
355 if(iCurTestCaseParamsPacket.iTestCase == KEndTestStepCaseNumber)
362 //Register to receive another packet from the message queue.
367 //This is called for case 2.
368 void CRemoteTestEnv::TestCaseTimedOut()
370 //Cancel the TestCaseListener and WorkerListener.
371 iTestCaseListener->Cancel();
372 iWorkerListener->Cancel();
374 //Thread timed out so log that it timed out and send the ERtvTimeout result.
375 iCurTestStep->REMOTE_ERR_PRINTF1(_L("Remote test step timed out."));
376 SendResult(ERtvTimeout);
378 //Tidy up. Because we timed out, we abandon the test step, so
379 //kill the thread and release even if this was not an EndTestStep
380 iCurWorker.Kill(KErrTimedOut);
385 //Register to receive another packet from the message queue.
390 //This is called for case 3.
391 void CRemoteTestEnv::WorkerExitted()
393 //Cancel the TimeoutTimer and TestCaseListener.
394 iTimeoutTimer->Cancel();
395 iTestCaseListener->Cancel();
397 //Even if we were running a EndTestStep (ie the thread will exit normally), TestCaseListener should still
398 //fire first, and it will cancel the WorkerListener - so we know that if we get here, it is because the
399 //thread exitted abnormally.
401 //Thread was panicked, so log the panic category before sending the ERtvPanic result.
402 TExitCategoryName exitCategory = iCurWorker.ExitCategory();
403 iCurTestStep->REMOTE_ERR_PRINTF3(_L("Remote test step panicked with: %S, code = %d."), &exitCategory, iCurWorker.ExitReason());
404 SendResult(ERtvPanic);
406 //Tidy up. Because we panicked, we abandon the test step, so
407 //release resources even if this was not an EndTestStep
412 //Register to receive another packet from the message queue.
417 TBool CRemoteTestEnv::SetupTestStep()
419 //Set the TRequestStatus that the worker thread triggers off for the first time.
420 //After this, the worker thread will set it back to KRequestPending itself.
421 iNotifyRunTestCase = KRequestPending;
424 TRAPD(err, iCurTestStep = CreateRemoteTestStepL(iCurTestCaseParamsPacket.iUid));
425 if(err == KErrUnknown)
427 //Unknown test step. Tell the driver app.
428 SendResult(ERtvUnknownTestUid);
431 else if(err != KErrNone || !iCurTestStep)
436 //Construct the test step base class.
437 TRAP(err, iCurTestStep->ConstructL(*this));
438 __ASSERT_ALWAYS(err == KErrNone, User::Invariant());
440 //Create Test Thread.
441 static const TInt KStackSize = 0x2000; // 8KB
442 static const TInt KHeapMinSize = 0x1000; // 4KB
443 static const TInt KHeapMaxSize = 0x1000000; // 16MB
444 TUint32 random = Math::Random();
446 _LIT(KThreadNameFormat, "%S-%u");
447 _LIT(KExecName, "EpTestRemoteExec");
448 threadName.Format(KThreadNameFormat, &KExecName, random);
449 err = iCurWorker.Create(threadName, TestThreadEntryPoint, KStackSize, KHeapMinSize, KHeapMaxSize, this);
450 __ASSERT_ALWAYS(err == KErrNone, User::Invariant());
452 //Start the test thread.
459 // The DoEglHeapMark and DoEglHeapCheck are intended to make sure memory
460 // allocations are freed when the testing is complete. The current
461 // implementation only supports the Symbian/Nokia reference implementation.
462 // An implementor of another EGL implementation is free to add their own
463 // variant of heapchecking here, with suitable #if around it.
464 // The function in egl should call __DbgMarkStart() and __DbgMarkEnd()
465 // on the heap for the egl implementation - or the equivalent if the
466 // heap is not a typical Symbian heap.
467 void CRemoteTestEnv::DoEglHeapMark()
469 #if USE_EGLHEAP_CHECKING
470 typedef void (*TEglDebugHeapMarkStartPtr)();
472 TEglDebugHeapMarkStartPtr heapMarkStart = reinterpret_cast<TEglDebugHeapMarkStartPtr>(eglGetProcAddress("egliDebugHeapMarkStart"));
480 void CRemoteTestEnv::DoEglHeapCheck()
482 #if USE_EGLHEAP_CHECKING
483 typedef EGLint (*TEglDebugHeapMarkEndPtr)(EGLint count);
485 TEglDebugHeapMarkEndPtr heapMarkEnd = reinterpret_cast<TEglDebugHeapMarkEndPtr>(eglGetProcAddress("egliDebugHeapMarkEnd"));
488 (void)heapMarkEnd(0);
493 #define __EGLHEAP_MARK DoEglHeapMark()
494 #define __EGLHEAP_MARKEND DoEglHeapCheck()
497 void CRemoteTestEnv::RunCurrentTestStepL()
499 TInt processHandleMarkDummy;
500 TInt threadHandleMarkStart;
501 TInt threadHandleMarkEnd;
502 TBool finished = EFalse;
506 //Wait to be signalled to run a test case.
507 User::WaitForRequest(iNotifyRunTestCase);
509 //We are aborting the test step. Tidy up EGL and exit.
510 if(iNotifyRunTestCase.Int() == KErrAbort)
512 iCurTestStep->EglEndL();
513 iNotifyRunTestCase = KRequestPending;
517 //Rearm the TRequestStatus (The first arming is done in the supervisor thread).
518 iNotifyRunTestCase = KRequestPending;
520 //Run the test case and panic if it leaves. Start/End are just special test cases.
521 if(iCurTestCaseParamsPacket.iTestCase == KStartTestStepCaseNumber)
523 //Mark the user heap & thread handle count (we don't care about the process handle count).
524 RThread().HandleCount(processHandleMarkDummy, threadHandleMarkStart);
529 TRAPD(err, iCurTestCaseVerdict = iCurTestStep->DoStartRemoteTestStepL(iCurTestCaseParamsPacket.iParams));
530 __ASSERT_ALWAYS(err == KErrNone, User::Panic(_L("tried to leave."), __LINE__));
532 else if(iCurTestCaseParamsPacket.iTestCase == KEndTestStepCaseNumber)
535 TRAPD(err, iCurTestCaseVerdict = iCurTestStep->DoEndRemoteTestStepL(iCurTestCaseParamsPacket.iParams));
536 __ASSERT_ALWAYS(err == KErrNone, User::Panic(_L("tried to leave."), __LINE__));
538 //This will cause a panic if the test step leaked memory or thread handles.
541 RThread().HandleCount(processHandleMarkDummy, threadHandleMarkEnd);
542 __ASSERT_ALWAYS(threadHandleMarkStart == threadHandleMarkEnd, User::Panic(_L("leaked handles."), KErrBadHandle));
544 //Exit the loop (and eventually the thread).
549 //Run a regular Test Case.
550 TRAPD(err, iCurTestCaseVerdict = iCurTestStep->DoRunRemoteTestCaseL(iCurTestCaseParamsPacket.iTestCase, iCurTestCaseParamsPacket.iParams));
551 __ASSERT_ALWAYS(err == KErrNone, User::Panic(_L("tried to leave."), __LINE__));
554 //Notify the supervisor that we have completed the test case.
556 TInt err = supervisor.Open(iSupervisorId);
557 __ASSERT_ALWAYS(err == KErrNone, User::Panic(_L("framework error."), __LINE__));
558 TRequestStatus* notifyFinishTestCase = &iTestCaseListener->iStatus;
559 supervisor.RequestComplete(notifyFinishTestCase, KErrNone);
565 TInt CRemoteTestEnv::TestThreadEntryPoint(TAny* aSelf)
567 CRemoteTestEnv* self = static_cast<CRemoteTestEnv*>(aSelf);
569 //Create cleanup stack.
570 CTrapCleanup* cleanup = CTrapCleanup::New();
573 //Create active scheduler.
574 CActiveScheduler* scheduler = new CActiveScheduler();
576 CActiveScheduler::Install(scheduler);
578 TRAPD(err, self->RunCurrentTestStepL());
579 __ASSERT_ALWAYS(err == KErrNone, User::Invariant());
588 void CRemoteTestEnv::SendResult(TRemoteTestVerdict aVerdict)
590 iResultOutQueue.SendBlocking(TRemoteTestResult(iCurTestCaseParamsPacket.iUid, iCurTestCaseParamsPacket.iTestCase, aVerdict));
594 void CRemoteTestEnv::SendLog(const TDesC8& aFile, TInt aLine, TInt aSeverity, const TDesC& aMessage)
596 iResultOutQueue.SendBlocking(TRemoteTestResult(iCurTestCaseParamsPacket.iUid, iCurTestCaseParamsPacket.iTestCase, aFile, aLine, aSeverity, aMessage));
599 //--------------------------------------------------------------------------------
602 //CRemoteTestStepBase ------------------------------------------------------------
604 CRemoteTestStepBase::CRemoteTestStepBase(TTestUid aUid) :
610 void CRemoteTestStepBase::ConstructL(CRemoteTestEnv& aTestEnv)
612 iTestEnv = &aTestEnv;
613 if (iEndpoint.Error() != KErrNone)
615 RDebug::Printf("Could not construct CRemoteTestStepBase"
616 " - is EglEndpointNOK enabled?? -- Error: %d", iEndpoint.Error());
617 User::Leave(iEndpoint.Error());
622 CRemoteTestStepBase::~CRemoteTestStepBase()
627 TRemoteTestVerdict CRemoteTestStepBase::DoStartRemoteTestStepL(const TRemoteTestParams& /*aMessageIn*/)
629 //Default implementation does nothing.
634 TRemoteTestVerdict CRemoteTestStepBase::DoEndRemoteTestStepL(const TRemoteTestParams& /*aMessageIn*/)
636 //Default implementation does nothing.
641 TInt CRemoteTestStepBase::Timeout() const
643 return KRemoteTestStepTimeout;
647 class TOverflowTruncate : public TDesOverflow
650 virtual void Overflow(TDes& /*aDes*/)
652 //Do nothing - just let it truncate.
657 void CRemoteTestStepBase::Log(const TText8* aFile, TInt aLine, TInt aSeverity, TRefByValue<const TDesC> aFmt, ...)
661 TOverflowTruncate overflow;
663 VA_START(list, aFmt);
665 buf.AppendFormatList(aFmt, list, &overflow);
667 iTestEnv->SendLog(file, aLine, aSeverity, buf);
672 void CRemoteTestStepBase::EglStartL()
674 eglInitialize(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL);
675 if (eglGetError()!=EGL_SUCCESS)
677 REMOTE_INFO_PRINTF1(_L("could not initialise egl"));
678 User::Leave(KErrGeneral);
683 void CRemoteTestStepBase::EglEndL()
685 eglTerminate(eglGetDisplay(EGL_DEFAULT_DISPLAY));
686 if (eglGetError()!=EGL_SUCCESS)
688 REMOTE_INFO_PRINTF1(_L("could not terminate egl"));
689 User::Leave(KErrGeneral);
695 const TEglEndpointWrap& CRemoteTestStepBase::EglEndpoint() const
700 //--------------------------------------------------------------------------------