pimappservices/calendar/tsrc/caltestlib.h
changeset 0 f979ecb2b13e
child 12 38571fd2a704
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 //
       
    15 
       
    16 #ifndef __CALTESTLIB_H__
       
    17 #define __CALTESTLIB_H__
       
    18 
       
    19 #include <badesca.h>
       
    20 #include <e32base.h>
       
    21 #include <hal.h>
       
    22 #include <f32file.h>
       
    23 #include <e32test.h>
       
    24 #include <calprogresscallback.h>
       
    25 #include <calentry.h>
       
    26 #include <coreappstest/testserver.h>
       
    27 #include <caldataexchange.h>
       
    28 #include <asclisession.h>
       
    29 #include <asshdalarm.h>
       
    30 
       
    31 class CCalSession;
       
    32 class CCalEntry;
       
    33 class CCalEntryView;
       
    34 class CCalInstance;
       
    35 class CCalInstanceView;
       
    36 class CCalIter;
       
    37 
       
    38 _LIT8(KDefaultTimeZone, "Europe/London");
       
    39 
       
    40 _LIT(KCalTestLibMutex, "CalTestLib_GlobalMutex");
       
    41 
       
    42 class CCalDataExchange;
       
    43 
       
    44 class CSetTimezone : public CActive
       
    45 	{
       
    46 public:
       
    47 	static CSetTimezone* NewL(const TDesC8& aZoneId, RPIMTestServer* aPIMTestServer);
       
    48 	~CSetTimezone();
       
    49 	void Start();
       
    50 	
       
    51 	// from CActive
       
    52 	void RunL();
       
    53 	void DoCancel();
       
    54 	
       
    55 private:
       
    56 	void ConstructL(const TDesC8& aZoneId, RPIMTestServer* aPIMTestServer);
       
    57 	CSetTimezone();
       
    58 	RPIMTestServer* iPIMTestServer; // Not owned
       
    59 	HBufC8* iZoneId;
       
    60 	TInt8 iStep;
       
    61 	};
       
    62 	
       
    63 /**  This is the Calendar Test Library class for Unit testing
       
    64 @internalComponent
       
    65 @test
       
    66 */
       
    67 class CCalTestLibrary : public CBase, public MCalProgressCallBack
       
    68 	{
       
    69 public:
       
    70 
       
    71 // Implementation of MCalProgressCallBack interface
       
    72 
       
    73 	void Progress(TInt /*aPercentageCompleted*/);
       
    74 	void Completed(TInt /*aError*/);
       
    75 	TBool NotifyProgress();
       
    76 
       
    77 
       
    78 // Constructor / Destructor
       
    79 
       
    80 	// In its destructor, the current opened calendar file will be closed and deleted
       
    81 	IMPORT_C ~CCalTestLibrary();
       
    82 
       
    83 	// To be used by most of test code, if it does not contains any asynchronous task. 
       
    84 	// Use the following functions to create an object. It will start the alarm console alert server.
       
    85 	IMPORT_C static CCalTestLibrary* NewL(TBool aDelFileFlag=ETrue);
       
    86 	IMPORT_C static CCalTestLibrary* NewLC(TBool aDelFileFlag=ETrue);	
       
    87 
       
    88 
       
    89 // File handling
       
    90 
       
    91 	// Return a file handle.
       
    92 	IMPORT_C RFs& FileSession();
       
    93 
       
    94 	// Create a calendar file. It leaves if the file exists.
       
    95 	IMPORT_C void CreateFileL(const TDesC& aFileName);
       
    96 	
       
    97 	// Create a calendar file. It will overwrite the existing file.
       
    98 	IMPORT_C void ReplaceFileL(const TDesC& aFileName);
       
    99 	
       
   100 	// Open a calendar file.
       
   101 	IMPORT_C void OpenFileL(const TDesC& aFileName, TBool aDelFileFlag=EFalse);
       
   102 
       
   103 	// Delete either a file which is in server's private directory by default or any other file in public directory.
       
   104 	IMPORT_C void DeleteFileL(const TDesC& aFileName, TBool aIsCalFile = ETrue);
       
   105 
       
   106 	// Used to append the file to the delete list
       
   107 	IMPORT_C void RegisterCalFileL(const TDesC& aFileName);
       
   108 
       
   109 
       
   110 // Session and Views handling
       
   111 	
       
   112 	// To be used by both synchronous  and asynchronous test.
       
   113 	IMPORT_C CCalSession& GetSession();
       
   114 
       
   115 	// The following functions cause index building.
       
   116 	// They should be used by asynchronous test. 
       
   117 	// There is no guarantee that the views are ready to use when the function returns. 
       
   118 	// The test code should use the call back function to be notified when the building index is completed.
       
   119 	IMPORT_C CCalEntryView& AsynCGetEntryViewL();
       
   120 	IMPORT_C CCalInstanceView& AsynCGetInstanceViewL();
       
   121 
       
   122 	// The following functions cause index building.
       
   123 	// They should be used by synchronous test. 
       
   124 	// The views are ready to be used when the function returns.
       
   125 	// Those two functions can be called as many times as you like.
       
   126 	IMPORT_C CCalEntryView& SynCGetEntryViewL();
       
   127 	IMPORT_C CCalInstanceView& SynCGetInstanceViewL();	
       
   128 
       
   129 	// Delete entry and instance views
       
   130 	IMPORT_C void DeleteViews();
       
   131 
       
   132 
       
   133 // Entry handling
       
   134 
       
   135 	// Create a GS entry. The uid of the entry will be filled with some random letters if aUid is NULL, otherwise, it use the uid specified.
       
   136 	IMPORT_C CCalEntry* CreateCalEntryL(CCalEntry::TType aType, HBufC8*& aUid);
       
   137 
       
   138 	// Set start and end time according to the type of the entry. It will set an random time its start\end time if it is not specified.
       
   139 	IMPORT_C static void SetEntryStartAndEndTimeL(CCalEntry* entry, const TTime& aStartTime=Time::NullTTime(), const TTime& aEndTime=Time::NullTTime(), TInt aYear = 2000);
       
   140 	
       
   141 	// Fill the descriptor with some random letters.
       
   142 	IMPORT_C void RandomText(TDes& aText, TBool aPossibleNulDes = EFalse);
       
   143 	IMPORT_C void RandomText8(TDes8& aText,TBool aPossibleNulDes = EFalse);
       
   144 	
       
   145 	IMPORT_C void CleanDatabaseL();
       
   146 	
       
   147 	IMPORT_C CCalIter& GetIterL();
       
   148 
       
   149 // Provide access to test server for time setting and file operations
       
   150 	IMPORT_C RPIMTestServer& PIMTestServer();
       
   151 	 
       
   152 // Useful utility functions
       
   153 	IMPORT_C void StoreEntryL(const CCalEntry& aEntry);
       
   154 	IMPORT_C void FetchAllInstancesL(RPointerArray<CCalInstance>& aInstances);
       
   155 	IMPORT_C void OutputInstanceDataL(const RPointerArray<CCalInstance>& aInstances);
       
   156 
       
   157 	/* Import / Export utility functions */
       
   158 	IMPORT_C void CreateTestFileL(const TDesC8& aVCalData, const TDesC& aFileName);	
       
   159 	IMPORT_C void ImportL(const TDesC& aImportVCalendarFile, RPointerArray<CCalEntry>& aEntriesImported);
       
   160 	IMPORT_C void ImportL(const TDesC8& aVCalendar, RPointerArray<CCalEntry>& aEntriesImported);
       
   161 	IMPORT_C void ExportL(const TDesC& aExportVCalendarFile, RPointerArray<CCalEntry>& aEntriesExported);
       
   162 	
       
   163 	
       
   164 	IMPORT_C CCalDataExchange& DataExchangeL();
       
   165 	IMPORT_C CTestRegister& TestRegister();
       
   166 	
       
   167 	IMPORT_C void SetTimeZoneL(const TDesC8& aZoneName);
       
   168 	//Used for closing the agenda serve
       
   169 	IMPORT_C void CloseAgendaServer();
       
   170 	IMPORT_C static void WaitForAgendaServerClose();
       
   171 
       
   172 //Used for reconnecting the agnda server after it has been closed
       
   173 	IMPORT_C void ConnectAgendaServerL();
       
   174 
       
   175 #if __WINS__	
       
   176 	IMPORT_C static void StartAlarmServerL();	
       
   177 #endif	
       
   178 	
       
   179 	IMPORT_C TBool WaitForAlarmEvent(TAlarmId aAlarmId, TAlarmChangeEvent aExpectedEvent, TAlarmState aExpectedState, TInt aTimeout);
       
   180 	
       
   181 protected:
       
   182 
       
   183 // Constructor
       
   184 
       
   185 	// To be used by derived classes.
       
   186 	IMPORT_C CCalTestLibrary(TBool aDelFileFlag=ETrue);
       
   187 	// BaseConstructL(), which starts the alarm console alert server, should be called in the constructor of the derived class.
       
   188 	// Typically, it is the case that the test contains some asynchronous behaviour.	
       
   189 	IMPORT_C void BaseConstructL();
       
   190 
       
   191 private:
       
   192 
       
   193 // No copy constructor and assignment operator
       
   194 
       
   195 	CCalTestLibrary(CCalTestLibrary& );
       
   196 	CCalTestLibrary& operator = (const CCalTestLibrary& );
       
   197 	void OpenCalendarSessionL(TBool aRefCountExists=ETrue);
       
   198 	void CloseCalendarSession(TBool aWaitForAgendaToClose=ETrue);
       
   199 public:
       
   200 	IMPORT_C void __dbgClearTzClientCacheL(TBool aRestartCaching);	
       
   201 
       
   202 private:
       
   203 
       
   204 // Member data
       
   205 
       
   206 	RPIMTestServer		iPIMTestServer;
       
   207 	CCalSession*		iSession;
       
   208 	CCalInstanceView*	iInstanceView;
       
   209 	CCalEntryView*		iEntryView;
       
   210 	CCalIter*			iIter;
       
   211 	CCalDataExchange*	iDataExchange;
       
   212 	CTestRegister*		iTestRegister;
       
   213 	TInt64				iSeed;
       
   214 	RFs					iFs;
       
   215 	RChunk				iChunk;
       
   216 	TInt*				iRefCount;
       
   217 	TInt				iCompletedError;
       
   218 
       
   219 	CDesCArray*			iFilesToDel;
       
   220 	TBool				iDelFileFlag;
       
   221 	RMutex				iChunkMutex;
       
   222 	RASCliSession		iAlarmServer;
       
   223 	};
       
   224 
       
   225 
       
   226 /** Template class for cleanup that needs ResetAndDestroy or Reset to be called
       
   227 
       
   228 @internalComponent
       
   229 @test
       
   230 
       
   231 @code
       
   232 
       
   233 To be used in the following way:
       
   234 
       
   235 	// array to store the loaded instances
       
   236 	
       
   237 	RPointerArray<CCalInstance> instanceArray;
       
   238 	CleanupResetAndDestroyPushL(instanceArray);
       
   239 
       
   240 
       
   241 	iTestLibrary->SynCGetInstanceViewL().FindInstanceL(instanceArray, CalCommon::EIncludeAll, timeRange);
       
   242 
       
   243 	
       
   244 	CleanupStack::PopAndDestroy(&instanceArray);
       
   245 
       
   246 @endcode
       
   247 */
       
   248 
       
   249 template<class T> class CleanupResetAndDestroy
       
   250 	{
       
   251 public:
       
   252 	static inline void PushL(T& aRef);
       
   253 private:
       
   254 	static void ResetAndDestroy(TAny* aPtr);
       
   255 	};
       
   256 
       
   257 // CleanupResetAndDestroy
       
   258 	
       
   259 template <class T> inline void CleanupResetAndDestroy<T>::PushL(T& aRef)
       
   260 	{
       
   261 	CleanupStack::PushL(TCleanupItem(&ResetAndDestroy, &aRef));
       
   262 	}
       
   263 	
       
   264 template <class T> void CleanupResetAndDestroy<T>::ResetAndDestroy(TAny *aPtr)
       
   265 	{
       
   266 	(static_cast<T*>(aPtr))->ResetAndDestroy();
       
   267 	}
       
   268 
       
   269 template <class T> inline void CleanupResetAndDestroyPushL(T& aRef)
       
   270 	{
       
   271 	CleanupResetAndDestroy<T>::PushL(aRef);
       
   272 	}	
       
   273 	
       
   274 
       
   275 /** Performance timer measurement class
       
   276 
       
   277 @internalComponent
       
   278 @test
       
   279 
       
   280 @code
       
   281 
       
   282 To be used in the following way:
       
   283 
       
   284 	static RTest test(_L("The test"));
       
   285 	
       
   286 	
       
   287 	<snip>
       
   288 
       
   289 
       
   290 	TPerformanceTimer timer(test);
       
   291 
       
   292 	timer.Start();
       
   293 		
       
   294 	<blah blah>
       
   295 		
       
   296 	timer.Stop();
       
   297 	
       
   298 	test.Printf(_L("Done\n"));	
       
   299 	
       
   300 	// printout the time it took to do <blah blah>
       
   301 	
       
   302 	timer.PrintOut();
       
   303 
       
   304 @endcode	
       
   305 */
       
   306 
       
   307 class TPerformanceTimer
       
   308 	{
       
   309 public:
       
   310 	IMPORT_C TPerformanceTimer(RTest& aTest);
       
   311 
       
   312 	IMPORT_C void Start();
       
   313 	IMPORT_C void Stop();
       
   314 	IMPORT_C void PrintOut() const;
       
   315 	IMPORT_C TTimeIntervalMicroSeconds32 ElapsedTime() const;
       
   316 	
       
   317 private:
       
   318 	RTest& iTest;
       
   319 	TUint iStartTickCount;
       
   320 	TUint iEndTickCount;
       
   321 	};
       
   322 
       
   323 #endif