os/textandloc/textrendering/texthandling/ttext/T_IMPORT.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/textrendering/texthandling/ttext/T_IMPORT.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,449 @@
     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 <e32std.h>
    1.23 +#include <e32base.h>
    1.24 +#include <e32svr.h>
    1.25 +
    1.26 +#include <txtetext.h>
    1.27 +#include <txtfrmat.h>
    1.28 +#include <txtfmlyr.h>
    1.29 +#include <txtrich.h>
    1.30 +#include <txtglobl.h>
    1.31 +#include <gdi.h>
    1.32 +#include <s32mem.h>
    1.33 +#include <charconv.h>
    1.34 +#include "T_IMPORT.h"
    1.35 +
    1.36 +LOCAL_D CTestStep *pTestStep = NULL;
    1.37 +#define test(cond)											\
    1.38 +	{														\
    1.39 +	TBool __bb = (cond);									\
    1.40 +	pTestStep->TEST(__bb);									\
    1.41 +	if (!__bb)												\
    1.42 +		{													\
    1.43 +		pTestStep->ERR_PRINTF1(_L("ERROR: Test Failed"));	\
    1.44 +		User::Leave(1);										\
    1.45 +		}													\
    1.46 +	}
    1.47 +#undef INFO_PRINTF1
    1.48 +#undef INFO_PRINTF2
    1.49 +// copy from tefexportconst.h
    1.50 +#define INFO_PRINTF1(p1)        pTestStep->Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrInfo, (p1))
    1.51 +#define INFO_PRINTF2(p1, p2)    pTestStep->Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrInfo, (p1), (p2))
    1.52 +
    1.53 +/* this fixes a MSVC link warning */
    1.54 +#ifdef __VC32__
    1.55 +#pragma comment (linker, "/opt:noref") 
    1.56 +#endif
    1.57 +
    1.58 +#define UNUSED_VAR(a) a = a
    1.59 +
    1.60 +const TInt KTestCleanupStack=0x40;
    1.61 +
    1.62 +_LIT(KTestFileName1, "c:\\etext\\climb.txg");
    1.63 +_LIT(KTestFileName2, "c:\\etext\\import1.txg");
    1.64 +
    1.65 +LOCAL_D void EnsureFileExists(const TDesC& aName)
    1.66 +	{
    1.67 +	RFs fs;
    1.68 +	fs.Connect();
    1.69 +	fs.MkDirAll(aName);
    1.70 +	RFile file;
    1.71 +	file.Create(fs, aName, EFileRead|EFileWrite);
    1.72 +	file.Close();
    1.73 +	fs.Close();
    1.74 +	}
    1.75 +
    1.76 +LOCAL_D CTrapCleanup* TheTrapCleanup=NULL;
    1.77 +LOCAL_D CPlainText* TheText=NULL;
    1.78 +LOCAL_D CRichText* richText=NULL;
    1.79 +LOCAL_D CParaFormatLayer* TheGlobalParaLayer=NULL;
    1.80 +LOCAL_D CCharFormatLayer* TheGlobalCharLayer=NULL;
    1.81 +
    1.82 +
    1.83 +
    1.84 +LOCAL_C void CreateTextL()
    1.85 +//
    1.86 +	{
    1.87 +	TheText=CPlainText::NewL();
    1.88 +//
    1.89 +	TCharFormat charFormat;
    1.90 +	TCharFormatMask charMask;
    1.91 +	TheGlobalCharLayer=CCharFormatLayer::NewL(charFormat,charMask);
    1.92 +	TParaFormatMask paraMask;
    1.93 +	TheGlobalParaLayer=CParaFormatLayer::NewL((CParaFormat*)NULL,paraMask);
    1.94 +	richText=CRichText::NewL(TheGlobalParaLayer,TheGlobalCharLayer);
    1.95 +	}
    1.96 +
    1.97 +
    1.98 +LOCAL_C void DestroyText()
    1.99 +//
   1.100 +	{
   1.101 +	delete TheText;
   1.102 +	delete TheGlobalParaLayer;
   1.103 +	delete TheGlobalCharLayer;
   1.104 +	}
   1.105 +
   1.106 +
   1.107 +LOCAL_C void ResetTextL()
   1.108 +// Initialise the rich text.
   1.109 +//
   1.110 +	{TheText->Reset();}
   1.111 +
   1.112 +
   1.113 +
   1.114 +LOCAL_C void ImportText1()
   1.115 +// 
   1.116 +	{
   1.117 +	TFileName file=_L("z:\\test\\app-framework\\etext\\import1.txt");
   1.118 +	ResetTextL();
   1.119 +	TInt count=TheText->ImportTextFileL(0,file,CPlainText::EOrganiseByParagraph);
   1.120 +	test(TheText->DocumentLength()==51);
   1.121 +	test(TheText->ParagraphCount()==10);
   1.122 +	test(count==51);
   1.123 +//	Now export
   1.124 +	TFileName exportFile=KTestFileName2();
   1.125 +	EnsureFileExists(exportFile);
   1.126 +	TheText->ExportAsTextL(exportFile,CPlainText::EOrganiseByParagraph,0);
   1.127 +	ResetTextL();
   1.128 +//	and import this export ?!!
   1.129 +	count=TheText->ImportTextFileL(0,exportFile,CPlainText::EOrganiseByLine);
   1.130 +	TInt documentLength=TheText->DocumentLength();
   1.131 +	test(documentLength==49);
   1.132 +	TInt paraCount=TheText->ParagraphCount();
   1.133 +	test(paraCount==3);
   1.134 +	test(count==49);
   1.135 +	ResetTextL();
   1.136 +	}
   1.137 +
   1.138 +
   1.139 +LOCAL_C void ImportText2()
   1.140 +// A very long text file of a realistic document nature!
   1.141 +//
   1.142 +	{
   1.143 +	TFileName file=_L("z:\\test\\app-framework\\etext\\climb.txt");
   1.144 +	ResetTextL();
   1.145 +	TInt count=TheText->ImportTextFileL(0,file,CPlainText::EOrganiseByParagraph);
   1.146 +	TInt documentLength=TheText->DocumentLength();
   1.147 +	test(documentLength==18396);
   1.148 +	TInt paraCount=TheText->ParagraphCount();
   1.149 +	test(paraCount==158);
   1.150 +	TInt wordCount=TheText->WordCount();
   1.151 +	test(wordCount=3450);
   1.152 +//	Now export
   1.153 +	TFileName exportFile=KTestFileName1();
   1.154 +	//
   1.155 +	//
   1.156 +	EnsureFileExists(exportFile);
   1.157 +	TRAPD(ret,
   1.158 +		TheText->ExportAsTextL(exportFile,CPlainText::EOrganiseByParagraph,0));
   1.159 +	test(ret==KErrNone);
   1.160 +	//
   1.161 +	ResetTextL();
   1.162 +//	and import this export ?!!??!
   1.163 +	count=TheText->ImportTextFileL(0,exportFile,CPlainText::EOrganiseByLine);
   1.164 +	documentLength=TheText->DocumentLength();
   1.165 +	paraCount=TheText->ParagraphCount();
   1.166 +	wordCount=TheText->WordCount();
   1.167 +	test(documentLength==18362);
   1.168 +	test(paraCount==34);
   1.169 +	test(wordCount=3450);
   1.170 +	ResetTextL();
   1.171 +	}
   1.172 +
   1.173 +	
   1.174 +/** 
   1.175 + Test code for INC044582 - [MX0416] Document - Document application crashed when converting file.
   1.176 + This occurs when more than 256 styles are used. The fix to this defect is to Leave with
   1.177 + KErrNotSupported if there are more than 256 styles
   1.178 +*/ 
   1.179 +LOCAL_C void TestInc044582()
   1.180 +
   1.181 +	{
   1.182 +	__UHEAP_MARK;
   1.183 +	
   1.184 +	//	create an empty style list
   1.185 +	CStyleList* list=CStyleList::NewL();
   1.186 +
   1.187 +	// create formats and add to cleanup stack
   1.188 +	CParaFormatLayer* GlobalParaFormatLayer=CParaFormatLayer::NewL();
   1.189 +	CleanupStack::PushL(GlobalParaFormatLayer);
   1.190 +	CCharFormatLayer* GlobalCharFormatLayer=CCharFormatLayer::NewL();
   1.191 +	CleanupStack::PushL(GlobalCharFormatLayer);
   1.192 +	
   1.193 +	// create RichText object and supply empty Style list
   1.194 +	CRichText* richText1=CRichText::NewL(GlobalParaFormatLayer,GlobalCharFormatLayer,*list);
   1.195 +
   1.196 +	// Load the RichText object with 300 paragraphs
   1.197 +	_LIT(para, "A line of text\x2029");
   1.198 +	for (TInt Z=0; Z<300; Z++)
   1.199 +		{
   1.200 +		// a line of text foloowed by the paragraph separator	
   1.201 +		richText1->InsertL(0,para);
   1.202 +		}
   1.203 +	
   1.204 +	// create a formats for the style
   1.205 +	CParaFormatLayer* paraLayer=CParaFormatLayer::NewL();
   1.206 +	CleanupStack::PushL(paraLayer);
   1.207 +	CCharFormatLayer* charLayer=CCharFormatLayer::NewL();
   1.208 +	CleanupStack::PushL(charLayer);
   1.209 +	
   1.210 +	// Set the richtext object to own the style list
   1.211 +	richText1->SetStyleListExternallyOwned( EFalse );
   1.212 +	
   1.213 +	// add more than 256 styles
   1.214 +	TInt StartOfNextPara=0;
   1.215 +	TInt Length=0;
   1.216 +	for ( TInt i= 0 ; i< 300; i++ )
   1.217 +		{
   1.218 +		// create a new style and add to the Style list
   1.219 +		CParagraphStyle*  paraStyle=CParagraphStyle::NewL(*paraLayer,*charLayer);
   1.220 +		RParagraphStyleInfo info(paraStyle);
   1.221 +		TRAPD(ret, list->AppendL(&info));
   1.222 +        UNUSED_VAR(ret);
   1.223 +
   1.224 +		// find the next paragraph and add a style to it	
   1.225 +		StartOfNextPara = richText1->CharPosOfParagraph(Length, i);
   1.226 +		richText1->ApplyParagraphStyleL(*paraStyle,StartOfNextPara,1,
   1.227 +			CParagraphStyle::ERetainAllSpecificFormats);
   1.228 +		}
   1.229 +	
   1.230 +	// Compress it to a buffer.
   1.231 +	CBufFlat* buffer = CBufFlat::NewL(500000);
   1.232 +	CleanupStack::PushL(buffer);
   1.233 +	RBufWriteStream output_stream(*buffer);
   1.234 +	
   1.235 +	// Attempt to "externalize" the RichText object which has more then 256 styles
   1.236 +	// This caused the problem to occur and should leave "with KErrNotSupported"
   1.237 +	TRAPD( error, richText1->ExternalizeMarkupDataL(output_stream));
   1.238 +	test( error == KErrNotSupported);
   1.239 +	
   1.240 +	output_stream.Close();
   1.241 +	delete richText1;
   1.242 +	CleanupStack::PopAndDestroy(5); // GlobalParaFormatLayer, GlobalCharFormatLayer, paraLayer, 
   1.243 +									// charLayer, buffer
   1.244 +	__UHEAP_MARKEND;
   1.245 +	}
   1.246 +	
   1.247 +static void TestUnicodeCompressionL()
   1.248 +	{
   1.249 +	CPlainText* plain1 = CPlainText::NewL();
   1.250 +	CleanupStack::PushL(plain1);
   1.251 +	CPlainText* plain2 = CPlainText::NewL();
   1.252 +	CleanupStack::PushL(plain2);
   1.253 +
   1.254 +	// Read complicated Unicode text from a file.
   1.255 +	TFileName raw_file_name = _L("z:\\test\\app-framework\\etext\\polyglot.txt");
   1.256 +	plain1->ImportTextFileL(0,raw_file_name,CPlainText::EOrganiseByParagraph);
   1.257 +	
   1.258 +	// Compress it to a buffer.
   1.259 +	CBufFlat* buffer = CBufFlat::NewL(1024);
   1.260 +	CleanupStack::PushL(buffer);
   1.261 +	RBufWriteStream output_stream(*buffer);
   1.262 +	plain1->ExternalizePlainTextL(output_stream);
   1.263 +	output_stream.Close();
   1.264 +
   1.265 +	// Decompress it to a different plain text object.
   1.266 +	RBufReadStream input_stream(*buffer);
   1.267 +	plain2->InternalizePlainTextL(input_stream);
   1.268 +	input_stream.Close();
   1.269 +
   1.270 +	// Test for equality.
   1.271 +	int length = plain1->DocumentLength();
   1.272 +	test(length == plain2->DocumentLength());
   1.273 +	int pos = 0;
   1.274 +	do
   1.275 +		{
   1.276 +		TPtrC p1 = plain1->Read(pos);
   1.277 +		TPtrC p2 = plain2->Read(pos);
   1.278 +		test(p1 == p2);
   1.279 +		pos += p1.Length();
   1.280 +		} while (pos < length);
   1.281 +
   1.282 +	CleanupStack::PopAndDestroy(3); // plain1, plain2, buffer
   1.283 +	}
   1.284 +
   1.285 +static void TestEncodingConversionL()
   1.286 +	{
   1.287 +	CPlainText* plain1 = CPlainText::NewL();
   1.288 +	CleanupStack::PushL(plain1);
   1.289 +	CPlainText* plain2 = CPlainText::NewL();
   1.290 +	CleanupStack::PushL(plain2);
   1.291 +	CBufSeg* buffer = CBufSeg::NewL(1024);
   1.292 +	CleanupStack::PushL(buffer);
   1.293 +
   1.294 +	// Read multilingual text from a file.
   1.295 +	TFileName file_name = _L("z:\\test\\app-framework\\etext\\polyglot.txt");
   1.296 +	plain1->ImportTextFileL(0,file_name,CPlainText::EOrganiseByParagraph);
   1.297 +
   1.298 +	// Convert to UTF8.
   1.299 +	RBufWriteStream output(*buffer,0);
   1.300 +	CPlainText::TImportExportParam param;
   1.301 +	param.iForeignEncoding = KCharacterSetIdentifierUtf8;
   1.302 +	CPlainText::TImportExportResult result;
   1.303 +	plain1->ExportTextL(0,output,param,result);
   1.304 +	output.Close();
   1.305 +
   1.306 +	// Read it into another plain text object.
   1.307 +	RBufReadStream input(*buffer,0);
   1.308 +	plain2->ImportTextL(0,input,param,result);
   1.309 +
   1.310 +	// Test for equality.
   1.311 +	int length1 = plain1->DocumentLength();
   1.312 +	TText* text1 = new(ELeave) TText[length1];
   1.313 +	TPtr ptr1(text1,length1);
   1.314 +	plain1->Extract(ptr1);
   1.315 +	int length2 = plain2->DocumentLength();
   1.316 +	TText* text2 = new(ELeave) TText[length2];
   1.317 +	TPtr ptr2(text2,length2);
   1.318 +	plain2->Extract(ptr2);
   1.319 +	test(length1 == length2);
   1.320 +	delete [] text1;
   1.321 +	delete [] text2;
   1.322 +	int pos = 0;
   1.323 +	do
   1.324 +		{
   1.325 +		TPtrC p1 = plain1->Read(pos);
   1.326 +		TPtrC p2 = plain2->Read(pos);
   1.327 +		test(p1 == p2);
   1.328 +		pos += p1.Length();
   1.329 +		} while (pos < length1);
   1.330 +
   1.331 +    INFO_PRINTF1(_L("DEF058651 - Propagated:NTT - Received MMS with line break CR set does not cause line feed."));
   1.332 +    buffer->Reset();
   1.333 +    plain1->Reset();
   1.334 +    
   1.335 +    TBuf8<100>  testString(_L8("This Is Test  Message"));
   1.336 +    testString[4] = 0x0D;
   1.337 +    testString[7] = 0x0A;
   1.338 +    testString[12] = 0x0D;
   1.339 +    testString[13] = 0x0A;
   1.340 +    buffer->InsertL(0,testString.Ptr(),testString.Length());
   1.341 +    
   1.342 +    RBufReadStream insert(*buffer,0);
   1.343 +    plain1->ImportTextL(0,insert,param,result);
   1.344 +    insert.Close();
   1.345 +
   1.346 +	TBuf<100> str;
   1.347 +	plain1->Extract(str);
   1.348 +    test(str[4] ==CEditableText::EParagraphDelimiter);
   1.349 +	CleanupStack::PopAndDestroy(3); // plain1, plain2, buffer
   1.350 +	}
   1.351 +
   1.352 +LOCAL_C void DoTestL()
   1.353 +// Main routine
   1.354 +//
   1.355 +    {
   1.356 +	INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-ETEXT-LEGACY-T_IMPORT-0001 CPlainText "));
   1.357 +	CreateTextL();
   1.358 +
   1.359 +	ImportText1();
   1.360 +	ImportText2();
   1.361 +
   1.362 +	TestUnicodeCompressionL();
   1.363 +	TestEncodingConversionL();
   1.364 +
   1.365 +	INFO_PRINTF1(_L("CRichText"));
   1.366 +	delete TheText;
   1.367 +	TheText=richText;
   1.368 +
   1.369 +	ImportText1();
   1.370 +	ImportText2();
   1.371 +
   1.372 +	INFO_PRINTF1(_L("CRichText - more than 256 styles, defect INC044582"));
   1.373 +	TestInc044582();
   1.374 +	
   1.375 +	DestroyText();
   1.376 +    }
   1.377 +
   1.378 +
   1.379 +LOCAL_C void setupCleanup()
   1.380 +// Initialise the cleanup stack.
   1.381 +//
   1.382 +    {
   1.383 +
   1.384 +	TheTrapCleanup=CTrapCleanup::New();
   1.385 +	TRAPD(r,\
   1.386 +		{\
   1.387 +		for (TInt i=KTestCleanupStack;i>0;i--)\
   1.388 +			CleanupStack::PushL((TAny*)1);\
   1.389 +		test(r==KErrNone);\
   1.390 +		CleanupStack::Pop(KTestCleanupStack);\
   1.391 +		});
   1.392 +	}
   1.393 +
   1.394 +
   1.395 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   1.396 +	{
   1.397 +	RFs fsSession;
   1.398 +	TInt err = fsSession.Connect();
   1.399 +	if(err == KErrNone)
   1.400 +		{
   1.401 +		TEntry entry;
   1.402 +		if(fsSession.Entry(aFullName, entry) == KErrNone)
   1.403 +			{
   1.404 +			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   1.405 +			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   1.406 +			if(err != KErrNone) 
   1.407 +				{
   1.408 +				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   1.409 +				}
   1.410 +			err = fsSession.Delete(aFullName);
   1.411 +			if(err != KErrNone) 
   1.412 +				{
   1.413 +				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   1.414 +				}
   1.415 +			}
   1.416 +		fsSession.Close();
   1.417 +		}
   1.418 +	else
   1.419 +		{
   1.420 +		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   1.421 +		}
   1.422 +	}
   1.423 +
   1.424 +CT_IMPORT::CT_IMPORT()
   1.425 +    {
   1.426 +    SetTestStepName(KTestStep_T_IMPORT);
   1.427 +    pTestStep = this;
   1.428 +    }
   1.429 +
   1.430 +TVerdict CT_IMPORT::doTestStepL()
   1.431 +    {
   1.432 +    SetTestStepResult(EFail);
   1.433 +
   1.434 +    INFO_PRINTF1(_L("Plain Text File Imports"));
   1.435 +    __UHEAP_MARK;
   1.436 +    setupCleanup();
   1.437 +    TRAPD(r, DoTestL());
   1.438 +
   1.439 +    ::DeleteDataFile(KTestFileName1);       //deletion of data files must be before call to End() - DEF047652
   1.440 +    ::DeleteDataFile(KTestFileName2);   
   1.441 +
   1.442 +    delete TheTrapCleanup;
   1.443 +
   1.444 +    __UHEAP_MARKEND;
   1.445 +    
   1.446 +    if (r == KErrNone)
   1.447 +        {
   1.448 +        SetTestStepResult(EPass);
   1.449 +        }
   1.450 +
   1.451 +    return TestStepResult();
   1.452 +    }