| 
sl@0
 | 
     1  | 
/*
  | 
| 
sl@0
 | 
     2  | 
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
  | 
| 
sl@0
 | 
     3  | 
* All rights reserved.
  | 
| 
sl@0
 | 
     4  | 
* This component and the accompanying materials are made available
  | 
| 
sl@0
 | 
     5  | 
* under the terms of the License "Eclipse Public License v1.0"
  | 
| 
sl@0
 | 
     6  | 
* which accompanies this distribution, and is available
  | 
| 
sl@0
 | 
     7  | 
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
  | 
| 
sl@0
 | 
     8  | 
*
  | 
| 
sl@0
 | 
     9  | 
* Initial Contributors:
  | 
| 
sl@0
 | 
    10  | 
* Nokia Corporation - initial contribution.
  | 
| 
sl@0
 | 
    11  | 
*
  | 
| 
sl@0
 | 
    12  | 
* Contributors:
  | 
| 
sl@0
 | 
    13  | 
*
  | 
| 
sl@0
 | 
    14  | 
* Description: 
  | 
| 
sl@0
 | 
    15  | 
* Example Code
  | 
| 
sl@0
 | 
    16  | 
*
  | 
| 
sl@0
 | 
    17  | 
*/
  | 
| 
sl@0
 | 
    18  | 
  | 
| 
sl@0
 | 
    19  | 
  | 
| 
sl@0
 | 
    20  | 
#include "tmsgapp.h"
  | 
| 
sl@0
 | 
    21  | 
#include <e32debug.h>
  | 
| 
sl@0
 | 
    22  | 
#include <f32file.h>
  | 
| 
sl@0
 | 
    23  | 
  | 
| 
sl@0
 | 
    24  | 
// UPS client library is only included to allow the test application
  | 
| 
sl@0
 | 
    25  | 
// to reset its decision records.
  | 
| 
sl@0
 | 
    26  | 
#include <ups/upsclient.h>	
  | 
| 
sl@0
 | 
    27  | 
  | 
| 
sl@0
 | 
    28  | 
_LIT(KTo, "+442071541000");
  | 
| 
sl@0
 | 
    29  | 
_LIT_SECURE_ID(KMySecureId, 0x01000003);
  | 
| 
sl@0
 | 
    30  | 
  | 
| 
sl@0
 | 
    31  | 
// CAlarm ********************************************************************
  | 
| 
sl@0
 | 
    32  | 
inline CAlarm::CAlarm() :CTimer(-1) {CActiveScheduler::Add(this);}
 | 
| 
sl@0
 | 
    33  | 
inline void CAlarm::ConstructL() {CTimer::ConstructL();}
 | 
| 
sl@0
 | 
    34  | 
  | 
| 
sl@0
 | 
    35  | 
void CAlarm::RunL() 
  | 
| 
sl@0
 | 
    36  | 
	{
 | 
| 
sl@0
 | 
    37  | 
	iObserver->WakeupL();
  | 
| 
sl@0
 | 
    38  | 
	}
  | 
| 
sl@0
 | 
    39  | 
  | 
| 
sl@0
 | 
    40  | 
void CAlarm::After(MAlarmObserver* aObserver, TTimeIntervalMicroSeconds32 aInterval)
  | 
| 
sl@0
 | 
    41  | 
	{
 | 
| 
sl@0
 | 
    42  | 
	iObserver = aObserver;
  | 
| 
sl@0
 | 
    43  | 
	CTimer::After(aInterval);
  | 
| 
sl@0
 | 
    44  | 
	}
  | 
| 
sl@0
 | 
    45  | 
  | 
| 
sl@0
 | 
    46  | 
// CSendMessages *************************************************************
  | 
| 
sl@0
 | 
    47  | 
CSendMessages::CSendMessages() : CActive(EPriorityStandard) 
  | 
| 
sl@0
 | 
    48  | 
	{
 | 
| 
sl@0
 | 
    49  | 
	CActiveScheduler::Add(this);
  | 
| 
sl@0
 | 
    50  | 
	}
  | 
| 
sl@0
 | 
    51  | 
  | 
| 
sl@0
 | 
    52  | 
CSendMessages::~CSendMessages() 
  | 
| 
sl@0
 | 
    53  | 
	{
 | 
| 
sl@0
 | 
    54  | 
	Deque();
  | 
| 
sl@0
 | 
    55  | 
	iMsgCon.Close();
  | 
| 
sl@0
 | 
    56  | 
	}
  | 
| 
sl@0
 | 
    57  | 
  | 
| 
sl@0
 | 
    58  | 
CSendMessages* CSendMessages::NewLC()
  | 
| 
sl@0
 | 
    59  | 
	{
 | 
| 
sl@0
 | 
    60  | 
	CSendMessages* self = new(ELeave) CSendMessages();
  | 
| 
sl@0
 | 
    61  | 
	CleanupStack::PushL(self);
  | 
| 
sl@0
 | 
    62  | 
	self->ConstructL();
  | 
| 
sl@0
 | 
    63  | 
	return self;
  | 
| 
sl@0
 | 
    64  | 
	}
  | 
| 
sl@0
 | 
    65  | 
  | 
| 
sl@0
 | 
    66  | 
void CSendMessages::ConstructL()
  | 
| 
sl@0
 | 
    67  | 
	{
 | 
| 
sl@0
 | 
    68  | 
	iAlarm.ConstructL();
  | 
| 
sl@0
 | 
    69  | 
	User::LeaveIfError(iMsgCon.Connect());
  | 
| 
sl@0
 | 
    70  | 
	TRequestStatus* status = &iStatus;
  | 
| 
sl@0
 | 
    71  | 
	*status = KRequestPending;
  | 
| 
sl@0
 | 
    72  | 
	SetActive();
  | 
| 
sl@0
 | 
    73  | 
	User::RequestComplete(status, KErrNone);
  | 
| 
sl@0
 | 
    74  | 
	}
  | 
| 
sl@0
 | 
    75  | 
  | 
| 
sl@0
 | 
    76  | 
TInt CSendMessages::RunError(TInt /* aError */)
  | 
| 
sl@0
 | 
    77  | 
	{
 | 
| 
sl@0
 | 
    78  | 
	CActiveScheduler::Stop(); // Non-recoverable error
  | 
| 
sl@0
 | 
    79  | 
	return KErrNone;
  | 
| 
sl@0
 | 
    80  | 
	}
  | 
| 
sl@0
 | 
    81  | 
  | 
| 
sl@0
 | 
    82  | 
void CSendMessages::DoCancel()
  | 
| 
sl@0
 | 
    83  | 
	{
 | 
| 
sl@0
 | 
    84  | 
	iMsgCon.CancelSendMsg();	
  | 
| 
sl@0
 | 
    85  | 
	iAlarm.Cancel();
  | 
| 
sl@0
 | 
    86  | 
	}
  | 
| 
sl@0
 | 
    87  | 
  | 
| 
sl@0
 | 
    88  | 
void CSendMessages::WakeupL()
  | 
| 
sl@0
 | 
    89  | 
/**
  | 
| 
sl@0
 | 
    90  | 
 * Timeout the pending message.
  | 
| 
sl@0
 | 
    91  | 
 */
  | 
| 
sl@0
 | 
    92  | 
	{
 | 
| 
sl@0
 | 
    93  | 
	iMsgCon.CancelSendMsg();
  | 
| 
sl@0
 | 
    94  | 
	}
  | 
| 
sl@0
 | 
    95  | 
  | 
| 
sl@0
 | 
    96  | 
void CSendMessages::RunL()
  | 
| 
sl@0
 | 
    97  | 
/**
  | 
| 
sl@0
 | 
    98  | 
 * Send a series of test messages and cancellations that should trigger
  | 
| 
sl@0
 | 
    99  | 
 * user prompts.
  | 
| 
sl@0
 | 
   100  | 
 * All test cases timeout after KDefaultTimeout to enable automated tests
  | 
| 
sl@0
 | 
   101  | 
 */
  | 
| 
sl@0
 | 
   102  | 
	{
 | 
| 
sl@0
 | 
   103  | 
	TInt err = iStatus.Int();
  | 
| 
sl@0
 | 
   104  | 
	if (iStatus != KErrNone && iStatus != KErrCancel && iStatus != KErrPermissionDenied) 
  | 
| 
sl@0
 | 
   105  | 
		{
 | 
| 
sl@0
 | 
   106  | 
		User::Leave(err);
  | 
| 
sl@0
 | 
   107  | 
		}
  | 
| 
sl@0
 | 
   108  | 
	iAlarm.Cancel();
  | 
| 
sl@0
 | 
   109  | 
	
  | 
| 
sl@0
 | 
   110  | 
	RDebug::Printf("Test %d completed with error %d", iTestNum, 0);
 | 
| 
sl@0
 | 
   111  | 
		
  | 
| 
sl@0
 | 
   112  | 
	++iTestNum;	// move on to next test case	
  | 
| 
sl@0
 | 
   113  | 
	switch (iTestNum)
  | 
| 
sl@0
 | 
   114  | 
		{
 | 
| 
sl@0
 | 
   115  | 
		case 1:
  | 
| 
sl@0
 | 
   116  | 
			{
 | 
| 
sl@0
 | 
   117  | 
			// Send message and wait for user prompt to complete			
  | 
| 
sl@0
 | 
   118  | 
			_LIT(KBody, "Hello");
  | 
| 
sl@0
 | 
   119  | 
			iMsgCon.SendMsg(KTo, KBody, iStatus);
  | 
| 
sl@0
 | 
   120  | 
			SetActive();
  | 
| 
sl@0
 | 
   121  | 
			iAlarm.After(this, KDefaultTimeout);
  | 
| 
sl@0
 | 
   122  | 
			break;
  | 
| 
sl@0
 | 
   123  | 
			}
  | 
| 
sl@0
 | 
   124  | 
		case 2:
  | 
| 
sl@0
 | 
   125  | 
			{
 | 
| 
sl@0
 | 
   126  | 
			// Send a message and cancel straight away, should be no user prompt
  | 
| 
sl@0
 | 
   127  | 
			_LIT(KBody, "Hello - Cancel immediate");
  | 
| 
sl@0
 | 
   128  | 
			iMsgCon.SendMsg(KTo, KBody, iStatus);
  | 
| 
sl@0
 | 
   129  | 
			SetActive();
  | 
| 
sl@0
 | 
   130  | 
			iAlarm.After(this, 1);
  | 
| 
sl@0
 | 
   131  | 
			break;
  | 
| 
sl@0
 | 
   132  | 
			}
  | 
| 
sl@0
 | 
   133  | 
		case 3:
  | 
| 
sl@0
 | 
   134  | 
			{
 | 
| 
sl@0
 | 
   135  | 
			// Send a message and attempt to cancel whilst the user prompt is being displayed.
  | 
| 
sl@0
 | 
   136  | 
			_LIT(KBody, "Hello - Cancel 5 secs");
  | 
| 
sl@0
 | 
   137  | 
			iMsgCon.SendMsg(KTo, KBody, iStatus);
  | 
| 
sl@0
 | 
   138  | 
			SetActive();
  | 
| 
sl@0
 | 
   139  | 
			iAlarm.After(this, 5 * 1000 * 1000);
  | 
| 
sl@0
 | 
   140  | 
			break;
  | 
| 
sl@0
 | 
   141  | 
			}
  | 
| 
sl@0
 | 
   142  | 
		case 4:
  | 
| 
sl@0
 | 
   143  | 
			{
 | 
| 
sl@0
 | 
   144  | 
			// Force the message server to exit
  | 
| 
sl@0
 | 
   145  | 
			iMsgCon.Close();
  | 
| 
sl@0
 | 
   146  | 
			_LIT(KInfo, "Waiting for messageserver to exit");
  | 
| 
sl@0
 | 
   147  | 
			User::InfoPrint(KInfo);
  | 
| 
sl@0
 | 
   148  | 
			User::After(5 * 1000 * 1000);	
  | 
| 
sl@0
 | 
   149  | 
			User::LeaveIfError(iMsgCon.Connect());	// reconnect
  | 
| 
sl@0
 | 
   150  | 
  | 
| 
sl@0
 | 
   151  | 
			// Send a message to the new server instance
  | 
| 
sl@0
 | 
   152  | 
			_LIT(KBody, "Hello again");
  | 
| 
sl@0
 | 
   153  | 
			iMsgCon.SendMsg(KTo, KBody, iStatus);
  | 
| 
sl@0
 | 
   154  | 
			SetActive();
  | 
| 
sl@0
 | 
   155  | 
			iAlarm.After(this, KDefaultTimeout);
  | 
| 
sl@0
 | 
   156  | 
			break;
  | 
| 
sl@0
 | 
   157  | 
			}
  | 
| 
sl@0
 | 
   158  | 
		default:
  | 
| 
sl@0
 | 
   159  | 
			// All done
  | 
| 
sl@0
 | 
   160  | 
			CActiveScheduler::Stop();
  | 
| 
sl@0
 | 
   161  | 
			break;
  | 
| 
sl@0
 | 
   162  | 
		}	
  | 
| 
sl@0
 | 
   163  | 
	}
  | 
| 
sl@0
 | 
   164  | 
  | 
| 
sl@0
 | 
   165  | 
void DeleteOldDecisionsL()
  | 
| 
sl@0
 | 
   166  | 
/**
  | 
| 
sl@0
 | 
   167  | 
 * Delete all decision records for this test application
  | 
| 
sl@0
 | 
   168  | 
 */
  | 
| 
sl@0
 | 
   169  | 
	{
 | 
| 
sl@0
 | 
   170  | 
	UserPromptService::RUpsManagement m;
  | 
| 
sl@0
 | 
   171  | 
	CleanupClosePushL(m);
  | 
| 
sl@0
 | 
   172  | 
	User::LeaveIfError(m.Connect());
  | 
| 
sl@0
 | 
   173  | 
	UserPromptService::CDecisionFilter* f = UserPromptService::CDecisionFilter::NewLC();
  | 
| 
sl@0
 | 
   174  | 
	f->SetClientSid(KMySecureId, UserPromptService::EEqual);
  | 
| 
sl@0
 | 
   175  | 
	m.RemoveDecisionsL(*f);
  | 
| 
sl@0
 | 
   176  | 
	CleanupStack::PopAndDestroy(2, &m); // m, f
  | 
| 
sl@0
 | 
   177  | 
	}
  | 
| 
sl@0
 | 
   178  | 
  | 
| 
sl@0
 | 
   179  | 
void MainL()
  | 
| 
sl@0
 | 
   180  | 
	{
 | 
| 
sl@0
 | 
   181  | 
	// Create active scheduler, reset UPS and start tests
  | 
| 
sl@0
 | 
   182  | 
	CActiveScheduler* s=new(ELeave) CActiveScheduler;	
  | 
| 
sl@0
 | 
   183  | 
	CleanupStack::PushL(s);
  | 
| 
sl@0
 | 
   184  | 
	CActiveScheduler::Install(s);
  | 
| 
sl@0
 | 
   185  | 
	DeleteOldDecisionsL();	// reset decision database
  | 
| 
sl@0
 | 
   186  | 
	CSendMessages* test = CSendMessages::NewLC();
  | 
| 
sl@0
 | 
   187  | 
	s->Start();
  | 
| 
sl@0
 | 
   188  | 
	CleanupStack::PopAndDestroy(2, s); // s, test
  | 
| 
sl@0
 | 
   189  | 
	
  | 
| 
sl@0
 | 
   190  | 
	// Add log file for automated test environment
  | 
| 
sl@0
 | 
   191  | 
	RFs fs;
  | 
| 
sl@0
 | 
   192  | 
	User::LeaveIfError(fs.Connect());
  | 
| 
sl@0
 | 
   193  | 
	CleanupClosePushL(fs);
  | 
| 
sl@0
 | 
   194  | 
	RFile log;
  | 
| 
sl@0
 | 
   195  | 
	CleanupClosePushL(log);
  | 
| 
sl@0
 | 
   196  | 
	User::LeaveIfError(log.Replace(fs, _L("c:\\tmsgapp.log"), EFileShareAny|EFileWrite));
 | 
| 
sl@0
 | 
   197  | 
	User::LeaveIfError(log.Write(_L8("\n\n0 tests failed out of 1\n")));
 | 
| 
sl@0
 | 
   198  | 
	CleanupStack::PopAndDestroy(2, &fs);
  | 
| 
sl@0
 | 
   199  | 
	}
  | 
| 
sl@0
 | 
   200  | 
  | 
| 
sl@0
 | 
   201  | 
GLDEF_C TInt E32Main()
  | 
| 
sl@0
 | 
   202  | 
	{
 | 
| 
sl@0
 | 
   203  | 
	CTrapCleanup* cleanup = CTrapCleanup::New();
  | 
| 
sl@0
 | 
   204  | 
	if(cleanup == NULL)
  | 
| 
sl@0
 | 
   205  | 
		{
 | 
| 
sl@0
 | 
   206  | 
		return KErrNoMemory;
  | 
| 
sl@0
 | 
   207  | 
		}
  | 
| 
sl@0
 | 
   208  | 
	TRAP_IGNORE(MainL());
  | 
| 
sl@0
 | 
   209  | 
	delete cleanup;
  | 
| 
sl@0
 | 
   210  | 
	return KErrNone;
  | 
| 
sl@0
 | 
   211  | 
	}
  | 
| 
sl@0
 | 
   212  | 
	
  |