|
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 |
|
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 } |
|
263 |
|
264 LOCAL_C void Test4L() |
|
265 { |
|
266 ResetDatabaseL(); |
|
267 |
|
268 CreateContactL(KCntName,KCntSurname,_L( "6543 123 4567 00"),KNullDesC); |
|
269 |
|
270 |
|
271 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 7),1); //Both numbers match by 7 digits.. |
|
272 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 8),1); //But they must differ for more digits |
|
273 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"), 9),1); |
|
274 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),10),1); |
|
275 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),11),1); |
|
276 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),12),1); |
|
277 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),13),1); |
|
278 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),14),1); |
|
279 TESTVALUE(CheckPhoneMatchL(_L("6543 123 456700"),15),1); |
|
280 } |
|
281 |
|
282 |
|
283 LOCAL_C void Test5L() |
|
284 { |
|
285 ResetDatabaseL(); |
|
286 |
|
287 CreateContactL(KCntName,KCntSurname,_L("01234 56789"),KNullDesC); |
|
288 |
|
289 //it definetly should match on itself.... |
|
290 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 7),1); //Both numbers match by 7 digits.. |
|
291 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 8),1); //But they must differ for more digits |
|
292 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"), 9),1); |
|
293 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),10),0); |
|
294 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),11),0); |
|
295 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),12),0); |
|
296 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),13),0); |
|
297 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),14),0); |
|
298 TESTVALUE(CheckPhoneMatchL(_L("00 44 1234 56789"),15),0); |
|
299 } |
|
300 |
|
301 /** |
|
302 +@SYMTestType UT |
|
303 +@SYMTestPriority Critical |
|
304 +@SYMDEF PDEF097460 |
|
305 +@SYMTestCaseDesc Tests that attempting to match a phone number consisting of |
|
306 +only "(" does not cause a panic. |
|
307 |
|
308 +@SYMTestActions |
|
309 +1. Add a number consisting only of "(". |
|
310 +2. Check that no panic or error occurs when we attempt to match. |
|
311 |
|
312 +@SYMTestExpectedResults No panic occurs. |
|
313 +*/ |
|
314 |
|
315 |
|
316 void TestPDEF097460() |
|
317 { |
|
318 test.Next(_L("Testing fix for PDEF097460.")); |
|
319 |
|
320 |
|
321 // |
|
322 // 1. Add a number consisting only of "(". |
|
323 // |
|
324 _LIT(KTestNumber3,"("); |
|
325 CreateContactL(KCntName,KCntSurname,KTestNumber3,KNullDesC); |
|
326 // |
|
327 // 2. Check that no panic or error occurs when we attempt to match. |
|
328 // |
|
329 TRAPD( err, CheckPhoneMatchL(KTestNumber3,1) ); |
|
330 test( err == KErrNone ); |
|
331 } |
|
332 // CCsvReader class // |
|
333 |
|
334 class CCsvReader : CBase |
|
335 { |
|
336 public: |
|
337 static CCsvReader* NewLC(const TDesC& filename); |
|
338 ~CCsvReader(); |
|
339 |
|
340 CDesCArray* ReadNextLineL(); |
|
341 |
|
342 private: |
|
343 CCsvReader() {}; |
|
344 |
|
345 RFs iFs; |
|
346 RFile iSrcFile; |
|
347 }; |
|
348 |
|
349 |
|
350 |
|
351 CCsvReader::~CCsvReader() |
|
352 { |
|
353 iSrcFile.Close(); |
|
354 } |
|
355 |
|
356 CCsvReader* CCsvReader::NewLC(const TDesC& filename) |
|
357 { |
|
358 CCsvReader* self = new (ELeave) CCsvReader(); |
|
359 CleanupStack::PushL(self); |
|
360 User::LeaveIfError(self->iFs.Connect()); |
|
361 User::LeaveIfError(self->iSrcFile.Open(self->iFs,filename,EFileRead)); |
|
362 return self; |
|
363 } |
|
364 |
|
365 |
|
366 CDesCArray* CCsvReader::ReadNextLineL() |
|
367 { |
|
368 TBool goOn = ETrue; |
|
369 TBuf8<1> symbol; |
|
370 TFileName buf; |
|
371 CDesCArraySeg* array = new (ELeave) CDesCArraySeg(3); |
|
372 CleanupStack::PushL(array); |
|
373 |
|
374 while (goOn) |
|
375 { |
|
376 User::LeaveIfError(iSrcFile.Read(symbol, 1)); |
|
377 if(symbol.Length() == 0) |
|
378 { |
|
379 if (buf.Length() > 0) //If there is something in the buffer - save in array |
|
380 array->AppendL(buf); |
|
381 |
|
382 if (array->Count() > 0) |
|
383 { |
|
384 CleanupStack::Pop(array); |
|
385 return array; //If array is not empty - return what we've got |
|
386 } |
|
387 else |
|
388 { |
|
389 CleanupStack::PopAndDestroy(array); |
|
390 return NULL; //If array is empty - return zero to indicate EOF |
|
391 } |
|
392 } |
|
393 |
|
394 TChar chr = symbol[0]; |
|
395 switch(chr) |
|
396 { |
|
397 case '\r': goOn = EFalse; |
|
398 case ';': array->AppendL(buf); |
|
399 buf.Zero(); |
|
400 case '\n': //Ignore |
|
401 break; |
|
402 |
|
403 default: |
|
404 buf.Append(chr); |
|
405 } |
|
406 } |
|
407 |
|
408 CleanupStack::Pop(array); |
|
409 return array; |
|
410 } |
|
411 |
|
412 |
|
413 LOCAL_C void FillInDatabaseL(const TDesC& file) |
|
414 { |
|
415 test.Printf(_L("Creating Database:\n")); |
|
416 CntTest->CreateDatabaseL(); |
|
417 |
|
418 CCsvReader* reader = CCsvReader::NewLC(file); |
|
419 FOREVER |
|
420 { |
|
421 CDesCArray* arr = reader->ReadNextLineL(); |
|
422 if (!arr) //EOF reached |
|
423 break; |
|
424 PUSH(arr); |
|
425 const TPtrC surname = (*arr)[0]; |
|
426 const TPtrC name = (*arr)[1]; |
|
427 const TPtrC phone = (*arr)[2]; |
|
428 CreateContactL(name, surname, phone, KNullDesC); |
|
429 |
|
430 test.Printf(surname.Mid(0,1)); |
|
431 POPD(arr); |
|
432 } |
|
433 POPD(reader); |
|
434 |
|
435 CntTest->Db()->CompactL(); |
|
436 } |
|
437 |
|
438 void SetPhoneNumberFieldIndex(const TContactItemId& id) |
|
439 { |
|
440 CContactItem* cnt = CntTest->Db()->ReadContactL(id); |
|
441 PUSH(cnt); |
|
442 TInt fldInd = 0; |
|
443 FOREVER //Find a field with phone number |
|
444 { |
|
445 fldInd = cnt->CardFields().FindNext(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL, fldInd+1); |
|
446 ASSERT(fldInd != KErrNotFound); |
|
447 CContactItemField& phoneFld = cnt->CardFields().operator[](fldInd); //fldInd |
|
448 const TPtrC& phoneNum = phoneFld.TextStorage()->Text(); |
|
449 if (phoneNum.Length()) |
|
450 { |
|
451 PhoneNumberFieldIndex = fldInd; |
|
452 break; |
|
453 } |
|
454 } |
|
455 POPD(cnt); |
|
456 } |
|
457 |
|
458 |
|
459 |
|
460 LOCAL_C CDesCArraySeg* MatchL(const TDesC& phone, TInt matchOn) |
|
461 { |
|
462 CDesCArraySeg* retArray = new (ELeave) CDesCArraySeg(3); |
|
463 PUSH(retArray); |
|
464 |
|
465 CContactIdArray* array = CntTest->Db()->MatchPhoneNumberL(phone,matchOn); |
|
466 CleanupStack::PushL(array); |
|
467 const TInt numberOfMatches = array->Count(); |
|
468 |
|
469 for(TInt j=0; j<numberOfMatches; ++j) //Cycle on results of matching |
|
470 { |
|
471 const TContactItemId& id = (*array)[j]; |
|
472 if (PhoneNumberFieldIndex == -1) //Find an index for the field with the phone (depends on template) |
|
473 SetPhoneNumberFieldIndex(id); |
|
474 |
|
475 CContactItem* cnt = CntTest->Db()->ReadContactL(id); |
|
476 CleanupStack::PushL(cnt); |
|
477 CContactItemField& phoneFld = cnt->CardFields().operator[](PhoneNumberFieldIndex); //todo: 27-magic number |
|
478 const TPtrC& phoneStr = phoneFld.TextStorage()->Text(); |
|
479 |
|
480 retArray->AppendL(phoneStr); |
|
481 |
|
482 CleanupStack::PopAndDestroy(cnt); |
|
483 } |
|
484 CleanupStack::PopAndDestroy(array); |
|
485 |
|
486 POP(retArray); |
|
487 return retArray; |
|
488 } |
|
489 |
|
490 TBool FilesAreIdenticaL(const TDesC& file1, const TDesC& file2) |
|
491 { |
|
492 RFile aFile; |
|
493 RFile bFile; |
|
494 TBuf8<256> aBuf; |
|
495 TBuf8<256> bBuf; |
|
496 TBool retVal = ETrue; |
|
497 |
|
498 TESTNOERRL(aFile.Open(fs,file1,EFileRead)); |
|
499 TESTNOERRL(bFile.Open(fs,file2,EFileRead)); |
|
500 |
|
501 do { |
|
502 TESTNOERRL(aFile.Read(aBuf)); |
|
503 TESTNOERRL(bFile.Read(bBuf)); |
|
504 if (aBuf != bBuf) |
|
505 { |
|
506 retVal = EFalse; |
|
507 break; |
|
508 } |
|
509 } |
|
510 while (aBuf.Length() > 0 || bBuf.Length() >0); |
|
511 |
|
512 aFile.Close(); |
|
513 bFile.Close(); |
|
514 |
|
515 return retVal; |
|
516 } |
|
517 |
|
518 LOCAL_C void CheckMatchingL(const TDesC& aDatabase, const TDesC& aMatchtable, const TDesC& aResultTable) |
|
519 { |
|
520 _LIT(KTempResult, "c:\\result.csv"); |
|
521 TFileName fileName; |
|
522 |
|
523 //Open existing database or create if it was not found |
|
524 //The code reads comma-seprated file and puts contacts to the database |
|
525 if (FileExists(KDatabaseFileName)) |
|
526 { |
|
527 CntTest->OpenDatabaseL(); |
|
528 } |
|
529 else |
|
530 { |
|
531 fileName.Format(KTestFileName, &aDatabase); |
|
532 FillInDatabaseL(fileName); |
|
533 } |
|
534 |
|
535 RFile outFile; |
|
536 TESTNOERRL(outFile.Replace(fs,KTempResult,EFileWrite)); |
|
537 |
|
538 TBuf<64> str; |
|
539 TBuf8<64> str8; |
|
540 |
|
541 //The reader reads comma separated phonenumbers |
|
542 fileName.Format(KTestFileName, &aMatchtable); |
|
543 CCsvReader* reader = CCsvReader::NewLC(fileName); |
|
544 |
|
545 //This cycle scans through the file with all the phonenumber we want to match with |
|
546 FOREVER |
|
547 { |
|
548 CDesCArray* arr = reader->ReadNextLineL(); |
|
549 if (!arr) //EOF reached |
|
550 break; |
|
551 CleanupStack::PushL(arr); |
|
552 |
|
553 const TPtrC& phoneNumber = (*arr)[0]; |
|
554 |
|
555 str.Format(_L("\r\n%S;\r\n"),&phoneNumber); |
|
556 str8.Copy(str); |
|
557 test.Printf(_L("%S"),&str); |
|
558 outFile.Write(str8); |
|
559 |
|
560 for (TInt i = 1; i<=16; ++i) //Cycle on number of digits to match |
|
561 { |
|
562 //Match of cntmodel |
|
563 CDesCArray* matches = MatchL(phoneNumber,i); |
|
564 TESTTRUE(matches != NULL); |
|
565 if (!matches) |
|
566 break; |
|
567 PUSH(matches); |
|
568 |
|
569 //Matches from model |
|
570 str.Format(_L("%d;%d"),i,matches->Count()); |
|
571 str8.Copy(str); |
|
572 //test.Printf(str); |
|
573 outFile.Write(str8); |
|
574 |
|
575 for(TInt j=0; j<matches->Count(); ++j) //Cycle on results of matching |
|
576 { //Each match returns an array on contacts - so we print them all |
|
577 const TPtrC& phoneStr = matches->operator[](j); |
|
578 str.Format(_L(";%S"),&phoneStr); |
|
579 str8.Copy(str); |
|
580 //test.Printf(_L("%S"),&str); |
|
581 outFile.Write(str8); |
|
582 } |
|
583 //test.Printf(_L("\r\n")); |
|
584 outFile.Write(_L8("\r\n")); |
|
585 POPD(matches); |
|
586 } |
|
587 POPD(arr); |
|
588 } |
|
589 outFile.Close(); |
|
590 POPD(reader); |
|
591 CntTest->CloseDatabase(); |
|
592 CntTest->DeleteDatabaseL(); |
|
593 |
|
594 fileName.Format(KTestFileName, &aResultTable); |
|
595 TESTTRUEL(FilesAreIdenticaL(fileName,KTempResult)); |
|
596 fs.Delete(KTempResult); |
|
597 } |
|
598 |
|
599 |
|
600 /** |
|
601 @SYMTestCaseID PIM-T-CNTMATCH-0002 |
|
602 @SYMTestType UT |
|
603 @SYMTestPriority Normal |
|
604 @SYMDEF PDEF104729/INC104452 |
|
605 @SYMTestCaseDesc Tests that attempting to match a phone number that exceeds |
|
606 KCntMaxTextFieldLength does not cause a panic. |
|
607 |
|
608 @SYMTestActions |
|
609 1. Run a match on a number that exceeds KCntMaxTextFieldLength. |
|
610 2. Check that no panic or error occurs when we attempt to match. |
|
611 |
|
612 @SYMTestExpectedResults No panic occurs. |
|
613 */ |
|
614 |
|
615 void TestMatchWithNumberGreaterThanKCntMaxTextFieldLength() |
|
616 { |
|
617 test.Next(_L("@SYMTESTCaseID:PIM-T-CNTMATCH-0002 Testing fix for PDEF104729.")); |
|
618 |
|
619 |
|
620 test.Printf(_L("Running a match with a number greater than KCntMaxTextFieldLength\n")); |
|
621 |
|
622 // 265 characters long. |
|
623 _LIT(KTestNumber,"333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"); |
|
624 |
|
625 CContactDatabase& db = *(CntTest->Db()); |
|
626 CContactIdArray* array = NULL; |
|
627 |
|
628 TRAPD(err , array = db.MatchPhoneNumberL(KTestNumber, 5)); |
|
629 |
|
630 CleanupStack::PushL(array); |
|
631 CleanupStack::PopAndDestroy(array); |
|
632 |
|
633 test( err == KErrNone ); |
|
634 test.Printf(_L("Handled match with a number greater than KCntMaxTextFieldLength OK\n")); |
|
635 } |
|
636 |
|
637 void TestMatchWithContactWithMultipleNumbersL() |
|
638 { |
|
639 /* |
|
640 * Create contact Y with phone number 0501111111 |
|
641 Add another phone number to Y 0441111111 |
|
642 phone match 0441111111 |
|
643 Result: phone should match to the second number |
|
644 * |
|
645 */ |
|
646 _LIT(KTelNo,"0501111111"); |
|
647 _LIT(KMobNo, "0441111111"); |
|
648 |
|
649 ResetDatabaseL(); |
|
650 CreateContactL(KCntName(), KCntSurname(), KTelNo(), KMobNo()); |
|
651 TESTVALUE(CheckPhoneMatchL( KMobNo(), 10),1); |
|
652 TESTVALUE(CheckPhoneMatchL( KTelNo(), 10),1); |
|
653 } |
|
654 |
|
655 void TestMatchWithContactAfterEditL() |
|
656 { |
|
657 /* |
|
658 * Create contact with number 1111111111 |
|
659 * edit the phone number to 2221111111 |
|
660 * try to do a phone match with 1111111111 |
|
661 * This should not match since the number has been edited. |
|
662 */ |
|
663 _LIT(KOrigTelNo,"1111111111"); |
|
664 _LIT(KEditedTelNo,"2221111111"); |
|
665 |
|
666 ResetDatabaseL(); |
|
667 TContactItemId contactId = CreateContactL(KCntName(), KCntSurname(), KOrigTelNo(), KNullDesC()); |
|
668 // Edit the contact |
|
669 CContactItem* contactItem = CntTest->Db()->OpenContactL(contactId); |
|
670 CleanupStack::PushL(contactItem); |
|
671 CContactItemFieldSet& fieldSet = contactItem->CardFields(); |
|
672 TInt pos = fieldSet.Find(KUidContactFieldVCardMapTEL); |
|
673 while (pos!=KErrNotFound) |
|
674 { |
|
675 if (fieldSet[pos].TextStorage()->Text() == KOrigTelNo()) |
|
676 { |
|
677 fieldSet[pos].TextStorage()->SetTextL(KEditedTelNo()); |
|
678 break; |
|
679 } |
|
680 pos = fieldSet.FindNext(KUidContactFieldVCardMapTEL, pos+1); |
|
681 } |
|
682 |
|
683 CntTest->Db()->CommitContactL(*contactItem); |
|
684 |
|
685 test (fieldSet[pos].TextStorage()->Text() == KEditedTelNo()); |
|
686 CleanupStack::PopAndDestroy(contactItem); |
|
687 |
|
688 TESTVALUE(CheckPhoneMatchL( KOrigTelNo(), 10),0); |
|
689 TESTVALUE(CheckPhoneMatchL( KEditedTelNo(), 10),1); |
|
690 } |
|
691 |
|
692 /** |
|
693 |
|
694 @SYMTestCaseID PIM-T-CNTMATCH-0001 |
|
695 |
|
696 */ |
|
697 |
|
698 LOCAL_C void DoTestsL() |
|
699 { |
|
700 test.Start(_L("@SYMTESTCaseID:PIM-T-CNTMATCH-0001 T_CntMatch")); |
|
701 |
|
702 |
|
703 CheckMatchingL(_L("cnt2.csv"), _L("mat2.csv"), _L("res2.csv")); |
|
704 |
|
705 CntTest->CreateDatabaseL(); |
|
706 TestPDEF097460(); |
|
707 PerfectMatchL(); |
|
708 DontUnderestimateTheMeaningOfZeroL(); |
|
709 Test2L(); |
|
710 TestCallingAmericaL(); |
|
711 Test3L(); |
|
712 Test4L(); |
|
713 Test5L(); |
|
714 TestMatchWithNumberGreaterThanKCntMaxTextFieldLength(); |
|
715 TestMatchWithContactWithMultipleNumbersL(); |
|
716 TestMatchWithContactAfterEditL(); |
|
717 |
|
718 CntTest->CloseDatabase(); |
|
719 CntTest->DeleteDatabaseL(); |
|
720 } |
|
721 |
|
722 |
|
723 |
|
724 GLDEF_C TInt E32Main() |
|
725 { |
|
726 CntTest=new(ELeave) CCntTest; |
|
727 CntTest->ConstructL(test,KDatabaseFileName); |
|
728 User::LeaveIfError(fs.Connect()); |
|
729 TRAPD(err,DoTestsL()); |
|
730 test(__NB_failures == 0); |
|
731 CntTest->EndTestLib(err); |
|
732 fs.Close(); |
|
733 |
|
734 return KErrNone; |
|
735 } |