messagingappbase/smsmtm/test/src/T_SmsSendText.cpp
changeset 25 84d9eb65b26f
parent 23 238255e8b033
child 27 e4592d119491
child 37 518b245aa84c
child 79 2981cb3aa489
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
     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_SmsSendText.h"
       
    17 #include <smuthdr.h>
       
    18 #include <txtrich.h>
       
    19 
       
    20 #include <testconfigfileparser.h>
       
    21 #include <simtsy.h>
       
    22 #include <smsuaddr.h>
       
    23 #include <e32property.h>
       
    24 #include <msvtestutils.h>
       
    25 RTest test(_L("T_SmsSendText Testrig"));
       
    26 CTrapCleanup* theCleanup;
       
    27 
       
    28 CSmsSendTextTest::~CSmsSendTextTest()
       
    29 /**
       
    30 	destructor
       
    31  */
       
    32 	{
       
    33           //	delete iTextSession;
       
    34 	}
       
    35 
       
    36 void CSmsSendTextTest::TestSendingL()
       
    37 /**
       
    38 	Tests Sending
       
    39  */
       
    40 	{
       
    41 	DoTestSendingL(iScriptFile);
       
    42 	}
       
    43 
       
    44 void SetTestNumberL(TInt aTestNumber)
       
    45   	{
       
    46   	test.Printf(_L("Changing the Test Number to %d\n"), aTestNumber);
       
    47 	TInt testState;
       
    48 	TInt error = RProperty::Get(KUidSystemCategory, KUidSASimTsyTestNumberValue, testState);
       
    49 	if(KErrNone != error)
       
    50 		{
       
    51 		User::LeaveIfError(RProperty::Define(KUidSystemCategory, KUidSASimTsyTestNumberValue, RProperty::EInt));
       
    52 		}
       
    53 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KUidSASimTsyTestNumberValue, aTestNumber));
       
    54   	}
       
    55 
       
    56 void CSmsSendTextTest::RunAutoL()
       
    57 /**
       
    58 	Runs tests in sequence, checks iStatus
       
    59  */
       
    60 	{
       
    61 	SetTestNumberL(0);
       
    62 
       
    63 	iSmsTest.TestStart(++iNextTest);
       
    64 	TestSendNoRecipientsL();
       
    65 	User::LeaveIfError(iStatus.Int());
       
    66 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    67 
       
    68 	CSmsSendTestBase::RunAutoL();
       
    69 	}
       
    70 
       
    71 void CSmsSendTextTest::RunL()
       
    72 /**
       
    73 	Handles completed async operations
       
    74  */
       
    75 	{
       
    76 	if (iOperation)
       
    77 		iSmsTest.SetProgressL(*iOperation);
       
    78 
       
    79 	switch (iSendTextTestState)
       
    80 		{
       
    81 		case EStateSendNoRecipients:
       
    82 			DoRunSendNoRecipientsL();
       
    83 			break;
       
    84 		default:
       
    85 			CSmsSendTestBase::RunL();
       
    86 			break;
       
    87 		}
       
    88 	}
       
    89 
       
    90 void CSmsSendTextTest::TestSendNoRecipientsL()
       
    91 /**
       
    92 	Tests sending with no receipients.
       
    93 	The error -1 (KErrNotFound) is expected.
       
    94  */
       
    95 	{
       
    96 	iState = EStateOther;
       
    97 	iSendTextTestState = EStateSendNoRecipients;
       
    98 
       
    99 	iSmsTest.Test().Next(_L("Sending No Recipients"));
       
   100 
       
   101 	iSmsTest.DeleteSmsMessagesL(KMsvGlobalOutBoxIndexEntryId);
       
   102 
       
   103 	TTime now;
       
   104 	now.HomeTime();
       
   105 	now += (TTimeIntervalSeconds) 5;
       
   106 
       
   107 	iSelection->Reset();
       
   108 
       
   109 	TBool read = EFalse;
       
   110 	TRAPD(err, read = iSmsTest.ReadScriptL(iScriptFile, KMsvGlobalOutBoxIndexEntryId, *iSelection, now));
       
   111 
       
   112 	iSmsTest.Test()(!err && read);
       
   113 
       
   114 
       
   115 	//Remove Recipients
       
   116 	TInt count = iSelection->Count();
       
   117 
       
   118 	CSmsHeader* header = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *iSmsTest.iRichText);
       
   119 	CleanupStack::PushL(header);
       
   120 
       
   121 	while (count--)
       
   122 		{
       
   123 		iSmsTest.SetEntryL(iSelection->At(count));
       
   124 		CMsvStore* store = iSmsTest.EditStoreL();
       
   125 		CleanupStack::PushL(store);
       
   126 
       
   127 		header->RestoreL(*store);
       
   128 
       
   129 		header->Recipients().ResetAndDestroy();
       
   130 
       
   131 		header->StoreL(*store);
       
   132 		store->CommitL();
       
   133 
       
   134 		CleanupStack::PopAndDestroy(); //store
       
   135 		}
       
   136 
       
   137 	CleanupStack::PopAndDestroy(); //header
       
   138 
       
   139 	iSmsTest.SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   140 
       
   141 	delete iOperation;
       
   142 	iOperation = NULL;
       
   143 	iOperation = MsvEntry().CopyL(*iSelection, iSmsTest.iSmsServiceId, iStatus);
       
   144 		
       
   145 	SetActive();
       
   146 	CActiveScheduler::Start();
       
   147 	}
       
   148 
       
   149 void CSmsSendTextTest::DoRunSendNoRecipientsL()
       
   150 /**
       
   151 	Checks the status and error.
       
   152 	The error -1 (KErrNotFound) is expected
       
   153  */
       
   154 	{
       
   155 	CActiveScheduler::Stop();
       
   156 
       
   157 	TSmsProgress prog = iSmsTest.iProgress;
       
   158 
       
   159 	test(prog.iError == KErrNone || iStatus.Int() == KErrNone);
       
   160 	iStatus = KErrNone;
       
   161 
       
   162 	TInt error = KErrNone;
       
   163 
       
   164 	iSmsTest.DisplaySendingStatesL(*iSelection);
       
   165 	test(iSmsTest.SendingCompleteL(*iSelection, error));
       
   166 	test(error == KErrNotFound);
       
   167 
       
   168 	iState = CSmsSendTestBase::EStateWaiting;
       
   169 	iSendTextTestState = EStateWaiting;
       
   170 
       
   171 	delete iOperation;
       
   172 	iOperation = NULL;
       
   173 	}
       
   174 
       
   175 void CSmsSendTextTest::TestSchedulingL()
       
   176 /**
       
   177 	Tests schedule sending
       
   178  */
       
   179 	{
       
   180 	DoTestSchedulingL(iScriptFile);
       
   181 	}
       
   182 
       
   183 
       
   184 CSmsSendTextTest::CSmsSendTextTest(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aCurrentTest)
       
   185 : CSmsSendTestBase(aSmsTest, aScriptFile, aCurrentTest)
       
   186 /**
       
   187 	Constructor
       
   188  */
       
   189 	{
       
   190 	}
       
   191 
       
   192 LOCAL_C void doMainL()
       
   193 	{
       
   194 	// Copying the config file to the root directory
       
   195 
       
   196 	_LIT(KFileName,"config.txt");
       
   197 	_LIT(KFileName2,"\\msgtest\\sms\\sms.script");
       
   198 	
       
   199 	RFs fs;
       
   200 	User::LeaveIfError(fs.Connect());
       
   201 	CleanupClosePushL(fs);
       
   202 
       
   203 	fs.SetSessionPath(_L("c:\\"));
       
   204 	fs.Delete(KFileName);					//< Ignore Error
       
   205 
       
   206 	CFileMan* fileMan = CFileMan::NewL(fs);
       
   207 	CleanupStack::PushL(fileMan);
       
   208 
       
   209 	User::LeaveIfError(fileMan->Copy(KFileName2, KFileName));
       
   210 
       
   211 	CleanupStack::PopAndDestroy(fileMan);
       
   212 	CleanupStack::PopAndDestroy(&fs);
       
   213 
       
   214 	// Open the socket server
       
   215 
       
   216 	RSocketServ serv;
       
   217 	RSocket socket;
       
   218 
       
   219 	User::LeaveIfError(serv.Connect());
       
   220 	CleanupClosePushL(serv);
       
   221 	
       
   222 	TProtocolDesc protoinfo;
       
   223 	TProtocolName protocolname(KSmsDatagram);
       
   224 	User::LeaveIfError(serv.FindProtocol(protocolname,protoinfo));
       
   225 	User::LeaveIfError(socket.Open(serv,protoinfo.iAddrFamily,protoinfo.iSockType,protoinfo.iProtocol));
       
   226 
       
   227 	TSmsAddr smsaddr;
       
   228 	smsaddr.SetSmsAddrFamily(ESmsAddrSendOnly);
       
   229 	User::LeaveIfError(socket.Bind(smsaddr));
       
   230 
       
   231 	CleanupClosePushL(socket);
       
   232 
       
   233 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   234 	CleanupStack::PushL(scheduler);
       
   235 	CActiveScheduler::Install( scheduler );
       
   236 
       
   237 	CSmsTestUtils* smsTest = CSmsTestUtils::NewL(test);
       
   238 	CleanupStack::PushL(smsTest);
       
   239 	TInt nextTest = 0;
       
   240 
       
   241 	smsTest->NotifySaPhoneOnL();
       
   242 	smsTest->WaitForInitializeL();
       
   243 
       
   244 	CSmsSendTextTest* textTest = CSmsSendTextTest::NewL(*smsTest, KSmsScript, nextTest);
       
   245 	CleanupStack::PushL(textTest);
       
   246 
       
   247 	textTest->StartL();
       
   248 
       
   249 	CleanupStack::PopAndDestroy(3); //SmsTest, scheduler, textTest
       
   250 
       
   251 	CleanupStack::PopAndDestroy(&socket);
       
   252 	CleanupStack::PopAndDestroy(&serv);
       
   253 	}
       
   254 
       
   255 GLDEF_C TInt E32Main()
       
   256 	{	
       
   257 	__UHEAP_MARK;
       
   258 	test.Start(_L("Setup"));
       
   259 	theCleanup = CTrapCleanup::New();
       
   260 	TRAPD(ret,doMainL());		
       
   261 	test(ret==KErrNone);
       
   262 	delete theCleanup;	
       
   263 	test.Console()->SetPos(0, 13);
       
   264 	test.End();
       
   265 	test.Close();
       
   266 	__UHEAP_MARKEND;
       
   267 	return(KErrNone);
       
   268 	}
       
   269 
       
   270 CSmsSendTextTest* CSmsSendTextTest::NewL(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aCurrentTest)
       
   271 	{
       
   272 	CSmsSendTextTest* self = new (ELeave) CSmsSendTextTest(aSmsTest, aScriptFile, aCurrentTest);
       
   273 	CleanupStack::PushL(self);
       
   274 
       
   275 	self->ConstructL();
       
   276 
       
   277 	CleanupStack::Pop();
       
   278 	return self;
       
   279 	}
       
   280 
       
   281 /*void CSmsSendTextTest::TestSendWithDataCallL()
       
   282 	{
       
   283 	TestConnectingL();
       
   284 	DoTestSendingL(KSmsScript);
       
   285 
       
   286 	iTextSession->Disconnect();
       
   287 	WaitForEndOfDataCallL();
       
   288 	test.Printf(_L("  Waiting 10s\n"));
       
   289 	User::After(10000000);
       
   290 	delete iTextSession;
       
   291 	iTextSession = NULL;
       
   292 	}
       
   293 
       
   294 
       
   295  void CSmsSendTextTest::WaitForEndOfDataCallL()
       
   296 	{
       
   297 	// Connect to etel
       
   298 	RTelServer etel;
       
   299 	User::LeaveIfError(etel.Connect());
       
   300 	CleanupClosePushL(etel);
       
   301 
       
   302 	// Get tsy name
       
   303 	TName tsy;
       
   304 	FaxUtils::GetTsyNameL(tsy);
       
   305 
       
   306 	// Load the TSY
       
   307 	User::LeaveIfError(etel.LoadPhoneModule(tsy));
       
   308 
       
   309 	// Get phone for the TSY
       
   310 	RTelServer::TPhoneInfo info;
       
   311 	test(FaxUtils::GetPhoneInfoForTsyL(etel, tsy, info));
       
   312 
       
   313 	// Load the phone
       
   314 	RPhone phone;
       
   315 	User::LeaveIfError(phone.Open(etel, info.iName));
       
   316 	CleanupClosePushL(phone);
       
   317 
       
   318 	// Get data line info
       
   319 	RPhone::TLineInfo data;
       
   320 	test(GetDataLineInfoForPhoneL(phone, data));
       
   321 
       
   322 	// Open the phone line
       
   323 	RLine line;
       
   324 	User::LeaveIfError(line.Open(phone, data.iName));
       
   325 	CleanupClosePushL(line);
       
   326 
       
   327 	// Get line hook status
       
   328 	RCall::THookStatus hook;
       
   329 	User::LeaveIfError(line.GetHookStatus(hook));
       
   330 
       
   331 	// Wait until the line is on hook
       
   332 	if (hook == RCall::EHookStatusOff)
       
   333 		{
       
   334 		test.Printf(_L("  Waiting for data call to end\n"));
       
   335 		TRequestStatus status;
       
   336 		line.NotifyHookChange(status, hook);
       
   337 		User::WaitForRequest(status);
       
   338 		}
       
   339 
       
   340 	// Unload everything
       
   341 	CleanupStack::PopAndDestroy(2); // line, phone
       
   342 	User::LeaveIfError(etel.UnloadPhoneModule(tsy));
       
   343 	CleanupStack::PopAndDestroy(); // etel
       
   344 	}
       
   345 
       
   346 void CSmsSendTextTest::TestConnectingL()
       
   347 	{
       
   348 	// Create a waiter
       
   349 	CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
   350 
       
   351 	// Create text sever session
       
   352 	iTextSession = CImTextServerSession::NewL();
       
   353 
       
   354 	// Create a timer
       
   355 	CTestTimer* timer = CTestTimer::NewL();
       
   356 	CleanupStack::PushL(timer);
       
   357 
       
   358 	// Connect to the internet
       
   359 	const TInt maxCount = 5;
       
   360 	TInt count = maxCount;
       
   361 
       
   362 	CImIAPPreferences* pref = CImIAPPreferences::NewLC();
       
   363 
       
   364 	do
       
   365 		{
       
   366 		test.Printf(_L("  Connecting to the internet (attempt %d of %d, last error %d)\n"), maxCount - count + 1, maxCount, wait->iStatus.Int());
       
   367 		iTextSession->QueueConnectL(wait->iStatus,_L("pop3.demon.co.uk"), 110, *pref);
       
   368 		wait->Start();
       
   369 		CActiveScheduler::Start();
       
   370 		} while (wait->iStatus != KErrNone && count--);
       
   371 
       
   372 	CleanupStack::PopAndDestroy(pref);
       
   373 
       
   374 	// Receive stuff
       
   375 	while (wait->iStatus == KErrNone)
       
   376 		{
       
   377 		// Receive some text
       
   378 		test.Printf(_L("  Receiving data\n"));
       
   379 		iTextSession->QueueReceiveNextTextLine(wait->iStatus);
       
   380 		wait->Start();
       
   381 
       
   382 		// Start a timer - so we give up eventually - 10s
       
   383 		timer->After(10000000);
       
   384 		CActiveScheduler::Start();
       
   385 
       
   386 		// Did the timer complete?
       
   387 		if (!timer->IsActive())
       
   388 			{
       
   389 			// Cancel the outstanding request
       
   390 			iTextSession->Cancel();
       
   391 			CActiveScheduler::Start();
       
   392 			}
       
   393 		else
       
   394 			{
       
   395 			// Get the data
       
   396 			timer->Cancel();
       
   397 			TBuf8<1020> textLineNarrow;
       
   398 			TBuf<1024> textLineWide;
       
   399 			iTextSession->GetCurrentTextLine(textLineNarrow);
       
   400 			textLineWide.Copy(textLineNarrow);
       
   401 
       
   402 			// Make it look nice!
       
   403 			if (textLineWide.Length() > 0)
       
   404 				{
       
   405 				textLineWide.Insert(0, _L("  "));
       
   406 				textLineWide.Append(_L("\n"));
       
   407 				test.Printf(textLineWide);
       
   408 				}
       
   409 			}
       
   410 		}
       
   411 	
       
   412 	// Tidy up
       
   413 	test(wait->iStatus == KErrNone || wait->iStatus == KErrCancel);
       
   414 	CleanupStack::PopAndDestroy(2); // timer, wait
       
   415 	}
       
   416 
       
   417 
       
   418 
       
   419 TBool CSmsSendTextTest::GetDataLineInfoForPhoneL(RPhone& aPhone, RPhone::TLineInfo& aLineInfo)
       
   420 	{
       
   421 	// Get number of lines
       
   422 	TInt line;
       
   423 	User::LeaveIfError(aPhone.EnumerateLines(line));
       
   424 
       
   425 	// Iterate through all the lines
       
   426 	while(line--)
       
   427 		{
       
   428 		// Get line info
       
   429 		RPhone::TLineInfo lineInfo;
       
   430 		User::LeaveIfError(aPhone.GetLineInfo(line, lineInfo));
       
   431 
       
   432 		// Does it support fax?
       
   433 		if ((lineInfo.iLineCapsFlags & RLine::KCapsData) != 0)
       
   434 			{
       
   435 			aLineInfo = lineInfo;
       
   436 			break;
       
   437 			}
       
   438 		}
       
   439 
       
   440 	// Did we find a fax line on the phone?
       
   441 	return line >= 0;
       
   442 	}
       
   443 */