os/ossrv/genericservices/taskscheduler/Test/ScheduledTaskTest/TU_TSCH_ScheduledTaskTest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/taskscheduler/Test/ScheduledTaskTest/TU_TSCH_ScheduledTaskTest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,343 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include <f32file.h>
    1.21 +#include <tz.h>
    1.22 +#include <schinfo.h>
    1.23 +#include <schinfointernal.h>
    1.24 +#include <schtask.h>
    1.25 +#include "Thelpers.h"
    1.26 +
    1.27 +_LIT(KHBufTest, "This is a CScheduledTask test");
    1.28 +_LIT(KTestName,	"Scheduled Task");
    1.29 +RTest	TheTest(KTestName);
    1.30 +
    1.31 +// persistent file for externalize and internalize test
    1.32 +_LIT(KFileName,"_:\\CSheduledTask.dat");
    1.33 +static TBuf<32> fileName;
    1.34 +
    1.35 +LOCAL_D RFs TheFsSession;
    1.36 +
    1.37 +//Test macroses and functions
    1.38 +static void Check(TInt aValue, TInt aLine)
    1.39 +	{
    1.40 +	if(!aValue)
    1.41 +		{
    1.42 +		(void)TheFsSession.Delete(fileName);
    1.43 +		TheTest(EFalse, aLine);
    1.44 +		}
    1.45 +	}
    1.46 +static  void Check(TInt aValue, TInt aExpected, TInt aLine)
    1.47 +	{
    1.48 +	if(aValue != aExpected)
    1.49 +		{
    1.50 +		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    1.51 +		(void)TheFsSession.Delete(fileName);
    1.52 +		TheTest(EFalse, aLine);
    1.53 +		}
    1.54 +	}
    1.55 +#define TEST(arg) ::Check((arg), __LINE__)
    1.56 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    1.57 +
    1.58 +
    1.59 +
    1.60 +/**
    1.61 +@file
    1.62 +@SYMTestCaseID				SYSLIB-SCHSVR-CT-0202
    1.63 +@SYMTestCaseDesc 			Check that externalize executes correctly for tasks with local based times
    1.64 +@SYMTestPriority 			low
    1.65 +@SYMTestActions  			Create a local time based instance of CSheduledTask and externalize
    1.66 +@SYMTestExpectedResults		The test must not fail.
    1.67 +@SYMPREQ					PREQ234
    1.68 +*/
    1.69 +LOCAL_D void doLocalTimeExternalizeTestL()
    1.70 +	{
    1.71 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0202 CScheduledTask Externalize Test (local time) "));
    1.72 +
    1.73 +	TheTest.Printf(_L("Tests with timezone set to Europe, London"));	
    1.74 +	RTz tz;
    1.75 +	tz.Connect();
    1.76 +	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
    1.77 +	CleanupStack::PushL(tzId);
    1.78 +	tz.SetTimeZoneL(*tzId);
    1.79 +	
    1.80 +	TheTest.Printf(_L("Tests with DST on"));	
    1.81 +	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
    1.82 +	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
    1.83 +	TTime time(date);
    1.84 +	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
    1.85 +	TEST(err == 0);
    1.86 +	User::After(KOneSecond * 3);	
    1.87 +	// set data for CScheduledTask construction
    1.88 +	TTaskInfo taskInfo;
    1.89 +	
    1.90 +	HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
    1.91 +	TPtr pData(taskdata->Des());
    1.92 +	pData.Append(KHBufTest);
    1.93 +
    1.94 +	TSecurityInfo securityInfo;
    1.95 +
    1.96 +	CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo, 
    1.97 +										taskdata, 
    1.98 +										ETimeSchedule, 
    1.99 +										securityInfo);
   1.100 +	CleanupStack::Pop(taskdata); //taskdata now owned by newTask
   1.101 +	CleanupStack::PushL(extTask);
   1.102 +	
   1.103 +	// set due time, 63284489700000000 microseconds since 1st Jan 0 AD
   1.104 +	// This is local time
   1.105 +	TDateTime extDate(2005, EMay, 15, 8, 55, 0, 0);
   1.106 +	TTime extTime(extDate);
   1.107 +	TTime extDueTime(extTime);
   1.108 +	extTask->OnDue(TTsTime(extDueTime,EFalse));
   1.109 +	
   1.110 +	// externalize
   1.111 +	RFile extFile;
   1.112 +	TFileName extFileName(fileName);
   1.113 +	TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
   1.114 +	RFileBuf extBuf;
   1.115 +	CleanupClosePushL(extBuf);
   1.116 +	extBuf.Attach(extFile);
   1.117 +	RWriteStream extStream(&extBuf);
   1.118 +
   1.119 +	TRAP(err, extTask->ExternalizeL(extStream));
   1.120 +	TEST(err == KErrNone);
   1.121 +	
   1.122 +	CleanupStack::PopAndDestroy(3, tzId);
   1.123 +	}
   1.124 +	
   1.125 +/**
   1.126 +@file
   1.127 +@SYMTestCaseID				SYSLIB-SCHSVR-CT-0203
   1.128 +@SYMTestCaseDesc 			Check that internalize executes correctly for tasks with local based times
   1.129 +@SYMTestPriority 			low
   1.130 +@SYMTestActions  			Create a local time based instance of CSheduledTask which does an internalize
   1.131 +@SYMTestExpectedResults		The test must not fail.
   1.132 +@SYMPREQ					PREQ234
   1.133 +*/
   1.134 +LOCAL_D void doLocalTimeInternalizeTestL()
   1.135 +	{
   1.136 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0203 CScheduledTask Internalize Test (local time) "));
   1.137 +
   1.138 +	// internalize
   1.139 +	RFile intFile;
   1.140 +	TFileName intFileName(fileName);
   1.141 +	TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
   1.142 +	RFileBuf intBuf;
   1.143 +	CleanupClosePushL(intBuf);
   1.144 +	intBuf.Attach(intFile);
   1.145 +	RReadStream intStream(&intBuf);
   1.146 +	
   1.147 +	CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
   1.148 +	
   1.149 +	// due date/time is 8.55am, 15 May 2005 -Daylight savings apply on this date
   1.150 +	TTsTime ttime = intTask->ValidUntil();
   1.151 +	TTime time = ttime.GetLocalTime();
   1.152 +	TDateTime dtime = time.DateTime();
   1.153 +	
   1.154 +	TEST(dtime.Year() == 2005);
   1.155 +	TEST(dtime.Month() == EMay);
   1.156 +	TEST(dtime.Day() == 15);
   1.157 +	TEST(dtime.Hour() == 8);
   1.158 +	TEST(dtime.Minute() == 55);
   1.159 +	TEST(dtime.Second() == 0);
   1.160 +	TEST(dtime.MicroSecond() == 0);
   1.161 +	
   1.162 +	// test offset - 3600 seconds (1 hour) because time of device is in BST
   1.163 +	TTimeIntervalSeconds offset(3600);
   1.164 +	TEST(ttime.GetOffset() == offset);
   1.165 +	
   1.166 +	// test difference between returned values
   1.167 +	TTime utcTime = intTask->ValidUntil().GetUtcTime();
   1.168 +	TTime localTime = intTask->ValidUntil().GetLocalTime();
   1.169 +	Int64 t = localTime.Int64() - utcTime.Int64();
   1.170 +	TTimeIntervalSeconds diff((localTime.Int64() - utcTime.Int64())/1000000);
   1.171 +	TEST(diff == offset);
   1.172 +	
   1.173 +	// test that this instance is home time and not UTC
   1.174 +	TEST(ttime.IsUtc() == EFalse);
   1.175 +
   1.176 +	// test data
   1.177 +	HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
   1.178 +	TEST(data->Compare(KHBufTest) == 0);
   1.179 +
   1.180 +	CleanupStack::PopAndDestroy(2, &intBuf);
   1.181 +	}
   1.182 +
   1.183 +/**
   1.184 +@file
   1.185 +@SYMTestCaseID				SYSLIB-SCHSVR-CT-0239 
   1.186 +@SYMTestCaseDesc 			Check that externalize executes correctly for tasks with UTC based times
   1.187 +@SYMTestPriority 			low
   1.188 +@SYMTestActions  			Create a UTC time based instance of CSheduledTask and externalize
   1.189 +@SYMTestExpectedResults		The test must not fail.
   1.190 +@SYMPREQ					PREQ234
   1.191 +*/
   1.192 +LOCAL_D void doUtcExternalizeTestL()
   1.193 +	{
   1.194 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0239 CScheduledTask Externalize Test (UTC) "));
   1.195 +
   1.196 +	TheTest.Printf(_L("Tests with timezone set to Europe, London"));	
   1.197 +	RTz tz;
   1.198 +	tz.Connect();
   1.199 +	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
   1.200 +	CleanupStack::PushL(tzId);
   1.201 +	tz.SetTimeZoneL(*tzId);
   1.202 +	
   1.203 +	TheTest.Printf(_L("Tests with DST on"));	
   1.204 +	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
   1.205 +	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
   1.206 +	TTime time(date);
   1.207 +	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
   1.208 +	TEST(err == 0);
   1.209 +	User::After(KOneSecond * 3);	
   1.210 +	// set data for CScheduledTask construction
   1.211 +	TTaskInfo taskInfo;
   1.212 +	
   1.213 +	HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
   1.214 +	TPtr pData(taskdata->Des());
   1.215 +	pData.Append(KHBufTest);
   1.216 +
   1.217 +	TSecurityInfo securityInfo;
   1.218 +
   1.219 +	CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo, 
   1.220 +										taskdata, 
   1.221 +										ETimeSchedule, 
   1.222 +										securityInfo);
   1.223 +	CleanupStack::Pop(taskdata); //taskdata now owned by newTask
   1.224 +	CleanupStack::PushL(extTask);
   1.225 +	
   1.226 +	// set due time, 63284489700000000 microseconds since 1st Jan 0 AD
   1.227 +	// This is UTC time
   1.228 +	TDateTime extDate(2005, EMay, 15, 8, 55, 0, 0);
   1.229 +	TTime extTime(extDate);
   1.230 +	TTime extDueTime(extTime);
   1.231 +	extTask->OnDue(TTsTime(extDueTime,ETrue));
   1.232 +	
   1.233 +	// externalize
   1.234 +	RFile extFile;
   1.235 +	TFileName extFileName(fileName);
   1.236 +	TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
   1.237 +	RFileBuf extBuf;
   1.238 +	CleanupClosePushL(extBuf);
   1.239 +	extBuf.Attach(extFile);
   1.240 +	RWriteStream extStream(&extBuf);
   1.241 +
   1.242 +	TRAP(err, extTask->ExternalizeL(extStream));
   1.243 +	TEST(err == KErrNone);
   1.244 +	
   1.245 +	CleanupStack::PopAndDestroy(3, tzId);
   1.246 +	}
   1.247 +	
   1.248 +/**
   1.249 +@file
   1.250 +@SYMTestCaseID				SYSLIB-SCHSVR-CT-0240	
   1.251 +@SYMTestCaseDesc 			Check that internalize executes correctly for tasks with UTC times
   1.252 +@SYMTestPriority 			low
   1.253 +@SYMTestActions  			Create a UTC based instance of CSheduledTask which does an internalize
   1.254 +@SYMTestExpectedResults		The test must not fail.
   1.255 +@SYMPREQ					PREQ234
   1.256 +*/
   1.257 +LOCAL_D void doUtcInternalizeTestL()
   1.258 +	{
   1.259 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0240 CScheduledTask Internalize Test (UTC) "));
   1.260 +
   1.261 +	// internalize
   1.262 +	RFile intFile;
   1.263 +	TFileName intFileName(fileName);
   1.264 +	TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
   1.265 +	RFileBuf intBuf;
   1.266 +	CleanupClosePushL(intBuf);
   1.267 +	intBuf.Attach(intFile);
   1.268 +	RReadStream intStream(&intBuf);
   1.269 +	
   1.270 +	CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
   1.271 +	
   1.272 +	// due date/time is 8.55am, 15 May 2005 -Daylight savings apply on this date
   1.273 +	TTsTime ttime = intTask->ValidUntil();
   1.274 +	TTime time = ttime.GetUtcTime();
   1.275 +	TDateTime dtime = time.DateTime();
   1.276 +	
   1.277 +	TEST(dtime.Year() == 2005);
   1.278 +	TEST(dtime.Month() == EMay);
   1.279 +	TEST(dtime.Day() == 15);
   1.280 +	TEST(dtime.Hour() == 8);
   1.281 +	TEST(dtime.Minute() == 55);
   1.282 +	TEST(dtime.Second() == 0);
   1.283 +	TEST(dtime.MicroSecond() == 0);
   1.284 +	
   1.285 +	// test offset - should be zero because the object is UTC based
   1.286 +	TTimeIntervalSeconds offset(0);
   1.287 +	TEST(ttime.GetOffset() == offset);
   1.288 +	
   1.289 +	// test difference between returned values
   1.290 +	TTime utcTime = intTask->ValidUntil().GetUtcTime();
   1.291 +	TTime localTime = intTask->ValidUntil().GetLocalTime();
   1.292 +	Int64 t = localTime.Int64() - utcTime.Int64();
   1.293 +	TTimeIntervalSeconds diff((localTime.Int64() - utcTime.Int64())/1000000);
   1.294 +	// difference should be the kernel offset from UTC	
   1.295 +	offset = User::UTCOffset();
   1.296 +	TEST(diff == offset);
   1.297 +	
   1.298 +	// test that this instance is UTC and not local time based
   1.299 +	TEST(ttime.IsUtc());
   1.300 +
   1.301 +	// test data
   1.302 +	HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
   1.303 +	TEST(data->Compare(KHBufTest) == 0);
   1.304 +
   1.305 +	CleanupStack::PopAndDestroy(2, &intBuf);
   1.306 +	}
   1.307 +
   1.308 +
   1.309 +static void RunTestsL()
   1.310 +	{
   1.311 +	doLocalTimeExternalizeTestL();
   1.312 +	doLocalTimeInternalizeTestL();
   1.313 +	doUtcExternalizeTestL();
   1.314 +	doUtcInternalizeTestL();
   1.315 +	}
   1.316 +
   1.317 +//***********************************************************************************
   1.318 +GLDEF_C TInt E32Main()
   1.319 + 	{
   1.320 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.321 +	TEST(tc != NULL);
   1.322 +	
   1.323 +	__UHEAP_MARK;
   1.324 +	
   1.325 +	fileName.Copy(KFileName);
   1.326 +	fileName[0] = RFs::GetSystemDriveChar();
   1.327 +
   1.328 +	TInt err = TheFsSession.Connect();
   1.329 +	TEST(err == KErrNone);
   1.330 +
   1.331 +	TheTest.Title();
   1.332 +	TheTest.Start(_L("Unit tests for CScheduledTask"));
   1.333 +	TRAP(err, ::RunTestsL())
   1.334 +	TEST(err == KErrNone);
   1.335 +	
   1.336 +	(void)TheFsSession.Delete(fileName);
   1.337 +	TheFsSession.Close();
   1.338 +	TheTest.End();
   1.339 +	TheTest.Close();
   1.340 +
   1.341 +	__UHEAP_MARKEND;
   1.342 +	
   1.343 +	delete tc;
   1.344 +	
   1.345 +	return(KErrNone);
   1.346 +	}