lowlevellibsandfws/apputils/tsrc/T_SCHED.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 "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 // Started by DWW, April 1997
       
    15 // Tests active scheduler
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <basched.h>
       
    21 #include <baerror.h>
       
    22 #include <baerrhan.h>
       
    23 
       
    24 #define KUidTestValue 0x43
       
    25 const TUid KUidTest={KUidTestValue};
       
    26 
       
    27 LOCAL_D RTest test(_L("T_SCHED"));
       
    28 
       
    29 class CSimpleErrorHandler : public CBaErrorHandler
       
    30 	{
       
    31 private:
       
    32 	TErrorHandlerResponse HandleError(TDes& aErrorText,TDes& aContextText);
       
    33 	};
       
    34 
       
    35 class CErrorHandlerWithState : public CBaErrorHandler
       
    36 	{
       
    37 public:
       
    38 	void SetState(TInt aState) { iState=aState; }
       
    39 private:
       
    40 	TErrorHandlerResponse HandleError(TDes& aErrorText,TDes& aContextText);
       
    41 private:
       
    42 	TInt iState;
       
    43 	};
       
    44 
       
    45 TErrorHandlerResponse CSimpleErrorHandler::HandleError(TDes& aErrorText,TDes& aContextText)
       
    46 	{
       
    47 	aErrorText=_L("ERROR");
       
    48 	aContextText=_L("CONTEXT");
       
    49 	return(EAlertDisplay);
       
    50 	}
       
    51 
       
    52 TErrorHandlerResponse CErrorHandlerWithState::HandleError(TDes& aErrorText,TDes& aContextText)
       
    53 	{
       
    54 	aErrorText.Num(iState);
       
    55 	aContextText.Zero();
       
    56 	return(iState? EInfoDisplay: ENoDisplay);
       
    57 	}
       
    58 
       
    59 class CTestScheduler : public CBaActiveScheduler
       
    60 	{
       
    61 private: // from CBaActiveScheduler
       
    62 	void DisplayError(TInt aError);
       
    63 private:
       
    64 	TInt iState;
       
    65 	};
       
    66 
       
    67 class CTestActive : public CActive
       
    68 	{
       
    69 public:
       
    70 	static CTestActive* NewL();
       
    71 private:
       
    72 	CTestActive();
       
    73 	~CTestActive();
       
    74 	void DoCancel();
       
    75 	void RunL();
       
    76 	void Queue();
       
    77 private:
       
    78 	TInt iState;
       
    79 	CSimpleErrorHandler iSimpleErrorHandler;
       
    80 	CErrorHandlerWithState iComplexErrorHandler;
       
    81 	};
       
    82 
       
    83 CTestActive::CTestActive()
       
    84 	: CActive(0)
       
    85 	{
       
    86 	}
       
    87 
       
    88 CTestActive::~CTestActive()
       
    89 	{
       
    90 	Cancel();
       
    91 	}
       
    92 
       
    93 CTestActive* CTestActive::NewL()
       
    94 	{ // static
       
    95 	CTestActive* self=new(ELeave) CTestActive;
       
    96 	CActiveScheduler::Add(self);
       
    97 	self->Queue();
       
    98 	return(self);
       
    99 	}
       
   100 
       
   101 void CTestActive::Queue()
       
   102 	{
       
   103 	TRequestStatus* pS=(&iStatus);
       
   104 	User::RequestComplete(pS,0);
       
   105 	SetActive();
       
   106 	}
       
   107 
       
   108 void CTestActive::DoCancel()
       
   109 	{
       
   110 	}
       
   111 
       
   112 void CTestActive::RunL()
       
   113 	{
       
   114 	Queue();
       
   115 	switch (iState++)
       
   116 		{
       
   117 	case 0:
       
   118 		test.Next(_L("Extended error"));
       
   119 		CBaActiveScheduler::DisplayExtendedError(KUidTest,66);
       
   120 		test.Next(_L("Leave for alert"));
       
   121 		CBaActiveScheduler::LeaveForAlert(KUidBaflDll,77);
       
   122 		CBaActiveScheduler::DisplayExtendedError(KUidTest,88); // won't reach here
       
   123 		break;
       
   124 	case 1:
       
   125 		test.Next(_L("Leave"));
       
   126 		User::Leave(-22);
       
   127 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,9); // won't reach here
       
   128 		break;
       
   129 	case 2:
       
   130 		test.Next(_L("Leave no alert"));
       
   131 		CBaActiveScheduler::LeaveNoAlert();
       
   132 		CBaActiveScheduler::DisplayExtendedError(KUidTest,55); // won't reach here
       
   133 		break;
       
   134 	case 3:
       
   135 		test.Next(_L("Extended error again"));
       
   136 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,33);
       
   137 		test.Next(_L("Leave for info print"));
       
   138 		CBaActiveScheduler::LeaveForInfoPrint(KUidTest,11);
       
   139 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,88); // won't reach here
       
   140 		break;
       
   141 	case 4:
       
   142 		test.Next(_L("Simple error handler"));
       
   143 		CBaActiveScheduler::LeaveForErrorHandler(&iSimpleErrorHandler);
       
   144 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,87); // won't reach here
       
   145 		break;
       
   146 	case 5:
       
   147 		test.Next(_L("Complex error handler - 1"));
       
   148 		CBaActiveScheduler::LeaveForErrorHandler(&iComplexErrorHandler);
       
   149 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,86); // won't reach here
       
   150 		break;
       
   151 	case 6:
       
   152 		test.Next(_L("Complex error handler - 2"));
       
   153 		iComplexErrorHandler.SetState(6);
       
   154 		CBaActiveScheduler::LeaveForErrorHandler(&iComplexErrorHandler);
       
   155 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,85); // won't reach here
       
   156 		break;
       
   157 	case 7:
       
   158 		test.Next(_L("Exit"));
       
   159 		CBaActiveScheduler::Exit();
       
   160 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,99); // won't reach here
       
   161 		break;
       
   162 	case 8:
       
   163 		CBaActiveScheduler::DisplayExtendedError(KUidBaflDll,999); // won't reach here
       
   164 		break;
       
   165 		}
       
   166 	}
       
   167 
       
   168 void CTestScheduler::DisplayError(TInt aError)
       
   169 	{
       
   170 	TBuf<80> errorText;
       
   171 	TBuf<80> contextText;
       
   172 	TErrorHandlerResponse resp;
       
   173 	switch (iState++)
       
   174 		{
       
   175 	case 0:
       
   176 		test(aError==KErrExtended);
       
   177 		test(iExtendedError.iComponent==KUidTest);
       
   178 		test(iExtendedError.iErrorNumber==66);
       
   179 		test(iExtendedError.iInformation==EFalse);
       
   180 		break;
       
   181 	case 1:
       
   182 		test(aError==KErrExtended);
       
   183 		test(iExtendedError.iComponent==KUidBaflDll);
       
   184 		test(iExtendedError.iErrorNumber==77);
       
   185 		test(iExtendedError.iInformation==EFalse);
       
   186 		break;
       
   187 	case 2:
       
   188 		test(aError==-22);
       
   189 		break;
       
   190 	case 3:
       
   191 		test(aError==KErrExtended);
       
   192 		test(iExtendedError.iComponent==KUidBaflDll);
       
   193 		test(iExtendedError.iErrorNumber==33);
       
   194 		test(iExtendedError.iInformation==EFalse);
       
   195 		break;
       
   196 	case 4:
       
   197 		test(aError==KErrExtended);
       
   198 		test(iExtendedError.iComponent==KUidTest);
       
   199 		test(iExtendedError.iErrorNumber==11);
       
   200 		test(iExtendedError.iInformation);
       
   201 		break;
       
   202 	case 5:
       
   203 		test(aError==KErrExtended);
       
   204 		test(iExtendedError.iComponent==KUidBaflErrorHandler);
       
   205 		resp=CBaErrorHandler::CallBack(iExtendedError.iErrorNumber,errorText,contextText);
       
   206 		test(resp==EAlertDisplay);
       
   207 		test(errorText==_L("ERROR"));
       
   208 		test(contextText==_L("CONTEXT"));
       
   209 		break;
       
   210 	case 6:
       
   211 		test(aError==KErrExtended);
       
   212 		test(iExtendedError.iComponent==KUidBaflErrorHandler);
       
   213 		resp=CBaErrorHandler::CallBack(iExtendedError.iErrorNumber,errorText,contextText);
       
   214 		test(resp==ENoDisplay);
       
   215 		test(errorText==_L("0"));
       
   216 		test(!contextText.Length());
       
   217 		break;
       
   218 	case 7:
       
   219 		test(aError==KErrExtended);
       
   220 		test(iExtendedError.iComponent==KUidBaflErrorHandler);
       
   221 		resp=CBaErrorHandler::CallBack(iExtendedError.iErrorNumber,errorText,contextText);
       
   222 		test(resp==EInfoDisplay);
       
   223 		test(errorText==_L("6"));
       
   224 		test(!contextText.Length());
       
   225 		break;
       
   226 	case 8:
       
   227 		test(EFalse); // should never reach here
       
   228 		}
       
   229 	}
       
   230 
       
   231 /**
       
   232 @SYMTestCaseID          SYSLIB-BAFL-CT-0442
       
   233 @SYMTestCaseDesc        Tests for the Active Scheduler
       
   234 @SYMTestPriority        High
       
   235 @SYMTestActions         Tests for the functionality of ActiveScheduler
       
   236 @SYMTestExpectedResults Test must not fail
       
   237 @SYMREQ                 REQ0000
       
   238 */
       
   239 void DoTests()
       
   240     {
       
   241 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0442 "));
       
   242 	CActiveScheduler* as=new(ELeave) CTestScheduler;
       
   243 	CActiveScheduler::Install(as);
       
   244 	CActive* ac=CTestActive::NewL();
       
   245 	TRAPD(ret,as->Start());
       
   246 	test(ret==KLeaveExit);
       
   247 	delete(as);
       
   248 	delete(ac);
       
   249     }
       
   250 
       
   251 GLDEF_C TInt E32Main()
       
   252 	{
       
   253     __UHEAP_MARK;
       
   254     CTrapCleanup *cleanup=CTrapCleanup::New();
       
   255 	test.Title();
       
   256 	test.Start(_L("Testing CBaActiveScheduler"));
       
   257     TRAPD(err,DoTests());
       
   258     test(err==KErrNone);
       
   259 	test.End();
       
   260     test.Close();
       
   261     delete cleanup;
       
   262     __UHEAP_MARKEND;
       
   263 	return(0);
       
   264     }