textrendering/textformatting/test/src/TTmSource.cpp
branchRCL_3
changeset 16 748ec5531811
parent 0 1fb32624e06b
child 17 336bee5c2d35
equal deleted inserted replaced
10:f902e87c146f 16:748ec5531811
     1 /*
     1 /*
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    22 
    22 
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    24 #include "TAGMA_INTERNAL.H"
    24 #include "TAGMA_INTERNAL.H"
    25 #endif
    25 #endif
    26 
    26 
       
    27 #include "ttmsource.h"
       
    28 
       
    29 
    27 #define UNUSED_VAR(a) a = a
    30 #define UNUSED_VAR(a) a = a
    28 
    31 
    29 CTrapCleanup* TrapCleanup;
    32 namespace LocalToTTmSource
    30 RTest test(_L("TTmSource - MTmSource tests"));
    33 {
       
    34 CTTmSourceStep* TestStep = NULL;
       
    35 #define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
       
    36 #define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
       
    37 }
       
    38 using namespace LocalToTTmSource;
    31 
    39 
    32 class TTestGraphicsDeviceMap : public MGraphicsDeviceMap
    40 class TTestGraphicsDeviceMap : public MGraphicsDeviceMap
    33 	{
    41 	{
    34 public:
    42 public:
    35 	TInt HorizontalTwipsToPixels(TInt a) const { return a; }
    43 	TInt HorizontalTwipsToPixels(TInt a) const { return a; }
    53 	virtual TBool LineBreakPossible(TUint aClass,TBool aBeforePicture,TBool aHaveSpaces) const
    61 	virtual TBool LineBreakPossible(TUint aClass,TBool aBeforePicture,TBool aHaveSpaces) const
    54 		{
    62 		{
    55 		++iRequestCount;
    63 		++iRequestCount;
    56 		if (aBeforePicture)
    64 		if (aBeforePicture)
    57 			{
    65 			{
    58 			test(aClass == iClassBefore);
    66             TESTPOINT(aClass == iClassBefore);
    59 			test(aHaveSpaces == iSpacesBefore);
    67             TESTPOINT(aHaveSpaces == iSpacesBefore);
    60 			return iResultBefore;
    68 			return iResultBefore;
    61 			}
    69 			}
    62 		test(aClass == iClassAfter);
    70 		TESTPOINT(aClass == iClassAfter);
    63 		test(aHaveSpaces == iSpacesAfter);
    71 		TESTPOINT(aHaveSpaces == iSpacesAfter);
    64 		return iResultAfter;
    72 		return iResultAfter;
    65 		}
    73 		}
    66 	// expected parameters for LineBreakPossible
    74 	// expected parameters for LineBreakPossible
    67 	TUint iClassBefore;
    75 	TUint iClassBefore;
    68 	TUint iClassAfter;
    76 	TUint iClassAfter;
   130 			second = aPrevClass;
   138 			second = aPrevClass;
   131 			}
   139 			}
   132 		if (ELineBreakClasses <= first && first < ELineBreakClasses + 10)
   140 		if (ELineBreakClasses <= first && first < ELineBreakClasses + 10)
   133 			{
   141 			{
   134 			++customCount;
   142 			++customCount;
   135 			test(first - ELineBreakClasses + '0' == FindNextCustomClass());
   143 			TESTPOINT(first - ELineBreakClasses + '0' == FindNextCustomClass());
   136 			TInt countSpaces = CountSpaces();
   144 			TInt countSpaces = CountSpaces();
   137 			test(!aHaveSpaces == !countSpaces);
   145 			TESTPOINT(!aHaveSpaces == !countSpaces);
   138 			}
   146 			}
   139 		if (ELineBreakClasses <= second && second < ELineBreakClasses + 10)
   147 		if (ELineBreakClasses <= second && second < ELineBreakClasses + 10)
   140 			{
   148 			{
   141 			++customCount;
   149 			++customCount;
   142 			TInt c = FindNextCustomClass();
   150 			TInt c = FindNextCustomClass();
   143 			test(second - ELineBreakClasses + '0' == c);
   151 			TESTPOINT(second - ELineBreakClasses + '0' == c);
   144 			}
   152 			}
   145 		if (0 == customCount)
   153 		if (0 == customCount)
   146 			return MTmSource::LineBreakPossible(aPrevClass, aNextClass, aHaveSpaces);
   154 			return MTmSource::LineBreakPossible(aPrevClass, aNextClass, aHaveSpaces);
   147 		// Between custom and non-custom classes, allow a break only with spaces
   155 		// Between custom and non-custom classes, allow a break only with spaces
   148 		// or between @ and 5
   156 		// or between @ and 5
   159 
   167 
   160 	virtual TBool GetLineBreakInContext(
   168 	virtual TBool GetLineBreakInContext(
   161 		const TDesC& aText, TInt aMinBreakPos, TInt aMaxBreakPos,
   169 		const TDesC& aText, TInt aMinBreakPos, TInt aMaxBreakPos,
   162 		TBool aForwards,TInt& aBreakPos) const
   170 		TBool aForwards,TInt& aBreakPos) const
   163 		{
   171 		{
   164 		test (iDirection == (aForwards? 1 : -1));
   172 	    TESTPOINT (iDirection == (aForwards? 1 : -1));
   165 		// The allowable break-points should not include the first
   173 		// The allowable break-points should not include the first
   166 		// and last characters of the run.
   174 		// and last characters of the run.
   167 		test (aMinBreakPos != 0);
   175 	    TESTPOINT (aMinBreakPos != 0);
   168 		for (TInt i = aMinBreakPos - 1; i <= aMaxBreakPos; ++i)
   176 		for (TInt i = aMinBreakPos - 1; i <= aMaxBreakPos; ++i)
   169 			test('@' == aText[i]);
   177 		    TESTPOINT('@' == aText[i]);
   170 		++iSaRequestCount;
   178 		++iSaRequestCount;
   171 		aBreakPos = iText->Ptr() + iSaBreakpoint - aText.Ptr();
   179 		aBreakPos = iText->Ptr() + iSaBreakpoint - aText.Ptr();
   172 		return aMinBreakPos <= aBreakPos && aBreakPos <= aMaxBreakPos;
   180 		return aMinBreakPos <= aBreakPos && aBreakPos <= aMaxBreakPos;
   173 		}
   181 		}
   174 
   182 
   175 	virtual TBool IsHangingCharacter(TUint aChar) const
   183 	virtual TBool IsHangingCharacter(TUint aChar) const
   176 		{
   184 		{
   177 		++iHangingCharRequestCount;
   185 		++iHangingCharRequestCount;
   178 		test(aChar == (*iText)[iMaxBreakPos]);
   186 		TESTPOINT(aChar == (*iText)[iMaxBreakPos]);
   179 		if (!iHangingChar)
   187 		if (!iHangingChar)
   180 			return EFalse;
   188 			return EFalse;
   181 		if (iDirection < 0)
   189 		if (iDirection < 0)
   182 			++iCurrentPos;
   190 			++iCurrentPos;
   183 		return ETrue;
   191 		return ETrue;
   198 		TBool r = MTmSource::GetLineBreakL(aText, aDocPos,
   206 		TBool r = MTmSource::GetLineBreakL(aText, aDocPos,
   199 			aMinBreakPos, aMaxBreakPos, aForwards,
   207 			aMinBreakPos, aMaxBreakPos, aForwards,
   200 			aBreakPos, aHangingChars, aBreakPosAfterSpaces);
   208 			aBreakPos, aHangingChars, aBreakPosAfterSpaces);
   201 		if (r)
   209 		if (r)
   202 			{
   210 			{
   203 			test(aMinBreakPos <= aBreakPos);
   211             TESTPOINT(aMinBreakPos <= aBreakPos);
   204 			test(0 < aBreakPos);
   212             TESTPOINT(0 < aBreakPos);
   205 			test(aBreakPos <= aHangingChars);
   213             TESTPOINT(aBreakPos <= aHangingChars);
   206 			test(aHangingChars <= aBreakPosAfterSpaces);
   214             TESTPOINT(aHangingChars <= aBreakPosAfterSpaces);
   207 			test(aBreakPos <= aMaxBreakPos);
   215             TESTPOINT(aBreakPos <= aMaxBreakPos);
   208 			test(aHangingChars == aBreakPos || iHangingChar);
   216             TESTPOINT(aHangingChars == aBreakPos || iHangingChar);
   209 			// If the direction was backwards, the algorithm should have
   217 			// If the direction was backwards, the algorithm should have
   210 			// checked if a hanging character was allowed.
   218 			// checked if a hanging character was allowed.
   211 			// This condition could be relaxed to allow it not to be checked
   219 			// This condition could be relaxed to allow it not to be checked
   212 			// if there is no break allowed between the possible hanging
   220 			// if there is no break allowed between the possible hanging
   213 			// character and the previous character.
   221 			// character and the previous character.
   214 			test(!aForwards || aText.Length() == aMaxBreakPos
   222             TESTPOINT(!aForwards || aText.Length() == aMaxBreakPos
   215 				|| 0 < iHangingCharRequestCount);
   223 				|| 0 < iHangingCharRequestCount);
   216 			// If the maximum break point was chosen or exceeded, the algorithm
   224 			// If the maximum break point was chosen or exceeded, the algorithm
   217 			// should have checked to find out whether a hanging character is
   225 			// should have checked to find out whether a hanging character is
   218 			// allowed.
   226 			// allowed.
   219 			test(aHangingChars < aMaxBreakPos
   227             TESTPOINT(aHangingChars < aMaxBreakPos
   220 				|| 0 < iHangingCharRequestCount);
   228 				|| 0 < iHangingCharRequestCount);
   221 			// Check that only spaces exist between aHangingChars and
   229 			// Check that only spaces exist between aHangingChars and
   222 			// aMaxBreakPos
   230 			// aMaxBreakPos
   223 			for (TInt i = aHangingChars; i != aBreakPosAfterSpaces; ++i)
   231 			for (TInt i = aHangingChars; i != aBreakPosAfterSpaces; ++i)
   224 				{
   232 				{
   225 				TUint n;
   233 				TUint n;
   226 				test(ESpLineBreakClass == LineBreakClass(aText[i], n, n));
   234 				TESTPOINT(ESpLineBreakClass == LineBreakClass(aText[i], n, n));
   227 				}
   235 				}
   228 			// Check that all the spaces were counted
   236 			// Check that all the spaces were counted
   229 			test(aBreakPosAfterSpaces == aText.Length()
   237 			TESTPOINT(aBreakPosAfterSpaces == aText.Length()
   230 				|| aText[aBreakPosAfterSpaces] != ' ');
   238 				|| aText[aBreakPosAfterSpaces] != ' ');
   231 			}
   239 			}
   232 		// Find out how many runs of two or more Sa there are, and check that
   240 		// Find out how many runs of two or more Sa there are, and check that
   233 		// this matches the number of times that it was requested.
   241 		// this matches the number of times that it was requested.
   234 		TInt minChecked = aMinBreakPos - 1;
   242 		TInt minChecked = aMinBreakPos - 1;
   244 			minChecked = 0;
   252 			minChecked = 0;
   245 		if (aText.Length() < maxChecked)
   253 		if (aText.Length() < maxChecked)
   246 			maxChecked = aText.Length();
   254 			maxChecked = aText.Length();
   247 		TInt runs = 0;
   255 		TInt runs = 0;
   248 		TInt sasSoFar = 0;
   256 		TInt sasSoFar = 0;
   249 		test (maxChecked - minChecked < 2
   257 		TESTPOINT (maxChecked - minChecked < 2
   250 			|| aText[minChecked] != '@'
   258 			|| aText[minChecked] != '@'
   251 			|| aText[minChecked + 1] != '@'
   259 			|| aText[minChecked + 1] != '@'
   252 			|| !aForwards
   260 			|| !aForwards
   253 			|| aHangingChars == iSaBreakpoint);
   261 			|| aHangingChars == iSaBreakpoint);
   254 		for (; minChecked != maxChecked; ++minChecked)
   262 		for (; minChecked != maxChecked; ++minChecked)
   262 				sasSoFar = 0;
   270 				sasSoFar = 0;
   263 				}
   271 				}
   264 			}
   272 			}
   265 		if (1 < sasSoFar)
   273 		if (1 < sasSoFar)
   266 			++runs;
   274 			++runs;
   267 		test(sasSoFar < 2 || aForwards || aHangingChars == iSaBreakpoint);
   275 		TESTPOINT(sasSoFar < 2 || aForwards || aHangingChars == iSaBreakpoint);
   268 		test(runs == iSaRequestCount);
   276 		TESTPOINT(runs == iSaRequestCount);
   269 		return r;
   277 		return r;
   270 		}
   278 		}
   271 
   279 
   272 	TInt FindNextCustomClass() const
   280 	TInt FindNextCustomClass() const
   273 		{
   281 		{
   330 	b2 = KMaxTInt;
   338 	b2 = KMaxTInt;
   331 	return t.GetLineBreakL(aText, 0, aMin, aMax, aForwards, b0, b1, b2)?
   339 	return t.GetLineBreakL(aText, 0, aMin, aMax, aForwards, b0, b1, b2)?
   332 		b1 : -1;
   340 		b1 : -1;
   333 	}
   341 	}
   334 
   342 
   335 void RunTests()
   343 CTTmSourceStep::CTTmSourceStep()
   336 	{
   344     {
   337 	test.Title();
   345     
   338 	test.Start(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-TTMSOURCE-0001 Line-Break Tests: "));
   346     }
   339 
   347 
   340 	test(-1 == TestLineBreak(_L(""), 0, 0, 0, 0, 0));
   348 
   341 	test(-1 == TestLineBreak(_L("5"), 0, 0, 0, 0, 0));
   349 TVerdict CTTmSourceStep::doTestStepL()
   342 	test(-1 == TestLineBreak(_L("5"), 0, 0, 0, 0, 1));
   350 	{
   343 	test(-1 == TestLineBreak(_L("@"), 1, 0, 0, 0, 0));
   351     SetTestStepResult(EPass);
   344 	test(1 == TestLineBreak(_L("a   b"), 0, 0, 0, 0, 0));
   352     TestStep = this;
   345 	test(-1 == TestLineBreak(_L("0 0 0 9    9"), 0, 0, 0, 0, 0));
   353     TESTPRINT(_L("TTmSource - MTmSource tests"));
   346 	test(-1 == TestLineBreak(_L("0 0 0 9    9"), 0, 0, 0, 0, 1));
   354 	TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-TTMSOURCE-0001 Line-Break Tests: "));
   347 	test(9 == TestLineBreak(_L("4242454445"), 0, 0, 0, 0, 0));
   355 
   348 	test(5 == TestLineBreak(_L("4242454445"), 0, 0, 0, 0, 1));
   356 	TEST(-1 == TestLineBreak(_L(""), 0, 0, 0, 0, 0));
   349 	test(5 == TestLineBreak(_L("hello there"), 0, 0, 0, 0, 0));
   357 	TEST(-1 == TestLineBreak(_L("5"), 0, 0, 0, 0, 0));
   350 	test(5 == TestLineBreak(_L("hello there"), 0, 0, 0, 0, 1));
   358 	TEST(-1 == TestLineBreak(_L("5"), 0, 0, 0, 0, 1));
   351 	test(-1 == TestLineBreak(_L("hel  the re"), 0, 0, 5, 7, 0));
   359 	TEST(-1 == TestLineBreak(_L("@"), 1, 0, 0, 0, 0));
   352 	test(-1 == TestLineBreak(_L("hel  the re"), 0, 0, 5, 7, 1));
   360 	TEST(1 == TestLineBreak(_L("a   b"), 0, 0, 0, 0, 0));
   353 	test(8 == TestLineBreak(_L("hel  the re"), 0, 1, 5, 7, 0));
   361 	TEST(-1 == TestLineBreak(_L("0 0 0 9    9"), 0, 0, 0, 0, 0));
   354 	test(8 == TestLineBreak(_L("hel  the re"), 0, 1, 6, 7, 1));
   362 	TEST(-1 == TestLineBreak(_L("0 0 0 9    9"), 0, 0, 0, 0, 1));
   355 	test(3 == TestLineBreak(_L("@@@@@"), 3, 0, 0, 0, 0));
   363 	TEST(9 == TestLineBreak(_L("4242454445"), 0, 0, 0, 0, 0));
   356 	test(3 == TestLineBreak(_L("@@@@@"), 3, 0, 0, 0, 1));
   364 	TEST(5 == TestLineBreak(_L("4242454445"), 0, 0, 0, 0, 1));
   357 	test(5 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 5, 0, 5, 0, 0));
   365 	TEST(5 == TestLineBreak(_L("hello there"), 0, 0, 0, 0, 0));
   358 	test(5 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 5, 0, 5, 0, 1));
   366 	TEST(5 == TestLineBreak(_L("hello there"), 0, 0, 0, 0, 1));
   359 	test(16 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 16, 0, 0, 0, 0));
   367 	TEST(-1 == TestLineBreak(_L("hel  the re"), 0, 0, 5, 7, 0));
   360 	test(16 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 16, 0, 0, 0, 1));
   368 	TEST(-1 == TestLineBreak(_L("hel  the re"), 0, 0, 5, 7, 1));
   361 	test(5 == TestLineBreak(_L("55@@@55"), 0, 0, 0, 0, 0));
   369 	TEST(8 == TestLineBreak(_L("hel  the re"), 0, 1, 5, 7, 0));
   362 	test(2 == TestLineBreak(_L("55@@@55"), 0, 0, 0, 0, 1));
   370 	TEST(8 == TestLineBreak(_L("hel  the re"), 0, 1, 6, 7, 1));
   363 	test(3 == TestLineBreak(_L("55@55"), 0, 0, 0, 0, 0));
   371 	TEST(3 == TestLineBreak(_L("@@@@@"), 3, 0, 0, 0, 0));
   364 	test(2 == TestLineBreak(_L("55@55"), 0, 0, 0, 0, 1));
   372 	TEST(3 == TestLineBreak(_L("@@@@@"), 3, 0, 0, 0, 1));
       
   373 	TEST(5 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 5, 0, 5, 0, 0));
       
   374 	TEST(5 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 5, 0, 5, 0, 1));
       
   375 	TEST(16 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 16, 0, 0, 0, 0));
       
   376 	TEST(16 == TestLineBreak(_L("9999@@@@@00099@@@@gfra"), 16, 0, 0, 0, 1));
       
   377 	TEST(5 == TestLineBreak(_L("55@@@55"), 0, 0, 0, 0, 0));
       
   378 	TEST(2 == TestLineBreak(_L("55@@@55"), 0, 0, 0, 0, 1));
       
   379 	TEST(3 == TestLineBreak(_L("55@55"), 0, 0, 0, 0, 0));
       
   380 	TEST(2 == TestLineBreak(_L("55@55"), 0, 0, 0, 0, 1));
   365 
   381 
   366 	// Test for DEF046468, which was caused by the TLineBreakIterator constructor accessing past the end of a string
   382 	// Test for DEF046468, which was caused by the TLineBreakIterator constructor accessing past the end of a string
   367 	test.Next(_L("Line-Break DEF046468 Test:"));
   383 	TESTPRINT(_L("Line-Break DEF046468 Test:"));
   368 	// Create a string of 16 chars with a picture code at the 17th position
   384 	// Create a string of 16 chars with a picture code at the 17th position
   369 	_LIT(KLarsString, "dolor sit amet, \xFFFC");
   385 	_LIT(KLarsString, "dolor sit amet, \xFFFC");
   370 	// Create a TPtrC for the 16 character string ( with the picture code after the string in memory )
   386 	// Create a TPtrC for the 16 character string ( with the picture code after the string in memory )
   371 	TBufC<20> KTestBuffer(KLarsString);
   387 	TBufC<20> KTestBuffer(KLarsString);
   372 	TPtrC KTestString( reinterpret_cast<const TUint16*>(KTestBuffer.Ptr()), 16);
   388 	TPtrC KTestString( reinterpret_cast<const TUint16*>(KTestBuffer.Ptr()), 16);
   373 	// Test the iterator overrun. If iterator accesses past the end of the array, it'll get picture code and crash
   389 	// Test the iterator overrun. If iterator accesses past the end of the array, it'll get picture code and crash
   374 	test(9 == TestLineBreak(KTestString,0,0,1,15,0));
   390 	TEST(9 == TestLineBreak(KTestString,0,0,1,15,0));
   375 
   391 	
   376 	test.End();
   392 	return TestStepResult();
   377 	test.Close();
       
   378 
       
   379 	}
   393 	}
   380 
       
   381 TInt E32Main()
       
   382 	{
       
   383 	TrapCleanup = CTrapCleanup::New();
       
   384 	TRAPD(err, RunTests());
       
   385     test(err == KErrNone);
       
   386 	delete TrapCleanup;
       
   387 	return 0;
       
   388 	}