sl@0
|
1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32test\active\t_idle.cpp
|
sl@0
|
15 |
// Overview:
|
sl@0
|
16 |
// Test the CIdle class. A CIdle and CTimer run on the same active scheduler and
|
sl@0
|
17 |
// the test displays how their callback function and RunL respectively are called.
|
sl@0
|
18 |
// API Information:
|
sl@0
|
19 |
// CIdle
|
sl@0
|
20 |
// Details:
|
sl@0
|
21 |
// - Create and install active scheduler.
|
sl@0
|
22 |
// - Create and start a timer, add this to the active scheduler, request an event
|
sl@0
|
23 |
// after a specified interval. The timer will reschedule itself for a finite
|
sl@0
|
24 |
// number of times, then it will cancel the CIdle object and then reschedule
|
sl@0
|
25 |
// itself again and eventually it will stop the active scheduler.
|
sl@0
|
26 |
// - Create a CIdle active object, start a background task that is encapsulated in
|
sl@0
|
27 |
// callback function. The CIdle will keep reschedule itself by always returning
|
sl@0
|
28 |
// TRUE from the callback.
|
sl@0
|
29 |
// - Output some text from timer's RunL and CIdle's callback to show when they are
|
sl@0
|
30 |
// called and how they interleave.
|
sl@0
|
31 |
// Platforms/Drives/Compatibility:
|
sl@0
|
32 |
// All.
|
sl@0
|
33 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
34 |
// Failures and causes:
|
sl@0
|
35 |
// Base Port information:
|
sl@0
|
36 |
//
|
sl@0
|
37 |
//
|
sl@0
|
38 |
|
sl@0
|
39 |
#include <e32test.h>
|
sl@0
|
40 |
|
sl@0
|
41 |
enum {EIdlePriority=1000,EMoreIdlePriority=800};
|
sl@0
|
42 |
|
sl@0
|
43 |
class CMyRequestManager : public CActiveScheduler
|
sl@0
|
44 |
{
|
sl@0
|
45 |
public:
|
sl@0
|
46 |
virtual void Error(TInt anError) const;
|
sl@0
|
47 |
};
|
sl@0
|
48 |
|
sl@0
|
49 |
class CMyTimer : public CTimer
|
sl@0
|
50 |
{
|
sl@0
|
51 |
public:
|
sl@0
|
52 |
static CMyTimer* New();
|
sl@0
|
53 |
void Start();
|
sl@0
|
54 |
virtual void RunL();
|
sl@0
|
55 |
protected:
|
sl@0
|
56 |
CMyTimer(TInt aPriority);
|
sl@0
|
57 |
private:
|
sl@0
|
58 |
enum {EMaxCount=10,EStopCount=5,ETimeReq=100000};
|
sl@0
|
59 |
TInt iCount;
|
sl@0
|
60 |
};
|
sl@0
|
61 |
|
sl@0
|
62 |
LOCAL_D RTest test(_L("T_IDLE"));
|
sl@0
|
63 |
LOCAL_D CIdle* MoreIdle;
|
sl@0
|
64 |
LOCAL_D TInt TheCount=0;
|
sl@0
|
65 |
|
sl@0
|
66 |
void CMyRequestManager::Error(TInt anError) const
|
sl@0
|
67 |
//
|
sl@0
|
68 |
// Called if any Run() method leaves.
|
sl@0
|
69 |
//
|
sl@0
|
70 |
{
|
sl@0
|
71 |
|
sl@0
|
72 |
test.Panic(anError,_L("CMyRequestManager::Error"));
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
CMyTimer* CMyTimer::New()
|
sl@0
|
76 |
//
|
sl@0
|
77 |
// Create a new CMyTimer.
|
sl@0
|
78 |
//
|
sl@0
|
79 |
{
|
sl@0
|
80 |
|
sl@0
|
81 |
return(new CMyTimer(EIdlePriority));
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
CMyTimer::CMyTimer(TInt aPriority)
|
sl@0
|
85 |
//
|
sl@0
|
86 |
// Constructor
|
sl@0
|
87 |
//
|
sl@0
|
88 |
: CTimer(aPriority),iCount(0)
|
sl@0
|
89 |
{}
|
sl@0
|
90 |
|
sl@0
|
91 |
void CMyTimer::Start()
|
sl@0
|
92 |
//
|
sl@0
|
93 |
// The timer has completed.
|
sl@0
|
94 |
//
|
sl@0
|
95 |
{
|
sl@0
|
96 |
|
sl@0
|
97 |
TRAPD(r, ConstructL());
|
sl@0
|
98 |
test(r==KErrNone);
|
sl@0
|
99 |
CActiveScheduler::Add(this);
|
sl@0
|
100 |
After(ETimeReq);
|
sl@0
|
101 |
}
|
sl@0
|
102 |
|
sl@0
|
103 |
void CMyTimer::RunL()
|
sl@0
|
104 |
//
|
sl@0
|
105 |
// The timer has completed.
|
sl@0
|
106 |
//
|
sl@0
|
107 |
{
|
sl@0
|
108 |
|
sl@0
|
109 |
iCount++;
|
sl@0
|
110 |
test.Printf(_L("Timer %03d\n"),iCount);
|
sl@0
|
111 |
if (iCount==EStopCount)
|
sl@0
|
112 |
MoreIdle->Cancel();
|
sl@0
|
113 |
if (iCount<EMaxCount)
|
sl@0
|
114 |
After(ETimeReq);
|
sl@0
|
115 |
else
|
sl@0
|
116 |
{
|
sl@0
|
117 |
test.Printf(_L("\n"));
|
sl@0
|
118 |
DoCancel();
|
sl@0
|
119 |
CActiveScheduler::Stop();
|
sl@0
|
120 |
}
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
TInt IdleResponse(TAny*)
|
sl@0
|
124 |
//
|
sl@0
|
125 |
// Respond to the idle callback
|
sl@0
|
126 |
//
|
sl@0
|
127 |
{
|
sl@0
|
128 |
|
sl@0
|
129 |
test.Printf(_L("\rIdle response number : %6d "),++TheCount);
|
sl@0
|
130 |
return(TRUE);
|
sl@0
|
131 |
}
|
sl@0
|
132 |
|
sl@0
|
133 |
TInt IdleCancel(TAny*)
|
sl@0
|
134 |
//
|
sl@0
|
135 |
// Cancel the other idle object
|
sl@0
|
136 |
//
|
sl@0
|
137 |
{
|
sl@0
|
138 |
|
sl@0
|
139 |
if (TheCount>9)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
MoreIdle->Cancel();
|
sl@0
|
142 |
return(FALSE);
|
sl@0
|
143 |
}
|
sl@0
|
144 |
return(TRUE);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
GLDEF_C TInt E32Main()
|
sl@0
|
148 |
//
|
sl@0
|
149 |
// Test idle objects.
|
sl@0
|
150 |
//
|
sl@0
|
151 |
{
|
sl@0
|
152 |
|
sl@0
|
153 |
test.Title();
|
sl@0
|
154 |
test.Start(_L("Testing idle object cancellation"));
|
sl@0
|
155 |
//
|
sl@0
|
156 |
CMyRequestManager* pR=new CMyRequestManager;
|
sl@0
|
157 |
test(pR!=NULL);
|
sl@0
|
158 |
CActiveScheduler::Install(pR);
|
sl@0
|
159 |
//
|
sl@0
|
160 |
CMyTimer* pT=CMyTimer::New();
|
sl@0
|
161 |
pT->Start();
|
sl@0
|
162 |
MoreIdle=CIdle::New(EIdlePriority);
|
sl@0
|
163 |
MoreIdle->Start(&IdleResponse);
|
sl@0
|
164 |
CActiveScheduler::Start();
|
sl@0
|
165 |
//
|
sl@0
|
166 |
test.End();
|
sl@0
|
167 |
return(0);
|
sl@0
|
168 |
}
|
sl@0
|
169 |
|