Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -6,17 +6,41 @@ |
7 | 7 | ok( mw.webfonts, 'mw.webfonts is defined' ); |
8 | 8 | } ); |
9 | 9 | |
| 10 | +test( '-- Web font application to body', function() { |
| 11 | + expect( 4 ); |
| 12 | + |
| 13 | + var invalidFont = 'NonExistingFont'; |
| 14 | + assertTrue( mw.webfonts.set( invalidFont ) === undefined, 'A non-existent font is not initialized' ); |
| 15 | + // TODO: test that the right thing was written to the log |
| 16 | + |
| 17 | + var $body = $( 'body' ); |
| 18 | + var bodyLang = $body.attr( 'lang' ); |
| 19 | + var oldConfig = { |
| 20 | + fontFamily: $body.css( 'font-family' ), |
| 21 | + fontSize: $body.css( 'font-size' ) |
| 22 | + }; |
| 23 | + var teluguFont = mw.webfonts.config.languages.te[0] |
| 24 | + $body.attr( 'lang', 'te' ); |
| 25 | + |
| 26 | + ok( mw.webfonts.set( teluguFont ), 'Attempted to load a Telugu font for the whole page' ); |
| 27 | + deepEqual( oldConfig, mw.webfonts.oldconfig, 'Previous body css was saved properly' ); |
| 28 | + |
| 29 | + // Restore <body> |
| 30 | + $body.attr( 'lang', bodyLang ); |
| 31 | + ok( mw.webfonts.reset(), 'Reset body after testing font application' ); |
| 32 | +} ); |
| 33 | + |
10 | 34 | test( '-- Dynamic font loading', function() { |
11 | 35 | expect( 7 ); |
12 | 36 | |
13 | | - var validFontName = 'Lohit Devanagari'; |
| 37 | + var validFontName = mw.webfonts.config.languages.hi[0]; |
14 | 38 | mw.webfonts.fonts = []; |
15 | 39 | var cssRulesLength = document.styleSheets.length; |
16 | | - assertTrue( mw.webfonts.addFont( validFontName ) , 'Add a Devanagari font' ); |
| 40 | + assertTrue( mw.webfonts.addFont( validFontName ), 'Add a Devanagari font' ); |
17 | 41 | assertTrue( $.inArray( validFontName, mw.webfonts.fonts ) >= 0 , 'Devanagari font loaded' ); |
18 | 42 | assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document' ); |
19 | 43 | var loadedFontsSize = mw.webfonts.fonts.length; |
20 | | - assertTrue( mw.webfonts.addFont( validFontName ) , 'Add the Devanagari font again' ); |
| 44 | + assertTrue( mw.webfonts.addFont( validFontName ), 'Add the Devanagari font again' ); |
21 | 45 | assertTrue( loadedFontsSize === mw.webfonts.fonts.length , 'A font that is already loaded is not loaded again' ); |
22 | 46 | assertFalse( mw.webfonts.addFont( 'Some non-existing font' ), 'addFont returns false if the font was not found' ); |
23 | 47 | assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'Loading the font does not add new css rules' ); |
— | — | @@ -33,19 +57,19 @@ |
34 | 58 | wgPageContentLanguage: "en", |
35 | 59 | } ); |
36 | 60 | |
37 | | - ok( $( 'body' ).append( "<p class='webfonts-testing-lang-attr'>Some content</p>"), ' A element for testing lang-based loading was appended to <body>' ); |
| 61 | + ok( $( 'body' ).append( "<p class='webfonts-testing-lang-attr'>Some content</p>"), 'An element for testing lang-based loading was appended to body' ); |
38 | 62 | $testElement = $( 'p.webfonts-testing-lang-attr' ) |
39 | 63 | assertTrue( $testElement !== [], 'The test element is defined' ); |
40 | 64 | |
41 | 65 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute' ); |
42 | 66 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since there is no lang attribute' ); |
43 | 67 | |
44 | | - ok( $testElement.attr( 'lang' , 'en' ) , 'The lang attribute of the test element was set to en (English)' ); |
| 68 | + ok( $testElement.attr( 'lang' , 'en' ), 'The lang attribute of the test element was set to en (English)' ); |
45 | 69 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute en' ); |
46 | 70 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The test element has no webfonts-lang-attr class since en lang has no fonts available' ); |
47 | 71 | |
48 | | - var tamilFont = 'Lohit Tamil'; |
49 | | - ok( $testElement.attr( 'lang' , 'ta' ) , 'Set lang attribute to ta (Tamil)' ); |
| 72 | + var tamilFont = mw.webfonts.config.languages.ta[0]; |
| 73 | + ok( $testElement.attr( 'lang' , 'ta' ), 'Set lang attribute to ta (Tamil)' ); |
50 | 74 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute ta' ); |
51 | 75 | assertTrue( $testElement.hasClass( 'webfonts-lang-attr' ), 'The test element has webfonts-lang-attr class' ); |
52 | 76 | assertTrue( $.inArray( tamilFont, mw.webfonts.fonts ) >= 0 , 'Tamil font loaded' ); |
— | — | @@ -54,14 +78,14 @@ |
55 | 79 | ok( mw.webfonts.reset(), 'Reset webfonts' ); |
56 | 80 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr since we reset it' ); |
57 | 81 | |
58 | | - ok( $testElement.remove(), 'The test element was removed from <body>' ); |
| 82 | + ok( $testElement.remove(), 'The test element was removed from body' ); |
59 | 83 | } ); |
60 | 84 | |
61 | 85 | test( '-- Dynamic font loading based on font-family style attribute', function() { |
62 | 86 | expect( 14 ) |
63 | 87 | |
64 | 88 | mw.webfonts.fonts = []; |
65 | | - ok( $( 'body' ).append( "<p class='webfonts-testing-font-family-style'>Some content</p>" ), 'An element for testing font-family loading was appended to <body>' ); |
| 89 | + ok( $( 'body' ).append( "<p class='webfonts-testing-font-family-style'>Some content</p>" ), 'An element for testing font-family loading was appended to body' ); |
66 | 90 | var $testElement = $( 'p.webfonts-testing-font-family-style' ); |
67 | 91 | assertTrue( $testElement !== [], 'The test element is defined' ); |
68 | 92 | |
— | — | @@ -79,12 +103,12 @@ |
80 | 104 | assertTrue( $.inArray( invalidFont, mw.webfonts.fonts ) === -1 , 'Font not loaded since it is not existing, including fallback fonts' ); |
81 | 105 | assertFalse( isFontFaceLoaded( invalidFont ), 'No new css rule added to the document since the font does not exist' ); |
82 | 106 | |
83 | | - var malayalamFont = 'AnjaliOldLipi'; |
| 107 | + var malayalamFont = mw.webfonts.config.languages.ml[0]; |
84 | 108 | $testElement.attr( 'style', 'font-family: ' + invalidFont + ', ' + malayalamFont + fallbackFonts ); |
85 | 109 | assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) === -1 , 'Fallback font not loaded yet' ); |
86 | 110 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Loading fonts from font-family' ); |
87 | 111 | assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) >= 0 , 'A fallback font was loaded' ); |
88 | | - assertTrue( isFontFaceLoaded( malayalamFont ) , 'New css rule added to the document for fallback font' ); |
| 112 | + assertTrue( isFontFaceLoaded( malayalamFont ), 'New css rule added to the document for fallback font' ); |
89 | 113 | |
90 | 114 | ok( $testElement.remove() ); |
91 | 115 | } ); |