mobilemessaging/smsmtm/test/src/t_SmsReplyToStep.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2005-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 //
       
    15 
       
    16 #include <testexecutelog.h>
       
    17 #include "t_SmsReplyToStep.h"
       
    18 #include <smuthdr.h>
       
    19 #include <txtrich.h>
       
    20 #include <msvuids.h>
       
    21 #include <smsuaddr.h>
       
    22 #include <csmsaccount.h>
       
    23 #include "gsmuIEOperations.h"
       
    24 #include <CommsDatTypesV1_1.h>
       
    25 
       
    26 using namespace CommsDat;
       
    27 LOCAL_D RTest test(_L("TestTextE_1_1"));
       
    28 _LIT(KTestMessageDir,"");
       
    29 
       
    30 const TTimeIntervalMicroSeconds32 KWaitForWatchersToStart = 20000000;
       
    31 const TTimeIntervalMicroSeconds32 KWaitForWatchersToStartDisplayInterval = 5000000;
       
    32 
       
    33 _LIT(KMessageData, "This is a simple text message");
       
    34 _LIT(KRecipientOk,		"+44111111111");  // has to be valid number and switched on
       
    35 
       
    36 		
       
    37 void EntryObserver::HandleEntryEventL(TMsvEntryEvent aEvent, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
       
    38 	{
       
    39 	switch (aEvent)
       
    40 		{
       
    41 			case EMsvEntryChanged:
       
    42 				{
       
    43 				break;
       
    44 				}
       
    45 			case EMsvNewChildren:
       
    46 				{	
       
    47 				CActiveScheduler::Stop();
       
    48 				break;
       
    49 				}
       
    50 						
       
    51 		default:
       
    52 				{
       
    53 				break;
       
    54 				}
       
    55 		}
       
    56 	}
       
    57 		
       
    58 CSmsReplyToStep::CSmsReplyToStep()
       
    59 :CTestStep()
       
    60 	{	
       
    61 	}
       
    62 		
       
    63 CSmsReplyToStep::~CSmsReplyToStep()
       
    64 	{
       
    65 	delete iTestUtils;
       
    66 	delete iEntry;
       
    67 	delete iScheduler;
       
    68 	delete iEntryObserver;
       
    69 	delete iSessionObserver;
       
    70 	delete iSession;
       
    71 	delete iWatchers;
       
    72 	delete iOperation;
       
    73 	delete iTestActive;
       
    74 	test.End();
       
    75 	test.Close();
       
    76 	__UHEAP_MARKEND;
       
    77 	}
       
    78 
       
    79 void CSmsReplyToStep::InitialiseReplyToStepL()
       
    80 {
       
    81 	SetTestStepName(KSmsReplyToStep1);
       
    82 	iScheduler = new (ELeave) CActiveScheduler;
       
    83 	CleanupStack::PushL(iScheduler); 
       
    84 	CActiveScheduler::Install(iScheduler);	
       
    85 }
       
    86 	
       
    87 void CSmsReplyToStep::InitialiseTestL()
       
    88 	{
       
    89 	INFO_PRINTF1(_L("Initialising the test..."));
       
    90 	iTestUtils = CSmsTestUtils::NewL(test);	
       
    91 	// Notify the system agent that the phone is on.
       
    92 	iTestUtils->NotifySaPhoneOnL();
       
    93 	
       
    94 	// Restore the service settings.
       
    95 	iTestUtils->SetEntryL(iTestUtils->iSmsServiceId);
       
    96 	CSmsAccount* account = CSmsAccount::NewLC();
       
    97 	account->LoadSettingsL(*iTestUtils->iServiceSettings);
       
    98 	
       
    99 	// Remove the default service centres from the service settings
       
   100 	TInt count = iTestUtils->iServiceSettings->ServiceCenterCount();
       
   101 	while( count-- )
       
   102 		{
       
   103 		iTestUtils->iServiceSettings->RemoveServiceCenter(count);
       
   104 		}
       
   105 		
       
   106 	// Add the Vodafone service centre - store the settings.
       
   107 	iTestUtils->iServiceSettings->AddServiceCenterL(_L("Vodafone"), _L("+447785016005"));
       
   108 
       
   109 	// Set delivery options - do matching and make reports visible.	
       
   110 	iTestUtils->iServiceSettings->SetDeliveryReport(ETrue);
       
   111 	iTestUtils->iServiceSettings->SetStatusReportHandling(CSmsSettings::EMoveReportToInboxVisibleAndMatch);
       
   112 
       
   113 	account->SaveSettingsL(*iTestUtils->iServiceSettings);
       
   114 	CleanupStack::PopAndDestroy(account);
       
   115 
       
   116 	// set up an opserver to observe the inbox
       
   117 	iEntryObserver = new (ELeave) EntryObserver();
       
   118 	iSessionObserver = new (ELeave) SessionObserver();
       
   119 	iSession = CMsvSession::OpenSyncL(*iSessionObserver);
       
   120 	TMsvSelectionOrdering ordering;	
       
   121 	iEntry = CMsvEntry::NewL(*iSession, KMsvGlobalInBoxIndexEntryIdValue,ordering);
       
   122 	iEntry->AddObserverL(*iEntryObserver);
       
   123 	
       
   124 	//used for acynchronous calls
       
   125 	iTestActive=new(ELeave) CTestActive;
       
   126 	
       
   127 	INFO_PRINTF1(_L("Successfully initialised test"));
       
   128 	
       
   129 }
       
   130 	
       
   131 void CSmsReplyToStep::InitialiseSimTsyL()
       
   132 	{
       
   133 	
       
   134 	INFO_PRINTF1(_L("Initialising the SimTsy..."));
       
   135 	
       
   136 	_LIT(KDefaultTsyName, "SIM"); 
       
   137 	HBufC* tsyName =KDefaultTsyName().AllocLC();
       
   138 
       
   139 	//Initialize TSY using the System Agent
       
   140 	TInt testState;
       
   141 	TInt testNumber=1;
       
   142 	if(KErrNone != RProperty::Get(KUidPSSimTsyCategory, KPSSimTsyTestNumber, testState))
       
   143 		{
       
   144 		User::LeaveIfError(RProperty::Define(KUidPSSimTsyCategory, KPSSimTsyTestNumber, RProperty::EInt));
       
   145 		}
       
   146 	User::LeaveIfError(RProperty::Set(KUidPSSimTsyCategory, KPSSimTsyTestNumber, testNumber));
       
   147 	if(KErrNone != RProperty::Get(KUidSystemCategory, KMsvTestUidPhonePwrValue, testState))
       
   148 		{
       
   149 		User::LeaveIfError(RProperty::Define(KUidSystemCategory, KMsvTestUidPhonePwrValue, RProperty::EInt));
       
   150 		}
       
   151 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KMsvTestUidPhonePwrValue, EMsvTestPhoneOn));
       
   152 
       
   153 	User::LeaveIfError(RProperty::Get(KUidPSSimTsyCategory, KPSSimTsyTestNumber, testNumber));
       
   154 
       
   155 	RTelServer etelServer;
       
   156 	User::LeaveIfError(etelServer.Connect());
       
   157 	CleanupClosePushL(etelServer);
       
   158 	User::LeaveIfError(etelServer.LoadPhoneModule(tsyName->Des()));
       
   159 
       
   160 	// Find the phone corresponding to this TSY and open a number of handles on it
       
   161 	TInt numPhones;
       
   162 	User::LeaveIfError(etelServer.EnumeratePhones(numPhones));	
       
   163 	TBool found=EFalse;
       
   164 	
       
   165 	RMobilePhone iPhone;
       
   166 	while (!found && numPhones--)
       
   167 		{
       
   168 		TName phoneTsy;
       
   169 		User::LeaveIfError(etelServer.GetTsyName(numPhones,phoneTsy));
       
   170 		if (phoneTsy.CompareF(tsyName->Des())==KErrNone)
       
   171 			{
       
   172 			found = ETrue;
       
   173 			RTelServer::TPhoneInfo info;
       
   174 			User::LeaveIfError(etelServer.GetPhoneInfo(numPhones,info));
       
   175 			CleanupClosePushL(iPhone);
       
   176 			User::LeaveIfError(iPhone.Open(etelServer,info.iName));
       
   177 			User::LeaveIfError(iPhone.Initialise());
       
   178 			//Required Pause to Allow SMSStack to Complete its Async Init call to the TSY and finish its StartUp.
       
   179 			TTimeIntervalMicroSeconds32 InitPause=9000000;  
       
   180 			User::After(InitPause);							
       
   181 			
       
   182 			CleanupStack::PopAndDestroy(&iPhone);
       
   183 			}
       
   184 		}
       
   185 	
       
   186 	CleanupStack::PopAndDestroy(&etelServer);	
       
   187 
       
   188 	CMDBSession* dbSession = CMDBSession::NewL(CMDBSession::LatestVersion());
       
   189 	CleanupStack::PushL(dbSession);
       
   190 	CMDBRecordSet<CCDGlobalSettingsRecord> globalSettingsRecord(KCDTIdGlobalSettingsRecord);
       
   191 	TRAPD(err, globalSettingsRecord.LoadL(*dbSession));
       
   192 	if(err != KErrNone)
       
   193 		{
       
   194 		User::Leave(KErrNotFound);				
       
   195 		}
       
   196 	
       
   197 	CCDModemBearerRecord *modemRecord = static_cast<CCDModemBearerRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdModemBearerRecord));		
       
   198 	CleanupStack::PushL(modemRecord);
       
   199 	modemRecord->SetRecordId(((CCDGlobalSettingsRecord*)globalSettingsRecord.iRecords[0])->iModemForPhoneServicesAndSMS);
       
   200 	modemRecord->LoadL(*dbSession);
       
   201 	modemRecord->iTsyName.SetMaxLengthL(tsyName->Des().Length());
       
   202 	modemRecord->iTsyName = tsyName->Des();	
       
   203 	modemRecord->ModifyL(*dbSession);
       
   204 	CleanupStack::PopAndDestroy(3);  //tsyName, dbSession, modemRecord 	
       
   205 	
       
   206 	INFO_PRINTF1(_L("Successfully initialised the Sim Tsy"));
       
   207 	}
       
   208 	
       
   209 void CSmsReplyToStep::StartWatcherL()
       
   210 	{
       
   211 	INFO_PRINTF1(_L("Start the Watcher"));
       
   212 
       
   213 	if( WatchersAlreadyRunningL() )
       
   214 		{
       
   215 		INFO_PRINTF1(_L("Watchers are already running\n"));
       
   216 		return;
       
   217 		}
       
   218 
       
   219 	iWatchers = CTestUtilsWatcherStarter::NewL();
       
   220 
       
   221 	TTimeIntervalMicroSeconds32 wait = KWaitForWatchersToStart;
       
   222 	TBool started = EFalse;
       
   223 	while( !started && wait.Int() > 0 )
       
   224 		{
       
   225 		INFO_PRINTF2(_L("Waiting %d secs for watchers to start..."), wait.Int() / 1000000);
       
   226 		wait = wait.Int() - KWaitForWatchersToStartDisplayInterval.Int();
       
   227 		User::After(KWaitForWatchersToStartDisplayInterval);
       
   228 		started = WatchersAlreadyRunningL();
       
   229 		}
       
   230 
       
   231 	if( !WatchersAlreadyRunningL() )
       
   232 		{
       
   233 		INFO_PRINTF1(_L("WARNING: NBS Watcher has not started yet\n"));
       
   234 		}
       
   235 	}
       
   236 	
       
   237 TBool CSmsReplyToStep::WatchersAlreadyRunningL()
       
   238 	{
       
   239 	RSocketServ server;
       
   240 	RSocket socket;
       
   241 
       
   242 	User::LeaveIfError(server.Connect());
       
   243 	CleanupClosePushL(server);
       
   244 
       
   245 		// test if it's there... 
       
   246 	TProtocolDesc protoInfo;
       
   247 	TProtocolName protocolname;
       
   248 	protocolname.Copy(KSmsDatagram);
       
   249 	User::LeaveIfError(server.FindProtocol(protocolname,protoInfo));
       
   250 
       
   251 	User::LeaveIfError(socket.Open(server,
       
   252 										protoInfo.iAddrFamily,
       
   253 										protoInfo.iSockType, 
       
   254 										protoInfo.iProtocol));
       
   255 	CleanupClosePushL(socket);
       
   256 	
       
   257 
       
   258 	TSmsAddr addr;
       
   259 	addr.SetSmsAddrFamily(ESmsAddrRecvAny);
       
   260 	TInt err = socket.Bind(addr);
       
   261 
       
   262 	CleanupStack::Pop(2, &server);
       
   263 	socket.CancelAll();
       
   264 	socket.Close();
       
   265 	server.Close();
       
   266 	return (err == KErrAlreadyExists);
       
   267 	}
       
   268 
       
   269 void CSmsReplyToStep::SetRecipientsL(CSmsHeader& aHeader)
       
   270 	{
       
   271 	iTestUtils->Printf(_L("--Setting single recipient\n"));
       
   272 	
       
   273 	CSmsNumber* rcpt = CSmsNumber::NewL();
       
   274 	CleanupStack::PushL(rcpt);
       
   275 	rcpt->SetAddressL(KRecipientOk);
       
   276 	aHeader.Recipients().AppendL(rcpt);
       
   277 	CleanupStack::Pop(rcpt);
       
   278 	}
       
   279 	
       
   280 void CSmsReplyToStep::CreateMessageL()
       
   281 	{
       
   282 	INFO_PRINTF1(_L("Creating message..."));
       
   283 	
       
   284 	TMsvEntry entry;
       
   285 	entry.SetVisible(ETrue); 
       
   286 	entry.SetInPreparation(ETrue); 
       
   287 	entry.iServiceId = iTestUtils->iSmsServiceId; 
       
   288 	entry.iType = KUidMsvMessageEntry; 
       
   289 	entry.iMtm = KUidMsgTypeSMS; 
       
   290 	entry.iDate.HomeTime(); 
       
   291 	entry.iSize = 0; 
       
   292 	entry.iDescription.Set(KNullDesC); 
       
   293 	entry.iDetails.Set(KNullDesC); 
       
   294 	entry.SetSendingState(KMsvSendStateScheduled);
       
   295 
       
   296 	// Create the SMS header object...
       
   297 	CSmsHeader* header = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *iTestUtils->iRichText);
       
   298 	CleanupStack::PushL(header);
       
   299 	
       
   300 	// Set the body text...
       
   301 	iTestUtils->iRichText->Reset();
       
   302 	iTestUtils->iRichText->InsertL(0, KMessageData);
       
   303 	
       
   304 	// Copy the message settings...
       
   305 	header->SetSmsSettingsL(*iTestUtils->iServiceSettings); 
       
   306 	
       
   307 	// Set the service centre
       
   308 	TInt defaultIndex = iTestUtils->iServiceSettings->DefaultServiceCenter();
       
   309 	header->SetServiceCenterAddressL(iTestUtils->iServiceSettings->GetServiceCenter(defaultIndex).Address());
       
   310 
       
   311 	// Set recipient - ask derived class
       
   312 	SetRecipientsL(*header);
       
   313 	
       
   314 	// Update entry description and details...
       
   315 	CArrayPtrFlat<CSmsNumber>& recipient = header->Recipients();
       
   316 	entry.iDetails.Set(recipient[0]->Address());
       
   317 	entry.iDescription.Set(iTestUtils->iRichText->Read(0, iTestUtils->iServiceSettings->DescriptionLength()));
       
   318 	entry.SetInPreparation(EFalse);
       
   319 	
       
   320 	// Create the entry - set context to the global outbox.
       
   321 	iTestUtils->iMsvEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   322 	iTestUtils->iMsvEntry->CreateL(entry);
       
   323 
       
   324 	// Create new store and save header information 
       
   325 	iTestUtils->iMsvEntry->SetEntryL(entry.Id()); 
       
   326 	CMsvStore* store = iTestUtils->iMsvEntry->EditStoreL(); 
       
   327 	CleanupStack::PushL(store); 
       
   328 	header->StoreL(*store);
       
   329 	store->StoreBodyTextL(*iTestUtils->iRichText);
       
   330 	store->CommitL(); 
       
   331 	CleanupStack::PopAndDestroy(2, header); 
       
   332 	
       
   333 	iMessageId = entry.Id();
       
   334 
       
   335 	}
       
   336 	
       
   337 void CSmsReplyToStep::SendMessageL()
       
   338 	{
       
   339 	INFO_PRINTF1(_L("Sending message..."));
       
   340 
       
   341 	delete iOperation;
       
   342 	iOperation = NULL;
       
   343 
       
   344 	CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection();
       
   345 	CleanupStack::PushL(sel);
       
   346 	sel->AppendL(iMessageId);
       
   347 	iTestUtils->DisplayMessageL(iMessageId);
       
   348 	iTestUtils->iMsvEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   349 	iOperation = iTestUtils->iMsvEntry->CopyL(*sel, iTestUtils->iSmsServiceId,iTestActive->iStatus);
       
   350 	iTestActive->StartL();
       
   351 	CActiveScheduler::Start();	
       
   352 	CleanupStack::PopAndDestroy(sel);
       
   353 	}
       
   354 
       
   355 TBool CSmsReplyToStep::CheckMessageL()
       
   356 	{
       
   357 
       
   358 	INFO_PRINTF1(_L("Received new message in Inbox - should be status report..."));
       
   359 		
       
   360 	TMsvSelectionOrdering ordering;	
       
   361 	ordering.ShowInvisibleEntries();
       
   362 	CMsvEntry* entry = CMsvEntry::NewL(*iSession, KMsvGlobalInBoxIndexEntryId,ordering);
       
   363 	CleanupStack::PushL(entry);
       
   364 	
       
   365 	CMsvEntrySelection* msvEntrySelection;
       
   366 	msvEntrySelection=entry->ChildrenL();
       
   367 	CleanupStack::PushL(msvEntrySelection);
       
   368 	
       
   369 	// Check how many mesasges there are - should be just 1
       
   370 	TInt count = msvEntrySelection->Count();
       
   371 	INFO_PRINTF3(_L("Number of new messages: %d, should be: %d"), count, 1);
       
   372 
       
   373 	// Restore the message
       
   374 	CSmsHeader* header = CSmsHeader::NewL(CSmsPDU::ESmsDeliver, *iTestUtils->iRichText);
       
   375 	CleanupStack::PushL(header);
       
   376 	entry->SetEntryL((*msvEntrySelection)[0]);
       
   377 
       
   378 	CMsvStore* store = entry->ReadStoreL();
       
   379 	CleanupStack::PushL(store);	
       
   380 	header->RestoreL(*store);
       
   381 	
       
   382 	if(store->HasBodyTextL())
       
   383 		{
       
   384 		store->RestoreBodyTextL(*iTestUtils->iRichText);	
       
   385 		}
       
   386 		
       
   387 	TBuf<KSmsDescriptionLength> desc;
       
   388 	User::LeaveIfError(TSmsUtilities::GetDescription(header->Message(), desc));
       
   389 	
       
   390 	//Check for a reply address field
       
   391 	CSmsReplyAddressOperations& operations = STATIC_CAST(CSmsReplyAddressOperations&,header->Message().GetOperationsForIEL(CSmsInformationElement::ESmsReplyAddressFormat));
       
   392 	TBool result=operations.ContainsReplyAddressIEL();
       
   393 
       
   394 	CleanupStack::PopAndDestroy(4,entry);	
       
   395 	return result;	
       
   396 	}
       
   397 
       
   398 TVerdict CSmsReplyToStep::doTestStepPreambleL()
       
   399 	{	
       
   400 	__UHEAP_MARK;
       
   401 	SetTestStepResult(EPass);
       
   402 	test.Start(_L("sdf"));
       
   403 	InitialiseTestL();
       
   404 	InitialiseSimTsyL();
       
   405 	StartWatcherL();
       
   406 	iTestUtils->DeleteSmsMessagesL(KMsvGlobalOutBoxIndexEntryId);
       
   407 	iTestUtils->DeleteSmsMessagesL(KMsvGlobalInBoxIndexEntryId);
       
   408 	iTestUtils->DeleteSmsMessagesL(KMsvSentEntryId);
       
   409 	CreateMessageL();
       
   410 	SendMessageL();
       
   411 	//wait for message to be received...
       
   412 	CActiveScheduler::Start();	
       
   413 
       
   414 	return TestStepResult();
       
   415 	}
       
   416 		
       
   417 
       
   418 TVerdict CSmsReplyToStep::doTestStepL()
       
   419 	{		
       
   420 	if(CheckMessageL())
       
   421 		{
       
   422 		SetTestStepResult(EPass);	
       
   423 		}
       
   424 	else
       
   425 		{
       
   426 		SetTestStepResult(EFail);			
       
   427 		}
       
   428 	
       
   429 	return TestStepResult();
       
   430 	}	
       
   431 
       
   432 TVerdict CSmsReplyToStep::doTestStepPostambleL()
       
   433 /**
       
   434  * @return - TVerdict code
       
   435  * Override of base class virtual
       
   436  */
       
   437 	{
       
   438 	return TestStepResult();
       
   439 	}
       
   440 	
       
   441 	
       
   442 
       
   443