sl@0: // Copyright (c) 2005-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 "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 CTestStep derived implementation
sl@0: // 
sl@0: //
sl@0: #include <schinfo.h>
sl@0: #include <schinfointernal.h>
sl@0: 
sl@0: #include "persist_utcStep.h"
sl@0: #include "Te_floating_scheduleSuiteDefs.h"
sl@0: 
sl@0: STaskSemaphore sem2;
sl@0: 
sl@0: Cpersist_utcStep::~Cpersist_utcStep()
sl@0: /**
sl@0:  * Destructor
sl@0:  */
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: Cpersist_utcStep::Cpersist_utcStep()
sl@0: /**
sl@0:  * Constructor
sl@0:  */
sl@0: 	{
sl@0: 	// **MUST** call SetTestStepName in the constructor as the controlling
sl@0: 	// framework uses the test step name immediately following construction to set
sl@0: 	// up the step's unique logging ID.
sl@0: 	SetTestStepName(Kpersist_utcStep);
sl@0: 	}
sl@0: 
sl@0: TVerdict Cpersist_utcStep::doTestStepPreambleL()
sl@0: /**
sl@0:  * @return - TVerdict code
sl@0:  * Override of base class virtual
sl@0:  */
sl@0: 	{
sl@0: 	SetTestStepResult(EFail);
sl@0: 	CTe_floating_scheduleSuiteStepBase::doTestStepPreambleL();
sl@0: 		
sl@0: 	// Delete old files.
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 
sl@0: 	sem2.CreateL();
sl@0: 
sl@0: 	TInt i = TheScheduler.Connect();
sl@0: 	TESTL (i==KErrNone);
sl@0: 		
sl@0: 	// Registering Client
sl@0: 	i = SchSvrHelpers::RegisterClientL(TheScheduler);
sl@0: 	TESTL (i==KErrNone);
sl@0: 	
sl@0: 	// create P&S variables for condition based tests
sl@0: 	User::LeaveIfError(RProperty::Define(KTestUid, KTestKey2, 0));
sl@0: 		
sl@0: 	SetTestStepResult(EPass);
sl@0: 	return TestStepResult();
sl@0: 	}
sl@0: 
sl@0: /*
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CIT-0278
sl@0: @SYMTestCaseDesc 			Persistant schedule - UTC
sl@0: @SYMTestPriority 			High
sl@0: @SYMTestActions  			For time and condition based test schedule task and check it fires
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: TVerdict Cpersist_utcStep::doTestStepL()
sl@0: /**
sl@0:  * @return - TVerdict code
sl@0:  * Override of base class pure virtual
sl@0:  * Our implementation only gets called if the base class doTestStepPreambleL() did
sl@0:  * not leave. That being the case, the current test result value will be EPass.
sl@0:  */
sl@0: 	{
sl@0: 	SetTestStepResult(EFail);
sl@0: 	
sl@0: 	_LIT(KTestName1, "Persistant UTC - Time-Based");
sl@0: 	_LIT(KTaskData1, "This is some really exciting task data (number 1)");
sl@0: 	_LIT(KTestName2, "Persistant UTC - Condition-Based");
sl@0: 	_LIT(KTaskData2, "This is some really exciting task data (number 2)");
sl@0: 	
sl@0: 	// Tests with timezone set to Europe, London
sl@0: 	RTz tz;
sl@0: 	tz.Connect();
sl@0: 	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
sl@0: 	CleanupStack::PushL(tzId);
sl@0: 	tz.SetTimeZoneL(*tzId);
sl@0: 	
sl@0: 	// Set the time to a known value, since this makes testing much easier (and more
sl@0: 	// repeatable).
sl@0: 	SchSvrHelpers::SetUTCTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
sl@0: 
sl@0: 	// Prepare schedules describing when we want the tasks to run (10:00 am & 10.01)
sl@0: 	
sl@0: 	// Creates a time based daily persistant schedule	
sl@0: 	TSchedulerItemRef ref1;
sl@0: 
sl@0: 	// This is the time when we want the time-based schedule to fire
sl@0: 	TDateTime datetime1(2000, EJanuary, 1, 10, 0, 0, 0);
sl@0: 	TTsTime startTimeForSchedule(datetime1, ETrue); // 10:00 am
sl@0: 	
sl@0: 	{
sl@0: 	CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
sl@0: 	CleanupStack::PushL(entryList);
sl@0: 	TScheduleEntryInfo2 entry1 (startTimeForSchedule, EDaily, 1, 30);
sl@0: 	entryList->AppendL(entry1);
sl@0: 
sl@0: 	TInt res = TheScheduler.CreatePersistentSchedule(ref1, *entryList);
sl@0: 	TESTL(res==KErrNone);
sl@0: 	CleanupStack::PopAndDestroy(); // entryList
sl@0: 	}
sl@0: 	
sl@0: 	// Disable the schedule whilst we set it up
sl@0: 	User::LeaveIfError(TheScheduler.DisableSchedule(ref1.iHandle));
sl@0: 	
sl@0: 	
sl@0: 	// Creates a condition based persistant schedule
sl@0: 	TSchedulerItemRef ref2;
sl@0: 	
sl@0: 	// This is the time when we want the condition-based schedule to fire
sl@0: 	TDateTime datetime2(2000, EJanuary, 1, 10, 1, 0, 0);
sl@0: 	TTsTime defaultRuntime(datetime2, ETrue); // 10:01 am
sl@0: 
sl@0: 	{
sl@0: 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(1);
sl@0: 	CleanupStack::PushL(conditionList);
sl@0: 	TTaskSchedulerCondition condition1;
sl@0: 	condition1.iCategory = KTestUid;
sl@0: 	condition1.iKey		= KTestKey1;
sl@0: 	condition1.iState	= 10;
sl@0: 	condition1.iType	= TTaskSchedulerCondition::EEquals;
sl@0: 	conditionList->AppendL(condition1);
sl@0: 
sl@0: 	TInt res = TheScheduler.CreatePersistentSchedule(ref2, *conditionList, defaultRuntime);
sl@0: 	TESTL(res==KErrNone);
sl@0: 	CleanupStack::PopAndDestroy(); // conditionList
sl@0: 	}	
sl@0: 	
sl@0: 	// Disable the schedule whilst we set it up
sl@0: 	User::LeaveIfError(TheScheduler.DisableSchedule(ref2.iHandle));
sl@0: 	
sl@0: 	// Kill the server to check if persisted schedules have been stored
sl@0: 	// Need to turn off JIT dubugging as we are panicking server and we 
sl@0: 	// want test to keep running.
sl@0: 	TBool jit = User::JustInTime();
sl@0: 	User::SetJustInTime(EFalse);
sl@0: 
sl@0: 	TheScheduler.__FaultServer();
sl@0: 	
sl@0: 	// Change UTC Offset whilst server is down	
sl@0: 	// Set UTC offset to +1Hr by moving to Europe, Paris
sl@0: 	tzId = CTzId::NewL(2656); //set the timezone to Europe/Paris
sl@0: 	tz.SetTimeZoneL(*tzId);
sl@0: 	
sl@0: 	User::After(1000000);
sl@0: 	
sl@0: 	// Turn on JIT again.
sl@0: 	User::SetJustInTime(jit);
sl@0: 	
sl@0: 	// Connect to the server again
sl@0: 	TInt res = TheScheduler.Connect();
sl@0: 	TESTL(res==KErrNone);
sl@0: 
sl@0: 	// Re-register
sl@0: 	TESTL(SchSvrHelpers::RegisterClientL(TheScheduler)==KErrNone);
sl@0: 
sl@0: 
sl@0: 	// Associate a task with the time-based schedule
sl@0: 	TTaskInfo taskInfo1;
sl@0: 	taskInfo1.iName = KTestName1;
sl@0: 	taskInfo1.iPriority = 2;
sl@0: 	taskInfo1.iRepeat = 0;
sl@0: 	// Create some data associated with this task	
sl@0: 	HBufC* taskData1 = KTaskData1().AllocLC();
sl@0: 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref1.iHandle));
sl@0: 	CleanupStack::PopAndDestroy();	// taskData1
sl@0: 		
sl@0: 	// Associate a task with the condition-based schedule
sl@0: 	TTaskInfo taskInfo2;
sl@0: 	taskInfo2.iName = KTestName2;
sl@0: 	taskInfo2.iPriority = 2;
sl@0: 	taskInfo2.iRepeat = 0;
sl@0: 	// Create some data associated with this task
sl@0: 	HBufC* taskData2 = KTaskData2().AllocLC();
sl@0: 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref2.iHandle));
sl@0: 	CleanupStack::PopAndDestroy(); // taskData2
sl@0: 
sl@0: 
sl@0: 	SchSvrHelpers::SetUTCTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 59, 50, 0))); // 9:59.50 am
sl@0: 
sl@0: 	User::LeaveIfError(TheScheduler.EnableSchedule(ref1.iHandle));
sl@0: 	User::LeaveIfError(TheScheduler.EnableSchedule(ref2.iHandle));
sl@0: 	
sl@0: 	// Now wait for the time-based schedule to fire
sl@0: 	TESTL(STaskSemaphore::WaitL(KDefaultTimeout) == KErrNone); 
sl@0: 
sl@0: 	TTime timeNow;
sl@0: 	timeNow.UniversalTime();
sl@0: 	TESTL(SchSvrHelpers::IsTimeTheSameNoSeconds(TTsTime(timeNow, ETrue), startTimeForSchedule));
sl@0: 
sl@0: 	// Now wait for the condition-based schedule to fire
sl@0: 	TESTL(STaskSemaphore::WaitL(KDefaultTimeout) == KErrNone); 
sl@0: 
sl@0: 	timeNow.UniversalTime();
sl@0: 	TESTL(SchSvrHelpers::IsTimeTheSameNoSeconds(TTsTime(timeNow, ETrue), defaultRuntime));
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(); // timezone ID
sl@0: 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
sl@0: 	SetTestStepResult(EPass);	
sl@0: 	return TestStepResult();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: TVerdict Cpersist_utcStep::doTestStepPostambleL()
sl@0: /**
sl@0:  * @return - TVerdict code
sl@0:  * Override of base class virtual
sl@0:  */
sl@0: 	{
sl@0: 	SetTestStepResult(EFail);
sl@0:   	CTe_floating_scheduleSuiteStepBase::doTestStepPostambleL();
sl@0:   	
sl@0:   	sem2.Close();
sl@0: 	
sl@0: 	// Delete all schedules
sl@0: 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
sl@0: 	SchSvrHelpers::Pause(2);
sl@0: 	
sl@0: 	// Delete old files
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 		
sl@0: 	TheScheduler.Close();
sl@0:   	
sl@0:   	SetTestStepResult(EPass);
sl@0: 	return TestStepResult();
sl@0: 	}