sl@0: /*
sl@0: * Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: #include <txtrich.h>
sl@0: #include <txtfmlyr.h>
sl@0: #include <s32mem.h>
sl@0: #include <s32file.h>
sl@0: #include <flddef.h>
sl@0: #include <fldbltin.h>
sl@0: #include "../incp/T_PMLPAR.H"
sl@0: #include "T_CONVRT.h"
sl@0: 
sl@0: #define test(cond)											\
sl@0: 	{														\
sl@0: 	TBool __bb = (cond);									\
sl@0: 	TEST(__bb);												\
sl@0: 	if (!__bb)												\
sl@0: 		{													\
sl@0: 		ERR_PRINTF1(_L("ERROR: Test Failed"));				\
sl@0: 		User::Leave(1);										\
sl@0: 		}													\
sl@0: 	}
sl@0: 
sl@0: #define UNUSED_VAR(a) a = a
sl@0: 
sl@0: const TInt KTestCleanupStack=0x20;
sl@0: 
sl@0: LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0: //
sl@0: LOCAL_D CRichText* TheText=NULL;
sl@0: LOCAL_D CParaFormatLayer* TheGlobalParaLayer=NULL;
sl@0: LOCAL_D CCharFormatLayer* TheGlobalCharLayer=NULL;
sl@0: 
sl@0: 
sl@0: ////////////////////////////////////////////////////////////////////////////////////////////
sl@0: class TTestFieldFactoryCONVRT : public MTextFieldFactory
sl@0: 	{
sl@0: public:
sl@0: 	// from MTextFieldFactory
sl@0: 	virtual CTextField* NewFieldL(TUid aFieldType); 
sl@0: 	// Creates a field of the type specified
sl@0: 	// Returns NULL if it does not recognise/support the field type
sl@0: 	};
sl@0: 
sl@0: CTextField* TTestFieldFactoryCONVRT::NewFieldL(TUid aFieldType)
sl@0: // Creates a field (in aHeader) of the type specified in aHeader
sl@0: // 
sl@0: 	{
sl@0: 	CTextField* field=NULL;
sl@0: 	if (aFieldType==KDateTimeFieldUid)
sl@0: 		field = (CTextField*)new(ELeave) CDateTimeField();
sl@0: 	return field;
sl@0: 	}
sl@0: /////////////////////////////////////////////////////////////////////////////////////////////
sl@0: 
sl@0: _LIT(KOutputFile, "c:\\etext\\t_convrt.tst");
sl@0: template <class T>
sl@0: void CT_CONVRT::testStoreRestoreL(T& aCopy,const T& aOriginal)
sl@0: // Test document persistance.
sl@0: //
sl@0:     {
sl@0: 	// set up the store
sl@0: 	RFs	theFs;
sl@0: 	theFs.Connect();
sl@0: 	//
sl@0: 	theFs.Delete(KOutputFile);
sl@0: 	theFs.MkDirAll(KOutputFile);
sl@0: 	CFileStore* theStore = CDirectFileStore::CreateL(theFs, KOutputFile, EFileRead | EFileWrite);
sl@0: 	CleanupStack::PushL(theStore);
sl@0: 	theStore->SetTypeL(KDirectFileStoreLayoutUid);
sl@0: 	//
sl@0: 	// store the original
sl@0: 	TStreamId id(0);
sl@0: 	TRAPD(ret,id=aOriginal.StoreL(*theStore));
sl@0: 		test(ret==KErrNone);
sl@0: 	//
sl@0: 	// restore into the copy
sl@0: 	TRAP(ret,aCopy.RestoreL(*theStore,id));
sl@0: 		test(ret==KErrNone);
sl@0: 	//
sl@0: 	// tidy up
sl@0: 	CleanupStack::PopAndDestroy();  // theStore
sl@0: 	theFs.Close();
sl@0:     }
sl@0: 
sl@0: 
sl@0: TInt CT_CONVRT::IsEqual(const CEditableText* aCopy,const CEditableText* aOriginal)
sl@0: //
sl@0: // Returns true if aCopy contents matches aOriginal contents.
sl@0: // Takes account of multiple segments of a segmented text component.
sl@0: //
sl@0: 	{
sl@0: 	TInt lengthOfOriginal=aOriginal->DocumentLength();
sl@0: 	TInt lengthOfCopy=aCopy->DocumentLength();
sl@0: 	test(lengthOfOriginal==lengthOfCopy);
sl@0: //
sl@0: 	TPtrC copy,orig;
sl@0: //
sl@0: 	TInt lengthRead=0;
sl@0: 	while(lengthRead<=lengthOfOriginal)
sl@0: 		{
sl@0: 		copy.Set((aCopy->Read(lengthRead)));
sl@0: 		orig.Set((aOriginal->Read(lengthRead)));
sl@0: 		for (TInt offset=0; offset<copy.Length(); offset++)
sl@0: 			test(copy[offset]==orig[offset]);
sl@0: 		lengthRead+=copy.Length();
sl@0: 		}
sl@0: 	test(lengthRead==lengthOfOriginal+1);
sl@0: 	return 1;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::DoTestRichTextL()
sl@0: //
sl@0: // Test streaming CRichText.
sl@0: //
sl@0: 	{// Create the global text components.
sl@0: 	const TInt KSmallestTextBuffer=1;
sl@0: 	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
sl@0: 	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
sl@0: 	//	
sl@0: 	CRichText* theCopy=NULL;
sl@0: 	//
sl@0: 	TInt biggest;
sl@0: 	TInt size=User::Available(biggest); 
sl@0: 	//
sl@0: 	TRAPD(ret,
sl@0: 	theCopy=CRichText::NewL(paraLayer,charLayer,CEditableText::ESegmentedStorage,KSmallestTextBuffer));
sl@0: 	//
sl@0: 	TInt newsize=User::Available(biggest);
sl@0: 	TInt footprint=size-newsize;
sl@0: 	//
sl@0: 	size=User::Available(biggest);
sl@0: 	CGlobalText* gText=NULL;
sl@0: 	TRAP(ret,
sl@0: 	gText=CGlobalText::NewL(paraLayer,charLayer,CEditableText::ESegmentedStorage,KSmallestTextBuffer));
sl@0: 	newsize=User::Available(biggest);
sl@0: 	TInt globalPrint=size-newsize;
sl@0: 	TBuf<50> buf;
sl@0: 	buf.Format(_L("Empty rich text takes: %d bytes\n"),footprint);
sl@0: 	INFO_PRINTF1(buf);
sl@0: 	buf.Format(_L("Empty global text takes: %d bytes\n"),globalPrint);
sl@0: 	INFO_PRINTF1(buf);
sl@0: //	test.Getch();
sl@0: 	delete gText;
sl@0: 	//
sl@0: 	// Now add a text field to this.
sl@0: 	TTestFieldFactoryCONVRT factory;
sl@0: 	TheText->SetFieldFactory(&factory);
sl@0: 	theCopy->SetFieldFactory(&factory);
sl@0: 	CTextField* field=NULL;
sl@0: 	TRAP(ret,
sl@0: 	field=factory.NewFieldL(KDateTimeFieldUid));
sl@0: 	test(ret==KErrNone);
sl@0: 	TRAP(ret,
sl@0: 	TheText->InsertFieldL(0,field,KDateTimeFieldUid));
sl@0: 	test(ret==KErrNone);
sl@0: 	TRAP(ret,
sl@0: 	TheText->UpdateFieldL(0));
sl@0: 	test(ret==KErrNone);
sl@0: 	//
sl@0: 	// Do the store/restore and test
sl@0: 	testStoreRestoreL(*theCopy,*TheText);
sl@0: 	test(IsEqual(theCopy,TheText));
sl@0: 	theCopy->Reset();  // lets me see inside the invariant;
sl@0: 
sl@0: 	delete theCopy;
sl@0: 	delete paraLayer;
sl@0: 	delete charLayer;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::LoadIntoText(TFileName& aFileName)
sl@0: //
sl@0: 	{
sl@0: 	CParser* myParser=NULL;
sl@0: 	TRAPD(ret,
sl@0: 	myParser=CParser::NewL());
sl@0: 	test(ret == KErrNone);
sl@0: 	TRAP(ret,
sl@0: 	TheText=myParser->ParseL(aFileName));
sl@0: 	test(ret == KErrNone);
sl@0: 	TheGlobalParaLayer=(CParaFormatLayer*)TheText->GlobalParaFormatLayer();
sl@0: 	TheGlobalCharLayer=(CCharFormatLayer*)TheText->GlobalCharFormatLayer();
sl@0: 	delete myParser;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::KillText()
sl@0: //
sl@0: 	{
sl@0: 	delete TheText;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::KillLayers()
sl@0: 	{
sl@0: 	delete TheGlobalParaLayer;
sl@0: 	delete TheGlobalCharLayer;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::Reset()
sl@0: //
sl@0: 	{
sl@0: 	KillText();
sl@0: 	KillLayers();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::GoL()
sl@0: //
sl@0: 	{
sl@0: 	INFO_PRINTF1(_L("Rich Text of Shared Para Formats Only"));
sl@0: 	TFileName fileName=_L("z:\\test\\app-framework\\etext\\shared.pml");
sl@0: 	LoadIntoText(fileName);
sl@0: 	TBool hasMarkupData=TheText->HasMarkupData();
sl@0: 	test(hasMarkupData);
sl@0: 	DoTestRichTextL();
sl@0: 	Reset();
sl@0: 	//
sl@0: 	INFO_PRINTF1(_L("Rich Text with specific character formatting"));
sl@0: 	fileName=_L("z:\\test\\app-framework\\etext\\test1.pml");
sl@0: 	LoadIntoText(fileName);
sl@0: 	hasMarkupData=TheText->HasMarkupData();
sl@0: 	test(hasMarkupData);
sl@0: 	DoTestRichTextL();
sl@0: 	Reset();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::setupCleanup()
sl@0: //
sl@0: // Initialise the cleanup stack.
sl@0: //
sl@0:     {
sl@0: 
sl@0: 	TheTrapCleanup=CTrapCleanup::New();
sl@0: 	TRAPD(r,\
sl@0: 		{\
sl@0: 		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0: 			CleanupStack::PushL((TAny*)1);\
sl@0: 		test(r==KErrNone);\
sl@0: 		CleanupStack::Pop(KTestCleanupStack);\
sl@0: 		});
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CT_CONVRT::DeleteDataFile(const TDesC& aFullName)
sl@0: 	{
sl@0: 	RFs fsSession;
sl@0: 	TInt err = fsSession.Connect();
sl@0: 	if(err == KErrNone)
sl@0: 		{
sl@0: 		TEntry entry;
sl@0: 		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0: 			{
sl@0: 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0: 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0: 			if(err != KErrNone) 
sl@0: 				{
sl@0: 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0: 				}
sl@0: 			err = fsSession.Delete(aFullName);
sl@0: 			if(err != KErrNone) 
sl@0: 				{
sl@0: 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0: 				}
sl@0: 			}
sl@0: 		fsSession.Close();
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: CT_CONVRT::CT_CONVRT()
sl@0:     {
sl@0:     SetTestStepName(KTestStep_T_CONVRT);
sl@0:     }
sl@0: 
sl@0: TVerdict CT_CONVRT::doTestStepL()
sl@0:     {
sl@0:     SetTestStepResult(EFail);
sl@0: 
sl@0:     setupCleanup();
sl@0:     __UHEAP_MARK;
sl@0: 
sl@0:     INFO_PRINTF1(_L("T_CONVRT - Rich Text Persistence"));
sl@0:     INFO_PRINTF1(_L("Persisting Rich Text"));
sl@0:     INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_CONVRT-0001 "));
sl@0:     TRAPD(error1, GoL());
sl@0: 
sl@0:     __UHEAP_MARKEND;
sl@0:     delete TheTrapCleanup;
sl@0: 
sl@0:     if(error1 == KErrNone)
sl@0:         {
sl@0:         SetTestStepResult(EPass);
sl@0:         }
sl@0: 
sl@0:     return TestStepResult();
sl@0:     }