Update contrib.
2 * Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <graphics/openfontrasterizer.h>
24 _LIT(KEonFontFileName,"z:\\resource\\fonts\\eon14.gdr");
26 _LIT(KEonFontFileName,"z:\\resource\\fonts\\eon.gdr");
29 // 'dummy' open fonts, as used by T_FSOPEN
30 _LIT(KFontDummy,"z:\\PlatTest\\Graphics\\TestData\\dummy_fonts\\dummy");
31 //_LIT(KFontDummy_b,"z:\\PlatTest\\Graphics\\TestData\\dummy_fonts\\dummy_b");
32 //_LIT(KFontDummy_i,"z:\\PlatTest\\Graphics\\TestData\\dummy_fonts\\dummy_i");
33 //_LIT(KFontDummy_bi,"z:\\PlatTest\\Graphics\\TestData\\dummy_fonts\\dummy_bi");
37 CTFntMem::CTFntMem(CTestStep* aStep):
38 CTGraphicsBase(aStep),
41 iHeap = UserHeap::ChunkHeap(NULL,0x80000,0x80000);
42 INFO_PRINTF1(_L("FontStore"));
45 void CTFntMem::RunTestCaseL(TInt aCurTestCase)
47 ((CTFntMemStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
51 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0024"));
52 TRAPD(err,TestNewL());
53 TEST(err == KErrNone);
55 case 2: // OOM handling for bitmap fonts
56 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0025"));
57 TRAP(err,TestAddFileL(KEonFontFileName));
58 TEST(err == KErrNone);
60 case 3: // OOM handling for open fonts
61 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0025"));
62 TRAP(err,TestAddFileL(KFontDummy));
63 TEST(err == KErrNone);
65 case 5: // OOM handling for second reference to a bitmap font
66 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0026"));
67 TRAP(err,TestAddFileTwiceL(KEonFontFileName));
68 TEST(err == KErrNone);
70 case 6: // OOM handling for second reference to an open font
71 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0026"));
72 TRAP(err,TestAddFileTwiceL(KFontDummy));
73 TEST(err == KErrNone);
76 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-FNTSTORE-0027"));
77 TRAP(err,TestGetNearestFontToDesignHeightInTwipsL());
78 TEST(err == KErrNone);
81 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-NearestOpenFontLeaksDEF095184-0001"));
82 TRAP(err,TestGetNearestOpenFontLeaksL());
83 TEST(err == KErrNone);
86 ((CTFntMemStep*)iStep)->SetTestStepID(_L("GRAPHICS-CTFbs-TestDuplicateFontFileEntries-0002"));
87 TRAP(err,TestDuplicateBitmapFontFileEntriesL());
88 TEST(err == KErrNone);
91 ((CTFntMemStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
92 ((CTFntMemStep*)iStep)->CloseTMSGraphicsStep();
96 ((CTFntMemStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
99 ((CTFntMemStep*)iStep)->RecordTestResultL();
104 GRAPHICS-FNTSTORE-0024
107 Simulates out of memory errors of the heap when trying to allocate
108 memory for a CFontStore object.
111 1. Cancels simulated heap allocation failure for the current thread's heap.
112 2. Simulates heap allocation failure for the current thread's heap.
113 3. Marks the start of checking the current thread's heap.
114 4. In a loop, tries to create a new CFontStore object with a pointer to
115 the heap class used for memory allocation.
116 5. Checking is done to see if the CFontStore was allocated or
117 if out of memory. The loop is broken when an object is created correctly.
119 @SYMTestExpectedResults
122 void CTFntMem::TestNewL()
124 INFO_PRINTF1(_L("NewL"));
126 TInt ret=KErrGeneral;
130 while (ret!=KErrNone)
133 __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
135 TRAP(ret,fs=CFontStore::NewL(iHeap));
136 RDebug::Print(_L("fs %d failrate %d, error %d\n"),fs,failRate, ret);
137 if (ret==KErrNoMemory)
143 else if(ret==KErrNone)
148 __UHEAP_SETFAIL(RHeap::ENone,failRate);
151 User::Panic(_L("Unknown error"),ret);
158 GRAPHICS-FNTSTORE-0025
161 Tests the Out Of Memory handling for bitmap- and open fonts.
164 1. Finds or creates a font file object to support a font file.
165 If an appropriate font file object exists then no new open font file is created.
166 In this case the reference count of the font file object is incremented.
167 2. Allocates the object from the heap and then initialises its contents
169 3. Installs and takes ownership of an Open Font rasterizer
170 4. Releases a hold on one or all font file objects (COpenFontFiles or CFontStoreFiles)
171 If aFileUid identifies a font file object, then the reference count for this
172 object is decremented. If this brings the reference count down to zero then
173 the font file object is removed from the font store, as well as the fonts
174 and typefaces associated with this file.
175 If, on the other hand, aFileUid's value is NULL, then all font file objects
176 are removed, along with all fonts and typefaces in the font store.
177 5. Simulates heap allocation failure for the current thread's heap.
178 6. Tries to add a font file to the font store and checks for memory allocation errors.
180 @SYMTestExpectedResults
183 void CTFntMem::TestAddFileL(const TDesC& aFileName)
185 INFO_PRINTF2(_L("AddFileL(\"%S\")"), &aFileName);
187 CFontStore *fs=CFontStore::NewL(iHeap);
189 // Install the dummy rasterizer - used for 'Open Font's
190 COpenFontRasterizer* r = CDummyRasterizer::NewL();
191 CleanupStack::PushL(r);
192 fs->InstallRasterizerL(r);
195 (void)fs->AddFileL(aFileName);
196 fs->RemoveFile(KNullUid);
197 TInt ret=KErrGeneral;
199 while (ret!=KErrNone)
203 __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
204 TRAP(ret, (void)fs->AddFileL(aFileName));
205 RDebug::Print(_L("NumTypefaces %d failrate %d, error %d\n"),fs->NumTypefaces(), failRate, ret);
206 if (ret==KErrNoMemory)
208 fs->RemoveFile(KNullUid);
211 TEST(fs->NumTypefaces()==0);
213 else if(ret==KErrNone)
215 fs->RemoveFile(KNullUid);
219 __UHEAP_SETFAIL(RHeap::ENone,failRate);
222 User::Panic(_L("Unknown error"),ret);
229 GRAPHICS-FNTSTORE-0026
232 Tests the Out Of Memory handling for second reference to a bitmap font and open fonts.
235 1. Finds or creates a font file object to support a font file.
236 If an appropriate font file object exists then no new open font file is created.
237 In this case the reference count of the font file object is incremented.
238 2. Allocates the object from the heap and then initialises its contents.
240 3. Installs and takes ownership of an Open Font rasterizer.
241 4. Loads a font file.
242 5. Simulates heap allocation failure for the current thread's heap.
243 6. Tries to add a font file a second time to the font store and checks for memory allocation errors.
244 7. Removes all font file objects.
246 @SYMTestExpectedResults
249 void CTFntMem::TestAddFileTwiceL(const TDesC& aFileName)
251 INFO_PRINTF2(_L("AddFileTwiceL(\"%S\")"), &aFileName);
253 CFontStore *fs=CFontStore::NewL(iHeap);
255 // Install the dummy rasterizer - used for 'Open Font's
256 COpenFontRasterizer* r = CDummyRasterizer::NewL();
257 CleanupStack::PushL(r);
258 fs->InstallRasterizerL(r);
261 // load the font file
262 (void)fs->AddFileL(aFileName);
263 TInt ret=KErrGeneral;
265 while (ret!=KErrNone)
267 __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
269 TRAP(ret, (void)fs->AddFileL(aFileName));
270 RDebug::Print(_L("NumTypefaces %d failrate %d, error %d\n"),fs->NumTypefaces(), failRate, ret);
272 // neither success or failure should use more memory
276 if ( (ret != KErrNoMemory) && (ret != KErrNone) )
278 User::Panic(_L("Unknown error"),ret);
283 fs->RemoveFile(KNullUid);
289 GRAPHICS-FNTSTORE-0027
292 Tests the function GetNearestFontToDesignHeightInTwips in Out Of Memory
296 1. Creates a new CFontStore object.
297 2. Adds a font file to the font store.
298 3. Creates a bitmap font object.
299 4. Gets the nearest font to design height in twips of the DejaVu Sans Condensed font.
300 5. Releases a hold on one or all font file objects (COpenFontFiles or CFontStoreFiles)
301 If aFileUid identifies a font file object, then the reference count for this
302 object is decremented. If this brings the reference count down to zero then
303 the font file object is removed from the font store, as well as the fonts
304 and typefaces associated with this file.
305 If, on the other hand, aFileUid's value is NULL, then all font file objects
306 are removed, along with all fonts and typefaces in the font store.
307 6. Simulates heap allocation failure for the current thread's heap.
308 7. Tries to get the nearest font to design height in twips for the font and
309 checks for memory allocation errors.
310 8. Deallocates the CFontStore object.
312 @SYMTestExpectedResults
315 void CTFntMem::TestGetNearestFontToDesignHeightInTwipsL()
317 INFO_PRINTF1(_L("GetNearestFontToDesignHeightInTwips"));
318 CFontStore *fs=CFontStore::NewL(iHeap);
319 (void)fs->AddFileL(KEonFontFileName);
322 TFontSpec spec(_L("DejaVu Sans Condensed"),200);
324 ret=fs->GetNearestFontToDesignHeightInTwips((CFont *&) font,spec);
326 if (ret!=KErrNone || font==NULL)
327 return; //otherwise the memory faulting test loop will deadlock!
328 fs->ReleaseFont(font);
333 __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
337 ret=fs->GetNearestFontToDesignHeightInTwips((CFont *&) font,spec);
338 RDebug::Print(_L("Font %d failrate %d\n"),font,failRate);
339 if (ret==KErrNoMemory)
342 __RHEAP_MARKEND(iHeap);
345 else if(ret==KErrNone)
347 fs->ReleaseFont(font);
349 __RHEAP_MARKEND(iHeap);
351 __UHEAP_SETFAIL(RHeap::ENone,failRate);
354 User::Panic(_L("Unknown error"),ret);
362 @SYMTestCaseID GRAPHICS-NearestOpenFontLeaksDEF095184-0001
370 Tests for leak case loading both nearest open and bitmap font.
371 The first call to GetNearestFontToDesignHeightInTwips loads the bitmap font.
372 The looped calls change the memory fault frequency until this succeeds,
373 but the open font is returned instead, and then until the bitmap font is again returned.
374 The test fontspec is specially chosen to cause these different results,
375 and the test code is not working if the differing results are not fetched.
377 @SYMTestExpectedResults
378 failrate 1: KErrNoMemory
379 failrate 2: Same font returned
380 failrate 3: Different font returned
381 failrate 4: Same font returned
383 Note that the exact results may change fail rates
384 if underlying changes are made to the font store.
387 void CTFntMem::TestGetNearestOpenFontLeaksL()
389 INFO_PRINTF1(_L("TestGetNearestOpenFontLeaksL"));
390 CFontStore *fs=CFontStore::NewL(iHeap);
391 CleanupStack::PushL(fs);
392 // Install the dummy rasterizer - used for 'Open Font's
393 COpenFontRasterizer* r = CDummyRasterizer::NewL();
394 CleanupStack::PushL(r);
395 fs->InstallRasterizerL(r);
396 //Need one of each font flavour
397 (void)fs->AddFileL(KFontDummy);
398 (void)fs->AddFileL(KEonFontFileName);
399 CleanupStack::Pop(r);
400 CleanupStack::Pop(fs);
401 bool differentResultFetched=false;
402 CBitmapFont *firstFont;
404 //Manufacture this font so that the bitmap font is favoured
405 //Note that the best I could do was get them equal, but that is enough to get bitmap chosen!
406 //Blank name is required to allow the dummy open font to be selected by similarity
407 TFontSpec reqSpec(_L(""),190);
408 reqSpec.iFontStyle.SetBitmapType(EMonochromeGlyphBitmap);
411 //first run - tests that the code actually works before faulting the memory
412 ret=fs->GetNearestFontToDesignHeightInTwips((CFont *&) firstFont,reqSpec);
414 TEST(firstFont!=NULL);
415 if (ret!=KErrNone || firstFont==NULL)
417 return; //otherwise the memory faulting test loop will deadlock!
421 firstSpec=firstFont->FontSpecInTwips();
422 fs->ReleaseFont(firstFont);
426 while(ret!=KErrNone || (failRate<30 && !differentResultFetched))
428 CBitmapFont *font=NULL;
430 __RHEAP_SETFAIL(iHeap,RHeap::EDeterministic,failRate);
433 TFontSpec spec(reqSpec);
434 ret=fs->GetNearestFontToDesignHeightInTwips((CFont *&) font,reqSpec);
435 if (ret==KErrNoMemory)
437 RDebug::Print(_L("failrate %d: KErrNoMemory\n"),failRate);
440 else if(ret==KErrNone)
445 TFontSpec spec=font->FontSpecInTwips();
446 if (!(spec==firstSpec))
448 differentResultFetched=true;
449 ret=KErrNoMemory; //actually there was a handled memory fault
450 RDebug::Print(_L("failrate %d: Different font returned\n"),failRate);
454 RDebug::Print(_L("failrate %d: Same font returned\n"),failRate);
459 RDebug::Print(_L("failrate %d: Error: NULL font returned\n"),failRate);
461 fs->ReleaseFont(font);
466 User::Panic(_L("Unexpected error"),ret);
468 __RHEAP_CHECK(iHeap,0);
469 __RHEAP_MARKEND(iHeap);
471 __RHEAP_SETFAIL(iHeap,RHeap::ENone,failRate);
474 if (!differentResultFetched)
476 INFO_PRINTF1(_L("Test did not return different results under memory stress (unexpected)"));
482 //The "bad" versions of these standard fonts have been hacked to give specific results
483 _LIT(KFBSERVFontFileBitmap, "Z:\\resource\\fonts\\ceurope.GDR");
484 //This file has the master GUID nobbled, but all the guid and typeface entries are duplicates.
485 _LIT(KFBSERVFontFileBadBitmap1, "Z:\\PlatTest\\Graphics\\TestData\\uniquified_fonts\\XXeuro.GDR");
486 //This file has the master GUID nobbled, one of the 8 fonts, and one of the typefaces has also been nobbled.
487 _LIT(KFBSERVFontFileBadBitmap2, "Z:\\PlatTest\\Graphics\\TestData\\uniquified_fonts\\YYeuro.GDR");
488 enum {KNobbledFontEnum=0x10ff5912}; //official code for LatinBold17 is 0x10005912
489 _LIT(NobbledTypefaceName, "XatinBold17");
493 @SYMTestCaseID GRAPHICS-CTFbs-TestDuplicateFontFileEntries-0002
497 @SYMTestCaseDesc If Bitmap fonts are loaded that are marginally unique compared to the
498 existing fonts then they are not actually added to the typeface system, and may even be discarded.
502 @SYMTestStatus Implemented
505 The original versions of the Bitmapfonts are first loaded to ensure that they have actually been loaded.
506 A version of the bitmap font which is identical except for the file UID should be rejected
507 Verify that the unique data for the next test does not already exist
508 A version of the bitmap font which has just 1 unique bitmap will be loaded, but only 1 typeface entry will be added.
509 Verify that the unique data for the previous test now does exist
510 The memory allocations through these steps should be deterministic
514 @SYMTestExpectedResults The test expects:
515 The original versions should load without changing the number of typefaces
516 The second load should return the same UID for the font
517 The identical fonts should return captured fail codes, and not change the typeface counts
518 The semi-identical font should perform a single increase to UIDs and typefaces
520 void CTFntMem::TestDuplicateBitmapFontFileEntriesL()
523 INFO_PRINTF1(_L("Test Load semi-duplicate bitmap font files"));
526 CFontStore *fs=CFontStore::NewL(iHeap);
527 CleanupStack::PushL(fs);
529 TInt numFacesBefore=fs->NumTypefaces();
530 TUid aIdBitMap1=TUid::Null();
531 TUint numAllocsBefore=iHeap->Count();
532 TUid aIdBitMap2=TUid::Null();
533 TUid aIdBitMap3=TUid::Null();
534 //make sure the originals of these fonts were safely opened
535 //both should simply cause a reference-count increase on the originator files.
536 TRAPD(err1,aIdBitMap1=fs->AddFileL(KFBSERVFontFileBitmap));
537 TEST(err1==KErrNone);
540 INFO_PRINTF1(_L("One of the expected fonts was missing. Test abandoned."));
544 TInt numFacesAfterLoadOriginals=fs->NumTypefaces();
545 TEST(numFacesBefore<numFacesAfterLoadOriginals);
546 TUint numAllocsAfterLoadOriginals=iHeap->Count();
549 //This bitmap font file contains no new fonts, so will ultimately be discarded
550 //The master UID has been nobbled so the file is not considered identical to its originator
551 TRAPD(err3,aIdBitMap2=fs->AddFileL(KFBSERVFontFileBadBitmap1));
552 TEST(err3==KErrAlreadyExists && aIdBitMap2==TUid::Null());
553 if (err3!=KErrAlreadyExists)
554 INFO_PRINTF2(_L("Unexpected error code was %d"),err3);
555 TInt numFacesAfterLoadBadFile3=fs->NumTypefaces();
556 TEST(numFacesAfterLoadOriginals==numFacesAfterLoadBadFile3);
557 TUint numAllocsAfterLoadBadFile3=iHeap->Count();
558 TEST(numAllocsAfterLoadBadFile3==numAllocsAfterLoadOriginals);
560 //Before loading the next test check that the new data about to be added does not already exist
563 TFontSpec fontSpec(NobbledTypefaceName,10);
565 TInt err5a1=fs->GetFontById(font,TUid::Uid(KNobbledFontEnum),algStyle);
566 TEST(font==NULL); //the nobbled UID should not already exist
570 TInt err5a2=fs->GetNearestFontInPixels(font, fontSpec);
573 TFontSpec specNoMatch=font->FontSpecInTwips();
574 TEST(specNoMatch.iTypeface.iName!=NobbledTypefaceName); //Nobbled font name should not be known yet
575 fs->ReleaseFont(font);
578 //This bitmap font file contains one new typeface and font
579 //Actually, they are bit file edited versions of existing ones!
580 TInt numAllocsBeforeLoadBadFile5 = iHeap->Count();
581 TRAPD(err5,aIdBitMap3=fs->AddFileL(KFBSERVFontFileBadBitmap2));
582 TEST(err5==KErrNone);
584 INFO_PRINTF2(_L("Unexpected error code was %d"),err5);
586 //The number of typefaces has now increased
587 TInt numFacesAfterLoadBadFile5=fs->NumTypefaces();
588 TEST(numFacesAfterLoadOriginals+1==numFacesAfterLoadBadFile5);
589 TUint numAllocsAfterLoadBadFile5=iHeap->Count();
590 TEST(numAllocsAfterLoadBadFile5==numAllocsBeforeLoadBadFile5+2);
593 TInt err5b1=fs->GetFontById(font,TUid::Uid(KNobbledFontEnum),algStyle);
594 TEST(err5b1==KErrNone && font!=NULL); //the nobbled UID should now exist
595 if (err5b1!=KErrNone)
596 INFO_PRINTF2(_L("Unexpected error code was %d"),err5b1);
599 TInt err5b2=fs->GetNearestFontInPixels(font2, fontSpec);
600 TEST(err5b2==KErrNone && font2!=NULL);
601 if (err5b2!=KErrNone)
602 INFO_PRINTF2(_L("Unexpected error code was %d"),err5b2);
605 TFontSpec specMatches=font->FontSpecInTwips();
606 TEST(specMatches.iTypeface.iName==NobbledTypefaceName); //the nobbled typeface should now exist
610 fs->ReleaseFont(font);
612 fs->ReleaseFont(font2);
615 if (aIdBitMap1!=TUid::Null()) fs->RemoveFile(aIdBitMap1);
616 if (aIdBitMap2!=TUid::Null()) fs->RemoveFile(aIdBitMap2);
617 if (aIdBitMap3!=TUid::Null()) fs->RemoveFile(aIdBitMap3);
619 //The added typeface should have been uninstalled
620 TInt numFacesAfterUninstall=fs->NumTypefaces();
621 TEST(numFacesAfterUninstall==numFacesBefore);
622 TUint numAllocsAfterUninstall=iHeap->Count();
624 CleanupStack::PopAndDestroy(fs);
627 __RHEAP_CHECK(iHeap,0);
628 __RHEAP_MARKEND(iHeap);
634 __CONSTRUCT_STEP__(FntMem)