summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/cntvcard/src/t_geo.cpp
blob: 35e320e136ad3fb974af70fc95c6229a436e5f4d (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
/*
* Copyright (c) 2007-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: 
* CR 1320 - REQ 8766
* Test harness for testing the import and export behaviour for the vCard GEO property.
* 1) Check that data can be added programatically
* 2) Check that the exported format is as expected
* 3) Check that vCard can be imported and the data is as expected.
* GEO property has the form of 
* GEO:[lat],[lon]
* The latitude and longitude can have upto 6 decimal places, there is also a range from -90 to 90 for the latitude 
* and -180 to 180 for the longitude, however there isn't any validation done. Invalid information is imported and 
* exported as it is.
* Include Files  
*
*/


#include <e32base.h>
#include <e32std.h>
#include <e32test.h>
#include "t_utils.h"
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
#include "cntdb_internal.h"
#endif


//  Global Variables
LOCAL_D CCntTest* CntTest=NULL;
LOCAL_D const TPtrC KDatabaseFileName=_L("C:vgeo.cdb");
LOCAL_D RTest test(_L("T_VGEO"));
LOCAL_D RFs fileSession;
const TInt KNumOfGEOTest = 6;

_LIT(KGEOVCardPath,"c:\\ExportGEO.vcf");

_LIT(KGEOStandard,"-45.0,122.0");
_LIT(KGEOMaxDecimalPlaces,"-45.123456,-122.123456");
_LIT(KGEONonRealNum,"ABCDEFG,HIJKLMNOP");
_LIT(KGEOOutOfRange,"1222.000,3253.0000");
_LIT(KGEOSingleLonValue,",23.00");
_LIT(KGEOSingleLatValue,"-86.00");

const TPtrC KGeoValues[KNumOfGEOTest] =
	{
		KGEOStandard (),
		KGEOMaxDecimalPlaces (),
		KGEONonRealNum (),
		KGEOOutOfRange (),
		KGEOSingleLonValue (),
		KGEOSingleLatValue ()
	};

//Verify the vCard. Check that the pattern is present and the number of time the pattern should be present.
TBool CheckExportedFileL(const TDesC& aVCardPath, const TDesC8& aPattern, TInt aCount = 1)
	{
	TInt counter = 0;
	RFileReadStream fileReadStream;
	CleanupClosePushL(fileReadStream);
    User::LeaveIfError(fileReadStream.Open(fileSession, aVCardPath, EFileRead));
    
    TBuf8<0x80> bufO;
    TInt err = KErrNone;
    do {
	    TRAP(err, fileReadStream.ReadL(bufO, TChar(0xa)));
	    if (err != KErrNone && err != KErrEof)
		    {
		    User::Leave(err);	
		    }
	    if (bufO.Find(aPattern)!= KErrNotFound)
	    	{
	    	++counter;
	    	}
	    } while (err != KErrEof);
    CleanupStack::PopAndDestroy(&fileReadStream);
    return (counter == aCount);
	}

//  Local Functions
LOCAL_C TContactItemId AddContactToDbL(const TDesC& aGeoValue)
	{
	_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 a Geo field and add the geo value to it
    CContactItemField* geoLoc = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGEO);
    geoLoc->SetMapping(KUidContactFieldVCardMapGEO);
    geoLoc->TextStorage()->SetTextL(aGeoValue);
    newCard->AddFieldL(*geoLoc);
    CleanupStack::Pop(geoLoc);
        
    // 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 = CntTest->Db()->AddNewContactL(*newCard);
    CleanupStack::PopAndDestroy(newCard);
    return contactId;
	}

LOCAL_C void ExportVCardL(const TDesC& aVCardPath, CContactDatabase* aContactDB, const CContactIdArray& aContactIdArray, TVCardVersion aVer = EVCard21)
	{
	RFileWriteStream writeStream;
	CleanupClosePushL(writeStream);
    User::LeaveIfError(writeStream.Replace(fileSession, aVCardPath, EFileWrite));
    if (aVer == EVCard21)
    	{
        aContactDB->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), aContactIdArray, writeStream, 0);
    	}
    else
    	{
    	aContactDB->ExportSelectedContactsL(TUid::Uid(KUidPBAPVCardConvImpl), aContactIdArray, writeStream, CContactDatabase::EDefault, 0, NULL, aVer, ETrue);
    	}
    CleanupStack::PopAndDestroy(&writeStream);
	}

LOCAL_C CArrayPtr<CContactItem>* ImportVCardL(const TDesC& aVCardPath, CContactDatabase* aContactDB)
	{
	TBool success=EFalse;
	RFileReadStream fileReadStream;
	CleanupClosePushL(fileReadStream);
	User::LeaveIfError(fileReadStream.Open(fileSession, aVCardPath, EFileRead));
	CArrayPtr<CContactItem>* contactItems = aContactDB->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),
			fileReadStream, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
	CleanupStack::PopAndDestroy(&fileReadStream);
	test (success);
	return contactItems;
	}

LOCAL_C TBool DoExportTestL( const TDesC& aGeoValue)
	{
	TBuf8<255> geoValueBuf8;
	geoValueBuf8.Copy(aGeoValue);
	CntTest->CreateDatabaseL();
	
	TContactItemId contactId = AddContactToDbL(aGeoValue);
	CContactIdArray* contactIdArray =  CContactIdArray::NewLC();
    contactIdArray->AddL(contactId);
	ExportVCardL(KGEOVCardPath, CntTest->Db(), *contactIdArray);//Exporting the contact
	CleanupStack::PopAndDestroy(contactIdArray);
	
    TBool success = CheckExportedFileL(KGEOVCardPath, geoValueBuf8);
    test (success);
    CntTest->CloseDatabase();
    return success;
	}

LOCAL_C TBool TestImportedContact(const CContactItem& aContactItem, const TDesC& aGeoValue)
	{
	TBool success = EFalse;
	const CContactItemFieldSet& contactFieldSet = aContactItem.CardFields();
	TInt pos = contactFieldSet.Find(KUidContactFieldGEO, KUidContactFieldVCardMapGEO);
	if (pos != KErrNotFound)
		{
		const CContactTextField*  contactTextField = contactFieldSet[pos].TextStorage();
		success = (contactTextField->Text().Compare(aGeoValue) == 0);
		test (success);
		}
	 return success;
	}

LOCAL_C TBool DoImportTestL(const TDesC& aGeoValue)
	{
	//Reset the Db
	CntTest->DeleteDatabaseL();
	CntTest->CreateDatabaseL();
	
	CArrayPtr<CContactItem>* contactItems = ImportVCardL(KGEOVCardPath, CntTest->Db());
	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,contactItems));
	test (contactItems->Count() == 1);
	CContactItem* contactItem = contactItems->At(0);
    TBool success =  TestImportedContact(*contactItem, aGeoValue);
	test (success);
	CleanupStack::PopAndDestroy();
	return success;
	}
/**
@SYMREQ 8766
@SYMTestType CIT
@SYMTestPriority Critical
@SYMTestCaseDesc This function verifies that the GEO field can be added and its text can contain the
                 GEO values of latitude and longitude. This tests all the import and export functionality of
                 the testcase ids of GEO-1 to GEO-12
@SYMTestActions Create a contact item and export the item to a vCard, the exported item is verified.
                 The vCard is imported and the contact item is verified.

@SYMTestExpectedResults 
The vCard exported should have the following line.
GEO:[lat],[lon]
Different [lat] and [lon] values are tested.
*/


LOCAL_C void TestGeoL(const TDesC& aGeoValue)
	{
	test.Next(_L(""));

	
    test(DoExportTestL(aGeoValue));
    
    test(DoImportTestL(aGeoValue));
	}

/**

@SYMTestCaseID     PIM-T-GEO-0001

*/

LOCAL_C void RunTestsL()
	{
	test.Start(_L("@SYMTESTCaseID:PIM-T-GEO-0001 T_VGEO"));

	CleanupClosePushL(fileSession);
    User::LeaveIfError(fileSession.Connect());
	
	for (TInt var = 0; var < KNumOfGEOTest; ++var)
		{
		TestGeoL(KGeoValues[var]);
		}
	fileSession.Delete(KGEOVCardPath()); 
	CleanupStack::PopAndDestroy(&fileSession); // fileSession
	}

//  Global Functions
GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	CntTest = new CCntTest;
	if ( CntTest)
		{
		//ActiveScheduler is created within CntTest
		TRAPD(err, CntTest->ConstructL(test,KDatabaseFileName));
		test(err==KErrNone);
		// Run tests inside TRAP harness
		TRAPD(mainError, RunTestsL());
		test(mainError==KErrNone);
		CntTest->EndTestLib (mainError);
		}

	__UHEAP_MARKEND;
	return 0;
	}