applayerprotocols/httptransportfw/Test/T_HttpOnline/T_CancelMultiTrans.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-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 // defines test class: CTextModeTestCancelMultiTrans
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "T_CancelMultiTrans.h"
       
    19 
       
    20 
       
    21 // Constants used in test
       
    22 const TInt KCancelTestEventWhenCancelled = -7398;
       
    23 
       
    24 
       
    25 // CTextModeTestCancelMultiTrans-----------------------------------
       
    26 
       
    27 CTextModeTestCancelMultiTrans* CTextModeTestCancelMultiTrans::NewL(const TDesC& aHostName, TInt aNumTrans, TInt aTestNum, TInt aDelay, TInt aIdxToCancel)
       
    28 	{
       
    29 	CTextModeTestCancelMultiTrans* me = new(ELeave)CTextModeTestCancelMultiTrans(aNumTrans, aTestNum, aDelay, aIdxToCancel);
       
    30 	CleanupStack::PushL(me);
       
    31 	me->ConstructL(aHostName);
       
    32 	CleanupStack::Pop();
       
    33 	return me;
       
    34 	}
       
    35 
       
    36 CTextModeTestCancelMultiTrans::~CTextModeTestCancelMultiTrans()
       
    37 	{
       
    38 	delete iCancelTimer;
       
    39 	iTransStatus.Close();
       
    40 	iTransIds.Close();
       
    41 	}
       
    42 
       
    43 const TDesC& CTextModeTestCancelMultiTrans::TestName()
       
    44 	{
       
    45 	_LIT(KHeaderTestNameFmt,"CTextModeTestCancelMultiTrans:%d");
       
    46 	iTestName.Format(KHeaderTestNameFmt(), iTestNum);
       
    47 	return iTestName;
       
    48 	}
       
    49 
       
    50 
       
    51 TInt CTextModeTestCancelMultiTrans::MHFRunError(TInt aError, 
       
    52 										RHTTPTransaction aTransaction,
       
    53 										const THTTPEvent& /*aEvent*/)
       
    54 	{
       
    55 	iEngine->Utils().LogIt(_L("TestCancelMultiTrans: failed with error code %d on transaction ID=%d\n"), aError, aTransaction.Id());
       
    56 	// Use the response status to convey the error code
       
    57 	aTransaction.Response().SetStatusCode(aError);
       
    58 	return KErrNone;
       
    59 	}
       
    60 
       
    61 void CTextModeTestCancelMultiTrans::MHFRunL(RHTTPTransaction aTransaction,
       
    62 						 const THTTPEvent& aEvent)
       
    63 	{
       
    64 	// look up the transaction ID in our list of launched transactions
       
    65 	TInt transIdx = KErrNotFound;
       
    66 	TInt ii = 0;
       
    67 	while ((ii < iMaxNumTransactions) && (transIdx == KErrNotFound))
       
    68 		{
       
    69 		if (iTransIds[ii] == aTransaction.Id())
       
    70 			transIdx = ii;
       
    71 		else
       
    72 			ii++;
       
    73 		}
       
    74 
       
    75 	// Leave with an error if the event is for the transaction which has been cancelled
       
    76 	if (iCancelTimer->HasFired() && (aTransaction.Id() == iTransIds[iIdxToCancel]))
       
    77 		{
       
    78 		iTransStatus[ii] = KCancelTestEventWhenCancelled;
       
    79 		User::Leave(KCancelTestEventWhenCancelled);
       
    80 		}
       
    81 
       
    82 	switch (aEvent.iStatus)
       
    83 		{
       
    84 	case THTTPEvent::EGotResponseHeaders:
       
    85 		{
       
    86 		// HTTP response headers have been received
       
    87 		iEngine->Utils().LogIt(_L("<Got response headers>"));
       
    88 		RHTTPResponse resp = aTransaction.Response();
       
    89 		iTransStatus[ii] = resp.StatusCode();
       
    90 		} break;
       
    91 	case THTTPEvent::ESubmit:
       
    92 		{
       
    93 		} break;
       
    94 	case THTTPEvent::EGotResponseBodyData:
       
    95 		{
       
    96 		// Some (more) body data has been received (in the HTTP response)
       
    97 		iEngine->Utils().LogIt(_L("<Got a chunk of data>"));
       
    98 		ValidateBodyL(aTransaction);
       
    99 		} break;
       
   100 	case THTTPEvent::EResponseComplete:
       
   101 		{
       
   102 		// The transaction's response is complete
       
   103 		iEngine->Utils().LogIt(_L("<Transaction Complete>"));
       
   104 		} break;
       
   105 	case THTTPEvent::ESucceeded:
       
   106 		{
       
   107 		iEngine->Utils().LogIt(_L("<Transaction validated successfully>"));
       
   108 		if (--iOpenTransactionCount == 0)
       
   109 			CActiveScheduler::Stop();
       
   110 		} break;
       
   111 	case THTTPEvent::EFailed:
       
   112 		{
       
   113 		iEngine->Utils().LogIt(_L("<Transaction NOT validated successfully>"));
       
   114 		if (--iOpenTransactionCount == 0)
       
   115 			CActiveScheduler::Stop();
       
   116 		iFailureError = KErrTestFailed;
       
   117 		}
       
   118 		break;
       
   119 	case THTTPEvent::EUnrecoverableError:
       
   120 		{
       
   121 		iEngine->Utils().LogIt(_L("<Test failed unrecoverably>"));
       
   122 		iFailureError = KErrTestFailed;
       
   123 		} 
       
   124 		break;
       
   125 	default:
       
   126 		{
       
   127 		iEngine->Utils().LogIt(_L("<unrecognised event> %d"),aEvent.iStatus);
       
   128 		if (aEvent.iStatus < 0)
       
   129 			{
       
   130 			iFailureError = aEvent.iStatus;
       
   131 			CActiveScheduler::Stop();
       
   132 			}
       
   133 		} 
       
   134 		break;
       
   135 		}
       
   136 	}
       
   137 
       
   138 void CTextModeTestCancelMultiTrans::DoRunL()
       
   139 	{
       
   140 	iSession.OpenL();
       
   141 	CleanupClosePushL(iSession);
       
   142 
       
   143 	// Open the HTTP session
       
   144 	RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   145 	RStringPool strP = iSession.StringPool();
       
   146 	connInfo.SetPropertyL(strP.StringF(HTTP::EMaxNumTransportHandlers,RHTTPSession::GetTable()), THTTPHdrVal(1));
       
   147 
       
   148 	// Create the array of transactions
       
   149 	CreateTransArrayL();
       
   150 
       
   151 	// Populate some other arrays used for event checking in MHFRunL
       
   152 	TInt ii;
       
   153 	for (ii = 0; ii < iMaxNumTransactions; ii++)
       
   154 		{
       
   155 		iTransIds.Append(iTransArray[ii].Id());
       
   156 		iTransStatus.Append(0);
       
   157 		}
       
   158 
       
   159 	// Create the delayed notifier
       
   160 	iCancelTimer = CDelayedRequestCanceller::NewL(iTransArray[iIdxToCancel], *this);
       
   161 	CActiveScheduler::Add(iCancelTimer);
       
   162 
       
   163 	// Set timer to fire when we want to cancel the transaction (50ms interval added per test)
       
   164 	iCancelTimer->After(iDelay);
       
   165 
       
   166 	// Go!
       
   167 	iFailureError = 0;
       
   168 	CActiveScheduler::Start();
       
   169 
       
   170 	// Close anything opened, and fail straight away on any failure error caught in MHFRunL
       
   171 	CleanupStack::PopAndDestroy(&iSession);
       
   172 	User::LeaveIfError(iFailureError);
       
   173 
       
   174 	// Anything other than 0 (no response received yet) or HTTP/200 status in any of the
       
   175 	// transactions is a failure for this test
       
   176 	// Difficult to be precise about which tests should run to completion (i.e. 200) and which won't, so
       
   177 	// set the test engine not to check status codes
       
   178 	iExpectedStatusCode = 0;
       
   179 	TInt status = 0, st = 0, id = 0;
       
   180 	for (ii = 0; ii < iTransStatus.Count(); ii++)
       
   181 		{
       
   182 		st = iTransStatus[ii];
       
   183 		if (st < 0)
       
   184 			{
       
   185 			status = st;
       
   186 			id = iTransIds[ii];
       
   187 			}
       
   188 		else if ((st != 0) && (st != 200))
       
   189 			{
       
   190 			status = KErrGeneral;
       
   191 			id = iTransIds[ii];
       
   192 			}
       
   193 		}
       
   194 
       
   195 	// Log the failure we've picked on to report
       
   196 	if (status < 0)
       
   197 		iEngine->Utils().LogIt(_L("TestCancelMultiTrans : transaction ID=%d failed with code %d"), id, status);
       
   198 	User::LeaveIfError(status);
       
   199 	}
       
   200 
       
   201 TInt CTextModeTestCancelMultiTrans::RunError(TInt aErr)
       
   202 	{
       
   203 	iEngine->Utils().LogIt(_L("\nTest failed with error code %d\n"), aErr);
       
   204 	return KErrNone;
       
   205 	}
       
   206 	
       
   207 void CTextModeTestCancelMultiTrans::DoCancel()
       
   208 	{
       
   209 	iCancelTimer->Cancel();
       
   210 	}
       
   211 
       
   212 CTextModeTestCancelMultiTrans::CTextModeTestCancelMultiTrans(TInt aNumTrans, TInt aTestNum, TInt aDelay, TInt aIdxToCancel)
       
   213 	:  CTextModeTestMultiTrans(aNumTrans), iTestNum(aTestNum), iDelay(aDelay), iIdxToCancel(aIdxToCancel)
       
   214 	{
       
   215 	}
       
   216 
       
   217 void CTextModeTestCancelMultiTrans::CancelFired(RHTTPTransaction aTrans)
       
   218 	{
       
   219 	aTrans.Cancel();
       
   220 	if (--iOpenTransactionCount == 0)
       
   221 		CActiveScheduler::Stop();
       
   222 	}
       
   223 
       
   224 void CTextModeTestCancelMultiTrans::ConstructL(const TDesC& aHostName)
       
   225 	{
       
   226 	// call Base ConstructL
       
   227 	CTextModeTestMultiTrans::ConstructL(aHostName);
       
   228 	}
       
   229 
       
   230 CTextModeTestCancelMultiTrans::CDelayedRequestCanceller*
       
   231 CTextModeTestCancelMultiTrans::CDelayedRequestCanceller::NewL(RHTTPTransaction aTrans, CTextModeTestCancelMultiTrans& aOwningTest)
       
   232 	{
       
   233 	CDelayedRequestCanceller* me = new(ELeave)CDelayedRequestCanceller(aTrans, aOwningTest);
       
   234 	CleanupStack::PushL(me);
       
   235 	me->ConstructL();
       
   236 	CleanupStack::Pop();
       
   237 	return me;
       
   238 	}
       
   239 
       
   240 CTextModeTestCancelMultiTrans::CDelayedRequestCanceller::CDelayedRequestCanceller(RHTTPTransaction aTrans, CTextModeTestCancelMultiTrans& aOwningTest)
       
   241 	: 	CTimer(EPriorityStandard), iTrans(aTrans), iOwningTest(&aOwningTest)
       
   242 	{
       
   243 	}
       
   244 
       
   245 void CTextModeTestCancelMultiTrans::CDelayedRequestCanceller::RunL()
       
   246 	{
       
   247 	iHasFired = ETrue;
       
   248 	iOwningTest->CancelFired(iTrans);
       
   249 	}