pimappservices/calendar/tsrc/tcal_iterator.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "caltestlib.h"
       
    17 #include <calentry.h>
       
    18 #include <calentryview.h>
       
    19 #include <calsession.h>
       
    20 #include <e32test.h>
       
    21 #include <caliterator.h>
       
    22 #include <caluser.h>
       
    23 
       
    24 // the objective of this test suite is to test the CCalIter interface and object behaviour
       
    25 
       
    26 _LIT(KTestName,"tcal_iterator");
       
    27 
       
    28 RTest test(KTestName);
       
    29 
       
    30 
       
    31 const TInt KNumberOfEntries = 20;
       
    32 
       
    33 class CIteratorTestManager : public CBase
       
    34 	{
       
    35 public:
       
    36 
       
    37     static CIteratorTestManager* NewLC();
       
    38 	~CIteratorTestManager();
       
    39 	
       
    40 	inline void CleanDatabaseL();
       
    41 	void TestEmptyIteratorL();
       
    42 	void TestAddEntryL(const TInt aNumToAdd);	
       
    43 	void TestIterEntryAndFetchL(const TInt aNumOfEntries);
       
    44 	void IterEntryAndFetchL(const TInt aNumOfIterations);
       
    45 	void TestIterEntryAndFetchAndAddL(const TInt aNumOfIterations, const TInt aNumToAdd, const TInt aNumOfEntries);
       
    46 	void TestIterEntryAndFetchAndDeleteL(const TInt aNumOfIterations, const TInt aNumToRemove, const TInt aNumOfEntries);
       
    47 	
       
    48 	
       
    49 private:
       
    50 
       
    51 	// avoid any method but NewL instancing the class
       
    52 	CIteratorTestManager() {	}
       
    53 	// no copy constructor and assignment operator
       
    54 	CIteratorTestManager(CIteratorTestManager& );
       
    55 	CIteratorTestManager& operator = (const CIteratorTestManager& );
       
    56 
       
    57 	void ConstructL();
       
    58 
       
    59 	
       
    60 private:
       
    61 
       
    62 	CCalTestLibrary* iTestLibrary;
       
    63 	};
       
    64 
       
    65 
       
    66 // Constructor/Destructor
       
    67 
       
    68 CIteratorTestManager* CIteratorTestManager::NewLC()
       
    69 	{
       
    70 	CIteratorTestManager* self = new (ELeave) CIteratorTestManager();
       
    71 	
       
    72 	CleanupStack::PushL(self);
       
    73 	self->ConstructL();
       
    74 	
       
    75 	return (self);
       
    76 	}
       
    77 
       
    78 
       
    79 void CIteratorTestManager::ConstructL()
       
    80 	{
       
    81 	iTestLibrary = CCalTestLibrary::NewL();
       
    82 	
       
    83 	iTestLibrary->ReplaceFileL(KTestName);
       
    84 	iTestLibrary->OpenFileL(KTestName);	
       
    85 	}	
       
    86 
       
    87 
       
    88 CIteratorTestManager::~CIteratorTestManager()	
       
    89 	{
       
    90 	delete iTestLibrary;
       
    91 	}
       
    92 
       
    93 
       
    94 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    95  * Test cases forming the Iterator test suite
       
    96  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    97 	
       
    98 inline void CIteratorTestManager::CleanDatabaseL()
       
    99 	{
       
   100 	iTestLibrary->CleanDatabaseL();
       
   101 	}
       
   102 
       
   103 
       
   104 void CIteratorTestManager::TestEmptyIteratorL()
       
   105 	{
       
   106 	// Test Empty Iterator Returns NULL Des	
       
   107 	test.Next(_L("Test Empty Iterator"));
       
   108 
       
   109 	
       
   110 	CCalIter& iter = iTestLibrary->GetIterL();
       
   111 	
       
   112 	TPtrC8 Uid(iter.FirstL());
       
   113 	test(Uid == KNullDesC8()); 
       
   114 	
       
   115 	// Try and iterate beyond the end of the iterator
       
   116 	TRAPD(err, Uid.Set(iter.NextL()));  
       
   117 	test(err == KErrCorrupt);
       
   118 	}
       
   119 
       
   120 
       
   121 
       
   122 void CIteratorTestManager::TestAddEntryL(const TInt aNumToAdd)
       
   123 	{
       
   124 	// Add Entries for Test
       
   125 	test.Next(_L("Adding entries"));
       
   126 
       
   127 	
       
   128 	TBuf<50> summary;
       
   129 	TBuf<50> location;
       
   130 	TBuf<50> description;
       
   131 
       
   132 
       
   133 	RPointerArray<CCalEntry> entriesToStore;
       
   134 	CleanupResetAndDestroyPushL(entriesToStore);
       
   135 	
       
   136 	for (TInt index = 0; index < aNumToAdd; ++index)
       
   137 		{
       
   138 		CCalEntry* entry;
       
   139 		HBufC8*    guid = HBufC8::NewL(255);
       
   140 		TPtr8      uidP = guid->Des();
       
   141 		
       
   142 		iTestLibrary->RandomText8(uidP);
       
   143 		
       
   144 		if( (index %2) == 0 )
       
   145 			{
       
   146 			entry = iTestLibrary->CreateCalEntryL(CCalEntry::ETodo, guid);
       
   147 			}
       
   148 		else
       
   149 			{
       
   150 			entry = iTestLibrary->CreateCalEntryL(CCalEntry::EAppt, guid);
       
   151 			}
       
   152 	
       
   153 		TInt err = entriesToStore.Append(entry);
       
   154 		test(err == KErrNone);
       
   155 		
       
   156 		iTestLibrary->SetEntryStartAndEndTimeL(entry);
       
   157 				
       
   158 		iTestLibrary->RandomText(summary);
       
   159 		entry->SetSummaryL(summary);
       
   160 		
       
   161 		iTestLibrary->RandomText(location);
       
   162 		entry->SetLocationL(location);
       
   163 		
       
   164 		iTestLibrary->RandomText(description);
       
   165 		entry->SetDescriptionL(description);
       
   166 		}
       
   167 
       
   168 	TInt entriesStored(0);
       
   169 	iTestLibrary->SynCGetEntryViewL().StoreL(entriesToStore, entriesStored); //temp
       
   170 	test(entriesStored == aNumToAdd);
       
   171 	
       
   172 	CleanupStack::PopAndDestroy(&entriesToStore);
       
   173 	}
       
   174 
       
   175 
       
   176 void CIteratorTestManager::TestIterEntryAndFetchL(const TInt aNumOfEntries)
       
   177 	{
       
   178 	test.Next(_L("Check Iterator entries"));
       
   179 
       
   180 	
       
   181 	CCalIter& iter = iTestLibrary->GetIterL();
       
   182 	
       
   183 	TInt i = 0;
       
   184 		
       
   185 	for( TPtrC8 Uid(iter.FirstL()); Uid != KNullDesC8(); Uid.Set(iter.NextL()), ++i )
       
   186 		{
       
   187 		RPointerArray<CCalEntry> entryList;
       
   188 		CleanupResetAndDestroyPushL(entryList);
       
   189 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   190 		CleanupStack::PopAndDestroy(&entryList);			
       
   191 		}
       
   192 		
       
   193   	test(i == aNumOfEntries);
       
   194 	}
       
   195 
       
   196 
       
   197 void CIteratorTestManager::IterEntryAndFetchL(const TInt aNumOfIterations)
       
   198 	{
       
   199 	test.Next(_L("Iterate entries"));
       
   200 
       
   201 	
       
   202 	CCalIter& iter = iTestLibrary->GetIterL();
       
   203 	
       
   204 	TInt i = 0;
       
   205 	
       
   206 	for( TPtrC8 Uid(iter.FirstL()); Uid != KNullDesC8() || i < aNumOfIterations; Uid.Set(iter.NextL()), ++i )
       
   207 		{
       
   208 		RPointerArray<CCalEntry> entryList;
       
   209 		CleanupResetAndDestroyPushL(entryList);
       
   210 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   211 		CleanupStack::PopAndDestroy(&entryList);			
       
   212 		}
       
   213 	}
       
   214 
       
   215 
       
   216 
       
   217 void CIteratorTestManager::TestIterEntryAndFetchAndAddL(const TInt aNumOfIterations, const TInt aNumToAdd, const TInt aNumOfEntries)
       
   218 	{
       
   219 	test.Next(_L("Iterate entries and Add new"));
       
   220 
       
   221 		
       
   222 	CCalIter& iter = iTestLibrary->GetIterL();
       
   223 	
       
   224 	TInt   i = 0;
       
   225 	TPtrC8 Uid(iter.FirstL());
       
   226 
       
   227 	for( ; Uid != KNullDesC8() && i < aNumOfIterations; Uid.Set(iter.NextL()), ++i )
       
   228 		{
       
   229 		RPointerArray<CCalEntry> entryList;
       
   230 		CleanupResetAndDestroyPushL(entryList);
       
   231 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   232 		CleanupStack::PopAndDestroy(&entryList);			
       
   233 		}		
       
   234 	
       
   235 	TestAddEntryL(aNumToAdd);
       
   236 	
       
   237 	for( ; Uid != KNullDesC8(); Uid.Set(iter.NextL()), ++i )
       
   238 		{
       
   239 		RPointerArray<CCalEntry> entryList;
       
   240 		CleanupResetAndDestroyPushL(entryList);
       
   241 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   242 		CleanupStack::PopAndDestroy(&entryList);			
       
   243 		}
       
   244 
       
   245 	test(i == aNumOfEntries);
       
   246 	}
       
   247 
       
   248 
       
   249 void CIteratorTestManager::TestIterEntryAndFetchAndDeleteL(const TInt aNumOfIterations, const TInt aNumToRemove, const TInt aNumOfEntries)
       
   250 	{
       
   251 	test.Next(_L("Iterate entries and Delete some"));
       
   252 
       
   253 		
       
   254 	CCalIter& iter = iTestLibrary->GetIterL();
       
   255 	
       
   256 	TInt   i = 0;	
       
   257 	TPtrC8 Uid(iter.FirstL());
       
   258 
       
   259 	for( ; Uid != KNullDesC8() && i < aNumOfIterations; Uid.Set(iter.NextL()), ++i )
       
   260 		{
       
   261 		RPointerArray<CCalEntry> entryList;
       
   262 		CleanupResetAndDestroyPushL(entryList);
       
   263 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   264 		CleanupStack::PopAndDestroy(&entryList);			
       
   265 		}		
       
   266 
       
   267 
       
   268 	for( TInt j = 0; Uid != KNullDesC8() && j < aNumToRemove; Uid.Set(iter.NextL()), ++j )
       
   269 		{
       
   270 		RPointerArray<CCalEntry> entryList;
       
   271 		CleanupResetAndDestroyPushL(entryList);
       
   272 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   273 		for( TInt k = 0; k < entryList.Count(); ++k )
       
   274 			{
       
   275 			iTestLibrary->SynCGetEntryViewL().DeleteL( *(entryList[k]) );
       
   276 			}
       
   277 		CleanupStack::PopAndDestroy(&entryList);			
       
   278 		}	
       
   279 
       
   280 
       
   281 	for( ; Uid != KNullDesC8(); Uid.Set(iter.NextL()), ++i )
       
   282 		{
       
   283 		RPointerArray<CCalEntry> entryList;
       
   284 		CleanupResetAndDestroyPushL(entryList);
       
   285 		iTestLibrary->SynCGetEntryViewL().FetchL(Uid, entryList);
       
   286 		CleanupStack::PopAndDestroy(&entryList);			
       
   287 		}
       
   288 
       
   289 	test(i == aNumOfEntries);
       
   290 	}
       
   291 	
       
   292 
       
   293 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   294  * DoTestL()
       
   295  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   296 	
       
   297 static void DoTestL()
       
   298 	{
       
   299 	CIteratorTestManager* testManager = CIteratorTestManager::NewLC();
       
   300 	
       
   301 	
       
   302 	TPerformanceTimer timer(test);
       
   303 	timer.Start();
       
   304 
       
   305 
       
   306 	// Run the test suite
       
   307 
       
   308 	// case 1: test the iterator when the database is empty
       
   309 	
       
   310 	testManager->TestEmptyIteratorL();
       
   311 
       
   312 
       
   313 	// case 2: test the iterator twice with some parent entries in the database
       
   314 
       
   315 	testManager->TestAddEntryL(KNumberOfEntries);
       
   316 
       
   317 	// iterate once to verify all the parent entries are seen
       
   318 	testManager->TestIterEntryAndFetchL(KNumberOfEntries);
       
   319 	// iterate again to verify all the parent entries are seen a second time
       
   320 	testManager->TestIterEntryAndFetchL(KNumberOfEntries);
       
   321 
       
   322 
       
   323 	// case 3: test the iterator twice with some parent entries add on top
       
   324 	//         of the previous ones in the database
       
   325 
       
   326 	testManager->TestAddEntryL(KNumberOfEntries);
       
   327 
       
   328 	// make sure the parent entries are the double now...
       
   329 	testManager->TestIterEntryAndFetchL(KNumberOfEntries * 2);
       
   330 	// and check a second time
       
   331 	testManager->TestIterEntryAndFetchL(KNumberOfEntries * 2);
       
   332 
       
   333 
       
   334 	// case 4: cleanup and restart from scratch the test of the iterator twice
       
   335 	//         with some parent entries in the database; making sure the iterator
       
   336 	//         behaves correctly working on a clean database
       
   337 
       
   338 	testManager->CleanDatabaseL();
       
   339 
       
   340 	testManager->TestEmptyIteratorL();
       
   341 
       
   342 	testManager->TestAddEntryL(KNumberOfEntries);
       
   343 
       
   344 	// iterate once to verify all the parent entries are seen
       
   345 	testManager->TestIterEntryAndFetchL(KNumberOfEntries);
       
   346 	// iterate again to verify all the parententries are seen a second time
       
   347 	testManager->TestIterEntryAndFetchL(KNumberOfEntries);	
       
   348 	
       
   349 	
       
   350 	// case 5: cleanup and restart from scratch the test of the iterator 
       
   351 	//         stopping half way, adding some entries in the database and
       
   352 	//         running again
       
   353 
       
   354 	testManager->CleanDatabaseL();
       
   355 
       
   356 	testManager->TestEmptyIteratorL();
       
   357 
       
   358 	testManager->TestAddEntryL(KNumberOfEntries);
       
   359 
       
   360 	// just iterate half of the parent entries
       
   361 	testManager->IterEntryAndFetchL(KNumberOfEntries / 2);
       
   362 	
       
   363 	// add some more parent entries
       
   364 	testManager->TestAddEntryL(KNumberOfEntries);	
       
   365 
       
   366 	// make sure the parent entries are the double now...
       
   367 	testManager->TestIterEntryAndFetchL(KNumberOfEntries * 2);
       
   368 	// and check a second time
       
   369 	testManager->TestIterEntryAndFetchL(KNumberOfEntries * 2);
       
   370 	
       
   371 	
       
   372 	// case 6: cleanup and restart from scratch the test of the iterator 
       
   373 	//         stopping half way, adding some parent entries in the database
       
   374 	//         and continue running
       
   375 	
       
   376 	testManager->CleanDatabaseL();
       
   377 
       
   378 	testManager->TestEmptyIteratorL();
       
   379 
       
   380 	testManager->TestAddEntryL(KNumberOfEntries);	
       
   381 /*** TODO: This test is failing at the moment, it just gets a partial number of new total entries
       
   382 	testManager->TestIterEntryAndFetchAndAddL(KNumberOfEntries / 2, KNumberOfEntries, KNumberOfEntries * 2);
       
   383 ***/
       
   384 
       
   385 
       
   386 	// case 7: cleanup and restart from scratch the test of the iterator 
       
   387 	//         stopping half way, deleting some parent entries in the database
       
   388 	//         and continue running
       
   389 
       
   390 	testManager->CleanDatabaseL();
       
   391 
       
   392 	testManager->TestEmptyIteratorL();
       
   393 
       
   394 	testManager->TestAddEntryL(KNumberOfEntries);	
       
   395 
       
   396 	testManager->TestIterEntryAndFetchAndDeleteL( (KNumberOfEntries / 2), (KNumberOfEntries / 4), (KNumberOfEntries - (KNumberOfEntries / 4)) );	
       
   397 
       
   398 
       
   399 	timer.Stop();
       
   400 	test.Printf(_L("Done\n"));
       
   401 	// printout performance time
       
   402 	timer.PrintOut();
       
   403 
       
   404 
       
   405 	CleanupStack::PopAndDestroy(testManager);
       
   406 	}
       
   407 
       
   408 
       
   409 /**
       
   410 
       
   411 @SYMTestCaseID     PIM-TCAL-ITERATOR-0001
       
   412 
       
   413 */
       
   414 
       
   415 TInt E32Main()
       
   416     {
       
   417 	__UHEAP_MARK;
       
   418 
       
   419 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-ITERATOR-0001 Calendar Interim API Iterator test suite"));
       
   420 
       
   421 	test.Title();
       
   422 
       
   423 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   424 	if (!trapCleanup)
       
   425 		{
       
   426 		return KErrNoMemory;
       
   427 		}
       
   428 
       
   429 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   430 	if (!scheduler)
       
   431 		{
       
   432 		delete trapCleanup;
       
   433 		return KErrNoMemory;
       
   434 		}
       
   435 	CActiveScheduler::Install(scheduler);	
       
   436 
       
   437 	TRAPD(ret, DoTestL());
       
   438 	test(ret == KErrNone);
       
   439 	
       
   440 	delete scheduler;
       
   441 	delete trapCleanup;	
       
   442 
       
   443 	test.End();
       
   444 	test.Close();
       
   445 
       
   446 	__UHEAP_MARKEND;
       
   447 
       
   448 	return (KErrNone);
       
   449     }