11import FONT_FACE_DEFINITIONS from '../../../fonts.json' ;
2- import { FONT_FACE_FALLBACKS , FontService } from './font.service' ;
2+ import { FONT_FACE_FALLBACKS , FontResolver , FontService } from './font.service' ;
33import { MockConsole } from './mock-console' ;
4+ import { isGecko } from './utils' ;
45
56// Mocking the document with ts-mockito does not work because the type definitions for document.fonts does not include
67// the add method
@@ -14,6 +15,60 @@ class FakeDocument {
1415}
1516
1617const mockedConsole : MockConsole = MockConsole . install ( ) ;
18+ describe ( 'FontResolver' , ( ) => {
19+ const fontResolver = new FontResolver ( ) ;
20+
21+ it ( 'can resolve specified_font' , ( ) => {
22+ const resolution = fontResolver . resolveFont ( { defaultFont : 'Andika' , writingSystem : { tag : 'en' } } ) ;
23+ expect ( resolution ) . toEqual ( {
24+ requestedFont : 'Andika' ,
25+ resolution : 'specified_font' ,
26+ family : 'Andika' ,
27+ url : FONT_FACE_DEFINITIONS [ 'Andika' ]
28+ } ) ;
29+ } ) ;
30+
31+ it ( 'can resolve near_match_specified_font' , ( ) => {
32+ const resolution = fontResolver . resolveFont ( { defaultFont : 'Andika Eng-Lit' , writingSystem : { tag : 'en' } } ) ;
33+ expect ( resolution ) . toEqual ( {
34+ requestedFont : 'Andika Eng-Lit' ,
35+ resolution : 'near_match_specified_font' ,
36+ family : 'Andika' ,
37+ url : FONT_FACE_DEFINITIONS [ 'Andika' ]
38+ } ) ;
39+ } ) ;
40+
41+ it ( 'can resolve writing_system_default and take region into account' , ( ) => {
42+ let resolution = fontResolver . resolveFont ( { defaultFont : 'Unknown font' , writingSystem : { tag : 'unk-Arab' } } ) ;
43+ expect ( resolution ) . toEqual ( {
44+ requestedFont : 'Unknown font' ,
45+ resolution : 'writing_system_default' ,
46+ family : 'Scheherazade New' ,
47+ url : FONT_FACE_DEFINITIONS [ 'Scheherazade New' ]
48+ } ) ;
49+ resolution = fontResolver . resolveFont ( { defaultFont : 'Unknown font' , writingSystem : { tag : 'unk-Arab-NE' } } ) ;
50+ expect ( resolution ) . toEqual ( {
51+ requestedFont : 'Unknown font' ,
52+ resolution : 'writing_system_default' ,
53+ family : 'Harmattan' ,
54+ url : FONT_FACE_DEFINITIONS [ 'Harmattan' ]
55+ } ) ;
56+ } ) ;
57+
58+ it ( 'can fall back to the default font' , ( ) => {
59+ const resolution = fontResolver . resolveFont ( { defaultFont : 'Unknown font' , writingSystem : { tag : 'unk' } } ) ;
60+ expect ( resolution ) . toEqual ( {
61+ requestedFont : 'Unknown font' ,
62+ resolution : 'default_font' ,
63+ family : 'Charis SIL' ,
64+ url : FONT_FACE_DEFINITIONS [ 'Charis SIL' ]
65+ } ) ;
66+ } ) ;
67+ } ) ;
68+
69+ function spec ( font ?: string ) : { defaultFont ?: string ; writingSystem : { tag : string } } {
70+ return { defaultFont : font , writingSystem : { tag : 'en' } } ;
71+ }
1772
1873describe ( 'FontService' , ( ) => {
1974 let fontService : FontService ;
@@ -23,31 +78,42 @@ describe('FontService', () => {
2378 } ) ;
2479
2580 it ( 'should default to Charis SIL when font is not specified' , ( ) => {
26- expect ( fontService . getCSSFontName ( undefined ) ) . toEqual ( 'Charis SIL' ) ;
27- expect ( fontService . getCSSFontName ( '' ) ) . toEqual ( 'Charis SIL' ) ;
81+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r / ) ;
82+ expect ( fontService . getFontFamilyFromProject ( spec ( undefined ) ) ) . toEqual ( 'Charis SIL' ) ;
83+ expect ( fontService . getFontFamilyFromProject ( spec ( '' ) ) ) . toEqual ( 'Charis SIL' ) ;
84+ mockedConsole . verify ( ) ;
85+ mockedConsole . reset ( ) ;
2886 } ) ;
2987
3088 it ( 'should default to Charis SIL when font is not recognized' , ( ) => {
31- mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n / ) ;
32- expect ( fontService . getCSSFontName ( 'zyz123' ) ) . toEqual ( 'Charis SIL' ) ;
89+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r z y z 1 2 3 / ) ;
90+ expect ( fontService . getFontFamilyFromProject ( spec ( 'zyz123' ) ) ) . toEqual ( 'Charis SIL' ) ;
3391 mockedConsole . verify ( ) ;
3492 mockedConsole . reset ( ) ;
3593 } ) ;
3694
3795 it ( 'should default to Charis SIL for proprietary serif fonts' , ( ) => {
38- expect ( fontService . getCSSFontName ( 'Times New Roman' ) ) . toEqual ( 'Charis SIL' ) ;
39- expect ( fontService . getCSSFontName ( 'Cambria' ) ) . toEqual ( 'Charis SIL' ) ;
40- expect ( fontService . getCSSFontName ( 'Sylfaen' ) ) . toEqual ( 'Charis SIL' ) ;
96+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r / ) ;
97+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Times New Roman' ) ) ) . toEqual ( 'Charis SIL' ) ;
98+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Cambria' ) ) ) . toEqual ( 'Charis SIL' ) ;
99+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Sylfaen' ) ) ) . toEqual ( 'Charis SIL' ) ;
100+ mockedConsole . verify ( ) ;
101+ mockedConsole . reset ( ) ;
41102 } ) ;
42103
43104 it ( 'should default to Andika for proprietary sans-serif fonts' , ( ) => {
44- expect ( fontService . getCSSFontName ( 'Arial' ) ) . toEqual ( 'Andika' ) ;
45- expect ( fontService . getCSSFontName ( 'Verdana' ) ) . toEqual ( 'Andika' ) ;
46- expect ( fontService . getCSSFontName ( 'Tahoma' ) ) . toEqual ( 'Andika' ) ;
105+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r / ) ;
106+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Arial' ) ) ) . toEqual ( 'Andika' ) ;
107+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Verdana' ) ) ) . toEqual ( 'Andika' ) ;
108+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Tahoma' ) ) ) . toEqual ( 'Andika' ) ;
109+ mockedConsole . verify ( ) ;
110+ mockedConsole . reset ( ) ;
47111 } ) ;
48112
49113 it ( 'should fall back from Annapurna SIL Thami to Annapurna SIL' , ( ) => {
50- expect ( fontService . getCSSFontName ( 'Annapurna SIL Thami' ) ) . toEqual ( 'Annapurna SIL' ) ;
114+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r / ) ;
115+ expect ( fontService . getFontFamilyFromProject ( spec ( 'Annapurna SIL Thami' ) ) ) . toEqual ( 'Annapurna SIL' ) ;
116+ mockedConsole . verify ( ) ;
51117 } ) ;
52118
53119 it ( 'should not have any broken font fallbacks' , ( ) => {
@@ -56,10 +122,28 @@ describe('FontService', () => {
56122 } ) ;
57123
58124 it ( 'should only load each font once' , ( ) => {
59- fontService . getCSSFontName ( 'Charis SIL' ) ;
60- fontService . getCSSFontName ( 'Charis SIL' ) ;
61- fontService . getCSSFontName ( 'Charis SIL' ) ;
125+ fontService . getFontFamilyFromProject ( spec ( 'Charis SIL' ) ) ;
126+ fontService . getFontFamilyFromProject ( spec ( 'Charis SIL' ) ) ;
127+ fontService . getFontFamilyFromProject ( spec ( 'Charis SIL' ) ) ;
62128
63129 expect ( ( ( fontService as any ) . document as FakeDocument ) . addCount ) . toEqual ( 1 ) ;
64130 } ) ;
131+
132+ it ( "should default to the specified writing system's default font" , ( ) => {
133+ mockedConsole . expectAndHide ( / N o f o n t d e f i n i t i o n f o r / ) ;
134+ const tagToExpectedFont = {
135+ 'aaa-Arab-NE' : 'Harmattan' ,
136+ 'aaa-Arab-PK' : isGecko ( ) ? 'Awami Nastaliq' : 'Scheherazade New' ,
137+ 'aaa-Arab' : 'Scheherazade New' ,
138+ 'aaa-Deva-IN' : 'Annapurna SIL'
139+ } ;
140+
141+ for ( const [ tag , font ] of Object . entries ( tagToExpectedFont ) ) {
142+ expect ( fontService . getFontFamilyFromProject ( { defaultFont : 'Unknown font' , writingSystem : { tag } } ) ) . toEqual (
143+ font
144+ ) ;
145+ }
146+ mockedConsole . verify ( ) ;
147+ mockedConsole . reset ( ) ;
148+ } ) ;
65149} ) ;
0 commit comments