commonappservices/alarmservertest/TestMultipleAlarmsSuite/src/TestBaseStep.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2005-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 // Contains implementation of CTestBaseStep class, from which all test steps
       
    15 // are derived
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology 
       
    22 */
       
    23 
       
    24 // User Include
       
    25 #include "TestBaseStep.h"
       
    26 #include "ProgressCallBack.h"
       
    27 /**
       
    28 Constructor. Sets the test step name. Testserver reference passed to make use 
       
    29 of TEF's method of sharing data between test steps
       
    30 @internalTechnology
       
    31 @test
       
    32 */
       
    33 CTestBaseStep::CTestBaseStep(CTestMultipleAlarmsServer& aTestServer) : iTestServer(aTestServer)
       
    34 	{
       
    35 	//Call base class method to set human readable name for test step
       
    36 	SetTestStepName(KTestBaseStep);
       
    37 	}
       
    38 
       
    39 /**
       
    40 Base class virtual. Checks the alert server thread death status after 
       
    41 any test is complete
       
    42 @return		EPass or EFail indicating the result of the test step.
       
    43 @internalTechnology
       
    44 @test
       
    45 */	
       
    46 TVerdict CTestBaseStep::doTestStepPostambleL()
       
    47 	{
       
    48 	CheckAltSrvThreadDeathStatus();
       
    49 	return TestStepResult();
       
    50 	}
       
    51 	
       
    52 /**
       
    53 Returns a pointer to the Test Server
       
    54 @internalTechnology
       
    55 @test
       
    56 */	
       
    57 CTestMultipleAlarmsServer* CTestBaseStep::TestServer()
       
    58 	{
       
    59 	return &iTestServer;
       
    60 	}
       
    61 
       
    62 /**
       
    63 Reads strings from a particular section in the ini file
       
    64 @param aSection Ini section name
       
    65 @param ... A variable number of arguments, in the form <key>, <value&> where
       
    66 <key> is the ini key whose value is to be read and <value&> is the address of 
       
    67 the variable to put the value read from ini. The last parameter in the variable
       
    68 argument must be NULL, to avoid unexpected behaviour
       
    69 @return True if all values are read, false if even one fails
       
    70 @internalTechnology
       
    71 @test
       
    72 */
       
    73 TBool CTestBaseStep::ReadStringsFromConfig(TRefByValue<const TDesC> aSection, ...)
       
    74 	{
       
    75 	// Mark the start of variable args
       
    76 	VA_LIST listOfArgs;
       
    77 	VA_START(listOfArgs, aSection);
       
    78 	
       
    79 	const TDesC* iniKey = NULL;
       
    80 	TPtrC* iniValue = NULL;
       
    81 
       
    82 	// Loop until NULL is encountered
       
    83 	while((iniKey = VA_ARG(listOfArgs, const TDesC*)) != NULL)
       
    84 		{
       
    85 		iniValue = VA_ARG(listOfArgs, TPtrC*);
       
    86 		// Read data from INI
       
    87 		if(!GetStringFromConfig(aSection, *iniKey, *iniValue))
       
    88 			{
       
    89 			return EFalse;
       
    90 			}
       
    91 		}
       
    92 	// Mark end	
       
    93 	VA_END(listOfArgs);
       
    94 	return ETrue;
       
    95 	}
       
    96 
       
    97 /**
       
    98 Reads TInts from a particular section in the ini file
       
    99 @param aSection Ini section name
       
   100 @param ... A variable number of arguments, in the form <key>, <value&> where
       
   101 <key> is the ini key whose value is to be read and <value&> is the address of 
       
   102 the variable to put the value read from ini. The last parameter in the variable
       
   103 argument must be NULL, to avoid unexpected behaviour
       
   104 @return True if all values are read, false if even one fails
       
   105 @internalTechnology
       
   106 @test
       
   107 */
       
   108 TBool CTestBaseStep::ReadIntsFromConfig(TRefByValue<const TDesC> aSection, ...)
       
   109 	{
       
   110 	// Mark the start of variable args
       
   111 	VA_LIST listOfArgs;
       
   112 	VA_START(listOfArgs, aSection);
       
   113 	
       
   114 	const TDesC* iniKey = NULL;
       
   115 	TInt* iniValue = NULL;
       
   116 	
       
   117 	// Loop until NULL is encountered
       
   118 	while((iniKey = VA_ARG(listOfArgs, const TDesC*)) != NULL)
       
   119 		{
       
   120 		iniValue = VA_ARG(listOfArgs, TInt*);
       
   121 		// Read data from INI
       
   122 		if(!GetIntFromConfig(aSection, *iniKey, *iniValue))
       
   123 			{
       
   124 			return EFalse;
       
   125 			}
       
   126 		}
       
   127 	// Mark end	
       
   128 	VA_END(listOfArgs);
       
   129 	return ETrue;
       
   130 	}
       
   131 
       
   132 /**
       
   133 Prints Ini-Error message and fails the test
       
   134 @internalTechnology
       
   135 @test
       
   136 */
       
   137 void CTestBaseStep::IniProblem()
       
   138 	{
       
   139 	_LIT(KIniProblem, "Ini file entries not as expected...Hence failing the test");
       
   140 	ERR_PRINTF1(KIniProblem);
       
   141 	SetTestStepResult(EFail);
       
   142 	}
       
   143 	
       
   144 /**
       
   145 Prints a default message indicating the test step in which the error occured and
       
   146 sets the test step error flag if the error value passed is not KErrNone.
       
   147 @param aError The error value
       
   148 @internalTechnology
       
   149 @test
       
   150 */
       
   151 void CTestBaseStep::PrintIfError(const TInt& aError)
       
   152 	{
       
   153 	_LIT(KErrorOccured, "Error occured in %S: %D");
       
   154 	if(aError != KErrNone)
       
   155 		{
       
   156 		ERR_PRINTF3(KErrorOccured, &TestStepName(), aError);
       
   157 		
       
   158 		// Set the error flag of the test step.
       
   159 		// TEF will decide whether it is pass or fail.
       
   160 		SetTestStepError(aError); 
       
   161 		}
       
   162 	}
       
   163 	
       
   164 /**
       
   165 Prints the error message passed as parameter and sets the test step error 
       
   166 flag if the error value passed is not KErrNone
       
   167 @param aMessage The message to be printed using INFO_PRINTF. The message must
       
   168 have a format specifier %D for holding the error no.
       
   169 @param aError The error value
       
   170 @internalTechnology
       
   171 @test
       
   172 */
       
   173 void CTestBaseStep::PrintIfError(const TDesC& aMessage, const TInt& aError)
       
   174 	{
       
   175 	if(aError != KErrNone)
       
   176 		{
       
   177 		ERR_PRINTF2(aMessage, aError);
       
   178 		
       
   179 		// Set the error flag of the test step.
       
   180 		// TEF will decide whether it is pass or fail.
       
   181 		SetTestStepError(aError);
       
   182 		}
       
   183 	}
       
   184 
       
   185 /*
       
   186 Gets the rich data from the file whose name is passed as parameter
       
   187 @param	aFileName Name of the file 
       
   188 @param	aAlarmContentBuf Empty descriptor which will hold the data read from the file
       
   189 @internalTechnology
       
   190 @test
       
   191 */
       
   192 void CTestBaseStep::GetAlarmContentFromFileL(const TDesC& aFileName, HBufC8*& aAlarmContentBuf)
       
   193 	{
       
   194 	RFs fs;
       
   195 	RFile file;
       
   196 	CleanupClosePushL(fs);
       
   197 	User::LeaveIfError(fs.Connect());
       
   198 	CleanupClosePushL(file);
       
   199 	User::LeaveIfError(file.Open(fs, aFileName, EFileRead | EFileShareReadersOnly));
       
   200 	TInt size;
       
   201 	User::LeaveIfError(file.Size(size));
       
   202 	aAlarmContentBuf = HBufC8::NewLC(size);
       
   203 	TPtr8 pointer(aAlarmContentBuf->Des());
       
   204 	User::LeaveIfError(file.Read(pointer, size));
       
   205 	CleanupStack::Pop(aAlarmContentBuf);
       
   206 	CleanupStack::PopAndDestroy(2, &fs); // file, fs
       
   207 	}
       
   208 	
       
   209 	
       
   210 /*
       
   211 Converts a string to TBool
       
   212 @return	The boolean equivalent
       
   213 @param	aString Descriptor containing the string to be converted
       
   214 @internalTechnology
       
   215 @test
       
   216 */
       
   217 TBool CTestBaseStep::GetBool(const TPtrC& aString)
       
   218 	{
       
   219 	_LIT(KTrue, "true");
       
   220 	return (aString.Compare(KTrue) == KErrNone) ? ETrue : EFalse;
       
   221 	}
       
   222 	
       
   223 /*
       
   224 Converts a string to TReal32
       
   225 @return	The TReal32 equivalent
       
   226 @param	aString Descriptor containing the string to be converted
       
   227 @internalTechnology
       
   228 @test
       
   229 */
       
   230 TReal32 CTestBaseStep::GetTReal32(const TPtrC& aString)
       
   231 	{
       
   232 	TReal32 real32;
       
   233 	TLex lex(aString);
       
   234 	lex.Val(real32);
       
   235 	return real32;
       
   236 	}
       
   237 
       
   238 /**
       
   239 Checks the alert server thread death status and prints the exit-category
       
   240 and reason if the thread is dead
       
   241 @internalTechnology
       
   242 @test
       
   243 */	
       
   244 void CTestBaseStep::CheckAltSrvThreadDeathStatus()
       
   245 	{
       
   246 	// Prints in Bold
       
   247 	_LIT(KAltSrvThreadDead, "<B>Alert Server Thread has died. Category = %S; Reason = %D</B>");
       
   248 
       
   249 	TPtrC exitCategory(TestServer()->AltSrvThread().ExitCategory());
       
   250 	if(exitCategory.Length() != 0)
       
   251 		{// Thread has ended. Diagnose and print
       
   252 		INFO_PRINTF3(KAltSrvThreadDead, &exitCategory, TestServer()->AltSrvThread().ExitReason());
       
   253 		TestServer()->AltSrvThread().Close();
       
   254 		}
       
   255 	}
       
   256 
       
   257 /**
       
   258 Starts the active scheduler and waits for the request whose TRequestStatus has
       
   259 been passed as parameter is complete
       
   260 @param aStatus The TRequestStatus for whose completion the thread must wait.
       
   261 @internalTechnology
       
   262 @test
       
   263 */	
       
   264 void CTestBaseStep::StartActiveSchedAndWaitL(TRequestStatus& aStatus)
       
   265 	{
       
   266 	CActiveScheduler::Start();
       
   267 	User::WaitForRequest(aStatus);
       
   268 	User::LeaveIfError(aStatus.Int());
       
   269 	}
       
   270 	
       
   271 /**
       
   272 Checks the time difference, with the allowed max seconds passed as parameter
       
   273 @param aStartTime The start time of the operation
       
   274 @param aEndTime The end time of the operation
       
   275 @param aMaxSecondsLimit Maximum seconds allowed for the operation
       
   276 @internalTechnology
       
   277 @test
       
   278 */	
       
   279 void CTestBaseStep::CheckTimeDifference(const TTime& aStartTime, const TTime& aEndTime, const TReal32 aMaxSecondsLimit)
       
   280 	{
       
   281 	TTimeIntervalMicroSeconds time = aEndTime.MicroSecondsFrom(aStartTime);
       
   282 	TReal32 rate = (static_cast<TReal32>(time.Int64())) / (1000000);
       
   283 	INFO_PRINTF3(_L("Expected completion time = %f seconds. Operation completed in %f seconds"), aMaxSecondsLimit, rate);
       
   284 	if(rate > aMaxSecondsLimit)
       
   285 		{
       
   286 		ERR_PRINTF1(_L("Operation took more time than expected...Failing the test"));
       
   287 		SetTestStepResult(EFail);
       
   288 		}
       
   289 	}
       
   290 
       
   291 /**
       
   292 Creates and initialises the cal entry session
       
   293 @param aCreateNewFile EFalse by default. If ETrue, the old file is deleted if it
       
   294 already exists
       
   295 @return aCalSession Pointer to the cal entry session for which the view is to
       
   296 be created
       
   297 @internalTechnology
       
   298 @test
       
   299 */
       
   300 CCalSession* CTestBaseStep::CreateAndInitializeCalSessionL(const TBool& aCreateNewFile)
       
   301 	{
       
   302 	CCalSession* calSession = CCalSession::NewL();
       
   303 	CleanupStack::PushL(calSession);
       
   304 	
       
   305 	if(aCreateNewFile)
       
   306 		{// Create new Cal file
       
   307 		TRAPD(error, calSession->CreateCalFileL(KTestCalFile));
       
   308 		if(error == KErrAlreadyExists)
       
   309 			{// Already exists, delete and create new
       
   310 			calSession->DeleteCalFileL(KTestCalFile);
       
   311 			calSession->CreateCalFileL(KTestCalFile);	
       
   312 			}
       
   313 		else
       
   314 			{
       
   315 			User::LeaveIfError(error);	
       
   316 			}
       
   317 		}
       
   318 		
       
   319 	// Open file	
       
   320 	calSession->OpenL(KTestCalFile);
       
   321 	CleanupStack::Pop(calSession);
       
   322 	return calSession;
       
   323 	}
       
   324 
       
   325 /**
       
   326 Creates the cal entry view
       
   327 @param aCalSession Pointer to the cal entry session for which the view is to
       
   328 be created
       
   329 @return A pointer to the created cal entry view
       
   330 @internalTechnology
       
   331 @test
       
   332 */
       
   333 CCalEntryView* CTestBaseStep::CreateEntryViewL(CCalSession* aCalSession)
       
   334 	{
       
   335 	TRequestStatus status;
       
   336 	CProgressCallBack* progress = new (ELeave) CProgressCallBack(status);
       
   337 	CleanupStack::PushL(progress);
       
   338 	CCalEntryView* calEntryView = CCalEntryView::NewL(*aCalSession, *progress);
       
   339 	CleanupStack::PushL(calEntryView);
       
   340 	StartActiveSchedAndWaitL(status);
       
   341 	CleanupStack::Pop(calEntryView);
       
   342 	CleanupStack::PopAndDestroy(progress);
       
   343 	return calEntryView;
       
   344 	}