fontservices/fontstore/tfs/T_GetNearestFont.cpp
changeset 0 1fb32624e06b
child 51 a7c938434754
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     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 * T_NearestFont.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @test
       
    23  @internalComponent Internal Symbian test code
       
    24 */
       
    25 
       
    26 #include "T_GetNearestFont.h"
       
    27 #include <hal.h>
       
    28 
       
    29 _LIT( KFontNotAvailable, "NotAvailableOnSystem" );
       
    30 _LIT( KTypefaceName, "DejaVu Sans Condensed" );
       
    31 _LIT( KFontAlias, "Alias");
       
    32 
       
    33 class CTGetNearestFont : public CTGraphicsBase
       
    34 	{
       
    35 public:
       
    36 	CTGetNearestFont(CTestStep* aStep);
       
    37 	virtual ~CTGetNearestFont();
       
    38 
       
    39 protected:
       
    40 //from 	CTGraphicsStep
       
    41 	virtual void RunTestCaseL(TInt aCurTestCase);
       
    42 	virtual void ConstructL();
       
    43 private:
       
    44 	void TestINC064346();
       
    45 	void AliasedFontCreationL();
       
    46 	void TestFontStoreL();
       
    47 	void TestSystemDefaultFont();
       
    48 	
       
    49 	CFbsTypefaceStore* iTfs;	
       
    50 	};
       
    51 
       
    52 CTGetNearestFont::CTGetNearestFont(CTestStep* aStep) :
       
    53 	CTGraphicsBase(aStep)
       
    54 	{
       
    55 	
       
    56 	}
       
    57 
       
    58 void CTGetNearestFont::ConstructL()
       
    59 	{
       
    60 	iTfs = CFbsTypefaceStore::NewL(NULL);	
       
    61 	}	
       
    62 	
       
    63  CTGetNearestFont::~CTGetNearestFont()
       
    64  	{
       
    65  	delete iTfs; 	
       
    66  	}
       
    67 	
       
    68 void CTGetNearestFont::RunTestCaseL( TInt aCurTestCase )
       
    69 	{	
       
    70 	((CTGetNearestFontStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
       
    71 	switch ( aCurTestCase )
       
    72 		{
       
    73 	case 1:
       
    74 		((CTGetNearestFontStep*)iStep)->SetTestStepID(_L("GRAPHICS-INC064346-0001"));
       
    75 		INFO_PRINTF1(_L("TestINC064346 Started"));
       
    76 		TestINC064346();
       
    77 		INFO_PRINTF1(_L("TestINC064346 Finished"));
       
    78 		break;
       
    79 		
       
    80 	case 2:
       
    81 		((CTGetNearestFontStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0005"));
       
    82 		INFO_PRINTF1(_L("TestFontStore Started"));
       
    83 		TestFontStoreL();
       
    84 		INFO_PRINTF1(_L("TestFontStore Finished"));
       
    85 		break;	
       
    86 		
       
    87 	case 3:
       
    88 		((CTGetNearestFontStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0006"));
       
    89 		INFO_PRINTF1(_L("Test System default font Started"));
       
    90 		TestSystemDefaultFont();
       
    91 		INFO_PRINTF1(_L("Test System default font Finished"));
       
    92 	break;
       
    93 		
       
    94 	case 4:
       
    95 		((CTGetNearestFontStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0051"));
       
    96 		INFO_PRINTF1(_L("AliasedFontCreationL Started"));
       
    97 		AliasedFontCreationL();
       
    98 		INFO_PRINTF1(_L("AliasedFontCreationL Finished"));
       
    99 		break;
       
   100 
       
   101 	case 5:
       
   102 		((CTGetNearestFontStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
       
   103 		((CTGetNearestFontStep*)iStep)->CloseTMSGraphicsStep();
       
   104 		TestComplete();		
       
   105 		break;
       
   106 		}
       
   107 	((CTGetNearestFontStep*)iStep)->RecordTestResultL();
       
   108 	}
       
   109 
       
   110 void CTGetNearestFont::TestINC064346()
       
   111 	{
       
   112 	/**
       
   113 	@SYMTestCaseID GRAPHICS-INC064346-0001
       
   114 	@SYMTestType UT
       
   115 	@SYMTestPriority Medium
       
   116 	@SYMDEF INC064346
       
   117 	@SYMTestCaseDesc Tests that the correct font height is returned when matching with a superscript font.
       
   118 	@SYMTestActions Get nearest font to design height for a font with the following parameters:
       
   119 		Font name must be specified that is a font that does not exist on the system.
       
   120 		Font must be superscript, proportional and serif.
       
   121 	@SYMTestExpectedResults The correct height must be returned for the nearest font found which is checked with the expected value.
       
   122 	*/
       
   123 	TInt ret = KErrGeneral;
       
   124 	CFont* font = NULL;	
       
   125 	TFontSpec spec(KFontNotAvailable, 15);
       
   126 	
       
   127 	spec.iFontStyle = TFontStyle(EPostureUpright, EStrokeWeightNormal, EPrintPosSuperscript);
       
   128 	spec.iTypeface.SetIsProportional(ETrue);
       
   129 	spec.iTypeface.SetIsSerif(ETrue);		
       
   130 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);		
       
   131 	TEST(ret == KErrNone);
       
   132 	INFO_PRINTF2(_L("Font height = %d"), font->HeightInPixels());		
       
   133 	TEST(10 == font->HeightInPixels());	
       
   134 	}
       
   135 
       
   136 void CTGetNearestFont::TestFontStoreL()
       
   137 	{
       
   138 	/**
       
   139 	@SYMTestCaseID GRAPHICS-FNTSTORE-0005
       
   140 	@SYMPREQ PREQ1228
       
   141 	@SYMREQ REQ5921
       
   142 	@SYMTestCaseDesc Tests the font store optimisation changes for reduce the font load time
       
   143 	@SYMTestPriority High
       
   144 	@SYMTestStatus Implemented
       
   145 	@SYMTestActions Get the nearest font to the design height for a font which exists and one which does not exists
       
   146 	@SYMTestExpectedResults Time taken for fetching an existing font should be less then time take for fetching a non existent font.
       
   147 							In addition fetching a non existent font should take almost the same time in principle, as taken by fetching 
       
   148 							n existing with old method (as old search method checks all existing fonts even if it finds one in between 
       
   149 							during search procedure).
       
   150 	*/
       
   151 	CFont* font = NULL;	
       
   152 	TFontSpec spec(KFontNotAvailable, 15);
       
   153 	
       
   154 	spec.iHeight = 15;
       
   155 	spec.iTypeface.iName = KFontNotAvailable;
       
   156 	spec.iFontStyle = TFontStyle(EPostureUpright, EStrokeWeightNormal, EPrintPosSuperscript);
       
   157 	spec.iTypeface.SetIsProportional(ETrue);		
       
   158 	
       
   159 	//Set the Bold and Italic attribute for the font
       
   160 	TOpenFontSpec openFontSpec(spec);
       
   161 	openFontSpec.SetBold(ETrue);
       
   162 	openFontSpec.SetItalic(ETrue);
       
   163 	openFontSpec.GetTFontSpec(spec);
       
   164 	
       
   165 	TInt64 		diffTime[2];
       
   166 		
       
   167 	//Run it twice with same font specifications except different typeface name
       
   168 	//First iteration for Non Existent font and second iteration for Existent font
       
   169 	for (TInt cntr = 0; cntr < 2; cntr++)
       
   170 		{		 	
       
   171 		TInt 		freq = 0;							
       
   172 		
       
   173 		TUint32	starttime = User::FastCounter();
       
   174 		TInt ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   175 		TUint32 endtime = User::FastCounter();
       
   176 		TEST(ret == KErrNone);
       
   177 		
       
   178 		User::LeaveIfError(HAL::Get(HALData::EFastCounterFrequency, freq));
       
   179 		TUint32 diff = endtime - starttime;
       
   180 		diffTime[cntr] = (1000000 * TInt64(diff)) / (TInt64)freq;			
       
   181 
       
   182 		TFontSpec returnedSpecs = font->FontSpecInTwips();
       
   183 		INFO_PRINTF3(_L("Font Desired  : Name  = %S Height = %d "), &spec.iTypeface.iName, spec.iHeight);
       
   184 		INFO_PRINTF4(_L("Font Returned : Name = %S Height = %d Time = %d Microseconds"), &returnedSpecs.iTypeface.iName, font->HeightInPixels(),TUint32(diffTime[cntr]));
       
   185 		
       
   186 		//Change the font spec to specify available typeface			
       
   187 		spec.iTypeface.iName = KTypefaceName;
       
   188 		
       
   189 		iTfs->ReleaseFont(font);				
       
   190 		}
       
   191 
       
   192 	TEST(diffTime[1] < diffTime[0]);
       
   193 	INFO_PRINTF2(_L("OPTIMISATION Factor %f"), (TReal(1)-(TReal(diffTime[1])/TReal(diffTime[0]))));	
       
   194 	}
       
   195 
       
   196 /**
       
   197 @SYMTestCaseID GRAPHICS-FNTSTORE-0006
       
   198 @SYMTestPriority Critical
       
   199 @SYMREQ REQ6158
       
   200 
       
   201 @SYMTestCaseDesc 
       
   202 Default system font
       
   203 
       
   204 @SYMTestActions 
       
   205 1 - Test without setting system default font
       
   206 2 - Set the system default font to typeface 1
       
   207 3 - Set the system default font to typeface 2
       
   208 4 - Set the system default font to an empty descriptor 
       
   209 5 - Set the system default font to large descriptor 
       
   210 6 - Set the system default font to a font that does not exist 
       
   211 7 - Set system font to an alias font 
       
   212 
       
   213 @SYMTestExpectedResults 
       
   214 The following applies when the typeface name is empty when requesting a font:
       
   215 If a default system typeface is not set then the nearest font to the specified font will be returned.
       
   216 If a default system typeface name is set then this will be the returned font.
       
   217 If the character length of the system typeface is greater than KMaxTypefaceNameLength then a KErrTooBig leave will occur.
       
   218 */
       
   219 void CTGetNearestFont::TestSystemDefaultFont()
       
   220 	{	
       
   221 	const TInt numTypefaces = iTfs->NumTypefaces();
       
   222 	TEST(numTypefaces >= 2);	// Must have at least 2 typefaces
       
   223 	
       
   224 	// Get the first typeface
       
   225 	TTypefaceSupport tfSupport1;
       
   226 	iTfs->TypefaceSupport(tfSupport1, 0);
       
   227 	TPtrC typeface1 = tfSupport1.iTypeface.iName;
       
   228 	
       
   229 	// Get the second typeface
       
   230 	TTypefaceSupport tfSupport2;
       
   231 	iTfs->TypefaceSupport(tfSupport2, 1);
       
   232 	TPtrC typeface2 = tfSupport2.iTypeface.iName;
       
   233 	
       
   234 	// Test without setting system default font and the returned font should not be typeface1 or typeface2
       
   235 	CFont* font = NULL;	
       
   236 	TFontSpec spec(KNullDesC, 15);		
       
   237 	spec.iTypeface.SetIsProportional(ETrue);
       
   238 	spec.iTypeface.SetIsSerif(ETrue);		
       
   239 	TInt ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   240 	TEST(ret == KErrNone);	
       
   241 	TFontSpec returnedSpecs = font->FontSpecInTwips();
       
   242 	TEST(returnedSpecs.iTypeface.iName != typeface1 && returnedSpecs.iTypeface.iName != typeface2);
       
   243 	iTfs->ReleaseFont(font);
       
   244 		
       
   245 	// Now set the system default font and the returned font should be typeface1
       
   246 	iTfs->SetSystemDefaultTypefaceNameL(typeface1);
       
   247 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   248 	TEST(ret == KErrNone);	
       
   249 	returnedSpecs = font->FontSpecInTwips();
       
   250 	TEST(returnedSpecs.iTypeface.iName == typeface1);
       
   251 	iTfs->ReleaseFont(font);
       
   252 	
       
   253 	// Now set the system default font and the returned font should be typeface2
       
   254 	iTfs->SetSystemDefaultTypefaceNameL(typeface2);
       
   255 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   256 	TEST(ret == KErrNone);	
       
   257 	returnedSpecs = font->FontSpecInTwips();
       
   258 	TEST(returnedSpecs.iTypeface.iName == typeface2);
       
   259 	iTfs->ReleaseFont(font);
       
   260 	
       
   261 	// Now set the system default font to an empty descriptor and the font returned should not be typeface1 or typeface2
       
   262 	iTfs->SetSystemDefaultTypefaceNameL(KNullDesC);
       
   263 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   264 	TEST(ret == KErrNone);	
       
   265 	returnedSpecs = font->FontSpecInTwips();
       
   266 	TEST(returnedSpecs.iTypeface.iName != typeface1 && returnedSpecs.iTypeface.iName != typeface2);
       
   267 	iTfs->ReleaseFont(font);
       
   268 	
       
   269 	// Set the system default font to large descriptor and we should get an error
       
   270 	TRAP(ret, iTfs->SetSystemDefaultTypefaceNameL(_L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")));
       
   271 	TEST(ret == KErrTooBig);		
       
   272 	
       
   273 	// Now set the system default font to a font that does not exist and the font returned should not be typeface 1 or typeface2
       
   274 	_LIT( KFontNotExisting, "FontDoesNotExist" );
       
   275 	iTfs->SetSystemDefaultTypefaceNameL(KFontNotExisting);
       
   276 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   277 	TEST(ret == KErrNone);	
       
   278 	returnedSpecs = font->FontSpecInTwips();
       
   279 	TEST(returnedSpecs.iTypeface.iName != typeface1 && returnedSpecs.iTypeface.iName != typeface2);
       
   280 	iTfs->ReleaseFont(font);	
       
   281 	
       
   282 	// Now set the system default font and set an alias on typeface1 - the returned font should still be typeface1
       
   283 	iTfs->SetSystemDefaultTypefaceNameL(typeface1);
       
   284 	iTfs->SetFontNameAliasL(typeface1, typeface2);
       
   285 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   286 	TEST(ret == KErrNone);	
       
   287 	returnedSpecs = font->FontSpecInTwips();
       
   288 	TEST(returnedSpecs.iTypeface.iName == typeface1);
       
   289 	iTfs->ReleaseFont(font);
       
   290 	
       
   291 	// Now set the system default font to an aliased font - the returned font should still be typeface1
       
   292 	iTfs->SetSystemDefaultTypefaceNameL(KFontAlias);
       
   293 	iTfs->SetFontNameAliasL(KFontAlias, typeface1);
       
   294 	ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   295 	TEST(ret == KErrNone);
       
   296 	returnedSpecs = font->FontSpecInTwips();
       
   297 	TEST(returnedSpecs.iTypeface.iName == typeface1);
       
   298 	iTfs->ReleaseFont(font);
       
   299 
       
   300 	// tear down the default font
       
   301 	iTfs->SetSystemDefaultTypefaceNameL(KNullDesC);
       
   302 	}
       
   303 
       
   304 /**
       
   305 @SYMTestCaseID GRAPHICS-FNTSTORE-0051
       
   306 @SYMTestType UT
       
   307 @SYMTestPriority Medium
       
   308 @SYMDEF INC127988
       
   309 @SYMTestCaseDesc Tests that an alias does not prevent a real font from being loaded.
       
   310 @SYMTestActions Create an alias using the name of a real font. Get the real font.  
       
   311 @SYMTestExpectedResults Ensure the real font, not the aliased font is returned.
       
   312 */
       
   313 void CTGetNearestFont::AliasedFontCreationL()
       
   314 	{
       
   315 	TInt numTypefaces = iTfs->NumTypefaces();
       
   316 
       
   317 	TTypefaceSupport tfSupport;
       
   318 
       
   319 	// Get the first different typeface
       
   320 	while (--numTypefaces)
       
   321 		{
       
   322 		iTfs->TypefaceSupport(tfSupport, numTypefaces);
       
   323 		if (tfSupport.iTypeface.iName != KTypefaceName)
       
   324 			{
       
   325 			break;
       
   326 			}
       
   327 		}
       
   328 
       
   329 	// Make sure we aren't at the end of the list
       
   330 	TEST(numTypefaces);
       
   331 
       
   332 	TPtrC typeface = tfSupport.iTypeface.iName;
       
   333 
       
   334 	// Alias the other font with the same name as the original
       
   335 	iTfs->SetFontNameAliasL(KTypefaceName, typeface);
       
   336 
       
   337 	// Request the original font (making sure we don't get the aliased font back)
       
   338 	CFont* font = NULL;
       
   339 	TFontSpec spec(KTypefaceName, 0);
       
   340 	TInt ret = iTfs->GetNearestFontToDesignHeightInPixels(font, spec);
       
   341 	TEST(ret == KErrNone);
       
   342 	TFontSpec returnedSpecs = font->FontSpecInTwips();
       
   343 	TEST(returnedSpecs.iTypeface.iName == KTypefaceName);
       
   344 	iTfs->ReleaseFont(font);
       
   345 	}
       
   346 
       
   347 //--------------
       
   348 __CONSTRUCT_STEP__(GetNearestFont)