genericservices/taskscheduler/Test/ScheduledTaskTest/TU_TSCH_ScheduledTaskTest.cpp
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 <e32test.h>
       
    17 #include <f32file.h>
       
    18 #include <tz.h>
       
    19 #include <schinfo.h>
       
    20 #include <schinfointernal.h>
       
    21 #include <schtask.h>
       
    22 #include "Thelpers.h"
       
    23 
       
    24 _LIT(KHBufTest, "This is a CScheduledTask test");
       
    25 _LIT(KTestName,	"Scheduled Task");
       
    26 RTest	TheTest(KTestName);
       
    27 
       
    28 // persistent file for externalize and internalize test
       
    29 _LIT(KFileName,"_:\\CSheduledTask.dat");
       
    30 static TBuf<32> fileName;
       
    31 
       
    32 LOCAL_D RFs TheFsSession;
       
    33 
       
    34 //Test macroses and functions
       
    35 static void Check(TInt aValue, TInt aLine)
       
    36 	{
       
    37 	if(!aValue)
       
    38 		{
       
    39 		(void)TheFsSession.Delete(fileName);
       
    40 		TheTest(EFalse, aLine);
       
    41 		}
       
    42 	}
       
    43 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    44 	{
       
    45 	if(aValue != aExpected)
       
    46 		{
       
    47 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    48 		(void)TheFsSession.Delete(fileName);
       
    49 		TheTest(EFalse, aLine);
       
    50 		}
       
    51 	}
       
    52 #define TEST(arg) ::Check((arg), __LINE__)
       
    53 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    54 
       
    55 
       
    56 
       
    57 /**
       
    58 @file
       
    59 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0202
       
    60 @SYMTestCaseDesc 			Check that externalize executes correctly for tasks with local based times
       
    61 @SYMTestPriority 			low
       
    62 @SYMTestActions  			Create a local time based instance of CSheduledTask and externalize
       
    63 @SYMTestExpectedResults		The test must not fail.
       
    64 @SYMPREQ					PREQ234
       
    65 */
       
    66 LOCAL_D void doLocalTimeExternalizeTestL()
       
    67 	{
       
    68 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0202 CScheduledTask Externalize Test (local time) "));
       
    69 
       
    70 	TheTest.Printf(_L("Tests with timezone set to Europe, London"));	
       
    71 	RTz tz;
       
    72 	tz.Connect();
       
    73 	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
       
    74 	CleanupStack::PushL(tzId);
       
    75 	tz.SetTimeZoneL(*tzId);
       
    76 	
       
    77 	TheTest.Printf(_L("Tests with DST on"));	
       
    78 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
       
    79 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
       
    80 	TTime time(date);
       
    81 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
       
    82 	TEST(err == 0);
       
    83 	User::After(KOneSecond * 3);	
       
    84 	// set data for CScheduledTask construction
       
    85 	TTaskInfo taskInfo;
       
    86 	
       
    87 	HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
       
    88 	TPtr pData(taskdata->Des());
       
    89 	pData.Append(KHBufTest);
       
    90 
       
    91 	TSecurityInfo securityInfo;
       
    92 
       
    93 	CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo, 
       
    94 										taskdata, 
       
    95 										ETimeSchedule, 
       
    96 										securityInfo);
       
    97 	CleanupStack::Pop(taskdata); //taskdata now owned by newTask
       
    98 	CleanupStack::PushL(extTask);
       
    99 	
       
   100 	// set due time, 63284489700000000 microseconds since 1st Jan 0 AD
       
   101 	// This is local time
       
   102 	TDateTime extDate(2005, EMay, 15, 8, 55, 0, 0);
       
   103 	TTime extTime(extDate);
       
   104 	TTime extDueTime(extTime);
       
   105 	extTask->OnDue(TTsTime(extDueTime,EFalse));
       
   106 	
       
   107 	// externalize
       
   108 	RFile extFile;
       
   109 	TFileName extFileName(fileName);
       
   110 	TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
       
   111 	RFileBuf extBuf;
       
   112 	CleanupClosePushL(extBuf);
       
   113 	extBuf.Attach(extFile);
       
   114 	RWriteStream extStream(&extBuf);
       
   115 
       
   116 	TRAP(err, extTask->ExternalizeL(extStream));
       
   117 	TEST(err == KErrNone);
       
   118 	
       
   119 	CleanupStack::PopAndDestroy(3, tzId);
       
   120 	}
       
   121 	
       
   122 /**
       
   123 @file
       
   124 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0203
       
   125 @SYMTestCaseDesc 			Check that internalize executes correctly for tasks with local based times
       
   126 @SYMTestPriority 			low
       
   127 @SYMTestActions  			Create a local time based instance of CSheduledTask which does an internalize
       
   128 @SYMTestExpectedResults		The test must not fail.
       
   129 @SYMPREQ					PREQ234
       
   130 */
       
   131 LOCAL_D void doLocalTimeInternalizeTestL()
       
   132 	{
       
   133 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0203 CScheduledTask Internalize Test (local time) "));
       
   134 
       
   135 	// internalize
       
   136 	RFile intFile;
       
   137 	TFileName intFileName(fileName);
       
   138 	TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
       
   139 	RFileBuf intBuf;
       
   140 	CleanupClosePushL(intBuf);
       
   141 	intBuf.Attach(intFile);
       
   142 	RReadStream intStream(&intBuf);
       
   143 	
       
   144 	CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
       
   145 	
       
   146 	// due date/time is 8.55am, 15 May 2005 -Daylight savings apply on this date
       
   147 	TTsTime ttime = intTask->ValidUntil();
       
   148 	TTime time = ttime.GetLocalTime();
       
   149 	TDateTime dtime = time.DateTime();
       
   150 	
       
   151 	TEST(dtime.Year() == 2005);
       
   152 	TEST(dtime.Month() == EMay);
       
   153 	TEST(dtime.Day() == 15);
       
   154 	TEST(dtime.Hour() == 8);
       
   155 	TEST(dtime.Minute() == 55);
       
   156 	TEST(dtime.Second() == 0);
       
   157 	TEST(dtime.MicroSecond() == 0);
       
   158 	
       
   159 	// test offset - 3600 seconds (1 hour) because time of device is in BST
       
   160 	TTimeIntervalSeconds offset(3600);
       
   161 	TEST(ttime.GetOffset() == offset);
       
   162 	
       
   163 	// test difference between returned values
       
   164 	TTime utcTime = intTask->ValidUntil().GetUtcTime();
       
   165 	TTime localTime = intTask->ValidUntil().GetLocalTime();
       
   166 	Int64 t = localTime.Int64() - utcTime.Int64();
       
   167 	TTimeIntervalSeconds diff((localTime.Int64() - utcTime.Int64())/1000000);
       
   168 	TEST(diff == offset);
       
   169 	
       
   170 	// test that this instance is home time and not UTC
       
   171 	TEST(ttime.IsUtc() == EFalse);
       
   172 
       
   173 	// test data
       
   174 	HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
       
   175 	TEST(data->Compare(KHBufTest) == 0);
       
   176 
       
   177 	CleanupStack::PopAndDestroy(2, &intBuf);
       
   178 	}
       
   179 
       
   180 /**
       
   181 @file
       
   182 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0239 
       
   183 @SYMTestCaseDesc 			Check that externalize executes correctly for tasks with UTC based times
       
   184 @SYMTestPriority 			low
       
   185 @SYMTestActions  			Create a UTC time based instance of CSheduledTask and externalize
       
   186 @SYMTestExpectedResults		The test must not fail.
       
   187 @SYMPREQ					PREQ234
       
   188 */
       
   189 LOCAL_D void doUtcExternalizeTestL()
       
   190 	{
       
   191 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0239 CScheduledTask Externalize Test (UTC) "));
       
   192 
       
   193 	TheTest.Printf(_L("Tests with timezone set to Europe, London"));	
       
   194 	RTz tz;
       
   195 	tz.Connect();
       
   196 	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
       
   197 	CleanupStack::PushL(tzId);
       
   198 	tz.SetTimeZoneL(*tzId);
       
   199 	
       
   200 	TheTest.Printf(_L("Tests with DST on"));	
       
   201 	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
       
   202 	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
       
   203 	TTime time(date);
       
   204 	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
       
   205 	TEST(err == 0);
       
   206 	User::After(KOneSecond * 3);	
       
   207 	// set data for CScheduledTask construction
       
   208 	TTaskInfo taskInfo;
       
   209 	
       
   210 	HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
       
   211 	TPtr pData(taskdata->Des());
       
   212 	pData.Append(KHBufTest);
       
   213 
       
   214 	TSecurityInfo securityInfo;
       
   215 
       
   216 	CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo, 
       
   217 										taskdata, 
       
   218 										ETimeSchedule, 
       
   219 										securityInfo);
       
   220 	CleanupStack::Pop(taskdata); //taskdata now owned by newTask
       
   221 	CleanupStack::PushL(extTask);
       
   222 	
       
   223 	// set due time, 63284489700000000 microseconds since 1st Jan 0 AD
       
   224 	// This is UTC time
       
   225 	TDateTime extDate(2005, EMay, 15, 8, 55, 0, 0);
       
   226 	TTime extTime(extDate);
       
   227 	TTime extDueTime(extTime);
       
   228 	extTask->OnDue(TTsTime(extDueTime,ETrue));
       
   229 	
       
   230 	// externalize
       
   231 	RFile extFile;
       
   232 	TFileName extFileName(fileName);
       
   233 	TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
       
   234 	RFileBuf extBuf;
       
   235 	CleanupClosePushL(extBuf);
       
   236 	extBuf.Attach(extFile);
       
   237 	RWriteStream extStream(&extBuf);
       
   238 
       
   239 	TRAP(err, extTask->ExternalizeL(extStream));
       
   240 	TEST(err == KErrNone);
       
   241 	
       
   242 	CleanupStack::PopAndDestroy(3, tzId);
       
   243 	}
       
   244 	
       
   245 /**
       
   246 @file
       
   247 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0240	
       
   248 @SYMTestCaseDesc 			Check that internalize executes correctly for tasks with UTC times
       
   249 @SYMTestPriority 			low
       
   250 @SYMTestActions  			Create a UTC based instance of CSheduledTask which does an internalize
       
   251 @SYMTestExpectedResults		The test must not fail.
       
   252 @SYMPREQ					PREQ234
       
   253 */
       
   254 LOCAL_D void doUtcInternalizeTestL()
       
   255 	{
       
   256 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0240 CScheduledTask Internalize Test (UTC) "));
       
   257 
       
   258 	// internalize
       
   259 	RFile intFile;
       
   260 	TFileName intFileName(fileName);
       
   261 	TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
       
   262 	RFileBuf intBuf;
       
   263 	CleanupClosePushL(intBuf);
       
   264 	intBuf.Attach(intFile);
       
   265 	RReadStream intStream(&intBuf);
       
   266 	
       
   267 	CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
       
   268 	
       
   269 	// due date/time is 8.55am, 15 May 2005 -Daylight savings apply on this date
       
   270 	TTsTime ttime = intTask->ValidUntil();
       
   271 	TTime time = ttime.GetUtcTime();
       
   272 	TDateTime dtime = time.DateTime();
       
   273 	
       
   274 	TEST(dtime.Year() == 2005);
       
   275 	TEST(dtime.Month() == EMay);
       
   276 	TEST(dtime.Day() == 15);
       
   277 	TEST(dtime.Hour() == 8);
       
   278 	TEST(dtime.Minute() == 55);
       
   279 	TEST(dtime.Second() == 0);
       
   280 	TEST(dtime.MicroSecond() == 0);
       
   281 	
       
   282 	// test offset - should be zero because the object is UTC based
       
   283 	TTimeIntervalSeconds offset(0);
       
   284 	TEST(ttime.GetOffset() == offset);
       
   285 	
       
   286 	// test difference between returned values
       
   287 	TTime utcTime = intTask->ValidUntil().GetUtcTime();
       
   288 	TTime localTime = intTask->ValidUntil().GetLocalTime();
       
   289 	Int64 t = localTime.Int64() - utcTime.Int64();
       
   290 	TTimeIntervalSeconds diff((localTime.Int64() - utcTime.Int64())/1000000);
       
   291 	// difference should be the kernel offset from UTC	
       
   292 	offset = User::UTCOffset();
       
   293 	TEST(diff == offset);
       
   294 	
       
   295 	// test that this instance is UTC and not local time based
       
   296 	TEST(ttime.IsUtc());
       
   297 
       
   298 	// test data
       
   299 	HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
       
   300 	TEST(data->Compare(KHBufTest) == 0);
       
   301 
       
   302 	CleanupStack::PopAndDestroy(2, &intBuf);
       
   303 	}
       
   304 
       
   305 
       
   306 static void RunTestsL()
       
   307 	{
       
   308 	doLocalTimeExternalizeTestL();
       
   309 	doLocalTimeInternalizeTestL();
       
   310 	doUtcExternalizeTestL();
       
   311 	doUtcInternalizeTestL();
       
   312 	}
       
   313 
       
   314 //***********************************************************************************
       
   315 GLDEF_C TInt E32Main()
       
   316  	{
       
   317 	CTrapCleanup* tc = CTrapCleanup::New();
       
   318 	TEST(tc != NULL);
       
   319 	
       
   320 	__UHEAP_MARK;
       
   321 	
       
   322 	fileName.Copy(KFileName);
       
   323 	fileName[0] = RFs::GetSystemDriveChar();
       
   324 
       
   325 	TInt err = TheFsSession.Connect();
       
   326 	TEST(err == KErrNone);
       
   327 
       
   328 	TheTest.Title();
       
   329 	TheTest.Start(_L("Unit tests for CScheduledTask"));
       
   330 	TRAP(err, ::RunTestsL())
       
   331 	TEST(err == KErrNone);
       
   332 	
       
   333 	(void)TheFsSession.Delete(fileName);
       
   334 	TheFsSession.Close();
       
   335 	TheTest.End();
       
   336 	TheTest.Close();
       
   337 
       
   338 	__UHEAP_MARKEND;
       
   339 	
       
   340 	delete tc;
       
   341 	
       
   342 	return(KErrNone);
       
   343 	}