messagingappbase/smsmtm/test/src/T_SmsCancel.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 <e32test.h>
       
    17 #include <e32uid.h>
       
    18 #include <e32std.h>
       
    19 #include <smuthdr.h>
       
    20 #include <smscmds.h>
       
    21 #include "T_SmsCancel.h"
       
    22 
       
    23 #define KSmsScript _L("sms.script")
       
    24 
       
    25 CSmsCancelTest::~CSmsCancelTest()
       
    26 	{
       
    27 	if (iSmsTest.iMsvSession)
       
    28 		{
       
    29 		iSmsTest.iMsvSession->RemoveObserver(*this);
       
    30 		}
       
    31 		iSocket.Close();
       
    32 		iSocketServ.Close();
       
    33 	}
       
    34 
       
    35 CSmsCancelTest::CSmsCancelTest(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aCurrentTest)
       
    36 : CSmsTestBase(aSmsTest, aScriptFile, aCurrentTest), iState(EStateWaiting)
       
    37 	{
       
    38 	}
       
    39 
       
    40 void CSmsCancelTest::RunAutoL()
       
    41 	{
       
    42 	
       
    43 	iSmsTest.TestStart(++iNextTest, _L("Cancel Sending"));
       
    44 	TestCancelSendingL();
       
    45 	User::LeaveIfError(iStatus.Int());
       
    46 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    47 
       
    48 	iSmsTest.TestStart(++iNextTest, _L("Cancel Sending after Scheduling"));
       
    49 	TestCancelSendingAfterScheduleL();
       
    50 	User::LeaveIfError(iStatus.Int());
       
    51 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    52 
       
    53 	iSmsTest.TestStart(++iNextTest);
       
    54 	TestCancelReadScL();
       
    55 	User::LeaveIfError(iStatus.Int());
       
    56 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    57 
       
    58 	iSmsTest.TestStart(++iNextTest);
       
    59 	TestCancelWriteScL();
       
    60 	User::LeaveIfError(iStatus.Int());
       
    61 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    62 
       
    63 	iSmsTest.TestStart(++iNextTest);
       
    64 	TestCancelReadSimParamsL();
       
    65 	User::LeaveIfError(iStatus.Int());
       
    66 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    67 
       
    68 	iSmsTest.TestStart(++iNextTest);
       
    69 	TestCancelEnumerateL();
       
    70 	User::LeaveIfError(iStatus.Int());
       
    71 	iSmsTest.TestFinish(iNextTest, KErrNone);
       
    72 	}
       
    73 
       
    74 void CSmsCancelTest::ConstructL()
       
    75 	{
       
    76 	iSmsTest.SetLogToFile();
       
    77 
       
    78 	iSelection = new (ELeave) CMsvEntrySelection();
       
    79 
       
    80 	iSmsTest.iMsvSession->AddObserverL(*this);
       
    81 
       
    82 	CActiveScheduler::Add(this);
       
    83 	
       
    84 	User::LeaveIfError(iSocketServ.Connect());
       
    85 	TProtocolDesc protoinfo;
       
    86 	TProtocolName protocolname(KSmsDatagram);
       
    87 	User::LeaveIfError(iSocketServ.FindProtocol(protocolname,protoinfo));
       
    88 	User::LeaveIfError(iSocket.Open(iSocketServ,protoinfo.iAddrFamily,protoinfo.iSockType,protoinfo.iProtocol));
       
    89 	
       
    90 	TSmsAddr smsaddr;
       
    91 	smsaddr.SetSmsAddrFamily(ESmsAddrSendOnly);
       
    92 	User::LeaveIfError(iSocket.Bind(smsaddr));
       
    93 	}
       
    94 
       
    95 void CSmsCancelTest::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* /*aArg2*/, TAny* /*aArg3*/)
       
    96 	{
       
    97 	CMsvEntrySelection* entries = NULL;
       
    98 
       
    99 	switch (aEvent)
       
   100 		{
       
   101 		case EMsvEntriesCreated:
       
   102 		case EMsvEntriesChanged:
       
   103 		case EMsvEntriesMoved:
       
   104 			entries = STATIC_CAST(CMsvEntrySelection*, aArg1);
       
   105 			break;
       
   106 		default:
       
   107 			return;
       
   108 		}
       
   109 
       
   110 	if (!IsActive())
       
   111 		return;
       
   112 
       
   113 	switch (iState)
       
   114 		{
       
   115 		case EStateSending:
       
   116 			{
       
   117 			iSmsTest.DisplaySendingStatesL(*entries);
       
   118 			CancelIfSendingL(*entries);
       
   119 
       
   120 			TInt error = KErrNone;
       
   121 
       
   122 			if (iSmsTest.SendingCompleteL(*iSelection, error) && !iOperation && iStatus == KRequestPending)
       
   123 				{
       
   124 				TRequestStatus* status = &iStatus;
       
   125 				User::RequestComplete(status, error);
       
   126 				}
       
   127 			break;
       
   128 			}
       
   129 		default:
       
   130 			break;
       
   131 		}
       
   132 	}
       
   133 
       
   134 void CSmsCancelTest::CancelIfSendingL(const CMsvEntrySelection& aSelection)
       
   135 	{
       
   136 	const TInt count = aSelection.Count();
       
   137 
       
   138 	for (TInt i = 0; i < count; i++)
       
   139 		{
       
   140 		TRAPD(err, iSmsTest.SetEntryL(aSelection[i]));
       
   141 
       
   142 		if (!err)
       
   143 			{
       
   144 			TMsvEntry entry = iSmsTest.Entry();
       
   145 
       
   146 			if (entry.SendingState() == KMsvSendStateSending)
       
   147 				{
       
   148 				if (iOperation)
       
   149 					{
       
   150 					iOperation->Cancel();
       
   151 					return;
       
   152 					}
       
   153 				else
       
   154 					{
       
   155 					entry.SetSendingState(KMsvSendStateSuspended);
       
   156 					TRAP(err, iSmsTest.ChangeEntryL(entry)); //ignore error
       
   157 
       
   158 					if (err)
       
   159 						{
       
   160 						iSmsTest.Printf(_L("Error Cancelling Entry %d: On ChangeEntryL()\n"), aSelection[i]);
       
   161 						} //end if
       
   162 					} //end if
       
   163 				} //end if
       
   164 			}
       
   165 		else
       
   166 			{
       
   167 			iSmsTest.Printf(_L("Error Cancelling Entry %d: On SetEntryL()\n"), aSelection[i]);
       
   168 			}
       
   169 		}
       
   170 	}
       
   171 
       
   172 void CSmsCancelTest::ShowMenuL()
       
   173 	{
       
   174 	iSmsTest.ResetMenu();
       
   175 
       
   176 	iSmsTest.AppendToMenuL(_L("Test Cancel Sending"));
       
   177 	iSmsTest.AppendToMenuL(_L("Test Cancel Scheduling"));
       
   178 
       
   179 	TInt result = iSmsTest.DisplayMenu(_L("SMS Cancel Test"));
       
   180 
       
   181 	if (result <= 0)
       
   182 		return;
       
   183 
       
   184 	switch (result)
       
   185 		{
       
   186 		case 1:
       
   187 			TestCancelSendingL();
       
   188 			break;
       
   189 		case 2:
       
   190 			TestCancelSendingAfterScheduleL();
       
   191 			break;
       
   192 		default:
       
   193 			User::Leave(KErrArgument);
       
   194 			break;
       
   195 		}
       
   196 
       
   197 	ShowMenuL();
       
   198 	}
       
   199 
       
   200 void CSmsCancelTest::RunL()
       
   201 	{
       
   202 	if (iOperation)
       
   203 		iSmsTest.SetProgressL(*iOperation);
       
   204 
       
   205 	switch (iState)
       
   206 		{
       
   207 		case EStateScheduling:
       
   208 			DoRunSchedulingL();
       
   209 			break;
       
   210 		case EStateSending:
       
   211 			DoRunSendingL();
       
   212 			break;
       
   213 		default:
       
   214 			{
       
   215 			break;
       
   216 			}
       
   217 		}
       
   218 
       
   219 	delete iOperation;
       
   220 	iOperation = NULL;
       
   221 	}
       
   222 
       
   223 void CSmsCancelTest::DoCancel()
       
   224 	{
       
   225 	if (iOperation)
       
   226 		{
       
   227 		iOperation->Cancel();
       
   228 		iSmsTest.Printf(_L("Operation Cancelled!\n"));
       
   229 		}
       
   230 	else
       
   231 		{
       
   232 		iSmsTest.Printf(_L("No operation to cancel!\n"));
       
   233 		}
       
   234 
       
   235 	delete iOperation;
       
   236 	iOperation = NULL;
       
   237 	}
       
   238 
       
   239 void CSmsCancelTest::DoRunSchedulingL()
       
   240 	{
       
   241 	if (iStatus == KErrNone)
       
   242 		iStatus = iSmsTest.iProgress.iError;
       
   243 
       
   244 	iSmsTest.Printf(_L("Scheduling completed with error %d\n"), iStatus);
       
   245 
       
   246 	TBool sent = EFalse;
       
   247 
       
   248 	if (iStatus == KErrNone)
       
   249 		{
       
   250 		iSmsTest.DisplaySendingStatesL(*iSelection);
       
   251 		TInt error = KErrNone;
       
   252 		sent = iSmsTest.SendingCompleteL(*iSelection, error);
       
   253 		iStatus = error;
       
   254 		}
       
   255 	
       
   256 	if (!sent)
       
   257 		{
       
   258 		iStatus = KRequestPending;
       
   259 		SetActive();
       
   260 		iState = EStateSending;
       
   261 		}
       
   262 	else
       
   263 		{
       
   264 		CActiveScheduler::Stop();
       
   265 		iState = EStateWaiting;
       
   266 		iSmsTest.Printf(_L("Scheduling completed with error %d\n"), iStatus);
       
   267 
       
   268 		if (!iSmsTest.RunAuto())
       
   269 			{
       
   270 			iSmsTest.Test().Printf(_L("\nPress any key to continue...\n"));
       
   271 			iSmsTest.Test().Getch();
       
   272 			}
       
   273 		}
       
   274 	}
       
   275 
       
   276 void CSmsCancelTest::DoRunSendingL()
       
   277 	{
       
   278 	CActiveScheduler::Stop();
       
   279 
       
   280 	if (!iOperation) //scheduled
       
   281 		{
       
   282 		TTimeIntervalMicroSeconds32 wait = 5000000;
       
   283 		iSmsTest.Printf(_L("\nWaiting %d seconds for SMSS to complete...\n\n"), wait.Int() / 1000000);
       
   284 		User::After(wait);
       
   285 		}
       
   286 
       
   287 	if (iStatus == KErrNone)
       
   288 		iStatus = iSmsTest.iProgress.iError;
       
   289 
       
   290 	iState = EStateWaiting;
       
   291 	iSmsTest.Printf(_L("Sending completed with error %d\n"), iStatus);
       
   292 
       
   293 	if (iStatus == KErrCancel)
       
   294 		{
       
   295 		iSmsTest.Printf(_L("Error %d is expected and OK\n"), iStatus);
       
   296 		iStatus = KErrNone;
       
   297 		}
       
   298 
       
   299 	iSmsTest.Printf(_L("Final Sending States of Messages:\n\n"), iStatus);
       
   300 	iSmsTest.DisplaySendingStatesL(*iSelection);
       
   301 
       
   302 	if (!iSmsTest.RunAuto())
       
   303 		{
       
   304 		iSmsTest.Test().Printf(_L("\nPress any key to continue...\n"));
       
   305 		iSmsTest.Test().Getch();
       
   306 		}
       
   307 	}
       
   308 
       
   309 RTest test(_L("SchSms Test"));
       
   310 
       
   311 
       
   312 LOCAL_C void doMainL()
       
   313 	{
       
   314 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   315 	CleanupStack::PushL(scheduler);
       
   316 	CActiveScheduler::Install( scheduler );
       
   317 
       
   318 	CSmsTestUtils* smsTest = CSmsTestUtils::NewL(test);
       
   319 	CleanupStack::PushL(smsTest);
       
   320 	TInt nextTest = 0;
       
   321 	
       
   322 	smsTest->NotifySaPhoneOnL();
       
   323 
       
   324 	CSmsCancelTest* cancelTest = CSmsCancelTest::NewLC(*smsTest, KSmsScript, nextTest);
       
   325 
       
   326 	smsTest->WaitForInitializeL();
       
   327 
       
   328 	cancelTest->StartL();
       
   329 
       
   330 	CleanupStack::PopAndDestroy(3); //SmsTest, scheduler
       
   331 	}
       
   332 
       
   333 
       
   334 GLDEF_C TInt E32Main()
       
   335 	{
       
   336 	__UHEAP_MARK;
       
   337 	test.Start(_L("Setup"));
       
   338 	CTrapCleanup* theCleanup = CTrapCleanup::New();
       
   339 	TRAPD(ret,doMainL());		
       
   340 	test(ret==KErrNone);
       
   341 	delete theCleanup;	
       
   342 	test.Console()->SetPos(0, 13);
       
   343 	test.End();
       
   344 	test.Close();
       
   345 	__UHEAP_MARKEND;
       
   346 	return(KErrNone);
       
   347 	}
       
   348 
       
   349 CSmsCancelTest* CSmsCancelTest::NewLC(CSmsTestUtils& aSmsTest, const TDesC& aScriptFile, TInt& aCurrentTest)
       
   350 	{
       
   351 	CSmsCancelTest* self = new (ELeave) CSmsCancelTest(aSmsTest, aScriptFile, aCurrentTest);
       
   352 	CleanupStack::PushL(self);
       
   353 	self->ConstructL();
       
   354 	return self;
       
   355 	}
       
   356 
       
   357 void CSmsCancelTest::TestCancelReadScL()
       
   358 	{
       
   359 	}
       
   360 
       
   361 void CSmsCancelTest::TestCancelWriteScL()
       
   362 	{
       
   363 	}
       
   364 
       
   365 void CSmsCancelTest::TestCancelReadSimParamsL()
       
   366 	{
       
   367 	}
       
   368 
       
   369 void CSmsCancelTest::TestCancelEnumerateL()
       
   370 	{
       
   371 	}
       
   372 
       
   373 void CSmsCancelTest::TestCancelSendingL()
       
   374 	{
       
   375 	DoTestSendingL(iScriptFile);
       
   376 	iState = EStateSending;
       
   377 	CActiveScheduler::Start();
       
   378 	}
       
   379 
       
   380 void CSmsCancelTest::TestCancelSendingAfterScheduleL()
       
   381 	{
       
   382 	DoTestSchedulingL(iScriptFile);
       
   383 	iState = EStateScheduling;
       
   384 	CActiveScheduler::Start();
       
   385 	}