kerneltest/e32test/active/t_dtim.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1996-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\active\t_dtim.cpp
       
    15 // Overview:
       
    16 // Test delta timers. 
       
    17 // API Information:
       
    18 // CDeltaTimer.
       
    19 // Details:
       
    20 // - Create a delta timer and queue a number of timed events with different time intervals.
       
    21 // The callback functions of the timed events perform various actions: 
       
    22 // - Requeuing itself
       
    23 // - Cancelling the previous or next timer
       
    24 // - Doing nothing
       
    25 // - Stopping the active scheduler
       
    26 // - Verifies the timed events are run correctly and orderly
       
    27 // - The callback for each timed event increments a counter each time they are run
       
    28 // - The counters are checked to check how many times each callback has been called
       
    29 // Platforms/Drives/Compatibility:
       
    30 // All.
       
    31 // Assumptions/Requirement/Pre-requisites:
       
    32 // Failures and causes:
       
    33 // Base Port information:
       
    34 // 
       
    35 //
       
    36 
       
    37 #include <e32std.h>
       
    38 #include <e32std_private.h>
       
    39 #include <e32base.h>
       
    40 #include <e32base_private.h>
       
    41 #include <e32test.h>
       
    42 #include <e32hal.h>
       
    43 #include <e32def.h>
       
    44 #include <e32def_private.h>
       
    45 
       
    46 RTest test(_L("T_DTIM"));
       
    47 
       
    48 
       
    49 TInt theResults[10];
       
    50 TCallBack* theCallBacks;
       
    51 TDeltaTimerEntry TheTimers[10];
       
    52 CDeltaTimer* theTimer;
       
    53 
       
    54 class CMyActiveScheduler : public CActiveScheduler
       
    55 	{
       
    56 public:
       
    57 	void Error(TInt anError)const;
       
    58 	static CMyActiveScheduler* NewL();
       
    59 	};
       
    60 
       
    61 void CMyActiveScheduler::Error(TInt /*anError*/) const
       
    62 //
       
    63 //
       
    64 //
       
    65 	{
       
    66 	User::Panic(_L("MYActiveScheduler"),0);
       
    67 	}
       
    68 
       
    69 CMyActiveScheduler* CMyActiveScheduler::NewL()
       
    70 	{
       
    71 	return new(ELeave) CMyActiveScheduler;
       
    72 	}
       
    73 
       
    74 struct TDeltaTimerCallData
       
    75 	{
       
    76 	CDeltaTimer* iTimer;
       
    77 	TDeltaTimerEntry *iTimers;
       
    78 	TInt iIndex;
       
    79 	};
       
    80 
       
    81 TInt print(TAny* anArg)
       
    82 //
       
    83 // Null entry. Just prints and increments it's counter.
       
    84 //
       
    85 	{
       
    86 	TInt index=(TInt)anArg;
       
    87 	theResults[index]++;
       
    88 	test.Printf(_L("Callback %d\r\n"),index);
       
    89 	return 0;
       
    90 	}
       
    91 
       
    92 TInt cancelNext(TAny * anArg)
       
    93 //
       
    94 // Cancels the next entry - assumes it is valid.
       
    95 //
       
    96 	{
       
    97 	TInt index=(TInt)anArg;
       
    98 	theTimer->Remove(TheTimers[index+1]);
       
    99 	return print(anArg);
       
   100 	}
       
   101 
       
   102 TInt requeue(TAny * anArg)
       
   103 //
       
   104 // Note that a requeue will run once every 200 ms until stop is called.
       
   105 //
       
   106 	{
       
   107 	TTimeIntervalMicroSeconds32 tickPeriod;
       
   108 	UserHal::TickPeriod(tickPeriod);
       
   109 	const TInt K13Ticks = 13 * tickPeriod.Int();
       
   110 
       
   111 	TInt index=(TInt)anArg;
       
   112 	theTimer->Queue(K13Ticks*2/3,TheTimers[index]);
       
   113 	return print(anArg);
       
   114 	}
       
   115 
       
   116 TInt requeuePrevious(TAny * anArg)
       
   117 //
       
   118 // Requeue the previous entry (assumes previous entry is valid handle)
       
   119 //
       
   120 	{
       
   121 	TTimeIntervalMicroSeconds32 tickPeriod;
       
   122 	UserHal::TickPeriod(tickPeriod);
       
   123 	const TInt K13Ticks = 13 * tickPeriod.Int();
       
   124 
       
   125 	TInt index=(TInt)anArg - 1;
       
   126 	theTimer->Queue(K13Ticks,TheTimers[index]);
       
   127 	return print(anArg);
       
   128 	}
       
   129 
       
   130 TInt cancel2ndFollowing(TAny * anArg)
       
   131 //
       
   132 // Assumes that 2nd following timer handle is valid.
       
   133 //
       
   134 	{
       
   135 	TInt index=(TInt)anArg;
       
   136 	theTimer->Remove(TheTimers[index+2]);
       
   137 	return print(anArg);
       
   138 	}
       
   139 
       
   140 TInt stop(TAny * anArg)
       
   141 //
       
   142 // Stops the active schduler
       
   143 //
       
   144 	{
       
   145 	TInt index=(TInt)anArg;
       
   146 	theResults[index]++;
       
   147 	test.Printf(_L("Callback %d, stopping\r\n"),index);
       
   148 	CMyActiveScheduler::Stop();
       
   149 
       
   150 	return 0;
       
   151 	}
       
   152 
       
   153 TInt E32Main()
       
   154 //
       
   155 //
       
   156 //
       
   157 	{
       
   158 
       
   159 	CMyActiveScheduler* s=NULL;
       
   160 	TRAPD(ret,s=CMyActiveScheduler::NewL())
       
   161 	test(ret==KErrNone);
       
   162 
       
   163 	CActiveScheduler::Install(s);
       
   164 	test.Title();
       
   165 	test.Start(_L("Timer"));
       
   166 	
       
   167 	__KHEAP_MARK;
       
   168 
       
   169 	TTimeIntervalMicroSeconds32 tickPeriod;
       
   170 	UserHal::TickPeriod(tickPeriod);
       
   171 
       
   172 	TRAP(ret,theTimer=CDeltaTimer::NewL(100, tickPeriod.Int()));
       
   173 	test(ret==KErrNone);
       
   174 
       
   175 	Mem::FillZ(theResults,10*sizeof(TInt));
       
   176 
       
   177 	TCallBack callBacks[10]=
       
   178 		{
       
   179 		/* 0 */ TCallBack(print,(TAny*)0),
       
   180 		/* 1 */ TCallBack(cancelNext,(TAny*)1),
       
   181 		/* 2 */ TCallBack(print,(TAny*)2),	// Gets cancelled
       
   182 		/* 3 */ TCallBack(print,(TAny*)3),		// Runs twice
       
   183 		/* 4 */ TCallBack(requeuePrevious,(TAny*)4),
       
   184 		/* 5 */ TCallBack(cancel2ndFollowing,(TAny*)5),
       
   185 		/* 6 */ TCallBack(print,(TAny*)6),
       
   186 		/* 7 */ TCallBack(cancelNext,(TAny*)7),	// Gets cancelled
       
   187 		/* 8 */ TCallBack(requeue,(TAny*)8),	// Runs twice, once on the same RunL as the stop
       
   188 		/* 9 */ TCallBack(stop,(TAny*)9),
       
   189 		};
       
   190 
       
   191 	theCallBacks=callBacks;
       
   192 	for (TInt i=0;i<10;i++)
       
   193 		TheTimers[i].Set(theCallBacks[i]);
       
   194 
       
   195 	const TInt K13Ticks = 13 * tickPeriod.Int();
       
   196 
       
   197 	theTimer->Queue(K13Ticks,TheTimers[0]);
       
   198 	theTimer->Queue(2*K13Ticks,TheTimers[1]);
       
   199 	theTimer->Queue(3*K13Ticks,TheTimers[2]);
       
   200 	theTimer->Queue(4*K13Ticks,TheTimers[3]);
       
   201 	theTimer->Queue(5*K13Ticks,TheTimers[4]);
       
   202 	theTimer->Queue(6*K13Ticks,TheTimers[5]);
       
   203 	theTimer->Queue(7*K13Ticks,TheTimers[6]);
       
   204 	theTimer->Queue(8*K13Ticks,TheTimers[7]);
       
   205 	theTimer->Queue(9*K13Ticks,TheTimers[8]);
       
   206 	theTimer->Queue(10*K13Ticks,TheTimers[9]);
       
   207 
       
   208 	CActiveScheduler::Start();
       
   209 
       
   210 	test(theResults[0]==1);
       
   211 	test(theResults[1]==1);
       
   212 	test(theResults[2]==0);
       
   213 	test(theResults[3]==2);
       
   214 	test(theResults[4]==1);
       
   215 	test(theResults[5]==1);
       
   216 	test(theResults[6]==1);
       
   217 	test(theResults[7]==0);
       
   218 	test(theResults[8]==2);
       
   219 	test(theResults[9]==1);
       
   220 
       
   221 	delete theTimer;
       
   222 
       
   223 	__KHEAP_MARKEND;
       
   224 
       
   225 	test.End();
       
   226 	return 0;
       
   227 	}