271
|
1 |
// Copyright (c) 1996-2010 Nokia Corporation and/or its subsidiary(-ies).
|
0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32test\active\t_asyc.cpp
|
|
15 |
// Overview:
|
|
16 |
// Exercise the priority mechanism of active objects whereby active
|
|
17 |
// objects are run in the priority order.
|
|
18 |
// API Information:
|
271
|
19 |
// CAsyncOneShot, CAsyncCallBack, CActiveScheduler.
|
0
|
20 |
// Details:
|
|
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.
|
271
|
27 |
// - Do the same tests again by using CAsyncOneShot derived class
|
|
28 |
// CAsyncCallBack which has a CallBack function to do the stuff
|
0
|
29 |
// Platforms/Drives/Compatibility:
|
|
30 |
// All.
|
|
31 |
// Assumptions/Requirement/Pre-requisites:
|
|
32 |
// Failures and causes:
|
|
33 |
// Base Port information:
|
|
34 |
//
|
|
35 |
//
|
|
36 |
|
|
37 |
#include <e32test.h>
|
|
38 |
|
271
|
39 |
LOCAL_D RTest test(_L("T_ASYC"));
|
|
40 |
|
0
|
41 |
enum {ETopPriority=1000,EMiddlePriority=900,ELatePriority=800};
|
|
42 |
|
|
43 |
const TInt KIToldYouSo=666;
|
|
44 |
|
271
|
45 |
|
|
46 |
//
|
|
47 |
|
0
|
48 |
class CMyActiveScheduler : public CActiveScheduler
|
|
49 |
{
|
|
50 |
public:
|
|
51 |
virtual void Error(TInt anError) const;
|
|
52 |
};
|
|
53 |
|
|
54 |
void CMyActiveScheduler::Error(TInt anError) const
|
|
55 |
//
|
|
56 |
// Called if any Run() method leaves.
|
|
57 |
//
|
|
58 |
{
|
|
59 |
|
|
60 |
test.Panic(anError,_L("CMyActiveScheduler::Error"));
|
|
61 |
}
|
|
62 |
|
271
|
63 |
//
|
|
64 |
// CAsyncOneShot derived class declarations
|
|
65 |
//
|
|
66 |
|
0
|
67 |
class CMyMultiShot : public CAsyncOneShot
|
|
68 |
{
|
|
69 |
public:
|
|
70 |
static CMyMultiShot* NewL(TInt aPriority,const TDesC& aMessage,TInt aCount);
|
|
71 |
void RunL();
|
|
72 |
CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount);
|
|
73 |
private:
|
|
74 |
TInt iCountRemaining;
|
|
75 |
TPtrC iMessage;
|
|
76 |
};
|
|
77 |
|
271
|
78 |
//
|
|
79 |
|
0
|
80 |
class CShouldNeverRun : public CAsyncOneShot
|
|
81 |
{
|
|
82 |
public:
|
|
83 |
void RunL();
|
|
84 |
static CShouldNeverRun* NewL();
|
271
|
85 |
CShouldNeverRun(TInt aValue);
|
0
|
86 |
};
|
|
87 |
|
271
|
88 |
//
|
|
89 |
|
0
|
90 |
class CStopTheScheduler : public CAsyncOneShot
|
|
91 |
{
|
|
92 |
public:
|
|
93 |
CStopTheScheduler(TInt aPriority);
|
|
94 |
static CStopTheScheduler* NewL(TInt aPriority);
|
|
95 |
void RunL();
|
|
96 |
};
|
|
97 |
|
271
|
98 |
//
|
|
99 |
// CAsyncCallBack derived class declarations
|
|
100 |
//
|
|
101 |
|
|
102 |
class CMyMultiShotACB : public CAsyncCallBack
|
|
103 |
{
|
|
104 |
public:
|
|
105 |
static CMyMultiShotACB* NewL(TInt aPriority,const TDesC& aMessage,TInt aCount);
|
|
106 |
void RunL(); //needs to be implemented since CAsyncCallBack::RunL is not exported
|
|
107 |
static TInt CallBackFunc(TAny* aPtr); // this is called by the CAsyncCallBack::RunL
|
|
108 |
CMyMultiShotACB(const TCallBack& aCallBack, TInt aPriority, const TDesC& aMessage, TInt aCount);
|
|
109 |
private:
|
|
110 |
TInt iCountRemaining;
|
|
111 |
TPtrC iMessage;
|
|
112 |
};
|
|
113 |
|
|
114 |
//
|
|
115 |
|
|
116 |
class CShouldNeverRunACB : public CAsyncCallBack
|
|
117 |
{
|
|
118 |
public:
|
|
119 |
void RunL(); //needs to be implemented since CAsyncCallBack::RunL is not exported
|
|
120 |
static TInt CallBackFunc(TAny*); // this is called by the CAsyncCallBack::RunL
|
|
121 |
static CShouldNeverRunACB* NewL();
|
|
122 |
CShouldNeverRunACB();
|
|
123 |
};
|
|
124 |
|
|
125 |
//
|
|
126 |
|
|
127 |
class CStopTheSchedulerACB : public CAsyncCallBack
|
|
128 |
{
|
|
129 |
public:
|
|
130 |
CStopTheSchedulerACB(TInt aPriority);
|
|
131 |
static CStopTheSchedulerACB* NewL(TInt aPriority);
|
|
132 |
static TInt CallBackFunc(TAny*); // this is called by the CAsyncCallBack::RunL
|
|
133 |
void RunL(); //needs to be implemented since CAsyncCallBack::RunL is not exported
|
|
134 |
};
|
|
135 |
|
|
136 |
//
|
|
137 |
// CAsyncOneShot implementations
|
|
138 |
//
|
|
139 |
|
0
|
140 |
CMyMultiShot* CMyMultiShot::NewL(TInt aPriority,const TDesC& aMessage,TInt aCount)
|
|
141 |
{
|
|
142 |
return new(ELeave)CMyMultiShot(aPriority,aMessage,aCount);
|
|
143 |
}
|
|
144 |
|
|
145 |
CMyMultiShot::CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount)
|
|
146 |
:CAsyncOneShot(aPriority),iMessage(aMessage)
|
|
147 |
{
|
|
148 |
iCountRemaining=aCount;
|
|
149 |
}
|
|
150 |
|
|
151 |
void CMyMultiShot::RunL()
|
|
152 |
{
|
|
153 |
if (iCountRemaining--)
|
|
154 |
{
|
|
155 |
test.Printf(_L("%S,%d\n\r"),&iMessage,iCountRemaining);
|
|
156 |
Call();
|
|
157 |
}
|
|
158 |
}
|
271
|
159 |
|
0
|
160 |
CShouldNeverRun* CShouldNeverRun::NewL()
|
|
161 |
{
|
271
|
162 |
return new(ELeave)CShouldNeverRun(KMinTInt);
|
0
|
163 |
}
|
|
164 |
|
271
|
165 |
CShouldNeverRun::CShouldNeverRun(TInt aValue)
|
|
166 |
:CAsyncOneShot(aValue)
|
0
|
167 |
{
|
|
168 |
}
|
|
169 |
|
|
170 |
void CShouldNeverRun::RunL()
|
|
171 |
{
|
|
172 |
User::Panic(_L("CShouldNeverRun"),KIToldYouSo);
|
|
173 |
}
|
|
174 |
|
|
175 |
CStopTheScheduler* CStopTheScheduler::NewL(TInt aPriority)
|
|
176 |
{
|
|
177 |
return new(ELeave)CStopTheScheduler(aPriority);
|
|
178 |
}
|
|
179 |
|
|
180 |
CStopTheScheduler::CStopTheScheduler(TInt aPriority)
|
|
181 |
:CAsyncOneShot(aPriority)
|
|
182 |
{
|
|
183 |
}
|
|
184 |
|
|
185 |
void CStopTheScheduler::RunL()
|
|
186 |
{
|
|
187 |
CActiveScheduler::Stop();
|
|
188 |
}
|
|
189 |
|
271
|
190 |
//
|
|
191 |
// CAsyncCallBack derived implementations
|
|
192 |
//
|
|
193 |
|
|
194 |
CMyMultiShotACB* CMyMultiShotACB::NewL(TInt aPriority,const TDesC& aMessage,TInt aCount)
|
|
195 |
{
|
|
196 |
TCallBack myCallBack(CMyMultiShotACB::CallBackFunc);
|
|
197 |
return new(ELeave)CMyMultiShotACB(myCallBack, aPriority,aMessage,aCount);
|
|
198 |
}
|
|
199 |
|
|
200 |
CMyMultiShotACB::CMyMultiShotACB(const TCallBack& aCallBack, TInt aPriority,const TDesC& aMessage,TInt aCount)
|
|
201 |
:CAsyncCallBack(aCallBack ,aPriority),iMessage(aMessage)
|
|
202 |
// this is calling the 2 parameter constructor for CAsyncCallBack
|
|
203 |
{
|
|
204 |
iCallBack.iPtr=this;
|
|
205 |
iCountRemaining=aCount;
|
|
206 |
}
|
|
207 |
|
|
208 |
TInt CMyMultiShotACB::CallBackFunc(TAny* aPtr)
|
|
209 |
{
|
|
210 |
CMyMultiShotACB* pMultiShot=(CMyMultiShotACB*)aPtr;
|
|
211 |
|
|
212 |
if (pMultiShot->iCountRemaining--)
|
|
213 |
{
|
|
214 |
test.Printf(_L("%S,%d\n\r"),&(pMultiShot->iMessage),pMultiShot->iCountRemaining);
|
|
215 |
pMultiShot->CallBack();
|
|
216 |
}
|
|
217 |
return KErrNone;
|
|
218 |
}
|
|
219 |
|
|
220 |
void CMyMultiShotACB::RunL()
|
|
221 |
{
|
|
222 |
iCallBack.CallBack();
|
|
223 |
}
|
|
224 |
|
|
225 |
CShouldNeverRunACB* CShouldNeverRunACB::NewL()
|
|
226 |
{
|
|
227 |
return new(ELeave)CShouldNeverRunACB;
|
|
228 |
}
|
|
229 |
|
|
230 |
CShouldNeverRunACB::CShouldNeverRunACB()
|
|
231 |
:CAsyncCallBack(KMinTInt)
|
|
232 |
// this is calling the 1 parameter constructor for CAsyncCallBack
|
|
233 |
// callback function needs to be set before setting active using ::Set method
|
|
234 |
{
|
|
235 |
}
|
|
236 |
|
|
237 |
TInt CShouldNeverRunACB::CallBackFunc(TAny*)
|
|
238 |
{
|
|
239 |
User::Panic(_L("CShouldNeverRunACB"),KIToldYouSo);
|
|
240 |
return KErrNone;
|
|
241 |
}
|
|
242 |
|
|
243 |
void CShouldNeverRunACB::RunL()
|
|
244 |
{
|
|
245 |
iCallBack.CallBack();
|
|
246 |
}
|
|
247 |
|
|
248 |
CStopTheSchedulerACB* CStopTheSchedulerACB::NewL(TInt aPriority)
|
|
249 |
{
|
|
250 |
return new(ELeave)CStopTheSchedulerACB(aPriority);
|
|
251 |
}
|
|
252 |
|
|
253 |
CStopTheSchedulerACB::CStopTheSchedulerACB(TInt aPriority)
|
|
254 |
:CAsyncCallBack(aPriority)
|
|
255 |
// this is calling the 1 parameter constructor for CAsyncCallBack
|
|
256 |
// callback function needs to be set before setting active using ::Set method
|
|
257 |
{
|
|
258 |
}
|
|
259 |
|
|
260 |
TInt CStopTheSchedulerACB::CallBackFunc(TAny*)
|
|
261 |
{
|
|
262 |
CActiveScheduler::Stop();
|
|
263 |
return KErrNone;
|
|
264 |
}
|
|
265 |
|
|
266 |
void CStopTheSchedulerACB::RunL()
|
|
267 |
{
|
|
268 |
iCallBack.CallBack();
|
|
269 |
}
|
|
270 |
|
|
271 |
|
0
|
272 |
GLDEF_C TInt E32Main()
|
|
273 |
//
|
|
274 |
// Test idle objects.
|
|
275 |
//
|
|
276 |
{
|
|
277 |
test.Title();
|
271
|
278 |
test.Start(_L("Testing CAsyncOneShot derived objects"));
|
0
|
279 |
//
|
|
280 |
CMyActiveScheduler* pR=new CMyActiveScheduler;
|
|
281 |
test(pR!=NULL);
|
|
282 |
CActiveScheduler::Install(pR);
|
|
283 |
//
|
|
284 |
CShouldNeverRun* pn=CShouldNeverRun::NewL();
|
|
285 |
pn->Call();
|
|
286 |
|
|
287 |
CStopTheScheduler* ps=CStopTheScheduler::NewL(ELatePriority);
|
|
288 |
ps->Call();
|
|
289 |
|
|
290 |
CMyMultiShot* multiShot1=CMyMultiShot::NewL(EMiddlePriority,_L("Call Ten times"),10);
|
|
291 |
multiShot1->Call();
|
|
292 |
|
|
293 |
CMyMultiShot* multiShot2=CMyMultiShot::NewL(ETopPriority,_L("Call five times"),5);
|
|
294 |
multiShot2->Call();
|
|
295 |
|
|
296 |
CActiveScheduler::Start();
|
271
|
297 |
|
|
298 |
test.Next(_L("Test DoCancel"));
|
|
299 |
// put to same priority as the CStopScheduler
|
|
300 |
CShouldNeverRun* pn2=new CShouldNeverRun(ELatePriority);
|
|
301 |
|
|
302 |
pn2->Call(); // queue the panic object
|
|
303 |
|
|
304 |
ps->Call(); // queue stop scheduler object
|
|
305 |
|
|
306 |
pn2->DoCancel(); //cancel immediately (panic if cancel fails)
|
|
307 |
|
|
308 |
CActiveScheduler::Start();
|
|
309 |
|
|
310 |
// cleanup, call destructors
|
|
311 |
delete pn;
|
|
312 |
delete pn2;
|
|
313 |
delete ps;
|
|
314 |
delete multiShot1;
|
|
315 |
delete multiShot2;
|
|
316 |
|
0
|
317 |
//
|
271
|
318 |
test.Next(_L("Testing CAsyncCallBack derived objects"));
|
|
319 |
|
|
320 |
CShouldNeverRunACB* pnACB=CShouldNeverRunACB::NewL();
|
|
321 |
TCallBack myCB1(CShouldNeverRunACB::CallBackFunc,pn);
|
|
322 |
pnACB->Set(myCB1); // set callback func
|
|
323 |
pnACB->CallBack(); // set active
|
|
324 |
|
|
325 |
CStopTheSchedulerACB* psACB=CStopTheSchedulerACB::NewL(ELatePriority);
|
|
326 |
TCallBack myCB2(CStopTheSchedulerACB::CallBackFunc,ps);
|
|
327 |
psACB->Set(myCB2); // set callback func
|
|
328 |
psACB->CallBack(); // set active
|
|
329 |
|
|
330 |
CMyMultiShotACB* multiShot1ACB=CMyMultiShotACB::NewL(EMiddlePriority,_L("Call Ten times"),10);
|
|
331 |
multiShot1ACB->CallBack(); // set active
|
|
332 |
|
|
333 |
CMyMultiShotACB* multiShot2ACB=CMyMultiShotACB::NewL(ETopPriority,_L("Call five times"),5);
|
|
334 |
multiShot2ACB->CallBack(); // set active
|
|
335 |
|
|
336 |
CActiveScheduler::Start();
|
|
337 |
|
|
338 |
// cleanup, call destructors
|
|
339 |
delete pR;
|
|
340 |
delete pnACB;
|
|
341 |
delete psACB;
|
|
342 |
delete multiShot1ACB;
|
|
343 |
delete multiShot2ACB;
|
|
344 |
|
0
|
345 |
test.End();
|
|
346 |
return(0);
|
|
347 |
}
|
|
348 |
|