phonebookengines_old/contactsmodel/tsrc/T_CntDelete.CPP
branchGCC_SURGE
changeset 41 d11de32a5e6f
parent 40 b46a585f6909
equal deleted inserted replaced
38:11319788d38f 41:d11de32a5e6f
       
     1 // Copyright (c) 2007-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 <e32test.h>
       
    17 #include "T_UTILS.H"
       
    18 
       
    19 CCntTest* CntTest=NULL;
       
    20 LOCAL_D RTest test(_L("T_CntDelete"));
       
    21 
       
    22 const TPtrC KDatabaseFileName=_L("C:T_CNTDELETE");
       
    23 
       
    24 const TPtrC KTestName=_L("Test Name No%d");
       
    25 const TPtrC KTestAddress=_L("Address %d");
       
    26 
       
    27 const TInt KTotalNumRecordsFirstUnknown  = 10;
       
    28 const TInt KTotalNumRecordsSecondUnknown = 1;
       
    29 const TInt KTotalNumRecordsSingle  = 1;
       
    30 
       
    31 const TInt KTotalUnknownEvents = 2;
       
    32 const TInt KTotalDeleteEvents  = KTotalNumRecordsSingle;
       
    33 const TInt KTotalAddedEvents = KTotalNumRecordsFirstUnknown + KTotalNumRecordsSecondUnknown + KTotalNumRecordsSingle;
       
    34 
       
    35 static const TInt KTimeout = 3000000; 
       
    36 
       
    37 TInt gUnknownChangesEvent = 0;
       
    38 TInt gAddedContactsEvent = 0;
       
    39 TInt gDeleteContactsEvent = 0;
       
    40 TInt gInvalidEvents = 0;
       
    41 
       
    42 class CMyObserver :  public CActive, public MContactDbObserver
       
    43 		{
       
    44 	public:
       
    45 		CMyObserver() : CActive(EPriorityIdle)
       
    46 			{
       
    47 			iStart.UniversalTime();
       
    48 			}
       
    49 		void Activate();
       
    50 				
       
    51 	private: // From CActive.
       
    52 		void RunL();
       
    53 		void DoCancel();
       
    54 		TInt RunError(TInt aError);
       
    55 
       
    56 	private: // From MContactDbObserver
       
    57 		void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
       
    58 		
       
    59 	private:
       
    60 		TTime 	iStart;
       
    61 		TTime 	iEnd;
       
    62 		};
       
    63 
       
    64 void CMyObserver::RunL()
       
    65 	{
       
    66 	iEnd.UniversalTime();
       
    67 	// Timeout has been excedeed, stop waiting
       
    68 	if( iEnd.MicroSecondsFrom( iStart.Int64() ) > KTimeout )
       
    69 		{		
       
    70 		CActiveScheduler::Stop();
       
    71 		}
       
    72 	else // Waiting for next event
       
    73 		{
       
    74 		Activate();
       
    75 		}
       
    76 	}
       
    77 
       
    78 void CMyObserver::DoCancel()
       
    79 	{
       
    80 	}
       
    81 
       
    82 TInt CMyObserver::RunError(TInt aError)
       
    83 	{
       
    84 	test.Printf( _L("CMyObserver:: Error in runL: %d"), aError );
       
    85 	return aError;
       
    86 	}
       
    87 
       
    88 void CMyObserver::Activate()
       
    89 	{
       
    90 	if(!IsActive())
       
    91 		{
       
    92 		TRequestStatus *pS=&iStatus;
       
    93 		User::RequestComplete(pS,KErrNone);
       
    94 		SetActive();
       
    95 		}
       
    96 	}
       
    97 
       
    98 
       
    99 void CMyObserver::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   100 	{  
       
   101 	switch( aEvent.iType )
       
   102 		{	
       
   103 		case EContactDbObserverEventUnknownChanges:
       
   104 			{
       
   105 			gUnknownChangesEvent++;
       
   106 			test.Printf( _L("Unknown Changes Event: id= %d\n"), aEvent.iContactId); 
       
   107 			Activate();	
       
   108 			break;
       
   109 			}		
       
   110 		case EContactDbObserverEventContactAdded:
       
   111 			{
       
   112 			gAddedContactsEvent++;
       
   113 			test.Printf( _L("Added: id= %d\n"), aEvent.iContactId );
       
   114 			Activate();			
       
   115 			break;
       
   116 			}
       
   117 		case EContactDbObserverEventContactDeleted:
       
   118 		    {
       
   119 		    gDeleteContactsEvent++;
       
   120 		    test.Printf( _L("Deleted: id= %d\n"), aEvent.iContactId );
       
   121 		    Activate();	
       
   122 		    break;
       
   123 		    }
       
   124 		default:
       
   125 			{
       
   126 			gInvalidEvents++;
       
   127 			test.Printf( _L("Invalid event: %d, contact id: %d"), aEvent.iType, aEvent.iContactId );
       
   128 			Activate();
       
   129 			break;
       
   130 			}
       
   131 		}
       
   132 	}
       
   133 
       
   134 
       
   135 LOCAL_C void SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName, TBool aAddField)
       
   136 	{
       
   137 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   138 	const TInt pos=fieldSet.Find(aType);
       
   139 	if (!aAddField && pos!=KErrNotFound)
       
   140 		fieldSet[pos].TextStorage()->SetTextL(aName);
       
   141 	else
       
   142 		{
       
   143 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,aType);
       
   144    		field->SetMapping(KUidContactFieldVCardMapUnusedN);
       
   145 		field->TextStorage()->SetTextL(aName);
       
   146 		aItem.AddFieldL(*field);
       
   147 		CleanupStack::Pop(); // field
       
   148 		}
       
   149 	}
       
   150 
       
   151 
       
   152 LOCAL_C void PopulateDatabaseL(TBool aPhoneNumbers, TInt aCount)
       
   153 	{
       
   154 	for (TInt ii=0;ii<aCount;ii++)
       
   155 		{
       
   156 		CContactItem* item=CContactCard::NewLC();
       
   157 		TBuf<16> name;
       
   158 		name.Format(KTestName,ii);
       
   159    		SetNameL(*item,KUidContactFieldFamilyName,name,ETrue);
       
   160 		if (aPhoneNumbers)
       
   161 			{
       
   162 			TBuf<20> number;
       
   163 			switch(ii%3)
       
   164 				{
       
   165 				case 0:
       
   166 					number.Format(_L("0171-%03d %04d"),(ii*9)%1000,((ii+11)*23)%10000);
       
   167 					break;
       
   168 				case 1:
       
   169 					number.Format(_L("%04d:%04d:%04d:%04d"),(ii*123)%10000,(ii*666)%10000,(ii*234)%10000);
       
   170 					break;
       
   171 				case 2:
       
   172 					number.Format(_L("+00%d-%03d %04d"),(ii*123)%100,(ii*13)%1000,((ii+13)*17)%10000);
       
   173 					break;
       
   174 				}
       
   175    			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   176 			if (!(ii%2))
       
   177 				{
       
   178 				number.Format(_L("0181-%03d %04d"),(ii*8)%1000,((ii+11)*22)%10000);
       
   179 	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   180 				number.Format(_L("01734-%06d"),(ii*123456)%1000000);
       
   181 	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   182 				}
       
   183 			}
       
   184 		if (ii%2)
       
   185 			{
       
   186 			TBuf<16> address;
       
   187 			address.Format(KTestAddress,ii);
       
   188 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldAddress);
       
   189 			field->SetMapping(KUidContactFieldVCardMapADR);
       
   190 			field->TextStorage()->SetText(address.AllocL());
       
   191 			field->SetTemplateField(ETrue);
       
   192 			item->AddFieldL(*field);
       
   193 			CleanupStack::Pop(); // field
       
   194 			}
       
   195 		if (ii%3)
       
   196 			{
       
   197 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeDateTime,KUidContactFieldBirthday);
       
   198 			field->DateTimeStorage()->SetTime(TDateTime(1990+ii,(TMonth)(ii%12),(ii*3)%28,1,2,3,4));
       
   199 			field->SetTemplateField(ETrue);
       
   200 			item->AddFieldL(*field);
       
   201 			CleanupStack::Pop(); // field
       
   202 			}
       
   203 		if (ii%4)
       
   204 			{
       
   205 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeStore,KUidContactFieldVCardMapAGENT);
       
   206     		field->SetMapping(KUidContactFieldVCardMapAGENT);
       
   207 			field->SetTemplateField(ETrue);
       
   208 			item->AddFieldL(*field);
       
   209 			CleanupStack::Pop(); // field
       
   210 			}
       
   211 		if (ii%6)
       
   212 			{
       
   213 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeContactItemId);
       
   214 			field->SetMapping(KUidContactFieldVCardMapAGENT);
       
   215 			field->SetTemplateField(ETrue);
       
   216 			item->AddFieldL(*field);
       
   217 			CleanupStack::Pop(); // field
       
   218 			}
       
   219 		CntTest->Db()->AddNewContactL(*item);
       
   220 		CleanupStack::PopAndDestroy(); // item
       
   221 		}
       
   222 	CntTest->Db()->SetDateFormatTextL(_L("%E%D%X%N%Y %1 %2 %3"));
       
   223 	}
       
   224 
       
   225 
       
   226 LOCAL_C void WaitForNotificationsL(CContactDatabase &aDb)
       
   227 	{
       
   228 	CMyObserver *observe = new (ELeave) CMyObserver();
       
   229 	CleanupStack::PushL( observe );
       
   230 	CContactChangeNotifier *notifier = CContactChangeNotifier::NewL(aDb, observe);
       
   231 	CleanupStack::PushL( notifier );
       
   232 	CActiveScheduler::Add(observe);
       
   233 	observe->Activate();
       
   234 	CActiveScheduler::Start();
       
   235 	CleanupStack::PopAndDestroy(notifier);
       
   236 	CleanupStack::PopAndDestroy(observe);
       
   237 	}
       
   238 		
       
   239 	
       
   240 LOCAL_C void DeleteMultipleContactsL()
       
   241 	{
       
   242 	CContactIdArray* idArray = CntTest->Db()->ContactsChangedSinceL(TTime(0));
       
   243 	CleanupStack::PushL(idArray);
       
   244 	CntTest->Db()->DeleteContactsL(*idArray);		
       
   245 	CleanupStack::PopAndDestroy(idArray);
       
   246 	}
       
   247 	
       
   248 	
       
   249 LOCAL_C void DeleteOneContactL()
       
   250     {
       
   251     CContactIdArray* idArray = CntTest->Db()->ContactsChangedSinceL(TTime(0));
       
   252 	CleanupStack::PushL(idArray);
       
   253 	if (idArray->Count() == 1)
       
   254 	    {	    
       
   255 	    CntTest->Db()->DeleteContactL((*idArray)[0]);		
       
   256 	    }
       
   257 	CleanupStack::PopAndDestroy(idArray);
       
   258     }
       
   259 	
       
   260 
       
   261 /**
       
   262 @SYMTestCaseID PIM-T-CNTDELETE-0001
       
   263 @SYMTestType UT
       
   264 @SYMTestPriority Medium
       
   265 @SYMDEF DEF103603
       
   266 @SYMTestCaseDependencies CntModel CoreAppsTest
       
   267 @SYMTestCaseDesc Check that deleting multiple contacts in one go will raise a EContactDbObserverEventUnknownChanges 
       
   268 @SYMTestActions Create many contacts. Delete all and wait for the event. Create one contact
       
   269 @SYMTestExpectedResults EContactDbObserverEventUnknownChanges should be the only 
       
   270 event that will be sent to the client when deleting the contacts.
       
   271 */
       
   272 	
       
   273 void DoTestsL()
       
   274     {  
       
   275 	test.Start(_L("@SYMTESTCaseID:PIM-T-CNTDELETE-0001 Create new database"));
       
   276 
       
   277 	CntTest->CreateDatabaseL();	
       
   278 
       
   279     // Create many contacts, delete all, wait for Unknown Changes Event
       
   280 	TRAPD(err,PopulateDatabaseL(ETrue, KTotalNumRecordsFirstUnknown));
       
   281 	test(err==KErrNone);
       
   282 	WaitForNotificationsL(*CntTest->Db());				
       
   283 	DeleteMultipleContactsL();
       
   284 	WaitForNotificationsL(*CntTest->Db());	
       
   285 		
       
   286 	// Create one contact, delete it, wait for Unknown Changes Event
       
   287 	TRAP(err,PopulateDatabaseL(ETrue, KTotalNumRecordsSecondUnknown));
       
   288 	test(err==KErrNone);
       
   289 	WaitForNotificationsL(*CntTest->Db());				
       
   290 	DeleteMultipleContactsL();
       
   291 	WaitForNotificationsL(*CntTest->Db());		
       
   292 	
       
   293 	// Create one contact, delete it, wait for Contact Deleted Event
       
   294 	TRAP(err,PopulateDatabaseL(ETrue, KTotalNumRecordsSingle));
       
   295 	test(err==KErrNone);
       
   296 	WaitForNotificationsL(*CntTest->Db());				
       
   297 	DeleteOneContactL();
       
   298 	WaitForNotificationsL(*CntTest->Db());		
       
   299 	
       
   300 	CntTest->CloseDatabase();
       
   301 	test.Next(_L("Delete database"));
       
   302 
       
   303 	CntTest->DeleteDatabaseL();
       
   304 	
       
   305 	test(
       
   306 	gUnknownChangesEvent == KTotalUnknownEvents && 
       
   307 	gAddedContactsEvent  == KTotalAddedEvents   &&
       
   308 	gDeleteContactsEvent == KTotalDeleteEvents  &&
       
   309 	gInvalidEvents       == 0
       
   310 	);
       
   311 	
       
   312 	test.Printf( _L("Test complete!\n") );
       
   313     }
       
   314     
       
   315 
       
   316 GLDEF_C TInt E32Main()
       
   317 	{
       
   318 	__UHEAP_MARK;
       
   319     CntTest=new(ELeave) CCntTest;
       
   320 	CntTest->ConstructL(test,KDatabaseFileName);
       
   321     TRAPD(err,DoTestsL());
       
   322 	CntTest->EndTestLib(err);	
       
   323 	__UHEAP_MARKEND;
       
   324 	return KErrNone;
       
   325     }