mobilemessaging/smsmtm/test/src/T_SmsSendAs.cpp
changeset 80 8b14b30db193
parent 0 72b543305e3a
equal deleted inserted replaced
79:2981cb3aa489 80:8b14b30db193
       
     1 // Copyright (c) 1999-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 "t_smssendas.h"
       
    17 #include "smscmds.h"
       
    18 #include "smuthdr.h"
       
    19 #include <txtrich.h>
       
    20 #include <txtfmlyr.h>
       
    21 #include "smsclnt.h"
       
    22 
       
    23 RTest test(_L("T_SmsSendAs Testrig"));
       
    24 CTrapCleanup* theCleanup;
       
    25 
       
    26 _LIT(KSmsScript, "sms.script");
       
    27 _LIT(KWapScript, "wap.script");
       
    28 _LIT(KNbsScript, "nbs.script");
       
    29 
       
    30 LOCAL_C void doMainL()
       
    31 	{
       
    32 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
    33 	CleanupStack::PushL(scheduler);
       
    34 	CActiveScheduler::Install( scheduler );
       
    35 
       
    36 	CSmsTestUtils* smsTest = CSmsTestUtils::NewL(test);
       
    37 	CleanupStack::PushL(smsTest);
       
    38 	TInt nextTest = 0;
       
    39 
       
    40 	smsTest->NotifySaPhoneOnL();
       
    41 
       
    42 	CSmsSendAsTest* asTest = CSmsSendAsTest::NewL(*smsTest, KNullDesC, nextTest);
       
    43 	CleanupStack::PushL(asTest);
       
    44 
       
    45 	smsTest->WaitForInitializeL();
       
    46 
       
    47 	asTest->StartL();
       
    48 
       
    49 	//CleanupStack::PopAndDestroy(3); //SmsTest, scheduler
       
    50 	CleanupStack::PopAndDestroy(asTest);
       
    51 	CleanupStack::PopAndDestroy(smsTest);
       
    52 	CleanupStack::PopAndDestroy(scheduler);
       
    53 	}
       
    54 
       
    55 GLDEF_C TInt E32Main()
       
    56 	{	
       
    57 	__UHEAP_MARK;
       
    58 	test.Start(_L("Setup"));
       
    59 	theCleanup = CTrapCleanup::New();
       
    60 	TRAPD(ret,doMainL());		
       
    61 	test(ret==KErrNone);
       
    62 	delete theCleanup;	
       
    63 	test.Console()->SetPos(0, 13);
       
    64 	test.End();
       
    65 	test.Close();
       
    66 	__UHEAP_MARKEND;
       
    67 	return(KErrNone);
       
    68 	}
       
    69 
       
    70 
       
    71 CSmsSendAsTest::~CSmsSendAsTest()
       
    72 /**
       
    73 	destructor
       
    74  */
       
    75 	{
       
    76 	iSocket.Close();
       
    77 	iSocketServ.Close();
       
    78 	}
       
    79 
       
    80 CSmsSendAsTest* CSmsSendAsTest::NewL(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aNextTest)
       
    81 	{
       
    82 	CSmsSendAsTest* self = new (ELeave) CSmsSendAsTest(aSmsTest, aScriptFile, aNextTest);
       
    83 	CleanupStack::PushL(self);
       
    84 
       
    85 	self->ConstructL();
       
    86 
       
    87 	CleanupStack::Pop();
       
    88 	return self;
       
    89 	}
       
    90 
       
    91 
       
    92 
       
    93 CSmsSendAsTest::CSmsSendAsTest(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aNextTest)
       
    94 : CSmsTestBase(aSmsTest, aScriptFile, aNextTest), iState(EStateWaiting)
       
    95 	{
       
    96 	}
       
    97 
       
    98 void CSmsSendAsTest::RunAutoL()
       
    99 /**
       
   100 	Runs tests in sequence, checks iStatus
       
   101  */
       
   102 	{
       
   103 	iSmsTest.TestStart(++iNextTest, _L("Test SendAs"));
       
   104 	TestSendAsL();
       
   105 	User::LeaveIfError(iStatus.Int());
       
   106 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
   107 	
       
   108 	iSmsTest.TestStart(++iNextTest, _L("Send the messages"));
       
   109 	TestSendingL();
       
   110 	User::LeaveIfError(iStatus.Int());
       
   111 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
   112 	}
       
   113 
       
   114 void CSmsSendAsTest::ConstructL()
       
   115 	{
       
   116 	iSmsTest.SetLogToFile();
       
   117 	iSmsTest.DeleteServicesL();
       
   118 	iSmsTest.CreateServicesL();
       
   119 
       
   120 	iSelection = new (ELeave) CMsvEntrySelection();
       
   121 	iTimer = CTestTimer::NewL();
       
   122 
       
   123 	User::LeaveIfError(iSocketServ.Connect());
       
   124 	TProtocolDesc protoinfo;
       
   125 	TProtocolName protocolname(KSmsDatagram);
       
   126 	User::LeaveIfError(iSocketServ.FindProtocol(protocolname,protoinfo));
       
   127 	User::LeaveIfError(iSocket.Open(iSocketServ,protoinfo.iAddrFamily,protoinfo.iSockType,protoinfo.iProtocol));
       
   128 
       
   129 	TSmsAddr smsaddr;
       
   130 	smsaddr.SetSmsAddrFamily(ESmsAddrSendOnly);
       
   131 	User::LeaveIfError(iSocket.Bind(smsaddr));
       
   132 
       
   133 	CActiveScheduler::Add(this);
       
   134 	}
       
   135 
       
   136 void CSmsSendAsTest::ShowMenuL()
       
   137 /**
       
   138 	Shows menu
       
   139  */
       
   140 	{
       
   141 	iSmsTest.ResetMenu();
       
   142 	iSmsTest.AppendToMenuL(_L("Test SendAs"));
       
   143 	iSmsTest.AppendToMenuL(_L("Send the messages"));
       
   144 	iSmsTest.AppendToMenuL(_L("Run auto"));
       
   145 
       
   146 	TInt result = iSmsTest.DisplayMenu(_L("SMS Service Centre Test"));
       
   147 
       
   148 	if (result <= 0)
       
   149 		return;
       
   150 
       
   151 	switch (result)
       
   152 		{
       
   153 		case 1:
       
   154 			TestSendAsL();
       
   155 			break;
       
   156 		case 2:
       
   157 			TestSendingL();
       
   158 			break;
       
   159 		case 3:
       
   160 			iSmsTest.SetRunAuto(ETrue);
       
   161 			RunAutoL();
       
   162 			break;
       
   163 		default:
       
   164 			User::Leave(KErrArgument);
       
   165 			break;
       
   166 		}
       
   167 
       
   168 	ShowMenuL();
       
   169 	}
       
   170 
       
   171 void CSmsSendAsTest::RunL()
       
   172 /**
       
   173 	Handles completed async operations
       
   174  */
       
   175 	{
       
   176 	CActiveScheduler::Stop();
       
   177 
       
   178 	switch (iState)
       
   179 		{
       
   180 		case EStateSending:
       
   181 			{
       
   182 			DoRunSendingL();
       
   183 			break;
       
   184 			}
       
   185 		default:
       
   186 			{
       
   187 			break;
       
   188 			}
       
   189 		}
       
   190 	}
       
   191 
       
   192 void CSmsSendAsTest::DoRunSendingL()
       
   193 /**
       
   194 	Checks the status
       
   195  */	
       
   196 	{
       
   197 	if (iOperation)
       
   198 		iSmsTest.SetProgressL(*iOperation);
       
   199 
       
   200 	if (iStatus == KErrNone)
       
   201 		iStatus = iSmsTest.iProgress.iError;
       
   202 
       
   203 	iSmsTest.Printf(_L("Printing completed with error %d\n"), iStatus.Int());
       
   204 
       
   205 	iSmsTest.DisplaySendingStatesL(*iSelection);
       
   206 	iState = EStateWaiting;
       
   207 
       
   208 	delete iOperation;
       
   209 	iOperation = NULL;
       
   210 	}
       
   211 
       
   212 void CSmsSendAsTest::DoCancel()
       
   213 /**
       
   214 	Cancels the async operation
       
   215  */
       
   216 	{
       
   217 	if (iOperation)
       
   218 		{
       
   219 		iOperation->Cancel();
       
   220 		iSmsTest.Printf(_L("Operation Cancelled!\n"));
       
   221 		}
       
   222 	delete iOperation;
       
   223 	iOperation = NULL;
       
   224 	}
       
   225 
       
   226 void CSmsSendAsTest::TestSendAsL()
       
   227 /**
       
   228 	Tests SendAs  
       
   229  */
       
   230 	{
       
   231 	DeleteChildrenL(KMsvGlobalOutBoxIndexEntryId);
       
   232 
       
   233 	TTime now;
       
   234 	now.HomeTime();
       
   235 
       
   236 	CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection;
       
   237 	CleanupStack::PushL(sel);
       
   238 	iSelection->Reset();
       
   239 	
       
   240 	iSmsTest(iSmsTest.ReadScriptL(KSmsScript, KMsvGlobalOutBoxIndexEntryId, *sel, now));
       
   241 	iSmsTest(iSmsTest.ReadScriptL(KWapScript, KMsvGlobalOutBoxIndexEntryId, *sel, now));
       
   242 	iSmsTest(iSmsTest.ReadScriptL(KNbsScript, KMsvGlobalOutBoxIndexEntryId, *sel, now));
       
   243 	const TInt count = sel->Count();
       
   244 
       
   245 	iSmsTest(count);
       
   246 
       
   247 	CRichText* richOld = CRichText::NewL(iSmsTest.iParaFormat, iSmsTest.iCharFormat);
       
   248 	CleanupStack::PushL(richOld);
       
   249 
       
   250 	CRichText* richNew = CRichText::NewL(iSmsTest.iParaFormat, iSmsTest.iCharFormat);
       
   251 	CleanupStack::PushL(richNew);
       
   252 
       
   253 	CSmsHeader* headerOld = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *richOld);
       
   254 	CleanupStack::PushL(headerOld);
       
   255 
       
   256 	CSmsHeader* headerNew = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *richNew);
       
   257 	CleanupStack::PushL(headerNew);
       
   258 
       
   259 	for (TInt i = 0; i < count; i++) //order important
       
   260 		{
       
   261 		//Get the existing message
       
   262 		TMsvId id = sel->At(i);
       
   263 		iSmsTest.SetEntryL(id);
       
   264 
       
   265 		TUid bioType;
       
   266 		bioType.iUid = iSmsTest.Entry().iBioType;
       
   267 
       
   268 			iSmsTest.Printf(_L("%d/%d SendAs for BioType %d\n"), i + 1, count, bioType.iUid); 
       
   269 
       
   270 			CMsvStore* store = iSmsTest.ReadStoreL();
       
   271 			CleanupStack::PushL(store);
       
   272 			headerOld->RestoreL(*store);
       
   273 			store->RestoreBodyTextL(*richOld);
       
   274 			CleanupStack::PopAndDestroy(store);
       
   275 			
       
   276 			CSendAs* sendAs = CSendAs::NewL(*this);
       
   277 			CleanupStack::PushL(sendAs);
       
   278 			
       
   279 			sendAs->SetMtmL(KUidMsgTypeSMS);
       
   280 			iSmsTest(sendAs->AvailableServices().Count());
       
   281 			sendAs->SetService(0);
       
   282 			sendAs->CreateMessageL(KMsvGlobalOutBoxIndexEntryId);
       
   283 
       
   284 			for (TInt j = 0; j < headerOld->Recipients().Count(); j++)
       
   285 				{
       
   286 				CSmsNumber* number = headerOld->Recipients()[j];
       
   287 				TPtrC name(number->Name());
       
   288 				TPtrC addr(number->Address());
       
   289 				sendAs->AddRecipientL(addr, name);
       
   290 				}
       
   291 
       
   292 			sendAs->SetBodyL(*richOld);
       
   293 
       
   294 			if (bioType.iUid != 0)
       
   295 				sendAs->SetBioTypeL(bioType);
       
   296 
       
   297 			sendAs->SaveMessageL(ETrue);
       
   298 
       
   299 			//Get the new message
       
   300 			CSmsClientMtm* mtm = REINTERPRET_CAST(CSmsClientMtm*, &sendAs->ClientMtm());
       
   301 			CMsvEntry& entry = mtm->Entry();
       
   302 			store = entry.ReadStoreL();
       
   303 			CleanupStack::PushL(store);
       
   304 			headerNew->RestoreL(*store);
       
   305 			store->RestoreBodyTextL(*richNew);
       
   306 			CleanupStack::PopAndDestroy(store);
       
   307 
       
   308 			iSmsTest.Printf(_L("\tCreated %d\n"), entry.EntryId());
       
   309 			test(id != entry.EntryId());
       
   310 			CompareHeadersL(*headerOld, *headerNew);
       
   311 
       
   312 			CSmsSettings* msgSettings = CSmsSettings::NewL();
       
   313 			CleanupStack::PushL(msgSettings);
       
   314 
       
   315 			//Check the settings are the same (except charset)
       
   316 			headerNew->GetSmsSettingsL(*msgSettings);
       
   317 			CompareSettingsL(mtm->ServiceSettings(), *msgSettings);
       
   318 			CleanupStack::PopAndDestroy(msgSettings);
       
   319 
       
   320 			iSelection->AppendL(entry.EntryId());
       
   321 
       
   322 			CleanupStack::PopAndDestroy(sendAs);
       
   323 			}
       
   324 
       
   325 	CleanupStack::PopAndDestroy(5);
       
   326 	}
       
   327 
       
   328 void CSmsSendAsTest::CompareSettingsL(const CSmsMessageSettings& aS1, const CSmsMessageSettings& aS2)
       
   329 /**
       
   330 	Compares two sms message settings 
       
   331 
       
   332 	@param		aS1			A sms message setting
       
   333 	@param		aS2			A sms message setting	
       
   334  */
       
   335 	{
       
   336 	RTest& t = iSmsTest.Test();
       
   337 	t(aS1.ValidityPeriod() == aS2.ValidityPeriod());
       
   338 	t(aS1.ValidityPeriodFormat() == aS2.ValidityPeriodFormat());
       
   339 	t(aS1.RejectDuplicate() == aS2.RejectDuplicate());
       
   340 	t(aS1.DeliveryReport() == aS2.DeliveryReport());
       
   341 	t(aS1.ReplyPath() == aS2.ReplyPath());
       
   342 	t(aS1.MessageConversion() == aS2.MessageConversion());
       
   343 	}
       
   344 
       
   345 void CSmsSendAsTest::CompareHeadersL(const CSmsHeader& aH1, const CSmsHeader& aH2) const
       
   346 /**
       
   347 	Compares two sms headers
       
   348 
       
   349 	@param		aH1			A sms message header
       
   350 	@param		aH2			A sms message header	
       
   351  */
       
   352 	{
       
   353 	RTest& t = iSmsTest.Test();
       
   354 	t(aH1.Type() == aH2.Type());
       
   355 
       
   356 	t(aH1.Recipients().Count() == aH2.Recipients().Count());
       
   357 
       
   358 	for (TInt i = 0; i < aH1.Recipients().Count(); i++)
       
   359 		{
       
   360 		CSmsNumber* number1 = aH1.Recipients()[i];
       
   361 		CSmsNumber* number2 = aH2.Recipients()[i];
       
   362 
       
   363 		t(number1->Address().Compare(number2->Address()) == 0);
       
   364 		t(number1->Name().Compare(number2->Name()) == 0);
       
   365 		}
       
   366 
       
   367 	t(aH1.FromAddress().CompareF(aH2.FromAddress()) == 0);
       
   368 
       
   369 	TInt length = aH1.Message().Buffer().Length();
       
   370 	HBufC* hBuf1 = HBufC::NewLC(length);
       
   371 	TPtr buf1 = hBuf1->Des();
       
   372 	aH1.Message().Buffer().Extract(buf1, 0, length);
       
   373 	
       
   374 	length = aH2.Message().Buffer().Length();
       
   375 	HBufC* hBuf2 = HBufC::NewLC(length);
       
   376 	TPtr buf2 = hBuf2->Des();
       
   377 	aH2.Message().Buffer().Extract(buf2, 0, length);
       
   378 
       
   379 	t(buf1.Compare(buf2) == 0);
       
   380 
       
   381 	CleanupStack::PopAndDestroy(2); //hBuf1, hBuf2
       
   382 	}
       
   383 
       
   384 void CSmsSendAsTest::TestSendingL()
       
   385 /**
       
   386 	Tests sending
       
   387  */
       
   388 	{
       
   389 	iSmsTest.Test().Next(_L("Test Sending"));
       
   390 
       
   391 	if (!iSelection->Count())
       
   392 		User::Leave(KErrNotFound);
       
   393 
       
   394 	delete iOperation;
       
   395 	iOperation = NULL;
       
   396 	iState = EStateSending;
       
   397 
       
   398 	MsvEntry().SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   399 	iOperation = MsvEntry().CopyL(*iSelection, iSmsTest.iSmsServiceId, iStatus);
       
   400 	SetActive();
       
   401 	CActiveScheduler::Start();
       
   402 	}
       
   403 
       
   404 
       
   405 void CSmsSendAsTest::DeleteChildrenL(TMsvId aParent)
       
   406 /**
       
   407 	Deletes children
       
   408 
       
   409 	@param		aParent			A Message Id
       
   410  */
       
   411 	{
       
   412 	iSmsTest.SetEntryL(aParent);
       
   413 	CMsvEntrySelection* sel = iSmsTest.ChildrenWithMtmLC(KUidMsgTypeSMS);
       
   414 	TInt count = sel->Count();
       
   415 
       
   416 	while (count--)
       
   417 		{
       
   418 		iSmsTest.DeleteEntryL(sel->At(count));
       
   419 		}
       
   420 
       
   421 	CleanupStack::PopAndDestroy(sel);
       
   422 	}
       
   423 
       
   424 TBool CSmsSendAsTest::CapabilityOK(TUid /*aCapability*/, TBool /*aResponse*/)
       
   425 /**
       
   426 	This is required by MSendAsObserver
       
   427  */
       
   428 	{
       
   429 	return ETrue;
       
   430 	}
       
   431