pimappservices/calendar/tsrc/tcal_compact.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2006-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 "tcal_compact.h"
       
    17 
       
    18 CCompactTestManager* CCompactTestManager::NewLC()
       
    19 	{
       
    20 	CCompactTestManager* self = new (ELeave) CCompactTestManager();
       
    21 	CleanupStack::PushL(self);
       
    22 	self->ConstructL();
       
    23 	return (self);
       
    24 	}
       
    25 
       
    26 CCompactTestManager* CCompactTestManager::NewL()
       
    27 	{
       
    28 	CCompactTestManager* self = CCompactTestManager::NewLC();
       
    29 	CleanupStack::Pop(self);
       
    30 	return (self);
       
    31 	}
       
    32 
       
    33 void CCompactTestManager::ConstructL()
       
    34 	{	
       
    35 	iTestLibrary = CCalTestLibrary::NewL();	
       
    36 	iTestLibrary->ReplaceFileL(KCalendarFile());
       
    37 	iTestLibrary->OpenFileL(KCalendarFile());
       
    38 	}
       
    39 
       
    40 CCompactTestManager::CCompactTestManager()
       
    41 	{
       
    42 	}
       
    43 	
       
    44 CCompactTestManager::~CCompactTestManager()
       
    45 	{
       
    46 	TRAP_IGNORE(iTestLibrary->DeleteFileL(KCalendarFile()));
       
    47 	delete iTestLibrary;
       
    48 	}
       
    49 
       
    50 // Populate the Calendar file with random entries exceeding the operations threshold triggering compact
       
    51 // Compare file sizes before adding and after deleting the same entry to ensure compacting was triggered
       
    52 void CCompactTestManager::TestSynchronousCompactingL()
       
    53 	{	
       
    54 	// Add multiple entries to the calendar file ensuring that the number of operations does not exceed 
       
    55 	// the compact threshold defined in the agenda model
       
    56 	AddEntriesToCalendarFileL(KMinEntriesBeforeCompact);
       
    57 	
       
    58 	// The steps below shall add and delete an entry and compare the file sizes before and after.
       
    59 	// Adding the entry is expected to trigger the compacting and thus once the same entry is deleted,
       
    60 	// the file size of the Calendar file after deleting the entry is expected to be less than its size before 
       
    61 	// adding the entry
       
    62 	
       
    63 	// Record the Calendar file size before initiating compacting
       
    64 	TInt fileSizeBeforeCompact = iTestLibrary->PIMTestServer().GetFileSizeL(KCalendarFilePath);
       
    65 	
       
    66 	test.Printf(_L("Calendar file size before compacting = %d"), fileSizeBeforeCompact);
       
    67 		
       
    68 	// Record timestamp before adding an entry that would trigger compacting
       
    69 	TTime univTime;
       
    70 	univTime.UniversalTime();
       
    71 	User::After(500000);
       
    72 	
       
    73 	TCalTime timeStamp;
       
    74 	timeStamp.SetTimeUtcL(univTime);
       
    75 	
       
    76 	// Exceed the operations threshold which will trigger the compacting
       
    77 	AddEntriesToCalendarFileL(1);
       
    78 	
       
    79 	RArray<TCalLocalUid> entriesLocalUids;
       
    80 	CleanupClosePushL(entriesLocalUids);
       
    81 
       
    82 	iTestLibrary->SynCGetEntryViewL().GetIdsModifiedSinceDateL(timeStamp, entriesLocalUids); 
       
    83 	test(entriesLocalUids.Count()==1);
       
    84 
       
    85 	// Delete the last entry
       
    86 	TInt entriesDeleted;
       
    87 	iTestLibrary->SynCGetEntryViewL().DeleteL(entriesLocalUids,entriesDeleted);
       
    88 	test(entriesDeleted==1);
       
    89 		
       
    90 	CleanupStack::PopAndDestroy(&entriesLocalUids);
       
    91 	
       
    92 	// Measure the file size after adding and deleting the same entry and thus verify compacting
       
    93 	TInt fileSizeAfterCompact = iTestLibrary->PIMTestServer().GetFileSizeL(KCalendarFilePath);
       
    94 	test.Printf(_L("Calendar file size after compacting = %d"), fileSizeAfterCompact);
       
    95 	
       
    96 	// Compacting should have reduced the size of the Calendar file even after adding and deleting 
       
    97 	// the same entry
       
    98 	test(fileSizeAfterCompact<fileSizeBeforeCompact);
       
    99 	}
       
   100 
       
   101 // Add fixed number of random entries to the Calendar file in one go
       
   102 void CCompactTestManager::AddEntriesToCalendarFileL(TInt aNumEntries)
       
   103 	{
       
   104 	RPointerArray<CCalEntry> entriesToAdd;
       
   105 	CleanupResetAndDestroyPushL(entriesToAdd);
       
   106 	
       
   107 	// Set up start and end dates
       
   108 	TTime startTime;
       
   109 	startTime.UniversalTime();
       
   110 	
       
   111 	TCalTime calStartTime;
       
   112 	calStartTime.SetTimeLocalL(startTime + TTimeIntervalDays(1));
       
   113 
       
   114 	TTime endTime(startTime + TTimeIntervalHours(4));
       
   115 	
       
   116 	TCalTime calEndTime;
       
   117 	calEndTime.SetTimeLocalL(endTime + TTimeIntervalDays(1));
       
   118 
       
   119 	TCalTime calRecIdTime;
       
   120 	calRecIdTime.SetTimeLocalL(startTime + TTimeIntervalMonths(1));
       
   121 	
       
   122 	// Create alarm and repeat rule objects for use later on
       
   123 	CCalAlarm* alarm = CCalAlarm::NewL();
       
   124 	CleanupStack::PushL(alarm);
       
   125 	
       
   126 	// Daily repeat
       
   127 	TCalRRule rule(TCalRRule::EDaily);
       
   128 	rule.SetInterval(1);
       
   129 	rule.SetDtStart(calStartTime);
       
   130 	
       
   131 	_LIT(KSummaryText, "abcdefghijklmnopqrstuvwxyz...abcdefghijklmnopqrstuvwxyz...abcdefghijklmnopqrstuvwxyz...abcdefghijklmnopqrstuvwxyz...abcdefghijklmnopqrstuvwxyz...");
       
   132 	
       
   133 	for (TInt i = 0; i < aNumEntries; ++i)
       
   134 		{
       
   135 		// get UID
       
   136 		TBuf8<50> buf;
       
   137 		iTestLibrary->RandomText8(buf);
       
   138 
       
   139 		HBufC8* guid = buf.AllocLC();
       
   140 		
       
   141 		// create basic entry
       
   142 		CCalEntry* entry = CCalEntry::NewL(CCalEntry::EAppt, guid, CCalEntry::EMethodNone, 0);
       
   143 		CleanupStack::Pop(guid);
       
   144 		CleanupStack::PushL(entry);
       
   145 
       
   146 		// set basic data
       
   147 		entry->SetStartAndEndTimeL(calStartTime, calEndTime);
       
   148 		entry->SetDescriptionL(_L("dummy description"));
       
   149 		entry->SetLocationL(_L("dummy location"));
       
   150 
       
   151 		alarm->SetTimeOffset(i);
       
   152 		entry->SetAlarmL(alarm);
       
   153 		entry->SetSummaryL(KSummaryText());
       
   154 		
       
   155 		AddCategoriesAndAttendeesToEntryL(*entry);
       
   156 		
       
   157 		entry->SetRRuleL(rule); 
       
   158 
       
   159 		entriesToAdd.AppendL(entry);
       
   160 		
       
   161 		CleanupStack::Pop(entry);
       
   162 		}
       
   163 		
       
   164 		TInt entriesAdded = 0;
       
   165 		if (entriesToAdd.Count() > 0)
       
   166 			{
       
   167 			test.Printf(_L("About to store %d entries, "), entriesToAdd.Count());
       
   168 			// store any remaining entries
       
   169 			iTestLibrary->SynCGetEntryViewL().StoreL(entriesToAdd, entriesAdded);
       
   170 			
       
   171 			test(entriesAdded == entriesToAdd.Count());
       
   172 			
       
   173 			entriesToAdd.ResetAndDestroy();
       
   174 			test.Printf(_L("done\n"));
       
   175 			}
       
   176 					
       
   177 	CleanupStack::PopAndDestroy(alarm);
       
   178 	CleanupStack::Pop(); // entriesToAdd
       
   179 	}
       
   180 
       
   181 void CCompactTestManager::AddCategoriesAndAttendeesToEntryL(CCalEntry& aEntry)
       
   182 	{
       
   183 	_LIT(KDummyCategoryName, "new category");
       
   184 	_LIT(KDummyAddress1, "dummy address 1");
       
   185 	_LIT(KDummyAddress2, "dummy address 2");
       
   186 	_LIT(KDummyAddress3, "dummy address 3");
       
   187 	_LIT(KDummyAddress4, "dummy address 4");
       
   188 	_LIT(KDummyAddress5, "dummy address 5");
       
   189 	_LIT(KDummySentBy1, "dummy sent by 1");
       
   190 	_LIT(KDummySentBy2, "dummy sent by 2");
       
   191 	
       
   192 	// add categories and attendees to 1 in 10 entries
       
   193 	CCalCategory* category1 = CCalCategory::NewL(KDummyCategoryName);
       
   194 	CleanupStack::PushL(category1);
       
   195 	
       
   196 	CCalCategory* category2 = CCalCategory::NewL(CCalCategory::ECalHoliday);
       
   197 	CleanupStack::PushL(category2);
       
   198 	
       
   199 	CCalAttendee* attendee1 = CCalAttendee::NewL(KDummyAddress1);
       
   200 	CleanupStack::PushL(attendee1);
       
   201 	
       
   202 	CCalAttendee* attendee2 = CCalAttendee::NewL(KDummyAddress2);
       
   203 	CleanupStack::PushL(attendee2);
       
   204 	
       
   205 	CCalAttendee* attendee3 = CCalAttendee::NewL(KDummyAddress3);
       
   206 	CleanupStack::PushL(attendee3);
       
   207 	
       
   208 	CCalAttendee* attendee4 = CCalAttendee::NewL(KDummyAddress4, KDummySentBy1);
       
   209 	CleanupStack::PushL(attendee4);
       
   210 	
       
   211 	CCalAttendee* attendee5 = CCalAttendee::NewL(KDummyAddress5, KDummySentBy2);
       
   212 	CleanupStack::PushL(attendee5);
       
   213 	
       
   214 	aEntry.AddCategoryL(category1); 
       
   215 	aEntry.AddCategoryL(category2); 
       
   216 	
       
   217 	aEntry.AddAttendeeL(attendee1); 
       
   218 	aEntry.AddAttendeeL(attendee2); 
       
   219 	aEntry.AddAttendeeL(attendee3); 
       
   220 	aEntry.AddAttendeeL(attendee4); 
       
   221 	aEntry.AddAttendeeL(attendee5); 
       
   222 
       
   223 	CleanupStack::Pop(attendee5);
       
   224 	CleanupStack::Pop(attendee4);
       
   225 	CleanupStack::Pop(attendee3);
       
   226 	CleanupStack::Pop(attendee2);
       
   227 	CleanupStack::Pop(attendee1);
       
   228 	
       
   229 	CleanupStack::Pop(category2);
       
   230 	CleanupStack::Pop(category1);
       
   231 	}
       
   232 	
       
   233 static void DoTestL()
       
   234 	{
       
   235 	CCompactTestManager* testManager = CCompactTestManager::NewLC();
       
   236 
       
   237 	testManager->TestSynchronousCompactingL();
       
   238 		
       
   239 	CleanupStack::PopAndDestroy(testManager);
       
   240 	}
       
   241 
       
   242 /**
       
   243 
       
   244 @SYMTestCaseID     PIM-TCAL-COMPACT-0001
       
   245 
       
   246 */
       
   247 
       
   248 TInt E32Main()
       
   249     {
       
   250 	__UHEAP_MARK;
       
   251 
       
   252 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-COMPACT-0001 Calendar Interim API - Calendar File Compacting Unit Test"));
       
   253 
       
   254 	test.Title();
       
   255 
       
   256 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   257 	if (!trapCleanup)
       
   258 		{
       
   259 		return KErrNoMemory;
       
   260 		}
       
   261 
       
   262 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   263 	if (!scheduler)
       
   264 		{
       
   265 		delete trapCleanup;
       
   266 		return KErrNoMemory;
       
   267 		}
       
   268 	CActiveScheduler::Install(scheduler);	
       
   269 
       
   270 	TRAPD(ret, DoTestL());
       
   271 	test(ret == KErrNone);
       
   272 	
       
   273 	delete scheduler;
       
   274 	delete trapCleanup;	
       
   275 
       
   276 	test.End();
       
   277 	test.Close();
       
   278 
       
   279 	__UHEAP_MARKEND;
       
   280 
       
   281 	return (KErrNone);
       
   282     }