pimappservices/calendar/tsrc/t_utc_conversions.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-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 // This test creates vCals from the text specified below, then imports this into a calendar entry.
       
    15 // This entry is then exported, and re-imported, then that re-imported entry is compared with the 
       
    16 // original imported entry to check they are the same.
       
    17 // The entry comparison focuses on timezone settings and daylight saving rules.
       
    18 // 
       
    19 //
       
    20 
       
    21 #include <e32test.h>
       
    22 #include <s32file.h>
       
    23 #include <calrrule.h>
       
    24 #include <calentryview.h>
       
    25 #include <calalarm.h>
       
    26 
       
    27 #include "caltestlib.h"
       
    28 
       
    29 #include <coreappstest/testserver.h>
       
    30 
       
    31 static RTest test(_L("t_utc_conversions"));
       
    32 
       
    33 const TDateTime KFeb1st_00_00(2005, EFebruary, 0, 0, 0, 0, 0);
       
    34 const TDateTime KFeb1st_23_59(2005, EFebruary, 0, 23, 59, 0, 0);
       
    35 	
       
    36 _LIT(KImportFile,"c:t_utc_conversions");
       
    37 
       
    38 _LIT8(KTimeZoneTokyo, "Asia/Tokyo");
       
    39 const TInt KHoursFromUTCInWinterInTokyo = 9;
       
    40 
       
    41 const TDateTime KFeb1st_12_00(2005, EFebruary, 0, 12, 0, 0, 0);
       
    42 const TDateTime KMar1st_12_00(2005, EMarch, 0, 12, 0, 0, 0);
       
    43 const TDateTime KJul1st_12_00(2005, EJuly, 0, 12, 0, 0, 0);
       
    44 const TDateTime KAug1st_12_00(2005, EAugust, 0, 12, 0, 0, 0);
       
    45 
       
    46 const TTimeIntervalMinutes KAlarmOffset(15);
       
    47 
       
    48 
       
    49 class CTestApp : public CBase
       
    50 	{
       
    51 public:
       
    52 	static CTestApp* NewL();
       
    53 	~CTestApp();
       
    54 	
       
    55 	void StartL();
       
    56 		
       
    57 private:
       
    58 	void TestApptL();
       
    59 	void TestEventL();
       
    60 	void TestTodoL();
       
    61 	void TestRptDefL();
       
    62 	void TestAgnExceptionL();
       
    63 	void TestTimesL(CCalEntry& aEntry);
       
    64 	
       
    65 	void ConstructL();
       
    66 	
       
    67 private:
       
    68 	TBuf8<256> iOriginalTz;
       
    69 	
       
    70 	CCalTestLibrary* iCalTestLib;
       
    71 	
       
    72 	RPIMTestServer iPIMTest;
       
    73 	
       
    74 	TInt8 iTestStep;
       
    75 	};
       
    76 
       
    77 CTestApp::~CTestApp()
       
    78 	{
       
    79 	if (iCalTestLib)
       
    80 		{
       
    81 		TRAP_IGNORE(iCalTestLib->SetTimeZoneL(iOriginalTz));
       
    82 		}
       
    83 	delete iCalTestLib;
       
    84 	}
       
    85 	
       
    86 CTestApp* CTestApp::NewL()
       
    87 	{
       
    88 	CTestApp* self = new (ELeave) CTestApp;
       
    89 	CleanupStack::PushL(self);
       
    90 	self->ConstructL();
       
    91 	CleanupStack::Pop(self);
       
    92 	return self;
       
    93 	}
       
    94 
       
    95 void CTestApp::ConstructL()
       
    96 	{
       
    97 	iCalTestLib = CCalTestLibrary::NewL();
       
    98 	iCalTestLib->ReplaceFileL(KImportFile);
       
    99 	iCalTestLib->OpenFileL(KImportFile);
       
   100 	
       
   101 	User::LeaveIfError(iPIMTest.Connect());
       
   102 	iPIMTest.GetTimeZoneL(iOriginalTz);
       
   103 	iCalTestLib->SetTimeZoneL(KTimeZoneTokyo());
       
   104 	}
       
   105 	
       
   106 void CTestApp::StartL()
       
   107 	{
       
   108 	TestApptL();
       
   109 	TestEventL();
       
   110 	TestTodoL();
       
   111 	TestRptDefL();
       
   112 	TestAgnExceptionL();
       
   113 	}
       
   114 
       
   115 void CTestApp::TestTimesL(CCalEntry& aEntry)
       
   116 	{
       
   117 	// set local start and end times to 12:00 (UTC should be 03:00)
       
   118 	TCalTime startTime;
       
   119 	startTime.SetTimeLocalL(KFeb1st_12_00);
       
   120 	TCalTime endTime;
       
   121 	endTime.SetTimeLocalL(KMar1st_12_00);
       
   122 	
       
   123 	aEntry.SetStartAndEndTimeL(startTime, endTime);
       
   124 	
       
   125 	TTimeIntervalHours hours(0);
       
   126 	TTime(KFeb1st_12_00).HoursFrom(aEntry.StartTimeL().TimeUtcL(), hours);
       
   127 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   128 	
       
   129 	TTime(KMar1st_12_00).HoursFrom(aEntry.EndTimeL().TimeUtcL(), hours);
       
   130 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   131 	
       
   132 	TTime(KFeb1st_12_00).HoursFrom(aEntry.StartTimeL().TimeLocalL(), hours);
       
   133 	test(hours.Int() == 0);
       
   134 	
       
   135 	TTime(KMar1st_12_00).HoursFrom(aEntry.EndTimeL().TimeLocalL(), hours);
       
   136 	test(hours.Int() == 0);
       
   137 	
       
   138 	// set UTC start and end times to 12:00 (local should be 21:00)
       
   139 	startTime.SetTimeUtcL(KFeb1st_12_00);
       
   140 	endTime.SetTimeUtcL(KMar1st_12_00);
       
   141 	aEntry.SetStartAndEndTimeL(startTime, endTime);
       
   142 	
       
   143 	TTime(KFeb1st_12_00).HoursFrom(aEntry.StartTimeL().TimeUtcL(), hours);
       
   144 	test(hours.Int() == 0);
       
   145 	
       
   146 	TTime(KMar1st_12_00).HoursFrom(aEntry.EndTimeL().TimeUtcL(), hours);
       
   147 	test(hours.Int() == 0);
       
   148 	
       
   149 	TTime(KFeb1st_12_00).HoursFrom(aEntry.StartTimeL().TimeLocalL(), hours);
       
   150 	test(hours.Int() == -KHoursFromUTCInWinterInTokyo);
       
   151 	
       
   152 	TTime(KMar1st_12_00).HoursFrom(aEntry.EndTimeL().TimeLocalL(), hours);
       
   153 	test(hours.Int() == -KHoursFromUTCInWinterInTokyo);
       
   154 	}
       
   155 
       
   156 void CTestApp::TestApptL()
       
   157 	{
       
   158 	HBufC8* guid = NULL;
       
   159 	CCalEntry* appt = iCalTestLib->CreateCalEntryL(CCalEntry::EAppt, guid);
       
   160 	CleanupStack::PushL(appt);
       
   161 	
       
   162 	TestTimesL(*appt);
       
   163 	
       
   164 	CleanupStack::PopAndDestroy(appt);
       
   165 	}
       
   166 
       
   167 void CTestApp::TestEventL()
       
   168 	{
       
   169 	HBufC8* guid = NULL;
       
   170 	CCalEntry* event = iCalTestLib->CreateCalEntryL(CCalEntry::EEvent, guid);
       
   171 	CleanupStack::PushL(event);
       
   172 	
       
   173 	TestTimesL(*event);
       
   174 	
       
   175 	CleanupStack::PopAndDestroy(event);
       
   176 	}
       
   177 
       
   178 void CTestApp::TestTodoL()
       
   179 	{
       
   180 	HBufC8* guid = NULL;
       
   181 	CCalEntry* todo = iCalTestLib->CreateCalEntryL(CCalEntry::ETodo, guid);
       
   182 	CleanupStack::PushL(todo);
       
   183 	
       
   184 	TestTimesL(*todo);
       
   185 	
       
   186 	// set local crossed out time to 12:00 (UTC should be 03:00)
       
   187 	TCalTime completedTime;
       
   188 	completedTime.SetTimeLocalL(KFeb1st_12_00);
       
   189 	todo->SetCompletedL(ETrue, completedTime);
       
   190 	
       
   191 	TTimeIntervalHours hours(0);
       
   192 	TTime(KFeb1st_12_00).HoursFrom(todo->CompletedTimeL().TimeUtcL(), hours);
       
   193 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   194 	
       
   195 	TTime(KFeb1st_12_00).HoursFrom(todo->CompletedTimeL().TimeLocalL(), hours);
       
   196 	test(hours.Int() == 0);
       
   197 	
       
   198 	// set UTC crossed out time to 12:00 (local should be 21:00)
       
   199 	completedTime.SetTimeUtcL(KFeb1st_12_00);
       
   200 	todo->SetCompletedL(ETrue, completedTime);
       
   201 	TTime(KFeb1st_12_00).HoursFrom(todo->CompletedTimeL().TimeUtcL(), hours);
       
   202 	test(hours.Int() == 0);
       
   203 	
       
   204 	TTime(KFeb1st_12_00).HoursFrom(todo->CompletedTimeL().TimeLocalL(), hours);
       
   205 	test(hours.Int() == -KHoursFromUTCInWinterInTokyo);
       
   206 	
       
   207 	CleanupStack::PopAndDestroy(todo);
       
   208 	}
       
   209 	
       
   210 void CTestApp::TestRptDefL()
       
   211 	{
       
   212 	TCalRRule rRule;
       
   213 	rRule.SetType(TCalRRule::EDaily);
       
   214 	
       
   215 	// set local start time to 12:00 (UTC should be 03:00)
       
   216 	TCalTime startTime;
       
   217 	startTime.SetTimeLocalL(KFeb1st_12_00);
       
   218 	rRule.SetDtStart(startTime);
       
   219 	
       
   220 	TTimeIntervalHours hours(0);
       
   221 	TTime(KFeb1st_12_00).HoursFrom(rRule.DtStart().TimeUtcL(), hours);
       
   222 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   223 	
       
   224 	TTime(KFeb1st_12_00).HoursFrom(rRule.DtStart().TimeLocalL(), hours);
       
   225 	test(hours.Int() == 0);
       
   226 	
       
   227 	// set UTC start time to 12:00 (local should be 21:00)
       
   228 	startTime.SetTimeUtcL(KFeb1st_12_00);
       
   229 	rRule.SetDtStart(startTime);
       
   230 	TTime(KFeb1st_12_00).HoursFrom(rRule.DtStart().TimeUtcL(), hours);
       
   231 	test(hours.Int() == 0);
       
   232 	
       
   233 	TTime(KFeb1st_12_00).HoursFrom(rRule.DtStart().TimeLocalL(), hours);
       
   234 	test(hours.Int() == -KHoursFromUTCInWinterInTokyo);
       
   235 	
       
   236 	// set local end time to 12:00 (UTC should be 03:00)
       
   237 	TCalTime endTime;
       
   238 	endTime.SetTimeLocalL(KFeb1st_12_00);
       
   239 	rRule.SetUntil(endTime);
       
   240 	
       
   241 	TTime(KFeb1st_12_00).HoursFrom(rRule.Until().TimeUtcL(), hours);
       
   242 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   243 	
       
   244 	TTime(KFeb1st_12_00).HoursFrom(rRule.Until().TimeLocalL(), hours);
       
   245 	test(hours.Int() == 0);
       
   246 	
       
   247 	// set UTC end time to 12:00 (UTC should be 21:00)
       
   248 	endTime.SetTimeUtcL(KFeb1st_12_00);
       
   249 	rRule.SetUntil(endTime);
       
   250 	TTime(KFeb1st_12_00).HoursFrom(rRule.Until().TimeUtcL(), hours);
       
   251 	test(hours.Int() == 0);
       
   252 	
       
   253 	TTime(KFeb1st_12_00).HoursFrom(rRule.Until().TimeLocalL(), hours);
       
   254 	test(hours.Int() == -KHoursFromUTCInWinterInTokyo);
       
   255 	}
       
   256 	
       
   257 void CTestApp::TestAgnExceptionL()
       
   258 	{
       
   259 	HBufC8* guid = NULL;
       
   260 	CCalEntry* entry = iCalTestLib->CreateCalEntryL(CCalEntry::EEvent, guid);
       
   261 	CleanupStack::PushL(entry);
       
   262 	
       
   263 	TCalTime entryStartTime;
       
   264 	entryStartTime.SetTimeLocalL(KFeb1st_12_00);
       
   265 	TCalTime entryEndTime;
       
   266 	entryEndTime.SetTimeLocalL(TTime(KFeb1st_12_00) + TTimeIntervalHours(1));
       
   267 	
       
   268 	entry->SetStartAndEndTimeL(entryStartTime, entryEndTime);
       
   269 	
       
   270 	// Add a 'local time' repeat rule to the entry
       
   271 	TCalRRule rRuleLocal;
       
   272 	rRuleLocal.SetType(TCalRRule::EDaily);
       
   273 	
       
   274 	TCalTime startTime;
       
   275 	startTime.SetTimeLocalL(KFeb1st_12_00);
       
   276 	rRuleLocal.SetDtStart(startTime);
       
   277 	rRuleLocal.SetCount(3); // for 3 days
       
   278 	
       
   279 	entry->SetRRuleL(rRuleLocal);
       
   280 		
       
   281 	// create a 'local time' exception list
       
   282 	RArray<TCalTime> exceptions;
       
   283 	CleanupClosePushL(exceptions);
       
   284 	
       
   285 	TCalTime exceptionTime;
       
   286 	exceptionTime.SetTimeLocalL(TTime(KFeb1st_12_00));
       
   287 	exceptions.AppendL(exceptionTime);
       
   288 	
       
   289 	entry->SetExceptionDatesL(exceptions);
       
   290 	exceptions.Reset();
       
   291 	entry->GetExceptionDatesL(exceptions);
       
   292 	
       
   293 	TTimeIntervalHours hours(0);
       
   294 	TTime(KFeb1st_12_00).HoursFrom(exceptions[0].TimeLocalL(), hours);
       
   295 	test(hours.Int() == 0);
       
   296 	exceptions.Reset();
       
   297 	
       
   298 	entry->ClearRepeatingPropertiesL();
       
   299 	
       
   300 	// Add a 'utc' repeat rule to the entry
       
   301 	TCalRRule rRuleUtc;
       
   302 	rRuleUtc.SetType(TCalRRule::EDaily);
       
   303 	
       
   304 	startTime.SetTimeUtcL(KFeb1st_12_00);
       
   305 	rRuleUtc.SetDtStart(startTime);
       
   306 	rRuleUtc.SetCount(3); // for 3 days
       
   307 	
       
   308 	entry->SetRRuleL(rRuleUtc);
       
   309 	
       
   310 	// set UTC exception time to 12:00 (local should be 21:00)
       
   311 	exceptionTime.SetTimeUtcL(TTime(KFeb1st_12_00) - TTimeIntervalHours(12));
       
   312 	exceptions.AppendL(exceptionTime);
       
   313 	
       
   314 	entry->SetExceptionDatesL(exceptions);
       
   315 	exceptions.Reset();
       
   316 	entry->GetExceptionDatesL(exceptions);
       
   317 	
       
   318 	TTime(KFeb1st_12_00).HoursFrom(exceptions[0].TimeUtcL(), hours);
       
   319 	test(hours.Int() == KHoursFromUTCInWinterInTokyo);
       
   320 	
       
   321 	CleanupStack::PopAndDestroy(); //exceptions.Close
       
   322 	CleanupStack::PopAndDestroy(entry);
       
   323 	}
       
   324 	
       
   325 static void doMainL()
       
   326 	{
       
   327 	CTestApp* testApp = CTestApp::NewL();
       
   328 	CleanupStack::PushL(testApp);
       
   329 	
       
   330 	testApp->StartL();
       
   331 	
       
   332 	CleanupStack::PopAndDestroy(testApp);
       
   333 	}
       
   334 
       
   335 
       
   336 /**
       
   337 
       
   338 @SYMTestCaseID     PIM-T-UTC-CONVERSIONS-0001
       
   339 
       
   340 */
       
   341 
       
   342 TInt E32Main()
       
   343 	{
       
   344 	__UHEAP_MARK;
       
   345 	test.Start(_L("@SYMTESTCaseID:PIM-T-UTC-CONVERSIONS-0001 t_utc_conversions"));
       
   346 
       
   347 	test.Title();
       
   348 	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
       
   349 	CActiveScheduler::Install(scheduler);
       
   350 	CTrapCleanup* theCleanup=CTrapCleanup::New();
       
   351 	TRAPD(ret,doMainL());
       
   352 	test(ret==KErrNone);
       
   353 	delete theCleanup;
       
   354 	delete scheduler;
       
   355 	test.End();
       
   356 	test.Close();
       
   357 	__UHEAP_MARKEND;
       
   358 	return(KErrNone);
       
   359 	}
       
   360