os/kernelhwsrv/kerneltest/e32test/system/t_inact.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/t_inact.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,355 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\system\t_inact.cpp
    1.18 +// Test inactivity timers
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <e32test.h>
    1.23 +
    1.24 +RTest test(_L("T_INACT"));
    1.25 +
    1.26 +class CTimer1 : public CTimer
    1.27 +	{
    1.28 +public:
    1.29 +	static CTimer1* NewL();
    1.30 +	CTimer1();
    1.31 +	void Start();
    1.32 +	virtual void RunL();
    1.33 +	};
    1.34 +
    1.35 +class CTimer2 : public CTimer
    1.36 +	{
    1.37 +public:
    1.38 +	static CTimer2* NewL();
    1.39 +	CTimer2();
    1.40 +	void Start();
    1.41 +	virtual void RunL();
    1.42 +	};
    1.43 +
    1.44 +class CTimer3 : public CTimer
    1.45 +	{
    1.46 +public:
    1.47 +	static CTimer3* NewL();
    1.48 +	CTimer3();
    1.49 +	void Start();
    1.50 +	virtual void RunL();
    1.51 +public:
    1.52 +	TInt iInactive;
    1.53 +	};
    1.54 +
    1.55 +class CRepeatedTimer : public CTimer
    1.56 +	{
    1.57 +public:
    1.58 +	static CRepeatedTimer* NewL(TInt aCount, TInt aPeriod);
    1.59 +	CRepeatedTimer();
    1.60 +	void Start();
    1.61 +	virtual void RunL();
    1.62 +public:
    1.63 +	TInt iCount;
    1.64 +	TInt iPeriod;
    1.65 +	};
    1.66 +
    1.67 +CActiveScheduler* ActiveSched;
    1.68 +CTimer1* Timer1;
    1.69 +CTimer2* Timer2;
    1.70 +CTimer3* Timer3;
    1.71 +CRepeatedTimer* RepTimer;
    1.72 +
    1.73 +CTimer1::CTimer1()
    1.74 +	: CTimer(EPriorityStandard)
    1.75 +	{
    1.76 +	}
    1.77 +
    1.78 +CTimer1* CTimer1::NewL()
    1.79 +	{
    1.80 +	CTimer1* pT=new (ELeave) CTimer1;
    1.81 +	CleanupStack::PushL(pT);
    1.82 +	pT->ConstructL();
    1.83 +	CleanupStack::Pop();
    1.84 +	return pT;
    1.85 +	}
    1.86 +
    1.87 +void CTimer1::Start()
    1.88 +	{
    1.89 +	Inactivity(7);
    1.90 +	}
    1.91 +
    1.92 +void CTimer1::RunL()
    1.93 +	{
    1.94 +	test.Printf(_L("CTimer1 expired\n"));
    1.95 +	Start();
    1.96 +	}
    1.97 +
    1.98 +CTimer2::CTimer2()
    1.99 +	: CTimer(EPriorityStandard)
   1.100 +	{
   1.101 +	}
   1.102 +
   1.103 +CTimer2* CTimer2::NewL()
   1.104 +	{
   1.105 +	CTimer2* pT=new (ELeave) CTimer2;
   1.106 +	CleanupStack::PushL(pT);
   1.107 +	pT->ConstructL();
   1.108 +	CleanupStack::Pop();
   1.109 +	return pT;
   1.110 +	}
   1.111 +
   1.112 +void CTimer2::Start()
   1.113 +	{
   1.114 +	Inactivity(13);
   1.115 +	}
   1.116 +
   1.117 +void CTimer2::RunL()
   1.118 +	{
   1.119 +	test.Printf(_L("CTimer2 expired\n"));
   1.120 +	Start();
   1.121 +	}
   1.122 +
   1.123 +CTimer3::CTimer3()
   1.124 +	: CTimer(EPriorityStandard)
   1.125 +	{
   1.126 +	}
   1.127 +
   1.128 +CTimer3* CTimer3::NewL()
   1.129 +	{
   1.130 +	CTimer3* pT=new (ELeave) CTimer3;
   1.131 +	CleanupStack::PushL(pT);
   1.132 +	pT->ConstructL();
   1.133 +	CleanupStack::Pop();
   1.134 +	return pT;
   1.135 +	}
   1.136 +
   1.137 +void CTimer3::Start()
   1.138 +	{
   1.139 +	iInactive=User::InactivityTime().Int();
   1.140 +	After(500000);
   1.141 +	}
   1.142 +
   1.143 +void CTimer3::RunL()
   1.144 +	{
   1.145 +	TInt inactive=User::InactivityTime().Int();
   1.146 +	if (inactive!=iInactive)
   1.147 +		{
   1.148 +		iInactive=inactive;
   1.149 +		test.Printf(_L("%d\n"),inactive);
   1.150 +		}
   1.151 +	After(500000);
   1.152 +	}
   1.153 +
   1.154 +CRepeatedTimer::CRepeatedTimer()
   1.155 +	: CTimer(EPriorityStandard)
   1.156 +	{
   1.157 +	}
   1.158 +
   1.159 +CRepeatedTimer* CRepeatedTimer::NewL(TInt aCount, TInt aPeriod)
   1.160 +	{
   1.161 +	CRepeatedTimer* pT=new (ELeave) CRepeatedTimer;
   1.162 +	pT->iCount=aCount;
   1.163 +	pT->iPeriod=aPeriod;
   1.164 +	CleanupStack::PushL(pT);
   1.165 +	pT->ConstructL();
   1.166 +	CleanupStack::Pop();
   1.167 +	return pT;
   1.168 +	}
   1.169 +
   1.170 +void CRepeatedTimer::Start()
   1.171 +	{
   1.172 +	Inactivity(iPeriod);
   1.173 +	}
   1.174 +
   1.175 +void CRepeatedTimer::RunL()
   1.176 +	{
   1.177 +	test.Printf(_L("RepeatTimer expired %d\n"),iCount);
   1.178 +	if (--iCount)
   1.179 +		Start();
   1.180 +	else
   1.181 +		CActiveScheduler::Stop();
   1.182 +	}
   1.183 +
   1.184 +void InitialiseL()
   1.185 +	{
   1.186 +	ActiveSched=new (ELeave) CActiveScheduler;
   1.187 +	Timer1=CTimer1::NewL();
   1.188 +	Timer2=CTimer2::NewL();
   1.189 +	Timer3=CTimer3::NewL();
   1.190 +	RepTimer=CRepeatedTimer::NewL(5,10);
   1.191 +	CActiveScheduler::Install(ActiveSched);
   1.192 +	CActiveScheduler::Add(Timer1);
   1.193 +	CActiveScheduler::Add(Timer2);
   1.194 +	CActiveScheduler::Add(Timer3);
   1.195 +	CActiveScheduler::Add(RepTimer);
   1.196 +	Timer1->Start();
   1.197 +	Timer2->Start();
   1.198 +	Timer3->Start();
   1.199 +	RepTimer->Start();
   1.200 +	}
   1.201 +
   1.202 +void TestNonPositiveTimeout()
   1.203 +	{
   1.204 +	TInt ret;
   1.205 +	TRequestStatus x1,x2;
   1.206 +	RTimer rt1,rt2;
   1.207 +
   1.208 +	test.Next(_L("Test RTimer::Inactivity() with zero timeout"));
   1.209 +	ret=rt1.CreateLocal();
   1.210 +	ret=rt2.CreateLocal();
   1.211 +
   1.212 +	rt1.Inactivity(x1, 2);
   1.213 +	User::After(500000);
   1.214 +	rt2.Inactivity(x2, 0);
   1.215 +	User::ResetInactivityTime();
   1.216 +
   1.217 +	User::WaitForRequest(x1);
   1.218 +	test(x1==KErrNone);
   1.219 +	User::WaitForRequest(x2);
   1.220 +	test(x2==KErrNone);
   1.221 +
   1.222 +	test.Next(_L("Test RTimer::Inactivity() with negative timeout"));
   1.223 +	rt1.Inactivity(x1, -1);
   1.224 +	User::WaitForRequest(x1);
   1.225 +	test(x1==KErrArgument);
   1.226 +	}
   1.227 +
   1.228 +GLDEF_C TInt E32Main()
   1.229 +	{
   1.230 +	test.Title();
   1.231 +	__UHEAP_MARK;
   1.232 +	test.Start(_L("Test RTimer::Inactivity"));
   1.233 +
   1.234 +	TestNonPositiveTimeout();
   1.235 +
   1.236 +	RTimer t1;
   1.237 +	RTimer t2;
   1.238 +	TInt r=t1.CreateLocal();
   1.239 +	test(r==KErrNone);
   1.240 +	r=t2.CreateLocal();
   1.241 +	test(r==KErrNone);
   1.242 +	test.Printf(_L("\nPress a key...\n"));
   1.243 +	test.Getch();
   1.244 +	TRequestStatus s;
   1.245 +	t1.Inactivity(s,5);
   1.246 +	TTime before;
   1.247 +	before.UniversalTime();
   1.248 +	test.Printf(_L("Wait... "));
   1.249 +	User::WaitForRequest(s);
   1.250 +	TTime after;
   1.251 +	after.UniversalTime();
   1.252 +	TTimeIntervalMicroSeconds interval=after.MicroSecondsFrom(before);
   1.253 +	TInt interval_int=I64INT(interval.Int64());
   1.254 +	test.Printf(_L("Timer expired after %d us\n"),interval_int);
   1.255 +	test.Printf(_L("Press a key...\n"));
   1.256 +	test.Getch();
   1.257 +	t1.Inactivity(s,5);
   1.258 +	before.UniversalTime();
   1.259 +	test.Printf(_L("Test changing time"));
   1.260 +	r=User::SetUTCTime(before+TTimeIntervalDays(1));
   1.261 +	test(r==KErrNone);
   1.262 +	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
   1.263 +	r=User::SetUTCTime(before-TTimeIntervalDays(1));
   1.264 +	test(r==KErrNone);
   1.265 +	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
   1.266 +	r=User::SetUTCTime(before);
   1.267 +	test(r==KErrNone);
   1.268 +	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
   1.269 +
   1.270 +	TTime secure;
   1.271 +	if ((r = secure.UniversalTimeSecure()) == KErrNone)
   1.272 +		r = User::SetUTCTimeSecure(secure-TTimeIntervalDays(1));
   1.273 +	if (r != KErrNone)
   1.274 +		{
   1.275 +		RDebug::Printf("WARNING: Secure clock change test skipped because secure time could not be changed!");
   1.276 +		}
   1.277 +	else
   1.278 +		{
   1.279 +		User::SetUTCTimeSecure(secure+TTimeIntervalDays(1));
   1.280 +		User::SetUTCTimeSecure(secure);
   1.281 +		test(s == KRequestPending);	// make sure secure time change doesn't trigger inactivity timers
   1.282 +		}
   1.283 +
   1.284 +	User::WaitForRequest(s);
   1.285 +	after.UniversalTime();
   1.286 +	interval=after.MicroSecondsFrom(before);
   1.287 +	interval_int=I64INT(interval.Int64());
   1.288 +	test.Printf(_L("Timer expired after %d us\n"),interval_int);
   1.289 +	test.Printf(_L("Press a key...\n"));
   1.290 +	test.Getch();
   1.291 +
   1.292 +	TInt inactive=User::InactivityTime().Int();
   1.293 +	TRequestStatus s1;
   1.294 +	TRequestStatus s2;
   1.295 +	t1.Inactivity(s1, 10);
   1.296 +	t2.Inactivity(s2, 15);
   1.297 +	FOREVER
   1.298 +		{
   1.299 +		TInt new_inact=User::InactivityTime().Int();
   1.300 +		if (new_inact!=inactive)
   1.301 +			{
   1.302 +			inactive=new_inact;
   1.303 +			test.Printf(_L("%d\n"),inactive);
   1.304 +			}
   1.305 +		if (s2!=KRequestPending)
   1.306 +			{
   1.307 +			User::WaitForRequest(s2);
   1.308 +			test(s2.Int()==KErrNone);
   1.309 +			test.Printf(_L("Timer 2 expired\n"));
   1.310 +			break;
   1.311 +			}
   1.312 +		if (s1!=KRequestPending)
   1.313 +			{
   1.314 +			User::WaitForRequest(s1);
   1.315 +			test(s1.Int()==KErrNone);
   1.316 +			test.Printf(_L("Timer 1 expired\n"));
   1.317 +			s1=KRequestPending;
   1.318 +			}
   1.319 +		}
   1.320 +
   1.321 +	test.Next(_L("Test RTimer::Cancel()"));
   1.322 +	test.Printf(_L("Press a key...\n"));
   1.323 +	test.Getch();
   1.324 +	t1.Inactivity(s1, 300);
   1.325 +	t1.Cancel();
   1.326 +	User::WaitForRequest(s1);
   1.327 +	test(s1==KErrCancel);
   1.328 +
   1.329 +	test.Next(_L("Test CTimer::Inactivity()"));
   1.330 +	test.Printf(_L("Press a key...\n"));
   1.331 +	test.Getch();
   1.332 +
   1.333 +	CTrapCleanup* pC=CTrapCleanup::New();
   1.334 +	test(pC!=NULL);
   1.335 +	TRAP(r,InitialiseL());
   1.336 +	test(r==KErrNone);
   1.337 +
   1.338 +	CActiveScheduler::Start();
   1.339 +
   1.340 +	Timer1->Cancel();
   1.341 +	Timer2->Cancel();
   1.342 +	Timer3->Cancel();
   1.343 +	RepTimer->Cancel();
   1.344 +
   1.345 +	delete Timer1;
   1.346 +	delete Timer2;
   1.347 +	delete Timer3;
   1.348 +	delete RepTimer;
   1.349 +	delete ActiveSched;
   1.350 +	delete pC;
   1.351 +
   1.352 +	test.Printf(_L("Press a key...\n"));
   1.353 +	test.Getch();
   1.354 +
   1.355 +	test.End();
   1.356 +	__UHEAP_MARKEND;
   1.357 +	return KErrNone;
   1.358 +	}