plugins/contacts/symbian/contactsmodel/tsrc/cntplsql/src/t_cqwertypredictivesearchtable.cpp
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Unit test class for testing 12-key predictive search table
       
    15 *
       
    16 */
       
    17 
       
    18 //  INTERNAL INCLUDES
       
    19 #include "t_cqwertypredictivesearchtable.h"
       
    20 #include "cqwertypredictivesearchtable.h"
       
    21 #include "dbsqlconstants.h"
       
    22 #include "cqwertykeymap.h"
       
    23 #include "t_predictivesearchtabledefs.h"
       
    24 
       
    25 //  SYSTEM INCLUDES
       
    26 #include <digia/eunit/eunitmacros.h>
       
    27 
       
    28 // Used to create HbKeymapFactory singleton to get rid of resource leak
       
    29 #include <QLocale>
       
    30 #include <hbinputkeymapfactory.h>
       
    31     
       
    32 
       
    33 _LIT(KFirstName, "qfirstname r");
       
    34 _LIT(KFirstName2, "otherFirstName");
       
    35 _LIT(KLastName, "elastname");
       
    36 _LIT(KLastName2, "anotherLastName");
       
    37 _LIT(KMail, "tmail1");
       
    38 _LIT(KMail2, "ymail2");
       
    39 _LIT(KMail3, "imail3");
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // UT_CQwertyPredictiveSearchTable::NewL
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 UT_CQwertyPredictiveSearchTable* UT_CQwertyPredictiveSearchTable::NewL()
       
    46     {
       
    47     UT_CQwertyPredictiveSearchTable* self = UT_CQwertyPredictiveSearchTable::NewLC();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // UT_CQwertyPredictiveSearchTable::NewLC
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 UT_CQwertyPredictiveSearchTable* UT_CQwertyPredictiveSearchTable::NewLC()
       
    57     {
       
    58     UT_CQwertyPredictiveSearchTable* self =
       
    59 		new( ELeave ) UT_CQwertyPredictiveSearchTable();
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // UT_CQwertyPredictiveSearchTable::~UT_CQwertyPredictiveSearchTable
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 UT_CQwertyPredictiveSearchTable::~UT_CQwertyPredictiveSearchTable()
       
    70     {
       
    71     delete iTable;
       
    72     iTable = NULL;
       
    73     
       
    74     iDB.Close(); // Must close DB before it can be deleted
       
    75     RSqlDatabase::Delete(KDBFile);
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // UT_CQwertyPredictiveSearchTable::UT_CQwertyPredictiveSearchTable
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 UT_CQwertyPredictiveSearchTable::UT_CQwertyPredictiveSearchTable()
       
    83     {
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // UT_CQwertyPredictiveSearchTable::ConstructL
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void UT_CQwertyPredictiveSearchTable::ConstructL()
       
    91     {
       
    92     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    93     // It generates the test case table.
       
    94     CEUnitTestSuiteClass::ConstructL();
       
    95     
       
    96 
       
    97 #if defined(USE_ORBIT_KEYMAP)
       
    98     // Create singleton outside actual test cases so that it is not treated as
       
    99     // resource leak, since it can't be deleted.
       
   100     HbKeymapFactory::instance();
       
   101 #endif
       
   102     }
       
   103     
       
   104 // -----------------------------------------------------------------------------
       
   105 // UT_CQwertyPredictiveSearchTable::SetupL
       
   106 // Must start with an empty DB file for each test case
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void UT_CQwertyPredictiveSearchTable::SetupL()
       
   110     {
       
   111     // Ignore error
       
   112 	RSqlDatabase::Delete(KDBFile);
       
   113     // If this causes KErrAlreadyExists, iDB won't be fully constructed
       
   114 	iDB.Create(KDBFile);
       
   115 
       
   116 	iTable = CQwertyPredictiveSearchTable::NewL(iDB);
       
   117 	// Create (empty) predictive search tables to DB
       
   118 	iTable->CreateTableL();
       
   119 	}
       
   120     
       
   121 // -----------------------------------------------------------------------------
       
   122 // UT_CQwertyPredictiveSearchTable::Teardown
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void UT_CQwertyPredictiveSearchTable::Teardown()
       
   126     {
       
   127     delete iTable;
       
   128 	iTable = NULL;
       
   129 
       
   130 	iDB.Close(); // Must close DB before it can be deleted
       
   131 	RSqlDatabase::Delete(KDBFile);
       
   132     }
       
   133 
       
   134 
       
   135 // TEST CASES
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // UT_CQwertyPredictiveSearchTable::UT_CreateInDbLL
       
   139 // The first test case shows resource leak if Orbit keymap is used. If harcoded
       
   140 // keymap is used, there is no resource leak.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void UT_CQwertyPredictiveSearchTable::UT_CreateInDbLL()
       
   144     {
       
   145 	// Initially all tables are empty
       
   146 	CheckItemCountL(InitTableVector());
       
   147 
       
   148 	AddContactL(KTestContactId, KFirstName, KLastName, KMail, KMail2, KMail3);
       
   149     
       
   150 	QVector<TInt> result = InitTableVector();
       
   151 	result[0] = 1;
       
   152 	result[2] = 1;
       
   153 	result[3] = 1;
       
   154 	result[4] = 1;
       
   155 	result[5] = 1;
       
   156 	result[7] = 1;
       
   157 	CheckItemCountL(result);
       
   158 
       
   159 	AddContactL(KTestContactId2, KNullDesC, KNullDesC, KNullDesC, KMail);
       
   160 	++result[4];
       
   161 	CheckItemCountL(result);
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // UT_CQwertyPredictiveSearchTable::UT_UpdateLL
       
   166 // Update couple of fields, including mail address and adding a new mail
       
   167 // addresses
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void UT_CQwertyPredictiveSearchTable::UT_UpdateLL()
       
   171 	{
       
   172 	_LIT(KNewMailAddress, "uNew.mail@add.ress");
       
   173 	_LIT(KNewFirstName, "tNewFn");
       
   174     
       
   175     AddContactL(KTestContactId, KFirstName, KLastName, KMail);
       
   176 
       
   177 	QVector<TInt> result = InitTableVector();
       
   178 	result[0] = 1;
       
   179 	result[2] = 1;
       
   180 	result[3] = 1;
       
   181 	result[4] = 1;
       
   182 	CheckItemCountL(result);
       
   183 
       
   184 	CContactItem* contact =
       
   185 		FillContactItemLC(KTestContactId, KNewFirstName, KLastName,
       
   186                           KNewMailAddress, KMail2, KMail);
       
   187     iTable->UpdateL(*contact);
       
   188     CleanupStack::PopAndDestroy(contact);
       
   189 
       
   190     result = InitTableVector();
       
   191 	result[2] = 1;
       
   192 	result[4] = 1;
       
   193 	result[5] = 1;
       
   194 	result[6] = 1;
       
   195 	CheckItemCountL(result);
       
   196 	}
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // UT_CQwertyPredictiveSearchTable::UT_DeleteLL
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void UT_CQwertyPredictiveSearchTable::UT_DeleteLL()
       
   203     {
       
   204     // Try to delete a non-existing item
       
   205     CContactItem* contact =
       
   206         FillContactItemLC(KTestContactId, KFirstName, KLastName, KMail2);
       
   207     TBool lowDiskErrorOccurred(EFalse);
       
   208     iTable->DeleteL(*contact, lowDiskErrorOccurred);
       
   209     
       
   210     // Add two contacts
       
   211     iTable->CreateInDbL(*contact);
       
   212     CContactItem* contact2 =
       
   213         FillContactItemLC(KTestContactId2, KFirstName2, KLastName2, KMail3);
       
   214     iTable->CreateInDbL(*contact2);
       
   215 
       
   216     QVector<TInt> result = InitTableVector();
       
   217     result[0] = 1;
       
   218     result[2] = 1;
       
   219     result[3] = 1;
       
   220     result[5] = 1;
       
   221     result[7] = 1;
       
   222     result[8] = 1;
       
   223     result[10] = 1;
       
   224     CheckItemCountL(result);
       
   225     
       
   226     iTable->DeleteL(*contact2, lowDiskErrorOccurred);
       
   227     CleanupStack::PopAndDestroy(contact2);
       
   228     
       
   229     result = InitTableVector();
       
   230     result[0] = 1;
       
   231     result[2] = 1;
       
   232     result[3] = 1;
       
   233     result[5] = 1;
       
   234     CheckItemCountL(result);
       
   235 
       
   236     iTable->DeleteL(*contact, lowDiskErrorOccurred);
       
   237     CleanupStack::PopAndDestroy(contact);
       
   238     
       
   239     CheckItemCountL(InitTableVector());
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // UT_CQwertyPredictiveSearchTable::InitTableVector
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 QVector<TInt> UT_CQwertyPredictiveSearchTable::InitTableVector() const
       
   247 	{
       
   248 	QVector<TInt> allTablesAreEmpty(CQwertyKeyMap::EAmountOfKeysInQwertyKeypad, 0);
       
   249 	return allTablesAreEmpty;
       
   250 	}
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // UT_CQwertyPredictiveSearchTable::AddContactL
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void UT_CQwertyPredictiveSearchTable::AddContactL(TContactItemId aContactId,
       
   257                                                   const TDesC& aFirstName,
       
   258                                                   const TDesC& aLastName,
       
   259                                                   const TDesC& aMail,
       
   260                                                   const TDesC& aMail2,
       
   261                                                   const TDesC& aMail3)
       
   262     {
       
   263 	CContactItem* contact = FillContactItemLC(aContactId,
       
   264                                               aFirstName,
       
   265                                               aLastName,
       
   266                                               aMail,
       
   267                                               aMail2,
       
   268                                               aMail3);
       
   269 	iTable->CreateInDbL(*contact);
       
   270 	CleanupStack::PopAndDestroy(contact);
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // UT_CQwertyPredictiveSearchTable::FillContactItemLC
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 CContactItem*
       
   278 UT_CQwertyPredictiveSearchTable::FillContactItemLC(TContactItemId aContactId,
       
   279                                                    const TDesC& aFirstName,
       
   280                                                    const TDesC& aLastName,
       
   281                                                    const TDesC& aMail,
       
   282                                                    const TDesC& aMail2,
       
   283                                                    const TDesC& aMail3) const
       
   284     {
       
   285     CContactItem* contact = CContactItem::NewLC(KUidContactCard);
       
   286     contact->SetId(aContactId);
       
   287 
       
   288     if (aFirstName.Length() > 0)
       
   289         {
       
   290         CContactItemField* field =
       
   291             CContactItemField::NewL(KStorageTypeText, KUidContactFieldGivenName);
       
   292         CContactTextField* textfield = field->TextStorage();
       
   293         CleanupStack::PushL(field);
       
   294         textfield->SetTextL(aFirstName);
       
   295         contact->AddFieldL(*field); // Takes ownership
       
   296         CleanupStack::Pop(field);
       
   297         }
       
   298     
       
   299     if (aLastName.Length() > 0)
       
   300         {
       
   301         CContactItemField* field =
       
   302             CContactItemField::NewL(KStorageTypeText, KUidContactFieldFamilyName);
       
   303         CleanupStack::PushL(field);
       
   304         field->TextStorage()->SetTextL(aLastName);
       
   305         contact->AddFieldL(*field); // Takes ownership
       
   306         CleanupStack::Pop(field);
       
   307         }
       
   308 
       
   309     if (aMail.Length() > 0)
       
   310         {
       
   311         CContactItemField* field =
       
   312             CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
       
   313         CleanupStack::PushL(field);
       
   314         field->TextStorage()->SetTextL(aMail);
       
   315         contact->AddFieldL(*field); // Takes ownership
       
   316         CleanupStack::Pop(field);
       
   317         }
       
   318     
       
   319     if (aMail2.Length() > 0)
       
   320         {
       
   321         CContactItemField* field =
       
   322             CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
       
   323         CleanupStack::PushL(field);
       
   324         field->TextStorage()->SetTextL(aMail2);
       
   325         contact->AddFieldL(*field); // Takes ownership
       
   326         CleanupStack::Pop(field);
       
   327         }
       
   328     
       
   329     if (aMail3.Length() > 0)
       
   330         {
       
   331         CContactItemField* field =
       
   332             CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
       
   333         CleanupStack::PushL(field);
       
   334         field->TextStorage()->SetTextL(aMail3);
       
   335         contact->AddFieldL(*field); // Takes ownership
       
   336         CleanupStack::Pop(field);
       
   337         }    
       
   338 	return contact;
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // UT_CQwertyPredictiveSearchTable::CheckItemCountL
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void UT_CQwertyPredictiveSearchTable::CheckItemCountL(
       
   346 	QVector<TInt> aCountInTables)
       
   347     {
       
   348 	const TInt KMaxTableNameLength = 4;
       
   349 
       
   350     HBufC* s = HBufC::NewLC(KCountSelect().Length() + KMaxTableNameLength);
       
   351 	TPtr ptr = s->Des();
       
   352 
       
   353 	QList<QChar> tables;
       
   354 	QT_TRYCATCH_LEAVING(tables = iTable->FillAllTables());
       
   355 	HBufC* tableName(NULL);
       
   356 	TInt tableNumber(0);
       
   357 	while ((tableName = iTable->GetNextTableNameL(tables)) != NULL)
       
   358 		{
       
   359 		ptr.Format(KCountSelect, tableName);
       
   360 		delete tableName;
       
   361 		tableName = NULL;
       
   362         TSqlScalarFullSelectQuery scalarQuery(iDB);
       
   363     
       
   364         TInt rowCount = scalarQuery.SelectIntL(ptr);
       
   365         
       
   366 		EUNIT_ASSERT_EQUALS(aCountInTables.at(tableNumber), rowCount);
       
   367 		++tableNumber;
       
   368         }
       
   369 
       
   370     CleanupStack::PopAndDestroy(s);
       
   371 	}
       
   372     
       
   373 //  TEST TABLE
       
   374 
       
   375 EUNIT_BEGIN_TEST_TABLE(
       
   376     UT_CQwertyPredictiveSearchTable,
       
   377     "UT_CQwertyPredictiveSearchTable",
       
   378     "UNIT" )
       
   379         
       
   380 EUNIT_TEST(
       
   381     "CreateInDbL - test",
       
   382     "UT_CQwertyPredictiveSearchTable",
       
   383     "CreateInDbL",
       
   384     "FUNCTIONALITY",
       
   385     SetupL, UT_CreateInDbLL, Teardown )
       
   386 
       
   387 EUNIT_TEST(
       
   388     "UpdateL - test update mail address",
       
   389     "UT_CQwertyPredictiveSearchTable",
       
   390     "UpdateL",
       
   391     "FUNCTIONALITY",
       
   392     SetupL, UT_UpdateLL, Teardown )
       
   393 
       
   394 EUNIT_TEST(
       
   395     "Delete - test",
       
   396     "UT_CQwertyPredictiveSearchTable",
       
   397     "DeleteL",
       
   398     "FUNCTIONALITY",
       
   399     SetupL, UT_DeleteLL, Teardown )
       
   400 
       
   401 EUNIT_END_TEST_TABLE
       
   402 
       
   403 //  END OF FILE