summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_contactdbevent.cpp
blob: 1799af7c4abf1de4585e9e3199db7c8d0a15b5ed (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
/*
* 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 <cntdb.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfilt.h>
#include <cntfldst.h>

RTest test(_L("t_contactdbevent"));

_LIT(KEventDbFileName,"c:t_contactdbevent.cdb");
const TInt KTimeMicroSec = 1000000;


class CPbObserver : public CTimer, public MContactDbObserver 
	{
public:
	static CPbObserver* NewL(TUint aConnectionId);
	void ConstructL();
	~CPbObserver(){}
public:
	//From MContactDbObserver
	void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
protected:
	CPbObserver(TUint aConnectionId);
	void RunL();
protected:
	TUint iPbConnectionId;	
	};

class CPbTester : public CBase 
	{
public:
	static CPbTester* NewL();
	~CPbTester();
	void StartTestL();
protected:
	void ConstructL();
	TContactItemId AddEntryL();
	void EditEntryL(TContactItemId itemId);

protected:
	CPbObserver* ipbObSvr;
	CContactDatabase* ipbDb;
	CContactChangeNotifier* ipbNotifier;	
	};

CPbObserver* CPbObserver::NewL(TUint aConnectionId)
	{
	CPbObserver* self = new (ELeave) CPbObserver(aConnectionId);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

void CPbObserver::ConstructL()
	{
	CTimer::ConstructL();
	}

void CPbObserver::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
	{
	if (aEvent.iType == EContactDbObserverEventContactChanged)
		{
		test.Printf(_L("Received a Contact changed event"));
		}
	if (aEvent.iType == EContactDbObserverEventContactDeleted)
		{
		test.Printf(_L("Received a Contact delete event"));
		}
	if (aEvent.iType == EContactDbObserverEventContactAdded) 
		{
		test.Printf(_L("Received a Contact added event"));
		}
	test (aEvent.iConnectionId == iPbConnectionId);
	}


CPbObserver::CPbObserver(TUint aConnectionId): CTimer(EPriorityStandard), 
                                               iPbConnectionId(aConnectionId)
	{
	CActiveScheduler::Add(this);
	}

void CPbObserver::RunL()
	{
	CActiveScheduler::Stop();
	}

CPbTester* CPbTester::NewL(void)
	{
	CPbTester *self = new(ELeave)CPbTester;
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

void CPbTester::ConstructL(void)
	{
	ipbDb=CContactDatabase::ReplaceL(KEventDbFileName);
	ipbObSvr = CPbObserver::NewL(ipbDb->ConnectionId());
	ipbNotifier = CContactChangeNotifier::NewL(*ipbDb, ipbObSvr);
	}

CPbTester::~CPbTester(void)
	{
	TRAP_IGNORE(ipbDb->DeleteDatabaseL(KEventDbFileName));
	delete ipbNotifier;
	delete ipbObSvr;
	delete ipbDb;
	}

void CPbTester::StartTestL()
	{
	TContactItemId id = AddEntryL();
	EditEntryL(id);
	ipbObSvr->After(KTimeMicroSec); //Allow CContactChangeNotifier to run
	CActiveScheduler::Start();
	}

TContactItemId CPbTester::AddEntryL()
	{
	_LIT(KForename,"John"); 
	_LIT(KSurname,"Smith"); 
	_LIT(KPhoneNumber,"+441617779700"); 

	// Create a  contact card to contain the data
	CContactCard* newCard = CContactCard::NewLC();

	// Create the firstName field and add the data to it
	CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
	firstName->TextStorage()->SetTextL(KForename);
	newCard->AddFieldL(*firstName);
	CleanupStack::Pop(firstName);

	// Create the lastName field and add the data to it
	CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
	lastName ->TextStorage()->SetTextL(KSurname);
	newCard->AddFieldL(*lastName);
	CleanupStack::Pop(lastName);

	// Create the phoneNo field and add the data to it
	CContactItemField* phoneNumber = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber);
	phoneNumber->SetMapping(KUidContactFieldVCardMapTEL);
	phoneNumber ->TextStorage()->SetTextL(KPhoneNumber);
	newCard->AddFieldL(*phoneNumber);
	CleanupStack::Pop(phoneNumber);

	// Add newCard to the database
	const TContactItemId contactId = ipbDb->AddNewContactL(*newCard);

	CleanupStack::PopAndDestroy(newCard);

	return contactId;
	}

void CPbTester::EditEntryL(TContactItemId itemId)
	{
	_LIT(KEmailAddress,"john.smith@symbian.com"); 
	CContactItem *item = ipbDb->OpenContactL(itemId);
	CContactCard* card = NULL;
	CleanupStack::PushL(item);

	card = (CContactCard*)item;
	// Create the emailAddress field and add the data to it
	CContactItemField* emailAddr = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldEMail);
	emailAddr->SetMapping(KUidContactFieldVCardMapEMAILINTERNET);
	emailAddr ->TextStorage()->SetTextL(KEmailAddress);
	card->AddFieldL(*emailAddr);
	CleanupStack::Pop(emailAddr);

	ipbDb->CommitContactL(*item);
	CleanupStack::PopAndDestroy(item);
	}

void RunTestL()
	{
	CPbTester* tester = CPbTester::NewL();
	CleanupStack::PushL(tester);
	tester->StartTestL();
	CleanupStack::PopAndDestroy(tester);
	}

/**

@SYMTestCaseID     PIM-T-CONTACTDBEVENT-0001

*/

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	CActiveScheduler* scheduler=new CActiveScheduler;
	CActiveScheduler::Install(scheduler);
	CTrapCleanup* cleanup = CTrapCleanup::New();
	test.Start(_L("@SYMTESTCaseID:PIM-T-CONTACTDBEVENT-0001 Testing Database event"));

	TRAPD(err,RunTestL());
	test(err==KErrNone);
	test.End();
	test.Close();
	delete cleanup;
    delete scheduler;
	__UHEAP_MARKEND;
	return KErrNone;
	}