Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -4,18 +4,76 @@ |
5 | 5 | expect(1); |
6 | 6 | |
7 | 7 | ok( mw.webfonts, 'mw.webfonts defined' ); |
8 | | -}); |
| 8 | +} ); |
9 | 9 | |
10 | 10 | test( '-- Dynamic font loading', function() { |
11 | 11 | expect( 7 ); |
12 | 12 | var cssRulesLength = document.styleSheets.length; |
13 | 13 | assertTrue( mw.webfonts.addFont( 'Lohit Devanagari' ) , 'Add the Lohit Devanagari font' ); |
14 | 14 | assertTrue( $.inArray( 'Lohit Devanagari', mw.webfonts.fonts ) >= 0 , 'Font loaded' ); |
15 | | - assertTrue( cssRulesLength +1 === document.styleSheets.length, 'New css rule added to the document' ); |
| 15 | + assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document' ); |
16 | 16 | var loadedFontsSize = mw.webfonts.fonts.length; |
17 | 17 | assertTrue( mw.webfonts.addFont( 'Lohit Devanagari' ) , 'Add the Lohit Devanagari font again' ); |
18 | 18 | assertTrue( loadedFontsSize === mw.webfonts.fonts.length , 'Already loaded fonts not loaded again.' ); |
19 | 19 | assertFalse( mw.webfonts.addFont( 'Some Non existing font' ), 'addFont should return false if font not found' ); |
20 | | - assertTrue( cssRulesLength +1 === document.styleSheets.length, 'No new css rules added at this stage' ); |
21 | | -}); |
| 20 | + assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'No new css rules added at this stage' ); |
| 21 | +} ); |
22 | 22 | |
| 23 | +test( '-- Dynamic font loading based on lang attribute', function() { |
| 24 | + expect( 15 ); |
| 25 | + |
| 26 | + ok( $( 'body' ).append( "<p class='webfonts-testing-lang-attr'>Some Content</p>") ); |
| 27 | + $testElement = $( 'p.webfonts-testing-lang-attr' ) |
| 28 | + assertTrue( $testElement !== undefined, 'Test element added' ) ; |
| 29 | + |
| 30 | + ok( mw.webfonts.loadFontsForLangAttr() ); |
| 31 | + assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since there is no lang attribute' ) ; |
| 32 | + |
| 33 | + ok( $testElement.attr( 'lang' , 'en' ) , 'Set lang attribute as english' ); |
| 34 | + ok( mw.webfonts.loadFontsForLangAttr() ); |
| 35 | + assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since there is en lang has no fonts available' ) ; |
| 36 | + |
| 37 | + ok( $testElement.attr( 'lang' , 'ta' ) , 'Set lang attribute as Tamil' ); |
| 38 | + var cssRulesLength = document.styleSheets.length; |
| 39 | + ok( mw.webfonts.loadFontsForLangAttr() ); |
| 40 | + assertTrue( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has webfonts-lang-attr class' ) ; |
| 41 | + assertTrue( $.inArray( 'Lohit Tamil', mw.webfonts.fonts ) >= 0 , 'Font loaded' ); |
| 42 | + assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document' ); |
| 43 | + |
| 44 | + ok( mw.webfonts.reset() ); |
| 45 | + assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr since we reset it' ) ; |
| 46 | + |
| 47 | + ok( $testElement.remove() ); |
| 48 | +} ); |
| 49 | + |
| 50 | +test( '-- Dynamic font loading based on font-family style attribute', function() { |
| 51 | + expect( 14 ) |
| 52 | + |
| 53 | + ok( $( 'body' ).append( "<p class='webfonts-testing-font-family-style'>Some Content</p>") ); |
| 54 | + var $testElement = $( 'p.webfonts-testing-font-family-style' ); |
| 55 | + assertTrue( $testElement !== undefined, 'Test element added' ) ; |
| 56 | + |
| 57 | + $testElement.css( 'font-family', 'RufScript, Arial, Helvetica, sans'); |
| 58 | + var cssRulesLength = document.styleSheets.length; |
| 59 | + assertTrue( $.inArray( 'RufScript', mw.webfonts.fonts ) === -1 , 'RufScript Font not loaded yet' ); |
| 60 | + ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
| 61 | + assertTrue( $.inArray( 'RufScript', mw.webfonts.fonts ) >= 0 , 'Font loaded' ); |
| 62 | + assertTrue( cssRulesLength +1 === document.styleSheets.length, 'New css rule added to the document' ); |
| 63 | + |
| 64 | + $testElement.css( 'font-family', 'NonExistingFont, Arial, Helvetica, sans' ); |
| 65 | + cssRulesLength = document.styleSheets.length; |
| 66 | + ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
| 67 | + assertTrue( $.inArray( 'NonExistingFont', mw.webfonts.fonts ) === -1 , 'Font not loaded since it is not existing, including fallback fonts' ); |
| 68 | + assertTrue( cssRulesLength === document.styleSheets.length, 'No new css rule added to the document' ); |
| 69 | + |
| 70 | + $testElement.css( 'font-family', 'NonExistingFont, AnjaliOldLipi, Arial, Helvetica, sans'); |
| 71 | + cssRulesLength = document.styleSheets.length; |
| 72 | + assertTrue( $.inArray( 'AnjaliOldLipi', mw.webfonts.fonts ) === -1 , 'Fallback font AnjaliOldLipi not loaded yet' ); |
| 73 | + ok( mw.webfonts.loadFontsForFontFamilyStyle() ); |
| 74 | + assertTrue( $.inArray( 'AnjaliOldLipi', mw.webfonts.fonts ) >= 0 , 'Fallback font AnjaliOldLipi loaded' ); |
| 75 | + assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document for fallbackfont' ); |
| 76 | + |
| 77 | + ok( $testElement.remove() ); |
| 78 | +} ); |
| 79 | + |
| 80 | + |
Index: trunk/extensions/WebFonts/resources/ext.webfonts.js |
— | — | @@ -73,6 +73,7 @@ |
74 | 74 | |
75 | 75 | // Remove the cookie |
76 | 76 | $.cookie( 'webfonts-font', 'none', { 'path': '/', 'expires': 30 } ); |
| 77 | + return true; |
77 | 78 | }, |
78 | 79 | |
79 | 80 | /** |
— | — | @@ -255,6 +256,7 @@ |
256 | 257 | } |
257 | 258 | } |
258 | 259 | }); |
| 260 | + return true; |
259 | 261 | }, |
260 | 262 | |
261 | 263 | /** |
— | — | @@ -273,6 +275,7 @@ |
274 | 276 | }); |
275 | 277 | } |
276 | 278 | }); |
| 279 | + return true; |
277 | 280 | }, |
278 | 281 | |
279 | 282 | /** |