summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_dbase.cpp
blob: 4ef13d90f2d7537daca80182a2ef39b70883ad8d (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
/*
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* 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 "t_utils.h"

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

const TPtrC KDatabaseFileName=_L("C:T_DBASE.cdb");

const TInt KNumTestContacts=20;

LOCAL_C void CreateDatabaseL()
//
// Create a database in a store
//
	{
	CntTest->TestLockServerCloses();
	CntTest->CreateDatabaseL();
	CntTest->TestLockServerExists();
	CntTest->DeleteAllTemplateFieldsL();
	CntTest->CloseDatabase();
	CntTest->TestLockServerCloses();
	}
	
LOCAL_C void OpenDatabaseL()
//
// Open an existing database in a store
//
	{
	CContactDatabase* db=CContactDatabase::OpenL(KDatabaseFileName);
	CntTest->TestLockServerExists();
	delete db;
	CntTest->TestLockServerCloses();
	}

LOCAL_C TPtrC Name(CContactItem& aItem)
	{
	CContactItemFieldSet& fieldSet=aItem.CardFields();
	const TInt pos=fieldSet.Find(KUidContactFieldFamilyName);
	if (pos==KErrNotFound)
		return _L("");
	return fieldSet[pos].TextStorage()->Text();
	}

LOCAL_C void SetDriveErrMsg()
	{
	test.Printf(_L("Error: For this test to run properly "));
#if defined(__WINS__)
	test.Printf(_L("_EPOC_DRIVE_D must be set up to create a valid drive D:\n"));
#else
	test.Printf(_L("a writable D: must exist\n"));
#endif
	test.Getch();
	}

LOCAL_C void TestDriveSettingL()
	{
	RFs fs;
	test(fs.Connect()==KErrNone);

	TDriveInfo driveInfo;
	if (fs.Drive(driveInfo,EDriveD)!=KErrNone || driveInfo.iType==EMediaNotPresent || driveInfo.iMediaAtt&KMediaAttWriteProtected)
		SetDriveErrMsg();
	else
		{
		TInt err=fs.MkDirAll(_L("D:\\___t___\\"));
		if (err!=KErrNone)
			SetDriveErrMsg();
		else
			{
			fs.RmDir(_L("D:\\___t___\\"));
			TUint att;
			TFileName name;
			CContactDatabase::GetDefaultNameL(name);
			if (fs.Att(name,att)==KErrNotFound)
				{
				CContactDatabase *createDb=CContactDatabase::CreateL();
				delete createDb;
				}
			TDriveUnit driveUnit;
			CContactDatabase::DatabaseDrive(driveUnit);
			if (driveUnit!=TDriveUnit(EDriveC))
				CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC));
		//
		// NEEDS _EPOC_DRIVE_D SET TO POINT TO A VALID DIRECTORY NAME
		//
			CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveD));
			test(CContactDatabase::DatabaseDrive(driveUnit));
			test(driveUnit==TDriveUnit(EDriveD));
		// _EPOC_DRIVE_P Must not exist for the following test
			TRAPD(err,CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveP)));
			test(err!=KErrNone);
			test(CContactDatabase::DatabaseDrive(driveUnit));
			test(driveUnit==TDriveUnit(EDriveD));
		//
			CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC));
			test(CContactDatabase::DatabaseDrive(driveUnit));
			test(driveUnit==TDriveUnit(EDriveC));

			//
			TDriveUnit cColon(EDriveC);
			CContactDatabase::SetDatabaseDriveL(cColon);	// Leave it set to C: for further tests
			test(CContactDatabase::DatabaseDrive(driveUnit));
			test(driveUnit==cColon);
			}
		}
	fs.Close();
	CntTest->TestLockServerCloses();
	}

LOCAL_C void TestDeleteFileL()
	{
	TFileName defaultName;
	CContactDatabase::GetDefaultNameL(defaultName);
	test(CntTest->Fs().SetAtt(defaultName,0,KEntryAttReadOnly)==KErrNone);
	CntTest->TestLockServerCloses();
	CContactDatabase *db=CContactDatabase::ReplaceL();
	delete db;
	CntTest->TestLockServerCloses();
	TUint att;
	test(CntTest->Fs().Att(defaultName,att)==KErrNone);
	CContactDatabase::DeleteDefaultFileL();
	test(CntTest->Fs().Att(defaultName,att)==KErrNotFound);
//
	db=CContactDatabase::CreateL();
	delete db;
	test(CntTest->Fs().Att(defaultName,att)==KErrNone);
	test(CntTest->Fs().SetAtt(defaultName,KEntryAttReadOnly,0)==KErrNone);
	TRAPD(readOnlyErr,CContactDatabase::DeleteDefaultFileL());
	test(readOnlyErr==KErrAccessDenied);
	test(CntTest->Fs().SetAtt(defaultName,0,KEntryAttReadOnly)==KErrNone);
	CContactDatabase::DeleteDefaultFileL();
	}

LOCAL_C void AddNewContactsL(CArrayFix<TContactItemId>* aIdList)
//
// Add KNumTestContacts new contacts with varying numbres of text fields
//
	{
	CntTest->TestLockServerCloses();
	CContactDatabase* db=CntTest->OpenDatabaseL();
	CntTest->TestLockServerExists();
	CContactCard* card=CContactCard::NewL();
	CleanupStack::PushL(card);
	TContactItemId id=KNullContactId;
	TContactItemId newId=KNullContactId;
	for (TInt ii=0;ii<KNumTestContacts;ii++)
		{
		TBuf<16> name;
		name.Format(_L("NAME #%d"),ii);
		SetNameL(*card,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText);
		card->AddFieldL(*field);
		CleanupStack::Pop(); // field
		newId=db->AddNewContactL(*card);
		id=newId;
		aIdList->AppendL(id);
		}
	CleanupStack::PopAndDestroy(); // card
	CntTest->CloseDatabase();
	CntTest->TestLockServerCloses();
	}

LOCAL_C void EditContactsL(CArrayFix<TContactItemId>* aIdList)
//
// Check then edit contact names
//
	{
	CContactDatabase* db=CntTest->OpenDatabaseL();
	TInt ii=0;
	for (;ii<KNumTestContacts;ii++)
		{
		const TInt index=(*aIdList)[ii];
		CContactItem* item=db->OpenContactL(index);
		CleanupStack::PushL(item);
		TBuf<16> name;
		name.Format(_L("NAME #%d"),ii);
		test(name==Name(*item));
		name.Format(_L("NEW NAME #%d"),index);
		SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
		db->CommitContactL(*item);
		CleanupStack::PopAndDestroy(); // item;
		}
	for (ii=0;ii<KNumTestContacts;ii++)
		{
		const TInt index=(*aIdList)[ii];
		CContactItem* item=db->ReadContactL(index);
		TBuf<16> name;
		name.Format(_L("NEW NAME #%d"),index);
		test(name==Name(*item));
		delete item;
		}
	for (ii=0;ii<KNumTestContacts;ii++)
		{
		const TInt index=(*aIdList)[ii];
		CContactItem* item=db->OpenContactLX(index);
		delete item;
		CleanupStack::PopAndDestroy(); // close
		}

	// try to read a non-existant contact
	TRAPD(err,db->ReadContactL(KNullContactId));
	test(err!=KErrNone);
	// try to edit a contact that hasn't been opened
	CContactItem* item=db->ReadContactLC((*aIdList)[0]);
	TRAP(err,db->CommitContactL(*item));
	CleanupStack::PopAndDestroy(); // item
	test(err==KErrAccessDenied);
	// try to close a non-existant contact
	TRAP(err,db->CloseContactL(KNullContactId));
	test(err==KErrNone); // test changed from 001 (using DBMS 043) - this test makes more sense anyway
	// try to close a contact that hasn't been opened
	TRAP(err,db->CloseContactL((*aIdList)[0]));
	test(err==KErrNone);
	
	CntTest->CloseDatabase();
	}

LOCAL_C void DeleteContactsL(CArrayFix<TContactItemId>* aIdList)
//
// Delete all contacts
//
	{
	CContactDatabase* db=CntTest->OpenDatabaseL();

	// try to delete a contact that is open
	const TContactItemId id=(*aIdList)[0];
	CContactItem* item=db->OpenContactL(id);
	CleanupStack::PushL(item);
	TRAPD(err,db->DeleteContactL(id));

	CleanupStack::PopAndDestroy(); // item
	test(err==KErrInUse);
	db->CloseContactL(id);
	// delete all contacts
	for (TInt ii=0;ii<KNumTestContacts;ii++)
		{
		const TInt index=(*aIdList)[ii];
		db->DeleteContactL(index);
		}
	// try to delete a non-existant contact
	TRAP(err,db->DeleteContactL(KNullContactId));
	test(err!=KErrNone);
	CntTest->CloseDatabase();
	}

void TestErr(TInt aErr)
    {
	if (aErr!=KErrNone)
		{
		test.Printf(_L("Error %d\n"),aErr);
		test(EFalse);
		}
	}
	
/**

@SYMTestCaseID     PIM-T-DBASE-0001

*/


void DoTestsL()
    {
	test.Start(_L("@SYMTESTCaseID:PIM-T-DBASE-0001 T_DBASE"));

	test.Next(_L("Test drive setting"));

	test.Printf(_L("The following tests will delete your default contacts file"));
	test.Printf(_L("Press to 'd' to run the test or any other key to skip them\n"));
	if (test.Getch()=='d')
		{
		test.Next(_L("Set Drive"));

		TRAPD(ret,TestDriveSettingL());
		TestErr(ret);
		test.Next(_L("Delete database file"));

		TRAP(ret,TestDeleteFileL());
		TestErr(ret);
		}
	test.Next(_L("Create new database"));

	TRAPD(ret,CreateDatabaseL());
	TestErr(ret);
	test.Next(_L("Re-open existing database"));

	TRAP(ret,OpenDatabaseL());
	TestErr(ret);
	CArrayFix<TContactItemId>* idList=new(ELeave) CArrayFixFlat<TContactItemId>(5);
	CleanupStack::PushL(idList);
	test.Next(_L("Add new contacts"));

	TRAP(ret,AddNewContactsL(idList));
	TestErr(ret);
	test.Next(_L("Edit contacts"));

	TRAP(ret,EditContactsL(idList));
	TestErr(ret);
	test.Next(_L("Delete contacts"));

	TRAP(ret,DeleteContactsL(idList));
	CleanupStack::PopAndDestroy(); // idList
	TestErr(ret);
	test.Next(_L("Delete empty 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;
    }