1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/textandloc/textrendering/texthandling/ttext/T_CONVS1.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,290 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <txtglobl.h>
1.23 +#include <txtfmlyr.h>
1.24 +#include <s32mem.h>
1.25 +#include <s32file.h>
1.26 +#include <flddef.h>
1.27 +#include <fldbltin.h>
1.28 +#include "T_CONVS1.h"
1.29 +
1.30 +#define test(cond) \
1.31 + { \
1.32 + TBool __bb = (cond); \
1.33 + TEST(__bb); \
1.34 + if (!__bb) \
1.35 + { \
1.36 + ERR_PRINTF1(_L("ERROR: Test Failed")); \
1.37 + User::Leave(1); \
1.38 + } \
1.39 + }
1.40 +
1.41 +const TInt KTestCleanupStack=0x20;
1.42 +
1.43 +LOCAL_D CTrapCleanup* TheTrapCleanup;
1.44 +
1.45 +LOCAL_D TPtrC bigBuf(_L("This is a very big buffer indeed, containing text and special characters,\
1.46 + big enough to fill a segment of an editable text component that employs segmented storage"));
1.47 +
1.48 +////////////////////////////////////////////////////////////////////////////////////////////
1.49 +class TTestFieldFactoryCONVS1 : public MTextFieldFactory
1.50 + {
1.51 +public:
1.52 + // from MTextFieldFactory
1.53 + virtual CTextField* NewFieldL(TUid aFieldType);
1.54 + // Creates a field of the type specified
1.55 + // Returns NULL if it does not recognise/support the field type
1.56 + };
1.57 +
1.58 +CTextField* TTestFieldFactoryCONVS1::NewFieldL(TUid aFieldType)
1.59 +// Creates a field (in aHeader) of the type specified in aHeader
1.60 +//
1.61 + {
1.62 + CTextField* field=NULL;
1.63 + if (aFieldType==KDateTimeFieldUid)
1.64 + field = (CTextField*)new(ELeave) CDateTimeField();
1.65 + return field;
1.66 + }
1.67 +/////////////////////////////////////////////////////////////////////////////////////////////
1.68 +
1.69 +_LIT(KOutputFile, "c:\\etext\\t_convs1.tst");
1.70 +template <class T>
1.71 +void CT_CONVS1::testStoreRestoreL(T& aCopy,const T& aOriginal)
1.72 +// Test document persistance.
1.73 +//
1.74 + {
1.75 + // set up the store
1.76 + RFs theFs;
1.77 + theFs.Connect();
1.78 + //
1.79 + theFs.Delete(KOutputFile);
1.80 + theFs.MkDirAll(KOutputFile);
1.81 + CFileStore* theStore=CDirectFileStore::CreateL(theFs,KOutputFile,EFileRead|EFileWrite);
1.82 + CleanupStack::PushL(theStore);
1.83 + theStore->SetTypeL(KDirectFileStoreLayoutUid);
1.84 + //
1.85 + // store the original
1.86 + TStreamId id(0);
1.87 + TRAPD(ret,id=aOriginal.StoreL(*theStore));
1.88 + test(ret==KErrNone);
1.89 + //
1.90 + // restore into the copy
1.91 + TRAP(ret,aCopy.RestoreL(*theStore,id));
1.92 + test(ret==KErrNone);
1.93 + //
1.94 + // tidy up
1.95 + CleanupStack::PopAndDestroy(); // theStore
1.96 + theFs.Close();
1.97 + }
1.98 +
1.99 +TInt CT_CONVS1::IsEqual(const CPlainText* aCopy,const CPlainText* aOriginal)
1.100 +//
1.101 +// Tests for equality of plain text components.
1.102 +// Takes account of multiple segments of a segmented text component.
1.103 +//
1.104 + {
1.105 + TInt lengthOfOriginal=aOriginal->DocumentLength();
1.106 + TInt lengthOfCopy=aCopy->DocumentLength();
1.107 + test(lengthOfOriginal==lengthOfCopy);
1.108 +//
1.109 + TPtrC copy,orig;
1.110 +//
1.111 + TInt lengthRead=0;
1.112 + while(lengthRead<=lengthOfOriginal)
1.113 + {
1.114 + copy.Set((aCopy->Read(lengthRead)));
1.115 + orig.Set((aOriginal->Read(lengthRead)));
1.116 + for (TInt offset=0; offset<orig.Length(); offset++)
1.117 + test(copy[offset]==orig[offset]);
1.118 + lengthRead+=orig.Length();
1.119 + }
1.120 + test(lengthRead==lengthOfOriginal+1);
1.121 + INFO_PRINTF1(_L("Restored plain text component matches original"));
1.122 + TInt copyFieldCount=aCopy->FieldCount();
1.123 + TInt origFieldCount=aOriginal->FieldCount();
1.124 + test(copyFieldCount==origFieldCount);
1.125 + return 1;
1.126 + }
1.127 +
1.128 +
1.129 +TInt CT_CONVS1::DocsEqual(const CGlobalText* aCopy,const CGlobalText* aOrig)
1.130 +//
1.131 +//
1.132 +//
1.133 + {
1.134 + test(IsEqual(aCopy,aOrig));
1.135 + return 1;
1.136 + }
1.137 +
1.138 +
1.139 +void CT_CONVS1::GenerateGlobalTextL()
1.140 +//
1.141 +// Create a global text documnet.
1.142 +//
1.143 + {
1.144 +// Set up the character format layer.
1.145 + CCharFormatLayer* cl1=CCharFormatLayer::NewL();
1.146 + TCharFormat charFormat; TCharFormatMask charMask;
1.147 + charMask.ClearAll();
1.148 + charFormat.iFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); charMask.SetAttrib(EAttFontStrokeWeight);
1.149 + charFormat.iFontSpec.iFontStyle.SetPosture(EPostureItalic); charMask.SetAttrib(EAttFontPosture);
1.150 + charFormat.iFontPresentation.iUnderline=EUnderlineOn; charMask.SetAttrib(EAttFontUnderline);
1.151 + cl1->SetL(charFormat,charMask);
1.152 + charMask.ClearAll();
1.153 +// Set up the paragraph format layer.
1.154 + CParaFormatLayer* l1=CParaFormatLayer::NewL();
1.155 + CParaFormat* paraFormat=CParaFormat::NewL(); TParaFormatMask paraMask;
1.156 + paraMask.ClearAll();
1.157 + TTabStop tab1,tab2;
1.158 + tab1.iTwipsPosition=5000; tab2.iTwipsPosition=5001;
1.159 + tab1.iType=TTabStop::ERightTab; tab2.iType=TTabStop::ECenteredTab;
1.160 + paraFormat->StoreTabL(tab1);
1.161 + paraFormat->StoreTabL(tab2);
1.162 + paraMask.SetAttrib(EAttTabStop);
1.163 + paraFormat->iLeftMarginInTwips=666; paraMask.SetAttrib(EAttLeftMargin);
1.164 + l1->SetL(paraFormat,paraMask);
1.165 + paraMask.ClearAll();
1.166 +// Now create the global text component.
1.167 + CGlobalText* globalDoc=CGlobalText::NewL(l1,cl1,CEditableText::ESegmentedStorage);
1.168 + globalDoc->InsertL(0,bigBuf);
1.169 +// Now create the global text that will be the restored one.
1.170 + CCharFormatLayer* cr1=CCharFormatLayer::NewL();
1.171 + TCharFormat ff;TCharFormatMask mm;cr1->SetL(ff,mm);
1.172 + CParaFormatLayer* r1=CParaFormatLayer::NewL();
1.173 + TParaFormatMask nn; r1->SetL((CParaFormat*)NULL,nn);
1.174 + CGlobalText* restoredDoc=CGlobalText::NewL(r1,cr1,CEditableText::ESegmentedStorage);
1.175 +// Store a text field in the global text.
1.176 + TTestFieldFactoryCONVS1 factory;
1.177 + globalDoc->SetFieldFactory(&factory);
1.178 + restoredDoc->SetFieldFactory(&factory);
1.179 + CTextField* field=NULL;
1.180 + TRAPD(ret,
1.181 + field=factory.NewFieldL(KDateTimeFieldUid));
1.182 + test(ret==KErrNone);
1.183 + TRAP(ret,
1.184 + globalDoc->InsertFieldL(0,field,KDateTimeFieldUid));
1.185 + test(ret==KErrNone);
1.186 +// And do the streaming/restore.
1.187 + INFO_PRINTF1(_L("Storing global text with field record"));
1.188 + INFO_PRINTF1(_L("Restoring global text"));
1.189 + testStoreRestoreL(*restoredDoc,*globalDoc);
1.190 + test(DocsEqual(restoredDoc,globalDoc));
1.191 +// Now clean up.
1.192 + TInt restoredCharChain=cr1->ChainCount();
1.193 + TInt restoredParaChain=r1->ChainCount();
1.194 + CCharFormatLayer* chCurrent=cr1;
1.195 + CCharFormatLayer* chNext=(CCharFormatLayer*)cr1->SenseBase();
1.196 + delete chCurrent;
1.197 + for (TInt loop=0;loop<restoredCharChain-1;loop++)
1.198 + {
1.199 + chCurrent=chNext;
1.200 + chNext=(CCharFormatLayer*)chCurrent->SenseBase();
1.201 + delete chCurrent;
1.202 + }
1.203 + CParaFormatLayer* paCurrent=r1;
1.204 + CParaFormatLayer* paNext=(CParaFormatLayer*)r1->SenseBase();
1.205 + delete paCurrent ;
1.206 + for (TInt ploop=0;ploop<restoredParaChain-1;ploop++)
1.207 + {
1.208 + paCurrent=paNext;
1.209 + paNext=(CParaFormatLayer*)paCurrent->SenseBase();
1.210 + delete paCurrent;
1.211 + }
1.212 + delete l1;
1.213 + delete cl1;
1.214 + delete paraFormat;
1.215 + delete restoredDoc;
1.216 + delete globalDoc;
1.217 + }
1.218 +
1.219 +
1.220 +void CT_CONVS1::setupCleanup()
1.221 +//
1.222 +// Initialise the cleanup stack.
1.223 +//
1.224 + {
1.225 +
1.226 + TheTrapCleanup=CTrapCleanup::New();
1.227 + TRAPD(r,\
1.228 + {\
1.229 + for (TInt i=KTestCleanupStack;i>0;i--)\
1.230 + CleanupStack::PushL((TAny*)1);\
1.231 + test(r==KErrNone);\
1.232 + CleanupStack::Pop(KTestCleanupStack);\
1.233 + });
1.234 + }
1.235 +
1.236 +
1.237 +void CT_CONVS1::DeleteDataFile(const TDesC& aFullName)
1.238 + {
1.239 + RFs fsSession;
1.240 + TInt err = fsSession.Connect();
1.241 + if(err == KErrNone)
1.242 + {
1.243 + TEntry entry;
1.244 + if(fsSession.Entry(aFullName, entry) == KErrNone)
1.245 + {
1.246 + RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
1.247 + err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
1.248 + if(err != KErrNone)
1.249 + {
1.250 + RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
1.251 + }
1.252 + err = fsSession.Delete(aFullName);
1.253 + if(err != KErrNone)
1.254 + {
1.255 + RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
1.256 + }
1.257 + }
1.258 + fsSession.Close();
1.259 + }
1.260 + else
1.261 + {
1.262 + RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
1.263 + }
1.264 + }
1.265 +
1.266 +CT_CONVS1::CT_CONVS1()
1.267 + {
1.268 + SetTestStepName(KTestStep_T_CONVS1);
1.269 + }
1.270 +
1.271 +TVerdict CT_CONVS1::doTestStepL()
1.272 + {
1.273 + SetTestStepResult(EFail);
1.274 +
1.275 + setupCleanup();
1.276 + __UHEAP_MARK;
1.277 +
1.278 + INFO_PRINTF1(_L("T_CONVS1 - GlobalText Persistence"));
1.279 + INFO_PRINTF1(_L("Generate global text"));
1.280 + INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_CONVS1-0001 "));
1.281 + TRAPD(error1, GenerateGlobalTextL());
1.282 +
1.283 + __UHEAP_MARKEND;
1.284 + DeleteDataFile(KOutputFile); //deletion of data files must be before call to End() - DEF047652
1.285 + delete TheTrapCleanup;
1.286 +
1.287 + if(error1 == KErrNone)
1.288 + {
1.289 + SetTestStepResult(EPass);
1.290 + }
1.291 +
1.292 + return TestStepResult();
1.293 + }