First public contribution.
1 // Copyright (c) 1996-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 // e32test\active\t_asyc.cpp
16 // Exercise the priority mechanism of active objects whereby active
17 // objects are run in the priority order.
19 // CAsyncOneShot, CActiveScheduler.
21 // - Install active scheduler.
22 // - Create active objects of different priorities and verify their RunL
23 // is called in the priority order.
24 // - Verify that a very low priority active object will not get the chance
25 // to run if a higher priority object keeps rescheduling itself or a
26 // higher priority object stops the active scheduler.
27 // Platforms/Drives/Compatibility:
29 // Assumptions/Requirement/Pre-requisites:
30 // Failures and causes:
31 // Base Port information:
37 enum {ETopPriority=1000,EMiddlePriority=900,ELatePriority=800};
39 const TInt KIToldYouSo=666;
41 class CMyActiveScheduler : public CActiveScheduler
44 virtual void Error(TInt anError) const;
47 LOCAL_D RTest test(_L("T_ASYC"));
49 void CMyActiveScheduler::Error(TInt anError) const
51 // Called if any Run() method leaves.
55 test.Panic(anError,_L("CMyActiveScheduler::Error"));
58 class CMyMultiShot : public CAsyncOneShot
61 static CMyMultiShot* NewL(TInt aPriority,const TDesC& aMessage,TInt aCount);
63 CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount);
69 class CShouldNeverRun : public CAsyncOneShot
73 static CShouldNeverRun* NewL();
77 class CStopTheScheduler : public CAsyncOneShot
80 CStopTheScheduler(TInt aPriority);
81 static CStopTheScheduler* NewL(TInt aPriority);
85 CMyMultiShot* CMyMultiShot::NewL(TInt aPriority,const TDesC& aMessage,TInt aCount)
87 return new(ELeave)CMyMultiShot(aPriority,aMessage,aCount);
90 CMyMultiShot::CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount)
91 :CAsyncOneShot(aPriority),iMessage(aMessage)
93 iCountRemaining=aCount;
96 void CMyMultiShot::RunL()
98 if (iCountRemaining--)
100 test.Printf(_L("%S,%d\n\r"),&iMessage,iCountRemaining);
104 CShouldNeverRun* CShouldNeverRun::NewL()
106 return new(ELeave)CShouldNeverRun;
109 CShouldNeverRun::CShouldNeverRun()
110 :CAsyncOneShot(KMinTInt)
114 void CShouldNeverRun::RunL()
116 User::Panic(_L("CShouldNeverRun"),KIToldYouSo);
119 CStopTheScheduler* CStopTheScheduler::NewL(TInt aPriority)
121 return new(ELeave)CStopTheScheduler(aPriority);
124 CStopTheScheduler::CStopTheScheduler(TInt aPriority)
125 :CAsyncOneShot(aPriority)
129 void CStopTheScheduler::RunL()
131 CActiveScheduler::Stop();
134 GLDEF_C TInt E32Main()
136 // Test idle objects.
140 test.Start(_L("Testing idle object cancellation"));
142 CMyActiveScheduler* pR=new CMyActiveScheduler;
144 CActiveScheduler::Install(pR);
146 CShouldNeverRun* pn=CShouldNeverRun::NewL();
149 CStopTheScheduler* ps=CStopTheScheduler::NewL(ELatePriority);
152 CMyMultiShot* multiShot1=CMyMultiShot::NewL(EMiddlePriority,_L("Call Ten times"),10);
155 CMyMultiShot* multiShot2=CMyMultiShot::NewL(ETopPriority,_L("Call five times"),5);
158 CActiveScheduler::Start();