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