emailservices/emailstore/tsrc/message_store_test/src/PartTests.cpp
changeset 1 12c456ceeff2
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 
       
    20 // [INCLUDE FILES] - do not remove
       
    21 #include <tz.h>
       
    22 #include "PartTests.h"
       
    23 //#include <ismsgstorepropertykeys.h>
       
    24 #include "messagestoreclientserver.h"
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 CPartTests* CPartTests::NewL( CStifLogger* aLog )
       
    29     {
       
    30     CPartTests* self = new(ELeave) CPartTests( aLog );
       
    31     CleanupStack::PushL( self );
       
    32     self->ConstructL();
       
    33     CleanupStack::Pop( self );
       
    34     return self;
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // Constructor
       
    39 // -----------------------------------------------------------------------------
       
    40 CPartTests::CPartTests( CStifLogger* aLog ) : CAsyncTestCaseBase( aLog ) 
       
    41     {
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // Destructor
       
    46 // -----------------------------------------------------------------------------
       
    47 CPartTests::~CPartTests()
       
    48     {
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // 2nd phase constructor
       
    53 // -----------------------------------------------------------------------------
       
    54 void CPartTests::ConstructL()
       
    55     {
       
    56     CAsyncTestCaseBase::ConstructL();
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // ExecuteL : start executing the test case
       
    61 // -----------------------------------------------------------------------------
       
    62 TBool CPartTests::ExecuteL()
       
    63     {
       
    64     LogHeader( _L("Part") );
       
    65     
       
    66     //account 1
       
    67     CMsgStoreAccount* account1 = CMsgStoreAccount::NewLC( 1234, KNullDesC );   //+1
       
    68     CMsgStoreMailBox* mailBox1 = iMsgStore->CreateAccountL( *account1 );
       
    69     CleanupStack::PushL( mailBox1 );                                           //+2
       
    70 
       
    71     CMsgStorePropertyContainer* properties = CMsgStorePropertyContainer::NewL();
       
    72     CleanupStack::PushL( properties );                                              //+3
       
    73     properties->AddPropertyL( _L8("name"), _L("some name") );
       
    74     
       
    75     TMsgStoreId inbox1Id = mailBox1->CreateFolderL( mailBox1->Id(), *properties );
       
    76     CMsgStoreFolder* inbox1 = mailBox1->FolderL( inbox1Id );
       
    77     CleanupStack::PushL( inbox1 );                                              //+4
       
    78     
       
    79     CMsgStoreMessage* message1 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
    80     CleanupStack::PushL( message1 );                                               //+5
       
    81     message1->CommitL();
       
    82     
       
    83     RPointerArray<CMsgStoreMessagePart> parts;
       
    84 
       
    85     iLog->Log( _L("Case %d: List parts when no parts yet"), iCaseId++ );
       
    86     
       
    87     message1->ChildPartsL( parts );
       
    88     CheckCondition( _L("no parts"), parts.Count() == 0 );               
       
    89     parts.ResetAndDestroy();
       
    90 
       
    91     iLog->Log( _L("Case %d: Add a part with no content"), iCaseId++ );
       
    92 
       
    93     CMsgStoreMessagePart* part = message1->AddChildPartL( *properties );
       
    94     ComparePropertiesL( *part, *properties );
       
    95     CheckCondition( _L("parent ID is correct"), part->ParentId() == message1->Id() );
       
    96     
       
    97     iLog->Log( _L("Case %d: Get ChildPart"), iCaseId++ );
       
    98     
       
    99     CMsgStoreMessagePart* compareMe = message1->ChildPartL( part->Id() );
       
   100     ComparePropertiesL( *part, *compareMe );
       
   101     CheckCondition( _L("parent ID is correct"), compareMe->ParentId() == message1->Id() );
       
   102     
       
   103     iLog->Log( _L("Case %d: Add a child part to a child child part"), iCaseId++ );
       
   104     
       
   105     CMsgStoreMessagePart* grandChild = part->AddChildPartL( *properties );
       
   106     ComparePropertiesL( *grandChild, *properties );
       
   107     CheckCondition( _L("grandChild's parent ID is correct"), grandChild->ParentId() == part->Id() );
       
   108     
       
   109     iLog->Log( _L("Case %d: Testing ChildPartL(aRecursive=ETrue)"), iCaseId++ );
       
   110     delete compareMe;
       
   111     compareMe = message1->ChildPartL( grandChild->Id(), ETrue );
       
   112     ComparePropertiesL( *grandChild, *compareMe );
       
   113     CheckCondition( _L("GrandChild's parent ID is correct"), compareMe->ParentId() == part->Id() );
       
   114     delete compareMe;
       
   115     
       
   116     iLog->Log( _L("Case %d: Testing RemoveChildPartL(aRecursive=ETrue)"), iCaseId++ );
       
   117     message1->RemoveChildPartL( grandChild->Id(), ETrue );
       
   118     delete grandChild;    
       
   119 
       
   120     iLog->Log( _L("  List parts") );
       
   121     
       
   122     message1->ChildPartsL( parts );
       
   123     CheckCondition( _L("1 part"), parts.Count() == 1 );             
       
   124     ComparePropertiesL( *parts[0], *properties );
       
   125     CheckCondition( _L("parent ID is correct"), parts[0]->ParentId() == message1->Id() );
       
   126     parts.ResetAndDestroy();
       
   127     
       
   128     TMsgStoreId partId = part->Id();
       
   129     delete part;
       
   130     
       
   131     iLog->Log( _L("Case %d: Add an attachment with content using file path"), iCaseId++ );
       
   132 
       
   133     _LIT( KFilename, "C:\\test_content.txt" );
       
   134     _LIT8( KContent, "THIS IS THE CONTENT FOR THIS TEST" );
       
   135     RFs   fs;
       
   136     User::LeaveIfError( fs.Connect() );
       
   137     RFile file;
       
   138     User::LeaveIfError( file.Replace( fs, KFilename, EFileWrite ) );
       
   139     file.Write( KContent );
       
   140     file.Close();
       
   141     fs.Close();
       
   142 
       
   143     part = message1->AddChildPartL( *properties, KFilename );    //new part
       
   144     
       
   145     iLog->Log( _L("  List parts") );
       
   146     
       
   147     message1->ChildPartsL( parts );
       
   148     CheckCondition( _L("2 parts"), parts.Count() == 2 );                
       
   149 
       
   150     compareMe = NULL;
       
   151     if( parts[0]->Id() == part->Id() )
       
   152         {
       
   153         compareMe = parts[0];               
       
   154         }
       
   155     else if( parts[1]->Id() == part->Id() )
       
   156         {
       
   157         compareMe = parts[1];                               
       
   158         } // end if
       
   159         
       
   160     CheckCondition( _L("found"), compareMe != NULL );
       
   161     CheckCondition( _L("parent ID is correct"), compareMe->ParentId() == message1->Id() );
       
   162 
       
   163     iLog->Log( _L("  Verify content") );
       
   164     
       
   165     TBuf8<100> contentBuf;
       
   166     compareMe->FetchContentToBufferL( contentBuf );
       
   167     CheckCondition( _L("content matches"), contentBuf.Compare( KContent ) == 0 );
       
   168 
       
   169     parts.ResetAndDestroy();
       
   170     delete part;
       
   171     
       
   172     iLog->Log( _L("Case %d: Add an attachment with content using file handle"), iCaseId++ );
       
   173     RFs   fs2;
       
   174     User::LeaveIfError( fs2.Connect() );
       
   175     RFile file2; 
       
   176     fs2.ShareProtected();
       
   177     User::LeaveIfError( file2.Open( fs2, KFilename, EFileRead ) );
       
   178 
       
   179     part = message1->AddChildPartL( *properties, file2 );    //new part
       
   180     
       
   181     file2.Close();
       
   182     fs2.Close();
       
   183     
       
   184     iLog->Log( _L("  List parts") );
       
   185     
       
   186     message1->ChildPartsL( parts );
       
   187     CheckCondition( _L("2 parts"), parts.Count() == 3 );                
       
   188 
       
   189     compareMe = NULL;
       
   190     for (TInt i=0; i < parts.Count() ; i++ )
       
   191         {
       
   192         if( parts[i]->Id() == part->Id() )
       
   193             {
       
   194             compareMe = parts[i];
       
   195             break;
       
   196             }
       
   197         }
       
   198         
       
   199     CheckCondition( _L("found"), compareMe != NULL );
       
   200     CheckCondition( _L("parent ID is correct"), compareMe->ParentId() == message1->Id() );
       
   201 
       
   202     iLog->Log( _L("  Verify content") );
       
   203     
       
   204     compareMe->FetchContentToBufferL( contentBuf );
       
   205     CheckCondition( _L("content matches"), contentBuf.Compare( KContent ) == 0 );
       
   206 
       
   207     parts.ResetAndDestroy();
       
   208     
       
   209     
       
   210     iLog->Log( _L("Case %d: Copy part"), iCaseId++ );
       
   211     
       
   212     CMsgStoreMessage* message2 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
   213     CleanupStack::PushL( message2 );                                                //+6
       
   214     message2->CommitL();
       
   215     
       
   216     TMsgStoreId newId = message1->CopyChildPartL( part->Id(), message2->Id() );
       
   217 
       
   218     CMsgStoreMessagePart* newpart = message2->ChildPartL( newId );
       
   219     ComparePropertiesL( *part, *newpart );
       
   220 
       
   221     contentBuf.SetLength( 0 );
       
   222     newpart->FetchContentToBufferL( contentBuf );
       
   223     CheckCondition( _L("content matches"), contentBuf.Compare( KContent ) == 0 );
       
   224     
       
   225     delete newpart;
       
   226     
       
   227     iLog->Log( _L("Case %d: Remove parts"), iCaseId++ );
       
   228     message1->RemoveChildPartL( part->Id() );
       
   229     message1->RemoveChildPartL( partId );
       
   230     
       
   231     message1->ChildPartsL( parts );
       
   232         
       
   233     CheckCondition( _L("one part left"), parts.Count() == 1 );              
       
   234     parts.ResetAndDestroy();
       
   235     
       
   236     delete part;
       
   237     
       
   238     iLog->Log( _L("Case %d: Nested parts"), iCaseId++ );
       
   239     // put an attachment back
       
   240     CMsgStorePropertyContainer* nestedProps = CMsgStorePropertyContainer::NewL();
       
   241     nestedProps->AddPropertyL(_L8("prop1"), _L("value 1") );
       
   242     
       
   243     part = message1->AddChildPartL( *properties );          
       
   244     CMsgStoreMessagePart* nestedPart = part->AddChildPartL( *nestedProps );
       
   245     nestedPart->ReplaceContentL( KContent );
       
   246     
       
   247     CMsgStoreMessagePart* comparePart = part->ChildPartL( nestedPart->Id() );
       
   248     ComparePropertiesL( *comparePart, *nestedProps );
       
   249     CheckCondition( _L("content length matches"), comparePart->ContentLengthL() == nestedPart->ContentLengthL() );
       
   250     comparePart->FetchContentToBufferL( contentBuf );
       
   251     CheckCondition( _L("content matches"), contentBuf == KContent );
       
   252     
       
   253     delete comparePart;
       
   254     
       
   255     iLog->Log( _L("Case %d: Fetch nested part from mailBox object"), iCaseId++ );
       
   256     
       
   257     CMsgStoreWritablePropertyContainer* container = mailBox1->FetchPropertiesL( nestedPart->Id() );
       
   258     ComparePropertiesL( *container, *nestedPart );
       
   259     delete container;
       
   260     
       
   261     iLog->Log( _L("Case %d: Fetch part from mailBox object"), iCaseId++ );
       
   262     
       
   263     container = mailBox1->FetchPropertiesL( part->Id() );
       
   264     ComparePropertiesL( *container, *part );
       
   265     delete container;
       
   266     
       
   267     iLog->Log( _L("Case %d: Fetch message from mailBox object"), iCaseId++ );
       
   268     
       
   269     container = mailBox1->FetchPropertiesL( message1->Id() );
       
   270     ComparePropertiesL( *container, *message1 );
       
   271     delete container;
       
   272     
       
   273     iLog->Log( _L("Case %d: Fetch folder from mailBox object"), iCaseId++ );
       
   274     
       
   275     container = mailBox1->FetchPropertiesL( inbox1->Id() );
       
   276     ComparePropertiesL( *container, *inbox1 );
       
   277     delete container;
       
   278     
       
   279     iLog->Log( _L("Case %d: ContainerTypeById"), iCaseId++ );
       
   280     
       
   281     TMsgStoreContainerType contentType = mailBox1->ContainerTypeById( part->Id() );
       
   282     CheckCondition( _L("container type is Part"), contentType == EMsgStorePartContainer );
       
   283     
       
   284     contentType = mailBox1->ContainerTypeById( message1->Id() );
       
   285     CheckCondition( _L("container type is Message"), contentType == EMsgStoreMessageContainer );
       
   286     
       
   287     contentType = mailBox1->ContainerTypeById( inbox1->Id() );
       
   288     CheckCondition( _L("container type is Folder"), contentType == EMsgStoreFolderContainer );
       
   289     
       
   290     contentType = mailBox1->ContainerTypeById( mailBox1->Id() );
       
   291     CheckCondition( _L("container type is Mailbox"), contentType == EMsgStoreMailboxContainer );
       
   292     
       
   293     /******************************************************************
       
   294      * (end of) Testing of the new CMsgStoreMailBox methods: FetchPropertiesL() and
       
   295      * ContentTypeBuId() requested by the Mail for Exchange team
       
   296      * ****************************************************************/
       
   297     
       
   298     iLog->Log( _L("Case %d: Add a message as an attachment (embedded msg)"), iCaseId++ );
       
   299     
       
   300     CMsgStoreMessage* nestedMsg = part->AddChildMessageL( *nestedProps );
       
   301     nestedMsg->ReplaceContentL( KContent );
       
   302     comparePart = part->ChildPartL( nestedMsg->Id() );
       
   303     ComparePropertiesL( *nestedMsg, *comparePart );
       
   304     comparePart->FetchContentToBufferL( contentBuf );
       
   305     CheckCondition( _L("content matches"), contentBuf == KContent );
       
   306     CheckCondition( _L("IsEmbeddedMessage()"), comparePart->IsEmbeddedMessageL() );
       
   307 
       
   308     iLog->Log( _L("Case %d: CommitL() not supported for embedded msgs"), iCaseId++ );
       
   309     //it should be safe to cast it to message
       
   310     CMsgStoreMessage* castedMessage = static_cast<CMsgStoreMessage*>(comparePart);
       
   311     //CommitL() should leave with KErrNotSupported
       
   312     TRAPD( err, castedMessage->CommitL() );
       
   313     CheckCondition( _L("CommitL() not supported for embedded msgs"), err = KErrNotSupported );
       
   314     
       
   315     iLog->Log( _L("Case %d: AbandonL() not supported for embedded msgs"), iCaseId++ );
       
   316     TRAP( err, castedMessage->AbandonL() );
       
   317     CheckCondition( _L("AbandonL() not supported for embedded msgs"), err = KErrNotSupported );
       
   318     
       
   319     RPointerArray<CMsgStoreMessagePart> nestedParts;
       
   320     part->ChildPartsL( nestedParts );
       
   321     CheckCondition( _L("2 nested parts"), nestedParts.Count() == 2 );
       
   322     
       
   323     nestedParts.ResetAndDestroy();
       
   324     delete comparePart;
       
   325     delete nestedMsg;
       
   326     delete nestedPart;
       
   327     delete part;
       
   328     delete nestedProps;
       
   329     
       
   330     // ERROR PATH TESTS
       
   331     
       
   332     // put an attachment back
       
   333     part = message1->AddChildPartL( *properties );          
       
   334     CleanupStack::PushL( part );                                                //+7
       
   335     
       
   336     iLog->Log( _L("Case %d: Error - ChildPartL for invalid part ID"), iCaseId++ );
       
   337     
       
   338     TRAPD( result, message1->ChildPartL( EMsgStorePartBits | 50 ) );
       
   339     CheckCondition( _L("not found"), result == KErrNotFound );
       
   340     
       
   341     iLog->Log( _L("Case %d: Error - ChildPartL for wrong message"), iCaseId++ );
       
   342     
       
   343     TRAP( result, message2->ChildPartL( part->Id() ) );
       
   344     CheckCondition( _L("not found"), result == KErrNotFound );
       
   345     
       
   346     iLog->Log( _L("Case %d: Error - AddAttachmentL (content) with nonexistent file"), iCaseId++ );
       
   347     
       
   348     CMsgStoreMessagePart* tempPart = message2->AddChildPartL( *properties );
       
   349     TRAP( result, tempPart->ReplaceContentWithFileL( _L("C:\\some_nonexistent_file.txt") ) );
       
   350     CheckCondition( _L("not found"), result == KErrNotFound );
       
   351     delete tempPart;
       
   352 
       
   353     iLog->Log( _L("Case %d: Error - RemoveChildPartL for invalid part ID"), iCaseId++ );
       
   354     
       
   355     TRAP( result, message2->RemoveChildPartL( part->Id() + 50  ) );
       
   356     CheckCondition( _L("not found"), result == KErrNotFound );
       
   357     
       
   358     // wrong parameter types
       
   359 
       
   360     iLog->Log( _L("Case %d: Error - ChildPartL with wrong type for part"), iCaseId++ );
       
   361     
       
   362     TRAP( result, message2->ChildPartL( message1->Id() ) );
       
   363     CheckCondition( _L("argument error"), result == KErrArgument );
       
   364     
       
   365     iLog->Log( _L("Case %d: Error - RemoveChildPartL with wrong type for part"), iCaseId++ );
       
   366     
       
   367     TRAP( result, message2->RemoveChildPartL( message1->Id() ) );
       
   368     CheckCondition( _L("argument error"), result == KErrArgument );
       
   369     
       
   370     // OUT OF MEMORY ERROR PATH TESTS
       
   371 
       
   372     iLog->Log( _L("Case %d: Error - Out of memory"), iCaseId++ );
       
   373     for( TInt i = 0; i < 1; i++ )
       
   374         {        
       
   375         SimulateLowDiskSpaceL( i );
       
   376         TRAP( result, message1->AddChildPartL( *properties ) );
       
   377         CheckCondition( _L("out of memory"), result == KErrNoMemory );
       
   378         }
       
   379         
       
   380     CleanupStack::PopAndDestroy( 7 );
       
   381     
       
   382     //we are done 
       
   383     return ETrue;
       
   384     }
       
   385 
       
   386 
       
   387 //  End of File