sl@0: // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include "t_framework.h" sl@0: sl@0: RTest test(RProcess().FileName()); sl@0: sl@0: CTestProgram::CTestProgram(const TDesC& aName) : iName(aName) sl@0: { sl@0: iThreadId = RThread().Id(); sl@0: } sl@0: sl@0: void CTestProgram::Panic() sl@0: { sl@0: TPtrC8 fd((TUint8*)iFile); sl@0: TPtrC8 cd((TUint8*)iCond); sl@0: sl@0: HBufC* fhb = HBufC::NewMax(fd.Length()); sl@0: test(fhb != 0); sl@0: HBufC* chb = HBufC::NewMax(cd.Length()); sl@0: test(chb != 0); sl@0: sl@0: fhb->Des().Copy(fd); sl@0: chb->Des().Copy(cd); sl@0: sl@0: test.Panic(iError, _L("Test '%S' Fails;\nCond: '%S'; File: '%S'; Line %d;\n"), &iName, chb, fhb, iLine); sl@0: } sl@0: sl@0: void CTestProgram::Panic(TInt aError, char* aCond, char* aFile, TInt aLine) sl@0: { sl@0: iError = aError; sl@0: iCond = aCond; sl@0: iFile = aFile; sl@0: iLine = aLine; sl@0: if (iThreadId != RThread().Id()) sl@0: { sl@0: RThread thr; sl@0: thr.Panic(_L("FAIL"), aError); sl@0: } sl@0: Panic(); sl@0: } sl@0: sl@0: void CTestProgram::Launch(TUint aCount) sl@0: { sl@0: // Remember the number of open handles. Just for a sanity check .... sl@0: TInt start_thc, start_phc; sl@0: RThread().HandleCount(start_phc, start_thc); sl@0: TF_ERROR(RThread().RequestCount(), RThread().RequestCount() == 0); sl@0: sl@0: test.Next(iName); sl@0: Run(aCount); sl@0: sl@0: // Sanity check for open handles sl@0: TInt end_thc, end_phc; sl@0: RThread().HandleCount(end_phc, end_thc); sl@0: TF_ERROR(end_thc - start_thc, start_thc == end_thc); sl@0: TF_ERROR(end_phc - start_phc, start_phc == end_phc); sl@0: // and also for pending requests ... sl@0: TF_ERROR(RThread().RequestCount(), RThread().RequestCount() == 0); sl@0: } sl@0: sl@0: void CTestProgram::LaunchGroup(CTestProgram** aGroup, TUint aCount) sl@0: { sl@0: for (CTestProgram** progP = aGroup; *progP; ++progP) sl@0: { sl@0: CTestProgram* prog = *progP; sl@0: prog->Launch(aCount); sl@0: } sl@0: } sl@0: sl@0: TInt CTestProgram::ThreadEntry(TAny* a) sl@0: { sl@0: CTestProgram* prog = (CTestProgram*) a; sl@0: prog->Run(prog->iCount); sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CTestProgram::Spawn(TUint aCount) sl@0: { sl@0: test.Next(iName); sl@0: iCount = aCount; sl@0: iStatus = KRequestPending; sl@0: RThread thr; sl@0: TInt r = thr.Create(KNullDesC, ThreadEntry, 0x2000, NULL, this); sl@0: TF_ERROR(r, r == KErrNone); sl@0: thr.NotifyDestruction(iDestroyStatus); sl@0: thr.Logon(iStatus); sl@0: thr.Resume(); sl@0: thr.Close(); sl@0: } sl@0: sl@0: void CTestProgram::Wait() sl@0: { sl@0: User::WaitForRequest(iStatus); sl@0: if (iStatus.Int() != KErrNone) sl@0: { sl@0: Panic(); sl@0: } sl@0: User::WaitForRequest(iDestroyStatus); sl@0: if (iDestroyStatus.Int() != KErrNone) sl@0: { sl@0: Panic(); sl@0: } sl@0: } sl@0: sl@0: void CTestProgram::SpawnGroup(CTestProgram** aGroup, TUint aCount) sl@0: { sl@0: test.Start(_L("=== Start Parallel Testing ===")); sl@0: CTestProgram** progP; sl@0: for (progP = aGroup; *progP; ++progP) sl@0: { sl@0: CTestProgram* prog = *progP; sl@0: prog->Spawn(aCount); sl@0: } sl@0: for (progP = aGroup; *progP; ++progP) sl@0: { sl@0: CTestProgram* prog = *progP; sl@0: prog->Wait(); sl@0: } sl@0: test.Next(_L("=== End Parallel Testing ===")); sl@0: test.End(); sl@0: } sl@0: sl@0: void CTestProgram::Exec(const TDesC& aFile, TAny* args, TInt size) sl@0: { sl@0: // sl@0: // Create the child process and pass args as a UNICODE command line. sl@0: // (we suppose that the args size is multiple of sizeof(TUint16)) sl@0: // sl@0: RProcess proc; sl@0: TInt r = proc.Create(aFile, TPtrC((TUint16*) args, size/sizeof(TUint16))); sl@0: TF_ERROR(r, r == KErrNone); sl@0: TRequestStatus status; sl@0: proc.Logon(status); sl@0: proc.Resume(); sl@0: User::WaitForRequest(status); sl@0: TF_ERROR(status.Int(), status.Int() == KErrNone); sl@0: proc.Close(); sl@0: } sl@0: sl@0: void CTestProgram::Start() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("GO!")); sl@0: } sl@0: sl@0: void CTestProgram::End() sl@0: { sl@0: test.End(); sl@0: }