os/ossrv/genericservices/taskscheduler/Test/TTsTimeUnitTests/TU_TSCH_ttstime.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 //
    15 
    16 #include "SCHTIME.H"
    17 
    18 #include <e32base.h>
    19 #include <e32test.h>
    20 #include <f32file.h>
    21 #include <s32file.h>
    22 
    23 #include <e32std.h>
    24 #include <tz.h>
    25 
    26 #include "Thelpers.h"
    27 
    28 
    29 _LIT(KTestName,	"TTsTime Tests");
    30 RTest	TheTest(KTestName);
    31 RTz		TheTzServer;
    32  
    33 static RFs			TheFsSession;
    34 _LIT(KFileName,"_:\\ttstime.dat");
    35 static TBuf<32> fileName;
    36 
    37 
    38 //
    39 //
    40 //Test macros and functions
    41 
    42 static void Check(TInt aValue, TInt aLine)
    43 	{
    44 	if(!aValue)
    45 		{
    46 		(void)TheFsSession.Delete(fileName);
    47 		TheTest(EFalse, aLine);
    48 		}
    49 	}
    50 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
    51 	{
    52 	if(aValue != aExpected)
    53 		{
    54 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    55 		(void)TheFsSession.Delete(fileName);
    56 		TheTest(EFalse, aLine);
    57 		}
    58 	}
    59 #define TEST(arg) ::Check((arg), __LINE__)
    60 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    61 
    62 /**
    63 @file
    64 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0204
    65 @SYMTestCaseDesc 			Check that the default constructor works properly
    66 @SYMTestPriority 			low
    67 @SYMTestActions  			Creates an instance of TTsTime and check its data
    68 @SYMTestExpectedResults		The test must not fail.
    69 @SYMPREQ					PREQ234
    70 */
    71 static void TestsWithDefaultContructorL()
    72 	{
    73 	//	Tests with default constructor:
    74 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0204 TTsTime() default constructor, TTsTime is UTC time by default "));
    75 	TTsTime tsTime; //defalut constructor, TTsTime is UTC	
    76 	
    77 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
    78 
    79 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
    80 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
    81 	TTime time(date);	
    82 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 	
    83 	TEST(err == 0);
    84 	
    85 	
    86 	// check that member data are set properly using GetUtcTime() and GetLocalTime()
    87 	TTime utcTime = TTime(tsTime.GetUtcTime());
    88 	TTime localTime = TTime(tsTime.GetLocalTime());
    89 	TDateTime testDate(0, EJanuary, 0, 1, 0, 0, 0);
    90 	TTime testTime(testDate);
    91 	TEST (localTime == testTime);	
    92 	
    93 	// check the offset
    94 	// because we set the current time zone to Europe/London and 
    95 	// ProcessOffsetEvent has been called in GetUtcTime, and since TTsTime is UTC by defalu then 
    96 	// TTsTime::iOffset remains the same
    97 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset(); 
    98 	TEST(tsTimeOffset == TTimeIntervalSeconds(0));
    99 	}
   100 	
   101 /**
   102 @file
   103 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0205
   104 @SYMTestCaseDesc 			Checks that the second constructor works properly
   105 @SYMTestPriority 			low
   106 @SYMTestActions  			Creates an instance of TTsTime and check its data
   107 @SYMTestExpectedResults		The test must not fail.
   108 @SYMPREQ					PREQ234
   109 */	
   110 static void TestWithSecondConstructorAndUtcL()
   111 	{
   112 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0205 TTsTime - test for 2nd constructor "));
   113 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
   114 
   115 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
   116 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
   117 	TTime time(date);
   118 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
   119 	TEST(err == 0);
   120 	
   121 	//	Tests with overloaded constructor 
   122 	TheTest.Next(_L("Test TTsTime with overloaded operator and TTsTime is UTC"));
   123 	TTsTime tsTime(time, ETrue); //Sets time to UTC time as boolean is ETrue
   124 	TTime utcTime = tsTime.GetUtcTime(); 
   125 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
   126 	TBool isUtc = tsTime.IsUtc();
   127 	TEST(utcTime == time);
   128 	TEST(tsTimeOffset == TTimeIntervalSeconds(0)); //because tsTime is UTC so its offset is zero
   129 	TEST(isUtc); 
   130 	}
   131 
   132 /**
   133 @file
   134 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0206
   135 @SYMTestCaseDesc 			Check TTsTime::SetHomeTime works properly
   136 @SYMTestPriority 			low
   137 @SYMTestActions  			Creates an instance of TTsTime, sets its values to home time and verifies them 
   138 @SYMTestExpectedResults		The test must not fail.
   139 @SYMPREQ					PREQ234
   140 */
   141 static void TestSetHomeTimeL()
   142 	{
   143 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0206 TTsTime::SetHomeTime test "));	
   144 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
   145 
   146 	// set the date and time of the device
   147 	TheTest.Next(_L("Test TTsTime is Local Time"));
   148 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
   149 	TTime time(date);
   150 	TInt err = SchSvrHelpers::SetUTCTimeL(time);
   151 	TEST(err == 0);
   152 	
   153 	//set date and time of TTsTime to home time
   154 	date =  TDateTime(2005, EMay, 15, 9, 55, 0, 0);
   155 	time = date;
   156 	TTsTime tsTime;
   157 	tsTime.SetLocalTime(time);
   158 	
   159 	TTime localTime = tsTime.GetLocalTime(); 
   160 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
   161 	TBool isUtc = tsTime.IsUtc();
   162 
   163 	// check that the returned local time is the same as the one we set earlier
   164 	TEST(localTime == time );
   165 
   166 	// check now that the stored utc time in TTsTime is correct
   167 	TTime utcTime = tsTime.GetUtcTime();
   168 	date =  TDateTime(2005, EMay, 15, 8, 55, 0, 0); 	
   169 	time = TTime(date);
   170 	TEST(utcTime == time );
   171 
   172 	// check offset - 3600 seconds (1 hour) because time of device is in BST
   173 	TEST(tsTimeOffset == TTimeIntervalSeconds(3600));
   174 	TEST(!isUtc); 	//This TTsTime is not UTC.
   175 	}
   176 	
   177 /**
   178 @file
   179 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0207
   180 @SYMTestCaseDesc 			Checks ProcessOffsetEvent 
   181 @SYMTestPriority 			low
   182 @SYMTestActions  			Creates an instance of TTsTime, changes timezone, and checks that the TTsTime object is updated correctly.
   183 @SYMTestExpectedResults		The test must not fail.
   184 @SYMPREQ					PREQ234
   185 */
   186 static void TestProcessOffsetEventL()
   187 	{
   188 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0207 ProcessOffsetEvent Test "));
   189 	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
   190 	
   191 	//set the current home time to 8.55am, 15 May 2005
   192 	//Daylight savings do apply on this date
   193 	//so utc time should be 7.55
   194 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
   195 	TTime time(date);	
   196 	TInt err = SchSvrHelpers::SetHomeTimeL(time);
   197 	TEST(err == KErrNone);
   198 		
   199 	//Check the ProcessOffsetEvent () method and print iUtcTime, iOffset
   200 	//create hometime based ttstime object
   201 	//should have 9.55 in iUtcTime
   202 	date = TDateTime(2005, EMay, 15, 10, 55, 0, 0);
   203 	time = date;
   204 	TTsTime tsTime(time, EFalse);
   205 
   206 	// remember current offset
   207 	TTimeIntervalSeconds savedOffset = User::UTCOffset();
   208 	
   209 	// Increase offset by 1Hr by moving to Paris
   210 	TheTest.Printf(_L("Move timezone to Paris, Europe. Check TTsTime updated accordingly"));
   211 	CTzId* tzParisId = CTzId::NewL(2656);
   212 	CleanupStack::PushL(tzParisId);
   213 	TheTzServer.SetTimeZoneL(*tzParisId);
   214 	
   215 	//get new UTC offset
   216 	TTimeIntervalSeconds newOffset = User::UTCOffset();
   217 	
   218 	// call ProcessOffsetEvent, should put 8:55 in iUtcTime 
   219 	tsTime.ProcessOffsetEvent();
   220 	
   221 	// check the updated iUtcTime
   222 	TTime utcTime = tsTime.GetUtcTime();
   223 	TDateTime a = utcTime.DateTime();
   224 	date = TDateTime(2005, EMay, 15, 8, 55, 0, 0);
   225 	time = date;
   226 	TEST(utcTime == time);
   227 	
   228 	// check the updated offset
   229 	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
   230 	TEST(tsTimeOffset == newOffset);
   231 	
   232 	CleanupStack::PopAndDestroy(tzParisId);
   233 	
   234 	//return Timezone to London, Europe for other tests
   235 	CTzId* tzLondonId = CTzId::NewL(2592); 
   236 	CleanupStack::PushL(tzLondonId);
   237 	TheTzServer.SetTimeZoneL(*tzLondonId);
   238 	CleanupStack::PopAndDestroy(tzLondonId);
   239 	}
   240 
   241 /**
   242 @file
   243 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0208
   244 @SYMTestCaseDesc 			Checks ExternalizeL and InternalizeL
   245 @SYMTestPriority 			low
   246 @SYMTestActions  			Creates an instance of TTsTime, Externalizes 
   247 							it and internaziles and checks that teh data is the same
   248 @SYMTestExpectedResults		The test must not fail.
   249 @SYMPREQ					PREQ234
   250 */	
   251 static void TestExternalizeInternalizeL()
   252 	{
   253 	// time is UTC
   254 	TTsTime time(TTime(5), ETrue);
   255 
   256 	//	Test externalise 
   257 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0208 Test TTsTime::ExternalizeL "));
   258 	RFile file;
   259 
   260 	TFileName testFileName(fileName);
   261 	TEST2(file.Replace(TheFsSession,testFileName,EFileWrite|EFileRead), KErrNone);
   262 	RFileBuf buf;
   263 	CleanupClosePushL(buf);
   264 	buf.Attach(file);
   265 	RWriteStream stream(&buf);
   266 	//Externalize the time
   267 	stream << time;
   268 	buf.SynchL();
   269 	CleanupStack::PopAndDestroy(&buf);
   270 	
   271 	//	Test internalise 
   272 	TheTest.Next(_L("Test TTsTime::InternalizeL"));
   273 	TTsTime newTime;
   274 	TInt err = file.Open(TheFsSession,testFileName,EFileRead);
   275 	TEST2(err, KErrNone);
   276 	RFileBuf buf2;
   277 	CleanupClosePushL(buf2);
   278 	buf2.Attach(file);
   279 	RReadStream stream2(&buf2);
   280 
   281 	//Internalize the time
   282 	stream2 >> newTime; //the externelized time is UTC time, same as the externalized time
   283 	TTime utcTime = newTime.GetUtcTime();
   284 	TTimeIntervalSeconds offset = newTime.GetOffset();
   285 	TBool isUtc = newTime.IsUtc();
   286 	TEST(utcTime == TTime(5));
   287 	TEST(offset == TTimeIntervalSeconds(0));
   288 	TEST(isUtc);
   289 	CleanupStack::PopAndDestroy(&buf2);	
   290 	}
   291 	
   292 	
   293 static void RunTestsL()
   294 	{
   295 	//All tests assume timezone is London, Europe.
   296 	TheTzServer.Connect();
   297 	CTzId* tzLondonId = CTzId::NewL(2592); 
   298 	CleanupStack::PushL(tzLondonId);
   299 	TheTzServer.SetTimeZoneL(*tzLondonId);
   300 
   301 
   302 	TestsWithDefaultContructorL();
   303 	TestWithSecondConstructorAndUtcL();
   304 	TestSetHomeTimeL();
   305 	TestProcessOffsetEventL();
   306 	TestExternalizeInternalizeL();
   307 	
   308 	CleanupStack::PopAndDestroy(tzLondonId);
   309 	TheTzServer.Close();
   310 	}
   311 
   312 GLDEF_C TInt E32Main()
   313  {
   314 	CTrapCleanup* tc = CTrapCleanup::New();
   315 	TEST(tc != NULL);
   316 	
   317 	__UHEAP_MARK;
   318 	
   319 	fileName.Copy(KFileName);
   320 	fileName[0] = RFs::GetSystemDriveChar();
   321 
   322 	TInt err = TheFsSession.Connect();
   323 	TEST2(err, KErrNone);
   324 
   325 	TheTest.Title();
   326 	TheTest.Start(_L("Task Scheduler TTsTime unit tests"));
   327 	TRAP(err, ::RunTestsL())
   328 	TEST2(err, KErrNone);
   329 	
   330 	(void)TheFsSession.Delete(fileName);
   331 	TheFsSession.Close();
   332 	TheTest.End();
   333 	TheTest.Close();
   334 
   335 	__UHEAP_MARKEND;
   336 	
   337 	delete tc;
   338 	
   339 	return(KErrNone);
   340 	
   341 	}
   342