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: //
sl@0: 
sl@0: #include "SCHTIME.H"
sl@0: 
sl@0: #include <e32base.h>
sl@0: #include <e32test.h>
sl@0: #include <f32file.h>
sl@0: #include <s32file.h>
sl@0: 
sl@0: #include <e32std.h>
sl@0: #include <tz.h>
sl@0: 
sl@0: #include "Thelpers.h"
sl@0: 
sl@0: 
sl@0: _LIT(KTestName,	"TTsTime Tests");
sl@0: RTest	TheTest(KTestName);
sl@0: RTz		TheTzServer;
sl@0:  
sl@0: static RFs			TheFsSession;
sl@0: _LIT(KFileName,"_:\\ttstime.dat");
sl@0: static TBuf<32> fileName;
sl@0: 
sl@0: 
sl@0: //
sl@0: //
sl@0: //Test macros and functions
sl@0: 
sl@0: static void Check(TInt aValue, TInt aLine)
sl@0: 	{
sl@0: 	if(!aValue)
sl@0: 		{
sl@0: 		(void)TheFsSession.Delete(fileName);
sl@0: 		TheTest(EFalse, aLine);
sl@0: 		}
sl@0: 	}
sl@0: static  void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0: 	{
sl@0: 	if(aValue != aExpected)
sl@0: 		{
sl@0: 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0: 		(void)TheFsSession.Delete(fileName);
sl@0: 		TheTest(EFalse, aLine);
sl@0: 		}
sl@0: 	}
sl@0: #define TEST(arg) ::Check((arg), __LINE__)
sl@0: #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0204
sl@0: @SYMTestCaseDesc 			Check that the default constructor works properly
sl@0: @SYMTestPriority 			low
sl@0: @SYMTestActions  			Creates an instance of TTsTime and check its data
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void TestsWithDefaultContructorL()
sl@0: 	{
sl@0: 	//	Tests with default constructor:
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0204 TTsTime() default constructor, TTsTime is UTC time by default "));
sl@0: 	TTsTime tsTime; //defalut constructor, TTsTime is UTC	
sl@0: 	
sl@0: 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0: 
sl@0: 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0: 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0: 	TTime time(date);	
sl@0: 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 	
sl@0: 	TEST(err == 0);
sl@0: 	
sl@0: 	
sl@0: 	// check that member data are set properly using GetUtcTime() and GetLocalTime()
sl@0: 	TTime utcTime = TTime(tsTime.GetUtcTime());
sl@0: 	TTime localTime = TTime(tsTime.GetLocalTime());
sl@0: 	TDateTime testDate(0, EJanuary, 0, 1, 0, 0, 0);
sl@0: 	TTime testTime(testDate);
sl@0: 	TEST (localTime == testTime);	
sl@0: 	
sl@0: 	// check the offset
sl@0: 	// because we set the current time zone to Europe/London and 
sl@0: 	// ProcessOffsetEvent has been called in GetUtcTime, and since TTsTime is UTC by defalu then 
sl@0: 	// TTsTime::iOffset remains the same
sl@0: 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset(); 
sl@0: 	TEST(tsTimeOffset == TTimeIntervalSeconds(0));
sl@0: 	}
sl@0: 	
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0205
sl@0: @SYMTestCaseDesc 			Checks that the second constructor works properly
sl@0: @SYMTestPriority 			low
sl@0: @SYMTestActions  			Creates an instance of TTsTime and check its data
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */	
sl@0: static void TestWithSecondConstructorAndUtcL()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0205 TTsTime - test for 2nd constructor "));
sl@0: 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0: 
sl@0: 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0: 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0: 	TTime time(date);
sl@0: 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
sl@0: 	TEST(err == 0);
sl@0: 	
sl@0: 	//	Tests with overloaded constructor 
sl@0: 	TheTest.Next(_L("Test TTsTime with overloaded operator and TTsTime is UTC"));
sl@0: 	TTsTime tsTime(time, ETrue); //Sets time to UTC time as boolean is ETrue
sl@0: 	TTime utcTime = tsTime.GetUtcTime(); 
sl@0: 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0: 	TBool isUtc = tsTime.IsUtc();
sl@0: 	TEST(utcTime == time);
sl@0: 	TEST(tsTimeOffset == TTimeIntervalSeconds(0)); //because tsTime is UTC so its offset is zero
sl@0: 	TEST(isUtc); 
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0206
sl@0: @SYMTestCaseDesc 			Check TTsTime::SetHomeTime works properly
sl@0: @SYMTestPriority 			low
sl@0: @SYMTestActions  			Creates an instance of TTsTime, sets its values to home time and verifies them 
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void TestSetHomeTimeL()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0206 TTsTime::SetHomeTime test "));	
sl@0: 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0: 
sl@0: 	// set the date and time of the device
sl@0: 	TheTest.Next(_L("Test TTsTime is Local Time"));
sl@0: 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0: 	TTime time(date);
sl@0: 	TInt err = SchSvrHelpers::SetUTCTimeL(time);
sl@0: 	TEST(err == 0);
sl@0: 	
sl@0: 	//set date and time of TTsTime to home time
sl@0: 	date =  TDateTime(2005, EMay, 15, 9, 55, 0, 0);
sl@0: 	time = date;
sl@0: 	TTsTime tsTime;
sl@0: 	tsTime.SetLocalTime(time);
sl@0: 	
sl@0: 	TTime localTime = tsTime.GetLocalTime(); 
sl@0: 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0: 	TBool isUtc = tsTime.IsUtc();
sl@0: 
sl@0: 	// check that the returned local time is the same as the one we set earlier
sl@0: 	TEST(localTime == time );
sl@0: 
sl@0: 	// check now that the stored utc time in TTsTime is correct
sl@0: 	TTime utcTime = tsTime.GetUtcTime();
sl@0: 	date =  TDateTime(2005, EMay, 15, 8, 55, 0, 0); 	
sl@0: 	time = TTime(date);
sl@0: 	TEST(utcTime == time );
sl@0: 
sl@0: 	// check offset - 3600 seconds (1 hour) because time of device is in BST
sl@0: 	TEST(tsTimeOffset == TTimeIntervalSeconds(3600));
sl@0: 	TEST(!isUtc); 	//This TTsTime is not UTC.
sl@0: 	}
sl@0: 	
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0207
sl@0: @SYMTestCaseDesc 			Checks ProcessOffsetEvent 
sl@0: @SYMTestPriority 			low
sl@0: @SYMTestActions  			Creates an instance of TTsTime, changes timezone, and checks that the TTsTime object is updated correctly.
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void TestProcessOffsetEventL()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0207 ProcessOffsetEvent Test "));
sl@0: 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0: 	
sl@0: 	//set the current home time to 8.55am, 15 May 2005
sl@0: 	//Daylight savings do apply on this date
sl@0: 	//so utc time should be 7.55
sl@0: 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0: 	TTime time(date);	
sl@0: 	TInt err = SchSvrHelpers::SetHomeTimeL(time);
sl@0: 	TEST(err == KErrNone);
sl@0: 		
sl@0: 	//Check the ProcessOffsetEvent () method and print iUtcTime, iOffset
sl@0: 	//create hometime based ttstime object
sl@0: 	//should have 9.55 in iUtcTime
sl@0: 	date = TDateTime(2005, EMay, 15, 10, 55, 0, 0);
sl@0: 	time = date;
sl@0: 	TTsTime tsTime(time, EFalse);
sl@0: 
sl@0: 	// remember current offset
sl@0: 	TTimeIntervalSeconds savedOffset = User::UTCOffset();
sl@0: 	
sl@0: 	// Increase offset by 1Hr by moving to Paris
sl@0: 	TheTest.Printf(_L("Move timezone to Paris, Europe. Check TTsTime updated accordingly"));
sl@0: 	CTzId* tzParisId = CTzId::NewL(2656);
sl@0: 	CleanupStack::PushL(tzParisId);
sl@0: 	TheTzServer.SetTimeZoneL(*tzParisId);
sl@0: 	
sl@0: 	//get new UTC offset
sl@0: 	TTimeIntervalSeconds newOffset = User::UTCOffset();
sl@0: 	
sl@0: 	// call ProcessOffsetEvent, should put 8:55 in iUtcTime 
sl@0: 	tsTime.ProcessOffsetEvent();
sl@0: 	
sl@0: 	// check the updated iUtcTime
sl@0: 	TTime utcTime = tsTime.GetUtcTime();
sl@0: 	TDateTime a = utcTime.DateTime();
sl@0: 	date = TDateTime(2005, EMay, 15, 8, 55, 0, 0);
sl@0: 	time = date;
sl@0: 	TEST(utcTime == time);
sl@0: 	
sl@0: 	// check the updated offset
sl@0: 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0: 	TEST(tsTimeOffset == newOffset);
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(tzParisId);
sl@0: 	
sl@0: 	//return Timezone to London, Europe for other tests
sl@0: 	CTzId* tzLondonId = CTzId::NewL(2592); 
sl@0: 	CleanupStack::PushL(tzLondonId);
sl@0: 	TheTzServer.SetTimeZoneL(*tzLondonId);
sl@0: 	CleanupStack::PopAndDestroy(tzLondonId);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0208
sl@0: @SYMTestCaseDesc 			Checks ExternalizeL and InternalizeL
sl@0: @SYMTestPriority 			low
sl@0: @SYMTestActions  			Creates an instance of TTsTime, Externalizes 
sl@0: 							it and internaziles and checks that teh data is the same
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */	
sl@0: static void TestExternalizeInternalizeL()
sl@0: 	{
sl@0: 	// time is UTC
sl@0: 	TTsTime time(TTime(5), ETrue);
sl@0: 
sl@0: 	//	Test externalise 
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0208 Test TTsTime::ExternalizeL "));
sl@0: 	RFile file;
sl@0: 
sl@0: 	TFileName testFileName(fileName);
sl@0: 	TEST2(file.Replace(TheFsSession,testFileName,EFileWrite|EFileRead), KErrNone);
sl@0: 	RFileBuf buf;
sl@0: 	CleanupClosePushL(buf);
sl@0: 	buf.Attach(file);
sl@0: 	RWriteStream stream(&buf);
sl@0: 	//Externalize the time
sl@0: 	stream << time;
sl@0: 	buf.SynchL();
sl@0: 	CleanupStack::PopAndDestroy(&buf);
sl@0: 	
sl@0: 	//	Test internalise 
sl@0: 	TheTest.Next(_L("Test TTsTime::InternalizeL"));
sl@0: 	TTsTime newTime;
sl@0: 	TInt err = file.Open(TheFsSession,testFileName,EFileRead);
sl@0: 	TEST2(err, KErrNone);
sl@0: 	RFileBuf buf2;
sl@0: 	CleanupClosePushL(buf2);
sl@0: 	buf2.Attach(file);
sl@0: 	RReadStream stream2(&buf2);
sl@0: 
sl@0: 	//Internalize the time
sl@0: 	stream2 >> newTime; //the externelized time is UTC time, same as the externalized time
sl@0: 	TTime utcTime = newTime.GetUtcTime();
sl@0: 	TTimeIntervalSeconds offset = newTime.GetOffset();
sl@0: 	TBool isUtc = newTime.IsUtc();
sl@0: 	TEST(utcTime == TTime(5));
sl@0: 	TEST(offset == TTimeIntervalSeconds(0));
sl@0: 	TEST(isUtc);
sl@0: 	CleanupStack::PopAndDestroy(&buf2);	
sl@0: 	}
sl@0: 	
sl@0: 	
sl@0: static void RunTestsL()
sl@0: 	{
sl@0: 	//All tests assume timezone is London, Europe.
sl@0: 	TheTzServer.Connect();
sl@0: 	CTzId* tzLondonId = CTzId::NewL(2592); 
sl@0: 	CleanupStack::PushL(tzLondonId);
sl@0: 	TheTzServer.SetTimeZoneL(*tzLondonId);
sl@0: 
sl@0: 
sl@0: 	TestsWithDefaultContructorL();
sl@0: 	TestWithSecondConstructorAndUtcL();
sl@0: 	TestSetHomeTimeL();
sl@0: 	TestProcessOffsetEventL();
sl@0: 	TestExternalizeInternalizeL();
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(tzLondonId);
sl@0: 	TheTzServer.Close();
sl@0: 	}
sl@0: 
sl@0: GLDEF_C TInt E32Main()
sl@0:  {
sl@0: 	CTrapCleanup* tc = CTrapCleanup::New();
sl@0: 	TEST(tc != NULL);
sl@0: 	
sl@0: 	__UHEAP_MARK;
sl@0: 	
sl@0: 	fileName.Copy(KFileName);
sl@0: 	fileName[0] = RFs::GetSystemDriveChar();
sl@0: 
sl@0: 	TInt err = TheFsSession.Connect();
sl@0: 	TEST2(err, KErrNone);
sl@0: 
sl@0: 	TheTest.Title();
sl@0: 	TheTest.Start(_L("Task Scheduler TTsTime unit tests"));
sl@0: 	TRAP(err, ::RunTestsL())
sl@0: 	TEST2(err, KErrNone);
sl@0: 	
sl@0: 	(void)TheFsSession.Delete(fileName);
sl@0: 	TheFsSession.Close();
sl@0: 	TheTest.End();
sl@0: 	TheTest.Close();
sl@0: 
sl@0: 	__UHEAP_MARKEND;
sl@0: 	
sl@0: 	delete tc;
sl@0: 	
sl@0: 	return(KErrNone);
sl@0: 	
sl@0: 	}
sl@0: