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