summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_owncar.cpp
blob: ea027796bbf6cfceb6aa62da3b0058cb2754af80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


#include <e32test.h>
#include <f32file.h>
#include <s32file.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include <bautils.h>
#include <barsc.h>
#include <barsread.h>
#include <cntmodel.rsg>
#include "t_utils.h"

CCntTest* CntTest=NULL;
LOCAL_D RTest test(_L("T_OWNCAR"));

const TPtrC KDatabaseFileName=_L("C:T_OWNCAR");

const TPtrC KTestName=_L("Test Name No%d");
const TPtrC KTestAddress=_L("Address %d");
const TInt KTotalNumRecords=50;
const TInt KTotalNumOwnCards=1;


#define KCardGivenName _L("Given Name #%d")
#define KCardFamilyName _L("Family Name #%d")
#define KCardFieldText _L("Card id %d, text field #%d")
#define KCardFieldEditedText _L("UPDATED Card id %d, text field #%d")

static const TInt KTimeout = 20000000;//20 seconds timeout

class CMyObserver :  public CActive, public MContactDbObserver
		{
	public:
		CMyObserver() : CActive(EPriorityIdle)
			{
			iStart.UniversalTime();
			}
		void Activate();
				
	private: // From CActive.
		void RunL();
		void DoCancel();
		TInt RunError(TInt aError);

	private: // from MContactDbObserver
		void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
		
	private:
		TTime 	iStart;
		TTime 	iEnd;
		};

void CMyObserver::RunL()
	{
	iEnd.UniversalTime();
	//Timeout has been excedeed, stop waiting
	if( iEnd.MicroSecondsFrom( iStart.Int64() ) > KTimeout )
		{
		test.Printf( _L("Observer has timed out, EContactDbObserverEventOwnCardChanged notification not received"));
		CActiveScheduler::Stop();
		}
	else //waiting for next event
		{
		Activate();
		}
	}

void CMyObserver::DoCancel()
	{
	}

TInt CMyObserver::RunError(TInt aError)
	{
	test.Printf( _L("CMyObserver:: Error in runL: %d"), aError );
	return aError;
	}

void CMyObserver::Activate()
	{
	if(!IsActive())
		{
		TRequestStatus *pS=&iStatus;
		User::RequestComplete(pS,KErrNone);
		SetActive();
		}
	}

void CMyObserver::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
	{  
	switch( aEvent.iType )
		{
		case EContactDbObserverEventOwnCardChanged:
			{
			test.Printf( _L("Own card changed event id: %d, contact id: %d"), aEvent.iType, aEvent.iContactId );
			Cancel();
			CActiveScheduler::Stop();
			break;
			}
		default:
			{
			test.Printf( _L("Other event id: %d, contact id: %d"), aEvent.iType, aEvent.iContactId );
			Activate();
			break;
			}
		}
	}

GLDEF_C void Panic(TInt aPanic)
//
// Panic the thread with CNTMODEL as the category
//
	{
	User::Panic(_L("T_OWNCAR"),aPanic);
	}

LOCAL_C void SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName, TBool aAddField)
//
// Set the contents of a text field, creating the field if required
//
	{
	CContactItemFieldSet& fieldSet=aItem.CardFields();
	const TInt pos=fieldSet.Find(aType);
	if (!aAddField && pos!=KErrNotFound)
		fieldSet[pos].TextStorage()->SetTextL(aName);
	else
		{
		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,aType);
   		field->SetMapping(KUidContactFieldVCardMapUnusedN);
		field->TextStorage()->SetTextL(aName);
		aItem.AddFieldL(*field);
		CleanupStack::Pop(); // field
		}
	}

LOCAL_C void PopulateDatabaseL(TBool aPhoneNumbers)
//
// Create and populate the database
//
	{
	for (TInt ii=0;ii<KTotalNumRecords;ii++)
		{
		CContactItem* item=CContactCard::NewLC();
		TBuf<16> name;
		name.Format(KTestName,ii);
   		SetNameL(*item,KUidContactFieldFamilyName,name,ETrue);
		if (aPhoneNumbers)
			{
			TBuf<20> number;
			switch(ii%3)
				{
				case 0:
					number.Format(_L("0171-%03d %04d"),(ii*9)%1000,((ii+11)*23)%10000);
					break;
				case 1:
					number.Format(_L("%04d:%04d:%04d:%04d"),(ii*123)%10000,(ii*666)%10000,(ii*234)%10000);
					break;
				case 2:
					number.Format(_L("+00%d-%03d %04d"),(ii*123)%100,(ii*13)%1000,((ii+13)*17)%10000);
					break;
				}
   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
			if (!(ii%2))
				{
				number.Format(_L("0181-%03d %04d"),(ii*8)%1000,((ii+11)*22)%10000);
	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
				number.Format(_L("01734-%06d"),(ii*123456)%1000000);
	   			SetNameL(*item,KUidContactFieldPhoneNumber,number,ETrue);
				}
			}
		if (ii%2)
			{
			TBuf<16> address;
			address.Format(KTestAddress,ii);
			CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldAddress);
			field->SetMapping(KUidContactFieldVCardMapADR);
			field->TextStorage()->SetText(address.AllocL());
			field->SetTemplateField(ETrue);
			item->AddFieldL(*field);
			CleanupStack::Pop(); // field
			}
		if (ii%3)
			{
			CContactItemField* field=CContactItemField::NewLC(KStorageTypeDateTime,KUidContactFieldBirthday);
			field->DateTimeStorage()->SetTime(TDateTime(1990+ii,(TMonth)(ii%12),(ii*3)%28,1,2,3,4));
			field->SetTemplateField(ETrue);
			item->AddFieldL(*field);
			CleanupStack::Pop(); // field
			}
		if (ii%4)
			{
			CContactItemField* field=CContactItemField::NewLC(KStorageTypeStore,KUidContactFieldVCardMapAGENT);
    		field->SetMapping(KUidContactFieldVCardMapAGENT);
			field->SetTemplateField(ETrue);
			item->AddFieldL(*field);
			CleanupStack::Pop(); // field
			}
		if (ii%6)
			{
			CContactItemField* field=CContactItemField::NewLC(KStorageTypeContactItemId);
			field->SetMapping(KUidContactFieldVCardMapAGENT);
			field->SetTemplateField(ETrue);
			item->AddFieldL(*field);
			CleanupStack::Pop(); // field
			}
		CntTest->Db()->AddNewContactL(*item);
		CleanupStack::PopAndDestroy(); // item
		}
	CntTest->Db()->SetDateFormatTextL(_L("%E%D%X%N%Y %1 %2 %3"));
	}



//////////////////////////////////////////////////////////////////////
//
//	main test modules
//
//////////////////////////////////////////////////////////////////////


LOCAL_C void CheckOwnCardCreatedL()
//
// make sure own card is the only card in db
//
	{
	test.Next(_L("Check Own Card Created"));

	CContactItem* newOwnCard = CntTest->Db()->CreateOwnCardL();
	delete newOwnCard;
	test(CntTest->Db()->CountL()==KTotalNumOwnCards);
	// check own card uid is set
	if (CntTest->Db()->OwnCardId()==NULL)
		User::Leave(KErrNotFound);
	CContactItem* tempOwnCard = CntTest->Db()->ReadContactL(CntTest->Db()->OwnCardId());
	delete tempOwnCard;
	}

LOCAL_C void SetOwnCardL(TInt aUid)
//
//  set an existing card to be new own card
//
	{
	test.Next(_L("Set Own Card"));

	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
	CleanupStack::PushL(tempCard);
	CntTest->Db()->SetOwnCardL(*tempCard);
	if(CntTest->Db()->OwnCardId()!=tempCard->Id())
		User::Leave(KErrNotFound);
	CleanupStack::PopAndDestroy(); // tempCard
	}


LOCAL_C void SetBadOwnCardL()
//
//	try to set a bad card as the new own card
//
	{
	test.Next(_L("Set Bad Own Card"));

	CContactItem* badOwnCard=CContactCard::NewLC();
	CntTest->Db()->SetOwnCardL(*badOwnCard);
	CleanupStack::PopAndDestroy(badOwnCard);
	}


LOCAL_C void GetOwnCardL(TInt aUid)
//
//	check own card persistence
//
	{
	test.Next(_L("Check Persistence"));

	if(CntTest->Db()->OwnCardId()!=aUid)
		User::Leave(KErrNotFound);
	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
	delete tempCard;
	}


LOCAL_C void DeleteOwnCardL()
//
//	delete the own card & check persistence
//
	{
	test.Next(_L("Delete Own Card"));

	CntTest->Db()->DeleteContactL(CntTest->Db()->OwnCardId());

	CntTest->CloseDatabase();
	CntTest->OpenDatabaseL();
	
	// ensure id is reset.
	if (CntTest->Db()->OwnCardId()!=KNullContactId)
		User::Leave(KErrNotFound);
	}


LOCAL_C void SetGoodOwnCardL(TInt aUid)
//
//	after deleting own card set a new one
//
	{
	test.Next(_L("Set Good Own Card"));

	// ownCard should be null.
	CContactItem* tempCard = CntTest->Db()->ReadContactL(aUid);
	CntTest->Db()->SetOwnCardL(*tempCard);
	delete tempCard;
	}

LOCAL_C void CreateOwnCardTwiceL()
//
//	Create own card twice and see ok
//
	{
	test.Next(_L("Create Own Card Twice"));


	CContactItem* newOwnCard = CntTest->Db()->CreateOwnCardL();
	CContactItem* newOwnCard2 = CntTest->Db()->CreateOwnCardL();
	delete newOwnCard;
	delete newOwnCard2;
	}

LOCAL_C void SetContactFieldsL(CContactDatabase &aDb, const TContactItemId aId)
	{
	static TInt i = 0;
	CContactItem *citem = aDb.OpenContactLX(aId);
	CleanupStack::PushL( citem );
	TBuf<16> name;
	name.Format(KTestName,++i);
	SetNameL(*citem,KUidContactFieldFamilyName,name,EFalse);
	SetNameL(*citem,KUidContactFieldPhoneNumber,name,EFalse);
	SetNameL(*citem,KUidContactFieldGivenName,name,EFalse);
	SetNameL(*citem,KUidContactFieldAdditionalName,name,EFalse);
	SetNameL(*citem,KUidContactFieldEMail,name,EFalse);
	SetNameL(*citem,KUidContactFieldCompanyName,name,EFalse);
	SetNameL(*citem,KUidContactFieldAddress,name,EFalse);
	aDb.CommitContactL(*citem);
	CleanupStack::PopAndDestroy(citem);
	CleanupStack::Pop();//lock
	}

LOCAL_C void WaitForNotificationsL(CContactDatabase &aDb)
	{
	CMyObserver *observe = new (ELeave) CMyObserver();
	CleanupStack::PushL( observe );
	CContactChangeNotifier *notifier = CContactChangeNotifier::NewL(aDb, observe);
	CleanupStack::PushL( notifier );
	CActiveScheduler::Add(observe);
	observe->Activate();
	CActiveScheduler::Start();
	CleanupStack::PopAndDestroy(notifier);
	CleanupStack::PopAndDestroy(observe);
	}
	
LOCAL_C void SetOwnCardL(CContactDatabase &aDb, const TContactItemId aId)
	{
	test.Printf(_L("Change owncard from: %d to: %d"), aDb.OwnCardId(), aId);
	CContactItem *citem = aDb.ReadContactLC(aId);
	aDb.SetOwnCardL( *citem );
	CleanupStack::PopAndDestroy(citem);
	}
 
/**
@SYMTestCaseID     PIM-T-OWNCAR-0002
@SYMTestType UT
@SYMTestPriority Medium
@SYMDEF DEF085699
@SYMTestCaseDesc 
Multiple Client Owncard Test
1) Create a contact and set it as the own card
2) Create another contact
3) Set the new contact to be the own card
4) Modify the first contact created (i.e not the one currently set to the own card)
5) View result of a CContactDatabase::OwnCardId() call in both the applications

@SYMTestActions 
Create Sessions A and B to the database
add two contacts using session A
Set contact 1 to own card using session A
Set contact 2 to own card using session A
Update Contact 1 using session A

@SYMTestExpectedResults 
Owncard should equal contact 2 in for both sessions
*/

LOCAL_C void MultipleClientOwncardTestL()
	{
	test.Next(_L("@SYMTESTCaseID:PIM-T-OWNCAR-0002 Multiple Client OwnCard Test"));

	CContactDatabase *db = CContactDatabase::ReplaceL( KDatabaseFileName );
	CContactDatabase *db2 = CContactDatabase::OpenL( KDatabaseFileName );
	CleanupStack::PushL( db );
	CleanupStack::PushL( db2 );
	CContactItemViewDef *viewAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
	viewAll->AddL(KUidContactFieldMatchAll);
	// Retrieve the system template
	CContactTemplate* ttemplate = static_cast< CContactTemplate* >
												( db->ReadContactLC( db->TemplateId(), *viewAll ) );
	// Create a new contact based on the system template
	CContactCard* contact = CContactCard::NewLC(ttemplate);
	
	// Add the new contact to the db
	TContactItemId id1 = db->AddNewContactL(*contact);
	SetContactFieldsL(*db,id1);
	
	test(db->OwnCardId() == db2->OwnCardId());
	SetOwnCardL(*db, id1);
	WaitForNotificationsL(*db2);

	TContactItemId oid1 = db->OwnCardId();
	TContactItemId oid2 = db2->OwnCardId();
	test( (oid1 == oid2) && (oid1 == id1) );
	
	test.Printf(_L("Add new contact; check that it is different from existing contacts"));
	TContactItemId id2 = db->AddNewContactL(*contact);
	SetContactFieldsL(*db,id2);
	test(id1 != id2);
	
	SetOwnCardL(*db, id2);
	WaitForNotificationsL(*db2);

	oid1 = db->OwnCardId();
	oid2 = db2->OwnCardId();
	test( (oid1 == oid2) && (oid1 == id2) );
	
	test.Printf(_L("modify contact 1, check that its not marked as an own card by either session"));
	SetContactFieldsL(*db,id1);
	WaitForNotificationsL(*db2);
	oid1 = db->OwnCardId();
	oid2 = db2->OwnCardId();
	test( (oid1 == oid2) && (oid1 == id2) );
	
	
	CleanupStack::PopAndDestroy(contact);
	CleanupStack::PopAndDestroy(ttemplate);
	CleanupStack::PopAndDestroy(viewAll);
	CleanupStack::PopAndDestroy(db2);
	CleanupStack::PopAndDestroy(db);
	}

/**

@SYMTestCaseID     PIM-T-OWNCAR-0001

*/

void DoTestsL()
    {
    MultipleClientOwncardTestL();
	test.Start(_L("@SYMTESTCaseID:PIM-T-OWNCAR-0001 Create new database"));

	TRAPD(err,CntTest->CreateDatabaseL());
	test(err==KErrNone);
	CntTest->Db()->OwnCardId();
	CntTest->CloseDatabase();
	CntTest->OpenDatabaseL();
	CntTest->Db()->OwnCardId();
	TRAP(err,CheckOwnCardCreatedL());
	test(err==KErrNone);
	CntTest->Db()->DeleteContactL(CntTest->Db()->OwnCardId());
	CntTest->CloseDatabase();
	CntTest->OpenDatabaseL();
	TRAP(err,PopulateDatabaseL(ETrue));
	test(err==KErrNone);
	TRAP(err,SetOwnCardL(CntTest->Db()->CountL()-2));
	test(err==KErrNone);
	TRAP(err,GetOwnCardL(CntTest->Db()->CountL()-2));
	test(err==KErrNone);
	TRAP(err,SetBadOwnCardL());
	test(err==KErrNotFound);
	CntTest->CloseDatabase();
	CntTest->OpenDatabaseL();
	TRAP(err,DeleteOwnCardL());
	test(err==KErrNone);
	TRAP(err,SetGoodOwnCardL(CntTest->Db()->CountL()-15));
	test(err==KErrNone);
	TRAP(err,CreateOwnCardTwiceL());
	test(err==KErrNone);
	CntTest->CloseDatabase();
	test.Next(_L("Delete database"));

	CntTest->DeleteDatabaseL();
    }

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
    CntTest=new(ELeave) CCntTest;
	CntTest->ConstructL(test,KDatabaseFileName);
    TRAPD(err,DoTestsL());
	CntTest->EndTestLib(err);
	__UHEAP_MARKEND;
	return KErrNone;
    }