summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/cntplsql/src/t_cqwertypredictivesearchtable.cpp
blob: 1f75bdff9822c3c3d366bc6f10fec430f2bd20ef (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright (c) 2010 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:  Unit test class for testing 12-key predictive search table
*
*/

//  INTERNAL INCLUDES
#include "t_cqwertypredictivesearchtable.h"
#include "cqwertypredictivesearchtable.h"
#include "dbsqlconstants.h"
#include "cqwertykeymap.h"
#include "t_predictivesearchtabledefs.h"

//  SYSTEM INCLUDES
#include <digia/eunit/eunitmacros.h>

// Used to create HbKeymapFactory singleton to get rid of resource leak
#include <QLocale>
#include <hbinputkeymapfactory.h>
    

_LIT(KFirstName, "qfirstname r");
_LIT(KFirstName2, "otherFirstName");
_LIT(KLastName, "elastname");
_LIT(KLastName2, "anotherLastName");
_LIT(KMail, "tmail1");
_LIT(KMail2, "ymail2");
_LIT(KMail3, "imail3");

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::NewL
// -----------------------------------------------------------------------------
//
UT_CQwertyPredictiveSearchTable* UT_CQwertyPredictiveSearchTable::NewL()
    {
    UT_CQwertyPredictiveSearchTable* self = UT_CQwertyPredictiveSearchTable::NewLC();
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::NewLC
// -----------------------------------------------------------------------------
//
UT_CQwertyPredictiveSearchTable* UT_CQwertyPredictiveSearchTable::NewLC()
    {
    UT_CQwertyPredictiveSearchTable* self =
		new( ELeave ) UT_CQwertyPredictiveSearchTable();
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::~UT_CQwertyPredictiveSearchTable
// -----------------------------------------------------------------------------
//
UT_CQwertyPredictiveSearchTable::~UT_CQwertyPredictiveSearchTable()
    {
    delete iTable;
    iTable = NULL;
    
    iDB.Close(); // Must close DB before it can be deleted
    RSqlDatabase::Delete(KDBFile);
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_CQwertyPredictiveSearchTable
// -----------------------------------------------------------------------------
//
UT_CQwertyPredictiveSearchTable::UT_CQwertyPredictiveSearchTable()
    {
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::ConstructL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::ConstructL()
    {
    // The ConstructL from the base class CEUnitTestSuiteClass must be called.
    // It generates the test case table.
    CEUnitTestSuiteClass::ConstructL();

    // Create singleton outside actual test cases so that it is not treated as
    // resource leak, since it can't be deleted.
    HbKeymapFactory::instance();
    }
    
// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::SetupL
// Must start with an empty DB file for each test case
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::SetupL()
    {
    // Ignore error
	RSqlDatabase::Delete(KDBFile);
    // If this causes KErrAlreadyExists, iDB won't be fully constructed
	iDB.Create(KDBFile);

	iTable = CQwertyPredictiveSearchTable::NewL(iDB);
	// Create (empty) predictive search tables to DB
	iTable->CreateTableL();
	}
    
// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::Teardown
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::Teardown()
    {
    delete iTable;
	iTable = NULL;

	iDB.Close(); // Must close DB before it can be deleted
	RSqlDatabase::Delete(KDBFile);
    }


// TEST CASES

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_CreateInDbLL
// The first test case shows resource leak if Orbit keymap is used. If harcoded
// keymap is used, there is no resource leak.
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_CreateInDbLL()
    {
	// Initially all tables are empty
	CheckItemCountL(InitTableVector());

	AddContactL(KTestContactId, KFirstName, KLastName, KMail, KMail2, KMail3);
    
	QVector<TInt> result = InitTableVector();
	result[0] = 1;
	result[2] = 1;
	result[3] = 1;
	result[4] = 1;
	result[5] = 1;
	result[7] = 1;
	CheckItemCountL(result);

	AddContactL(KTestContactId2, KNullDesC, KNullDesC, KNullDesC, KMail);
	++result[4];
	CheckItemCountL(result);
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_UpdateLL
// Update couple of fields, including mail address and adding a new mail
// addresses
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_UpdateLL()
	{
	_LIT(KNewMailAddress, "uNew.mail@add.ress");
	_LIT(KNewFirstName, "tNewFn");
    
    AddContactL(KTestContactId, KFirstName, KLastName, KMail);

	QVector<TInt> result = InitTableVector();
	result[0] = 1;
	result[2] = 1;
	result[3] = 1;
	result[4] = 1;
	CheckItemCountL(result);

	CContactItem* contact =
		FillContactItemLC(KTestContactId, KNewFirstName, KLastName,
                          KNewMailAddress, KMail2, KMail);
    iTable->UpdateL(*contact);
    CleanupStack::PopAndDestroy(contact);

    result = InitTableVector();
	result[2] = 1;
	result[4] = 1;
	result[5] = 1;
	result[6] = 1;
	CheckItemCountL(result);
	}

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_DeleteLL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_DeleteLL()
    {
    // Try to delete a non-existing item
    CContactItem* contact =
        FillContactItemLC(KTestContactId, KFirstName, KLastName, KMail2);
    TBool lowDiskErrorOccurred(EFalse);
    iTable->DeleteL(*contact, lowDiskErrorOccurred);
    
    // Add two contacts
    iTable->CreateInDbL(*contact);
    CContactItem* contact2 =
        FillContactItemLC(KTestContactId2, KFirstName2, KLastName2, KMail3);
    iTable->CreateInDbL(*contact2);

    QVector<TInt> result = InitTableVector();
    result[0] = 1;
    result[2] = 1;
    result[3] = 1;
    result[5] = 1;
    result[7] = 1;
    result[8] = 1;
    result[10] = 1;
    CheckItemCountL(result);
    
    iTable->DeleteL(*contact2, lowDiskErrorOccurred);
    CleanupStack::PopAndDestroy(contact2);
    
    result = InitTableVector();
    result[0] = 1;
    result[2] = 1;
    result[3] = 1;
    result[5] = 1;
    CheckItemCountL(result);

    iTable->DeleteL(*contact, lowDiskErrorOccurred);
    CleanupStack::PopAndDestroy(contact);
    
    CheckItemCountL(InitTableVector());
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_UnmappedMailAddressL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_UnmappedMailAddressL()
    {
    // All names and mail addresses begin with unmapped characters, so contact
    // is not stored to QWERTY tables at all. Since it can't be searched with
	// predictive search.
    _LIT(KUnmappedName, "¨unknownCharAtStart");
    _LIT(KUnmappedName2, "¨nameAfterUnk");
    _LIT(KUnmappedMail, "mailto:¨user@domain");
    _LIT(KUnmappedMail2, "¨addr@call.id");
    _LIT(KUnmappedMail3, "¨begins@by.unk.char");
    AddContactL(KTestContactId, KUnmappedName, KUnmappedName2,
			    KUnmappedMail, KUnmappedMail2, KUnmappedMail3); 
    // Check tables are empty
    CheckItemCountL(InitTableVector());
    
    
    // One mail address begins with a mapped character
    AddContactL(KTestContactId2, KNullDesC, KUnmappedName2, 
                KUnmappedMail, KUnmappedMail2, KMail);
    
    QVector<TInt> result = InitTableVector();
    result[4] = 1; // KMail
    CheckItemCountL(result);
    
   
    // All mail addresses begin with unmapped characters, but contact can be
    // searched by first name. Contact is stored to QWERTY tables.
    _LIT(KNameWithUnmappedChars, "E¨¨");
    AddContactL(KTestContactId3, KNameWithUnmappedChars, KNullDesC, 
                KUnmappedMail, KUnmappedMail2);
    
    result[2] = 1; // KNameWithUnmappedChars
    CheckItemCountL(result);
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_MailAddressWithUnknownCharactersL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_MailAddressWithUnknownCharactersL()
    {
	// Contact has only mail address, and it contains (but does not begin by)
	// unknown characters. It should be stored to tables.
	_LIT(KMailWithUnmappedChars, "(¨&)");
	AddContactL(KTestContactId3, KNullDesC, KNullDesC,
                KMailWithUnmappedChars);
    
	QVector<TInt> result = InitTableVector();
    result[32] = 1; // KMailWithUnmappedChars
    CheckItemCountL(result);
	}

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::UT_BeginByNumberL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::UT_BeginByNumberL()
    {
    // Several names and mail addresses begin by number. They are all stored.
    _LIT(KNameBeginsByNbr, "8nbrAtStart");
    _LIT(KMailBeginsByNbr, "mailto:0123user@domain");
    _LIT(KMail2BeginsByNbr, "800@call.id");
    _LIT(KMail3BeginsByNbr, "5begins@by.nbr");
    AddContactL(KTestContactId, KNameBeginsByNbr, KFirstName2, KMailBeginsByNbr,
    			KMail2BeginsByNbr, KMail3BeginsByNbr);
    
    QVector<TInt> result = InitTableVector();
    result[7] = 1;  // KFirstName2
    result[36] = 1; // begins by 5
    result[39] = 2; // begins by 8
    result[41] = 2; // begins by 0
    CheckItemCountL(result);
    }


// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::InitTableVector
// -----------------------------------------------------------------------------
//
QVector<TInt> UT_CQwertyPredictiveSearchTable::InitTableVector() const
	{
	QVector<TInt> allTablesAreEmpty(CQwertyKeyMap::EMaxAmountOfKeysInQwertyKeypad, 0);
	return allTablesAreEmpty;
	}

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::AddContactL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::AddContactL(TContactItemId aContactId,
                                                  const TDesC& aFirstName,
                                                  const TDesC& aLastName,
                                                  const TDesC& aMail,
                                                  const TDesC& aMail2,
                                                  const TDesC& aMail3)
    {
	CContactItem* contact = FillContactItemLC(aContactId,
                                              aFirstName,
                                              aLastName,
                                              aMail,
                                              aMail2,
                                              aMail3);
	iTable->CreateInDbL(*contact);
	CleanupStack::PopAndDestroy(contact);
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::FillContactItemLC
// -----------------------------------------------------------------------------
//
CContactItem*
UT_CQwertyPredictiveSearchTable::FillContactItemLC(TContactItemId aContactId,
                                                   const TDesC& aFirstName,
                                                   const TDesC& aLastName,
                                                   const TDesC& aMail,
                                                   const TDesC& aMail2,
                                                   const TDesC& aMail3) const
    {
    CContactItem* contact = CContactItem::NewLC(KUidContactCard);
    contact->SetId(aContactId);

    if (aFirstName.Length() > 0)
        {
        CContactItemField* field =
            CContactItemField::NewL(KStorageTypeText, KUidContactFieldGivenName);
        CContactTextField* textfield = field->TextStorage();
        CleanupStack::PushL(field);
        textfield->SetTextL(aFirstName);
        contact->AddFieldL(*field); // Takes ownership
        CleanupStack::Pop(field);
        }
    
    if (aLastName.Length() > 0)
        {
        CContactItemField* field =
            CContactItemField::NewL(KStorageTypeText, KUidContactFieldFamilyName);
        CleanupStack::PushL(field);
        field->TextStorage()->SetTextL(aLastName);
        contact->AddFieldL(*field); // Takes ownership
        CleanupStack::Pop(field);
        }

    if (aMail.Length() > 0)
        {
        CContactItemField* field =
            CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
        CleanupStack::PushL(field);
        field->TextStorage()->SetTextL(aMail);
        contact->AddFieldL(*field); // Takes ownership
        CleanupStack::Pop(field);
        }
    
    if (aMail2.Length() > 0)
        {
        CContactItemField* field =
            CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
        CleanupStack::PushL(field);
        field->TextStorage()->SetTextL(aMail2);
        contact->AddFieldL(*field); // Takes ownership
        CleanupStack::Pop(field);
        }
    
    if (aMail3.Length() > 0)
        {
        CContactItemField* field =
            CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
        CleanupStack::PushL(field);
        field->TextStorage()->SetTextL(aMail3);
        contact->AddFieldL(*field); // Takes ownership
        CleanupStack::Pop(field);
        }    
	return contact;
    }

// -----------------------------------------------------------------------------
// UT_CQwertyPredictiveSearchTable::CheckItemCountL
// -----------------------------------------------------------------------------
//
void UT_CQwertyPredictiveSearchTable::CheckItemCountL(
	QVector<TInt> aCountInTables)
    {
	const TInt KMaxTableNameLength = 4;

    HBufC* s = HBufC::NewLC(KCountSelect().Length() + KMaxTableNameLength);
	TPtr ptr = s->Des();

	QList<QChar> tables;
	QT_TRYCATCH_LEAVING(tables = iTable->FillAllTables());
	HBufC* tableName(NULL);
	TInt tableNumber(0);
	while ((tableName = iTable->GetNextTableNameL(tables)) != NULL)
		{
		ptr.Format(KCountSelect, tableName);
		delete tableName;
		tableName = NULL;
        TSqlScalarFullSelectQuery scalarQuery(iDB);
    
        TInt rowCount = scalarQuery.SelectIntL(ptr);
        TInt expectedCount = aCountInTables.at(tableNumber);

        if (expectedCount != rowCount)
            {
            RDebug::Print(_L("wrong item count in table nbr %d, expected=%d, actual=%d"),
                          tableNumber, expectedCount, rowCount);
            EUNIT_ASSERT_EQUALS(expectedCount, rowCount);
            }
		++tableNumber;
        }

    CleanupStack::PopAndDestroy(s);
	}
    
//  TEST TABLE

EUNIT_BEGIN_TEST_TABLE(
    UT_CQwertyPredictiveSearchTable,
    "UT_CQwertyPredictiveSearchTable",
    "UNIT" )
        
EUNIT_TEST(
    "CreateInDbL - test",
    "UT_CQwertyPredictiveSearchTable",
    "CreateInDbL",
    "FUNCTIONALITY",
    SetupL, UT_CreateInDbLL, Teardown )

EUNIT_TEST(
    "UpdateL - test update mail address",
    "UT_CQwertyPredictiveSearchTable",
    "UpdateL",
    "FUNCTIONALITY",
    SetupL, UT_UpdateLL, Teardown )

EUNIT_TEST(
    "Delete - test",
    "UT_CQwertyPredictiveSearchTable",
    "DeleteL",
    "FUNCTIONALITY",
    SetupL, UT_DeleteLL, Teardown )

EUNIT_TEST(
    "CreateInDbL - unmapped mail address",
    "UT_CQwertyPredictiveSearchTable",
    "CreateInDbL",
    "FUNCTIONALITY",
    SetupL, UT_UnmappedMailAddressL, Teardown )

EUNIT_TEST(
    "CreateInDbL - mail address contains unmapped chars",
    "UT_CQwertyPredictiveSearchTable",
    "CreateInDbL",
    "FUNCTIONALITY",
    SetupL, UT_MailAddressWithUnknownCharactersL, Teardown )
	
EUNIT_END_TEST_TABLE

//  END OF FILE