os/textandloc/textrendering/texthandling/ttext/T_CONVRT.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <txtrich.h>
sl@0
    20
#include <txtfmlyr.h>
sl@0
    21
#include <s32mem.h>
sl@0
    22
#include <s32file.h>
sl@0
    23
#include <flddef.h>
sl@0
    24
#include <fldbltin.h>
sl@0
    25
#include "../incp/T_PMLPAR.H"
sl@0
    26
#include "T_CONVRT.h"
sl@0
    27
sl@0
    28
#define test(cond)											\
sl@0
    29
	{														\
sl@0
    30
	TBool __bb = (cond);									\
sl@0
    31
	TEST(__bb);												\
sl@0
    32
	if (!__bb)												\
sl@0
    33
		{													\
sl@0
    34
		ERR_PRINTF1(_L("ERROR: Test Failed"));				\
sl@0
    35
		User::Leave(1);										\
sl@0
    36
		}													\
sl@0
    37
	}
sl@0
    38
sl@0
    39
#define UNUSED_VAR(a) a = a
sl@0
    40
sl@0
    41
const TInt KTestCleanupStack=0x20;
sl@0
    42
sl@0
    43
LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0
    44
//
sl@0
    45
LOCAL_D CRichText* TheText=NULL;
sl@0
    46
LOCAL_D CParaFormatLayer* TheGlobalParaLayer=NULL;
sl@0
    47
LOCAL_D CCharFormatLayer* TheGlobalCharLayer=NULL;
sl@0
    48
sl@0
    49
sl@0
    50
////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    51
class TTestFieldFactoryCONVRT : public MTextFieldFactory
sl@0
    52
	{
sl@0
    53
public:
sl@0
    54
	// from MTextFieldFactory
sl@0
    55
	virtual CTextField* NewFieldL(TUid aFieldType); 
sl@0
    56
	// Creates a field of the type specified
sl@0
    57
	// Returns NULL if it does not recognise/support the field type
sl@0
    58
	};
sl@0
    59
sl@0
    60
CTextField* TTestFieldFactoryCONVRT::NewFieldL(TUid aFieldType)
sl@0
    61
// Creates a field (in aHeader) of the type specified in aHeader
sl@0
    62
// 
sl@0
    63
	{
sl@0
    64
	CTextField* field=NULL;
sl@0
    65
	if (aFieldType==KDateTimeFieldUid)
sl@0
    66
		field = (CTextField*)new(ELeave) CDateTimeField();
sl@0
    67
	return field;
sl@0
    68
	}
sl@0
    69
/////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    70
sl@0
    71
_LIT(KOutputFile, "c:\\etext\\t_convrt.tst");
sl@0
    72
template <class T>
sl@0
    73
void CT_CONVRT::testStoreRestoreL(T& aCopy,const T& aOriginal)
sl@0
    74
// Test document persistance.
sl@0
    75
//
sl@0
    76
    {
sl@0
    77
	// set up the store
sl@0
    78
	RFs	theFs;
sl@0
    79
	theFs.Connect();
sl@0
    80
	//
sl@0
    81
	theFs.Delete(KOutputFile);
sl@0
    82
	theFs.MkDirAll(KOutputFile);
sl@0
    83
	CFileStore* theStore = CDirectFileStore::CreateL(theFs, KOutputFile, EFileRead | EFileWrite);
sl@0
    84
	CleanupStack::PushL(theStore);
sl@0
    85
	theStore->SetTypeL(KDirectFileStoreLayoutUid);
sl@0
    86
	//
sl@0
    87
	// store the original
sl@0
    88
	TStreamId id(0);
sl@0
    89
	TRAPD(ret,id=aOriginal.StoreL(*theStore));
sl@0
    90
		test(ret==KErrNone);
sl@0
    91
	//
sl@0
    92
	// restore into the copy
sl@0
    93
	TRAP(ret,aCopy.RestoreL(*theStore,id));
sl@0
    94
		test(ret==KErrNone);
sl@0
    95
	//
sl@0
    96
	// tidy up
sl@0
    97
	CleanupStack::PopAndDestroy();  // theStore
sl@0
    98
	theFs.Close();
sl@0
    99
    }
sl@0
   100
sl@0
   101
sl@0
   102
TInt CT_CONVRT::IsEqual(const CEditableText* aCopy,const CEditableText* aOriginal)
sl@0
   103
//
sl@0
   104
// Returns true if aCopy contents matches aOriginal contents.
sl@0
   105
// Takes account of multiple segments of a segmented text component.
sl@0
   106
//
sl@0
   107
	{
sl@0
   108
	TInt lengthOfOriginal=aOriginal->DocumentLength();
sl@0
   109
	TInt lengthOfCopy=aCopy->DocumentLength();
sl@0
   110
	test(lengthOfOriginal==lengthOfCopy);
sl@0
   111
//
sl@0
   112
	TPtrC copy,orig;
sl@0
   113
//
sl@0
   114
	TInt lengthRead=0;
sl@0
   115
	while(lengthRead<=lengthOfOriginal)
sl@0
   116
		{
sl@0
   117
		copy.Set((aCopy->Read(lengthRead)));
sl@0
   118
		orig.Set((aOriginal->Read(lengthRead)));
sl@0
   119
		for (TInt offset=0; offset<copy.Length(); offset++)
sl@0
   120
			test(copy[offset]==orig[offset]);
sl@0
   121
		lengthRead+=copy.Length();
sl@0
   122
		}
sl@0
   123
	test(lengthRead==lengthOfOriginal+1);
sl@0
   124
	return 1;
sl@0
   125
	}
sl@0
   126
sl@0
   127
sl@0
   128
void CT_CONVRT::DoTestRichTextL()
sl@0
   129
//
sl@0
   130
// Test streaming CRichText.
sl@0
   131
//
sl@0
   132
	{// Create the global text components.
sl@0
   133
	const TInt KSmallestTextBuffer=1;
sl@0
   134
	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
sl@0
   135
	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
sl@0
   136
	//	
sl@0
   137
	CRichText* theCopy=NULL;
sl@0
   138
	//
sl@0
   139
	TInt biggest;
sl@0
   140
	TInt size=User::Available(biggest); 
sl@0
   141
	//
sl@0
   142
	TRAPD(ret,
sl@0
   143
	theCopy=CRichText::NewL(paraLayer,charLayer,CEditableText::ESegmentedStorage,KSmallestTextBuffer));
sl@0
   144
	//
sl@0
   145
	TInt newsize=User::Available(biggest);
sl@0
   146
	TInt footprint=size-newsize;
sl@0
   147
	//
sl@0
   148
	size=User::Available(biggest);
sl@0
   149
	CGlobalText* gText=NULL;
sl@0
   150
	TRAP(ret,
sl@0
   151
	gText=CGlobalText::NewL(paraLayer,charLayer,CEditableText::ESegmentedStorage,KSmallestTextBuffer));
sl@0
   152
	newsize=User::Available(biggest);
sl@0
   153
	TInt globalPrint=size-newsize;
sl@0
   154
	TBuf<50> buf;
sl@0
   155
	buf.Format(_L("Empty rich text takes: %d bytes\n"),footprint);
sl@0
   156
	INFO_PRINTF1(buf);
sl@0
   157
	buf.Format(_L("Empty global text takes: %d bytes\n"),globalPrint);
sl@0
   158
	INFO_PRINTF1(buf);
sl@0
   159
//	test.Getch();
sl@0
   160
	delete gText;
sl@0
   161
	//
sl@0
   162
	// Now add a text field to this.
sl@0
   163
	TTestFieldFactoryCONVRT factory;
sl@0
   164
	TheText->SetFieldFactory(&factory);
sl@0
   165
	theCopy->SetFieldFactory(&factory);
sl@0
   166
	CTextField* field=NULL;
sl@0
   167
	TRAP(ret,
sl@0
   168
	field=factory.NewFieldL(KDateTimeFieldUid));
sl@0
   169
	test(ret==KErrNone);
sl@0
   170
	TRAP(ret,
sl@0
   171
	TheText->InsertFieldL(0,field,KDateTimeFieldUid));
sl@0
   172
	test(ret==KErrNone);
sl@0
   173
	TRAP(ret,
sl@0
   174
	TheText->UpdateFieldL(0));
sl@0
   175
	test(ret==KErrNone);
sl@0
   176
	//
sl@0
   177
	// Do the store/restore and test
sl@0
   178
	testStoreRestoreL(*theCopy,*TheText);
sl@0
   179
	test(IsEqual(theCopy,TheText));
sl@0
   180
	theCopy->Reset();  // lets me see inside the invariant;
sl@0
   181
sl@0
   182
	delete theCopy;
sl@0
   183
	delete paraLayer;
sl@0
   184
	delete charLayer;
sl@0
   185
	}
sl@0
   186
sl@0
   187
sl@0
   188
void CT_CONVRT::LoadIntoText(TFileName& aFileName)
sl@0
   189
//
sl@0
   190
	{
sl@0
   191
	CParser* myParser=NULL;
sl@0
   192
	TRAPD(ret,
sl@0
   193
	myParser=CParser::NewL());
sl@0
   194
	test(ret == KErrNone);
sl@0
   195
	TRAP(ret,
sl@0
   196
	TheText=myParser->ParseL(aFileName));
sl@0
   197
	test(ret == KErrNone);
sl@0
   198
	TheGlobalParaLayer=(CParaFormatLayer*)TheText->GlobalParaFormatLayer();
sl@0
   199
	TheGlobalCharLayer=(CCharFormatLayer*)TheText->GlobalCharFormatLayer();
sl@0
   200
	delete myParser;
sl@0
   201
	}
sl@0
   202
sl@0
   203
sl@0
   204
void CT_CONVRT::KillText()
sl@0
   205
//
sl@0
   206
	{
sl@0
   207
	delete TheText;
sl@0
   208
	}
sl@0
   209
sl@0
   210
sl@0
   211
void CT_CONVRT::KillLayers()
sl@0
   212
	{
sl@0
   213
	delete TheGlobalParaLayer;
sl@0
   214
	delete TheGlobalCharLayer;
sl@0
   215
	}
sl@0
   216
sl@0
   217
sl@0
   218
void CT_CONVRT::Reset()
sl@0
   219
//
sl@0
   220
	{
sl@0
   221
	KillText();
sl@0
   222
	KillLayers();
sl@0
   223
	}
sl@0
   224
sl@0
   225
sl@0
   226
void CT_CONVRT::GoL()
sl@0
   227
//
sl@0
   228
	{
sl@0
   229
	INFO_PRINTF1(_L("Rich Text of Shared Para Formats Only"));
sl@0
   230
	TFileName fileName=_L("z:\\test\\app-framework\\etext\\shared.pml");
sl@0
   231
	LoadIntoText(fileName);
sl@0
   232
	TBool hasMarkupData=TheText->HasMarkupData();
sl@0
   233
	test(hasMarkupData);
sl@0
   234
	DoTestRichTextL();
sl@0
   235
	Reset();
sl@0
   236
	//
sl@0
   237
	INFO_PRINTF1(_L("Rich Text with specific character formatting"));
sl@0
   238
	fileName=_L("z:\\test\\app-framework\\etext\\test1.pml");
sl@0
   239
	LoadIntoText(fileName);
sl@0
   240
	hasMarkupData=TheText->HasMarkupData();
sl@0
   241
	test(hasMarkupData);
sl@0
   242
	DoTestRichTextL();
sl@0
   243
	Reset();
sl@0
   244
	}
sl@0
   245
sl@0
   246
sl@0
   247
void CT_CONVRT::setupCleanup()
sl@0
   248
//
sl@0
   249
// Initialise the cleanup stack.
sl@0
   250
//
sl@0
   251
    {
sl@0
   252
sl@0
   253
	TheTrapCleanup=CTrapCleanup::New();
sl@0
   254
	TRAPD(r,\
sl@0
   255
		{\
sl@0
   256
		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0
   257
			CleanupStack::PushL((TAny*)1);\
sl@0
   258
		test(r==KErrNone);\
sl@0
   259
		CleanupStack::Pop(KTestCleanupStack);\
sl@0
   260
		});
sl@0
   261
	}
sl@0
   262
sl@0
   263
sl@0
   264
void CT_CONVRT::DeleteDataFile(const TDesC& aFullName)
sl@0
   265
	{
sl@0
   266
	RFs fsSession;
sl@0
   267
	TInt err = fsSession.Connect();
sl@0
   268
	if(err == KErrNone)
sl@0
   269
		{
sl@0
   270
		TEntry entry;
sl@0
   271
		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0
   272
			{
sl@0
   273
			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0
   274
			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0
   275
			if(err != KErrNone) 
sl@0
   276
				{
sl@0
   277
				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0
   278
				}
sl@0
   279
			err = fsSession.Delete(aFullName);
sl@0
   280
			if(err != KErrNone) 
sl@0
   281
				{
sl@0
   282
				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0
   283
				}
sl@0
   284
			}
sl@0
   285
		fsSession.Close();
sl@0
   286
		}
sl@0
   287
	else
sl@0
   288
		{
sl@0
   289
		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0
   290
		}
sl@0
   291
	}
sl@0
   292
sl@0
   293
CT_CONVRT::CT_CONVRT()
sl@0
   294
    {
sl@0
   295
    SetTestStepName(KTestStep_T_CONVRT);
sl@0
   296
    }
sl@0
   297
sl@0
   298
TVerdict CT_CONVRT::doTestStepL()
sl@0
   299
    {
sl@0
   300
    SetTestStepResult(EFail);
sl@0
   301
sl@0
   302
    setupCleanup();
sl@0
   303
    __UHEAP_MARK;
sl@0
   304
sl@0
   305
    INFO_PRINTF1(_L("T_CONVRT - Rich Text Persistence"));
sl@0
   306
    INFO_PRINTF1(_L("Persisting Rich Text"));
sl@0
   307
    INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_CONVRT-0001 "));
sl@0
   308
    TRAPD(error1, GoL());
sl@0
   309
sl@0
   310
    __UHEAP_MARKEND;
sl@0
   311
    delete TheTrapCleanup;
sl@0
   312
sl@0
   313
    if(error1 == KErrNone)
sl@0
   314
        {
sl@0
   315
        SetTestStepResult(EPass);
sl@0
   316
        }
sl@0
   317
sl@0
   318
    return TestStepResult();
sl@0
   319
    }