kerneltest/e32test/system/t_inact.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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 	TInt ret;
       
   202 	TRequestStatus x1,x2;
       
   203 	RTimer rt1,rt2;
       
   204 
       
   205 	test.Next(_L("Test RTimer::Inactivity() with zero timeout"));
       
   206 	ret=rt1.CreateLocal();
       
   207 	ret=rt2.CreateLocal();
       
   208 
       
   209 	rt1.Inactivity(x1, 2);
       
   210 	User::After(500000);
       
   211 	rt2.Inactivity(x2, 0);
       
   212 	User::ResetInactivityTime();
       
   213 
       
   214 	User::WaitForRequest(x1);
       
   215 	test(x1==KErrNone);
       
   216 	User::WaitForRequest(x2);
       
   217 	test(x2==KErrNone);
       
   218 
       
   219 	test.Next(_L("Test RTimer::Inactivity() with negative timeout"));
       
   220 	rt1.Inactivity(x1, -1);
       
   221 	User::WaitForRequest(x1);
       
   222 	test(x1==KErrArgument);
       
   223 	}
       
   224 
       
   225 GLDEF_C TInt E32Main()
       
   226 	{
       
   227 	test.Title();
       
   228 	__UHEAP_MARK;
       
   229 	test.Start(_L("Test RTimer::Inactivity"));
       
   230 
       
   231 	TestNonPositiveTimeout();
       
   232 
       
   233 	RTimer t1;
       
   234 	RTimer t2;
       
   235 	TInt r=t1.CreateLocal();
       
   236 	test(r==KErrNone);
       
   237 	r=t2.CreateLocal();
       
   238 	test(r==KErrNone);
       
   239 	test.Printf(_L("\nPress a key...\n"));
       
   240 	test.Getch();
       
   241 	TRequestStatus s;
       
   242 	t1.Inactivity(s,5);
       
   243 	TTime before;
       
   244 	before.UniversalTime();
       
   245 	test.Printf(_L("Wait... "));
       
   246 	User::WaitForRequest(s);
       
   247 	TTime after;
       
   248 	after.UniversalTime();
       
   249 	TTimeIntervalMicroSeconds interval=after.MicroSecondsFrom(before);
       
   250 	TInt interval_int=I64INT(interval.Int64());
       
   251 	test.Printf(_L("Timer expired after %d us\n"),interval_int);
       
   252 	test.Printf(_L("Press a key...\n"));
       
   253 	test.Getch();
       
   254 	t1.Inactivity(s,5);
       
   255 	before.UniversalTime();
       
   256 	test.Printf(_L("Test changing time"));
       
   257 	r=User::SetUTCTime(before+TTimeIntervalDays(1));
       
   258 	test(r==KErrNone);
       
   259 	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
       
   260 	r=User::SetUTCTime(before-TTimeIntervalDays(1));
       
   261 	test(r==KErrNone);
       
   262 	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
       
   263 	r=User::SetUTCTime(before);
       
   264 	test(r==KErrNone);
       
   265 	test(s==KRequestPending);	// make sure time change doesn't trigger inactivity timers
       
   266 
       
   267 	TTime secure;
       
   268 	if ((r = secure.UniversalTimeSecure()) == KErrNone)
       
   269 		r = User::SetUTCTimeSecure(secure-TTimeIntervalDays(1));
       
   270 	if (r != KErrNone)
       
   271 		{
       
   272 		RDebug::Printf("WARNING: Secure clock change test skipped because secure time could not be changed!");
       
   273 		}
       
   274 	else
       
   275 		{
       
   276 		User::SetUTCTimeSecure(secure+TTimeIntervalDays(1));
       
   277 		User::SetUTCTimeSecure(secure);
       
   278 		test(s == KRequestPending);	// make sure secure time change doesn't trigger inactivity timers
       
   279 		}
       
   280 
       
   281 	User::WaitForRequest(s);
       
   282 	after.UniversalTime();
       
   283 	interval=after.MicroSecondsFrom(before);
       
   284 	interval_int=I64INT(interval.Int64());
       
   285 	test.Printf(_L("Timer expired after %d us\n"),interval_int);
       
   286 	test.Printf(_L("Press a key...\n"));
       
   287 	test.Getch();
       
   288 
       
   289 	TInt inactive=User::InactivityTime().Int();
       
   290 	TRequestStatus s1;
       
   291 	TRequestStatus s2;
       
   292 	t1.Inactivity(s1, 10);
       
   293 	t2.Inactivity(s2, 15);
       
   294 	FOREVER
       
   295 		{
       
   296 		TInt new_inact=User::InactivityTime().Int();
       
   297 		if (new_inact!=inactive)
       
   298 			{
       
   299 			inactive=new_inact;
       
   300 			test.Printf(_L("%d\n"),inactive);
       
   301 			}
       
   302 		if (s2!=KRequestPending)
       
   303 			{
       
   304 			User::WaitForRequest(s2);
       
   305 			test(s2.Int()==KErrNone);
       
   306 			test.Printf(_L("Timer 2 expired\n"));
       
   307 			break;
       
   308 			}
       
   309 		if (s1!=KRequestPending)
       
   310 			{
       
   311 			User::WaitForRequest(s1);
       
   312 			test(s1.Int()==KErrNone);
       
   313 			test.Printf(_L("Timer 1 expired\n"));
       
   314 			s1=KRequestPending;
       
   315 			}
       
   316 		}
       
   317 
       
   318 	test.Next(_L("Test RTimer::Cancel()"));
       
   319 	test.Printf(_L("Press a key...\n"));
       
   320 	test.Getch();
       
   321 	t1.Inactivity(s1, 300);
       
   322 	t1.Cancel();
       
   323 	User::WaitForRequest(s1);
       
   324 	test(s1==KErrCancel);
       
   325 
       
   326 	test.Next(_L("Test CTimer::Inactivity()"));
       
   327 	test.Printf(_L("Press a key...\n"));
       
   328 	test.Getch();
       
   329 
       
   330 	CTrapCleanup* pC=CTrapCleanup::New();
       
   331 	test(pC!=NULL);
       
   332 	TRAP(r,InitialiseL());
       
   333 	test(r==KErrNone);
       
   334 
       
   335 	CActiveScheduler::Start();
       
   336 
       
   337 	Timer1->Cancel();
       
   338 	Timer2->Cancel();
       
   339 	Timer3->Cancel();
       
   340 	RepTimer->Cancel();
       
   341 
       
   342 	delete Timer1;
       
   343 	delete Timer2;
       
   344 	delete Timer3;
       
   345 	delete RepTimer;
       
   346 	delete ActiveSched;
       
   347 	delete pC;
       
   348 
       
   349 	test.Printf(_L("Press a key...\n"));
       
   350 	test.Getch();
       
   351 
       
   352 	test.End();
       
   353 	__UHEAP_MARKEND;
       
   354 	return KErrNone;
       
   355 	}