os/ossrv/genericservices/taskscheduler/Test/ScheduledTaskTest/TU_TSCH_ScheduledTaskTest.cpp
First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include <schinfointernal.h>
24 _LIT(KHBufTest, "This is a CScheduledTask test");
25 _LIT(KTestName, "Scheduled Task");
26 RTest TheTest(KTestName);
28 // persistent file for externalize and internalize test
29 _LIT(KFileName,"_:\\CSheduledTask.dat");
30 static TBuf<32> fileName;
32 LOCAL_D RFs TheFsSession;
34 //Test macroses and functions
35 static void Check(TInt aValue, TInt aLine)
39 (void)TheFsSession.Delete(fileName);
40 TheTest(EFalse, aLine);
43 static void Check(TInt aValue, TInt aExpected, TInt aLine)
45 if(aValue != aExpected)
47 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
48 (void)TheFsSession.Delete(fileName);
49 TheTest(EFalse, aLine);
52 #define TEST(arg) ::Check((arg), __LINE__)
53 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
59 @SYMTestCaseID SYSLIB-SCHSVR-CT-0202
60 @SYMTestCaseDesc Check that externalize executes correctly for tasks with local based times
62 @SYMTestActions Create a local time based instance of CSheduledTask and externalize
63 @SYMTestExpectedResults The test must not fail.
66 LOCAL_D void doLocalTimeExternalizeTestL()
68 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0202 CScheduledTask Externalize Test (local time) "));
70 TheTest.Printf(_L("Tests with timezone set to Europe, London"));
73 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
74 CleanupStack::PushL(tzId);
75 tz.SetTimeZoneL(*tzId);
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);
81 TInt err = SchSvrHelpers::SetUTCTimeL(time);
83 User::After(KOneSecond * 3);
84 // set data for CScheduledTask construction
87 HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
88 TPtr pData(taskdata->Des());
89 pData.Append(KHBufTest);
91 TSecurityInfo securityInfo;
93 CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo,
97 CleanupStack::Pop(taskdata); //taskdata now owned by newTask
98 CleanupStack::PushL(extTask);
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));
109 TFileName extFileName(fileName);
110 TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
112 CleanupClosePushL(extBuf);
113 extBuf.Attach(extFile);
114 RWriteStream extStream(&extBuf);
116 TRAP(err, extTask->ExternalizeL(extStream));
117 TEST(err == KErrNone);
119 CleanupStack::PopAndDestroy(3, tzId);
124 @SYMTestCaseID SYSLIB-SCHSVR-CT-0203
125 @SYMTestCaseDesc Check that internalize executes correctly for tasks with local based times
127 @SYMTestActions Create a local time based instance of CSheduledTask which does an internalize
128 @SYMTestExpectedResults The test must not fail.
131 LOCAL_D void doLocalTimeInternalizeTestL()
133 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0203 CScheduledTask Internalize Test (local time) "));
137 TFileName intFileName(fileName);
138 TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
140 CleanupClosePushL(intBuf);
141 intBuf.Attach(intFile);
142 RReadStream intStream(&intBuf);
144 CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
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();
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);
159 // test offset - 3600 seconds (1 hour) because time of device is in BST
160 TTimeIntervalSeconds offset(3600);
161 TEST(ttime.GetOffset() == offset);
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);
170 // test that this instance is home time and not UTC
171 TEST(ttime.IsUtc() == EFalse);
174 HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
175 TEST(data->Compare(KHBufTest) == 0);
177 CleanupStack::PopAndDestroy(2, &intBuf);
182 @SYMTestCaseID SYSLIB-SCHSVR-CT-0239
183 @SYMTestCaseDesc Check that externalize executes correctly for tasks with UTC based times
185 @SYMTestActions Create a UTC time based instance of CSheduledTask and externalize
186 @SYMTestExpectedResults The test must not fail.
189 LOCAL_D void doUtcExternalizeTestL()
191 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0239 CScheduledTask Externalize Test (UTC) "));
193 TheTest.Printf(_L("Tests with timezone set to Europe, London"));
196 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
197 CleanupStack::PushL(tzId);
198 tz.SetTimeZoneL(*tzId);
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);
204 TInt err = SchSvrHelpers::SetUTCTimeL(time);
206 User::After(KOneSecond * 3);
207 // set data for CScheduledTask construction
210 HBufC* taskdata = HBufC::NewLC(KHBufTest().Length());
211 TPtr pData(taskdata->Des());
212 pData.Append(KHBufTest);
214 TSecurityInfo securityInfo;
216 CScheduledTask* extTask = new(ELeave) CScheduledTask(taskInfo,
220 CleanupStack::Pop(taskdata); //taskdata now owned by newTask
221 CleanupStack::PushL(extTask);
223 // set due time, 63284489700000000 microseconds since 1st Jan 0 AD
225 TDateTime extDate(2005, EMay, 15, 8, 55, 0, 0);
226 TTime extTime(extDate);
227 TTime extDueTime(extTime);
228 extTask->OnDue(TTsTime(extDueTime,ETrue));
232 TFileName extFileName(fileName);
233 TEST2(extFile.Replace(TheFsSession,extFileName,EFileWrite), KErrNone);
235 CleanupClosePushL(extBuf);
236 extBuf.Attach(extFile);
237 RWriteStream extStream(&extBuf);
239 TRAP(err, extTask->ExternalizeL(extStream));
240 TEST(err == KErrNone);
242 CleanupStack::PopAndDestroy(3, tzId);
247 @SYMTestCaseID SYSLIB-SCHSVR-CT-0240
248 @SYMTestCaseDesc Check that internalize executes correctly for tasks with UTC times
250 @SYMTestActions Create a UTC based instance of CSheduledTask which does an internalize
251 @SYMTestExpectedResults The test must not fail.
254 LOCAL_D void doUtcInternalizeTestL()
256 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0240 CScheduledTask Internalize Test (UTC) "));
260 TFileName intFileName(fileName);
261 TEST2(intFile.Open(TheFsSession,intFileName,EFileRead), KErrNone);
263 CleanupClosePushL(intBuf);
264 intBuf.Attach(intFile);
265 RReadStream intStream(&intBuf);
267 CScheduledTask* intTask = CScheduledTask::NewLC(intStream);
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();
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);
282 // test offset - should be zero because the object is UTC based
283 TTimeIntervalSeconds offset(0);
284 TEST(ttime.GetOffset() == offset);
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);
295 // test that this instance is UTC and not local time based
299 HBufC* data = const_cast<HBufC*>(&(intTask->Data()));
300 TEST(data->Compare(KHBufTest) == 0);
302 CleanupStack::PopAndDestroy(2, &intBuf);
306 static void RunTestsL()
308 doLocalTimeExternalizeTestL();
309 doLocalTimeInternalizeTestL();
310 doUtcExternalizeTestL();
311 doUtcInternalizeTestL();
314 //***********************************************************************************
315 GLDEF_C TInt E32Main()
317 CTrapCleanup* tc = CTrapCleanup::New();
322 fileName.Copy(KFileName);
323 fileName[0] = RFs::GetSystemDriveChar();
325 TInt err = TheFsSession.Connect();
326 TEST(err == KErrNone);
329 TheTest.Start(_L("Unit tests for CScheduledTask"));
330 TRAP(err, ::RunTestsL())
331 TEST(err == KErrNone);
333 (void)TheFsSession.Delete(fileName);
334 TheFsSession.Close();