sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Example Code sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "tmsgapp.h" sl@0: #include sl@0: #include sl@0: sl@0: // UPS client library is only included to allow the test application sl@0: // to reset its decision records. sl@0: #include sl@0: sl@0: _LIT(KTo, "+442071541000"); sl@0: _LIT_SECURE_ID(KMySecureId, 0x01000003); sl@0: sl@0: // CAlarm ******************************************************************** sl@0: inline CAlarm::CAlarm() :CTimer(-1) {CActiveScheduler::Add(this);} sl@0: inline void CAlarm::ConstructL() {CTimer::ConstructL();} sl@0: sl@0: void CAlarm::RunL() sl@0: { sl@0: iObserver->WakeupL(); sl@0: } sl@0: sl@0: void CAlarm::After(MAlarmObserver* aObserver, TTimeIntervalMicroSeconds32 aInterval) sl@0: { sl@0: iObserver = aObserver; sl@0: CTimer::After(aInterval); sl@0: } sl@0: sl@0: // CSendMessages ************************************************************* sl@0: CSendMessages::CSendMessages() : CActive(EPriorityStandard) sl@0: { sl@0: CActiveScheduler::Add(this); sl@0: } sl@0: sl@0: CSendMessages::~CSendMessages() sl@0: { sl@0: Deque(); sl@0: iMsgCon.Close(); sl@0: } sl@0: sl@0: CSendMessages* CSendMessages::NewLC() sl@0: { sl@0: CSendMessages* self = new(ELeave) CSendMessages(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: void CSendMessages::ConstructL() sl@0: { sl@0: iAlarm.ConstructL(); sl@0: User::LeaveIfError(iMsgCon.Connect()); sl@0: TRequestStatus* status = &iStatus; sl@0: *status = KRequestPending; sl@0: SetActive(); sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: TInt CSendMessages::RunError(TInt /* aError */) sl@0: { sl@0: CActiveScheduler::Stop(); // Non-recoverable error sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CSendMessages::DoCancel() sl@0: { sl@0: iMsgCon.CancelSendMsg(); sl@0: iAlarm.Cancel(); sl@0: } sl@0: sl@0: void CSendMessages::WakeupL() sl@0: /** sl@0: * Timeout the pending message. sl@0: */ sl@0: { sl@0: iMsgCon.CancelSendMsg(); sl@0: } sl@0: sl@0: void CSendMessages::RunL() sl@0: /** sl@0: * Send a series of test messages and cancellations that should trigger sl@0: * user prompts. sl@0: * All test cases timeout after KDefaultTimeout to enable automated tests sl@0: */ sl@0: { sl@0: TInt err = iStatus.Int(); sl@0: if (iStatus != KErrNone && iStatus != KErrCancel && iStatus != KErrPermissionDenied) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: iAlarm.Cancel(); sl@0: sl@0: RDebug::Printf("Test %d completed with error %d", iTestNum, 0); sl@0: sl@0: ++iTestNum; // move on to next test case sl@0: switch (iTestNum) sl@0: { sl@0: case 1: sl@0: { sl@0: // Send message and wait for user prompt to complete sl@0: _LIT(KBody, "Hello"); sl@0: iMsgCon.SendMsg(KTo, KBody, iStatus); sl@0: SetActive(); sl@0: iAlarm.After(this, KDefaultTimeout); sl@0: break; sl@0: } sl@0: case 2: sl@0: { sl@0: // Send a message and cancel straight away, should be no user prompt sl@0: _LIT(KBody, "Hello - Cancel immediate"); sl@0: iMsgCon.SendMsg(KTo, KBody, iStatus); sl@0: SetActive(); sl@0: iAlarm.After(this, 1); sl@0: break; sl@0: } sl@0: case 3: sl@0: { sl@0: // Send a message and attempt to cancel whilst the user prompt is being displayed. sl@0: _LIT(KBody, "Hello - Cancel 5 secs"); sl@0: iMsgCon.SendMsg(KTo, KBody, iStatus); sl@0: SetActive(); sl@0: iAlarm.After(this, 5 * 1000 * 1000); sl@0: break; sl@0: } sl@0: case 4: sl@0: { sl@0: // Force the message server to exit sl@0: iMsgCon.Close(); sl@0: _LIT(KInfo, "Waiting for messageserver to exit"); sl@0: User::InfoPrint(KInfo); sl@0: User::After(5 * 1000 * 1000); sl@0: User::LeaveIfError(iMsgCon.Connect()); // reconnect sl@0: sl@0: // Send a message to the new server instance sl@0: _LIT(KBody, "Hello again"); sl@0: iMsgCon.SendMsg(KTo, KBody, iStatus); sl@0: SetActive(); sl@0: iAlarm.After(this, KDefaultTimeout); sl@0: break; sl@0: } sl@0: default: sl@0: // All done sl@0: CActiveScheduler::Stop(); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void DeleteOldDecisionsL() sl@0: /** sl@0: * Delete all decision records for this test application sl@0: */ sl@0: { sl@0: UserPromptService::RUpsManagement m; sl@0: CleanupClosePushL(m); sl@0: User::LeaveIfError(m.Connect()); sl@0: UserPromptService::CDecisionFilter* f = UserPromptService::CDecisionFilter::NewLC(); sl@0: f->SetClientSid(KMySecureId, UserPromptService::EEqual); sl@0: m.RemoveDecisionsL(*f); sl@0: CleanupStack::PopAndDestroy(2, &m); // m, f sl@0: } sl@0: sl@0: void MainL() sl@0: { sl@0: // Create active scheduler, reset UPS and start tests sl@0: CActiveScheduler* s=new(ELeave) CActiveScheduler; sl@0: CleanupStack::PushL(s); sl@0: CActiveScheduler::Install(s); sl@0: DeleteOldDecisionsL(); // reset decision database sl@0: CSendMessages* test = CSendMessages::NewLC(); sl@0: s->Start(); sl@0: CleanupStack::PopAndDestroy(2, s); // s, test sl@0: sl@0: // Add log file for automated test environment sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: RFile log; sl@0: CleanupClosePushL(log); sl@0: User::LeaveIfError(log.Replace(fs, _L("c:\\tmsgapp.log"), EFileShareAny|EFileWrite)); sl@0: User::LeaveIfError(log.Write(_L8("\n\n0 tests failed out of 1\n"))); sl@0: CleanupStack::PopAndDestroy(2, &fs); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: CTrapCleanup* cleanup = CTrapCleanup::New(); sl@0: if(cleanup == NULL) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: TRAP_IGNORE(MainL()); sl@0: delete cleanup; sl@0: return KErrNone; sl@0: } sl@0: