os/ossrv/genericservices/taskscheduler/Test/TTsTimeUnitTests/TU_TSCH_ttstime.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "SCHTIME.H"
sl@0
    17
sl@0
    18
#include <e32base.h>
sl@0
    19
#include <e32test.h>
sl@0
    20
#include <f32file.h>
sl@0
    21
#include <s32file.h>
sl@0
    22
sl@0
    23
#include <e32std.h>
sl@0
    24
#include <tz.h>
sl@0
    25
sl@0
    26
#include "Thelpers.h"
sl@0
    27
sl@0
    28
sl@0
    29
_LIT(KTestName,	"TTsTime Tests");
sl@0
    30
RTest	TheTest(KTestName);
sl@0
    31
RTz		TheTzServer;
sl@0
    32
 
sl@0
    33
static RFs			TheFsSession;
sl@0
    34
_LIT(KFileName,"_:\\ttstime.dat");
sl@0
    35
static TBuf<32> fileName;
sl@0
    36
sl@0
    37
sl@0
    38
//
sl@0
    39
//
sl@0
    40
//Test macros and functions
sl@0
    41
sl@0
    42
static void Check(TInt aValue, TInt aLine)
sl@0
    43
	{
sl@0
    44
	if(!aValue)
sl@0
    45
		{
sl@0
    46
		(void)TheFsSession.Delete(fileName);
sl@0
    47
		TheTest(EFalse, aLine);
sl@0
    48
		}
sl@0
    49
	}
sl@0
    50
static  void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
    51
	{
sl@0
    52
	if(aValue != aExpected)
sl@0
    53
		{
sl@0
    54
		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
    55
		(void)TheFsSession.Delete(fileName);
sl@0
    56
		TheTest(EFalse, aLine);
sl@0
    57
		}
sl@0
    58
	}
sl@0
    59
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
    60
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
    61
sl@0
    62
/**
sl@0
    63
@file
sl@0
    64
@SYMTestCaseID				SYSLIB-SCHSVR-CT-0204
sl@0
    65
@SYMTestCaseDesc 			Check that the default constructor works properly
sl@0
    66
@SYMTestPriority 			low
sl@0
    67
@SYMTestActions  			Creates an instance of TTsTime and check its data
sl@0
    68
@SYMTestExpectedResults		The test must not fail.
sl@0
    69
@SYMPREQ					PREQ234
sl@0
    70
*/
sl@0
    71
static void TestsWithDefaultContructorL()
sl@0
    72
	{
sl@0
    73
	//	Tests with default constructor:
sl@0
    74
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0204 TTsTime() default constructor, TTsTime is UTC time by default "));
sl@0
    75
	TTsTime tsTime; //defalut constructor, TTsTime is UTC	
sl@0
    76
	
sl@0
    77
	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0
    78
sl@0
    79
	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0
    80
	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0
    81
	TTime time(date);	
sl@0
    82
	TInt err = SchSvrHelpers::SetUTCTimeL(time); 	
sl@0
    83
	TEST(err == 0);
sl@0
    84
	
sl@0
    85
	
sl@0
    86
	// check that member data are set properly using GetUtcTime() and GetLocalTime()
sl@0
    87
	TTime utcTime = TTime(tsTime.GetUtcTime());
sl@0
    88
	TTime localTime = TTime(tsTime.GetLocalTime());
sl@0
    89
	TDateTime testDate(0, EJanuary, 0, 1, 0, 0, 0);
sl@0
    90
	TTime testTime(testDate);
sl@0
    91
	TEST (localTime == testTime);	
sl@0
    92
	
sl@0
    93
	// check the offset
sl@0
    94
	// because we set the current time zone to Europe/London and 
sl@0
    95
	// ProcessOffsetEvent has been called in GetUtcTime, and since TTsTime is UTC by defalu then 
sl@0
    96
	// TTsTime::iOffset remains the same
sl@0
    97
	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset(); 
sl@0
    98
	TEST(tsTimeOffset == TTimeIntervalSeconds(0));
sl@0
    99
	}
sl@0
   100
	
sl@0
   101
/**
sl@0
   102
@file
sl@0
   103
@SYMTestCaseID				SYSLIB-SCHSVR-CT-0205
sl@0
   104
@SYMTestCaseDesc 			Checks that the second constructor works properly
sl@0
   105
@SYMTestPriority 			low
sl@0
   106
@SYMTestActions  			Creates an instance of TTsTime and check its data
sl@0
   107
@SYMTestExpectedResults		The test must not fail.
sl@0
   108
@SYMPREQ					PREQ234
sl@0
   109
*/	
sl@0
   110
static void TestWithSecondConstructorAndUtcL()
sl@0
   111
	{
sl@0
   112
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0205 TTsTime - test for 2nd constructor "));
sl@0
   113
	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0
   114
sl@0
   115
	//set the current utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0
   116
	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0
   117
	TTime time(date);
sl@0
   118
	TInt err = SchSvrHelpers::SetUTCTimeL(time); 
sl@0
   119
	TEST(err == 0);
sl@0
   120
	
sl@0
   121
	//	Tests with overloaded constructor 
sl@0
   122
	TheTest.Next(_L("Test TTsTime with overloaded operator and TTsTime is UTC"));
sl@0
   123
	TTsTime tsTime(time, ETrue); //Sets time to UTC time as boolean is ETrue
sl@0
   124
	TTime utcTime = tsTime.GetUtcTime(); 
sl@0
   125
	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0
   126
	TBool isUtc = tsTime.IsUtc();
sl@0
   127
	TEST(utcTime == time);
sl@0
   128
	TEST(tsTimeOffset == TTimeIntervalSeconds(0)); //because tsTime is UTC so its offset is zero
sl@0
   129
	TEST(isUtc); 
sl@0
   130
	}
sl@0
   131
sl@0
   132
/**
sl@0
   133
@file
sl@0
   134
@SYMTestCaseID				SYSLIB-SCHSVR-CT-0206
sl@0
   135
@SYMTestCaseDesc 			Check TTsTime::SetHomeTime works properly
sl@0
   136
@SYMTestPriority 			low
sl@0
   137
@SYMTestActions  			Creates an instance of TTsTime, sets its values to home time and verifies them 
sl@0
   138
@SYMTestExpectedResults		The test must not fail.
sl@0
   139
@SYMPREQ					PREQ234
sl@0
   140
*/
sl@0
   141
static void TestSetHomeTimeL()
sl@0
   142
	{
sl@0
   143
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0206 TTsTime::SetHomeTime test "));	
sl@0
   144
	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0
   145
sl@0
   146
	// set the date and time of the device
sl@0
   147
	TheTest.Next(_L("Test TTsTime is Local Time"));
sl@0
   148
	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0
   149
	TTime time(date);
sl@0
   150
	TInt err = SchSvrHelpers::SetUTCTimeL(time);
sl@0
   151
	TEST(err == 0);
sl@0
   152
	
sl@0
   153
	//set date and time of TTsTime to home time
sl@0
   154
	date =  TDateTime(2005, EMay, 15, 9, 55, 0, 0);
sl@0
   155
	time = date;
sl@0
   156
	TTsTime tsTime;
sl@0
   157
	tsTime.SetLocalTime(time);
sl@0
   158
	
sl@0
   159
	TTime localTime = tsTime.GetLocalTime(); 
sl@0
   160
	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0
   161
	TBool isUtc = tsTime.IsUtc();
sl@0
   162
sl@0
   163
	// check that the returned local time is the same as the one we set earlier
sl@0
   164
	TEST(localTime == time );
sl@0
   165
sl@0
   166
	// check now that the stored utc time in TTsTime is correct
sl@0
   167
	TTime utcTime = tsTime.GetUtcTime();
sl@0
   168
	date =  TDateTime(2005, EMay, 15, 8, 55, 0, 0); 	
sl@0
   169
	time = TTime(date);
sl@0
   170
	TEST(utcTime == time );
sl@0
   171
sl@0
   172
	// check offset - 3600 seconds (1 hour) because time of device is in BST
sl@0
   173
	TEST(tsTimeOffset == TTimeIntervalSeconds(3600));
sl@0
   174
	TEST(!isUtc); 	//This TTsTime is not UTC.
sl@0
   175
	}
sl@0
   176
	
sl@0
   177
/**
sl@0
   178
@file
sl@0
   179
@SYMTestCaseID				SYSLIB-SCHSVR-CT-0207
sl@0
   180
@SYMTestCaseDesc 			Checks ProcessOffsetEvent 
sl@0
   181
@SYMTestPriority 			low
sl@0
   182
@SYMTestActions  			Creates an instance of TTsTime, changes timezone, and checks that the TTsTime object is updated correctly.
sl@0
   183
@SYMTestExpectedResults		The test must not fail.
sl@0
   184
@SYMPREQ					PREQ234
sl@0
   185
*/
sl@0
   186
static void TestProcessOffsetEventL()
sl@0
   187
	{
sl@0
   188
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0207 ProcessOffsetEvent Test "));
sl@0
   189
	TheTest.Printf(_L("Timezone set to Europe, London, DST on"));	
sl@0
   190
	
sl@0
   191
	//set the current home time to 8.55am, 15 May 2005
sl@0
   192
	//Daylight savings do apply on this date
sl@0
   193
	//so utc time should be 7.55
sl@0
   194
	TDateTime date(2005, EMay, 15, 8, 55, 0, 0);
sl@0
   195
	TTime time(date);	
sl@0
   196
	TInt err = SchSvrHelpers::SetHomeTimeL(time);
sl@0
   197
	TEST(err == KErrNone);
sl@0
   198
		
sl@0
   199
	//Check the ProcessOffsetEvent () method and print iUtcTime, iOffset
sl@0
   200
	//create hometime based ttstime object
sl@0
   201
	//should have 9.55 in iUtcTime
sl@0
   202
	date = TDateTime(2005, EMay, 15, 10, 55, 0, 0);
sl@0
   203
	time = date;
sl@0
   204
	TTsTime tsTime(time, EFalse);
sl@0
   205
sl@0
   206
	// remember current offset
sl@0
   207
	TTimeIntervalSeconds savedOffset = User::UTCOffset();
sl@0
   208
	
sl@0
   209
	// Increase offset by 1Hr by moving to Paris
sl@0
   210
	TheTest.Printf(_L("Move timezone to Paris, Europe. Check TTsTime updated accordingly"));
sl@0
   211
	CTzId* tzParisId = CTzId::NewL(2656);
sl@0
   212
	CleanupStack::PushL(tzParisId);
sl@0
   213
	TheTzServer.SetTimeZoneL(*tzParisId);
sl@0
   214
	
sl@0
   215
	//get new UTC offset
sl@0
   216
	TTimeIntervalSeconds newOffset = User::UTCOffset();
sl@0
   217
	
sl@0
   218
	// call ProcessOffsetEvent, should put 8:55 in iUtcTime 
sl@0
   219
	tsTime.ProcessOffsetEvent();
sl@0
   220
	
sl@0
   221
	// check the updated iUtcTime
sl@0
   222
	TTime utcTime = tsTime.GetUtcTime();
sl@0
   223
	TDateTime a = utcTime.DateTime();
sl@0
   224
	date = TDateTime(2005, EMay, 15, 8, 55, 0, 0);
sl@0
   225
	time = date;
sl@0
   226
	TEST(utcTime == time);
sl@0
   227
	
sl@0
   228
	// check the updated offset
sl@0
   229
	TTimeIntervalSeconds tsTimeOffset = tsTime.GetOffset();
sl@0
   230
	TEST(tsTimeOffset == newOffset);
sl@0
   231
	
sl@0
   232
	CleanupStack::PopAndDestroy(tzParisId);
sl@0
   233
	
sl@0
   234
	//return Timezone to London, Europe for other tests
sl@0
   235
	CTzId* tzLondonId = CTzId::NewL(2592); 
sl@0
   236
	CleanupStack::PushL(tzLondonId);
sl@0
   237
	TheTzServer.SetTimeZoneL(*tzLondonId);
sl@0
   238
	CleanupStack::PopAndDestroy(tzLondonId);
sl@0
   239
	}
sl@0
   240
sl@0
   241
/**
sl@0
   242
@file
sl@0
   243
@SYMTestCaseID				SYSLIB-SCHSVR-CT-0208
sl@0
   244
@SYMTestCaseDesc 			Checks ExternalizeL and InternalizeL
sl@0
   245
@SYMTestPriority 			low
sl@0
   246
@SYMTestActions  			Creates an instance of TTsTime, Externalizes 
sl@0
   247
							it and internaziles and checks that teh data is the same
sl@0
   248
@SYMTestExpectedResults		The test must not fail.
sl@0
   249
@SYMPREQ					PREQ234
sl@0
   250
*/	
sl@0
   251
static void TestExternalizeInternalizeL()
sl@0
   252
	{
sl@0
   253
	// time is UTC
sl@0
   254
	TTsTime time(TTime(5), ETrue);
sl@0
   255
sl@0
   256
	//	Test externalise 
sl@0
   257
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0208 Test TTsTime::ExternalizeL "));
sl@0
   258
	RFile file;
sl@0
   259
sl@0
   260
	TFileName testFileName(fileName);
sl@0
   261
	TEST2(file.Replace(TheFsSession,testFileName,EFileWrite|EFileRead), KErrNone);
sl@0
   262
	RFileBuf buf;
sl@0
   263
	CleanupClosePushL(buf);
sl@0
   264
	buf.Attach(file);
sl@0
   265
	RWriteStream stream(&buf);
sl@0
   266
	//Externalize the time
sl@0
   267
	stream << time;
sl@0
   268
	buf.SynchL();
sl@0
   269
	CleanupStack::PopAndDestroy(&buf);
sl@0
   270
	
sl@0
   271
	//	Test internalise 
sl@0
   272
	TheTest.Next(_L("Test TTsTime::InternalizeL"));
sl@0
   273
	TTsTime newTime;
sl@0
   274
	TInt err = file.Open(TheFsSession,testFileName,EFileRead);
sl@0
   275
	TEST2(err, KErrNone);
sl@0
   276
	RFileBuf buf2;
sl@0
   277
	CleanupClosePushL(buf2);
sl@0
   278
	buf2.Attach(file);
sl@0
   279
	RReadStream stream2(&buf2);
sl@0
   280
sl@0
   281
	//Internalize the time
sl@0
   282
	stream2 >> newTime; //the externelized time is UTC time, same as the externalized time
sl@0
   283
	TTime utcTime = newTime.GetUtcTime();
sl@0
   284
	TTimeIntervalSeconds offset = newTime.GetOffset();
sl@0
   285
	TBool isUtc = newTime.IsUtc();
sl@0
   286
	TEST(utcTime == TTime(5));
sl@0
   287
	TEST(offset == TTimeIntervalSeconds(0));
sl@0
   288
	TEST(isUtc);
sl@0
   289
	CleanupStack::PopAndDestroy(&buf2);	
sl@0
   290
	}
sl@0
   291
	
sl@0
   292
	
sl@0
   293
static void RunTestsL()
sl@0
   294
	{
sl@0
   295
	//All tests assume timezone is London, Europe.
sl@0
   296
	TheTzServer.Connect();
sl@0
   297
	CTzId* tzLondonId = CTzId::NewL(2592); 
sl@0
   298
	CleanupStack::PushL(tzLondonId);
sl@0
   299
	TheTzServer.SetTimeZoneL(*tzLondonId);
sl@0
   300
sl@0
   301
sl@0
   302
	TestsWithDefaultContructorL();
sl@0
   303
	TestWithSecondConstructorAndUtcL();
sl@0
   304
	TestSetHomeTimeL();
sl@0
   305
	TestProcessOffsetEventL();
sl@0
   306
	TestExternalizeInternalizeL();
sl@0
   307
	
sl@0
   308
	CleanupStack::PopAndDestroy(tzLondonId);
sl@0
   309
	TheTzServer.Close();
sl@0
   310
	}
sl@0
   311
sl@0
   312
GLDEF_C TInt E32Main()
sl@0
   313
 {
sl@0
   314
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   315
	TEST(tc != NULL);
sl@0
   316
	
sl@0
   317
	__UHEAP_MARK;
sl@0
   318
	
sl@0
   319
	fileName.Copy(KFileName);
sl@0
   320
	fileName[0] = RFs::GetSystemDriveChar();
sl@0
   321
sl@0
   322
	TInt err = TheFsSession.Connect();
sl@0
   323
	TEST2(err, KErrNone);
sl@0
   324
sl@0
   325
	TheTest.Title();
sl@0
   326
	TheTest.Start(_L("Task Scheduler TTsTime unit tests"));
sl@0
   327
	TRAP(err, ::RunTestsL())
sl@0
   328
	TEST2(err, KErrNone);
sl@0
   329
	
sl@0
   330
	(void)TheFsSession.Delete(fileName);
sl@0
   331
	TheFsSession.Close();
sl@0
   332
	TheTest.End();
sl@0
   333
	TheTest.Close();
sl@0
   334
sl@0
   335
	__UHEAP_MARKEND;
sl@0
   336
	
sl@0
   337
	delete tc;
sl@0
   338
	
sl@0
   339
	return(KErrNone);
sl@0
   340
	
sl@0
   341
	}
sl@0
   342