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\system\t_inact.cpp
|
|
15 |
// Test inactivity timers
|
|
16 |
//
|
|
17 |
//
|
|
18 |
|
|
19 |
#include <e32test.h>
|
|
20 |
|
|
21 |
RTest test(_L("T_INACT"));
|
|
22 |
|
|
23 |
class CTimer1 : public CTimer
|
|
24 |
{
|
|
25 |
public:
|
|
26 |
static CTimer1* NewL();
|
|
27 |
CTimer1();
|
|
28 |
void Start();
|
|
29 |
virtual void RunL();
|
|
30 |
};
|
|
31 |
|
|
32 |
class CTimer2 : public CTimer
|
|
33 |
{
|
|
34 |
public:
|
|
35 |
static CTimer2* NewL();
|
|
36 |
CTimer2();
|
|
37 |
void Start();
|
|
38 |
virtual void RunL();
|
|
39 |
};
|
|
40 |
|
|
41 |
class CTimer3 : public CTimer
|
|
42 |
{
|
|
43 |
public:
|
|
44 |
static CTimer3* NewL();
|
|
45 |
CTimer3();
|
|
46 |
void Start();
|
|
47 |
virtual void RunL();
|
|
48 |
public:
|
|
49 |
TInt iInactive;
|
|
50 |
};
|
|
51 |
|
|
52 |
class CRepeatedTimer : public CTimer
|
|
53 |
{
|
|
54 |
public:
|
|
55 |
static CRepeatedTimer* NewL(TInt aCount, TInt aPeriod);
|
|
56 |
CRepeatedTimer();
|
|
57 |
void Start();
|
|
58 |
virtual void RunL();
|
|
59 |
public:
|
|
60 |
TInt iCount;
|
|
61 |
TInt iPeriod;
|
|
62 |
};
|
|
63 |
|
|
64 |
CActiveScheduler* ActiveSched;
|
|
65 |
CTimer1* Timer1;
|
|
66 |
CTimer2* Timer2;
|
|
67 |
CTimer3* Timer3;
|
|
68 |
CRepeatedTimer* RepTimer;
|
|
69 |
|
|
70 |
CTimer1::CTimer1()
|
|
71 |
: CTimer(EPriorityStandard)
|
|
72 |
{
|
|
73 |
}
|
|
74 |
|
|
75 |
CTimer1* CTimer1::NewL()
|
|
76 |
{
|
|
77 |
CTimer1* pT=new (ELeave) CTimer1;
|
|
78 |
CleanupStack::PushL(pT);
|
|
79 |
pT->ConstructL();
|
|
80 |
CleanupStack::Pop();
|
|
81 |
return pT;
|
|
82 |
}
|
|
83 |
|
|
84 |
void CTimer1::Start()
|
|
85 |
{
|
|
86 |
Inactivity(7);
|
|
87 |
}
|
|
88 |
|
|
89 |
void CTimer1::RunL()
|
|
90 |
{
|
|
91 |
test.Printf(_L("CTimer1 expired\n"));
|
|
92 |
Start();
|
|
93 |
}
|
|
94 |
|
|
95 |
CTimer2::CTimer2()
|
|
96 |
: CTimer(EPriorityStandard)
|
|
97 |
{
|
|
98 |
}
|
|
99 |
|
|
100 |
CTimer2* CTimer2::NewL()
|
|
101 |
{
|
|
102 |
CTimer2* pT=new (ELeave) CTimer2;
|
|
103 |
CleanupStack::PushL(pT);
|
|
104 |
pT->ConstructL();
|
|
105 |
CleanupStack::Pop();
|
|
106 |
return pT;
|
|
107 |
}
|
|
108 |
|
|
109 |
void CTimer2::Start()
|
|
110 |
{
|
|
111 |
Inactivity(13);
|
|
112 |
}
|
|
113 |
|
|
114 |
void CTimer2::RunL()
|
|
115 |
{
|
|
116 |
test.Printf(_L("CTimer2 expired\n"));
|
|
117 |
Start();
|
|
118 |
}
|
|
119 |
|
|
120 |
CTimer3::CTimer3()
|
|
121 |
: CTimer(EPriorityStandard)
|
|
122 |
{
|
|
123 |
}
|
|
124 |
|
|
125 |
CTimer3* CTimer3::NewL()
|
|
126 |
{
|
|
127 |
CTimer3* pT=new (ELeave) CTimer3;
|
|
128 |
CleanupStack::PushL(pT);
|
|
129 |
pT->ConstructL();
|
|
130 |
CleanupStack::Pop();
|
|
131 |
return pT;
|
|
132 |
}
|
|
133 |
|
|
134 |
void CTimer3::Start()
|
|
135 |
{
|
|
136 |
iInactive=User::InactivityTime().Int();
|
|
137 |
After(500000);
|
|
138 |
}
|
|
139 |
|
|
140 |
void CTimer3::RunL()
|
|
141 |
{
|
|
142 |
TInt inactive=User::InactivityTime().Int();
|
|
143 |
if (inactive!=iInactive)
|
|
144 |
{
|
|
145 |
iInactive=inactive;
|
|
146 |
test.Printf(_L("%d\n"),inactive);
|
|
147 |
}
|
|
148 |
After(500000);
|
|
149 |
}
|
|
150 |
|
|
151 |
CRepeatedTimer::CRepeatedTimer()
|
|
152 |
: CTimer(EPriorityStandard)
|
|
153 |
{
|
|
154 |
}
|
|
155 |
|
|
156 |
CRepeatedTimer* CRepeatedTimer::NewL(TInt aCount, TInt aPeriod)
|
|
157 |
{
|
|
158 |
CRepeatedTimer* pT=new (ELeave) CRepeatedTimer;
|
|
159 |
pT->iCount=aCount;
|
|
160 |
pT->iPeriod=aPeriod;
|
|
161 |
CleanupStack::PushL(pT);
|
|
162 |
pT->ConstructL();
|
|
163 |
CleanupStack::Pop();
|
|
164 |
return pT;
|
|
165 |
}
|
|
166 |
|
|
167 |
void CRepeatedTimer::Start()
|
|
168 |
{
|
|
169 |
Inactivity(iPeriod);
|
|
170 |
}
|
|
171 |
|
|
172 |
void CRepeatedTimer::RunL()
|
|
173 |
{
|
|
174 |
test.Printf(_L("RepeatTimer expired %d\n"),iCount);
|
|
175 |
if (--iCount)
|
|
176 |
Start();
|
|
177 |
else
|
|
178 |
CActiveScheduler::Stop();
|
|
179 |
}
|
|
180 |
|
|
181 |
void InitialiseL()
|
|
182 |
{
|
|
183 |
ActiveSched=new (ELeave) CActiveScheduler;
|
|
184 |
Timer1=CTimer1::NewL();
|
|
185 |
Timer2=CTimer2::NewL();
|
|
186 |
Timer3=CTimer3::NewL();
|
|
187 |
RepTimer=CRepeatedTimer::NewL(5,10);
|
|
188 |
CActiveScheduler::Install(ActiveSched);
|
|
189 |
CActiveScheduler::Add(Timer1);
|
|
190 |
CActiveScheduler::Add(Timer2);
|
|
191 |
CActiveScheduler::Add(Timer3);
|
|
192 |
CActiveScheduler::Add(RepTimer);
|
|
193 |
Timer1->Start();
|
|
194 |
Timer2->Start();
|
|
195 |
Timer3->Start();
|
|
196 |
RepTimer->Start();
|
|
197 |
}
|
|
198 |
|
|
199 |
void TestNonPositiveTimeout()
|
|
200 |
{
|
|
201 |
TRequestStatus x1,x2;
|
|
202 |
RTimer rt1,rt2;
|
|
203 |
|
|
204 |
test.Next(_L("Test RTimer::Inactivity() with zero timeout"));
|
121
|
205 |
rt1.CreateLocal();
|
|
206 |
rt2.CreateLocal();
|
0
|
207 |
|
|
208 |
rt1.Inactivity(x1, 2);
|
|
209 |
User::After(500000);
|
|
210 |
rt2.Inactivity(x2, 0);
|
|
211 |
User::ResetInactivityTime();
|
|
212 |
|
|
213 |
User::WaitForRequest(x1);
|
|
214 |
test(x1==KErrNone);
|
|
215 |
User::WaitForRequest(x2);
|
|
216 |
test(x2==KErrNone);
|
|
217 |
|
|
218 |
test.Next(_L("Test RTimer::Inactivity() with negative timeout"));
|
|
219 |
rt1.Inactivity(x1, -1);
|
|
220 |
User::WaitForRequest(x1);
|
|
221 |
test(x1==KErrArgument);
|
|
222 |
}
|
|
223 |
|
|
224 |
GLDEF_C TInt E32Main()
|
|
225 |
{
|
|
226 |
test.Title();
|
|
227 |
__UHEAP_MARK;
|
|
228 |
test.Start(_L("Test RTimer::Inactivity"));
|
|
229 |
|
|
230 |
TestNonPositiveTimeout();
|
|
231 |
|
|
232 |
RTimer t1;
|
|
233 |
RTimer t2;
|
|
234 |
TInt r=t1.CreateLocal();
|
|
235 |
test(r==KErrNone);
|
|
236 |
r=t2.CreateLocal();
|
|
237 |
test(r==KErrNone);
|
|
238 |
test.Printf(_L("\nPress a key...\n"));
|
|
239 |
test.Getch();
|
|
240 |
TRequestStatus s;
|
|
241 |
t1.Inactivity(s,5);
|
|
242 |
TTime before;
|
|
243 |
before.UniversalTime();
|
|
244 |
test.Printf(_L("Wait... "));
|
|
245 |
User::WaitForRequest(s);
|
|
246 |
TTime after;
|
|
247 |
after.UniversalTime();
|
|
248 |
TTimeIntervalMicroSeconds interval=after.MicroSecondsFrom(before);
|
|
249 |
TInt interval_int=I64INT(interval.Int64());
|
|
250 |
test.Printf(_L("Timer expired after %d us\n"),interval_int);
|
|
251 |
test.Printf(_L("Press a key...\n"));
|
|
252 |
test.Getch();
|
|
253 |
t1.Inactivity(s,5);
|
|
254 |
before.UniversalTime();
|
|
255 |
test.Printf(_L("Test changing time"));
|
|
256 |
r=User::SetUTCTime(before+TTimeIntervalDays(1));
|
|
257 |
test(r==KErrNone);
|
|
258 |
test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers
|
|
259 |
r=User::SetUTCTime(before-TTimeIntervalDays(1));
|
|
260 |
test(r==KErrNone);
|
|
261 |
test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers
|
|
262 |
r=User::SetUTCTime(before);
|
|
263 |
test(r==KErrNone);
|
|
264 |
test(s==KRequestPending); // make sure time change doesn't trigger inactivity timers
|
|
265 |
|
|
266 |
TTime secure;
|
|
267 |
if ((r = secure.UniversalTimeSecure()) == KErrNone)
|
|
268 |
r = User::SetUTCTimeSecure(secure-TTimeIntervalDays(1));
|
|
269 |
if (r != KErrNone)
|
|
270 |
{
|
|
271 |
RDebug::Printf("WARNING: Secure clock change test skipped because secure time could not be changed!");
|
|
272 |
}
|
|
273 |
else
|
|
274 |
{
|
|
275 |
User::SetUTCTimeSecure(secure+TTimeIntervalDays(1));
|
|
276 |
User::SetUTCTimeSecure(secure);
|
|
277 |
test(s == KRequestPending); // make sure secure time change doesn't trigger inactivity timers
|
|
278 |
}
|
|
279 |
|
|
280 |
User::WaitForRequest(s);
|
|
281 |
after.UniversalTime();
|
|
282 |
interval=after.MicroSecondsFrom(before);
|
|
283 |
interval_int=I64INT(interval.Int64());
|
|
284 |
test.Printf(_L("Timer expired after %d us\n"),interval_int);
|
|
285 |
test.Printf(_L("Press a key...\n"));
|
|
286 |
test.Getch();
|
|
287 |
|
|
288 |
TInt inactive=User::InactivityTime().Int();
|
|
289 |
TRequestStatus s1;
|
|
290 |
TRequestStatus s2;
|
|
291 |
t1.Inactivity(s1, 10);
|
|
292 |
t2.Inactivity(s2, 15);
|
|
293 |
FOREVER
|
|
294 |
{
|
|
295 |
TInt new_inact=User::InactivityTime().Int();
|
|
296 |
if (new_inact!=inactive)
|
|
297 |
{
|
|
298 |
inactive=new_inact;
|
|
299 |
test.Printf(_L("%d\n"),inactive);
|
|
300 |
}
|
|
301 |
if (s2!=KRequestPending)
|
|
302 |
{
|
|
303 |
User::WaitForRequest(s2);
|
|
304 |
test(s2.Int()==KErrNone);
|
|
305 |
test.Printf(_L("Timer 2 expired\n"));
|
|
306 |
break;
|
|
307 |
}
|
|
308 |
if (s1!=KRequestPending)
|
|
309 |
{
|
|
310 |
User::WaitForRequest(s1);
|
|
311 |
test(s1.Int()==KErrNone);
|
|
312 |
test.Printf(_L("Timer 1 expired\n"));
|
|
313 |
s1=KRequestPending;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
test.Next(_L("Test RTimer::Cancel()"));
|
|
318 |
test.Printf(_L("Press a key...\n"));
|
|
319 |
test.Getch();
|
|
320 |
t1.Inactivity(s1, 300);
|
|
321 |
t1.Cancel();
|
|
322 |
User::WaitForRequest(s1);
|
|
323 |
test(s1==KErrCancel);
|
|
324 |
|
|
325 |
test.Next(_L("Test CTimer::Inactivity()"));
|
|
326 |
test.Printf(_L("Press a key...\n"));
|
|
327 |
test.Getch();
|
|
328 |
|
|
329 |
CTrapCleanup* pC=CTrapCleanup::New();
|
|
330 |
test(pC!=NULL);
|
|
331 |
TRAP(r,InitialiseL());
|
|
332 |
test(r==KErrNone);
|
|
333 |
|
|
334 |
CActiveScheduler::Start();
|
|
335 |
|
|
336 |
Timer1->Cancel();
|
|
337 |
Timer2->Cancel();
|
|
338 |
Timer3->Cancel();
|
|
339 |
RepTimer->Cancel();
|
|
340 |
|
|
341 |
delete Timer1;
|
|
342 |
delete Timer2;
|
|
343 |
delete Timer3;
|
|
344 |
delete RepTimer;
|
|
345 |
delete ActiveSched;
|
|
346 |
delete pC;
|
|
347 |
|
|
348 |
test.Printf(_L("Press a key...\n"));
|
|
349 |
test.Getch();
|
|
350 |
|
|
351 |
test.End();
|
|
352 |
__UHEAP_MARKEND;
|
|
353 |
return KErrNone;
|
|
354 |
}
|