phonebookengines_old/contactsmodel/tsrc/T_OWNCAR.CPP
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     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 #include <e32test.h>
       
    17 #include <f32file.h>
       
    18 #include <s32file.h>
       
    19 #include <cntdb.h>
       
    20 #include <cntitem.h>
       
    21 #include <cntfield.h>
       
    22 #include <cntfldst.h>
       
    23 #include <bautils.h>
       
    24 #include <barsc.h>
       
    25 #include <barsread.h>
       
    26 #include <cntmodel.rsg>
       
    27 #include "T_UTILS.H"
       
    28 
       
    29 CCntTest* CntTest=NULL;
       
    30 LOCAL_D RTest test(_L("T_OWNCAR"));
       
    31 
       
    32 const TPtrC KDatabaseFileName=_L("C:T_OWNCAR");
       
    33 
       
    34 const TPtrC KTestName=_L("Test Name No%d");
       
    35 const TPtrC KTestAddress=_L("Address %d");
       
    36 const TInt KTotalNumRecords=50;
       
    37 const TInt KTotalNumOwnCards=1;
       
    38 
       
    39 
       
    40 #define KCardGivenName _L("Given Name #%d")
       
    41 #define KCardFamilyName _L("Family Name #%d")
       
    42 #define KCardFieldText _L("Card id %d, text field #%d")
       
    43 #define KCardFieldEditedText _L("UPDATED Card id %d, text field #%d")
       
    44 
       
    45 static const TInt KTimeout = 20000000;//20 seconds timeout
       
    46 
       
    47 class CMyObserver :  public CActive, public MContactDbObserver
       
    48 		{
       
    49 	public:
       
    50 		CMyObserver() : CActive(EPriorityIdle)
       
    51 			{
       
    52 			iStart.UniversalTime();
       
    53 			}
       
    54 		void Activate();
       
    55 				
       
    56 	private: // From CActive.
       
    57 		void RunL();
       
    58 		void DoCancel();
       
    59 		TInt RunError(TInt aError);
       
    60 
       
    61 	private: // from MContactDbObserver
       
    62 		void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
       
    63 		
       
    64 	private:
       
    65 		TTime 	iStart;
       
    66 		TTime 	iEnd;
       
    67 		};
       
    68 
       
    69 void CMyObserver::RunL()
       
    70 	{
       
    71 	iEnd.UniversalTime();
       
    72 	//Timeout has been excedeed, stop waiting
       
    73 	if( iEnd.MicroSecondsFrom( iStart.Int64() ) > KTimeout )
       
    74 		{
       
    75 		test.Printf( _L("Observer has timed out, EContactDbObserverEventOwnCardChanged notification not received"));
       
    76 		CActiveScheduler::Stop();
       
    77 		}
       
    78 	else //waiting for next event
       
    79 		{
       
    80 		Activate();
       
    81 		}
       
    82 	}
       
    83 
       
    84 void CMyObserver::DoCancel()
       
    85 	{
       
    86 	}
       
    87 
       
    88 TInt CMyObserver::RunError(TInt aError)
       
    89 	{
       
    90 	test.Printf( _L("CMyObserver:: Error in runL: %d"), aError );
       
    91 	return aError;
       
    92 	}
       
    93 
       
    94 void CMyObserver::Activate()
       
    95 	{
       
    96 	if(!IsActive())
       
    97 		{
       
    98 		TRequestStatus *pS=&iStatus;
       
    99 		User::RequestComplete(pS,KErrNone);
       
   100 		SetActive();
       
   101 		}
       
   102 	}
       
   103 
       
   104 void CMyObserver::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   105 	{  
       
   106 	switch( aEvent.iType )
       
   107 		{
       
   108 		case EContactDbObserverEventOwnCardChanged:
       
   109 			{
       
   110 			test.Printf( _L("Own card changed event id: %d, contact id: %d"), aEvent.iType, aEvent.iContactId );
       
   111 			Cancel();
       
   112 			CActiveScheduler::Stop();
       
   113 			break;
       
   114 			}
       
   115 		default:
       
   116 			{
       
   117 			test.Printf( _L("Other event id: %d, contact id: %d"), aEvent.iType, aEvent.iContactId );
       
   118 			Activate();
       
   119 			break;
       
   120 			}
       
   121 		}
       
   122 	}
       
   123 
       
   124 GLDEF_C void Panic(TInt aPanic)
       
   125 //
       
   126 // Panic the thread with CNTMODEL as the category
       
   127 //
       
   128 	{
       
   129 	User::Panic(_L("T_OWNCAR"),aPanic);
       
   130 	}
       
   131 
       
   132 LOCAL_C void SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName, TBool aAddField)
       
   133 //
       
   134 // Set the contents of a text field, creating the field if required
       
   135 //
       
   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 LOCAL_C void PopulateDatabaseL(TBool aPhoneNumbers)
       
   152 //
       
   153 // Create and populate the database
       
   154 //
       
   155 	{
       
   156 	for (TInt ii=0;ii<KTotalNumRecords;ii++)
       
   157 		{
       
   158 		CContactItem* item=CContactCard::NewLC();
       
   159 		TBuf<16> name;
       
   160 		name.Format(KTestName,ii);
       
   161    		SetNameL(*item,KUidContactFieldFamilyName,name,ETrue);
       
   162 		if (aPhoneNumbers)
       
   163 			{
       
   164 			TBuf<20> number;
       
   165 			switch(ii%3)
       
   166 				{
       
   167 				case 0:
       
   168 					number.Format(_L("0171-%03d %04d"),(ii*9)%1000,((ii+11)*23)%10000);
       
   169 					break;
       
   170 				case 1:
       
   171 					number.Format(_L("%04d:%04d:%04d:%04d"),(ii*123)%10000,(ii*666)%10000,(ii*234)%10000);
       
   172 					break;
       
   173 				case 2:
       
   174 					number.Format(_L("+00%d-%03d %04d"),(ii*123)%100,(ii*13)%1000,((ii+13)*17)%10000);
       
   175 					break;
       
   176 				}
       
   177    			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   178 			if (!(ii%2))
       
   179 				{
       
   180 				number.Format(_L("0181-%03d %04d"),(ii*8)%1000,((ii+11)*22)%10000);
       
   181 	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   182 				number.Format(_L("01734-%06d"),(ii*123456)%1000000);
       
   183 	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
       
   184 				}
       
   185 			}
       
   186 		if (ii%2)
       
   187 			{
       
   188 			TBuf<16> address;
       
   189 			address.Format(KTestAddress,ii);
       
   190 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldAddress);
       
   191 			field->SetMapping(KUidContactFieldVCardMapADR);
       
   192 			field->TextStorage()->SetText(address.AllocL());
       
   193 			field->SetTemplateField(ETrue);
       
   194 			item->AddFieldL(*field);
       
   195 			CleanupStack::Pop(); // field
       
   196 			}
       
   197 		if (ii%3)
       
   198 			{
       
   199 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeDateTime,KUidContactFieldBirthday);
       
   200 			field->DateTimeStorage()->SetTime(TDateTime(1990+ii,(TMonth)(ii%12),(ii*3)%28,1,2,3,4));
       
   201 			field->SetTemplateField(ETrue);
       
   202 			item->AddFieldL(*field);
       
   203 			CleanupStack::Pop(); // field
       
   204 			}
       
   205 		if (ii%4)
       
   206 			{
       
   207 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeStore,KUidContactFieldVCardMapAGENT);
       
   208     		field->SetMapping(KUidContactFieldVCardMapAGENT);
       
   209 			field->SetTemplateField(ETrue);
       
   210 			item->AddFieldL(*field);
       
   211 			CleanupStack::Pop(); // field
       
   212 			}
       
   213 		if (ii%6)
       
   214 			{
       
   215 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeContactItemId);
       
   216 			field->SetMapping(KUidContactFieldVCardMapAGENT);
       
   217 			field->SetTemplateField(ETrue);
       
   218 			item->AddFieldL(*field);
       
   219 			CleanupStack::Pop(); // field
       
   220 			}
       
   221 		CntTest->Db()->AddNewContactL(*item);
       
   222 		CleanupStack::PopAndDestroy(); // item
       
   223 		}
       
   224 	CntTest->Db()->SetDateFormatTextL(_L("%E%D%X%N%Y %1 %2 %3"));
       
   225 	}
       
   226 
       
   227 
       
   228 
       
   229 //
       
   230 //
       
   231 //	main test modules
       
   232 //
       
   233 //
       
   234 
       
   235 
       
   236 LOCAL_C void CheckOwnCardCreatedL()
       
   237 //
       
   238 // make sure own card is the only card in db
       
   239 //
       
   240 	{
       
   241 	test.Next(_L("Check Own Card Created"));
       
   242 
       
   243 	CContactItem* newOwnCard = CntTest->Db()->CreateOwnCardL();
       
   244 	delete newOwnCard;
       
   245 	test(CntTest->Db()->CountL()==KTotalNumOwnCards);
       
   246 	// check own card uid is set
       
   247 	if (CntTest->Db()->OwnCardId()==NULL)
       
   248 		User::Leave(KErrNotFound);
       
   249 	CContactItem* tempOwnCard = CntTest->Db()->ReadContactL(CntTest->Db()->OwnCardId());
       
   250 	delete tempOwnCard;
       
   251 	}
       
   252 
       
   253 LOCAL_C void SetOwnCardL(TInt aUid)
       
   254 //
       
   255 //  set an existing card to be new own card
       
   256 //
       
   257 	{
       
   258 	test.Next(_L("Set Own Card"));
       
   259 
       
   260 	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
       
   261 	CleanupStack::PushL(tempCard);
       
   262 	CntTest->Db()->SetOwnCardL(*tempCard);
       
   263 	if(CntTest->Db()->OwnCardId()!=tempCard->Id())
       
   264 		User::Leave(KErrNotFound);
       
   265 	CleanupStack::PopAndDestroy(); // tempCard
       
   266 	}
       
   267 
       
   268 
       
   269 LOCAL_C void SetBadOwnCardL()
       
   270 //
       
   271 //	try to set a bad card as the new own card
       
   272 //
       
   273 	{
       
   274 	test.Next(_L("Set Bad Own Card"));
       
   275 
       
   276 	CContactItem* badOwnCard=CContactCard::NewLC();
       
   277 	CntTest->Db()->SetOwnCardL(*badOwnCard);
       
   278 	CleanupStack::PopAndDestroy(badOwnCard);
       
   279 	}
       
   280 
       
   281 
       
   282 LOCAL_C void GetOwnCardL(TInt aUid)
       
   283 //
       
   284 //	check own card persistence
       
   285 //
       
   286 	{
       
   287 	test.Next(_L("Check Persistence"));
       
   288 
       
   289 	if(CntTest->Db()->OwnCardId()!=aUid)
       
   290 		User::Leave(KErrNotFound);
       
   291 	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
       
   292 	delete tempCard;
       
   293 	}
       
   294 
       
   295 
       
   296 LOCAL_C void DeleteOwnCardL()
       
   297 //
       
   298 //	delete the own card & check persistence
       
   299 //
       
   300 	{
       
   301 	test.Next(_L("Delete Own Card"));
       
   302 
       
   303 	CntTest->Db()->DeleteContactL(CntTest->Db()->OwnCardId());
       
   304 
       
   305 	CntTest->CloseDatabase();
       
   306 	CntTest->OpenDatabaseL();
       
   307 	
       
   308 	// ensure id is reset.
       
   309 	if (CntTest->Db()->OwnCardId()!=KNullContactId)
       
   310 		User::Leave(KErrNotFound);
       
   311 	}
       
   312 
       
   313 
       
   314 LOCAL_C void SetGoodOwnCardL(TInt aUid)
       
   315 //
       
   316 //	after deleting own card set a new one
       
   317 //
       
   318 	{
       
   319 	test.Next(_L("Set Good Own Card"));
       
   320 
       
   321 	// ownCard should be null.
       
   322 	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
       
   323 	CntTest->Db()->SetOwnCardL(*tempCard);
       
   324 	delete tempCard;
       
   325 	}
       
   326 
       
   327 LOCAL_C void CreateOwnCardTwiceL()
       
   328 //
       
   329 //	Create own card twice and see ok
       
   330 //
       
   331 	{
       
   332 	test.Next(_L("Create Own Card Twice"));
       
   333 
       
   334 
       
   335 	CContactItem* newOwnCard = CntTest->Db()->CreateOwnCardL();
       
   336 	CContactItem* newOwnCard2 = CntTest->Db()->CreateOwnCardL();
       
   337 	delete newOwnCard;
       
   338 	delete newOwnCard2;
       
   339 	}
       
   340 
       
   341 LOCAL_C void SetContactFieldsL(CContactDatabase &aDb, const TContactItemId aId)
       
   342 	{
       
   343 	static TInt i = 0;
       
   344 	CContactItem *citem = aDb.OpenContactLX(aId);
       
   345 	CleanupStack::PushL( citem );
       
   346 	TBuf<16> name;
       
   347 	name.Format(KTestName,++i);
       
   348 	SetNameL(*citem,KUidContactFieldFamilyName,name,EFalse);
       
   349 	SetNameL(*citem,KUidContactFieldPhoneNumber,name,EFalse);
       
   350 	SetNameL(*citem,KUidContactFieldGivenName,name,EFalse);
       
   351 	SetNameL(*citem,KUidContactFieldAdditionalName,name,EFalse);
       
   352 	SetNameL(*citem,KUidContactFieldEMail,name,EFalse);
       
   353 	SetNameL(*citem,KUidContactFieldCompanyName,name,EFalse);
       
   354 	SetNameL(*citem,KUidContactFieldAddress,name,EFalse);
       
   355 	aDb.CommitContactL(*citem);
       
   356 	CleanupStack::PopAndDestroy(citem);
       
   357 	CleanupStack::Pop();//lock
       
   358 	}
       
   359 
       
   360 LOCAL_C void WaitForNotificationsL(CContactDatabase &aDb)
       
   361 	{
       
   362 	CMyObserver *observe = new (ELeave) CMyObserver();
       
   363 	CleanupStack::PushL( observe );
       
   364 	CContactChangeNotifier *notifier = CContactChangeNotifier::NewL(aDb, observe);
       
   365 	CleanupStack::PushL( notifier );
       
   366 	CActiveScheduler::Add(observe);
       
   367 	observe->Activate();
       
   368 	CActiveScheduler::Start();
       
   369 	CleanupStack::PopAndDestroy(notifier);
       
   370 	CleanupStack::PopAndDestroy(observe);
       
   371 	}
       
   372 	
       
   373 LOCAL_C void SetOwnCardL(CContactDatabase &aDb, const TContactItemId aId)
       
   374 	{
       
   375 	test.Printf(_L("Change owncard from: %d to: %d"), aDb.OwnCardId(), aId);
       
   376 	CContactItem *citem = aDb.ReadContactLC(aId);
       
   377 	aDb.SetOwnCardL( *citem );
       
   378 	CleanupStack::PopAndDestroy(citem);
       
   379 	}
       
   380  
       
   381 /**
       
   382 @SYMTestCaseID     PIM-T-OWNCAR-0002
       
   383 @SYMTestType UT
       
   384 @SYMTestPriority Medium
       
   385 @SYMDEF DEF085699
       
   386 @SYMTestCaseDesc 
       
   387 Multiple Client Owncard Test
       
   388 1) Create a contact and set it as the own card
       
   389 2) Create another contact
       
   390 3) Set the new contact to be the own card
       
   391 4) Modify the first contact created (i.e not the one currently set to the own card)
       
   392 5) View result of a CContactDatabase::OwnCardId() call in both the applications
       
   393 
       
   394 @SYMTestActions 
       
   395 Create Sessions A and B to the database
       
   396 add two contacts using session A
       
   397 Set contact 1 to own card using session A
       
   398 Set contact 2 to own card using session A
       
   399 Update Contact 1 using session A
       
   400 
       
   401 @SYMTestExpectedResults 
       
   402 Owncard should equal contact 2 in for both sessions
       
   403 */
       
   404 
       
   405 LOCAL_C void MultipleClientOwncardTestL()
       
   406 	{
       
   407 	test.Next(_L("@SYMTESTCaseID:PIM-T-OWNCAR-0002 Multiple Client OwnCard Test"));
       
   408 
       
   409 	CContactDatabase *db = CContactDatabase::ReplaceL( KDatabaseFileName );
       
   410 	CContactDatabase *db2 = CContactDatabase::OpenL( KDatabaseFileName );
       
   411 	CleanupStack::PushL( db );
       
   412 	CleanupStack::PushL( db2 );
       
   413 	CContactItemViewDef *viewAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
       
   414 	viewAll->AddL(KUidContactFieldMatchAll);
       
   415 	// Retrieve the system template
       
   416 	CContactTemplate* ttemplate = static_cast< CContactTemplate* >
       
   417 												( db->ReadContactLC( db->TemplateId(), *viewAll ) );
       
   418 	// Create a new contact based on the system template
       
   419 	CContactCard* contact = CContactCard::NewLC(ttemplate);
       
   420 	
       
   421 	// Add the new contact to the db
       
   422 	TContactItemId id1 = db->AddNewContactL(*contact);
       
   423 	SetContactFieldsL(*db,id1);
       
   424 	
       
   425 	test(db->OwnCardId() == db2->OwnCardId());
       
   426 	SetOwnCardL(*db, id1);
       
   427 	WaitForNotificationsL(*db2);
       
   428 
       
   429 	TContactItemId oid1 = db->OwnCardId();
       
   430 	TContactItemId oid2 = db2->OwnCardId();
       
   431 	test( (oid1 == oid2) && (oid1 == id1) );
       
   432 	
       
   433 	test.Printf(_L("Add new contact; check that it is different from existing contacts"));
       
   434 	TContactItemId id2 = db->AddNewContactL(*contact);
       
   435 	SetContactFieldsL(*db,id2);
       
   436 	test(id1 != id2);
       
   437 	
       
   438 	SetOwnCardL(*db, id2);
       
   439 	WaitForNotificationsL(*db2);
       
   440 
       
   441 	oid1 = db->OwnCardId();
       
   442 	oid2 = db2->OwnCardId();
       
   443 	test( (oid1 == oid2) && (oid1 == id2) );
       
   444 	
       
   445 	test.Printf(_L("modify contact 1, check that its not marked as an own card by either session"));
       
   446 	SetContactFieldsL(*db,id1);
       
   447 	WaitForNotificationsL(*db2);
       
   448 	oid1 = db->OwnCardId();
       
   449 	oid2 = db2->OwnCardId();
       
   450 	test( (oid1 == oid2) && (oid1 == id2) );
       
   451 	
       
   452 	
       
   453 	CleanupStack::PopAndDestroy(contact);
       
   454 	CleanupStack::PopAndDestroy(ttemplate);
       
   455 	CleanupStack::PopAndDestroy(viewAll);
       
   456 	CleanupStack::PopAndDestroy(db2);
       
   457 	CleanupStack::PopAndDestroy(db);
       
   458 	}
       
   459 
       
   460 /**
       
   461 
       
   462 @SYMTestCaseID     PIM-T-OWNCAR-0001
       
   463 
       
   464 */
       
   465 
       
   466 void DoTestsL()
       
   467     {
       
   468     MultipleClientOwncardTestL();
       
   469 	test.Start(_L("@SYMTESTCaseID:PIM-T-OWNCAR-0001 Create new database"));
       
   470 
       
   471 	TRAPD(err,CntTest->CreateDatabaseL());
       
   472 	test(err==KErrNone);
       
   473 	CntTest->Db()->OwnCardId();
       
   474 	CntTest->CloseDatabase();
       
   475 	CntTest->OpenDatabaseL();
       
   476 	CntTest->Db()->OwnCardId();
       
   477 	TRAP(err,CheckOwnCardCreatedL());
       
   478 	test(err==KErrNone);
       
   479 	CntTest->Db()->DeleteContactL(CntTest->Db()->OwnCardId());
       
   480 	CntTest->CloseDatabase();
       
   481 	CntTest->OpenDatabaseL();
       
   482 	TRAP(err,PopulateDatabaseL(ETrue));
       
   483 	test(err==KErrNone);
       
   484 	TRAP(err,SetOwnCardL(CntTest->Db()->CountL()-2));
       
   485 	test(err==KErrNone);
       
   486 	TRAP(err,GetOwnCardL(CntTest->Db()->CountL()-2));
       
   487 	test(err==KErrNone);
       
   488 	TRAP(err,SetBadOwnCardL());
       
   489 	test(err==KErrNotFound);
       
   490 	CntTest->CloseDatabase();
       
   491 	CntTest->OpenDatabaseL();
       
   492 	TRAP(err,DeleteOwnCardL());
       
   493 	test(err==KErrNone);
       
   494 	TRAP(err,SetGoodOwnCardL(CntTest->Db()->CountL()-15));
       
   495 	test(err==KErrNone);
       
   496 	TRAP(err,CreateOwnCardTwiceL());
       
   497 	test(err==KErrNone);
       
   498 	CntTest->CloseDatabase();
       
   499 	test.Next(_L("Delete database"));
       
   500 
       
   501 	CntTest->DeleteDatabaseL();
       
   502     }
       
   503 
       
   504 GLDEF_C TInt E32Main()
       
   505 	{
       
   506 	__UHEAP_MARK;
       
   507     CntTest=new(ELeave) CCntTest;
       
   508 	CntTest->ConstructL(test,KDatabaseFileName);
       
   509     TRAPD(err,DoTestsL());
       
   510 	CntTest->EndTestLib(err);
       
   511 	__UHEAP_MARKEND;
       
   512 	return KErrNone;
       
   513     }