author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 31 Aug 2010 16:34:26 +0300 | |
branch | RCL_3 |
changeset 43 | c1f20ce4abcf |
parent 0 | a41df078684a |
child 44 | 3e88ff8f41d5 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1995-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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32test\active\t_ctimer.cpp |
|
15 |
// Overview: |
|
16 |
// Test timer functionality. |
|
17 |
// API Information: |
|
18 |
// CTimer |
|
19 |
// Details: |
|
20 |
// - Create some timer active objects, install an active scheduler, and the |
|
21 |
// active objects, request event after an interval and verify that: |
|
22 |
// - Absolute timer set with time less than current time return KErrUnderflow. |
|
23 |
// - Relative timer set with 0 goes off immediately. |
|
24 |
// - Absolute timer set to a time of now returns KErrUnderflow. |
|
25 |
// - Absolute timer set with time more than the current time returns KErrNone. |
|
26 |
// - Repeated timer is cancelled as expected. |
|
27 |
// - Call relative timer's After function without adding it to the active scheduler |
|
28 |
// and check for panic. |
|
29 |
// - Call absolute timer's At function without adding it to the active scheduler and |
|
30 |
// check for panic. |
|
43
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
31 |
// - Call 1s inactivity timer |
0 | 32 |
// - Check if heap has been corrupted by the tests. |
33 |
// Platforms/Drives/Compatibility: |
|
34 |
// All. |
|
35 |
// Assumptions/Requirement/Pre-requisites: |
|
36 |
// Failures and causes: |
|
37 |
// Base Port information: |
|
38 |
// |
|
39 |
// |
|
40 |
||
41 |
#include <e32test.h> |
|
42 |
#include <e32panic.h> |
|
43 |
||
44 |
enum TDirective {ERelInvalidTime,ERelNotAdded,EAbNotAdded,EHighResNotAdded}; |
|
45 |
||
46 |
LOCAL_D RTest test(_L("T_CTIMER")); |
|
47 |
LOCAL_D const TInt KRepeatID=999; |
|
48 |
LOCAL_D const TInt ID1=101; |
|
49 |
LOCAL_D const TInt ID2=102; |
|
50 |
LOCAL_D const TInt ID3=103; |
|
51 |
LOCAL_D TInt A[5]; //When a timer expires its identifier is placed in here |
|
52 |
||
53 |
class TestCTimer |
|
54 |
{ |
|
55 |
public: |
|
56 |
void Test1(); |
|
57 |
void Test2(); |
|
58 |
void Test3(); |
|
59 |
}; |
|
60 |
||
61 |
class myScheduler : public CActiveScheduler |
|
62 |
{ |
|
63 |
public: |
|
64 |
virtual void Error(TInt anError)const{User::Leave(anError);} |
|
65 |
}; |
|
66 |
||
67 |
class myTimer : public CTimer |
|
68 |
{ |
|
69 |
public: |
|
70 |
myTimer(const TInt aPriority, const TInt anId):CTimer(aPriority){iIdentifier=anId; iCount=0;} |
|
71 |
void RunL(void); |
|
72 |
void Start(void); |
|
73 |
static void SetNum(const TInt aNum) {iNum=aNum; iTotalCount=0;} |
|
74 |
private: |
|
75 |
TInt iIdentifier; |
|
76 |
TInt iCount; |
|
77 |
static TInt iNum; |
|
78 |
static TInt iTotalCount; |
|
79 |
}; |
|
80 |
||
43
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
// for inactivity test |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
class myInactTimer : public CTimer |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
{ |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
public: |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
myInactTimer(const TInt aPriority):CTimer(aPriority){;} |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
void RunL(void); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
void Start(void); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
}; |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
|
0 | 90 |
TInt myTimer::iTotalCount; |
91 |
TInt myTimer::iNum; |
|
92 |
||
93 |
void myTimer::RunL(void) |
|
94 |
// |
|
95 |
// Timer has completed |
|
96 |
// |
|
97 |
{ |
|
98 |
||
99 |
A[iTotalCount++]=iIdentifier; |
|
100 |
if (iIdentifier==KRepeatID) |
|
101 |
{ |
|
102 |
if(++iCount>2) |
|
103 |
Cancel(); |
|
104 |
else |
|
105 |
After(100000); |
|
106 |
} |
|
107 |
if (iTotalCount>=iNum) |
|
108 |
CActiveScheduler::Stop(); |
|
109 |
} |
|
110 |
||
111 |
void myTimer::Start(void) |
|
112 |
// |
|
113 |
// Start a timer going. |
|
114 |
// |
|
115 |
{ |
|
116 |
||
117 |
ConstructL(); |
|
118 |
CActiveScheduler::Add(this); |
|
119 |
} |
|
120 |
||
43
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
121 |
void myInactTimer::RunL(void) |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
122 |
// |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
// Timer has completed |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
// |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
{ |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
CActiveScheduler::Stop(); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
127 |
} |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
|
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
void myInactTimer::Start(void) |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
// |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
// Start a timer going. |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
// |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
{ |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
ConstructL(); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
CActiveScheduler::Add(this); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
} |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
|
0 | 138 |
LOCAL_D TInt ThreadEntry(TAny* aDirective) |
139 |
// |
|
140 |
// Test thread |
|
141 |
// |
|
142 |
{ |
|
143 |
||
144 |
myTimer* pTimer=new myTimer(0,ID1); |
|
145 |
TTime time; |
|
146 |
switch((TUint)aDirective) |
|
147 |
{ |
|
148 |
case ERelInvalidTime: // Setting a relative timer with a negative time panics |
|
149 |
CActiveScheduler::Install(new myScheduler); |
|
150 |
pTimer->Start(); |
|
151 |
pTimer->After(-100000); |
|
152 |
break; |
|
153 |
case ERelNotAdded: // Requesting an un-added relative timer panics |
|
154 |
pTimer->After(100000); |
|
155 |
break; |
|
156 |
case EAbNotAdded: // Requesting an un-added absolute timer panics |
|
157 |
time.HomeTime(); |
|
158 |
pTimer->At(time); |
|
159 |
break; |
|
160 |
case EHighResNotAdded: // Requesting an un-added HighRes timer panics |
|
161 |
pTimer->HighRes(100000); |
|
162 |
break; |
|
163 |
} |
|
164 |
return(KErrNone); |
|
165 |
} |
|
166 |
||
167 |
void TestCTimer::Test1() |
|
168 |
// |
|
169 |
// Test timer active objects. |
|
170 |
// |
|
171 |
{ |
|
172 |
||
173 |
test.Start(_L("Create objects")); |
|
174 |
myTimer* pTimer1=new myTimer(0, ID1); |
|
175 |
myTimer* pTimer2=new myTimer(0, ID2); |
|
176 |
myTimer* pTimer3=new myTimer(0, ID3); |
|
177 |
TTime time; |
|
178 |
pTimer1->Start(); |
|
179 |
pTimer2->Start(); |
|
180 |
pTimer3->Start(); |
|
181 |
// |
|
182 |
test.Next(_L("Abs timer with time less than now set KErrUnderflow")); |
|
183 |
myTimer::SetNum(1); |
|
184 |
time.HomeTime(); |
|
185 |
pTimer1->At(time+TTimeIntervalSeconds(-1)); |
|
186 |
CActiveScheduler::Start(); |
|
187 |
test(pTimer1->iStatus==KErrUnderflow); |
|
188 |
// |
|
189 |
test.Next(_L("Rel timer with 0 goes off immediately")); |
|
190 |
myTimer::SetNum(2); |
|
191 |
pTimer1->After(0); |
|
192 |
pTimer2->After(1000000); |
|
193 |
CActiveScheduler::Start(); |
|
194 |
test(A[0]==ID1 && A[1]==ID2); |
|
195 |
// |
|
196 |
test.Next(_L("Abs timer to a time of now sets KErrUnderflow")); |
|
197 |
myTimer::SetNum(1); |
|
198 |
time.UniversalTime(); |
|
199 |
pTimer1->AtUTC(time); |
|
200 |
CActiveScheduler::Start(); |
|
201 |
test(pTimer1->iStatus==KErrUnderflow); |
|
202 |
// |
|
203 |
test.Next(_L("Abs timer set to future")); |
|
204 |
myTimer::SetNum(2); |
|
205 |
pTimer1->After(2000000); |
|
206 |
time.HomeTime(); |
|
207 |
pTimer2->At(time+TTimeIntervalSeconds(1)); |
|
208 |
CActiveScheduler::Start(); |
|
209 |
test(A[0]==ID2 && A[1]==ID1&& pTimer1->iStatus==KErrNone && pTimer2->iStatus==KErrNone); |
|
210 |
// |
|
211 |
test.Next(_L("Cancel a repeating timer after 3")); |
|
212 |
myTimer::SetNum(4); |
|
213 |
myTimer* pRepeater=new myTimer(0,KRepeatID); |
|
214 |
pRepeater->Start(); |
|
215 |
pTimer1->After(1000000); |
|
216 |
pRepeater->After(100000); |
|
217 |
CActiveScheduler::Start(); |
|
218 |
test(A[0]==KRepeatID && A[1]==KRepeatID && A[2]==KRepeatID && A[3]==ID1); |
|
219 |
||
220 |
// |
|
221 |
test.Next(_L("HighRes timer")); |
|
222 |
myTimer::SetNum(1); |
|
223 |
pTimer1->HighRes(1000000); |
|
224 |
CActiveScheduler::Start(); |
|
225 |
test(A[0]==ID1 && pTimer1->iStatus==KErrNone); |
|
226 |
// |
|
43
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
test.Next(_L("Inactivity 1s")); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
User::ResetInactivityTime(); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
myInactTimer* pInactTimer=new myInactTimer(0); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
pInactTimer->Start(); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
test.Printf(_L("inactivity...")); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
pInactTimer->Inactivity(1); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
CActiveScheduler::Start(); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
test.Printf(_L("...back")); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
test(pInactTimer->iStatus==KErrNone); |
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
// |
0 | 237 |
test.Next(_L("Destroy objects")); |
238 |
delete pTimer1; |
|
239 |
delete pTimer2; |
|
240 |
delete pTimer3; |
|
241 |
delete pRepeater; |
|
43
c1f20ce4abcf
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
242 |
delete pInactTimer; |
0 | 243 |
// |
244 |
test.End(); |
|
245 |
} |
|
246 |
||
247 |
void TestCTimer::Test2() |
|
248 |
// |
|
249 |
// Test the panics |
|
250 |
// |
|
251 |
{ |
|
252 |
||
253 |
RThread thread; |
|
254 |
TRequestStatus stat; |
|
255 |
// |
|
256 |
// Calling At or After or HighRes of an object not added to the queue panics |
|
257 |
// |
|
258 |
test.Start(_L("Queue rel when not added")); |
|
259 |
test(thread.Create(_L("myThread"),ThreadEntry,KDefaultStackSize,0x2000,0x2000,(TAny*)ERelNotAdded)==KErrNone); |
|
260 |
thread.Logon(stat); |
|
261 |
thread.Resume(); |
|
262 |
User::WaitForRequest(stat); |
|
263 |
test(thread.ExitCategory().Compare(_L("E32USER-CBase"))==0); |
|
264 |
test(thread.ExitReason()==ETimNotAdded); |
|
265 |
test(thread.ExitType()==EExitPanic); |
|
266 |
CLOSE_AND_WAIT(thread); |
|
267 |
// |
|
268 |
test.Next(_L("Queue abs when not added")); |
|
269 |
test(thread.Create(_L("myThread"),ThreadEntry,KDefaultStackSize,0x2000,0x2000,(TAny*)EAbNotAdded)==KErrNone); |
|
270 |
thread.Logon(stat); |
|
271 |
thread.Resume(); |
|
272 |
User::WaitForRequest(stat); |
|
273 |
test(thread.ExitCategory().Compare(_L("E32USER-CBase"))==0); |
|
274 |
test(thread.ExitReason()==ETimNotAdded); |
|
275 |
test(thread.ExitType()==EExitPanic); |
|
276 |
CLOSE_AND_WAIT(thread); |
|
277 |
// |
|
278 |
test.Next(_L("Queue HighRes when not added")); |
|
279 |
test(thread.Create(_L("myThread"),ThreadEntry,KDefaultStackSize,0x2000,0x2000,(TAny*)EHighResNotAdded)==KErrNone); |
|
280 |
thread.Logon(stat); |
|
281 |
thread.Resume(); |
|
282 |
User::WaitForRequest(stat); |
|
283 |
test(thread.ExitCategory().Compare(_L("E32USER-CBase"))==0); |
|
284 |
test(thread.ExitReason()==ETimNotAdded); |
|
285 |
test(thread.ExitType()==EExitPanic); |
|
286 |
CLOSE_AND_WAIT(thread); |
|
287 |
// |
|
288 |
test.End(); |
|
289 |
} |
|
290 |
||
291 |
LOCAL_D TInt Test3ThreadEntry(TAny* aArg) |
|
292 |
// |
|
293 |
// Test thread for RTimer::HighRes |
|
294 |
// |
|
295 |
{ |
|
296 |
TRequestStatus status; |
|
297 |
RTimer timer; |
|
298 |
TInt r = timer.CreateLocal(); |
|
299 |
if (r != KErrNone) |
|
300 |
return r; |
|
301 |
timer.HighRes(status, (TInt) aArg); |
|
302 |
timer.Cancel(); |
|
303 |
User::WaitForRequest(status); |
|
304 |
timer.Close(); |
|
305 |
if (status != KErrNone && status != KErrCancel) |
|
306 |
return status.Int(); |
|
307 |
return KErrNone; |
|
308 |
} |
|
309 |
||
310 |
void TestRTimerHighRes(TInt aInterval, TExitType aExpectedExitType) |
|
311 |
{ |
|
312 |
RThread thread; |
|
313 |
TRequestStatus stat; |
|
314 |
test(thread.Create(_L("myThread"),Test3ThreadEntry,KDefaultStackSize,0x2000,0x2000,(TAny*)aInterval)==KErrNone); |
|
315 |
thread.Logon(stat); |
|
316 |
thread.Resume(); |
|
317 |
User::WaitForRequest(stat); |
|
318 |
test(thread.ExitType()==aExpectedExitType); |
|
319 |
if (thread.ExitType()==EExitKill) |
|
320 |
test(thread.ExitReason()==KErrNone); |
|
321 |
CLOSE_AND_WAIT(thread); |
|
322 |
} |
|
323 |
||
324 |
void TestCTimer::Test3() |
|
325 |
// |
|
326 |
// Test RTimer::HighRes argument checking |
|
327 |
// |
|
328 |
{ |
|
329 |
test.Start(_L("Test RTimer::HighRes argument checking")); |
|
330 |
TestRTimerHighRes(0, EExitKill); |
|
331 |
TestRTimerHighRes(0x7FFFEC79, EExitKill); |
|
332 |
TestRTimerHighRes(0x7FFFFFFF, EExitKill); |
|
333 |
TestRTimerHighRes(0x80000000, EExitPanic); |
|
334 |
test.End(); |
|
335 |
} |
|
336 |
||
337 |
GLDEF_C TInt E32Main() |
|
338 |
// |
|
339 |
// Test the CTimer class |
|
340 |
// |
|
341 |
{ |
|
342 |
// don't want just in time debugging as we trap panics |
|
343 |
TBool justInTime=User::JustInTime(); |
|
344 |
User::SetJustInTime(EFalse); |
|
345 |
||
346 |
test.Title(); |
|
347 |
__UHEAP_MARK; |
|
348 |
TestCTimer T; |
|
349 |
myScheduler* pScheduler=new myScheduler; |
|
350 |
CActiveScheduler::Install(pScheduler); |
|
351 |
test.Start(_L("Test1")); |
|
352 |
T.Test1(); |
|
353 |
test.Next(_L("Test2")); |
|
354 |
T.Test2(); |
|
355 |
CActiveScheduler::Install(NULL); |
|
356 |
delete pScheduler; |
|
357 |
T.Test3(); |
|
358 |
test.End(); |
|
359 |
__UHEAP_MARKEND; |
|
360 |
||
361 |
User::SetJustInTime(justInTime); |
|
362 |
return(KErrNone); |
|
363 |
} |
|
364 |