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.
29 _LIT(KTestName, "TTsTime Tests");
30 RTest TheTest(KTestName);
33 static RFs TheFsSession;
34 _LIT(KFileName,"_:\\ttstime.dat");
35 static TBuf<32> fileName;
40 //Test macros and functions
42 static void Check(TInt aValue, TInt aLine)
46 (void)TheFsSession.Delete(fileName);
47 TheTest(EFalse, aLine);
50 static void Check(TInt aValue, TInt aExpected, TInt aLine)
52 if(aValue != aExpected)
54 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
55 (void)TheFsSession.Delete(fileName);
56 TheTest(EFalse, aLine);
59 #define TEST(arg) ::Check((arg), __LINE__)
60 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
64 @SYMTestCaseID SYSLIB-SCHSVR-CT-0204
65 @SYMTestCaseDesc Check that the default constructor works properly
67 @SYMTestActions Creates an instance of TTsTime and check its data
68 @SYMTestExpectedResults The test must not fail.
71 static void TestsWithDefaultContructorL()
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
77 TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
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);
82 TInt err = SchSvrHelpers::SetUTCTimeL(time);
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);
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));
103 @SYMTestCaseID SYSLIB-SCHSVR-CT-0205
104 @SYMTestCaseDesc Checks that the second constructor works properly
106 @SYMTestActions Creates an instance of TTsTime and check its data
107 @SYMTestExpectedResults The test must not fail.
110 static void TestWithSecondConstructorAndUtcL()
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"));
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);
118 TInt err = SchSvrHelpers::SetUTCTimeL(time);
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
134 @SYMTestCaseID SYSLIB-SCHSVR-CT-0206
135 @SYMTestCaseDesc Check TTsTime::SetHomeTime works properly
137 @SYMTestActions Creates an instance of TTsTime, sets its values to home time and verifies them
138 @SYMTestExpectedResults The test must not fail.
141 static void TestSetHomeTimeL()
143 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0206 TTsTime::SetHomeTime test "));
144 TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
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);
150 TInt err = SchSvrHelpers::SetUTCTimeL(time);
153 //set date and time of TTsTime to home time
154 date = TDateTime(2005, EMay, 15, 9, 55, 0, 0);
157 tsTime.SetLocalTime(time);
159 TTime localTime = tsTime.GetLocalTime();
160 TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
161 TBool isUtc = tsTime.IsUtc();
163 // check that the returned local time is the same as the one we set earlier
164 TEST(localTime == time );
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);
170 TEST(utcTime == time );
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.
179 @SYMTestCaseID SYSLIB-SCHSVR-CT-0207
180 @SYMTestCaseDesc Checks ProcessOffsetEvent
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.
186 static void TestProcessOffsetEventL()
188 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0207 ProcessOffsetEvent Test "));
189 TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
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);
196 TInt err = SchSvrHelpers::SetHomeTimeL(time);
197 TEST(err == KErrNone);
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);
204 TTsTime tsTime(time, EFalse);
206 // remember current offset
207 TTimeIntervalSeconds savedOffset = User::UTCOffset();
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);
216 TTimeIntervalSeconds newOffset = User::UTCOffset();
218 // call ProcessOffsetEvent, should put 8:55 in iUtcTime
219 tsTime.ProcessOffsetEvent();
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);
226 TEST(utcTime == time);
228 // check the updated offset
229 TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
230 TEST(tsTimeOffset == newOffset);
232 CleanupStack::PopAndDestroy(tzParisId);
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);
243 @SYMTestCaseID SYSLIB-SCHSVR-CT-0208
244 @SYMTestCaseDesc Checks ExternalizeL and InternalizeL
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.
251 static void TestExternalizeInternalizeL()
254 TTsTime time(TTime(5), ETrue);
257 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0208 Test TTsTime::ExternalizeL "));
260 TFileName testFileName(fileName);
261 TEST2(file.Replace(TheFsSession,testFileName,EFileWrite|EFileRead), KErrNone);
263 CleanupClosePushL(buf);
265 RWriteStream stream(&buf);
266 //Externalize the time
269 CleanupStack::PopAndDestroy(&buf);
272 TheTest.Next(_L("Test TTsTime::InternalizeL"));
274 TInt err = file.Open(TheFsSession,testFileName,EFileRead);
275 TEST2(err, KErrNone);
277 CleanupClosePushL(buf2);
279 RReadStream stream2(&buf2);
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));
289 CleanupStack::PopAndDestroy(&buf2);
293 static void RunTestsL()
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);
302 TestsWithDefaultContructorL();
303 TestWithSecondConstructorAndUtcL();
305 TestProcessOffsetEventL();
306 TestExternalizeInternalizeL();
308 CleanupStack::PopAndDestroy(tzLondonId);
312 GLDEF_C TInt E32Main()
314 CTrapCleanup* tc = CTrapCleanup::New();
319 fileName.Copy(KFileName);
320 fileName[0] = RFs::GetSystemDriveChar();
322 TInt err = TheFsSession.Connect();
323 TEST2(err, KErrNone);
326 TheTest.Start(_L("Task Scheduler TTsTime unit tests"));
327 TRAP(err, ::RunTestsL())
328 TEST2(err, KErrNone);
330 (void)TheFsSession.Delete(fileName);
331 TheFsSession.Close();