emailservices/emailstore/tsrc/message_store_test/src/MruTests.cpp
changeset 1 12c456ceeff2
child 8 e1b6206813b4
equal deleted inserted replaced
0:8466d47a6819 1:12c456ceeff2
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 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:  Base class for all test cases class member functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //#include <BAUTILS.H>
       
    20 #include "MruTests.h"
       
    21 //#include <ismsgstorepropertykeys.h>
       
    22 #include "messagestoreclientserver.h"
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 CMruTests* CMruTests::NewL( CStifLogger* aLog )
       
    27     {
       
    28     CMruTests* self = new(ELeave) CMruTests( aLog );
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL();
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // Constructor
       
    37 // -----------------------------------------------------------------------------
       
    38 CMruTests::CMruTests( CStifLogger* aLog ) : CAsyncTestCaseBase( aLog ) 
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // Destructor
       
    44 // -----------------------------------------------------------------------------
       
    45 CMruTests::~CMruTests()
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // 2nd phase constructor
       
    51 // -----------------------------------------------------------------------------
       
    52 void CMruTests::ConstructL()
       
    53     {
       
    54     CAsyncTestCaseBase::ConstructL();
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // ExecuteL : start executing the test case
       
    59 // -----------------------------------------------------------------------------
       
    60 TBool CMruTests::ExecuteL()
       
    61     {
       
    62     LogHeader( _L("MRU") );
       
    63     
       
    64     CMsgStoreAccount* account1 = CMsgStoreAccount::NewL(1234, _L("Account 1") );
       
    65     CleanupStack::PushL( account1 );                                            //+1
       
    66     
       
    67     CMsgStoreAccount* account2 = CMsgStoreAccount::NewL(1234, _L("") );
       
    68     CleanupStack::PushL( account2 );                                            //+2
       
    69     
       
    70     CMsgStoreAccount* account3 = CMsgStoreAccount::NewL(5678, _L("Account 3") );
       
    71     CleanupStack::PushL( account3 );                                            //+3
       
    72     
       
    73     CMsgStoreMailBox* mailBox1 = iMsgStore->CreateAccountL( *account1 );
       
    74     CleanupStack::PushL( mailBox1 );                                            //+4
       
    75     
       
    76     CMsgStoreMailBox* mailBox2 = iMsgStore->CreateAccountL( *account2 );
       
    77     CleanupStack::PushL( mailBox2 );                                            //+5
       
    78     
       
    79     CMsgStoreMailBox* mailBox3 = iMsgStore->CreateAccountL( *account3 );
       
    80     CleanupStack::PushL( mailBox3 );                                            //+6
       
    81 
       
    82     iLog->Log( _L("Case %d: Calling MruAddressesL() on empty mailbox"), iCaseId++ );
       
    83     RPointerArray<CMsgStoreAddress> expectedArray;
       
    84     CompareMruArraysL( mailBox2, expectedArray );
       
    85     
       
    86     RPointerArray<CMsgStoreAddress> inputArray;
       
    87     
       
    88     TBuf<100> addrBuf;
       
    89     TBuf<100> nameBuf;
       
    90     
       
    91     for ( TInt i = 0 ; i < 20 ; i++ )
       
    92         {
       
    93         addrBuf.Format(_L("name%d@nok%d.com"), i, i );
       
    94         nameBuf.Format(_L("Fname%d LName%d"), i, i );
       
    95         if ( i == 3 || i == 6 )
       
    96             {
       
    97             nameBuf.Copy( KNullDesC );
       
    98             }
       
    99         CMsgStoreAddress* address = CMsgStoreAddress::NewL( addrBuf, nameBuf );
       
   100         inputArray.Append( address );
       
   101         expectedArray.Append( address );
       
   102         }
       
   103     
       
   104     TRAPD( err, mailBox1->AddMruAddressesL( inputArray ) );
       
   105     CheckCondition( _L("AddMruAddressesL() ok"), err == KErrNone );
       
   106     
       
   107     CompareMruArraysL( mailBox1, expectedArray );
       
   108     
       
   109     inputArray.Reset();  //do not destroy because the memory is also in the expectedArray
       
   110    
       
   111     iLog->Log( _L("Case %d: Add more address to the top of MRU address list"), iCaseId++ );
       
   112     for ( TInt i = 0 ; i < 20 ; i++ )
       
   113         {
       
   114         addrBuf.Format(_L("new name%d@nok%d.com"), i, i );
       
   115         nameBuf.Format(_L("new Fname%d LName%d"), i, i );
       
   116         if ( i == 3 || i == 7 )
       
   117             {
       
   118             nameBuf.Copy( KNullDesC );
       
   119             }
       
   120         CMsgStoreAddress* address = CMsgStoreAddress::NewL( addrBuf, nameBuf );
       
   121         inputArray.Append( address );
       
   122         //add the new addresss to the top of the array in the correct order
       
   123         expectedArray.Insert( address, i );
       
   124         }
       
   125     
       
   126     iLog->Log( _L("Case %d: Calling AddMruAddressesL()"), iCaseId++ );
       
   127     TRAP( err, mailBox1->AddMruAddressesL( inputArray ) );
       
   128     CheckCondition( _L("AddMruAddressesL() ok"), err == KErrNone );
       
   129     
       
   130     CompareMruArraysL( mailBox1, expectedArray );
       
   131     
       
   132     iLog->Log( _L("Case %d: Promoting some address to top and add some to the top"), iCaseId++ );
       
   133     inputArray.Reset();
       
   134     for ( TInt i = 0 ; i < expectedArray.Count() ; i += 3 )
       
   135         {
       
   136         CMsgStoreAddress* address = expectedArray[i];
       
   137         inputArray.Insert( address, 0 );
       
   138         expectedArray.Remove(i);
       
   139         expectedArray.Insert( address, 0 );
       
   140         }
       
   141     for ( TInt i = 40 ; i < 43 ; i++ )
       
   142         {
       
   143         addrBuf.Format(_L("new name%d@nok%d.com"), i, i );
       
   144         nameBuf.Format(_L("new Fname%d LName%d"), i, i );
       
   145         CMsgStoreAddress* address = CMsgStoreAddress::NewL( addrBuf, nameBuf );
       
   146         inputArray.Insert(address, 5);
       
   147         expectedArray.Insert(address, 5);
       
   148         }
       
   149     TRAP( err, mailBox1->AddMruAddressesL( inputArray ) );
       
   150     CheckCondition( _L("AddMruAddressesL() ok"), err == KErrNone );
       
   151     
       
   152     CompareMruArraysL( mailBox1, expectedArray );
       
   153     
       
   154     TInt newMaxCount=30;
       
   155     iLog->Log( _L("Case %d: Change the MaxMruAddressCount"), iCaseId++ );
       
   156     iMsgStore->SetMaxMruAddressCountL( newMaxCount );
       
   157     while( expectedArray.Count() > newMaxCount )
       
   158         {
       
   159         CMsgStoreAddress* address = expectedArray[newMaxCount];
       
   160         expectedArray.Remove(newMaxCount);
       
   161         delete address;
       
   162         }
       
   163     CompareMruArraysL( mailBox1, expectedArray );
       
   164 
       
   165     inputArray.Reset();
       
   166     
       
   167     iLog->Log( _L("Case %d: Add some more to make sure only %d are returned"), iCaseId++ );
       
   168     for ( int i = 0 ; i < 10 ; i++ )
       
   169         {
       
   170         addrBuf.Format(_L("newer name%d@nok%d.com"), i, i );
       
   171         nameBuf.Format(_L("newer Fname%d LName%d"), i, i );
       
   172         CMsgStoreAddress* address = CMsgStoreAddress::NewL( addrBuf, nameBuf );
       
   173         inputArray.Insert(address, 0);
       
   174         expectedArray.Insert(address, 0);
       
   175         }
       
   176     while( expectedArray.Count() > newMaxCount )
       
   177         {
       
   178         CMsgStoreAddress* address = expectedArray[newMaxCount];
       
   179         expectedArray.Remove(newMaxCount);
       
   180         delete address;
       
   181         }
       
   182     
       
   183     TRAP( err, mailBox1->AddMruAddressesL( inputArray ) );
       
   184     CheckCondition( _L("AddMruAddressesL() ok"), err == KErrNone );
       
   185     
       
   186     CompareMruArraysL( mailBox1, expectedArray );
       
   187     
       
   188     //__LOG_WRITE_INFO("Test Dynamic Encryption")
       
   189     //TRAP( err, iMsgStore->EnableEncryptionL() );
       
   190     //CheckCondition( _L("EnableEncryption() should return KErrNone or KErrAlreadyExists() ok"), err == KErrNone || err == KErrAlreadyExists);
       
   191     //if ( err == KErrAlreadyExists )
       
   192     //    {
       
   193         //encryption may be still going on, make sure it's complete before calling DisableEncryption()
       
   194     //    WaitUntilEncryptDecryptCompleteL( EMsgStoreESEncrypting, mailBox1 );
       
   195         
       
   196     //    __LOG_WRITE_INFO("Store is encrypted, decrypt it")
       
   197     //    iMsgStore->DisableEncryptionL();
       
   198     //    __LOG_WRITE_INFO("Wait for decryption to complete")
       
   199     //    WaitUntilEncryptDecryptCompleteL( EMsgStoreESDecrypting, mailBox1 );
       
   200     //    }
       
   201     //else
       
   202     //    {
       
   203     //    __LOG_WRITE_INFO("Wait for encryption to complete")
       
   204     //    WaitUntilEncryptDecryptCompleteL( EMsgStoreESEncrypting, mailBox1 );
       
   205     //    }
       
   206     
       
   207     //__LOG_WRITE_INFO("Comparing results")
       
   208     //CompareMruArraysL( mailBox1, expectedArray );
       
   209     
       
   210     //add a few more address to see if it works when encryption is turned on
       
   211 
       
   212     inputArray.Reset();
       
   213     
       
   214     CleanupStack::PopAndDestroy( 6 );
       
   215     
       
   216     //we are done 
       
   217     return ETrue;
       
   218     }
       
   219 
       
   220 void CMruTests::CompareMruArraysL( CMsgStoreMailBox* aMailBox, RPointerArray<CMsgStoreAddress>& aExpected )
       
   221     {
       
   222     iLog->Log(_L("  >> CompareMruArraysL") );
       
   223     
       
   224     RPointerArray<CMsgStoreAddress> results;
       
   225     TRAPD(err, aMailBox->MruAddressesL( results ) );
       
   226     CheckCondition( _L("MruAddressesL() ok"), err == KErrNone );
       
   227 
       
   228     CheckCondition( _L("same counts"), aExpected.Count() == results.Count() );
       
   229     
       
   230     TInt count = aExpected.Count();
       
   231     
       
   232     iLog->Log( _L("  comparing %d addresses"), count);
       
   233     
       
   234     for ( TInt i = 0 ; i < count  ; i++ )
       
   235         {
       
   236         CMsgStoreAddress* addr1 = aExpected[i];
       
   237         CMsgStoreAddress* addr2 = results[i];
       
   238         iLog->Log( _L("  i=%d, addr=%S, dispName=%S"), i, &addr2->EmailAddress(), &addr2->DisplayName() );
       
   239         if ( addr1->EmailAddress() != addr2->EmailAddress() )
       
   240             {
       
   241             iLog->Log( _L("  Different email address found!! index=%d addr1=%S, addr2=%S"), i, &addr1->EmailAddress(), &addr2->EmailAddress() );
       
   242             User::Leave( KErrNotFound );
       
   243             }
       
   244         if ( addr1->DisplayName() != addr2->DisplayName() )
       
   245             {
       
   246             iLog->Log( _L("  Different display name found!! index=%d name1=%S, name2=%S"), i, &addr1->DisplayName(), &addr2->DisplayName() );
       
   247             User::Leave( KErrNotFound );
       
   248             }
       
   249         }
       
   250     
       
   251     results.ResetAndDestroy();
       
   252     
       
   253     iLog->Log(_L("  addresses all match!") );
       
   254     
       
   255     iLog->Log(_L("  << CompareMruArraysL") );
       
   256     
       
   257     }
       
   258 
       
   259 
       
   260 //  End of File