phonebookengines_old/contactsmodel/tsrc/t_cntidtest.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2007-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 // System includes
       
    17 #include <e32cons.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntfield.h>
       
    20 #include <cntitem.h>
       
    21 #include <cntfldst.h>
       
    22 
       
    23 #include "t_cntidtest.h"
       
    24 
       
    25 // Local definitions
       
    26 _LIT(KCntSysTemplateInfo,"Sys template has %d fields\n");
       
    27 _LIT(KCntCreatingContacts,"Creating %d contacts\n");
       
    28 _LIT(KCntCreatingContact,"Creating contact %d\n");
       
    29 _LIT(KCntPrintContactIds,"Printing contact ids\n");
       
    30 _LIT(KCntPrintContactId, "Contact ID: %d\n" );
       
    31 _LIT(KCntPrintContactField, "Field number: %d ID: %d\n" );
       
    32 _LIT(KDbFileName,"c:t_cntIdTest.cdb");
       
    33 _LIT(KREADIDFAILED,"failed to verify the field ID\n");
       
    34 
       
    35 GLREF_C RTest test;
       
    36 
       
    37 const TInt KCntTestContactCount = 1;
       
    38 const TInt KCntTestBufSize = 64;
       
    39 
       
    40 //--------------------------------------------------------------------------
       
    41 // CCntTest::CCntTest
       
    42 //--------------------------------------------------------------------------
       
    43 //
       
    44 CCntTest::CCntTest()
       
    45 :   CActive(EPriorityStandard)
       
    46     {
       
    47     CActiveScheduler::Add(this);
       
    48     }
       
    49     
       
    50 //--------------------------------------------------------------------------
       
    51 // CCntTest::~CCntTest
       
    52 //--------------------------------------------------------------------------
       
    53 //
       
    54 CCntTest::~CCntTest()
       
    55     {
       
    56     Cancel();
       
    57     iContactIds.Close();
       
    58     delete iContactDb;
       
    59     }
       
    60 
       
    61 //--------------------------------------------------------------------------
       
    62 // CCntTest::NewL
       
    63 //--------------------------------------------------------------------------
       
    64 //
       
    65 CCntTest* CCntTest::NewL()
       
    66     {
       
    67     CCntTest* self = new (ELeave) CCntTest();
       
    68     CleanupStack::PushL(self);
       
    69     self->ConstructL();
       
    70     CleanupStack::Pop(self);
       
    71     return self;
       
    72     }
       
    73 
       
    74 //--------------------------------------------------------------------------
       
    75 // CCntTest::ConstructL
       
    76 //--------------------------------------------------------------------------
       
    77 //
       
    78 void CCntTest::ConstructL()
       
    79     {
       
    80     iContactDb = CContactDatabase::ReplaceL(KDbFileName);
       
    81     }
       
    82 
       
    83 //--------------------------------------------------------------------------
       
    84 // CCntTest::RunTest
       
    85 //--------------------------------------------------------------------------
       
    86 //
       
    87 void CCntTest::RunTest()
       
    88     {
       
    89     iPhase = ECntTestTemplate;
       
    90     IssueRequest();
       
    91     }
       
    92 
       
    93 //--------------------------------------------------------------------------
       
    94 // CCntTest::RunL
       
    95 //--------------------------------------------------------------------------
       
    96 //
       
    97 void CCntTest::RunL()
       
    98     {
       
    99     switch( iPhase )
       
   100         {        
       
   101         case ECntTestTemplate:
       
   102             PrintTemplateInfoL();
       
   103             break;
       
   104             
       
   105         case ECntTestCreating:
       
   106             CreateContactsToDbL(KCntTestContactCount);
       
   107             break;
       
   108             
       
   109         case ECntTestPrint:
       
   110             ReadContactFieldIdsL();
       
   111             break;
       
   112             
       
   113         case ECntTestDone:
       
   114             CompleteL();
       
   115             break;
       
   116             
       
   117         default:
       
   118             break;
       
   119         }
       
   120     }
       
   121      
       
   122 //--------------------------------------------------------------------------
       
   123 // CCntTest::DoCancel
       
   124 //--------------------------------------------------------------------------
       
   125 //
       
   126 void CCntTest::DoCancel()
       
   127     {       
       
   128     }         
       
   129        
       
   130 //--------------------------------------------------------------------------
       
   131 // CCntTest::CreateTemplateL
       
   132 //--------------------------------------------------------------------------
       
   133 //
       
   134 void CCntTest::PrintTemplateInfoL()
       
   135     {
       
   136     CContactItem* sysTemplate = 
       
   137         iContactDb->OpenContactL(iContactDb->TemplateId());
       
   138     CleanupStack::PushL(sysTemplate);
       
   139         
       
   140     CContactItemFieldSet& fieldSet = sysTemplate->CardFields();
       
   141 
       
   142     test.Printf( KCntSysTemplateInfo, fieldSet.Count() );
       
   143 
       
   144     CleanupStack::PopAndDestroy(); // sysTemplate
       
   145     
       
   146     IssueRequest();
       
   147     iPhase = ECntTestCreating;
       
   148     }
       
   149 
       
   150 //--------------------------------------------------------------------------
       
   151 // CCntTest::CreateContactsToDbL
       
   152 //--------------------------------------------------------------------------
       
   153 //
       
   154 void CCntTest::CreateContactsToDbL(TInt aCount)
       
   155     {
       
   156     test.Printf(KCntCreatingContacts, aCount);
       
   157     
       
   158     for (TInt i(0); i < aCount; ++i )
       
   159         {
       
   160         test.Printf(KCntCreatingContact, i );
       
   161         User::LeaveIfError( iContactIds.Append(DoCreateContactL(i)) );
       
   162         }    
       
   163     IssueRequest();
       
   164     iPhase = ECntTestPrint;
       
   165     }
       
   166 
       
   167 //--------------------------------------------------------------------------
       
   168 // CCntTest::DoCreateContactL
       
   169 //--------------------------------------------------------------------------
       
   170 //
       
   171 TContactItemId CCntTest::DoCreateContactL( TInt aIdentifier )
       
   172     {
       
   173     TBuf<KCntTestBufSize> name(_L("Contact "));
       
   174     TBuf<KCntTestBufSize> number1(_L("1231231320") );
       
   175     TBuf<KCntTestBufSize> number2(_L("1231231321") );
       
   176     
       
   177     name.AppendNum(aIdentifier);
       
   178     number1.AppendNum(aIdentifier);
       
   179     number2.AppendNum(aIdentifier);
       
   180     
       
   181     CContactCard* card = CContactCard::NewLC();
       
   182 
       
   183     card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapUnusedN,
       
   184                                    KUidContactFieldGivenName, 
       
   185                                    _L("ContactModel")));
       
   186     CleanupStack::Pop();
       
   187     card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapUnusedN,
       
   188                                    KUidContactFieldFamilyName, 
       
   189                                    name ));
       
   190     CleanupStack::Pop();
       
   191     card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapTEL,
       
   192                                    KUidContactFieldPhoneNumber, 
       
   193                                    number1 ));
       
   194     CleanupStack::Pop();
       
   195     card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapTELFAX,
       
   196                                    KUidContactFieldPhoneNumber, 
       
   197                                    number2 ));
       
   198     CleanupStack::Pop();
       
   199     
       
   200     TContactItemId id = iContactDb->AddNewContactL(*card);
       
   201     CleanupStack::PopAndDestroy(card);    
       
   202     return id;    
       
   203     }
       
   204 
       
   205 //--------------------------------------------------------------------------
       
   206 // CCmKillerCntModel::CreateFieldLC
       
   207 //--------------------------------------------------------------------------
       
   208 //
       
   209 CContactItemField* CCntTest::CreateFieldLC(
       
   210     const TUid aMappingUId,
       
   211     TFieldType aFieldType, 
       
   212     const TDesC &aText)
       
   213     {
       
   214     CContactItemField* field = 
       
   215         CContactItemField::NewLC(KStorageTypeText, aFieldType);        
       
   216     field->SetMapping(aMappingUId);
       
   217     // field->SetTemplateField(ETrue);
       
   218     field->TextStorage()->SetTextL(aText);
       
   219     return field;    
       
   220     }
       
   221 
       
   222 //--------------------------------------------------------------------------
       
   223 // CCntTest::PrintContactFieldIdsL
       
   224 //--------------------------------------------------------------------------
       
   225 //
       
   226 void CCntTest::ReadContactFieldIdsL()
       
   227     {
       
   228     test.Printf(KCntPrintContactIds);
       
   229     
       
   230     TInt contactCount(iContactIds.Count());    
       
   231     for (TInt i(0); i < contactCount; ++i )
       
   232         {
       
   233         test.Printf(KCntPrintContactId, iContactIds[i] );
       
   234         
       
   235         CContactItem* item = iContactDb->ReadContactL(iContactIds[i]);
       
   236         CleanupStack::PushL(item);
       
   237         
       
   238         CContactItemFieldSet& fieldSet = item->CardFields();        
       
   239         TInt fieldCount( fieldSet.Count() );        
       
   240         for (TInt j(0); j < fieldCount; ++j )
       
   241             {
       
   242             TInt fieldId( fieldSet[j].Id() );
       
   243             test.Printf(KCntPrintContactField, j, fieldId );
       
   244             if(fieldId != j)
       
   245             	{
       
   246             	test.Printf(KREADIDFAILED);
       
   247                 test(EFalse); // fail the test
       
   248             	}
       
   249             }
       
   250         iContactDb->CloseContactL( item->Id() );
       
   251         CleanupStack::PopAndDestroy(); // item
       
   252         }
       
   253     
       
   254     IssueRequest();
       
   255     iPhase = ECntTestDone;
       
   256     }
       
   257 
       
   258 //--------------------------------------------------------------------------
       
   259 // CCntTest::CompleteL
       
   260 //--------------------------------------------------------------------------
       
   261 //
       
   262 void CCntTest::CompleteL()
       
   263     {
       
   264     CActiveScheduler::Stop();
       
   265     }
       
   266     
       
   267 //--------------------------------------------------------------------------
       
   268 // CCntTest::IssueRequest
       
   269 //--------------------------------------------------------------------------
       
   270 //
       
   271 void CCntTest::IssueRequest()
       
   272     {
       
   273     TRequestStatus* status = &iStatus;
       
   274     User::RequestComplete(status, KErrNone );
       
   275     SetActive();
       
   276     }
       
   277