systemhealthmanagement/systemhealthmgr/test/tsysmon/src/tsysmon_stepbase.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code
       
    20 */
       
    21 
       
    22 #include <apacmdln.h>
       
    23 #include <apgcli.h>
       
    24 #include <sysmonclisess.h>
       
    25 #include <startupproperties.h>
       
    26 #include <ssm/ssmstartupproperties.h>
       
    27 #include "testprocgoodsession.h"
       
    28 #include "tsysmon_stepbase.h"
       
    29 
       
    30 /** 
       
    31 Waits for the process to rendevouz, then resumes it.
       
    32 */
       
    33 void CTestStepBase::ResumeL(RProcess& aProcess)
       
    34 	{
       
    35 	TRequestStatus status;
       
    36 	aProcess.Rendezvous(status);
       
    37 	if (status == KRequestPending)
       
    38 		{
       
    39 		aProcess.Resume();
       
    40 		}
       
    41 	else
       
    42 		{
       
    43 		aProcess.Kill(KErrGeneral);
       
    44 		TESTEL(EFalse, status.Int());
       
    45 		}
       
    46 	
       
    47 	User::WaitForRequest(status);
       
    48 	INFO_PRINTF1(_L("Process started"));
       
    49 	}
       
    50 
       
    51 /**
       
    52 Connects to sysmon and request monitoring of aProcess using aProp.
       
    53 */
       
    54 
       
    55 void CTestStepBase::MonitorL(const RProcess& aProcess, const CSsmStartupProperties& aProp)
       
    56 	{	
       
    57 	RSysMonSession sess;
       
    58 	sess.OpenL();
       
    59 	CleanupClosePushL(sess);
       
    60 	TRAPD(err, sess.MonitorL(aProp, aProcess));
       
    61 	TESTL(err == KErrNone);	
       
    62 	CleanupStack::PopAndDestroy(&sess);
       
    63 	INFO_PRINTF1(_L("Monitoring initiated"));
       
    64 	}		
       
    65 
       
    66 /**
       
    67 Connects to sysmon and request monitoring of aProcess using aProp.
       
    68 */
       
    69 void CTestStepBase::MonitorL(const RProcess& aProcess, const CStartupProperties& aProp)
       
    70 	{	
       
    71 	RSysMonSession sess;
       
    72 	sess.OpenL();
       
    73 	CleanupClosePushL(sess);
       
    74 	TRAPD(err, sess.MonitorL(aProp, aProcess));
       
    75 	TESTL(err == KErrNone);	
       
    76 	CleanupStack::PopAndDestroy(&sess);
       
    77 	INFO_PRINTF1(_L("Monitoring initiated"));
       
    78 	}		
       
    79 
       
    80 /**
       
    81 Creates and resumes aProcess with file=KTestProcGood args=KLaunchServerCommandLineOption, 
       
    82 then sets up monitoring with aStartMethod and 1 retry.
       
    83 */
       
    84 void CTestStepBase::StartAndMonitorUsingStartupPropL(RProcess& aProcess, TStartMethod aStartMethod)
       
    85 	{
       
    86 	//Start a process that launch a CServer2
       
    87 	INFO_PRINTF1(_L("Going start a process"));
       
    88 	User::LeaveIfError(aProcess.Create(KTestProcGood, KLaunchServerCommandLineOption));
       
    89 	ResumeL(aProcess);
       
    90 	
       
    91 	//Setup monitoring	
       
    92 	INFO_PRINTF1(_L("Going to request process monitoring"));
       
    93 	CStartupProperties* prop = CStartupProperties::NewLC(KTestProcGood, KLaunchServerCommandLineOption);
       
    94 	prop->SetMonitored(ETrue);
       
    95 	prop->SetStartupType(EStartProcess);
       
    96 	prop->SetStartMethod(aStartMethod);
       
    97 	prop->SetNoOfRetries(1);
       
    98 	MonitorL(aProcess, *prop);	
       
    99 	CleanupStack::PopAndDestroy(prop);
       
   100 
       
   101 	AssertServerIsRunning();
       
   102 	}
       
   103 
       
   104 /**
       
   105 Creates and resumes aProcess with file=KTestProcGood args=KLaunchServerCommandLineOption, 
       
   106 then sets up monitoring with aStartMethod and 1 retry.
       
   107 */
       
   108 void CTestStepBase::StartAndMonitorUsingSsmStartupPropL(RProcess& aProcess, TStartMethod aStartMethod)
       
   109 	{
       
   110 	//Start a process that launch a CServer2
       
   111 	INFO_PRINTF1(_L("Going start a process"));
       
   112 	User::LeaveIfError(aProcess.Create(KTestProcGood, KLaunchServerCommandLineOption));
       
   113 	ResumeL(aProcess);
       
   114 	
       
   115 	//Setup monitoring	
       
   116 	INFO_PRINTF1(_L("Going to request process monitoring"));
       
   117 	CSsmStartupProperties* prop = CSsmStartupProperties::NewLC(KTestProcGood, KLaunchServerCommandLineOption);
       
   118 	prop->SetExecutionBehaviour(static_cast <TSsmExecutionBehaviour> (aStartMethod));
       
   119 	TSsmMonitorInfo monitorInfo;
       
   120 	monitorInfo.iRestartPolicy = ESsmIgnoreOnFailure;
       
   121 	monitorInfo.iRestartMode = 0;
       
   122 	monitorInfo.iTimeout = 0;
       
   123 	monitorInfo.iRetries = 1;
       
   124 	prop->SetMonitorInfoL(monitorInfo);
       
   125 	MonitorL(aProcess, *prop);
       
   126 	CleanupStack::PopAndDestroy(prop);
       
   127 	
       
   128 	AssertServerIsRunning();
       
   129 	}
       
   130 
       
   131 void CTestStepBase::AssertServerIsRunning()
       
   132 	{
       
   133 	//Assert that the server is running
       
   134 	RTestProcGoodSession server;
       
   135 	TInt err = server.Connect();
       
   136 	TEST(KErrNone == err);
       
   137 	if(KErrNone == err)
       
   138 		{
       
   139 		INFO_PRINTF1(_L("Asserted that server is running"));		
       
   140 		}
       
   141 	server.Close();
       
   142 	}
       
   143 
       
   144 /**
       
   145 Launch native application aAppName using an unconnected RApaLsSession
       
   146 */
       
   147 void CTestStepBase::StartViewlessBgApplicationL(const TDesC& aAppName, TThreadId& aTreadId, TRequestStatus& aRequestStatus)
       
   148 	{
       
   149 	RApaLsSession apa;
       
   150 	TThreadId threadId;
       
   151 	CApaCommandLine* const cmdLine = CApaCommandLine::NewLC();
       
   152 	cmdLine->SetExecutableNameL(aAppName);
       
   153 	cmdLine->SetCommandL(EApaCommandBackgroundAndWithoutViews);
       
   154 	User::LeaveIfError(apa.StartApp(*cmdLine, aTreadId, &aRequestStatus));
       
   155 	CleanupStack::PopAndDestroy(cmdLine);
       
   156 	}
       
   157 
       
   158 /**
       
   159 Asserts that at least one process starting with the name aProcessName is running.
       
   160 */
       
   161 TBool CTestStepBase::Exists(const TDesC& aProcessName)
       
   162 	{	
       
   163 	TPtrC ptr(aProcessName);
       
   164 	ptr.Set( ptr.Ptr(), ptr.Find(_L(".")) );
       
   165 	TFullName procSerchTerm( ptr );
       
   166 	procSerchTerm += _L("*");
       
   167 
       
   168 	TFindProcess find(procSerchTerm);
       
   169 	TFullName name;
       
   170 	TBool found = EFalse;
       
   171 	while(find.Next(name)==KErrNone)
       
   172 		{
       
   173 		RProcess process;
       
   174 		const TInt err = process.Open(find);
       
   175 		if (err == KErrNone)
       
   176 			{
       
   177 			if (process.ExitType() == EExitPending)
       
   178 				{
       
   179 				found = ETrue;
       
   180 				process.Close(); 
       
   181 				break;
       
   182 				}
       
   183 			process.Close(); 
       
   184 			}
       
   185 		}
       
   186 	return found;
       
   187 	}