multimediacommsengine/mmcefloorctrlplugin/tsrc/ut_floorctrlplugin/src/UT_CFCReceiver.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 //  CLASS HEADER
       
    22 #include "ut_cfcreceiver.h"
       
    23 
       
    24 //  EXTERNAL INCLUDES
       
    25 #include <digia/eunit/eunitmacros.h>
       
    26 
       
    27 
       
    28 //  INTERNAL INCLUDES
       
    29 #include "fcreceiver.h"
       
    30 #include "fcsender.h"
       
    31 
       
    32 const TInt KTimerDelay = 100;
       
    33 const TUint KIAPID = 6;
       
    34 const TInt KLength = 50;
       
    35 const TInt KPort = 500;
       
    36 
       
    37 _LIT8(KHello, "hello...receivingtesting");
       
    38 // CONSTRUCTION
       
    39 UT_CFCReceiver* UT_CFCReceiver::NewL()
       
    40     {
       
    41     UT_CFCReceiver* self = UT_CFCReceiver::NewLC(); 
       
    42     CleanupStack::Pop(self);
       
    43     return self;
       
    44     }
       
    45 
       
    46 UT_CFCReceiver* UT_CFCReceiver::NewLC()
       
    47     {
       
    48     UT_CFCReceiver* self = new( ELeave ) UT_CFCReceiver();
       
    49     CleanupStack::PushL( self );
       
    50 	self->ConstructL(); 
       
    51     return self;
       
    52     }
       
    53 
       
    54 // Destructor (virtual by CBase)
       
    55 UT_CFCReceiver::~UT_CFCReceiver()
       
    56     {
       
    57     }
       
    58 
       
    59 // Default constructor
       
    60 UT_CFCReceiver::UT_CFCReceiver()
       
    61     {
       
    62     }
       
    63 
       
    64 // Second phase construct
       
    65 void UT_CFCReceiver::ConstructL()
       
    66     {
       
    67     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    68     // It generates the test case table.
       
    69     CEUnitTestSuiteClass::ConstructL();
       
    70     }
       
    71 
       
    72 //  METHODS
       
    73 
       
    74 
       
    75 
       
    76 void UT_CFCReceiver::SetupL(  )
       
    77     {
       
    78     iConnection = CFCConnectionContainer::NewL(*this);
       
    79 	
       
    80 	User::LeaveIfError( iSocketServ.Connect( KESockDefaultMessageSlots) );
       
    81 	 iEventCount = 0;    
       
    82 	iTimer = CPeriodic::NewL( EPriorityNormal );
       
    83     } 
       
    84 
       
    85 void UT_CFCReceiver::Teardown(  )
       
    86     {
       
    87 	if( iEventCount > 0)
       
    88 		{
       
    89 		if( !iWait.IsStarted() )
       
    90 			{
       
    91 			iWait.Start();
       
    92 			}
       
    93 		}
       
    94 	delete iTimer;
       
    95 	iTimer = NULL;
       
    96 
       
    97 	if (iReceiver)
       
    98 		{		
       
    99 		iReceiver->Cancel();
       
   100 		}
       
   101 	if(iSender)
       
   102 		iSender->Cancel();
       
   103 	if(iConnection)
       
   104 		{
       
   105 		iConnection->CloseConnection();
       
   106 		}
       
   107 	iSocket.Close();
       
   108 	delete iSender;
       
   109 	iSender = NULL;	
       
   110     delete iReceiver;
       
   111     iReceiver =NULL;
       
   112     delete iConnection;
       
   113     iConnection = NULL;
       
   114     iSocketServ.Close();
       
   115     delete iData;
       
   116     iData= NULL;
       
   117     }
       
   118 
       
   119 void UT_CFCReceiver::UT_CFCReceiver_NewLL(  )
       
   120     {
       
   121    	iConnection->OpenConnection(KIAPID, iSocketServ  );
       
   122    	iReceiver = CFCReceiver::NewL(iSocket, *this);
       
   123 	iConnection->Cancel();
       
   124     iConnection->RunL();
       
   125     
       
   126 	EUNIT_ASSERT( iErr==0);
       
   127     }
       
   128 
       
   129 void UT_CFCReceiver::UT_CFCReceiver_NewLCL(  )
       
   130     {
       
   131 	iConnection->OpenConnection(KIAPID, iSocketServ  );
       
   132    	iReceiver = CFCReceiver::NewLC(iSocket, *this);
       
   133    	CleanupStack::Pop( iReceiver );
       
   134    	iConnection->Cancel();
       
   135     iConnection->RunL();
       
   136     
       
   137 	EUNIT_ASSERT( iErr==0);
       
   138     }
       
   139 
       
   140 void UT_CFCReceiver::UT_CFCReceiver_ListenL(  )
       
   141     {
       
   142     iConnection->OpenConnection(KIAPID, iSocketServ  );
       
   143     
       
   144    	iReceiver = CFCReceiver::NewL(iSocket, *this);
       
   145     iConnection->Cancel();
       
   146     iConnection->RunL();
       
   147     
       
   148     EUNIT_ASSERT( iErr==0);
       
   149     }
       
   150 
       
   151 void UT_CFCReceiver::UT_CFCReceiver_ReceivedDataLL(  )
       
   152     {
       
   153     iReceiver =CFCReceiver::NewL(iSocket, *this);
       
   154     HBufC8* data = HBufC8::NewL(KLength);
       
   155 	data->Des().Copy(KHello);
       
   156 	iReceiver->ReceivedData(data);
       
   157 	EUNIT_ASSERT( data==iData);
       
   158  	}
       
   159 
       
   160 void UT_CFCReceiver::UT_CFCReceiver_RunErrorL(  )
       
   161     {
       
   162     iReceiver  = CFCReceiver::NewL(iSocket, *this);
       
   163     iConnection->OpenConnection(KIAPID, iSocketServ  );
       
   164     iConnection->Cancel();
       
   165     iConnection->RunL();
       
   166     
       
   167     EUNIT_ASSERT( iErr==0);
       
   168 	EUNIT_ASSERT( iReceiver->RunError(KErrNoMemory) == KErrNoMemory);
       
   169 	EUNIT_ASSERT( iReceiver->RunError(KErrBadName) == KErrNone);
       
   170 	}
       
   171 
       
   172 void UT_CFCReceiver::UT_CFCReceiver_TestRunLL(  )
       
   173 	{
       
   174 	iReceiver  = CFCReceiver::NewL(iSocket, *this);
       
   175 	TRequestStatus* status = &iReceiver->iStatus;
       
   176     User::RequestComplete(status, KErrNoMemory);
       
   177     EUNIT_ASSERT_SPECIFIC_LEAVE( iReceiver->RunL(), KErrNoMemory);
       
   178    	iReceiver->Cancel();
       
   179    	User::WaitForAnyRequest();
       
   180 	}
       
   181 	
       
   182 void UT_CFCReceiver::UT_CFCReceiver_TestRunL2L(  )
       
   183 	{
       
   184 	// with data
       
   185 	iConnection->OpenConnection( KIAPID, iSocketServ  );
       
   186 	iReceiver  = CFCReceiver::NewL(iSocket, *this);
       
   187 	iConnection->Cancel();
       
   188     iConnection->RunL();
       
   189     
       
   190 	HBufC8* msg = KHello().AllocL();
       
   191 	delete iReceiver->iReceivedMsg;
       
   192 	iReceiver->iReceivedMsg = msg;
       
   193  	TRequestStatus* status = &iReceiver->iStatus;
       
   194    	User::RequestComplete(status, KErrNone);
       
   195 	EUNIT_ASSERT_NO_LEAVE( iReceiver->RunL() );
       
   196     EUNIT_ASSERT( iErr==KErrNone);
       
   197     EUNIT_ASSERT( iData && iData->Compare( *msg ) == 0 );
       
   198     iReceiver->Cancel();
       
   199     User::WaitForAnyRequest();
       
   200 	}
       
   201 
       
   202 void UT_CFCReceiver::UT_CFCReceiver_TestRunL3L(  )
       
   203 	{
       
   204 	iReceiver  = CFCReceiver::NewL(iSocket, *this);
       
   205 	TRequestStatus* status = &iReceiver->iStatus;
       
   206     User::RequestComplete(status, KErrCancel);
       
   207     iReceiver->RunL();
       
   208     EUNIT_ASSERT( iErr==KErrNone);
       
   209    	iReceiver->Cancel();
       
   210    	User::WaitForAnyRequest();
       
   211 	}
       
   212 
       
   213 void UT_CFCReceiver::UT_CFCReceiver_TestRunL4L(  )
       
   214 	{
       
   215 	iReceiver  = CFCReceiver::NewL(iSocket, *this);
       
   216 	TRequestStatus* status = &iReceiver->iStatus;
       
   217     User::RequestComplete(status, KErrTotalLossOfPrecision );
       
   218     iReceiver->RunL();
       
   219     EUNIT_ASSERT( iErr==KErrTotalLossOfPrecision);
       
   220    	iReceiver->Cancel();
       
   221    	User::WaitForAnyRequest();
       
   222 	}
       
   223 
       
   224 void UT_CFCReceiver::ReceivedData(HBufC8* aData)
       
   225 	{
       
   226 	iData= aData;
       
   227 	}
       
   228 
       
   229 void UT_CFCReceiver::ErrorNotify(TInt aErrCode)
       
   230 	{
       
   231 	iErr=aErrCode;
       
   232 	}
       
   233 //
       
   234 void UT_CFCReceiver::ConnectionStarted( TInt aError )
       
   235 	{
       
   236 	TRAPD(leaveErr ,ConnectionStartedL(aError) );
       
   237 	if(leaveErr!=KErrNone)
       
   238 		{
       
   239 		ErrorNotify( leaveErr);	
       
   240 		}
       
   241 	iErr=aError;
       
   242 	}	
       
   243 void UT_CFCReceiver::ConnectionStartedL( TInt aError )
       
   244 	{
       
   245 	iConnectErr= aError;
       
   246 	User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet, KSockDatagram,
       
   247 								KProtocolInetUdp, iConnection->Connection()) );
       
   248 
       
   249 	TInetAddr addr;
       
   250 	addr.SetPort(KPort);
       
   251 	addr.SetAddress(KInetAddrAny);
       
   252 	TInt err = iSocket.Bind(addr);
       
   253 	if (err == KErrInUse)
       
   254 		{
       
   255 		ErrorNotify(KErrInUse);
       
   256 		}
       
   257 	else{
       
   258 		ErrorNotify(err);
       
   259 		}
       
   260 	EUNIT_ASSERT(iErr==0);
       
   261 	iReceiver->Listen();
       
   262 	}
       
   263 
       
   264 TInt UT_CFCReceiver::StopScheduler( TAny* aThis )
       
   265 	{
       
   266 	UT_CFCReceiver* self = reinterpret_cast< UT_CFCReceiver* >( aThis );
       
   267 	// set test to pass and stop scheduler
       
   268 	self->iCallBackCalled = ETrue;
       
   269 	return 0;
       
   270 	}
       
   271 	
       
   272 //  TEST TABLE
       
   273 
       
   274 EUNIT_BEGIN_TEST_TABLE( 
       
   275     UT_CFCReceiver,
       
   276     "UT_CFCReceiver",
       
   277     "UNIT" )
       
   278 
       
   279 EUNIT_TEST(
       
   280     "NewL - test ",
       
   281     "CFCReceiver",
       
   282     "NewL",
       
   283     "FUNCTIONALITY",
       
   284     SetupL, UT_CFCReceiver_NewLL, Teardown)
       
   285 
       
   286 EUNIT_TEST(
       
   287     "NewLC - test ",
       
   288     "CFCReceiver",
       
   289     "NewLC",
       
   290     "FUNCTIONALITY",
       
   291     SetupL, UT_CFCReceiver_NewLCL, Teardown)
       
   292 
       
   293 EUNIT_TEST(
       
   294     "Listen - test ",
       
   295     "CFCReceiver",
       
   296     "Listen",
       
   297     "FUNCTIONALITY",
       
   298     SetupL, UT_CFCReceiver_ListenL, Teardown)
       
   299 
       
   300 EUNIT_TEST(
       
   301     "ReceivedDataL - test ",
       
   302     "CFCReceiver",
       
   303     "ReceivedDataL",
       
   304     "FUNCTIONALITY",
       
   305     SetupL, UT_CFCReceiver_ReceivedDataLL, Teardown)
       
   306 
       
   307 EUNIT_TEST(
       
   308     "RunErrorL - test ",
       
   309     "CFCReceiver",
       
   310     "RunError",
       
   311     "FUNCTIONALITY",
       
   312     SetupL, UT_CFCReceiver_RunErrorL, Teardown)
       
   313 
       
   314 EUNIT_TEST(
       
   315     "RunL - test ",
       
   316     "CFCReceiver",
       
   317     "RunL",
       
   318     "FUNCTIONALITY",
       
   319     SetupL, UT_CFCReceiver_TestRunLL, Teardown)
       
   320 
       
   321 EUNIT_TEST(
       
   322     "RunL - test 2",
       
   323     "CFCReceiver",
       
   324     "RunL",
       
   325     "FUNCTIONALITY",
       
   326     SetupL, UT_CFCReceiver_TestRunL2L, Teardown)
       
   327     
       
   328 EUNIT_TEST(
       
   329     "RunL - test 3",
       
   330     "CFCReceiver",
       
   331     "RunL",
       
   332     "FUNCTIONALITY",
       
   333     SetupL, UT_CFCReceiver_TestRunL3L, Teardown)
       
   334 
       
   335 EUNIT_TEST(
       
   336     "RunL - test 4",
       
   337     "CFCReceiver",
       
   338     "RunL",
       
   339     "FUNCTIONALITY",
       
   340     SetupL, UT_CFCReceiver_TestRunL4L, Teardown)
       
   341 
       
   342 EUNIT_END_TEST_TABLE
       
   343 
       
   344 //  END OF FILE