sl@0: // Copyright (c) 1995-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: // e32test\active\t_idle.cpp sl@0: // Overview: sl@0: // Test the CIdle class. A CIdle and CTimer run on the same active scheduler and sl@0: // the test displays how their callback function and RunL respectively are called. sl@0: // API Information: sl@0: // CIdle sl@0: // Details: sl@0: // - Create and install active scheduler. sl@0: // - Create and start a timer, add this to the active scheduler, request an event sl@0: // after a specified interval. The timer will reschedule itself for a finite sl@0: // number of times, then it will cancel the CIdle object and then reschedule sl@0: // itself again and eventually it will stop the active scheduler. sl@0: // - Create a CIdle active object, start a background task that is encapsulated in sl@0: // callback function. The CIdle will keep reschedule itself by always returning sl@0: // TRUE from the callback. sl@0: // - Output some text from timer's RunL and CIdle's callback to show when they are sl@0: // called and how they interleave. sl@0: // Platforms/Drives/Compatibility: sl@0: // All. sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: enum {EIdlePriority=1000,EMoreIdlePriority=800}; sl@0: sl@0: class CMyRequestManager : public CActiveScheduler sl@0: { sl@0: public: sl@0: virtual void Error(TInt anError) const; sl@0: }; sl@0: sl@0: class CMyTimer : public CTimer sl@0: { sl@0: public: sl@0: static CMyTimer* New(); sl@0: void Start(); sl@0: virtual void RunL(); sl@0: protected: sl@0: CMyTimer(TInt aPriority); sl@0: private: sl@0: enum {EMaxCount=10,EStopCount=5,ETimeReq=100000}; sl@0: TInt iCount; sl@0: }; sl@0: sl@0: LOCAL_D RTest test(_L("T_IDLE")); sl@0: LOCAL_D CIdle* MoreIdle; sl@0: LOCAL_D TInt TheCount=0; sl@0: sl@0: void CMyRequestManager::Error(TInt anError) const sl@0: // sl@0: // Called if any Run() method leaves. sl@0: // sl@0: { sl@0: sl@0: test.Panic(anError,_L("CMyRequestManager::Error")); sl@0: } sl@0: sl@0: CMyTimer* CMyTimer::New() sl@0: // sl@0: // Create a new CMyTimer. sl@0: // sl@0: { sl@0: sl@0: return(new CMyTimer(EIdlePriority)); sl@0: } sl@0: sl@0: CMyTimer::CMyTimer(TInt aPriority) sl@0: // sl@0: // Constructor sl@0: // sl@0: : CTimer(aPriority),iCount(0) sl@0: {} sl@0: sl@0: void CMyTimer::Start() sl@0: // sl@0: // The timer has completed. sl@0: // sl@0: { sl@0: sl@0: TRAPD(r, ConstructL()); sl@0: test(r==KErrNone); sl@0: CActiveScheduler::Add(this); sl@0: After(ETimeReq); sl@0: } sl@0: sl@0: void CMyTimer::RunL() sl@0: // sl@0: // The timer has completed. sl@0: // sl@0: { sl@0: sl@0: iCount++; sl@0: test.Printf(_L("Timer %03d\n"),iCount); sl@0: if (iCount==EStopCount) sl@0: MoreIdle->Cancel(); sl@0: if (iCount9) sl@0: { sl@0: MoreIdle->Cancel(); sl@0: return(FALSE); sl@0: } sl@0: return(TRUE); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: // sl@0: // Test idle objects. sl@0: // sl@0: { sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Testing idle object cancellation")); sl@0: // sl@0: CMyRequestManager* pR=new CMyRequestManager; sl@0: test(pR!=NULL); sl@0: CActiveScheduler::Install(pR); sl@0: // sl@0: CMyTimer* pT=CMyTimer::New(); sl@0: pT->Start(); sl@0: MoreIdle=CIdle::New(EIdlePriority); sl@0: MoreIdle->Start(&IdleResponse); sl@0: CActiveScheduler::Start(); sl@0: // sl@0: test.End(); sl@0: return(0); sl@0: } sl@0: