os/textandloc/textrendering/textformatting/test/src/TInterpreter.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/textrendering/textformatting/test/src/TInterpreter.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,316 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-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 +* TInterpreter.cpp unit tests for RTmBoundingRectInterpreter
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include "TestPicture.h"
    1.24 +#include "TestLayout.h"
    1.25 +#include "TGraphicsContext.h"
    1.26 +#include "TMINTERP.H"
    1.27 +#include "tinterpreter.h"
    1.28 +#include <txtrich.h>
    1.29 +#include <e32test.h>
    1.30 +
    1.31 +
    1.32 +namespace LocalToTInterpreter
    1.33 +{
    1.34 +_LIT(KLeftToRight1, "abc \x5D0 def abc \x5D0\x5D1\x5D2 \x5D0\x5D1\x5D2 xyz abc a\tb\tc\td\te.");
    1.35 +_LIT(KLeftToRight2, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    1.36 +_LIT(KContingentBreak, "\xFFFC");
    1.37 +
    1.38 +CTInterpreterStep* TestStep = NULL;
    1.39 +#define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
    1.40 +#define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
    1.41 +
    1.42 +}
    1.43 +using namespace LocalToTInterpreter;
    1.44 +
    1.45 +
    1.46 +/**
    1.47 +Checks if one region is a subset of another.
    1.48 +@param aRegion
    1.49 +	The potential superset.
    1.50 +@param aPotentialSubset
    1.51 +	The potential subset.
    1.52 +@return
    1.53 +	ETrue if and only if aPotentialSubset is contained within aRegion.
    1.54 +@internalComponent
    1.55 +*/
    1.56 +TBool IsSubsetL(const TRegion& aRegion, const TRegion& aPotentialSubset)
    1.57 +	{
    1.58 +	RRegion sub;
    1.59 +	sub.Copy(aPotentialSubset);
    1.60 +	sub.SubRegion(aRegion);
    1.61 +	if (sub.CheckError())
    1.62 +		User::Leave(KErrNoMemory);
    1.63 +	return sub.IsEmpty();
    1.64 +	}
    1.65 +
    1.66 +/**
    1.67 +Checks if two regions are equal.
    1.68 +@param aA First region.
    1.69 +@param aB Second region.
    1.70 +@return
    1.71 +	ETrue if the regions are equal, false otherwise.
    1.72 +@internalComponent
    1.73 +*/
    1.74 +TBool RegionsEqualL(const TRegion& aA, const TRegion& aB)
    1.75 +	{
    1.76 +	return IsSubsetL(aA, aB) && IsSubsetL(aB, aA);
    1.77 +	}
    1.78 +
    1.79 +/**
    1.80 +Gets the region corresponding to the selection. Builds it up by adding
    1.81 +selections together. The selections are at (n * aStartStep, aEnd + n *
    1.82 +aEndStep) for n <- {0, 1, 2...}. The iteration ends when either the start would
    1.83 +be beyond the length of the document or when the start is at 0 and the end is
    1.84 +beyond the length of the document.
    1.85 +@param aRegion Returns the region.
    1.86 +@param aLayout The layout to get the selection of.
    1.87 +@param aStartStep How different the starts of successive selections are.
    1.88 +@param aEnd The end of the first selection.
    1.89 +@param aEndStep How different the ends of the successive selections are.
    1.90 +
    1.91 +@internalComponent
    1.92 +*/
    1.93 +void GetSelectionL(TRegion& aRegion, CTestTmTextLayout& aLayout,
    1.94 +	TInt aStartStep, TInt aEnd, TInt aEndStep)
    1.95 +	{
    1.96 +	aRegion.Clear();
    1.97 +	TRect rect;
    1.98 +	TInt documentLength = aLayout.Source().DocumentLength();
    1.99 +	TInt start = 0;
   1.100 +	while (start < documentLength && !(documentLength < aEnd && start == 0))
   1.101 +		{
   1.102 +		TTmInterpreterParam param(aLayout.Layout());
   1.103 +		RTmBoundingRectInterpreter interp(aLayout.Source(), param);
   1.104 +		if (interp.FirstRect(start, aEnd, rect))
   1.105 +			{
   1.106 +			aRegion.AddRect(rect);
   1.107 +			while (interp.NextRect(rect))
   1.108 +				aRegion.AddRect(rect);
   1.109 +			}
   1.110 +		start += aStartStep;
   1.111 +		aEnd += aEndStep;
   1.112 +		interp.Close();
   1.113 +		}
   1.114 +	if (aRegion.CheckError())
   1.115 +		User::Leave(KErrNoMemory);
   1.116 +	}
   1.117 +
   1.118 +/**
   1.119 +Tests RTmBoundingRectInterpreter for a particular piece of text.
   1.120 +@internalComponent
   1.121 +*/
   1.122 +void TestTextL(CTestTmTextLayout& aLayout)
   1.123 +	{
   1.124 +	RRegion region1;
   1.125 +	RRegion region2;
   1.126 +	RRegion region3;
   1.127 +	CleanupClosePushL(region1);
   1.128 +	CleanupClosePushL(region2);
   1.129 +	CleanupClosePushL(region3);
   1.130 +
   1.131 +	GetSelectionL(region1, aLayout, 1, 1, 1);
   1.132 +	GetSelectionL(region2, aLayout, 0, 1, 1);
   1.133 +	GetSelectionL(region3, aLayout, 0, aLayout.Source().DocumentLength(), 1);
   1.134 +
   1.135 +	TESTPOINT(RegionsEqualL(region1, region2));
   1.136 +	TESTPOINT(RegionsEqualL(region1, region3));
   1.137 +
   1.138 +	CleanupStack::PopAndDestroy(&region3);
   1.139 +	CleanupStack::PopAndDestroy(&region2);
   1.140 +	CleanupStack::PopAndDestroy(&region1);
   1.141 +	}
   1.142 +
   1.143 +
   1.144 +void TestsL()
   1.145 +	{
   1.146 +	CParaFormatLayer* paraLayer = CParaFormatLayer::NewL();
   1.147 +	CleanupStack::PushL(paraLayer);
   1.148 +	CCharFormatLayer* charLayer = CCharFormatLayer::NewL();
   1.149 +	CleanupStack::PushL(charLayer);
   1.150 +	CRichText* richText = CRichText::NewL(paraLayer, charLayer);
   1.151 +	CleanupStack::PushL(richText);
   1.152 +
   1.153 +	TESTPRINT(_L("RTmBoundingRectInterpreter consistency of coverage"));
   1.154 +	richText->Reset();
   1.155 +	richText->InsertL(0, KLeftToRight1);
   1.156 +	CTestTmTextLayout* text1 = CTestTmTextLayout::NewLC(*richText, 100);
   1.157 +	TSize pictureSize(15, 15);
   1.158 +	CTestPicture* picture = new (ELeave) CTestPicture;
   1.159 +	picture->SetSizeInPixels( pictureSize, text1->Device() );
   1.160 +	TPictureHeader pictureHeader;
   1.161 +	pictureHeader.iPicture = picture;
   1.162 +	pictureHeader.iSize = pictureSize;
   1.163 +	richText->InsertL(19, pictureHeader);
   1.164 +	TTmReformatParam param;
   1.165 +	param.iStartChar = 19;
   1.166 +	param.iOldLength = 0;
   1.167 +	param.iNewLength = 1;
   1.168 +	param.iMaxExtraLines = KMaxTInt;
   1.169 +	param.iParFormatChanged = ETrue;
   1.170 +	param.iParInvalid = EFalse;
   1.171 +	TTmReformatResult out;
   1.172 +	text1->FormatL(param, out);
   1.173 +	TestTextL(*text1);
   1.174 +
   1.175 +	//Test for finding text chunks adjoining a given document position
   1.176 +	text1->TestAdjacentChunks();
   1.177 +
   1.178 +	CleanupStack::PopAndDestroy(text1);
   1.179 +	CleanupStack::PopAndDestroy(richText);
   1.180 +	CleanupStack::PopAndDestroy(charLayer);
   1.181 +	CleanupStack::PopAndDestroy(paraLayer);
   1.182 +	}
   1.183 +
   1.184 +
   1.185 +/**
   1.186 +@SYMTestCaseID          SYSLIB-FORM-UT-1591
   1.187 +@SYMTestCaseDesc	    Tests to make sure when contingent line breaks, "\xFFFC", are inserted into richtext
   1.188 +						(with and without a picture attached) and FormatL is called it does not lead to a
   1.189 +						panic in 'IsLegalBreakBeforeL' or 'IsLegalBreakAfterL'.
   1.190 +@SYMTestPriority 	    High
   1.191 +@SYMTestActions  	    Insert some richtext, then insert a TPictureHeader into the text (which inserts a
   1.192 +						contingent line break), then call FormatL. Then insert just a contingent line break,
   1.193 +						with no TPictureHeader associated with it and call FormatL (for IsLegalBreakAfterL).
   1.194 +						Then repeat the process, but making sure the text is scanned in the opposite
   1.195 +						direction, so IsLegalBreakBeforeL is called.
   1.196 +@SYMTestExpectedResults Test must not fail
   1.197 +@SYMDEF                 DEF077884
   1.198 +*/
   1.199 +
   1.200 +void Def077884L()
   1.201 +	{
   1.202 +	TInt testStartLength = 52;
   1.203 +	TInt testEndLength = 56;
   1.204 +
   1.205 +	CParaFormatLayer* paraLayer = CParaFormatLayer::NewL();
   1.206 +	CleanupStack::PushL(paraLayer);
   1.207 +	CCharFormatLayer* charLayer = CCharFormatLayer::NewL();
   1.208 +	CleanupStack::PushL(charLayer);
   1.209 +	CRichText* richText = CRichText::NewL(paraLayer, charLayer);
   1.210 +	CleanupStack::PushL(richText);
   1.211 +
   1.212 +	TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-UT-1591 DEF077884: TSourcePictureBreaker crashes when picture not found. "));
   1.213 +
   1.214 +
   1.215 +	richText->Reset();
   1.216 +
   1.217 +	richText->InsertL(0, KLeftToRight2);
   1.218 +
   1.219 +	TESTPOINT(testStartLength == richText->DocumentLength());
   1.220 +
   1.221 +	CTestTmTextLayout* text1 = CTestTmTextLayout::NewLC(*richText, 100);
   1.222 +
   1.223 +	TTmReformatResult out;
   1.224 +	TTmReformatParam param;
   1.225 +	param.iStartChar = 0;
   1.226 +	param.iOldLength = 0;
   1.227 +	param.iNewLength = 1;
   1.228 +	param.iMaxExtraLines = KMaxTInt;
   1.229 +	param.iParFormatChanged = ETrue;
   1.230 +	param.iParInvalid = EFalse;
   1.231 +
   1.232 +	richText->InsertL(14, KContingentBreak); // Insert a contingent linebreak with no picture
   1.233 +
   1.234 +	text1->FormatL(param, out);	// Scans the text from right to left.
   1.235 +
   1.236 +
   1.237 +	TSize pictureSize(100, 100);
   1.238 +	CTestPicture* picture = new (ELeave) CTestPicture;
   1.239 +	picture->SetSizeInPixels(pictureSize, text1->Device());
   1.240 +
   1.241 +	TPictureHeader pictureHeader;
   1.242 +	pictureHeader.iPicture = picture;
   1.243 +	pictureHeader.iSize = pictureSize;
   1.244 +
   1.245 +	richText->InsertL(15, pictureHeader); // Insert a contingent linebreak with picture
   1.246 +
   1.247 +	param.iOldLength = 1;
   1.248 +	param.iNewLength = 2;
   1.249 +
   1.250 +	text1->FormatL(param, out);	// Scans the text from right to left.
   1.251 +
   1.252 +
   1.253 +	TTmFormatParam formatParam;
   1.254 +	formatParam.iFlags = 0;
   1.255 +	// iFlags now need to be set to make sure the text is scanned in the opposite direction when
   1.256 +	// FormatL is called.
   1.257 +	formatParam.iFlags = formatParam.iFlags | TTmFormatParamBase::ELegalLineBreaksOnly;
   1.258 +	formatParam.iFlags = formatParam.iFlags | TTmFormatParamBase::EWrap;
   1.259 + 	formatParam.iWrapWidth = 12;
   1.260 +	formatParam.iMaxHeight = 100;
   1.261 +	formatParam.iMaxLines = 10;
   1.262 +	formatParam.iEllipsis = 0xFFFF;
   1.263 +	formatParam.iStartChar = 0;
   1.264 +	formatParam.iEndChar = 50;
   1.265 +	formatParam.iLineInPar = 0;
   1.266 +
   1.267 +	param.iOldLength = 2;
   1.268 +	param.iNewLength = 3;
   1.269 +
   1.270 +	CTmTextLayout* text2 = new (ELeave) CTmTextLayout;
   1.271 +
   1.272 +	text2->SetTextL(text1->Source(),formatParam);
   1.273 +
   1.274 +	richText->InsertL(28, KContingentBreak); // Insert a contingent linebreak with no picture
   1.275 +
   1.276 +	text2->FormatL(formatParam, param, out); // Scans the text from left to right.
   1.277 +
   1.278 +
   1.279 +	CTestPicture* picture2 = new (ELeave) CTestPicture;
   1.280 +	picture2->SetSizeInPixels( pictureSize, text1->Device() );
   1.281 +
   1.282 +	TPictureHeader pictureHeader2;
   1.283 +	pictureHeader2.iPicture = picture2;
   1.284 +	pictureHeader2.iSize = pictureSize;
   1.285 +
   1.286 +	richText->InsertL(34, pictureHeader2); // Insert a contingent linebreak with picture
   1.287 +
   1.288 +	param.iOldLength = 3;
   1.289 +	param.iNewLength = 4;
   1.290 +
   1.291 +	text2->FormatL(formatParam, param, out); // Scans the text from left to right.
   1.292 +
   1.293 +
   1.294 +	TestTextL(*text1);
   1.295 +
   1.296 +	TESTPOINT(testEndLength == richText->DocumentLength());
   1.297 +
   1.298 +	CleanupStack::PopAndDestroy(text1);
   1.299 +	CleanupStack::PopAndDestroy(richText);
   1.300 +	CleanupStack::PopAndDestroy(charLayer);
   1.301 +	CleanupStack::PopAndDestroy(paraLayer);
   1.302 +	}
   1.303 +
   1.304 +/**
   1.305 +Tests RTmBoundingRectInterpreter.
   1.306 +@internalComponent
   1.307 +*/
   1.308 +TVerdict CTInterpreterStep::doTestStepL()
   1.309 +	{
   1.310 +    SetTestStepResult(EPass);
   1.311 +    TestStep = this;
   1.312 +    TESTPRINT(_L("TInterpreter unit"));
   1.313 +    TESTPRINT(_L("Start TInterpreter.exe Tests"));
   1.314 +	TestsL();
   1.315 +	Def077884L();
   1.316 +	return TestStepResult();
   1.317 +	}
   1.318 +
   1.319 +