1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/active/t_idle.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,169 @@
1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\active\t_idle.cpp
1.18 +// Overview:
1.19 +// Test the CIdle class. A CIdle and CTimer run on the same active scheduler and
1.20 +// the test displays how their callback function and RunL respectively are called.
1.21 +// API Information:
1.22 +// CIdle
1.23 +// Details:
1.24 +// - Create and install active scheduler.
1.25 +// - Create and start a timer, add this to the active scheduler, request an event
1.26 +// after a specified interval. The timer will reschedule itself for a finite
1.27 +// number of times, then it will cancel the CIdle object and then reschedule
1.28 +// itself again and eventually it will stop the active scheduler.
1.29 +// - Create a CIdle active object, start a background task that is encapsulated in
1.30 +// callback function. The CIdle will keep reschedule itself by always returning
1.31 +// TRUE from the callback.
1.32 +// - Output some text from timer's RunL and CIdle's callback to show when they are
1.33 +// called and how they interleave.
1.34 +// Platforms/Drives/Compatibility:
1.35 +// All.
1.36 +// Assumptions/Requirement/Pre-requisites:
1.37 +// Failures and causes:
1.38 +// Base Port information:
1.39 +//
1.40 +//
1.41 +
1.42 +#include <e32test.h>
1.43 +
1.44 +enum {EIdlePriority=1000,EMoreIdlePriority=800};
1.45 +
1.46 +class CMyRequestManager : public CActiveScheduler
1.47 + {
1.48 +public:
1.49 + virtual void Error(TInt anError) const;
1.50 + };
1.51 +
1.52 +class CMyTimer : public CTimer
1.53 + {
1.54 +public:
1.55 + static CMyTimer* New();
1.56 + void Start();
1.57 + virtual void RunL();
1.58 +protected:
1.59 + CMyTimer(TInt aPriority);
1.60 +private:
1.61 + enum {EMaxCount=10,EStopCount=5,ETimeReq=100000};
1.62 + TInt iCount;
1.63 + };
1.64 +
1.65 +LOCAL_D RTest test(_L("T_IDLE"));
1.66 +LOCAL_D CIdle* MoreIdle;
1.67 +LOCAL_D TInt TheCount=0;
1.68 +
1.69 +void CMyRequestManager::Error(TInt anError) const
1.70 +//
1.71 +// Called if any Run() method leaves.
1.72 +//
1.73 + {
1.74 +
1.75 + test.Panic(anError,_L("CMyRequestManager::Error"));
1.76 + }
1.77 +
1.78 +CMyTimer* CMyTimer::New()
1.79 +//
1.80 +// Create a new CMyTimer.
1.81 +//
1.82 + {
1.83 +
1.84 + return(new CMyTimer(EIdlePriority));
1.85 + }
1.86 +
1.87 +CMyTimer::CMyTimer(TInt aPriority)
1.88 +//
1.89 +// Constructor
1.90 +//
1.91 + : CTimer(aPriority),iCount(0)
1.92 + {}
1.93 +
1.94 +void CMyTimer::Start()
1.95 +//
1.96 +// The timer has completed.
1.97 +//
1.98 + {
1.99 +
1.100 + TRAPD(r, ConstructL());
1.101 + test(r==KErrNone);
1.102 + CActiveScheduler::Add(this);
1.103 + After(ETimeReq);
1.104 + }
1.105 +
1.106 +void CMyTimer::RunL()
1.107 +//
1.108 +// The timer has completed.
1.109 +//
1.110 + {
1.111 +
1.112 + iCount++;
1.113 + test.Printf(_L("Timer %03d\n"),iCount);
1.114 + if (iCount==EStopCount)
1.115 + MoreIdle->Cancel();
1.116 + if (iCount<EMaxCount)
1.117 + After(ETimeReq);
1.118 + else
1.119 + {
1.120 + test.Printf(_L("\n"));
1.121 + DoCancel();
1.122 + CActiveScheduler::Stop();
1.123 + }
1.124 + }
1.125 +
1.126 +TInt IdleResponse(TAny*)
1.127 +//
1.128 +// Respond to the idle callback
1.129 +//
1.130 + {
1.131 +
1.132 + test.Printf(_L("\rIdle response number : %6d "),++TheCount);
1.133 + return(TRUE);
1.134 + }
1.135 +
1.136 +TInt IdleCancel(TAny*)
1.137 +//
1.138 +// Cancel the other idle object
1.139 +//
1.140 + {
1.141 +
1.142 + if (TheCount>9)
1.143 + {
1.144 + MoreIdle->Cancel();
1.145 + return(FALSE);
1.146 + }
1.147 + return(TRUE);
1.148 + }
1.149 +
1.150 +GLDEF_C TInt E32Main()
1.151 +//
1.152 +// Test idle objects.
1.153 +//
1.154 + {
1.155 +
1.156 + test.Title();
1.157 + test.Start(_L("Testing idle object cancellation"));
1.158 +//
1.159 + CMyRequestManager* pR=new CMyRequestManager;
1.160 + test(pR!=NULL);
1.161 + CActiveScheduler::Install(pR);
1.162 +//
1.163 + CMyTimer* pT=CMyTimer::New();
1.164 + pT->Start();
1.165 + MoreIdle=CIdle::New(EIdlePriority);
1.166 + MoreIdle->Start(&IdleResponse);
1.167 + CActiveScheduler::Start();
1.168 +//
1.169 + test.End();
1.170 + return(0);
1.171 + }
1.172 +