os/textandloc/textrendering/textformatting/test/src/TInterpreter.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003-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 * TInterpreter.cpp unit tests for RTmBoundingRectInterpreter
    16 *
    17 */
    18 
    19 
    20 #include "TestPicture.h"
    21 #include "TestLayout.h"
    22 #include "TGraphicsContext.h"
    23 #include "TMINTERP.H"
    24 #include "tinterpreter.h"
    25 #include <txtrich.h>
    26 #include <e32test.h>
    27 
    28 
    29 namespace LocalToTInterpreter
    30 {
    31 _LIT(KLeftToRight1, "abc \x5D0 def abc \x5D0\x5D1\x5D2 \x5D0\x5D1\x5D2 xyz abc a\tb\tc\td\te.");
    32 _LIT(KLeftToRight2, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    33 _LIT(KContingentBreak, "\xFFFC");
    34 
    35 CTInterpreterStep* TestStep = NULL;
    36 #define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
    37 #define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
    38 
    39 }
    40 using namespace LocalToTInterpreter;
    41 
    42 
    43 /**
    44 Checks if one region is a subset of another.
    45 @param aRegion
    46 	The potential superset.
    47 @param aPotentialSubset
    48 	The potential subset.
    49 @return
    50 	ETrue if and only if aPotentialSubset is contained within aRegion.
    51 @internalComponent
    52 */
    53 TBool IsSubsetL(const TRegion& aRegion, const TRegion& aPotentialSubset)
    54 	{
    55 	RRegion sub;
    56 	sub.Copy(aPotentialSubset);
    57 	sub.SubRegion(aRegion);
    58 	if (sub.CheckError())
    59 		User::Leave(KErrNoMemory);
    60 	return sub.IsEmpty();
    61 	}
    62 
    63 /**
    64 Checks if two regions are equal.
    65 @param aA First region.
    66 @param aB Second region.
    67 @return
    68 	ETrue if the regions are equal, false otherwise.
    69 @internalComponent
    70 */
    71 TBool RegionsEqualL(const TRegion& aA, const TRegion& aB)
    72 	{
    73 	return IsSubsetL(aA, aB) && IsSubsetL(aB, aA);
    74 	}
    75 
    76 /**
    77 Gets the region corresponding to the selection. Builds it up by adding
    78 selections together. The selections are at (n * aStartStep, aEnd + n *
    79 aEndStep) for n <- {0, 1, 2...}. The iteration ends when either the start would
    80 be beyond the length of the document or when the start is at 0 and the end is
    81 beyond the length of the document.
    82 @param aRegion Returns the region.
    83 @param aLayout The layout to get the selection of.
    84 @param aStartStep How different the starts of successive selections are.
    85 @param aEnd The end of the first selection.
    86 @param aEndStep How different the ends of the successive selections are.
    87 
    88 @internalComponent
    89 */
    90 void GetSelectionL(TRegion& aRegion, CTestTmTextLayout& aLayout,
    91 	TInt aStartStep, TInt aEnd, TInt aEndStep)
    92 	{
    93 	aRegion.Clear();
    94 	TRect rect;
    95 	TInt documentLength = aLayout.Source().DocumentLength();
    96 	TInt start = 0;
    97 	while (start < documentLength && !(documentLength < aEnd && start == 0))
    98 		{
    99 		TTmInterpreterParam param(aLayout.Layout());
   100 		RTmBoundingRectInterpreter interp(aLayout.Source(), param);
   101 		if (interp.FirstRect(start, aEnd, rect))
   102 			{
   103 			aRegion.AddRect(rect);
   104 			while (interp.NextRect(rect))
   105 				aRegion.AddRect(rect);
   106 			}
   107 		start += aStartStep;
   108 		aEnd += aEndStep;
   109 		interp.Close();
   110 		}
   111 	if (aRegion.CheckError())
   112 		User::Leave(KErrNoMemory);
   113 	}
   114 
   115 /**
   116 Tests RTmBoundingRectInterpreter for a particular piece of text.
   117 @internalComponent
   118 */
   119 void TestTextL(CTestTmTextLayout& aLayout)
   120 	{
   121 	RRegion region1;
   122 	RRegion region2;
   123 	RRegion region3;
   124 	CleanupClosePushL(region1);
   125 	CleanupClosePushL(region2);
   126 	CleanupClosePushL(region3);
   127 
   128 	GetSelectionL(region1, aLayout, 1, 1, 1);
   129 	GetSelectionL(region2, aLayout, 0, 1, 1);
   130 	GetSelectionL(region3, aLayout, 0, aLayout.Source().DocumentLength(), 1);
   131 
   132 	TESTPOINT(RegionsEqualL(region1, region2));
   133 	TESTPOINT(RegionsEqualL(region1, region3));
   134 
   135 	CleanupStack::PopAndDestroy(&region3);
   136 	CleanupStack::PopAndDestroy(&region2);
   137 	CleanupStack::PopAndDestroy(&region1);
   138 	}
   139 
   140 
   141 void TestsL()
   142 	{
   143 	CParaFormatLayer* paraLayer = CParaFormatLayer::NewL();
   144 	CleanupStack::PushL(paraLayer);
   145 	CCharFormatLayer* charLayer = CCharFormatLayer::NewL();
   146 	CleanupStack::PushL(charLayer);
   147 	CRichText* richText = CRichText::NewL(paraLayer, charLayer);
   148 	CleanupStack::PushL(richText);
   149 
   150 	TESTPRINT(_L("RTmBoundingRectInterpreter consistency of coverage"));
   151 	richText->Reset();
   152 	richText->InsertL(0, KLeftToRight1);
   153 	CTestTmTextLayout* text1 = CTestTmTextLayout::NewLC(*richText, 100);
   154 	TSize pictureSize(15, 15);
   155 	CTestPicture* picture = new (ELeave) CTestPicture;
   156 	picture->SetSizeInPixels( pictureSize, text1->Device() );
   157 	TPictureHeader pictureHeader;
   158 	pictureHeader.iPicture = picture;
   159 	pictureHeader.iSize = pictureSize;
   160 	richText->InsertL(19, pictureHeader);
   161 	TTmReformatParam param;
   162 	param.iStartChar = 19;
   163 	param.iOldLength = 0;
   164 	param.iNewLength = 1;
   165 	param.iMaxExtraLines = KMaxTInt;
   166 	param.iParFormatChanged = ETrue;
   167 	param.iParInvalid = EFalse;
   168 	TTmReformatResult out;
   169 	text1->FormatL(param, out);
   170 	TestTextL(*text1);
   171 
   172 	//Test for finding text chunks adjoining a given document position
   173 	text1->TestAdjacentChunks();
   174 
   175 	CleanupStack::PopAndDestroy(text1);
   176 	CleanupStack::PopAndDestroy(richText);
   177 	CleanupStack::PopAndDestroy(charLayer);
   178 	CleanupStack::PopAndDestroy(paraLayer);
   179 	}
   180 
   181 
   182 /**
   183 @SYMTestCaseID          SYSLIB-FORM-UT-1591
   184 @SYMTestCaseDesc	    Tests to make sure when contingent line breaks, "\xFFFC", are inserted into richtext
   185 						(with and without a picture attached) and FormatL is called it does not lead to a
   186 						panic in 'IsLegalBreakBeforeL' or 'IsLegalBreakAfterL'.
   187 @SYMTestPriority 	    High
   188 @SYMTestActions  	    Insert some richtext, then insert a TPictureHeader into the text (which inserts a
   189 						contingent line break), then call FormatL. Then insert just a contingent line break,
   190 						with no TPictureHeader associated with it and call FormatL (for IsLegalBreakAfterL).
   191 						Then repeat the process, but making sure the text is scanned in the opposite
   192 						direction, so IsLegalBreakBeforeL is called.
   193 @SYMTestExpectedResults Test must not fail
   194 @SYMDEF                 DEF077884
   195 */
   196 
   197 void Def077884L()
   198 	{
   199 	TInt testStartLength = 52;
   200 	TInt testEndLength = 56;
   201 
   202 	CParaFormatLayer* paraLayer = CParaFormatLayer::NewL();
   203 	CleanupStack::PushL(paraLayer);
   204 	CCharFormatLayer* charLayer = CCharFormatLayer::NewL();
   205 	CleanupStack::PushL(charLayer);
   206 	CRichText* richText = CRichText::NewL(paraLayer, charLayer);
   207 	CleanupStack::PushL(richText);
   208 
   209 	TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-UT-1591 DEF077884: TSourcePictureBreaker crashes when picture not found. "));
   210 
   211 
   212 	richText->Reset();
   213 
   214 	richText->InsertL(0, KLeftToRight2);
   215 
   216 	TESTPOINT(testStartLength == richText->DocumentLength());
   217 
   218 	CTestTmTextLayout* text1 = CTestTmTextLayout::NewLC(*richText, 100);
   219 
   220 	TTmReformatResult out;
   221 	TTmReformatParam param;
   222 	param.iStartChar = 0;
   223 	param.iOldLength = 0;
   224 	param.iNewLength = 1;
   225 	param.iMaxExtraLines = KMaxTInt;
   226 	param.iParFormatChanged = ETrue;
   227 	param.iParInvalid = EFalse;
   228 
   229 	richText->InsertL(14, KContingentBreak); // Insert a contingent linebreak with no picture
   230 
   231 	text1->FormatL(param, out);	// Scans the text from right to left.
   232 
   233 
   234 	TSize pictureSize(100, 100);
   235 	CTestPicture* picture = new (ELeave) CTestPicture;
   236 	picture->SetSizeInPixels(pictureSize, text1->Device());
   237 
   238 	TPictureHeader pictureHeader;
   239 	pictureHeader.iPicture = picture;
   240 	pictureHeader.iSize = pictureSize;
   241 
   242 	richText->InsertL(15, pictureHeader); // Insert a contingent linebreak with picture
   243 
   244 	param.iOldLength = 1;
   245 	param.iNewLength = 2;
   246 
   247 	text1->FormatL(param, out);	// Scans the text from right to left.
   248 
   249 
   250 	TTmFormatParam formatParam;
   251 	formatParam.iFlags = 0;
   252 	// iFlags now need to be set to make sure the text is scanned in the opposite direction when
   253 	// FormatL is called.
   254 	formatParam.iFlags = formatParam.iFlags | TTmFormatParamBase::ELegalLineBreaksOnly;
   255 	formatParam.iFlags = formatParam.iFlags | TTmFormatParamBase::EWrap;
   256  	formatParam.iWrapWidth = 12;
   257 	formatParam.iMaxHeight = 100;
   258 	formatParam.iMaxLines = 10;
   259 	formatParam.iEllipsis = 0xFFFF;
   260 	formatParam.iStartChar = 0;
   261 	formatParam.iEndChar = 50;
   262 	formatParam.iLineInPar = 0;
   263 
   264 	param.iOldLength = 2;
   265 	param.iNewLength = 3;
   266 
   267 	CTmTextLayout* text2 = new (ELeave) CTmTextLayout;
   268 
   269 	text2->SetTextL(text1->Source(),formatParam);
   270 
   271 	richText->InsertL(28, KContingentBreak); // Insert a contingent linebreak with no picture
   272 
   273 	text2->FormatL(formatParam, param, out); // Scans the text from left to right.
   274 
   275 
   276 	CTestPicture* picture2 = new (ELeave) CTestPicture;
   277 	picture2->SetSizeInPixels( pictureSize, text1->Device() );
   278 
   279 	TPictureHeader pictureHeader2;
   280 	pictureHeader2.iPicture = picture2;
   281 	pictureHeader2.iSize = pictureSize;
   282 
   283 	richText->InsertL(34, pictureHeader2); // Insert a contingent linebreak with picture
   284 
   285 	param.iOldLength = 3;
   286 	param.iNewLength = 4;
   287 
   288 	text2->FormatL(formatParam, param, out); // Scans the text from left to right.
   289 
   290 
   291 	TestTextL(*text1);
   292 
   293 	TESTPOINT(testEndLength == richText->DocumentLength());
   294 
   295 	CleanupStack::PopAndDestroy(text1);
   296 	CleanupStack::PopAndDestroy(richText);
   297 	CleanupStack::PopAndDestroy(charLayer);
   298 	CleanupStack::PopAndDestroy(paraLayer);
   299 	}
   300 
   301 /**
   302 Tests RTmBoundingRectInterpreter.
   303 @internalComponent
   304 */
   305 TVerdict CTInterpreterStep::doTestStepL()
   306 	{
   307     SetTestStepResult(EPass);
   308     TestStep = this;
   309     TESTPRINT(_L("TInterpreter unit"));
   310     TESTPRINT(_L("Start TInterpreter.exe Tests"));
   311 	TestsL();
   312 	Def077884L();
   313 	return TestStepResult();
   314 	}
   315 
   316