1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericservices/taskscheduler/Test/TTsTimeUnitTests/TU_TSCH_ttstime.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,342 @@
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 "SCHTIME.H"
1.20 +
1.21 +#include <e32base.h>
1.22 +#include <e32test.h>
1.23 +#include <f32file.h>
1.24 +#include <s32file.h>
1.25 +
1.26 +#include <e32std.h>
1.27 +#include <tz.h>
1.28 +
1.29 +#include "Thelpers.h"
1.30 +
1.31 +
1.32 +_LIT(KTestName, "TTsTime Tests");
1.33 +RTest TheTest(KTestName);
1.34 +RTz TheTzServer;
1.35 +
1.36 +static RFs TheFsSession;
1.37 +_LIT(KFileName,"_:\\ttstime.dat");
1.38 +static TBuf<32> fileName;
1.39 +
1.40 +
1.41 +//
1.42 +//
1.43 +//Test macros and functions
1.44 +
1.45 +static void Check(TInt aValue, TInt aLine)
1.46 + {
1.47 + if(!aValue)
1.48 + {
1.49 + (void)TheFsSession.Delete(fileName);
1.50 + TheTest(EFalse, aLine);
1.51 + }
1.52 + }
1.53 +static void Check(TInt aValue, TInt aExpected, TInt aLine)
1.54 + {
1.55 + if(aValue != aExpected)
1.56 + {
1.57 + RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
1.58 + (void)TheFsSession.Delete(fileName);
1.59 + TheTest(EFalse, aLine);
1.60 + }
1.61 + }
1.62 +#define TEST(arg) ::Check((arg), __LINE__)
1.63 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
1.64 +
1.65 +/**
1.66 +@file
1.67 +@SYMTestCaseID SYSLIB-SCHSVR-CT-0204
1.68 +@SYMTestCaseDesc Check that the default constructor works properly
1.69 +@SYMTestPriority low
1.70 +@SYMTestActions Creates an instance of TTsTime and check its data
1.71 +@SYMTestExpectedResults The test must not fail.
1.72 +@SYMPREQ PREQ234
1.73 +*/
1.74 +static void TestsWithDefaultContructorL()
1.75 + {
1.76 + // Tests with default constructor:
1.77 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0204 TTsTime() default constructor, TTsTime is UTC time by default "));
1.78 + TTsTime tsTime; //defalut constructor, TTsTime is UTC
1.79 +
1.80 + TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
1.81 +
1.82 + //set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
1.83 + TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
1.84 + TTime time(date);
1.85 + TInt err = SchSvrHelpers::SetUTCTimeL(time);
1.86 + TEST(err == 0);
1.87 +
1.88 +
1.89 + // check that member data are set properly using GetUtcTime() and GetLocalTime()
1.90 + TTime utcTime = TTime(tsTime.GetUtcTime());
1.91 + TTime localTime = TTime(tsTime.GetLocalTime());
1.92 + TDateTime testDate(0, EJanuary, 0, 1, 0, 0, 0);
1.93 + TTime testTime(testDate);
1.94 + TEST (localTime == testTime);
1.95 +
1.96 + // check the offset
1.97 + // because we set the current time zone to Europe/London and
1.98 + // ProcessOffsetEvent has been called in GetUtcTime, and since TTsTime is UTC by defalu then
1.99 + // TTsTime::iOffset remains the same
1.100 + TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
1.101 + TEST(tsTimeOffset == TTimeIntervalSeconds(0));
1.102 + }
1.103 +
1.104 +/**
1.105 +@file
1.106 +@SYMTestCaseID SYSLIB-SCHSVR-CT-0205
1.107 +@SYMTestCaseDesc Checks that the second constructor works properly
1.108 +@SYMTestPriority low
1.109 +@SYMTestActions Creates an instance of TTsTime and check its data
1.110 +@SYMTestExpectedResults The test must not fail.
1.111 +@SYMPREQ PREQ234
1.112 +*/
1.113 +static void TestWithSecondConstructorAndUtcL()
1.114 + {
1.115 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0205 TTsTime - test for 2nd constructor "));
1.116 + TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
1.117 +
1.118 + //set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
1.119 + TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
1.120 + TTime time(date);
1.121 + TInt err = SchSvrHelpers::SetUTCTimeL(time);
1.122 + TEST(err == 0);
1.123 +
1.124 + // Tests with overloaded constructor
1.125 + TheTest.Next(_L("Test TTsTime with overloaded operator and TTsTime is UTC"));
1.126 + TTsTime tsTime(time, ETrue); //Sets time to UTC time as boolean is ETrue
1.127 + TTime utcTime = tsTime.GetUtcTime();
1.128 + TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
1.129 + TBool isUtc = tsTime.IsUtc();
1.130 + TEST(utcTime == time);
1.131 + TEST(tsTimeOffset == TTimeIntervalSeconds(0)); //because tsTime is UTC so its offset is zero
1.132 + TEST(isUtc);
1.133 + }
1.134 +
1.135 +/**
1.136 +@file
1.137 +@SYMTestCaseID SYSLIB-SCHSVR-CT-0206
1.138 +@SYMTestCaseDesc Check TTsTime::SetHomeTime works properly
1.139 +@SYMTestPriority low
1.140 +@SYMTestActions Creates an instance of TTsTime, sets its values to home time and verifies them
1.141 +@SYMTestExpectedResults The test must not fail.
1.142 +@SYMPREQ PREQ234
1.143 +*/
1.144 +static void TestSetHomeTimeL()
1.145 + {
1.146 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0206 TTsTime::SetHomeTime test "));
1.147 + TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
1.148 +
1.149 + // set the date and time of the device
1.150 + TheTest.Next(_L("Test TTsTime is Local Time"));
1.151 + TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
1.152 + TTime time(date);
1.153 + TInt err = SchSvrHelpers::SetUTCTimeL(time);
1.154 + TEST(err == 0);
1.155 +
1.156 + //set date and time of TTsTime to home time
1.157 + date = TDateTime(2005, EMay, 15, 9, 55, 0, 0);
1.158 + time = date;
1.159 + TTsTime tsTime;
1.160 + tsTime.SetLocalTime(time);
1.161 +
1.162 + TTime localTime = tsTime.GetLocalTime();
1.163 + TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
1.164 + TBool isUtc = tsTime.IsUtc();
1.165 +
1.166 + // check that the returned local time is the same as the one we set earlier
1.167 + TEST(localTime == time );
1.168 +
1.169 + // check now that the stored utc time in TTsTime is correct
1.170 + TTime utcTime = tsTime.GetUtcTime();
1.171 + date = TDateTime(2005, EMay, 15, 8, 55, 0, 0);
1.172 + time = TTime(date);
1.173 + TEST(utcTime == time );
1.174 +
1.175 + // check offset - 3600 seconds (1 hour) because time of device is in BST
1.176 + TEST(tsTimeOffset == TTimeIntervalSeconds(3600));
1.177 + TEST(!isUtc); //This TTsTime is not UTC.
1.178 + }
1.179 +
1.180 +/**
1.181 +@file
1.182 +@SYMTestCaseID SYSLIB-SCHSVR-CT-0207
1.183 +@SYMTestCaseDesc Checks ProcessOffsetEvent
1.184 +@SYMTestPriority low
1.185 +@SYMTestActions Creates an instance of TTsTime, changes timezone, and checks that the TTsTime object is updated correctly.
1.186 +@SYMTestExpectedResults The test must not fail.
1.187 +@SYMPREQ PREQ234
1.188 +*/
1.189 +static void TestProcessOffsetEventL()
1.190 + {
1.191 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0207 ProcessOffsetEvent Test "));
1.192 + TheTest.Printf(_L("Timezone set to Europe, London, DST on"));
1.193 +
1.194 + //set the current home time to 8.55am, 15 May 2005
1.195 + //Daylight savings do apply on this date
1.196 + //so utc time should be 7.55
1.197 + TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
1.198 + TTime time(date);
1.199 + TInt err = SchSvrHelpers::SetHomeTimeL(time);
1.200 + TEST(err == KErrNone);
1.201 +
1.202 + //Check the ProcessOffsetEvent () method and print iUtcTime, iOffset
1.203 + //create hometime based ttstime object
1.204 + //should have 9.55 in iUtcTime
1.205 + date = TDateTime(2005, EMay, 15, 10, 55, 0, 0);
1.206 + time = date;
1.207 + TTsTime tsTime(time, EFalse);
1.208 +
1.209 + // remember current offset
1.210 + TTimeIntervalSeconds savedOffset = User::UTCOffset();
1.211 +
1.212 + // Increase offset by 1Hr by moving to Paris
1.213 + TheTest.Printf(_L("Move timezone to Paris, Europe. Check TTsTime updated accordingly"));
1.214 + CTzId* tzParisId = CTzId::NewL(2656);
1.215 + CleanupStack::PushL(tzParisId);
1.216 + TheTzServer.SetTimeZoneL(*tzParisId);
1.217 +
1.218 + //get new UTC offset
1.219 + TTimeIntervalSeconds newOffset = User::UTCOffset();
1.220 +
1.221 + // call ProcessOffsetEvent, should put 8:55 in iUtcTime
1.222 + tsTime.ProcessOffsetEvent();
1.223 +
1.224 + // check the updated iUtcTime
1.225 + TTime utcTime = tsTime.GetUtcTime();
1.226 + TDateTime a = utcTime.DateTime();
1.227 + date = TDateTime(2005, EMay, 15, 8, 55, 0, 0);
1.228 + time = date;
1.229 + TEST(utcTime == time);
1.230 +
1.231 + // check the updated offset
1.232 + TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
1.233 + TEST(tsTimeOffset == newOffset);
1.234 +
1.235 + CleanupStack::PopAndDestroy(tzParisId);
1.236 +
1.237 + //return Timezone to London, Europe for other tests
1.238 + CTzId* tzLondonId = CTzId::NewL(2592);
1.239 + CleanupStack::PushL(tzLondonId);
1.240 + TheTzServer.SetTimeZoneL(*tzLondonId);
1.241 + CleanupStack::PopAndDestroy(tzLondonId);
1.242 + }
1.243 +
1.244 +/**
1.245 +@file
1.246 +@SYMTestCaseID SYSLIB-SCHSVR-CT-0208
1.247 +@SYMTestCaseDesc Checks ExternalizeL and InternalizeL
1.248 +@SYMTestPriority low
1.249 +@SYMTestActions Creates an instance of TTsTime, Externalizes
1.250 + it and internaziles and checks that teh data is the same
1.251 +@SYMTestExpectedResults The test must not fail.
1.252 +@SYMPREQ PREQ234
1.253 +*/
1.254 +static void TestExternalizeInternalizeL()
1.255 + {
1.256 + // time is UTC
1.257 + TTsTime time(TTime(5), ETrue);
1.258 +
1.259 + // Test externalise
1.260 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0208 Test TTsTime::ExternalizeL "));
1.261 + RFile file;
1.262 +
1.263 + TFileName testFileName(fileName);
1.264 + TEST2(file.Replace(TheFsSession,testFileName,EFileWrite|EFileRead), KErrNone);
1.265 + RFileBuf buf;
1.266 + CleanupClosePushL(buf);
1.267 + buf.Attach(file);
1.268 + RWriteStream stream(&buf);
1.269 + //Externalize the time
1.270 + stream << time;
1.271 + buf.SynchL();
1.272 + CleanupStack::PopAndDestroy(&buf);
1.273 +
1.274 + // Test internalise
1.275 + TheTest.Next(_L("Test TTsTime::InternalizeL"));
1.276 + TTsTime newTime;
1.277 + TInt err = file.Open(TheFsSession,testFileName,EFileRead);
1.278 + TEST2(err, KErrNone);
1.279 + RFileBuf buf2;
1.280 + CleanupClosePushL(buf2);
1.281 + buf2.Attach(file);
1.282 + RReadStream stream2(&buf2);
1.283 +
1.284 + //Internalize the time
1.285 + stream2 >> newTime; //the externelized time is UTC time, same as the externalized time
1.286 + TTime utcTime = newTime.GetUtcTime();
1.287 + TTimeIntervalSeconds offset = newTime.GetOffset();
1.288 + TBool isUtc = newTime.IsUtc();
1.289 + TEST(utcTime == TTime(5));
1.290 + TEST(offset == TTimeIntervalSeconds(0));
1.291 + TEST(isUtc);
1.292 + CleanupStack::PopAndDestroy(&buf2);
1.293 + }
1.294 +
1.295 +
1.296 +static void RunTestsL()
1.297 + {
1.298 + //All tests assume timezone is London, Europe.
1.299 + TheTzServer.Connect();
1.300 + CTzId* tzLondonId = CTzId::NewL(2592);
1.301 + CleanupStack::PushL(tzLondonId);
1.302 + TheTzServer.SetTimeZoneL(*tzLondonId);
1.303 +
1.304 +
1.305 + TestsWithDefaultContructorL();
1.306 + TestWithSecondConstructorAndUtcL();
1.307 + TestSetHomeTimeL();
1.308 + TestProcessOffsetEventL();
1.309 + TestExternalizeInternalizeL();
1.310 +
1.311 + CleanupStack::PopAndDestroy(tzLondonId);
1.312 + TheTzServer.Close();
1.313 + }
1.314 +
1.315 +GLDEF_C TInt E32Main()
1.316 + {
1.317 + CTrapCleanup* tc = CTrapCleanup::New();
1.318 + TEST(tc != NULL);
1.319 +
1.320 + __UHEAP_MARK;
1.321 +
1.322 + fileName.Copy(KFileName);
1.323 + fileName[0] = RFs::GetSystemDriveChar();
1.324 +
1.325 + TInt err = TheFsSession.Connect();
1.326 + TEST2(err, KErrNone);
1.327 +
1.328 + TheTest.Title();
1.329 + TheTest.Start(_L("Task Scheduler TTsTime unit tests"));
1.330 + TRAP(err, ::RunTestsL())
1.331 + TEST2(err, KErrNone);
1.332 +
1.333 + (void)TheFsSession.Delete(fileName);
1.334 + TheFsSession.Close();
1.335 + TheTest.End();
1.336 + TheTest.Close();
1.337 +
1.338 + __UHEAP_MARKEND;
1.339 +
1.340 + delete tc;
1.341 +
1.342 + return(KErrNone);
1.343 +
1.344 + }
1.345 +