messagingfw/wappushfw/tpush/t_clientmtmfind.cpp
changeset 22 bde600d88860
parent 0 8e480a14352b
equal deleted inserted replaced
21:08008ce8a6df 22:bde600d88860
       
     1 // Copyright (c) 2003-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 #include <mtclreg.h>
       
    17 #include <msvids.h>
       
    18 #include <msvuids.h>
       
    19 #include <txtrich.h>
       
    20 #include <mtmdef.h>
       
    21 #include <pushentry.h>
       
    22 #include "pushtests.h"
       
    23 #include "pushclientmtm.h"
       
    24 
       
    25 /*------------------------------------------------------------------------------
       
    26 //------------------------------------------------------------------------------
       
    27 Test Procedure:
       
    28  Proper behaviour is that a search for a string returns the parts of the message
       
    29  in which it resides. Current parts to search are Body, Originator and 
       
    30  Description.
       
    31 
       
    32 //------------------------------------------------------------------------------
       
    33 //----------------------------------------------------------------------------*/
       
    34 
       
    35 const TInt KPushTestBuffer = 128;
       
    36 
       
    37 _LIT(KTest0, "Test0");
       
    38 _LIT(KTest1, "Test1");
       
    39 _LIT(KTest2, "Test2");
       
    40 _LIT(KTest3, "Test3");
       
    41 _LIT(KTest4, "Test4");
       
    42 _LIT(KTest5, "Test5");
       
    43 _LIT(KTest6, "Test6");
       
    44 _LIT(KTest7, "Test7");
       
    45 _LIT(KTest8, "Test8");
       
    46 _LIT(KLogSucceeded, "OK");
       
    47 _LIT(KLogFailed, "FAILED");
       
    48 _LIT(KLogOutput,"WapPushClientMtm %S %S");
       
    49 _LIT(KSpace, " ");
       
    50 
       
    51 _LIT(KMTMDataFullName,"z:\\system\\data\\wappushmtm.rsc");
       
    52 
       
    53 //------------------------------------------------------------------------------
       
    54 void CWapClientMtmFindTest::RunL()
       
    55 	{
       
    56 	iDummyObserver = new(ELeave) CDummyObserver;
       
    57 	iSession = CMsvSession::OpenSyncL(*iDummyObserver);
       
    58 	// Install the mtm group if necessary
       
    59 	TInt returnVal = iSession->InstallMtmGroup(KMTMDataFullName);
       
    60 	if (returnVal != KErrNone && returnVal != KErrAlreadyExists)
       
    61 		User::Leave(returnVal);
       
    62 
       
    63 	CBaseMtm* wapPushClientMtm = NULL;
       
    64 	CClientMtmRegistry* mtmRegistry = CClientMtmRegistry::NewL(*iSession);
       
    65 	CleanupStack::PushL(mtmRegistry);
       
    66 
       
    67 	wapPushClientMtm = mtmRegistry->NewMtmL(KUidMtmWapPush);
       
    68 	CleanupStack::PushL(wapPushClientMtm);
       
    69 
       
    70 	CMsvEntry* entry = CMsvEntry::NewL(*iSession, KMsvRootIndexEntryId, TMsvSelectionOrdering());
       
    71 	CleanupStack::PushL(entry);
       
    72 	SetupFindDataL(entry);
       
    73 
       
    74 	entry->SetEntryL(iMessageId);
       
    75 	CleanupStack::Pop(entry);
       
    76 	wapPushClientMtm->SetCurrentEntryL(entry);
       
    77 
       
    78 	// restore the message
       
    79 	entry->SetEntryL(iMessageId);
       
    80 	CMsvStore* msvStore1 = entry->ReadStoreL();
       
    81 	CleanupStack::PushL(msvStore1);
       
    82 	msvStore1->RestoreBodyTextL(wapPushClientMtm->Body());
       
    83 	CleanupStack::PopAndDestroy(msvStore1);
       
    84 
       
    85 	PerformTests(wapPushClientMtm);
       
    86 	
       
    87 	// Delete the entry
       
    88 	entry->SetEntryL(iMessageId);
       
    89 	CMsvStore* msvStore2 = entry->EditStoreL();
       
    90 	CleanupStack::PushL(msvStore2);
       
    91 	msvStore2->DeleteBodyTextL();
       
    92 	msvStore2->CommitL();
       
    93 	CleanupStack::PopAndDestroy(msvStore2);
       
    94 
       
    95 	// Install the mtm group if necessary
       
    96 	returnVal = iSession->DeInstallMtmGroup(KMTMDataFullName);
       
    97 
       
    98 	CleanupStack::PopAndDestroy(2, mtmRegistry); // wapPushClientMtm, mtmRegistry
       
    99 	entry = NULL; // Removed by mtm being destroyed
       
   100 	}
       
   101 
       
   102 //------------------------------------------------------------------------------
       
   103 void CWapClientMtmFindTest::SetupFindDataL(CMsvEntry* aEntry)
       
   104 	{
       
   105 	/*
       
   106 	* Test method. Currently 3 areas are searched for when doing a find:
       
   107 	* Body, details (originator) and descriptor. Find returns a value of OR'd
       
   108 	* flags representing the areas in which the given text was found. To test
       
   109 	* this properly we want to test all combinations. 2^3 = 8 tests. A '1'
       
   110 	* represents the existence of the searched-for text in the repective
       
   111 	* location.
       
   112 	* 
       
   113 	* Test  Body Dtls Desc
       
   114 	*
       
   115 	* Test0    0    0    0
       
   116 	* Test1    0    0    1
       
   117 	* Test2    0    1    0
       
   118 	* Test3    0    1    1
       
   119 	* Test4    1    0    0
       
   120 	* Test5    1    0    1
       
   121 	* Test6    1    1    0
       
   122 	* Test7    1    1    1
       
   123 	*
       
   124 	* Test 8 repeats test 7 with a leave
       
   125 	*/
       
   126 
       
   127 	TMsvEntry tmsvEntry;
       
   128 
       
   129 	HBufC* descriptorContents = HBufC::NewLC(KPushTestBuffer);
       
   130 	HBufC* originatorContents = HBufC::NewLC(KPushTestBuffer);
       
   131 	HBufC* bodyContents = HBufC::NewLC(KPushTestBuffer);
       
   132 	TPtr contentPtr(descriptorContents->Des());
       
   133 
       
   134 	tmsvEntry.SetComplete(EFalse);
       
   135 	tmsvEntry.SetReadOnly(EFalse);
       
   136 	tmsvEntry.iMtm       = KUidMtmWapPush;
       
   137 	tmsvEntry.iType      = KUidMsvMessageEntry;
       
   138 	tmsvEntry.iServiceId = KMsvLocalServiceIndexEntryId;
       
   139 
       
   140 	// Set the descriptor for description contents.
       
   141 	contentPtr.Append(KTest1);
       
   142 	contentPtr.Append(KSpace);
       
   143 	contentPtr.Append(KTest3);
       
   144 	contentPtr.Append(KSpace);
       
   145 	contentPtr.Append(KTest5);
       
   146 	contentPtr.Append(KSpace);
       
   147 	contentPtr.Append(KTest7);
       
   148 	tmsvEntry.iDescription.Set(contentPtr);
       
   149 
       
   150 	// Set the details contents for details contents.
       
   151 	contentPtr.Set(originatorContents->Des());
       
   152 	contentPtr.Append(KTest2);
       
   153 	contentPtr.Append(KSpace);
       
   154 	contentPtr.Append(KTest3);
       
   155 	contentPtr.Append(KSpace);
       
   156 	contentPtr.Append(KTest6);
       
   157 	contentPtr.Append(KSpace);
       
   158 	contentPtr.Append(KTest7);
       
   159 	tmsvEntry.iDetails.Set(contentPtr);
       
   160 
       
   161 	// Set the descriptor contents for body contents.
       
   162 	contentPtr.Set(bodyContents->Des());
       
   163 	contentPtr.Append(KTest4);
       
   164 	contentPtr.Append(KSpace);
       
   165 	contentPtr.Append(KTest5);
       
   166 	contentPtr.Append(KSpace);
       
   167 	contentPtr.Append(KTest6);
       
   168 	contentPtr.Append(KSpace);
       
   169 	contentPtr.Append(KTest7);
       
   170 
       
   171 	// Put an entry in a known location - the outbox
       
   172 	aEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   173 	aEntry->CreateL(tmsvEntry);
       
   174 	iSession->CleanupEntryPushL(tmsvEntry.Id());
       
   175 	iMessageId = tmsvEntry.Id();
       
   176 	aEntry->SetEntryL(iMessageId);
       
   177 
       
   178 	CParaFormatLayer* paraFormatLayer=CParaFormatLayer::NewL();
       
   179 	CleanupStack::PushL(paraFormatLayer);
       
   180 
       
   181 	CCharFormatLayer* charFormatLayer=CCharFormatLayer::NewL(); 
       
   182 	CleanupStack::PushL(charFormatLayer);
       
   183 
       
   184 	CRichText* bodyText = CRichText::NewL(paraFormatLayer, charFormatLayer);
       
   185 	CleanupStack::PushL(bodyText);
       
   186 	bodyText->InsertL(0, contentPtr);
       
   187 
       
   188 	CMsvStore* msvStore = aEntry->EditStoreL();
       
   189 	CleanupStack::PushL(msvStore);
       
   190 	msvStore->StoreBodyTextL(*bodyText);
       
   191 	msvStore->CommitL();
       
   192 	
       
   193 	tmsvEntry.SetReadOnly(EFalse);
       
   194 	tmsvEntry.SetVisible(ETrue);
       
   195 	tmsvEntry.SetInPreparation(EFalse);
       
   196 	aEntry->ChangeL(tmsvEntry);
       
   197 	
       
   198 	CleanupStack::PopAndDestroy(4, paraFormatLayer); // msvStore, bodyText, charFormatLayer, paraFormatLayer
       
   199 	iSession->CleanupEntryPop();
       
   200 	CleanupStack::PopAndDestroy(3, descriptorContents); // bodyContents, originiatorContents, descriptorContents
       
   201 	}
       
   202 
       
   203 //------------------------------------------------------------------------------
       
   204 void CWapClientMtmFindTest::PerformTests(CBaseMtm* aWapPushClientMtm)
       
   205 	{
       
   206 	TMsvPartList noWhere = 0;
       
   207 	TMsvPartList allParts = KMsvMessagePartBody | 
       
   208 		KMsvMessagePartOriginator | 
       
   209 		KMsvMessagePartDescription;
       
   210 	TMsvPartList originatorAndDescription = 
       
   211 		KMsvMessagePartOriginator | KMsvMessagePartDescription;
       
   212 	TMsvPartList bodyAndDescription = 
       
   213 		KMsvMessagePartBody | KMsvMessagePartDescription;
       
   214 	TMsvPartList bodyAndOriginator =
       
   215 		KMsvMessagePartBody | KMsvMessagePartOriginator;
       
   216 
       
   217 	TestAndPrint(aWapPushClientMtm, KTest0, noWhere);
       
   218 	TestAndPrint(aWapPushClientMtm, KTest1, KMsvMessagePartDescription);
       
   219 	TestAndPrint(aWapPushClientMtm, KTest2, KMsvMessagePartOriginator);
       
   220 	TestAndPrint(aWapPushClientMtm, KTest3, originatorAndDescription);
       
   221 	TestAndPrint(aWapPushClientMtm, KTest4, KMsvMessagePartBody);
       
   222 	TestAndPrint(aWapPushClientMtm, KTest5, bodyAndDescription);
       
   223 	TestAndPrint(aWapPushClientMtm, KTest6, bodyAndOriginator);
       
   224 	TestAndPrint(aWapPushClientMtm, KTest7, allParts);
       
   225 
       
   226 	// Test8 : perform Test7 again, but with a memory allocation failure.
       
   227 #if defined(_DEBUG)
       
   228 	TInt fail=0;
       
   229 #endif
       
   230 	TInt err = KErrNone;
       
   231 
       
   232 	__UHEAP_FAILNEXT(fail);
       
   233 	__UHEAP_MARK;
       
   234 	TRAP(err, aWapPushClientMtm->Find(KTest7, allParts));
       
   235 	__UHEAP_RESET;
       
   236 	__UHEAP_MARKEND;
       
   237 
       
   238 	TBuf<KPushLogBuffer> buffer;
       
   239 	if (err == KErrNone)
       
   240 		{
       
   241 		buffer.Format(KLogOutput, &KTest8, &KLogSucceeded);
       
   242 		}
       
   243 	else
       
   244 		{
       
   245 		buffer.Format(KLogOutput, &KTest8, &KLogFailed);
       
   246 		}
       
   247 	WPLPrintf(buffer);
       
   248 
       
   249 	iEngine->TestCompleted(iStatus.Int());
       
   250 	}
       
   251 
       
   252 //------------------------------------------------------------------------------
       
   253 void CWapClientMtmFindTest::TestAndPrint(CBaseMtm* aWapPushClientMtm, const TDesC& aTestText, TMsvPartList aResultList)
       
   254 	{
       
   255 	TBuf<KPushLogBuffer> buffer;
       
   256 	TMsvPartList allParts = KMsvMessagePartBody | 
       
   257 		KMsvMessagePartOriginator | 
       
   258 		KMsvMessagePartDescription;
       
   259 
       
   260 	if (aWapPushClientMtm->Find(aTestText, allParts) == aResultList)
       
   261 		{
       
   262 		buffer.Format(KLogOutput, &aTestText, &KLogSucceeded);
       
   263 		}
       
   264 	else
       
   265 		{
       
   266 		buffer.Format(KLogOutput, &aTestText, &KLogFailed);
       
   267 		}
       
   268 	WPLPrintf(buffer);
       
   269 	}
       
   270 
       
   271 //------------------------------------------------------------------------------
       
   272 void CWapClientMtmFindTest::DoCancel()
       
   273 	{
       
   274 	iEngine->TestCompleted(KErrCancel);
       
   275 	}
       
   276 
       
   277 //------------------------------------------------------------------------------
       
   278 TInt CWapClientMtmFindTest::RunError(TInt aError)
       
   279 	{
       
   280 	TBuf<80> buf;
       
   281 	_LIT(KRunError, "CWapClientMtmFindTest::RunError");
       
   282 	buf.Format(KRunError, aError);
       
   283 	iEngine->Printf(KRunError,TestName());
       
   284 	iEngine->TestCompleted(aError);
       
   285 	return KErrNone;
       
   286 	}
       
   287 
       
   288 //------------------------------------------------------------------------------
       
   289 const TDesC& CWapClientMtmFindTest::TestName()
       
   290 	{
       
   291 	_LIT(KTextWapClientMtmFindTest, "WapPushClientMtm Find Test");
       
   292 	return KTextWapClientMtmFindTest;
       
   293 	}
       
   294 
       
   295 //------------------------------------------------------------------------------
       
   296 CWapClientMtmFindTest::~CWapClientMtmFindTest()
       
   297 	{
       
   298 	delete iDummyObserver; 
       
   299 	delete iSession;
       
   300 	}