os/textandloc/fontservices/fontstore/tfs/t_linkedfontsmemory.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-2009 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 *
    16 */
    17 
    18 
    19 #include "FNTSTD.H"
    20 #include "t_linkedfontsmemory.h"
    21 #include "T_LinkedFonts2.h"
    22 #include "fbsmessage.h"
    23 #include <hal.h>
    24 #include <linkedfonts.h>
    25 
    26 class CTLinkedFontsMemory : public CTLinkedFonts2
    27 	{
    28 public:
    29 	CTLinkedFontsMemory(CTestStep* aStep);
    30 	~CTLinkedFontsMemory();
    31 	//base class static void TestPanic(TInt aPanic);
    32 protected:
    33 // From CTGraphicsStep
    34 	virtual void RunTestCaseL(TInt aCurTestCase);
    35 private:
    36 	
    37 	TInt TestLinkedFontsMemory1L(TInt aTypefaces);
    38 	void TestLinkedFontsMemory1L();
    39 	TInt TestLinkedFontsCreateLinkedTypefacesL(const TDesC& aName,TInt aTypefaces);
    40 	TInt TestLinkedFontsMemory_Part2(const TDesC& aName);
    41 	void TestLinkedFontsMemory2();
    42 	void TestLinkedFontsMemory3();
    43 	void TestLinkedFontsMemory4();
    44 	void TestMemoryUsage();
    45 	
    46 	void SetHeapFail(RFbsSession::THeapFailType aType, TInt aFailures);
    47 	TInt HeapCount(RFbsSession::THeapFailType aType);
    48 	void SetHeapReset(RFbsSession::THeapFailType aType);
    49 	void SetHeapCheck(RFbsSession::THeapFailType aType);
    50 	void TestCompareText();
    51 	TInt FontAndBitmapServerSharedHeap();
    52 	void DisplayRamInfo(const TDesC &aTextInfo) const;
    53 	TBool CheckRasterizerInstalledL();
    54 	};
    55 
    56 CTLinkedFontsMemory::CTLinkedFontsMemory(CTestStep* aStep)
    57 	:	CTLinkedFonts2(aStep)
    58 	{
    59 	}	
    60 
    61 CTLinkedFontsMemory::~CTLinkedFontsMemory()
    62 	{
    63 	}
    64 
    65 void CTLinkedFontsMemory::SetHeapFail(RFbsSession::THeapFailType aType, TInt aFailures)
    66 	{
    67 	__ASSERT_ALWAYS(iFbs, TestPanic(KErrGeneral));
    68 	iFbs->SendCommand(EFbsMessSetHeapFail, aType, aFailures);
    69 	}
    70 
    71 TInt CTLinkedFontsMemory::HeapCount(RFbsSession::THeapFailType aType)
    72 	{
    73 	__ASSERT_ALWAYS(iFbs, TestPanic(KErrGeneral));
    74 	return iFbs->SendCommand(EFbsMessHeapCount, aType);
    75 	}
    76 
    77 void CTLinkedFontsMemory::SetHeapReset(RFbsSession::THeapFailType aType)
    78 	{
    79 	__ASSERT_ALWAYS(iFbs, TestPanic(KErrGeneral));
    80 	iFbs->SendCommand(EFbsMessSetHeapReset, aType);
    81 	}
    82 
    83 void CTLinkedFontsMemory::SetHeapCheck(RFbsSession::THeapFailType aType)
    84 	{
    85 	__ASSERT_ALWAYS(iFbs, TestPanic(KErrGeneral));
    86 	iFbs->SendCommand(EFbsMessSetHeapCheck, aType);
    87 	}
    88 
    89 TInt CTLinkedFontsMemory::FontAndBitmapServerSharedHeap()
    90 	{
    91 	__ASSERT_ALWAYS(iFbs, TestPanic(KErrGeneral));
    92 	return iFbs->SendCommand(EFbsMessHeap);
    93 	}
    94 
    95 _LIT(KLinkedFontsMemoryTestFont,"MemTestLinkedFnt");
    96 TBool CTLinkedFontsMemory::CheckRasterizerInstalledL()
    97 	{
    98 	CFbsTypefaceStore* store = CFbsTypefaceStore::NewL(iDev);
    99 	CleanupStack::PushL(store);
   100 	
   101 	// Create typeface to be linked
   102 	CLinkedTypefaceSpecification *typefaceSpec;
   103 	typefaceSpec = CLinkedTypefaceSpecification::NewLC(KLinkedFontsMemoryTestFont);
   104 	
   105 	CLinkedTypefaceGroup* group1 = CLinkedTypefaceGroup::NewLC(1);
   106 	typefaceSpec->AddLinkedTypefaceGroupL(*group1);
   107 	CleanupStack::Pop();
   108 	
   109 	// Add first typeface
   110 	CLinkedTypefaceElementSpec *elementSpec1;
   111 	_LIT(KLinkedTypefaceTT, "DejaVu Sans Condensed");
   112 	elementSpec1 = CLinkedTypefaceElementSpec::NewLC(KLinkedTypefaceTT, 1);
   113 	elementSpec1->SetCanonical(ETrue);
   114 		
   115 	typefaceSpec->AddTypefaceAtBackL(*elementSpec1);
   116 	CleanupStack::Pop(1, elementSpec1);
   117 	
   118 	// Now try regstering the linked typeface
   119 	TRAPD(ret, typefaceSpec->RegisterLinkedTypefaceL(*store));
   120 	CleanupStack::PopAndDestroy(2, store);
   121 
   122 	switch (ret)
   123 		{
   124 		case KErrAlreadyExists:
   125 		case KErrNone:
   126 			return ETrue;
   127 		default:
   128 			INFO_PRINTF1(_L("Linked fonts memory: no linked fonts rasterizer found"));
   129 			return EFalse;
   130 		}
   131 	}
   132 
   133 _LIT(KDrawMe, "lInKED FOnTS");
   134 
   135 void CTLinkedFontsMemory::TestCompareText()
   136 	{
   137 	_LIT(KWithout, "WithoutLinkedFontTT");
   138 	TTypeface typeface;
   139 	typeface.iName = KWithout;
   140 	TFontSpec fs;
   141 	fs.iTypeface = typeface;
   142 	fs.iHeight = 20;
   143 	CFbsFont* font = NULL;
   144 	TInt err1 = iDev->GetNearestFontToDesignHeightInPixels(font, fs);
   145 
   146 	if (font && err1 == KErrNone)
   147 		{
   148 		// Use the font
   149 		TSize scrSize = iDev->SizeInPixels();
   150 		iGc->UseFont(font);
   151 		iGc->DrawText(KDrawMe, TPoint((scrSize.iWidth / 2) + 50, 50));
   152 		iGc->DiscardFont();
   153 		iDev->ReleaseFont(font);
   154 		TPoint left(0, 0);
   155 		TPoint right(scrSize.iWidth / 2, 0);
   156 		TBool val = iDev->RectCompare(TRect(left, TPoint(scrSize.iWidth / 2, scrSize.iHeight)), *iDev,TRect(right, TPoint(scrSize.iWidth, scrSize.iHeight)));
   157 		if (!val)
   158 			{
   159 			iGc->DrawRect(TRect(left, TPoint(scrSize.iWidth / 2, scrSize.iHeight)));
   160 			iGc->DrawRect(TRect(right, TPoint(scrSize.iWidth, scrSize.iHeight)));
   161 			}
   162 		TEST(val);
   163 		
   164 		}
   165 	else
   166 		{
   167 		TEST(0);  //there is a failure
   168 		}
   169 	}
   170 
   171 /** these fonts need to be provided, and are used for creating linked typefaces
   172 TLinkedFont1.ttf
   173 Typeface name: LinkedFont1
   174 This will be the dominant font. It contains 178 characters.
   175 Some of the characters (13) are deleted from this file and separated in to other three files.
   176 Characters missing are F, D, J, M, P, S, V, X, Z, a, n, l, y.
   177 
   178 TLinkedFont2.ttf
   179 Typeface name: LinkedFont2
   180 Characters available (7):	F, J, M, S, Z, a, y.
   181 
   182 TLinkedFont3.ttf
   183 Typeface name: LinkedFont3
   184 Characters available (4):	D, P, x, l
   185 
   186 TLinkedFont4.ttf
   187 Typeface name: LinkedFont4
   188 Characters available (2):	V, n
   189 
   190 TWithoutLinkedFont.ttf
   191 Typeface name: WithoutLinkedFont
   192 Characters available:	It contains 191 characters U+0020-U+007E and U+00A0-U+00FF
   193 */	
   194 
   195 _LIT(KLinkedFont1, "LinkedFont1TT");
   196 _LIT(KLinkedFont2, "LinkedFont2TT");
   197 _LIT(KLinkedFont3, "LinkedFont3TT");
   198 _LIT(KLinkedFont4, "LinkedFont4TT");
   199 
   200 /** Memory test helper function to create different typefaces.  Function adds from 1 to four
   201  typefaces.
   202 */
   203 TInt CTLinkedFontsMemory::TestLinkedFontsCreateLinkedTypefacesL(const TDesC &aName,TInt aTypefaces)
   204 	{
   205 	TInt ret = KErrNone;
   206 
   207 	CLinkedTypefaceSpecification *typefaceSpec;
   208 
   209 	typefaceSpec = CLinkedTypefaceSpecification::NewLC(aName);
   210 	
   211 	CLinkedTypefaceGroup* group1;
   212 
   213 	group1 = CLinkedTypefaceGroup::NewLC(1); //may well leave, function trapped, groupID = 1
   214 	typefaceSpec->AddLinkedTypefaceGroupL(*group1);
   215 	CleanupStack::Pop(); //pop the group, now referenced via typeface
   216 	
   217 	CLinkedTypefaceElementSpec *elementSpec1;
   218 	elementSpec1 = CLinkedTypefaceElementSpec::NewLC(KLinkedFont1, 1);	
   219 	elementSpec1->SetCanonical(ETrue);
   220 	typefaceSpec->AddTypefaceAtBackL(*elementSpec1);
   221 	CleanupStack::Pop();  //pop the element, now referenced via typeface
   222 
   223 	// Create and add second linked typeface
   224 	if (aTypefaces>=2)
   225 		{
   226 		CLinkedTypefaceElementSpec *elementSpec2;
   227 		elementSpec2 = CLinkedTypefaceElementSpec::NewLC(KLinkedFont2, 1);	
   228 		typefaceSpec->AddTypefaceAtBackL(*elementSpec2);
   229 		CleanupStack::Pop();  //pop the element, now referenced via typeface
   230 		}
   231 
   232 	// Create and add third typeface to be linked
   233 	if (aTypefaces>=3)
   234 		{
   235 		CLinkedTypefaceElementSpec *elementSpec3;
   236 		elementSpec3 = CLinkedTypefaceElementSpec::NewLC(KLinkedFont3, 1);	
   237 		typefaceSpec->AddTypefaceAtBackL(*elementSpec3);
   238 		CleanupStack::Pop();  //pop the element, now referenced via typeface
   239 		}
   240 
   241 	// Create and add fourth typeface to be linked
   242 	if (aTypefaces>=4)
   243 		{
   244 		CLinkedTypefaceElementSpec *elementSpec4;
   245 		elementSpec4 = CLinkedTypefaceElementSpec::NewLC(KLinkedFont4, 1);	
   246 		TRAP(ret,typefaceSpec->AddTypefaceAtBackL(*elementSpec4));
   247 		CleanupStack::Pop();  //pop the element, now referenced via typeface
   248 		}
   249 
   250 	// Try registering the typeface
   251 	CFbsTypefaceStore* store = CFbsTypefaceStore::NewL(iDev);
   252 	CleanupStack::PushL(store);
   253 	typefaceSpec->RegisterLinkedTypefaceL(*store);
   254 	CleanupStack::PopAndDestroy(2, typefaceSpec); //store and typefaceSpec
   255 	return ret;
   256 	}
   257 
   258 /** This is the function which does the operations which are tested, called from OOM loop
   259  * in general the function does not return a low memory error; the function just leaves.
   260  * An exception may be for registration.
   261 */
   262 TInt CTLinkedFontsMemory::TestLinkedFontsMemory1L(TInt aTypefaces)
   263 	{
   264 	TInt ret = KErrNone;
   265 	
   266 	//these are for the linked typefaces
   267 	_LIT(KLinkedTypeface1, "LinkedMem1-1"); //has LinkedFont1
   268 	_LIT(KLinkedTypeface2, "LinkedMem1-2"); //has LinkedFont1, LinkedFont2
   269 	_LIT(KLinkedTypeface3, "LinkedMem1-3"); //has LinkedFont1, LinkedFont2, LinkedFont3
   270 	_LIT(KLinkedTypeface4, "LinkedMem1-4"); //has LinkedFont1, LinkedFont2, LinkedFont3, LinkedFont4	
   271 	
   272 	// Create canonical typeface to be linked
   273 
   274 	switch (aTypefaces)
   275 		{
   276 		case 1:
   277 			ret = TestLinkedFontsCreateLinkedTypefacesL(KLinkedTypeface1,1);
   278 			break;
   279 		case 2:
   280 			ret = TestLinkedFontsCreateLinkedTypefacesL(KLinkedTypeface2,2);
   281 			break;
   282 		case 3:
   283 			ret = TestLinkedFontsCreateLinkedTypefacesL(KLinkedTypeface3,3);
   284 			break;
   285 		case 4:
   286 			ret = TestLinkedFontsCreateLinkedTypefacesL(KLinkedTypeface4,4);
   287 			break;
   288 		default:
   289 			__ASSERT_ALWAYS(ETrue,Panic(EFntTypefaceIndexOutOfRange));
   290 			break;
   291 		}
   292 	
   293 	RDebug::Printf("%d TestLinkedFontsMemory1L(%d)", ret, aTypefaces);
   294 	return ret;
   295 	}
   296 
   297 /**
   298    @SYMTestCaseID			GRAPHICS-FNTSTORE-LINKEDFONTS-0102
   299 
   300    @SYMPREQ					PREQ2146
   301 
   302    @SYMREQ					REQ10924, REQ10925, REQ10926
   303 
   304    @SYMTestCaseDesc			To ensure that a linked typeface can be created and registered 
   305    							without memory leaks, under low memory conditions. 
   306 
   307    @SYMTestPriority			Critical
   308 
   309    @SYMTestStatus			Implemented
   310 
   311    @SYMTestActions			(1) Create an OOM loop,	setting this process's heap memory to fail.
   312    								testing the client side heap memory allocation.
   313 								In the loop create and register a linked typeface with 1-4
   314 								typefaces
   315 
   316    @SYMTestExpectedResults	(1) - There are no memory leaks, and the test exits the OOM loop.
   317 */
   318 void CTLinkedFontsMemory::TestLinkedFontsMemory1L()
   319 	{
   320 	TInt failures;
   321 
   322 	INFO_PRINTF1(_L("TestLinkedFontsMemory"));
   323 	TInt steps;
   324 	for (steps=1;steps<=4;steps++)
   325 		{
   326 
   327 		for (failures = 1; ; failures++)
   328 			{
   329 			__UHEAP_SETFAIL(RHeap::EDeterministic,failures);
   330 			__UHEAP_MARK;
   331 
   332 			// Test steps
   333 			TInt err;
   334 			TInt val = KErrNone;
   335 			TRAP(err, val= TestLinkedFontsMemory1L(steps));
   336 			__UHEAP_CHECK(0);
   337 			__UHEAP_RESET;
   338 			__UHEAP_MARKEND;
   339 
   340 			if (err == KErrNotFound)
   341 				{
   342 				INFO_PRINTF3(_L("Failure below occurred on Step %i; failure=%i"), steps, failures);
   343 				TEST(EFalse);
   344 				break;
   345 				}
   346 			
   347 			if ((err == KErrNone) && (val == KErrNone))
   348 				{
   349 				break;
   350 				}
   351 			}
   352 		}
   353 	}
   354 
   355 /**
   356    @SYMTestCaseID			GRAPHICS-FNTSTORE-LINKEDFONTS-0103
   357 
   358    @SYMPREQ					PREQ2146
   359 
   360    @SYMREQ					REQ10924, REQ10925, REQ10926
   361 
   362    @SYMTestCaseDesc			To ensure that a linked typeface can be created and fonts created
   363 							and used without memory leaks, under low memory conditions
   364 
   365    @SYMTestPriority			Critical
   366 
   367    @SYMTestStatus			Implemented
   368 
   369    @SYMTestActions			(1) Create an OOM loop, setting the server's heap memory to fail.
   370 								In the loop create and register a linked typeface with 1-4
   371 								typefaces.  
   372 							(2) Draw text with the four linked fonts from the four
   373 								linked typefaces.  
   374 							(3) Compare the text output against an unlinked font with the same 
   375 								character forms.
   376 
   377    @SYMTestExpectedResults	(1) - There are no memory leaks, and the test exits the OOM loop.
   378    							(2) - The text is draw successfully
   379    							(3) - The text compares identically.
   380 */	
   381 void CTLinkedFontsMemory::TestLinkedFontsMemory2()
   382 	{
   383 	TInt failures;
   384 
   385 	INFO_PRINTF1(_L("TestLinkedFontsMemory Server Heap"));
   386 
   387 	// Initially test for linked typeface creation
   388 	_LIT(name1, "typefaceNameMem2-1");
   389 	_LIT(name2, "typefaceNameMem2-2");
   390 	_LIT(name3, "typefaceNameMem2-3");
   391 	_LIT(name4, "typefaceNameMem2-4");
   392 
   393 	TInt steps;
   394 	for (steps=1;steps<=4;steps++)
   395 		{
   396 		INFO_PRINTF2(_L("Testing creating linked typeface %d typefaces"),steps);
   397 		for (failures = 1;;failures++)
   398 			{
   399 			// Try creating the typeface
   400 
   401 			TInt err;
   402 			TInt val = KErrNone;
   403 
   404 			SetHeapFail(RFbsSession::EHeapFailTypeServerMemory, failures);
   405 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //on
   406 			SetHeapCheck(RFbsSession::EHeapFailTypeHeapMemory); //on
   407 
   408 			switch(steps)
   409 				{
   410 				case 1:
   411 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name1, steps));
   412 					break;
   413 				case 2:
   414 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name2, steps));
   415 					break;
   416 				case 3:
   417 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name3, steps));
   418 					break;
   419 				case 4:
   420 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name4, steps));
   421 					break;
   422 				}
   423 
   424 			SetHeapReset(RFbsSession::EHeapFailTypeServerMemory);
   425 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //off
   426 			SetHeapCheck(RFbsSession::EHeapFailTypeHeapMemory); //off
   427 
   428 			//If the linked font cannot be created because the typefaces are not present or
   429 			//it has already been created then fail the test.
   430 			if (err == KErrNotFound || err == KErrAlreadyExists)
   431 				{
   432 				INFO_PRINTF1(_L("Failure below caused by missing linked font element file or linked font file already being generated."));
   433 				TEST(EFalse);
   434 				break;
   435 				}
   436 			
   437 			if ((val >= 0) && (err == KErrNone))
   438 				break;
   439 			}
   440 		}
   441 
   442 	INFO_PRINTF1(_L("Testing linked typeface font creation and usage"));
   443 	for(steps=1;steps<=4;steps++)
   444 		{
   445 		INFO_PRINTF2(_L("Testing font usage: linked font with %d fonts"),steps);
   446 		for (failures = 1; ; failures++)
   447 			{
   448 			TInt val = 0;
   449 #ifdef _DEBUG
   450 			TInt serverBefore=HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   451 			TInt countBefore =0;
   452 
   453 			RHeap *heap = (RHeap*)FontAndBitmapServerSharedHeap();
   454 			
   455 			if (heap)
   456 				{
   457 				__RHEAP_MARK(heap);
   458 				countBefore = heap->Count();
   459 				}
   460 			SetHeapFail(RFbsSession::EHeapFailTypeServerMemory, failures);
   461 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //on
   462 #endif
   463 
   464 			switch(steps)
   465 				{
   466 				case 1:
   467 					val=TestLinkedFontsMemory_Part2(name1);
   468 					break;
   469 				case 2:
   470 					val=TestLinkedFontsMemory_Part2(name2);
   471 					break;
   472 				case 3:
   473 					val=TestLinkedFontsMemory_Part2(name3);
   474 					break;
   475 				case 4:
   476 					val=TestLinkedFontsMemory_Part2(name4);
   477 					break;
   478 				}
   479 #ifdef _DEBUG
   480 			TInt serverAfter = HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   481 			TInt countAfter=0;
   482 			TInt badCell=0;
   483 			if (heap)
   484 				{
   485 				countAfter = heap->Count();
   486 				badCell = __RHEAP_MARKEND(heap);
   487 				}
   488 
   489 			SetHeapReset(RFbsSession::EHeapFailTypeServerMemory);
   490 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //off
   491 #endif
   492 			if (val == KErrNone)
   493 				{
   494 				if (steps == 4)
   495 					{
   496 					//do a comparison of the text
   497 					TestCompareText();
   498 					}
   499 				break;
   500 				}
   501 #ifdef _DEBUG
   502 			TEST(badCell == 0);
   503 			TEST(countBefore == countAfter);
   504 			TEST(serverBefore == serverAfter);
   505 #endif
   506 			}
   507 		}
   508 	}
   509 
   510 TInt CTLinkedFontsMemory::TestLinkedFontsMemory_Part2(const TDesC &aName)
   511 	{
   512 
   513 	TTypeface Typeface;
   514 	Typeface.iName = aName;
   515 	TFontSpec fs;
   516 	fs.iTypeface = Typeface;
   517 	fs.iHeight = 20;
   518 	CFbsFont* font = NULL;
   519 	TInt err1 = iDev->GetNearestFontToDesignHeightInPixels(font, fs);
   520 
   521 	if (font && err1 == KErrNone)
   522 		{
   523 		// Use the font
   524 		iGc->UseFont(font);
   525 		iGc->Clear();
   526 		iGc->DrawText(KDrawMe, TPoint(50, 50));
   527 		iGc->DiscardFont();
   528 		iDev->ReleaseFont(font);
   529 		}
   530 	else if (font == NULL)
   531 		{
   532 		TEST(err1==KErrNoMemory);
   533 		if (err1==KErrNone)
   534 			{
   535 			err1 = KErrGeneral;
   536 			}
   537 		}
   538 
   539 	return err1;
   540 	}
   541 
   542 /**
   543    @SYMTestCaseID			GRAPHICS-FNTSTORE-LINKEDFONTS-0104
   544 
   545    @SYMPREQ					PREQ2146
   546 
   547    @SYMREQ					REQ10924, REQ10925, REQ10926
   548 
   549    @SYMTestCaseDesc			To ensure that a linked typeface can be created and fonts created
   550 							and used under low memory conditions
   551 
   552    @SYMTestPriority			Critical
   553 
   554    @SYMTestStatus			Implemented
   555 
   556    @SYMTestActions			(1) This function checks that no memory has been retained from the
   557 								usage of a linked font.  This tests server heap and shared heap
   558 								memory.
   559 
   560    @SYMTestExpectedResults	(1) - There are no memory leaks.
   561 */
   562 void CTLinkedFontsMemory::TestLinkedFontsMemory3()
   563 	{
   564 	_LIT( KTest1, "typefaceNameMem2-1");
   565 	_LIT( KTest2, "typefaceNameMem2-2");
   566 	_LIT( KTest3, "typefaceNameMem2-3");
   567 	_LIT( KTest4, "typefaceNameMem2-4");
   568 
   569 #if _DEBUG
   570 		TInt serverBefore = HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   571 		// need to get heap from client side.
   572 		RHeap *heap = (RHeap*)FontAndBitmapServerSharedHeap();
   573 		TInt countBefore=0;
   574 		if (heap)
   575 			{
   576 			__RHEAP_MARK(heap);
   577 			countBefore = heap->Count();
   578 			}
   579 		TInt serverAfter;
   580 #endif
   581 	for (TInt numRuns = 0 ; numRuns <= 50 ; numRuns++)
   582 		{
   583 		for (TInt steps=1;steps<=4;steps++)
   584 			{
   585 			TTypeface Typeface;
   586 			switch (steps)
   587 				{
   588 				case 1:
   589 					Typeface.iName = KTest1;
   590 					break;
   591 				case 2:
   592 					Typeface.iName = KTest2;
   593 					break;
   594 				case 3:
   595 					Typeface.iName = KTest3;
   596 					break;
   597 				case 4:
   598 					Typeface.iName = KTest4;
   599 					break;
   600 				}
   601 			TFontSpec fs;
   602 			fs.iTypeface = Typeface;
   603 			fs.iHeight = 50;
   604 			TInt count;
   605 			CFbsFont *fnt[10];
   606 	
   607 			TInt err;
   608 			for (count = 0; count < 10; count++)
   609 				{
   610 				err = iDev->GetNearestFontToDesignHeightInPixels(fnt[count], fs);
   611 				//now try getting the same font twice, if count is 5
   612 				if(count!=5)
   613 					fs.iHeight++;
   614 				TEST(err == KErrNone);
   615 				}
   616 			for (count = 0; count < 10; count++)
   617 				{
   618 				iDev->ReleaseFont(fnt[count]);
   619 				}
   620 			}
   621 		}
   622 #ifdef _DEBUG
   623 		serverAfter = HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   624 		TInt countAfter=0;
   625 		TInt badCell=0;
   626 		if (heap)
   627 			{
   628 			countAfter = heap->Count();
   629 			badCell = __RHEAP_MARKEND(heap);
   630 			}
   631 		TEST(badCell == 0);
   632 		TEST (countAfter <= countBefore);
   633 
   634 			RDebug::Printf("serverAfter(%i) <= serverBefore(%i)", serverAfter, serverBefore);
   635 			INFO_PRINTF3(_L("serverAfter(%i) <= serverBefore(%i)"), serverAfter, serverBefore);
   636 
   637 		TEST(serverAfter <= serverBefore); // Note some true type tables are freed.
   638 #endif
   639 	}
   640 
   641 /**
   642    @SYMTestCaseID			GRAPHICS-FNTSTORE-LINKEDFONTS-0105
   643 
   644    @SYMPREQ					PREQ2146
   645 
   646    @SYMREQ					REQ10924, REQ10925, REQ10926
   647 
   648    @SYMTestCaseDesc			To ensure that a linked typeface can be created and fonts created
   649 							and used under low memory conditions of the font and bitmap
   650 							server shared heap.
   651 
   652    @SYMTestPriority			Critical
   653 
   654    @SYMTestStatus			Implemented
   655 
   656    @SYMTestActions			(1) Create an OOM loop, setting the shared heap memory to fail.
   657 								In the loop create and register a linked typeface with 1-4
   658 								typefaces.  Draw text with the four linked fonts from the four
   659 								linked typefaces.
   660 
   661    @SYMTestExpectedResults	(1) - There are no memory leaks, and the test exits the OOM loop.
   662 */
   663 void CTLinkedFontsMemory::TestLinkedFontsMemory4()
   664 	{
   665 
   666 	TInt failures;
   667 
   668 	INFO_PRINTF1(_L("TestLinkedFontsMemory Shared Heap"));
   669 
   670 	// Initially test for linked typeface creation
   671 	// Initially test for linked typeface creation
   672 	_LIT(name1, "typefaceNameMem4-1");
   673 	_LIT(name2, "typefaceNameMem4-2");
   674 	_LIT(name3, "typefaceNameMem4-3");
   675 	_LIT(name4, "typefaceNameMem4-4");
   676 
   677 	TInt steps;
   678 	for(steps=1;steps<=4;steps++)
   679 		{
   680 		INFO_PRINTF2(_L("Testing creating linked typeface %d typefaces"),steps);
   681 		for (failures = 1; ; failures++)
   682 			{
   683 			// Try creating the typeface
   684 
   685 			TInt err;
   686 			TInt val = KErrNone;
   687 
   688 			SetHeapFail(RFbsSession::EHeapFailTypeHeapMemory, failures);
   689 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //on
   690 			SetHeapCheck(RFbsSession::EHeapFailTypeHeapMemory); //on
   691 
   692 			switch(steps)
   693 				{
   694 				case 1:
   695 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name1, 1));
   696 					break;
   697 				case 2:
   698 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name2, 2));
   699 					break;
   700 				case 3:
   701 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name3, 3));
   702 					break;
   703 				case 4:
   704 					TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name4, 4));
   705 					break;
   706 				}
   707 
   708 			SetHeapReset(RFbsSession::EHeapFailTypeHeapMemory);
   709 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //off
   710 			SetHeapCheck(RFbsSession::EHeapFailTypeHeapMemory); //off
   711 
   712 			//If the linked font cannot be created because the typefaces are not present or
   713 			//it has already been created then fail the test.
   714 			if (err == KErrNotFound || err == KErrAlreadyExists)
   715 				{
   716 				INFO_PRINTF1(_L("Failure below caused by missing linked font element file or linked font file already being generated."));
   717 				TEST(EFalse);
   718 				break;
   719 				}
   720 			
   721 			if ((val >= 0) && (err == KErrNone))
   722 				break;
   723 			}
   724 		}
   725 
   726 	INFO_PRINTF1(_L("Testing linked typeface font creation and usage"));
   727 	for (steps=1;steps<=4;steps++)
   728 		{
   729 		INFO_PRINTF2(_L("Testing font usage: linked font with %d fonts"), steps);
   730 		for (failures = 1; ; failures++)
   731 			{
   732 			TInt val = 0;
   733 #ifdef _DEBUG
   734 			TInt serverBefore=HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   735 			RHeap *heap=(RHeap*)FontAndBitmapServerSharedHeap();
   736 			TInt countBefore = 0;
   737 			if (heap)
   738 				{
   739 				__RHEAP_MARK(heap);
   740 			 	countBefore = heap->Count();
   741 				}
   742 			SetHeapFail(RFbsSession::EHeapFailTypeHeapMemory, failures);
   743 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //on
   744 #endif
   745 			switch (steps)
   746 				{
   747 				case 1:
   748 					val = TestLinkedFontsMemory_Part2(name1);
   749 					break;
   750 				case 2:
   751 					val = TestLinkedFontsMemory_Part2(name2);
   752 					break;
   753 				case 3:
   754 					val = TestLinkedFontsMemory_Part2(name3);
   755 					break;
   756 				case 4:
   757 					val = TestLinkedFontsMemory_Part2(name4);
   758 					break;
   759 				}
   760 #ifdef _DEBUG
   761 			TInt serverAfter = HeapCount(RFbsSession::EHeapFailTypeServerMemory);
   762 			TInt countAfter =0;
   763 			TInt badCell = 0;
   764 			if (heap)
   765 				{
   766 				countAfter = heap->Count();
   767 				badCell = __RHEAP_MARKEND(heap);
   768 				}
   769 			SetHeapReset(RFbsSession::EHeapFailTypeHeapMemory);
   770 			SetHeapCheck(RFbsSession::EHeapFailTypeServerMemory); //off
   771 #endif
   772 			if (val == KErrNone)
   773 				{
   774 				if (steps == 4)
   775 					{
   776 					TestCompareText();
   777 					}
   778 				break;
   779 				}
   780 #ifdef _DEBUG
   781 			TEST(badCell == 0);
   782 			TEST (countBefore == countAfter);
   783 			TEST(serverBefore == serverAfter);
   784 #endif
   785 			}
   786 		}
   787 	}
   788 
   789 void CTLinkedFontsMemory::DisplayRamInfo(const TDesC& aTextInfo) const
   790 	{
   791 	TInt val;
   792 	HAL::Get(HALData::EMemoryRAMFree, val);
   793 	TBuf <80> buf;
   794 	_LIT(KTotalRamText, "RAM free: %d ");
   795 	buf.Format(KTotalRamText,val);
   796 	buf.Append(aTextInfo);
   797 	iStep->INFO_PRINTF1(buf);
   798 	}
   799 
   800 /**
   801    @SYMTestCaseID			GRAPHICS-FNTSTORE-LINKEDFONTS-0106
   802 
   803    @SYMPREQ					PREQ2146
   804 
   805    @SYMREQ					REQ10924, REQ10925, REQ10926
   806 
   807    @SYMTestCaseDesc			To test memory usage performance of linked font creation. 
   808    							Test memory usage performance of linked font character access.
   809 
   810    @SYMTestPriority			High
   811 
   812    @SYMTestStatus			Implemented
   813 
   814    @SYMTestActions			(1) Display available ram.  Create a linked typeface with 1-4 linked typeface
   815 								and display ram available.
   816 							(2) Create four linked fonts from the four linked typefaces.  After each step
   817 								display the available RAM.
   818 							(3) Use each of the four linked fonts in turn for drawing text, and at each
   819 								step display the available RAM.	
   820 
   821    @SYMTestExpectedResults	(1) (2) (3) RAM usage is displayed.  
   822    							There are no memory leaks.
   823 */
   824 void CTLinkedFontsMemory::TestMemoryUsage()
   825 	{
   826 
   827 	INFO_PRINTF1(_L("Test Memory Usage 1"));
   828 
   829 	// Initially test for linked typeface creation
   830 	// Initially test for linked typeface creation
   831 	_LIT(name0, "WithoutLinkedFontTT");
   832 	_LIT(name1, "typefaceMemUse-1");
   833 	_LIT(name2, "typefaceMemUse-2");
   834 	_LIT(name3, "typefaceMemUse-3");
   835 	_LIT(name4, "typefaceMemUse-4");
   836 
   837 	TInt steps;
   838 	_LIT(KPriorToTesting, "Prior to testing");
   839 
   840 	DisplayRamInfo(KPriorToTesting);
   841 
   842 	TInt err=KErrNone;
   843 	TInt val=KErrNone;
   844 	//get memory after registering a typeface
   845 	for(steps=1;steps<=4;steps++)
   846 		{
   847 		switch(steps)
   848 			{
   849 			case 1:
   850 				TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name1, 1));
   851 				_LIT(KOneTypeface, "Typeface with one typeface created");
   852 				DisplayRamInfo(KOneTypeface);
   853 				break;
   854 			case 2:
   855 				TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name2, 2));
   856 				_LIT(KTwoTypeface, "Typeface with two typefaces created");
   857 				DisplayRamInfo(KTwoTypeface);
   858 				break;
   859 			case 3:
   860 				TRAP(err, val = TestLinkedFontsCreateLinkedTypefacesL(name3, 3));
   861 				_LIT(KThreeTypeface, "Typeface with three typefaces created");
   862 				DisplayRamInfo(KThreeTypeface);
   863 				break;
   864 			case 4:
   865 				TRAP(err,val = TestLinkedFontsCreateLinkedTypefacesL(name4, 4));
   866 				_LIT(KFourTypeface, "Typeface with four typefaces created");
   867 				DisplayRamInfo(KFourTypeface);
   868 				break;
   869 			}
   870 		TEST(err == KErrNone);
   871 		TEST(val == KErrNone);
   872 		}
   873 
   874 	CFont* font[5];
   875 	memset (font,0, sizeof(CFont*)*5);
   876 
   877 	_LIT(KGettingFonts, "Now getting 5 fonts, the first one of which is not linked");
   878 	DisplayRamInfo(KGettingFonts);
   879 	//get memory after retrieving a font
   880 	for (steps=0;steps<5;steps++)
   881 		{
   882 		switch (steps)
   883 			{
   884 			case 0:
   885 				{
   886 				TTypeface typeface;
   887 				typeface.iName = name0;
   888 				TFontSpec fs;
   889 				fs.iTypeface = typeface;
   890 				fs.iHeight = 20;
   891 				err = iDev->GetNearestFontToDesignHeightInPixels(font[steps], fs);
   892 				TEST(err==KErrNone);
   893 				_LIT(KOneFont, "Non linked font typeface");
   894 				DisplayRamInfo(KOneFont);
   895 				}
   896 			break;
   897 
   898 			case 1:
   899 				{
   900 				TTypeface typeface;
   901 				typeface.iName = name1;
   902 				TFontSpec fs;
   903 				fs.iTypeface = typeface;
   904 				fs.iHeight = 20;
   905 				err = iDev->GetNearestFontToDesignHeightInPixels(font[steps], fs);
   906 				TEST(err==KErrNone);
   907 				_LIT(KOneFont, "LinkedFont one typeface");
   908 				DisplayRamInfo(KOneFont);
   909 				}
   910 			break;
   911 			case 2:
   912 				{
   913 				TTypeface typeface;
   914 				typeface.iName = name2;
   915 				TFontSpec fs;
   916 				fs.iTypeface = typeface;
   917 				fs.iHeight = 20;
   918 				err = iDev->GetNearestFontToDesignHeightInPixels(font[steps], fs);
   919 				TEST(err==KErrNone);
   920 				_LIT(KTwoFont, "LinkedFont two typefaces");
   921 				DisplayRamInfo(KTwoFont);
   922 				}
   923 			break;
   924 			case 3:
   925 				{
   926 				TTypeface typeface;
   927 				typeface.iName = name3;
   928 				TFontSpec fs;
   929 				fs.iTypeface = typeface;
   930 				fs.iHeight = 20;
   931 				err = iDev->GetNearestFontToDesignHeightInPixels(font[steps], fs);
   932 				TEST(err==KErrNone);
   933 				_LIT(KThreeFont, "LinkedFont three typefaces");
   934 				DisplayRamInfo(KThreeFont);
   935 				}
   936 			break;
   937 			case 4:
   938 				{
   939 				TTypeface typeface;
   940 				typeface.iName = name4;
   941 				TFontSpec fs;
   942 				fs.iTypeface = typeface;
   943 				fs.iHeight = 20;
   944 				err = iDev->GetNearestFontToDesignHeightInPixels(font[steps], fs);
   945 				TEST(err==KErrNone);
   946 				_LIT(KFourFont, "LinkedFont four typefaces");
   947 				DisplayRamInfo(KFourFont);
   948 				}
   949 			break;
   950 			}
   951 		}
   952 
   953 	//get memory after font usage
   954 	const TDesC* string[5];
   955 	_LIT(KString0,"A");
   956 	_LIT(KString1,"B");
   957 	_LIT(KString2,"F");
   958 	_LIT(KString3,"D");
   959 	_LIT(KString4,"V");
   960 	string[0]=&KString0();
   961 	string[1]=&KString1();
   962 	string[2]=&KString2();
   963 	string[3]=&KString3();
   964 	string[4]=&KString4();
   965 
   966 	_LIT(KWritingCharacter,"Now outputing a single character from each of the five fonts");
   967 	DisplayRamInfo(KWritingCharacter);
   968 	for (steps=0;steps<5;steps++)
   969 		{
   970 		// Use the font
   971 		iGc->UseFont(font[steps]);
   972 		iGc->Clear();
   973 		_LIT(KNowUsingFont,"Now using font");
   974 		DisplayRamInfo(KNowUsingFont);
   975 		iGc->DrawText(*string[steps], TPoint(100, 100));
   976 		TBuf <80> buf;
   977 		_LIT(KNumberOfSteps,"The font in use has %d linked fonts");
   978 		buf.Format(KNumberOfSteps, steps);
   979 		DisplayRamInfo(buf);
   980 		iGc->DiscardFont();
   981 		}
   982 
   983 	//release the fonts
   984 	for (steps=0;steps<5;steps++)
   985 		{
   986 		iDev->ReleaseFont(font[steps]);
   987 		}
   988 	_LIT(KTestEnd,"At the end of the memory test");
   989 	DisplayRamInfo(KTestEnd);
   990 	}
   991 
   992 void CTLinkedFontsMemory::RunTestCaseL( TInt aCurTestCase )
   993 	{
   994 		
   995 	((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
   996 	switch ( aCurTestCase )
   997 		{
   998 	case 1:
   999 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
  1000 		StartServer();
  1001 		if (!CheckRasterizerInstalledL())
  1002 			{
  1003 			TestComplete();
  1004 			return;
  1005 			}
  1006 		break;
  1007 	case 2:
  1008 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-LINKEDFONTS-0102"));
  1009 #ifdef _DEBUG
  1010 		//memory tests are not valid under DEBUG/ urel, and they will fail
  1011 		__UHEAP_MARK;
  1012 		// This is for client side memory
  1013 		TestLinkedFontsMemory1L();
  1014 		__UHEAP_MARKEND;
  1015 		__UHEAP_RESET;
  1016 #endif
  1017 		break;
  1018 	case 3:
  1019 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
  1020 #ifdef _DEBUG
  1021 		__UHEAP_MARK;
  1022 		//((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-LINKEDFONTS-0103"));
  1023 		// This is for server side memory
  1024 		//TestLinkedFontsMemory2(); //- test crashes the server (from the rasterizer)
  1025 		//TEST(EFalse);
  1026 		__UHEAP_MARKEND;
  1027 		__UHEAP_RESET;
  1028 #endif
  1029 		break;
  1030 	case 4:
  1031 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-LINKEDFONTS-0104"));
  1032 #ifdef _DEBUG
  1033 		__UHEAP_MARK;
  1034 		// This is to check that there are no leaks getting and releasing fonts
  1035 		//TestLinkedFontsMemory3();
  1036 		__UHEAP_MARKEND;
  1037 		__UHEAP_RESET;
  1038 #endif
  1039 		break;
  1040 	case 5:
  1041 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-LINKEDFONTS-0105"));
  1042 #ifdef _DEBUG
  1043 		__UHEAP_MARK;
  1044 		// This is for heap memory
  1045 		//TestLinkedFontsMemory4();
  1046 		__UHEAP_MARKEND;
  1047 		__UHEAP_RESET;
  1048 #endif
  1049 		break;
  1050 
  1051 	case 6:
  1052 #ifdef _DEBUG
  1053 		__UHEAP_MARK;
  1054 #endif		
  1055 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-LINKEDFONTS-0106"));
  1056 		TestMemoryUsage();
  1057 #ifdef _DEBUG		
  1058 		__UHEAP_MARKEND;
  1059 		__UHEAP_RESET;
  1060 #endif		
  1061 		break;	
  1062 	case 7:
  1063 		((CTLinkedFontsMemoryStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
  1064 		((CTLinkedFontsMemoryStep*)iStep)->CloseTMSGraphicsStep();
  1065 		TestComplete();
  1066 		break;
  1067 		}
  1068 	((CTLinkedFontsMemoryStep*)iStep)->RecordTestResultL();
  1069 	}
  1070 
  1071 // --------------
  1072 __CONSTRUCT_STEP__(LinkedFontsMemory)