/**
 * @SYMTestCaseID BA-CTSY-INT-MB-0013
 * @SYMFssID BA/CTSY/PKTS-0013
 * @SYMTestCaseDesc Support being notified when MBMS network service availability changes to unavailable.
 * @SYMTestPriority High
 * @SYMTestActions  RPacketService::NotifyMbmsNetworkServiceStatusChange
 * @SYMTestExpectedResults Pass - MBMS network availability status is returned correctly.  
 * @SYMTestType CIT
 * @SYMTestCaseDependencies live/manual
 *
 * Reason for test: Verify that NotifyMbmsNetworkServiceStatusChange completes correctly with correct MBMS network status.
 *
 * @return - none
 */
void CCTsyUCaseMbms007::CCtsyIntegrationTestMbms0001L()
    {
        
	//
	// SET UP
	//
	        
   OpenEtelServerL( EUseExtendedError );
	CleanupStack::PushL( TCleanupItem( Cleanup, this ) );
	OpenPhoneL();	

	RBuf8 data;
	CleanupClosePushL(data);

	RBuf8 data2;
	CleanupClosePushL(data2);
				
	RPacketService packetService;
    OpenPacketServiceL( packetService );
    CleanupClosePushL( packetService );

    // Open new context                
	TInfoName contextId;
    RPacketMbmsContext packetMbmsContext;
    packetMbmsContext.OpenNewContext( packetService, contextId );
    CleanupClosePushL( packetMbmsContext );
   
   
   	TRequestStatus requestStatus;
   	TRequestStatus requestStatus2;
	TMbmsNetworkServiceStatus regStatus;
   	TRequestStatus mockLtsyStatus;
   	TMbmsNetworkServiceStatus regStatuscomplete = EMbmsSupported; 	
   		
    // Ensure packet service status is RPacketService::EStatusAttached.
	RPacketService::TStatus sendStatus = RPacketService::EStatusAttached;
	TBool isResumed = EFalse;
	TMockLtsyData2 <RPacketService::TStatus, TBool > ltsyData(sendStatus, isResumed);
	ltsyData.SerialiseL(data);
	RPacketService::TStatus contextStatus;
	packetService.NotifyStatusChange(requestStatus2, contextStatus);
	
	iMockLTSY.NotifyTerminated(mockLtsyStatus);	
	iMockLTSY.CompleteL(EPacketNotifyStatusChange, KErrNone, data); //
	
	User::WaitForRequest(mockLtsyStatus);
	User::WaitForRequest(requestStatus2);
	AssertMockLtsyStatusL();
	ASSERT_EQUALS(KErrNone, requestStatus2.Int());
	ASSERT_EQUALS(sendStatus, contextStatus);	

	// post RPacketService::NotifyMbmsNetworkServiceStatusChange
    packetService.NotifyMbmsNetworkServiceStatusChange(requestStatus, regStatus);
    
	TMbmsNetworkServiceStatus sndData = EMbmsSupported;
	TMockLtsyData1<TMbmsNetworkServiceStatus> ltsyData2(sndData);
	data.Close();
	ltsyData2.SerialiseL(data);		
	iMockLTSY.NotifyTerminated(mockLtsyStatus);		
    
    // Ensure RPacketService::NotifyMbmsNetworkServiceStatusChange completes with status EMbmsSupported.
	iMockLTSY.CompleteL(EPacketNotifyMbmsNetworkServiceStatusChange, KErrNone, data);
	User::WaitForRequest(mockLtsyStatus);	
	ASSERT_EQUALS(KErrNone, mockLtsyStatus.Int());	
    ASSERT_TRUE(regStatuscomplete == regStatus);
    
	//
	// SET UP END
	//    
            
	//
	// TEST START
	//
	
	
	regStatuscomplete = EMbmsSupportUnknown; 
			
	// post RPacketService::NotifyMbmsNetworkServiceStatusChange				
    packetService.NotifyMbmsNetworkServiceStatusChange(requestStatus, regStatus);
    
	// Ensure packet service status is RPacketService::EStatusUnattached
   	sendStatus = RPacketService::EStatusUnattached;
	isResumed = EFalse;
	TMockLtsyData2 <RPacketService::TStatus, TBool > ltsyData3(sendStatus, isResumed);
	ltsyData3.SerialiseL(data);
	packetService.NotifyStatusChange(requestStatus2, contextStatus);
	
	iMockLTSY.NotifyTerminated(mockLtsyStatus);	
	iMockLTSY.CompleteL(EPacketNotifyStatusChange, KErrNone, data); //
	
	User::WaitForRequest(mockLtsyStatus);
	User::WaitForRequest(requestStatus2);
	AssertMockLtsyStatusL();
	ASSERT_EQUALS(KErrNone, requestStatus2.Int());
	ASSERT_EQUALS(sendStatus, contextStatus);
		 
	TMbmsNetworkServiceStatus sndData2 = EMbmsSupportUnknown;
	TMockLtsyData1<TMbmsNetworkServiceStatus> ltsyData4(sndData2);
	data.Close();
	ltsyData4.SerialiseL(data);		
	iMockLTSY.NotifyTerminated(mockLtsyStatus);	
		
    // Ensure RPacketService::NotifyMbmsNetworkServiceStatusChange completes with status EMbmsSupportUnknown.
	iMockLTSY.CompleteL(EPacketNotifyMbmsNetworkServiceStatusChange, KErrNone, data);
	User::WaitForRequest(mockLtsyStatus);	
	ASSERT_EQUALS(KErrNone, mockLtsyStatus.Int());	
    ASSERT_TRUE(regStatuscomplete == regStatus);
	
	//
	// TEST END
	//	
	
	AssertMockLtsyStatusL();
	CleanupStack::PopAndDestroy( 5, this ); // packetMbmsContext, packetService, data, data2, this 
    }
/**
 * @SYMTestCaseID BA-CTSY-INT-MB-0014
 * @SYMFssID BA/CTSY/PKTS-0014
 * @SYMTestCaseDesc Support updating monitor list by adding an entry.
 * @SYMTestPriority High
 * @SYMTestActions RPacketService::NotifyMbmsNetworkServiceStatusChange, RPacketService::UpdateMbmsMonitorServiceListL
 * @SYMTestExpectedResults Pass - Service is added to the monitored service list.  
 * @SYMTestType CIT
 * @SYMTestCaseDependencies live/manual
 *
 * Reason for test: Verify that services is added correctly to the monitored service list.
 *
 * @return - none
 */
void CCTsyUCaseMbms008::CCtsyIntegrationTestMbms0001L()
    {
        
	//
	// SET UP
	//
	        
   	OpenEtelServerL( EUseExtendedError );
	CleanupStack::PushL( TCleanupItem( Cleanup, this ) );
	OpenPhoneL();	

	RBuf8 data;
	CleanupClosePushL(data);
				
	RPacketService packetService;
    OpenPacketServiceL( packetService );
    CleanupClosePushL( packetService );

   	TRequestStatus requestStatus;
   	TRequestStatus requestStatus2;
   	TRequestStatus mockLtsyStatus;
   	    
    // Ensure packet service status is RPacketService::EStatusAttached.
	RPacketService::TStatus sendStatus = RPacketService::EStatusAttached;
	TBool isResumed = EFalse;
	TMockLtsyData2 <RPacketService::TStatus, TBool > ltsyData(sendStatus, isResumed);
	ltsyData.SerialiseL(data);
	RPacketService::TStatus contextStatus;
	packetService.NotifyStatusChange(requestStatus2, contextStatus);
	
	iMockLTSY.NotifyTerminated(mockLtsyStatus);	
	iMockLTSY.CompleteL(EPacketNotifyStatusChange, KErrNone, data); //
	
	User::WaitForRequest(mockLtsyStatus);
	User::WaitForRequest(requestStatus2);
	AssertMockLtsyStatusL();
	ASSERT_EQUALS(KErrNone, requestStatus2.Int());
	ASSERT_EQUALS(sendStatus, contextStatus);
     	    
	//
	// SET UP END
	//    
            
	//
	// TEST START
	//
	
	
   	TInt maxAllowed = 3; 
	TInt maxvalue = 3;
 	
 	// check maximum monitored service value .
 	TMockLtsyData1<TInt> ltsyData2(maxvalue);
 	data.Close();
	ltsyData2.SerialiseL(data);
 	iMockLTSY.NotifyTerminated(mockLtsyStatus);	
	iMockLTSY.CompleteL(EPacketEnumerateMbmsMonitorServiceList, KErrNone, data);
	User::WaitForRequest(mockLtsyStatus);	
	ASSERT_EQUALS(KErrNone, mockLtsyStatus.Int());		
	ASSERT_EQUALS( maxvalue, maxAllowed );

 	CFilteringActiveScheduler scheduler;
	CActiveScheduler::Install(&scheduler);
	
	// post RPacketService::NotifyMbmsNetworkServiceAvailabilityChange
	packetService.NotifyMbmsServiceAvailabilityChange( requestStatus );
		
	//List retriever
	CRetrievePcktMbmsMonitoredServices* monitorList = CRetrievePcktMbmsMonitoredServices::NewL(packetService);	 
	CleanupStack::PushL(monitorList);

	CActiveRetriever::ResetRequestsNumber();
	CActiveRetriever* activeRetriever = CActiveRetriever::NewL(*monitorList);
	CleanupStack::PushL(activeRetriever);
	scheduler.AddRetrieverL(*activeRetriever);
							
	RBuf8 expectData1;
	CleanupClosePushL(expectData1);
	TInt i;
    TMockLtsyData1<TInt> expExpect1(i);
    expExpect1.SerialiseL(expectData1);
    
	RBuf8 CompleteData;
	CleanupClosePushL(CompleteData);
	
	TRequestStatus aReqStatus;
    RPacketService::TMbmsServiceAvailabilityV1 existingParams;
    
    
    CPcktMbmsMonitoredServiceList* multipleEntries = CPcktMbmsMonitoredServiceList ::NewL();
	CleanupStack::PushL(multipleEntries);
	
	for(TInt i=0;i < MBMS_MONITORSERVICECOUNTMULTIPLE ;i++)
		{
		existingParams.iTmgi.SetServiceId(i); 
	    existingParams.iTmgi.SetMCC(i);
	    existingParams.iTmgi.SetMNC(i);
	    existingParams.iMbmsServiceMode = KBroadcast;
	    existingParams.iMbmsAccessBearer = E2GBearer;
	   	existingParams.iMbmsAvailabilityStatus = EMbmsUnavailable;
		multipleEntries->AddEntryL( existingParams );
		}
	
	iMockLTSY.ExpectL(EPacketUpdateMbmsMonitorServiceList, expectData1);
	iMockLTSY.CompleteL(EPacketUpdateMbmsMonitorServiceList, KErrNone, CompleteData);
	
	// PacketService::UpdateMbmsMonitorServiceListL
	packetService.UpdateMbmsMonitorServiceListL(aReqStatus, EAddEntries, multipleEntries);	
	
	// wait for completion of UpdateMbmsMonitorServiceListL(	
	User::WaitForRequest(aReqStatus);
	// wait for completion of NotifyMbmsServiceAvailabilityChange
	User::WaitForRequest( requestStatus );
	AssertMockLtsyStatusL();		
	ASSERT_EQUALS( KErrNone, aReqStatus.Int() );

	monitorList->Start(activeRetriever->Status());
	activeRetriever->Activate();
	scheduler.StartScheduler();

	ASSERT_EQUALS(0, CActiveRetriever::ResetRequestsNumber());
	ASSERT_EQUALS(KErrNone, activeRetriever->iStatus.Int());

	CPcktMbmsMonitoredServiceList* list = monitorList->RetrieveListL();
	CleanupStack::PushL( list );
	
	ASSERT_EQUALS(MBMS_MONITORSERVICECOUNTMULTIPLE, list->Enumerate() );
	
	monitorList->Start( activeRetriever->Status() );
	activeRetriever->Activate();
	scheduler.StartScheduler();

	ASSERT_EQUALS( 0, CActiveRetriever::ResetRequestsNumber() );
	ASSERT_EQUALS( KErrNone, activeRetriever->iStatus.Int() );
	
	list = monitorList->RetrieveListL();
	CleanupStack::PushL( list );
	
	// ensure that Services is added correctly into the monitored service list	
	ASSERT_TRUE( TComparator<CPcktMbmsMonitoredServiceList>::IsEqual( *multipleEntries, *list ) );

	//
	// TEST END
	//	
	
	AssertMockLtsyStatusL();
	CleanupStack::PopAndDestroy( 10, this ); // packetMbmsContext, packetService, this 
    }