phonebookengines/contactsmodel/tsrc/T_CntMatch.cpp
branchRCL_3
changeset 20 f4a778e096c2
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 // Copyright (c) 2003-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 
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <f32file.h>
       
    20 #include "T_UTILS.H"
       
    21 
       
    22 _LIT(KTestName,"T_CntMatch");
       
    23 #include "testhelpers.h"
       
    24 
       
    25 RFs fs;
       
    26 
       
    27 _LIT(KDatabaseFileName,"c:T_CntMatch");
       
    28 
       
    29 #if defined(__EABI__)
       
    30  _LIT(KTestFileName, "z:\\CntmodelTest\\%S");
       
    31 #else
       
    32  _LIT(KTestFileName, "z:\\system\\programs\\cntmodelTest\\%S");
       
    33 #endif
       
    34 
       
    35 _LIT(KCntName,"Firstname");
       
    36 _LIT(KCntSurname,"Surname");
       
    37 
       
    38 GLDEF_D CCntTest* CntTest=NULL;
       
    39 TInt PhoneNumberFieldIndex = -1;
       
    40 
       
    41 TBool FileExists(const TDesC& aFilename)
       
    42 	{
       
    43 	TUint val;
       
    44 	TBool res = fs.Att(aFilename, val) == KErrNone;
       
    45 	return res;
       
    46 	}
       
    47 
       
    48 
       
    49 // Delete the current contacts database and create a new one
       
    50 //
       
    51 LOCAL_C void ResetDatabaseL()
       
    52 	{
       
    53 	CntTest->CloseDatabase();
       
    54 	CntTest->DeleteDatabaseL();
       
    55 	CntTest->CreateDatabaseL();
       
    56 	CntTest->OpenDatabaseL();
       
    57 	// Reset all server side speed dials ;-)))
       
    58 	CntTest->Db()->ResetServerSpeedDialsL();
       
    59 	}
       
    60 
       
    61 
       
    62 LOCAL_C TContactItemId CreateContactL(const TDesC& aFirstName, const TDesC& aFamilyName, const TDesC& aPhoneNo, const TDesC& aMobileNo)
       
    63 	{
       
    64 	CContactItem* item=CContactCard::NewLC();
       
    65 	SetNameL(*item,KUidContactFieldGivenName,KUidContactFieldVCardMapUnusedN,aFirstName,ETrue);
       
    66 	SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,aFamilyName,ETrue);
       
    67 	SetNameL(*item,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,aPhoneNo,ETrue); //KUidContactFieldVCardMapTELFAX
       
    68 
       
    69 	if (aMobileNo.Length() > 0)
       
    70 		SetNameL(*item,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapCELL,aMobileNo,ETrue);
       
    71 
       
    72 	item->SetTemplateRefId(KGoldenTemplateId);
       
    73 	TContactItemId id=CntTest->Db()->AddNewContactL(*item);
       
    74 	CleanupStack::PopAndDestroy(item);
       
    75 	return id;
       
    76 	}
       
    77 
       
    78 
       
    79 LOCAL_C TInt CheckPhoneMatchL(const TDesC& aPhoneNumberToMatch, TInt aNumberOfMatchDigits) //, TInt aExpectedNumOfMatches)
       
    80 	{
       
    81 	TBuf<256> testCase;
       
    82 	_LIT(KFormat,"Checking matches to %S, matching %d digits");
       
    83 	testCase.Format(KFormat, &aPhoneNumberToMatch, aNumberOfMatchDigits);
       
    84 	//test.Next(_L(" "));
       
    85 
       
    86 	CContactDatabase& db = *(CntTest->Db());
       
    87 	CContactIdArray* array = db.MatchPhoneNumberL(aPhoneNumberToMatch,aNumberOfMatchDigits);
       
    88 	CleanupStack::PushL(array);
       
    89 	const TInt numberOfMatches = array->Count();
       
    90 	CleanupStack::PopAndDestroy(array);
       
    91 	return numberOfMatches;
       
    92 	}
       
    93 
       
    94 
       
    95 //Simple test cases
       
    96 LOCAL_C void PerfectMatchL()
       
    97 	{
       
    98 	//Database contains the only phone number.
       
    99 	//We search for exactly the same number.
       
   100 	//The matching must return one for all the rage of digits.
       
   101 
       
   102 	ResetDatabaseL();
       
   103 
       
   104 	_LIT(KTestNumber2,"4362 5120 7000 0001");
       
   105 	CreateContactL(KCntName,KCntSurname,KTestNumber2,KNullDesC);
       
   106 
       
   107 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,1) == 1);
       
   108 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,2) == 1);
       
   109 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,3) == 1);
       
   110 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,4) == 1);
       
   111 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,5) == 1);
       
   112 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,6) == 1);
       
   113 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,7) == 1);
       
   114 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,8) == 1);
       
   115 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,9) == 1);
       
   116 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,10) == 1);
       
   117 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,11) == 1);
       
   118 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,12) == 1);
       
   119 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,13) == 1);
       
   120 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,14) == 1);
       
   121 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,15) == 1);
       
   122 	TESTTRUE(CheckPhoneMatchL(KTestNumber2,16) == 0);	// AB - only 15 digits..
       
   123 	//Here it works...
       
   124 
       
   125 
       
   126 	//The same story - match on itself must happen for all the rage of digits.
       
   127 	_LIT(KTestNumber,"789543625 120 7582 2381");
       
   128 	CreateContactL(KCntName,KCntSurname,KTestNumber,KNullDesC);
       
   129 
       
   130 	TESTTRUE(CheckPhoneMatchL(KTestNumber,1) == 1);
       
   131 	TESTTRUE(CheckPhoneMatchL(KTestNumber,2) == 0);
       
   132 	TESTTRUE(CheckPhoneMatchL(KTestNumber,3) == 0);
       
   133 	TESTTRUE(CheckPhoneMatchL(KTestNumber,4) == 0);
       
   134 	TESTTRUE(CheckPhoneMatchL(KTestNumber,5) == 0);
       
   135 	TESTTRUE(CheckPhoneMatchL(KTestNumber,6) == 0);
       
   136 	TESTTRUE(CheckPhoneMatchL(KTestNumber,7) == 1);
       
   137 	TESTTRUE(CheckPhoneMatchL(KTestNumber,8) == 1);
       
   138 	TESTTRUE(CheckPhoneMatchL(KTestNumber,9) == 1);
       
   139 	TESTTRUE(CheckPhoneMatchL(KTestNumber,10) == 1);
       
   140 	TESTTRUE(CheckPhoneMatchL(KTestNumber,11) == 1);
       
   141 	TESTTRUE(CheckPhoneMatchL(KTestNumber,12) == 1);
       
   142 	TESTTRUE(CheckPhoneMatchL(KTestNumber,13) == 1);
       
   143 	TESTTRUE(CheckPhoneMatchL(KTestNumber,14) == 1);
       
   144 	TESTTRUE(CheckPhoneMatchL(KTestNumber,15) == 1);
       
   145 	TESTTRUE(CheckPhoneMatchL(KTestNumber,16) == 0);	// AB - not 16 digits, 15 is max.
       
   146 	//Here it doesn't
       
   147 
       
   148 	/*  At the moment, the function simply returns "no matches" if the
       
   149 		number of match digits is incorrect. At no point does it leave with
       
   150 		KErrArgument; if it did, it is possible that client code could be
       
   151 		broken because it expects the API to return zero, not that it should
       
   152 		leave. Whilst leaving with KErrArgument is a laudable aspiration, it
       
   153 		should not be changed here without going through the appropriate
       
   154 		assessment process. Suggest raising a CR.
       
   155 
       
   156 	//We shouldn't be able to match on less than 1 digit...
       
   157 	TRAPD(err,CheckPhoneMatchL(KTestNumber,0));
       
   158 	TESTVALUE(err, KErrArgument);
       
   159 
       
   160 	TRAP(err,CheckPhoneMatchL(KTestNumber,-15));
       
   161 	TESTVALUE(err, KErrArgument);
       
   162 
       
   163 	//We do not provide matching for more than 16 digits.
       
   164 	TRAP(err, CheckPhoneMatchL(KTestNumber,200));
       
   165 	TESTVALUE(err, KErrArgument);
       
   166 
       
   167 	*/
       
   168 	}
       
   169 
       
   170 LOCAL_C void DontUnderestimateTheMeaningOfZeroL()
       
   171 	{
       
   172 	ResetDatabaseL();
       
   173 
       
   174 	//Database contains only this number 207 946 0321;
       
   175 	CreateContactL(KCntName,KCntSurname,_L("207 946 0321"),KNullDesC);
       
   176 	//Trying to search on that exact number by 6 digits...
       
   177 		TESTVALUE(CheckPhoneMatchL(		_L("207 946 0321"), 6),0);
       
   178 	//No matches, well I kind of expected it... :-(
       
   179 
       
   180 	//Now we add a number that differs from the first one by one digit..
       
   181 	CreateContactL(KCntName,KCntSurname,_L("207 046 0321"),KNullDesC);
       
   182 
       
   183 	//Repeat our search
       
   184 		TESTVALUE(CheckPhoneMatchL(		_L("207 946 0321"), 6),1);
       
   185 	//It matches!!... Please don't laugh! There is logic in it!
       
   186 	//If the number doesn't match to itself - then it should match on different one! ;-)
       
   187 	}
       
   188 
       
   189 
       
   190 LOCAL_C void Test2L()
       
   191 	{
       
   192 	ResetDatabaseL();
       
   193 
       
   194 	//if 5324 doesn't match to  228 100 5324...
       
   195 	CreateContactL(KCntName,KCntSurname,_L("207 100 0324"),KNullDesC);
       
   196 	TESTVALUE(CheckPhoneMatchL(					_L("0324"),11),0);
       
   197 	TESTVALUE(CheckPhoneMatchL(					_L("0324"), 4),0);
       
   198 
       
   199 	//it shouldn't match to 228 000 5324 as well, but... it does
       
   200 	CreateContactL(KCntName,KCntSurname,_L("207 000 0324"),KNullDesC);
       
   201 	TESTVALUE(CheckPhoneMatchL(					_L("0324"),11),1);	// AB
       
   202 	TESTVALUE(CheckPhoneMatchL(					_L("0324"), 4),1);
       
   203 	}
       
   204 
       
   205 
       
   206 LOCAL_C void TestCallingAmericaL()
       
   207 	{
       
   208 	ResetDatabaseL();
       
   209 
       
   210 	//here are some additional test numbers for testing the acceptance of the '(+' combination
       
   211 	CreateContactL(KCntName,KCntSurname,_L("(+020) 79460123"),KNullDesC);
       
   212 	CreateContactL(KCntName,KCntSurname,_L("(020) 79460123"),KNullDesC);
       
   213 	CreateContactL(KCntName,KCntSurname,_L("+020 79460123"),KNullDesC);
       
   214 
       
   215 	//when searching for any of the above numbers 3 instances should be found
       
   216 	TESTVALUE(CheckPhoneMatchL(_L("(+020) 79460123"),10),3);
       
   217 	TESTVALUE(CheckPhoneMatchL(_L("(020) 79460123"),10),3);
       
   218 	TESTVALUE(CheckPhoneMatchL(_L("+020 79460123"),10),3);
       
   219 
       
   220 	// Note that the phone number parser does generate a hash of 0 for all
       
   221 	//these now that the '(+'combination is recognised
       
   222 	CreateContactL(KCntName,KCntSurname,_L("(+1) 0755 345 3644 644"),KNullDesC);
       
   223 	CreateContactL(KCntName,KCntSurname,_L("(+1) 0789 37737 39173-1"),KNullDesC);
       
   224 	CreateContactL(KCntName,KCntSurname,_L("(+1) 0755 789 445 557"),KNullDesC);
       
   225 
       
   226 	//these numbers should no longer be found
       
   227 	TESTVALUE(CheckPhoneMatchL(_L("(650)365-0000"),4),0);
       
   228 	TESTVALUE(CheckPhoneMatchL(_L("20 00000 00000"),11),0);
       
   229 
       
   230 	//the american number should now be found with out a problem
       
   231 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"), 7),1);
       
   232 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"), 8),1);
       
   233 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"), 9),1);
       
   234 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),10),1);
       
   235 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),11),1);
       
   236 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),12),1);
       
   237 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),13),1);
       
   238 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),14),1);
       
   239 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),15),1);
       
   240 	TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"),16),1);
       
   241     TESTVALUE(CheckPhoneMatchL(_L("(+1) 0755 345 3644 644"), KBestMatchingPhoneNumbers),1);
       
   242 	}
       
   243 
       
   244 
       
   245 LOCAL_C void Test3L()
       
   246 	{
       
   247 	ResetDatabaseL();
       
   248 
       
   249 	CreateContactL(KCntName,KCntSurname,_L(		   "123 4567"),KNullDesC);
       
   250 	CreateContactL(KCntName,KCntSurname,_L("9876543 123 4567"),KNullDesC);
       
   251 
       
   252 	//it definetly should match on itself....
       
   253 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"), 7),2); //Both numbers match by 7 digits..
       
   254 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"), 8),2); //But they must differ for more digits
       
   255 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"), 9),2); // AB - all these should match both...
       
   256 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),10),2);
       
   257 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),11),2);
       
   258 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),12),2);
       
   259 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),13),2);
       
   260 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),14),2);
       
   261 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"),15),2);
       
   262 	TESTVALUE(CheckPhoneMatchL(_L("123 4567"), KBestMatchingPhoneNumbers),2);
       
   263 	}
       
   264 
       
   265 LOCAL_C void Test4L()
       
   266 	{
       
   267 	ResetDatabaseL();
       
   268 
       
   269 	CreateContactL(KCntName,KCntSurname,_L(	  "6543 123 4567 00"),KNullDesC);
       
   270 
       
   271 
       
   272 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 7),1); //Both numbers match by 7 digits..
       
   273 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 8),1); //But they must differ for more digits
       
   274 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 9),1);
       
   275 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),10),1);
       
   276 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),11),1);
       
   277 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),12),1);
       
   278 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),13),1);
       
   279 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),14),1);
       
   280 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),15),1);
       
   281 	TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), KBestMatchingPhoneNumbers),1);
       
   282 	}
       
   283 
       
   284 
       
   285 LOCAL_C void Test5L()
       
   286 	{
       
   287 	ResetDatabaseL();
       
   288 
       
   289 	CreateContactL(KCntName,KCntSurname,_L("01234 56789"),KNullDesC);
       
   290 
       
   291 	//it definetly should match on itself....
       
   292 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 7),1); //Both numbers match by 7 digits..
       
   293 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 8),1); //But they must differ for more digits
       
   294 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 9),1);
       
   295 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),10),0);
       
   296 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),11),0);
       
   297 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),12),0);
       
   298 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),13),0);
       
   299 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),14),0);
       
   300 	TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),15),0);
       
   301     TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), KBestMatchingPhoneNumbers),1);
       
   302 	}
       
   303 
       
   304 /**
       
   305 +@SYMTestType UT
       
   306 +@SYMTestPriority Critical
       
   307 +@SYMDEF PDEF097460
       
   308 +@SYMTestCaseDesc Tests that attempting to match a phone number consisting of
       
   309 +only "(" does not cause a panic.
       
   310 
       
   311 +@SYMTestActions
       
   312 +1. Add a number consisting only of "(".
       
   313 +2. Check that no panic or error occurs when we attempt to match.
       
   314 
       
   315 +@SYMTestExpectedResults No panic occurs.
       
   316 +*/
       
   317 
       
   318 
       
   319 void TestPDEF097460()
       
   320 	{
       
   321 	test.Next(_L("Testing fix for PDEF097460."));
       
   322 
       
   323 
       
   324 	//
       
   325 	// 1. Add a number consisting only of "(".
       
   326 	//
       
   327 	_LIT(KTestNumber3,"(");
       
   328 	CreateContactL(KCntName,KCntSurname,KTestNumber3,KNullDesC);
       
   329 	//
       
   330 	// 2. Check that no panic or error occurs when we attempt to match.
       
   331 	//
       
   332 	TRAPD( err, CheckPhoneMatchL(KTestNumber3,1) );
       
   333 	test( err == KErrNone );
       
   334 	}
       
   335 // CCsvReader  class //
       
   336 
       
   337 class CCsvReader : CBase
       
   338 	{
       
   339 public:
       
   340 	static CCsvReader* NewLC(const TDesC& filename);
       
   341 	~CCsvReader();
       
   342 
       
   343 	CDesCArray* ReadNextLineL();
       
   344 
       
   345 private:
       
   346 	CCsvReader() {};
       
   347 
       
   348 	RFs iFs;
       
   349 	RFile iSrcFile;
       
   350 	};
       
   351 
       
   352 
       
   353 
       
   354 CCsvReader::~CCsvReader()
       
   355 	{
       
   356 	iSrcFile.Close();
       
   357 	}
       
   358 
       
   359 CCsvReader* CCsvReader::NewLC(const TDesC& filename)
       
   360 	{
       
   361 	CCsvReader* self = new (ELeave) CCsvReader();
       
   362 	CleanupStack::PushL(self);
       
   363 	User::LeaveIfError(self->iFs.Connect());
       
   364 	User::LeaveIfError(self->iSrcFile.Open(self->iFs,filename,EFileRead));
       
   365 	return self;
       
   366 	}
       
   367 
       
   368 
       
   369 CDesCArray* CCsvReader::ReadNextLineL()
       
   370 	{
       
   371 	TBool goOn = ETrue;
       
   372 	TBuf8<1> symbol;
       
   373 	TFileName buf;
       
   374 	CDesCArraySeg* array = new (ELeave) CDesCArraySeg(3);
       
   375 	CleanupStack::PushL(array);
       
   376 
       
   377 	while (goOn)
       
   378 		{
       
   379 		User::LeaveIfError(iSrcFile.Read(symbol, 1));
       
   380 		if(symbol.Length() == 0)
       
   381 			{
       
   382 			if (buf.Length() > 0) //If there is something in the buffer - save in array
       
   383 				array->AppendL(buf);
       
   384 
       
   385 			if (array->Count() > 0)
       
   386 				{
       
   387 				CleanupStack::Pop(array);
       
   388 				return array; //If array is not empty - return what we've got
       
   389 				}
       
   390 			else
       
   391 				{
       
   392 				CleanupStack::PopAndDestroy(array);
       
   393 				return NULL;  //If array is empty - return zero to indicate EOF
       
   394 				}
       
   395 			}
       
   396 
       
   397 		TChar chr = symbol[0];
       
   398 		switch(chr)
       
   399 			{
       
   400 			case '\r': 	goOn = EFalse;
       
   401 			case ';': 	array->AppendL(buf);
       
   402 						buf.Zero();
       
   403 			case '\n':  //Ignore
       
   404 			break;
       
   405 
       
   406 			default:
       
   407 				buf.Append(chr);
       
   408 			}
       
   409 		}
       
   410 
       
   411 	CleanupStack::Pop(array);
       
   412 	return array;
       
   413 	}
       
   414 
       
   415 
       
   416 LOCAL_C void FillInDatabaseL(const TDesC& file)
       
   417 	{
       
   418 	test.Printf(_L("Creating Database:\n"));
       
   419 	CntTest->CreateDatabaseL();
       
   420 
       
   421 	CCsvReader* reader = CCsvReader::NewLC(file);
       
   422 	FOREVER
       
   423 		{
       
   424 		CDesCArray* arr = reader->ReadNextLineL();
       
   425 		if (!arr) //EOF reached
       
   426 			break;
       
   427 		PUSH(arr);
       
   428 		const TPtrC surname	= (*arr)[0];
       
   429 		const TPtrC name	= (*arr)[1];
       
   430 		const TPtrC phone	= (*arr)[2];
       
   431 		CreateContactL(name, surname, phone, KNullDesC);
       
   432 
       
   433 		test.Printf(surname.Mid(0,1));
       
   434 		POPD(arr);
       
   435 		}
       
   436 	POPD(reader);
       
   437 
       
   438 	CntTest->Db()->CompactL();
       
   439 	}
       
   440 
       
   441 void SetPhoneNumberFieldIndex(const TContactItemId& id)
       
   442 	{
       
   443 	CContactItem* cnt = CntTest->Db()->ReadContactL(id);
       
   444 	PUSH(cnt);
       
   445 	TInt fldInd = 0;
       
   446 	FOREVER  //Find a field with phone number
       
   447 		{
       
   448 		fldInd = cnt->CardFields().FindNext(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL, fldInd+1);
       
   449 		ASSERT(fldInd != KErrNotFound);
       
   450 		CContactItemField& phoneFld = cnt->CardFields().operator[](fldInd); //fldInd
       
   451 		const TPtrC& phoneNum = phoneFld.TextStorage()->Text();
       
   452 		if (phoneNum.Length())
       
   453 			{
       
   454 			PhoneNumberFieldIndex = fldInd;
       
   455 			break;
       
   456 			}
       
   457 		}
       
   458 	POPD(cnt);
       
   459 	}
       
   460 
       
   461 
       
   462 
       
   463 LOCAL_C CDesCArraySeg* MatchL(const TDesC& phone, TInt matchOn)
       
   464 	{
       
   465 	CDesCArraySeg* retArray = new (ELeave) CDesCArraySeg(3);
       
   466 	PUSH(retArray);
       
   467 
       
   468 	CContactIdArray* array = CntTest->Db()->MatchPhoneNumberL(phone,matchOn);
       
   469 	CleanupStack::PushL(array);
       
   470 	const TInt numberOfMatches = array->Count();
       
   471 
       
   472 	for(TInt j=0; j<numberOfMatches; ++j)  //Cycle on results of matching
       
   473 		{
       
   474 		const TContactItemId& id = (*array)[j];
       
   475 		if (PhoneNumberFieldIndex == -1) //Find an index for the field with the phone (depends on template)
       
   476 			SetPhoneNumberFieldIndex(id);
       
   477 
       
   478 		CContactItem* cnt = CntTest->Db()->ReadContactL(id);
       
   479 		CleanupStack::PushL(cnt);
       
   480 		CContactItemField& phoneFld = cnt->CardFields().operator[](PhoneNumberFieldIndex); //todo: 27-magic number
       
   481 		const TPtrC& phoneStr = phoneFld.TextStorage()->Text();
       
   482 
       
   483 		retArray->AppendL(phoneStr);
       
   484 
       
   485 		CleanupStack::PopAndDestroy(cnt);
       
   486 		}
       
   487 	CleanupStack::PopAndDestroy(array);
       
   488 
       
   489 	POP(retArray);
       
   490 	return retArray;
       
   491 	}
       
   492 
       
   493 TBool FilesAreIdenticaL(const TDesC& file1, const TDesC& file2)
       
   494 	{
       
   495 	RFile aFile;
       
   496 	RFile bFile;
       
   497 	TBuf8<256> aBuf;
       
   498 	TBuf8<256> bBuf;
       
   499 	TBool retVal = ETrue;
       
   500 
       
   501 	TESTNOERRL(aFile.Open(fs,file1,EFileRead));
       
   502 	TESTNOERRL(bFile.Open(fs,file2,EFileRead));
       
   503 
       
   504 	do  {
       
   505 		TESTNOERRL(aFile.Read(aBuf));
       
   506 		TESTNOERRL(bFile.Read(bBuf));
       
   507 		if (aBuf != bBuf)
       
   508 			{
       
   509 			retVal =  EFalse;
       
   510 			break;
       
   511 			}
       
   512 		}
       
   513 	while (aBuf.Length() > 0 || bBuf.Length() >0);
       
   514 
       
   515 	aFile.Close();
       
   516 	bFile.Close();
       
   517 
       
   518 	return retVal;
       
   519 	}
       
   520 
       
   521 LOCAL_C void CheckMatchingL(const TDesC& aDatabase, const TDesC& aMatchtable, const TDesC& aResultTable)
       
   522 	{
       
   523 	_LIT(KTempResult, "c:\\result.csv");
       
   524 	TFileName fileName;
       
   525 
       
   526 	//Open existing database or create if it was not found
       
   527 	//The code reads comma-seprated file and puts contacts to the database
       
   528 	if (FileExists(KDatabaseFileName))
       
   529 		{
       
   530 		CntTest->OpenDatabaseL();
       
   531 		}
       
   532 	else
       
   533 		{
       
   534 		fileName.Format(KTestFileName, &aDatabase);
       
   535 		FillInDatabaseL(fileName);
       
   536 		}
       
   537 
       
   538 	RFile outFile;
       
   539 	TESTNOERRL(outFile.Replace(fs,KTempResult,EFileWrite));
       
   540 
       
   541 	TBuf<64>  str;
       
   542 	TBuf8<64> str8;
       
   543 
       
   544 	//The reader reads comma separated phonenumbers
       
   545 	fileName.Format(KTestFileName, &aMatchtable);
       
   546  	CCsvReader* reader = CCsvReader::NewLC(fileName);
       
   547 
       
   548 	//This cycle scans through the file with all the phonenumber we want to match with
       
   549 	FOREVER
       
   550 		{
       
   551 		CDesCArray* arr = reader->ReadNextLineL();
       
   552 		if (!arr) //EOF reached
       
   553 			break;
       
   554 		CleanupStack::PushL(arr);
       
   555 
       
   556 		const TPtrC& phoneNumber = (*arr)[0];
       
   557 
       
   558 		str.Format(_L("\r\n%S;\r\n"),&phoneNumber);
       
   559 		str8.Copy(str);
       
   560 		test.Printf(_L("%S"),&str);
       
   561 		outFile.Write(str8);
       
   562 
       
   563 		for (TInt i = 1; i<=16; ++i) //Cycle on number of digits to match
       
   564 			{
       
   565 			//Match of cntmodel
       
   566 			CDesCArray* matches = MatchL(phoneNumber,i);
       
   567 			TESTTRUE(matches != NULL);
       
   568 			if (!matches)
       
   569 				break;
       
   570 			PUSH(matches);
       
   571 
       
   572 			//Matches from model
       
   573 			str.Format(_L("%d;%d"),i,matches->Count());
       
   574 			str8.Copy(str);
       
   575 			//test.Printf(str);
       
   576 			outFile.Write(str8);
       
   577 
       
   578 			for(TInt j=0; j<matches->Count(); ++j)  //Cycle on results of matching
       
   579 				{									//Each match returns an array on contacts - so we print them all
       
   580 				const TPtrC& phoneStr = matches->operator[](j);
       
   581 				str.Format(_L(";%S"),&phoneStr);
       
   582 				str8.Copy(str);
       
   583 				//test.Printf(_L("%S"),&str);
       
   584 				outFile.Write(str8);
       
   585 				}
       
   586 			//test.Printf(_L("\r\n"));
       
   587 			outFile.Write(_L8("\r\n"));
       
   588 			POPD(matches);
       
   589 			}
       
   590 		POPD(arr);
       
   591 		}
       
   592 	outFile.Close();
       
   593 	POPD(reader);
       
   594 	CntTest->CloseDatabase();
       
   595 	CntTest->DeleteDatabaseL();
       
   596 
       
   597 	fileName.Format(KTestFileName, &aResultTable);
       
   598 	TESTTRUEL(FilesAreIdenticaL(fileName,KTempResult));
       
   599 	fs.Delete(KTempResult);
       
   600 	}
       
   601 
       
   602 
       
   603 /**
       
   604 @SYMTestCaseID PIM-T-CNTMATCH-0002
       
   605 @SYMTestType UT
       
   606 @SYMTestPriority Normal
       
   607 @SYMDEF PDEF104729/INC104452
       
   608 @SYMTestCaseDesc Tests that attempting to match a phone number that exceeds
       
   609 KCntMaxTextFieldLength does not cause a panic.
       
   610 
       
   611 @SYMTestActions
       
   612 1. Run a match on a number that exceeds KCntMaxTextFieldLength.
       
   613 2. Check that no panic or error occurs when we attempt to match.
       
   614 
       
   615 @SYMTestExpectedResults No panic occurs.
       
   616 */
       
   617 
       
   618 void TestMatchWithNumberGreaterThanKCntMaxTextFieldLength()
       
   619 	{
       
   620 	test.Next(_L("@SYMTESTCaseID:PIM-T-CNTMATCH-0002 Testing fix for PDEF104729."));
       
   621 
       
   622 
       
   623 	test.Printf(_L("Running a match with a number greater than KCntMaxTextFieldLength\n"));
       
   624 
       
   625 	// 265 characters long.
       
   626 	_LIT(KTestNumber,"333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333");
       
   627 
       
   628 	CContactDatabase& db = *(CntTest->Db());
       
   629 	CContactIdArray* array = NULL;
       
   630 
       
   631 	TRAPD(err , array = db.MatchPhoneNumberL(KTestNumber, 5));
       
   632 
       
   633 	CleanupStack::PushL(array);
       
   634 	CleanupStack::PopAndDestroy(array);
       
   635 
       
   636 	test( err == KErrNone );
       
   637 	test.Printf(_L("Handled match with a number greater than KCntMaxTextFieldLength OK\n"));
       
   638 	}
       
   639 
       
   640 void TestMatchWithContactWithMultipleNumbersL()
       
   641 	{
       
   642 	/*
       
   643 	 * Create contact Y with phone number 0501111111
       
   644 	   Add another phone number to Y 0441111111
       
   645 	   phone match 0441111111
       
   646 	   Result: phone should match to the second number
       
   647 	 *
       
   648 	 */
       
   649 	_LIT(KTelNo,"0501111111");
       
   650 	_LIT(KMobNo, "0441111111");
       
   651 
       
   652 	ResetDatabaseL();
       
   653 	CreateContactL(KCntName(), KCntSurname(), KTelNo(), KMobNo());
       
   654 	TESTVALUE(CheckPhoneMatchL(	KMobNo(), 10),1);
       
   655     TESTVALUE(CheckPhoneMatchL(	KTelNo(), 10),1);
       
   656 		}
       
   657 
       
   658 void TestMatchWithContactAfterEditL()
       
   659 	{
       
   660 	/*
       
   661 	 * Create contact with number 1111111111
       
   662 	 * edit the phone number to 2221111111
       
   663 	 * try to do a phone match with 1111111111
       
   664 	 * This should not match since the number has been edited.
       
   665 	 */
       
   666 	_LIT(KOrigTelNo,"1111111111");
       
   667 	_LIT(KEditedTelNo,"2221111111");
       
   668 
       
   669 	ResetDatabaseL();
       
   670 	TContactItemId contactId = CreateContactL(KCntName(), KCntSurname(), KOrigTelNo(), KNullDesC());
       
   671     //	Edit the contact
       
   672 	CContactItem* contactItem = CntTest->Db()->OpenContactL(contactId);
       
   673 	CleanupStack::PushL(contactItem);
       
   674 	CContactItemFieldSet& fieldSet = contactItem->CardFields();
       
   675 	TInt pos = fieldSet.Find(KUidContactFieldVCardMapTEL);
       
   676 	while (pos!=KErrNotFound)
       
   677 		{
       
   678 		if (fieldSet[pos].TextStorage()->Text() == KOrigTelNo())
       
   679 			{
       
   680 			fieldSet[pos].TextStorage()->SetTextL(KEditedTelNo());
       
   681 			break;
       
   682 			}
       
   683 		pos = fieldSet.FindNext(KUidContactFieldVCardMapTEL, pos+1);
       
   684 		}
       
   685 
       
   686 	CntTest->Db()->CommitContactL(*contactItem);
       
   687 
       
   688 	test (fieldSet[pos].TextStorage()->Text() == KEditedTelNo());
       
   689 	CleanupStack::PopAndDestroy(contactItem);
       
   690 
       
   691 	TESTVALUE(CheckPhoneMatchL(	KOrigTelNo(), 10),0);
       
   692 	TESTVALUE(CheckPhoneMatchL(	KEditedTelNo(), 10),1);
       
   693 	}
       
   694 
       
   695 LOCAL_C void TestBestMatchingStrategyL()
       
   696     {
       
   697     ResetDatabaseL();
       
   698     CreateContactL(KCntName,KCntSurname,_L("34567"),KNullDesC);
       
   699     TESTVALUE(CheckPhoneMatchL(_L("358401234567"),KBestMatchingPhoneNumbers),0);
       
   700     TESTVALUE(CheckPhoneMatchL(_L("34567"), KBestMatchingPhoneNumbers),1);
       
   701     
       
   702     ResetDatabaseL();
       
   703     CreateContactL(KCntName,KCntSurname,_L("358401234567"),KNullDesC);
       
   704     TESTVALUE(CheckPhoneMatchL(_L("358401234567"),KBestMatchingPhoneNumbers),1);
       
   705     TESTVALUE(CheckPhoneMatchL(_L("34567"), KBestMatchingPhoneNumbers),0);
       
   706     
       
   707     ResetDatabaseL();
       
   708     CreateContactL(KCntName,KCntSurname,_L("3560 0123456"),KNullDesC);
       
   709     TESTVALUE(CheckPhoneMatchL(_L("0000 0123456"),KBestMatchingPhoneNumbers),1);
       
   710     // false positive?
       
   711     TESTVALUE(CheckPhoneMatchL(_L("123456"), KBestMatchingPhoneNumbers),1);
       
   712     
       
   713     ResetDatabaseL();
       
   714     CreateContactL(KCntName,KCntSurname,_L("1234567"),KNullDesC);
       
   715     TESTVALUE(CheckPhoneMatchL(_L("358401234567"),KBestMatchingPhoneNumbers),1);
       
   716     TESTVALUE(CheckPhoneMatchL(_L("34567"), KBestMatchingPhoneNumbers),0);
       
   717     
       
   718     ResetDatabaseL();
       
   719     CreateContactL(KCntName,KCntSurname,_L("0000 0123456"),KNullDesC);
       
   720     TESTVALUE(CheckPhoneMatchL(_L("0123456"),KBestMatchingPhoneNumbers),1);
       
   721     TESTVALUE(CheckPhoneMatchL(_L("123456"), KBestMatchingPhoneNumbers),1);
       
   722     TESTVALUE(CheckPhoneMatchL(_L("3456"), KBestMatchingPhoneNumbers),0);
       
   723     
       
   724     ResetDatabaseL();
       
   725     CreateContactL(KCntName,KCntSurname,_L("020 7700 9001"),KNullDesC);
       
   726     CreateContactL(KCntName,KCntSurname,_L("20 7700 90012"),KNullDesC);
       
   727     CreateContactL(KCntName,KCntSurname,_L("020 7700 9081"),KNullDesC);
       
   728     CreateContactL(KCntName,KCntSurname,_L("120 7700 9081"),KNullDesC);
       
   729     CreateContactL(KCntName,KCntSurname,_L("9120 7700 9081"),KNullDesC);
       
   730     TESTVALUE(CheckPhoneMatchL(_L("020 7700 9001"), KBestMatchingPhoneNumbers),1);
       
   731     TESTVALUE(CheckPhoneMatchL(_L("20 7700 90012"), KBestMatchingPhoneNumbers),1);
       
   732     TESTVALUE(CheckPhoneMatchL(_L("020 7700 9081"), KBestMatchingPhoneNumbers),3);
       
   733     TESTVALUE(CheckPhoneMatchL(_L("120 7700 9081"), KBestMatchingPhoneNumbers),3);
       
   734     TESTVALUE(CheckPhoneMatchL(_L("9120 7700 9081"), KBestMatchingPhoneNumbers),3);
       
   735     TESTVALUE(CheckPhoneMatchL(_L("20 7700 9081"), KBestMatchingPhoneNumbers),3);
       
   736 
       
   737     ResetDatabaseL();
       
   738     CreateContactL(KCntName,KCntSurname,_L("9999 9990 0999 999"),KNullDesC);
       
   739     CreateContactL(KCntName,KCntSurname,_L("9000 0000 0000 000"),KNullDesC);
       
   740     CreateContactL(KCntName,KCntSurname,_L("0000 0000 0000 000"),KNullDesC);
       
   741     CreateContactL(KCntName,KCntSurname,_L("0000 0000 0000 009"),KNullDesC);
       
   742     CreateContactL(KCntName,KCntSurname,_L("9 9000 000"),KNullDesC);
       
   743     CreateContactL(KCntName,KCntSurname,_L("9000 000"),KNullDesC);
       
   744     TESTVALUE(CheckPhoneMatchL(_L("9999 9990 0999 999"), KBestMatchingPhoneNumbers),1);
       
   745     TESTVALUE(CheckPhoneMatchL(_L("9000 0000 0000 000"), KBestMatchingPhoneNumbers),2);
       
   746     TESTVALUE(CheckPhoneMatchL(_L("0000 0000 0000 000"), KBestMatchingPhoneNumbers),2);
       
   747     TESTVALUE(CheckPhoneMatchL(_L("0000 0000 0000 009"), KBestMatchingPhoneNumbers),1);
       
   748     TESTVALUE(CheckPhoneMatchL(_L("9 9000 000"), KBestMatchingPhoneNumbers),2);
       
   749     TESTVALUE(CheckPhoneMatchL(_L("9000 000"), KBestMatchingPhoneNumbers),2);
       
   750     TESTVALUE(CheckPhoneMatchL(_L("0000 000"), KBestMatchingPhoneNumbers),2);
       
   751     
       
   752     ResetDatabaseL();
       
   753     CreateContactL(KCntName,KCntSurname,_L("443049607"),KNullDesC);
       
   754     TESTVALUE(CheckPhoneMatchL(_L("358443049607"), KBestMatchingPhoneNumbers),1);
       
   755     
       
   756     ResetDatabaseL();
       
   757     CreateContactL(KCntName,KCntSurname,_L("0443049607"),KNullDesC);
       
   758     TESTVALUE(CheckPhoneMatchL(_L("358443049607"), KBestMatchingPhoneNumbers),1);
       
   759 
       
   760     ResetDatabaseL();
       
   761     CreateContactL(KCntName,KCntSurname,_L("358443049607"),KNullDesC);
       
   762     TESTVALUE(CheckPhoneMatchL(_L("443049607"), KBestMatchingPhoneNumbers),1);
       
   763     
       
   764     ResetDatabaseL();
       
   765     CreateContactL(KCntName,KCntSurname,_L("358443049607"),KNullDesC);
       
   766     TESTVALUE(CheckPhoneMatchL(_L("0443049607"), KBestMatchingPhoneNumbers),1);
       
   767     
       
   768     ResetDatabaseL();
       
   769     CreateContactL(KCntName,KCntSurname,_L("358443049607"),KNullDesC);
       
   770     TESTVALUE(CheckPhoneMatchL(_L("3049607"), KBestMatchingPhoneNumbers),1);
       
   771     
       
   772     ResetDatabaseL();
       
   773     CreateContactL(KCntName,KCntSurname,_L("358443049607"),KNullDesC);
       
   774     TESTVALUE(CheckPhoneMatchL(_L("03049607"), KBestMatchingPhoneNumbers),1);
       
   775     
       
   776     ResetDatabaseL();
       
   777     CreateContactL(KCntName,KCntSurname,_L("443049607"),KNullDesC);
       
   778     TESTVALUE(CheckPhoneMatchL(_L("0443049607"), KBestMatchingPhoneNumbers),1);
       
   779     
       
   780     ResetDatabaseL();
       
   781     CreateContactL(KCntName,KCntSurname,_L("0443049607"),KNullDesC);
       
   782     TESTVALUE(CheckPhoneMatchL(_L("443049607"), KBestMatchingPhoneNumbers),1);
       
   783     
       
   784     ResetDatabaseL();
       
   785     CreateContactL(KCntName,KCntSurname,_L("3049607"),KNullDesC);
       
   786     TESTVALUE(CheckPhoneMatchL(_L("358443049607"), KBestMatchingPhoneNumbers),1);
       
   787     
       
   788     ResetDatabaseL();
       
   789     CreateContactL(KCntName,KCntSurname,_L("03049607"),KNullDesC);
       
   790     TESTVALUE(CheckPhoneMatchL(_L("358443049607"), KBestMatchingPhoneNumbers),1);
       
   791     
       
   792     ResetDatabaseL();
       
   793     CreateContactL(KCntName,KCntSurname,_L("03049607"),KNullDesC);
       
   794     TESTVALUE(CheckPhoneMatchL(_L("0358443049607"), KBestMatchingPhoneNumbers),1);
       
   795     
       
   796     ResetDatabaseL();
       
   797     CreateContactL(KCntName,KCntSurname,_L("4443049607"),KNullDesC);
       
   798     TESTVALUE(CheckPhoneMatchL(_L("3584443049607"), KBestMatchingPhoneNumbers),1);
       
   799     
       
   800     ResetDatabaseL();
       
   801     CreateContactL(KCntName,KCntSurname,_L("584443049607"),KNullDesC);
       
   802     TESTVALUE(CheckPhoneMatchL(_L("4443049607"), KBestMatchingPhoneNumbers),1);
       
   803     
       
   804     ResetDatabaseL();
       
   805     CreateContactL(KCntName,KCntSurname,_L("401234567"),KNullDesC);
       
   806     TESTVALUE(CheckPhoneMatchL(_L("2041234567"), KBestMatchingPhoneNumbers),0);
       
   807     
       
   808     ResetDatabaseL();
       
   809     CreateContactL(KCntName,KCntSurname,_L("0401234567"),KNullDesC);
       
   810     CreateContactL(KCntName,KCntSurname,_L("0501234567"),KNullDesC);
       
   811     TESTVALUE(CheckPhoneMatchL(_L("0401234567"), KBestMatchingPhoneNumbers),1);
       
   812     
       
   813     ResetDatabaseL();
       
   814     CreateContactL(KCntName,KCntSurname,_L("020421234567"),KNullDesC);
       
   815     CreateContactL(KCntName,KCntSurname,_L("005021234567"),KNullDesC);
       
   816     TESTVALUE(CheckPhoneMatchL(_L("020421234567"), KBestMatchingPhoneNumbers),1);
       
   817     
       
   818     }
       
   819 
       
   820 /**
       
   821 
       
   822 @SYMTestCaseID     PIM-T-CNTMATCH-0001
       
   823 
       
   824 */
       
   825 
       
   826 LOCAL_C void DoTestsL()
       
   827 	{
       
   828 	test.Start(_L("@SYMTESTCaseID:PIM-T-CNTMATCH-0001 T_CntMatch"));
       
   829 
       
   830 
       
   831 	CheckMatchingL(_L("cnt2.csv"), _L("mat2.csv"), _L("res2.csv"));
       
   832 
       
   833 	CntTest->CreateDatabaseL();
       
   834 	TestPDEF097460();
       
   835 	PerfectMatchL();
       
   836 	DontUnderestimateTheMeaningOfZeroL();
       
   837 	Test2L();
       
   838 	TestCallingAmericaL();
       
   839 	Test3L();
       
   840 	Test4L();
       
   841 	Test5L();
       
   842 	TestMatchWithNumberGreaterThanKCntMaxTextFieldLength();
       
   843 	TestMatchWithContactWithMultipleNumbersL();
       
   844 	TestMatchWithContactAfterEditL();
       
   845 	TestBestMatchingStrategyL();
       
   846 	CntTest->CloseDatabase();
       
   847 	CntTest->DeleteDatabaseL();
       
   848     }
       
   849 
       
   850 
       
   851 
       
   852 GLDEF_C TInt E32Main()
       
   853 	{
       
   854     CntTest=new(ELeave) CCntTest;
       
   855 	CntTest->ConstructL(test,KDatabaseFileName);
       
   856 	User::LeaveIfError(fs.Connect());
       
   857     TRAPD(err,DoTestsL());
       
   858 	test(__NB_failures == 0);
       
   859 	CntTest->EndTestLib(err);
       
   860 	fs.Close();
       
   861 
       
   862 	return KErrNone;
       
   863     }