Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -6,9 +6,51 @@ |
7 | 7 | * @copyright Copyright © 2012 Santhosh Thottingal, Amir E. Aharoni |
8 | 8 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
9 | 9 | */ |
| 10 | +( function () { |
10 | 11 | |
11 | 12 | module( 'ext.webfonts', QUnit.newMwEnvironment() ); |
12 | 13 | |
| 14 | +function isFontFaceLoaded( fontFamilyName ) { |
| 15 | + var lastStyleIndex, styleIndex, lastStyleSheet, cssText; |
| 16 | + |
| 17 | + lastStyleIndex = document.styleSheets.length - 1; |
| 18 | + |
| 19 | + // Iterate from last. |
| 20 | + for ( styleIndex = lastStyleIndex; styleIndex > 0; styleIndex-- ) { |
| 21 | + lastStyleSheet = document.styleSheets[styleIndex]; |
| 22 | + if ( !lastStyleSheet ) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + if ( !lastStyleSheet.cssRules[0] ) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + cssText = lastStyleSheet.cssRules[0].cssText; |
| 29 | + if ( cssText.indexOf( '@font-face' ) >= 0 && cssText.indexOf( fontFamilyName ) >= 0 ) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return false; |
| 35 | +} |
| 36 | + |
| 37 | +// Convert a font-family string to an array. This is needed |
| 38 | +// because browsers change the string by adding or removing spaces, |
| 39 | +// so the string cannot be compared in a uniform way. |
| 40 | +function fontFamilyList( fontFamilyString ) { |
| 41 | + var fontList, fontIndex; |
| 42 | + |
| 43 | + // Create a list |
| 44 | + fontList = fontFamilyString.split( /, */ ); |
| 45 | + |
| 46 | + // Remove the quotes from font names |
| 47 | + for ( fontIndex = 0; fontIndex < fontList.length; ++fontIndex ) { |
| 48 | + fontList[fontIndex] = fontList[fontIndex].replace( /^["']/, '' ); |
| 49 | + fontList[fontIndex] = fontList[fontIndex].replace( /["']$/, '' ); |
| 50 | + } |
| 51 | + |
| 52 | + return fontList; |
| 53 | +} |
| 54 | + |
13 | 55 | test( '-- Initial check', function() { |
14 | 56 | expect(1); |
15 | 57 | |
— | — | @@ -27,10 +69,10 @@ |
28 | 70 | return; |
29 | 71 | } |
30 | 72 | |
31 | | - expect( 27 ); |
| 73 | + expect( 18 ); |
32 | 74 | |
33 | 75 | var invalidFont = 'NonExistingFont'; |
34 | | - assertTrue( mw.webfonts.set( invalidFont ) === undefined, 'A non-existent font is not initialized' ); |
| 76 | + strictEqual( mw.webfonts.set( invalidFont ), undefined, 'A non-existent font is not initialized' ); |
35 | 77 | // TODO: test that the right thing was written to the log |
36 | 78 | |
37 | 79 | var $body = $( 'body' ); |
— | — | @@ -42,17 +84,12 @@ |
43 | 85 | var teluguFont = mw.webfonts.config.languages.te[0]; |
44 | 86 | $body.attr( 'lang', 'te' ); |
45 | 87 | |
46 | | - ok( $( 'body' ).append( "<input class='webfonts-testing-element'>input content</input>"), 'An input element for testing was appended to body' ); |
47 | | - var $inputElement = $( 'input.webfonts-testing-element' ); |
48 | | - assertTrue( $inputElement !== [], 'The input test element is defined' ); |
49 | | - ok( $( 'body' ).append( "<select class='webfonts-testing-element'>select content</select>"), 'A select element for testing was appended to body' ); |
50 | | - var $selectElement = $( 'select.webfonts-testing-element' ); |
51 | | - assertTrue( $selectElement !== [], 'The select test element is defined' ); |
52 | | - ok( $( 'body' ).append( "<textarea class='webfonts-testing-element'>textarea content</textarea>"), 'A textarea element for testing was appended to body' ); |
53 | | - var $textareaElement = $( 'textarea.webfonts-testing-element' ); |
54 | | - assertTrue( $textareaElement !== [], 'The textarea test element is defined' ); |
| 88 | + var $inputElement = $( '<input value="input content"/>' ); |
| 89 | + var $selectElement = $( '<select><option value="foobar">Foobar</option></select>' ); |
| 90 | + var $textareaElement = $( '<textarea>textarea content</textarea>' ); |
| 91 | + $( '#qunit-fixture' ).append( $inputElement, $selectElement, $textareaElement ); |
55 | 92 | |
56 | | - ok( mw.webfonts.set( teluguFont ), 'Attempted to load a Telugu font for the whole page' ); |
| 93 | + assertTrue( mw.webfonts.set( teluguFont ), 'Attempted to load a Telugu font for the whole page' ); |
57 | 94 | var fallbackFonts = 'Helvetica, Arial, sans-serif'; |
58 | 95 | deepEqual( oldConfig, mw.webfonts.oldconfig, 'Previous body css was saved properly' ); |
59 | 96 | |
— | — | @@ -71,7 +108,7 @@ |
72 | 109 | equals( $.cookie( 'webfonts-font' ), teluguFont, 'Correct cookie for the font was set' ); |
73 | 110 | |
74 | 111 | // Reset everything |
75 | | - ok( mw.webfonts.set( false ) === undefined, 'Reset body after testing font application' ); |
| 112 | + strictEqual( mw.webfonts.set( false ), undefined, 'Reset body after testing font application' ); |
76 | 113 | equals( $body.css( 'font-family' ), oldConfig.fontFamily, 'Previous font-family for body was restored' ); |
77 | 114 | equals( $body.css( 'font-size' ), oldConfig.fontSize, 'Previous font-size for body was restored' ); |
78 | 115 | equals( $inputElement.css( 'font-family' ), oldConfig.fontFamily, 'Previous font-family for body was restored' ); |
— | — | @@ -81,10 +118,6 @@ |
82 | 119 | equals( $textareaElement.css( 'font-family' ), oldConfig.fontFamily, 'Previous font-family for the textarea element was restored' ); |
83 | 120 | equals( $textareaElement.css( 'font-size' ), oldConfig.fontSize, 'Previous font-size for the textarea element was restored' ); |
84 | 121 | |
85 | | - ok( $inputElement.remove(), 'The input test element was removed from body' ); |
86 | | - ok( $selectElement.remove(), 'The select test element was removed from body' ); |
87 | | - ok( $textareaElement.remove(), 'The textarea test element was removed from body' ); |
88 | | - |
89 | 122 | // Cookie set |
90 | 123 | equals( $.cookie( 'webfonts-font' ), 'none', 'The cookie was removed' ); |
91 | 124 | |
— | — | @@ -116,7 +149,7 @@ |
117 | 150 | return; |
118 | 151 | } |
119 | 152 | |
120 | | - expect( 15 ); |
| 153 | + expect( 12 ); |
121 | 154 | |
122 | 155 | mw.webfonts.fonts = []; |
123 | 156 | mw.config.set( { |
— | — | @@ -126,9 +159,8 @@ |
127 | 160 | wgPageContentLanguage: "en" |
128 | 161 | } ); |
129 | 162 | |
130 | | - ok( $( 'body' ).append( "<p class='webfonts-testing-lang-attr'>Some content</p>"), 'An element for testing lang-based loading was appended to body' ); |
131 | | - var $testElement = $( 'p.webfonts-testing-lang-attr' ); |
132 | | - assertTrue( $testElement !== [], 'The test element is defined' ); |
| 163 | + var $testElement = $( '<p>Some content</p>' ); |
| 164 | + $( '#qunit-fixture' ).append( $testElement ); |
133 | 165 | |
134 | 166 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute' ); |
135 | 167 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since there is no lang attribute' ); |
— | — | @@ -146,9 +178,6 @@ |
147 | 179 | |
148 | 180 | ok( mw.webfonts.reset(), 'Reset webfonts after testing application by lang' ); |
149 | 181 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The testing element has no webfonts-lang-attr since we reset it' ); |
150 | | - // equals( $( 'body' ).find( '*[lang]' ).length, 0, 'There are no elements with the webfonts-lang-attr class' ); |
151 | | - |
152 | | - ok( $testElement.remove(), 'The test element was removed from body' ); |
153 | 182 | } ); |
154 | 183 | |
155 | 184 | test( '-- Dynamic font loading based on font-family style attribute', function() { |
— | — | @@ -156,23 +185,23 @@ |
157 | 186 | return; |
158 | 187 | } |
159 | 188 | |
160 | | - expect( 14 ); |
| 189 | + expect( 11 ); |
161 | 190 | |
162 | 191 | mw.webfonts.fonts = []; |
163 | | - ok( $( 'body' ).append( "<p class='webfonts-testing-font-family-style'>Some content</p>" ), 'An element for testing font-family loading was appended to body' ); |
164 | | - var $testElement = $( 'p.webfonts-testing-font-family-style' ); |
165 | | - assertTrue( $testElement !== [], 'The test element is defined' ); |
166 | 192 | |
| 193 | + var $testElement = $( '<p>Some content</p>' ); |
| 194 | + $( '#qunit-fixture' ).append( $testElement ); |
| 195 | + |
167 | 196 | var latinWebFont = 'RufScript'; |
168 | 197 | var fallbackFonts = 'Helvetica, Arial, sans-serif'; |
169 | | - $testElement.attr( 'style','font-family: ' + latinWebFont + ', ' + fallbackFonts ); |
| 198 | + $testElement.attr( 'style', 'font-family: ' + latinWebFont + ', ' + fallbackFonts ); |
170 | 199 | assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) === -1, 'Latin font not loaded yet' ); |
171 | 200 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Loaded fonts from font-family' ); |
172 | 201 | assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) >= 0, 'Latin font loaded' ); |
173 | 202 | assertTrue( isFontFaceLoaded( latinWebFont ), 'New css rule added to the document for Latin' ); |
174 | 203 | |
175 | 204 | var invalidFont = 'NonExistingFont'; |
176 | | - $testElement.attr( 'style','font-family: ' + invalidFont + ', ' + fallbackFonts ); |
| 205 | + $testElement.attr( 'style', 'font-family: ' + invalidFont + ', ' + fallbackFonts ); |
177 | 206 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Attempted to load non-existing fonts specified in font-family' ); |
178 | 207 | assertTrue( $.inArray( invalidFont, mw.webfonts.fonts ) === -1, 'Font not loaded since it is not existing, including fallback fonts' ); |
179 | 208 | assertFalse( isFontFaceLoaded( invalidFont ), 'No new css rule added to the document since the font does not exist' ); |
— | — | @@ -183,8 +212,6 @@ |
184 | 213 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Loading fonts from font-family' ); |
185 | 214 | assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) >= 0, 'A fallback font was loaded' ); |
186 | 215 | assertTrue( isFontFaceLoaded( malayalamFont ), 'New css rule added to the document for fallback font' ); |
187 | | - |
188 | | - ok( $testElement.remove() ); |
189 | 216 | } ); |
190 | 217 | |
191 | 218 | test( '-- Build the menu', function() { |
— | — | @@ -212,37 +239,4 @@ |
213 | 240 | } |
214 | 241 | } ); |
215 | 242 | |
216 | | -// Tests end here |
217 | | - |
218 | | -isFontFaceLoaded = function( fontFamilyName ) { |
219 | | - var lastStyleIndex = document.styleSheets.length - 1; |
220 | | - |
221 | | - // Iterate from last. |
222 | | - for( var styleIndex = lastStyleIndex; styleIndex > 0; styleIndex-- ) { |
223 | | - var lastStyleSheet = document.styleSheets[styleIndex]; |
224 | | - if ( !lastStyleSheet ) { continue; } |
225 | | - if ( !lastStyleSheet.cssRules[0] ) { continue; } |
226 | | - var cssText = lastStyleSheet.cssRules[0].cssText; |
227 | | - if ( cssText.indexOf( '@font-face' ) >= 0 && cssText.indexOf( fontFamilyName ) >= 0 ) { |
228 | | - return true; |
229 | | - } |
230 | | - } |
231 | | - |
232 | | - return false; |
233 | | -}; |
234 | | - |
235 | | -// Convert a font-family string to an array. This is needed |
236 | | -// because browsers change the string by adding or removing spaces, |
237 | | -// so the string cannot be compared in a uniform way. |
238 | | -fontFamilyList = function( fontFamilyString ) { |
239 | | - // Create a list |
240 | | - var fontList = fontFamilyString.split( /, */ ); |
241 | | - |
242 | | - // Remove the quotes from font names |
243 | | - for ( var fontIndex = 0; fontIndex < fontList.length; ++fontIndex) { |
244 | | - fontList[fontIndex] = fontList[fontIndex].replace( /^["']/, '' ); |
245 | | - fontList[fontIndex] = fontList[fontIndex].replace( /["']$/, '' ); |
246 | | - } |
247 | | - |
248 | | - return fontList; |
249 | | -}; |
| 243 | +}()); |