messagingfw/msgtestfw/TestActions/Base/Attachments/src/CMtfTestActionCreateAttachmentNoFileClose.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-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 // __ACTION_INFO_BEGIN__ 
       
    15 // [Action Name]
       
    16 // CreateAttachmentNoFileClose
       
    17 // [Action Parameters]
       
    18 // Session        <input>: Reference to the session.
       
    19 // MsgId		  <input>: Value of the message Id.
       
    20 // DataFilePath	  <input>: File path that contains the data to populate new attachment with.
       
    21 // AttachmentId   <output>: Value of the created attachment id.
       
    22 // [Action Description]
       
    23 // Creates a new file attachment on the specified message entry directly in message store.
       
    24 // This test action attempts to Commit the store without the attachment file handle closed.
       
    25 // [APIs Used]
       
    26 // CMsvSession::GetEntryL
       
    27 // CMsvStore::EditStoreL
       
    28 // CMsvStore::AttachmentManagerL
       
    29 // CMsvAttachment::NewL
       
    30 // CMsvAttachment::SetMimeType
       
    31 // MMsvAttachmentManager::CreateAttachmentL
       
    32 // __ACTION_INFO_END__
       
    33 // 
       
    34 //
       
    35 
       
    36 
       
    37 #include "CMtfTestActionCreateAttachmentNoFileClose.h"
       
    38 #include "CMtfAsyncWaiter.h"
       
    39 #include "CMtfTestCase.h"
       
    40 #include "CMtfTestActionParameters.h"
       
    41 #include "MtfTestActionUtilsUser.h"
       
    42 
       
    43 #include <miutset.h>
       
    44 #include <mmsvattachmentmanager.h>
       
    45 #include <cmsvattachment.h>
       
    46 
       
    47 CMtfTestAction* CMtfTestActionCreateAttachmentNoFileClose::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    48 	{
       
    49 	CMtfTestActionCreateAttachmentNoFileClose* self = new(ELeave) CMtfTestActionCreateAttachmentNoFileClose(aTestCase);
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL(aActionParameters);
       
    52 	CleanupStack::Pop();
       
    53 	return self;
       
    54 	}
       
    55 	
       
    56 
       
    57 CMtfTestActionCreateAttachmentNoFileClose::CMtfTestActionCreateAttachmentNoFileClose(CMtfTestCase& aTestCase)
       
    58 	: CMtfSynchronousTestAction(aTestCase)
       
    59 	{
       
    60 	}
       
    61 
       
    62 
       
    63 CMtfTestActionCreateAttachmentNoFileClose::~CMtfTestActionCreateAttachmentNoFileClose()
       
    64 	{
       
    65 	}
       
    66 
       
    67 
       
    68 void CMtfTestActionCreateAttachmentNoFileClose::ExecuteActionL()
       
    69 	{
       
    70 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionCreateAttachmentNoFileClose);
       
    71 	_LIT(KTxtFileName, "NewFile.txt");
       
    72 	_LIT8(KTxtFileMimeType, "*/*");
       
    73 	
       
    74 	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
       
    75 	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
       
    76 	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
       
    77 
       
    78 	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
       
    79 	CleanupStack::PushL(entry);
       
    80 	
       
    81 	CMsvStore* store = entry->EditStoreL();
       
    82 	CleanupStack::PushL(store);
       
    83 	
       
    84 	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
       
    85 	CleanupStack::PushL(waiter);
       
    86 	
       
    87 	CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
       
    88 	CleanupStack::PushL(attachment);
       
    89 
       
    90 	attachment->SetMimeTypeL(KTxtFileMimeType());
       
    91 	
       
    92 	MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
    93 	attachment->SetAttachmentNameL(KTxtFileName());
       
    94 	
       
    95 	RFile newFile;
       
    96 	manager.CreateAttachmentL(KTxtFileName, newFile, attachment, waiter->iStatus);
       
    97 	CleanupStack::Pop(attachment); // ownership passed to manager
       
    98 	waiter->StartAndWait();
       
    99 	CleanupClosePushL(newFile);
       
   100 	User::LeaveIfError(waiter->Result());
       
   101 	
       
   102 	TMsvAttachmentId attachmentId = attachment->Id();
       
   103 
       
   104 	PopulateFileL(newFile, *dataFilePath);
       
   105 	
       
   106 	// Don't close the returned file as we want to commit while file is open
       
   107 	// Should be able to Commit while file is open.
       
   108 	store->CommitL();
       
   109 	
       
   110 	CleanupStack::PopAndDestroy(&newFile);
       
   111 	CleanupStack::PopAndDestroy(waiter);
       
   112 	CleanupStack::PopAndDestroy(2, entry); // store, entry
       
   113 	
       
   114 	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(3));
       
   115 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionCreateAttachmentNoFileClose);
       
   116 	TestCase().ActionCompletedL(*this);
       
   117 	}
       
   118 
       
   119 void CMtfTestActionCreateAttachmentNoFileClose::PopulateFileL(RFile& aFile, const TDesC& aDataFilePath)
       
   120 	{
       
   121 	RFs fs;
       
   122 	User::LeaveIfError(fs.Connect());
       
   123 	CleanupClosePushL(fs);
       
   124 	
       
   125 	RFile dataFile;
       
   126 	User::LeaveIfError(dataFile.Open(fs, aDataFilePath, EFileRead));
       
   127 	CleanupClosePushL(fs);
       
   128 	
       
   129 	TInt fileSize = 0;
       
   130 	User::LeaveIfError(dataFile.Size(fileSize));
       
   131 	
       
   132 	HBufC8* dataBuffer = HBufC8::NewLC(fileSize);
       
   133 	TPtr8 bufferPtr(dataBuffer->Des());
       
   134 	
       
   135 	User::LeaveIfError(dataFile.Read(bufferPtr));
       
   136 	
       
   137 	User::LeaveIfError(aFile.Write(bufferPtr));
       
   138 	
       
   139 	CleanupStack::PopAndDestroy(3, &fs); // dataBuffer, dataFile, fs
       
   140 	}
       
   141