os/textandloc/textrendering/textformatting/test/src/TLINEPAG.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * Class to test the line paginator.
    16 *
    17 */
    18 
    19 
    20 #include <basched.h>
    21 #include <frmpage.h>
    22 #include <frmconst.h>
    23 #include <bautils.h>
    24 #include <e32test.h>
    25 #include "tlinepag.h"
    26 
    27 namespace LocalToTLinePag
    28 {
    29 _LIT(KTLinePag, "TLinePag");
    30 CTLinePagStep*  TestStep = NULL;
    31 #define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
    32 #define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
    33 }
    34 using namespace LocalToTLinePag;
    35 ///////////////////////////////////////////////////////////////////////////////////////
    36 ///////////////////////////////////////////////////////////////////////////////////////
    37 //Test macroses and functions
    38 
    39 LOCAL_C void CheckL(TInt aValue, TInt aExpected, TInt aLine)
    40 	{
    41 	if(aValue != aExpected)
    42 		{
    43         TBuf<256> buf;
    44         buf.AppendFormat(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    45         TestStep->print(buf,(TText8*)__FILE__,aLine);
    46 		TestStep->testpoint(EFalse,(TText8*)__FILE__,aLine);
    47 		}
    48 	}
    49 
    50 class CLinePaginatorTest : public CBase
    51 	{
    52 public:
    53 	static CLinePaginatorTest* NewL();
    54 	~CLinePaginatorTest();
    55 	void OpenTestFileL();
    56 	void StartPaginateL();
    57 	TBool CompareArrays();
    58 private:
    59 	void ConstructL();
    60 	CLinePaginatorTest();
    61 	TBool PaginateSectionL();
    62 	TBool ReadTestFile(TPageLine& aline);
    63 	void WriteOutputFile(TPageLine aLine);
    64 	void StorePageBreakL(TInt aDocPos);
    65 private:
    66 	CArrayFixFlat<TInt>* iCharsPerPage;
    67 	TLinePaginator iPaginator;
    68 	CArrayFixFlat<TInt>* iTestChars;
    69 	TInt iNumPages;
    70 	RFile iTestFile;
    71 	RFs  ifsClient;
    72 	TInt iFilePos;
    73 	TBool iTestPageBreak;
    74 	};
    75 
    76 
    77 #define TEST_FILE _L("z:\\test\\app-framework\\form\\input\\page.txt")
    78 
    79 const TInt KGranularity = 10;
    80 
    81 CLinePaginatorTest* CLinePaginatorTest::NewL()
    82 	{
    83 	CLinePaginatorTest* self=new(ELeave) CLinePaginatorTest();
    84 	self->ConstructL();
    85 	return self;
    86 	}
    87 
    88 CLinePaginatorTest::CLinePaginatorTest()
    89 	{}
    90 
    91 CLinePaginatorTest::~CLinePaginatorTest()
    92 	{
    93 	iTestFile.Close();
    94 	ifsClient.Close();
    95 	delete iTestChars;
    96 	delete iCharsPerPage;
    97 	}
    98 
    99 void CLinePaginatorTest::ConstructL()
   100 	{
   101 	iCharsPerPage=new(ELeave) CArrayFixFlat<TInt>(KGranularity);
   102 	iTestChars=new(ELeave) CArrayFixFlat<TInt>(KGranularity);
   103 
   104 	iFilePos=0;
   105 	iNumPages=0;
   106 
   107 	iPaginator.SetArray(iCharsPerPage);
   108 	}
   109 
   110 
   111 void CLinePaginatorTest::StartPaginateL(/*parameters*/)
   112 	{
   113 	// do initialisation stuff
   114 	while(PaginateSectionL())
   115 		{
   116 		}
   117 	TBool success=CompareArrays();
   118 	CheckL(success, ETrue,__LINE__);
   119 	}
   120 
   121 
   122 TBool CLinePaginatorTest::PaginateSectionL()
   123 	{
   124 	TPageLine line;
   125 
   126 	while (ReadTestFile(line))
   127 		{
   128 		iPaginator.AppendLineL(line);
   129 		if (iTestPageBreak)
   130 			StorePageBreakL(line.iDocPos);
   131 		WriteOutputFile(line);
   132 		}
   133 
   134 	iPaginator.FlushL(line.iDocPos);
   135 	StorePageBreakL(line.iDocPos);
   136 
   137 	return(EFalse);
   138 	}
   139 
   140 void CLinePaginatorTest::OpenTestFileL()
   141 	{
   142 	TBuf8<128> text;
   143 	TInt err=ifsClient.Connect();
   144 	err=iTestFile.Open(ifsClient,TEST_FILE,EFileStream|EFileRead|EFileShareReadersOnly);
   145 
   146 
   147 	iTestFile.Read(0,text,128);
   148 	TInt startNum=text.Locate('<')+1;
   149 	text.Delete(0,startNum);
   150 	TLex8 lex=text;
   151 	TInt pageHeight;
   152 	lex.Val(pageHeight);
   153 	iPaginator.SetPageHeight(pageHeight);
   154 
   155 	TBuf<254> buf;
   156 	buf.AppendFormat(_L("DocPos\tHeight\tKeep\tStart\tHeight of Pages = <%d>\n"), pageHeight);
   157 	TESTPRINT(buf);
   158 	}
   159 
   160 TBool CLinePaginatorTest::ReadTestFile(TPageLine& aLine)
   161 	{
   162 	TLex8 lex;
   163 	TBuf8<128> textBuffer;
   164 	TBuf8<128> numBuffer;
   165 	TInt startNum;
   166 
   167 	iTestFile.Read(iFilePos,textBuffer,128);
   168 	if (textBuffer.Locate('X') != KErrNotFound && textBuffer.Locate('X') < textBuffer.Locate('\r'))
   169 		{
   170 		startNum=textBuffer.Locate('\n')+1;
   171   		textBuffer.Delete(0,startNum);
   172 		iFilePos+=startNum;
   173 		lex=textBuffer;
   174 		lex.Val(aLine.iDocPos);
   175 		TBuf<254> buf;
   176 		buf.AppendFormat(_L("%d\tX\n"), aLine.iDocPos);
   177 		TESTPRINT(buf);
   178 		return EFalse;
   179 		}
   180 
   181 	startNum=textBuffer.Locate('\n')+1;
   182 	textBuffer.Delete(0,startNum);
   183 	iFilePos+=startNum;
   184 	lex=textBuffer;
   185 	lex.Val(aLine.iDocPos);
   186 
   187 	startNum=textBuffer.Locate('\t')+1;
   188 	textBuffer.Delete(0,startNum);
   189 	iFilePos+=startNum;
   190 	lex=textBuffer;
   191 	lex.Val(aLine.iLineHeight);
   192 
   193 	startNum=textBuffer.Locate('\t')+1;
   194 	textBuffer.Delete(0,startNum);
   195 	iFilePos+=startNum;
   196 	lex=textBuffer;
   197 	lex.Val(aLine.iKeepWithNext);
   198 
   199 	startNum=textBuffer.Locate('\t')+1;
   200 	textBuffer.Delete(0,startNum);
   201 	iFilePos+=startNum;
   202 	lex=textBuffer;
   203 	lex.Val(aLine.iStartNewPage);
   204 
   205 	if (textBuffer.Locate('\t')<textBuffer.Locate('\r')
   206 		&& textBuffer.Locate('B')!=KErrNotFound
   207 		&& textBuffer.Locate('B')<textBuffer.Locate('\r'))
   208 		iTestPageBreak=ETrue;
   209 	else
   210 		iTestPageBreak=EFalse;
   211 
   212 	iFilePos+=textBuffer.Locate('\r')+1;
   213 	return ETrue;
   214 	}
   215 
   216 void CLinePaginatorTest::WriteOutputFile(TPageLine aLine)
   217 	{
   218     TBuf<256> buf;
   219 	if (iTestPageBreak)
   220 		{
   221 		buf.AppendFormat(_L("%d\t%d\t%d\t%d\tBREAK\n"), aLine.iDocPos, aLine.iLineHeight, aLine.iKeepWithNext, aLine.iStartNewPage);
   222 		TESTPRINT(buf);		
   223 		}
   224 	else
   225 		{
   226         buf.Zero();        
   227 		buf.AppendFormat(_L("%d\t%d\t%d\t%d\n"), aLine.iDocPos, aLine.iLineHeight, aLine.iKeepWithNext, aLine.iStartNewPage);
   228 		TESTPRINT(buf);
   229 		}
   230 
   231 	}
   232 
   233 void CLinePaginatorTest::StorePageBreakL(TInt aDocPos)
   234 	{
   235 	iNumPages++;
   236 	iTestChars->AppendL(aDocPos);
   237 	}
   238 
   239 //#pragma warning( disable : 4701 )	//local variable 'docPosError' may be used without having been initialized
   240 TBool CLinePaginatorTest::CompareArrays()
   241 	{
   242 	TInt numPages=iCharsPerPage->Count();
   243 	TInt numTestPages=iTestChars->Count();
   244 	TInt numChars;
   245 	TInt numTestChars;
   246 	TBool pagesSame=EFalse;
   247 	TBuf8<128> text;
   248 	TInt prevDocPos=0;
   249 	TInt docPosError=0;
   250 
   251 	TBuf<256> buf;
   252 	if (numPages==numTestPages)
   253 		{
   254  		buf.AppendFormat(_L("Correct Number of Pages = %d\n"), numPages);
   255  		TESTPRINT(buf);
   256 		pagesSame=ETrue;
   257 		for (TInt i=0; i<numPages; i++)
   258 			{
   259 			numChars=(*iCharsPerPage)[i];
   260 			numTestChars=(*iTestChars)[i]-prevDocPos;
   261 			if(numChars!=numTestChars)
   262 				{
   263 				if (pagesSame)
   264 					docPosError=(*iTestChars)[i];
   265 				pagesSame=EFalse;
   266 				}
   267 			prevDocPos=(*iTestChars)[i];
   268 			}
   269 		if (pagesSame)
   270 			{            
   271  			TESTPRINT(_L("Correct Number of Characters on Pages -- PASSED\n"));
   272 			}
   273 		else
   274 			{
   275             TESTPRINT(_L("Incorrect Number of Characters on Pages -- FAILED\n"));            
   276             buf.Zero();
   277 			buf.AppendFormat(_L("First Error Occurs at Position = %d"), docPosError);
   278 			TESTPRINT(buf);
   279 			}
   280 		}
   281 	else
   282 		{
   283         TESTPRINT(_L("Incorrect Number of Pages  -- FAILED\n"));
   284         buf.Zero();
   285 		buf.AppendFormat(_L("Number Calculated	by LinePaginator = %d\n"), numPages);		
   286 		buf.AppendFormat(_L("Number Contained in File = %d\n"), numTestPages);
   287 		TESTPRINT(buf);
   288 		}
   289 
   290 	iTestFile.Close();
   291 	return pagesSame;
   292 	}
   293 
   294 
   295 void LinePaginateL()
   296 	{
   297 	CLinePaginatorTest* linePaginate=CLinePaginatorTest::NewL();
   298 	linePaginate->OpenTestFileL();
   299 	linePaginate->StartPaginateL();
   300 	delete linePaginate;
   301 	}
   302 
   303 TVerdict CTLinePagStep::doTestStepL()
   304 	{
   305     SetTestStepResult(EPass);
   306     TestStep = this;
   307 	TESTPRINT(KTLinePag);
   308 	TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-LINEPAG-0001 CLinePaginatorTest tests "));
   309 	TRAPD(error, LinePaginateL());
   310 	TESTPOINT(error == KErrNone);
   311 	return TestStepResult();
   312 	}
   313 
   314 void FormPanic(TFormPanic aPanic)
   315 	{
   316 	User::Panic(_L("Form"),aPanic);
   317 	}
   318