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