Index: trunk/extensions/WebFonts/tests/qunit/ext.webfonts.tests.js |
— | — | @@ -1,3 +1,12 @@ |
| 2 | +/** |
| 3 | + * QUnit tests for WebFonts |
| 4 | + * |
| 5 | + * @file |
| 6 | + * @author Santhosh Thottingal, Amir E. Aharoni |
| 7 | + * @copyright Copyright © 2012 Santhosh Thottingal, Amir E. Aharoni |
| 8 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 9 | + */ |
| 10 | + |
2 | 11 | module( 'ext.webfonts', QUnit.newMwEnvironment() ); |
3 | 12 | |
4 | 13 | test( '-- Initial check', function() { |
— | — | @@ -7,9 +16,10 @@ |
8 | 17 | // TODO: need a better way to test this |
9 | 18 | ok( mw.webfonts, 'The WebFonts extension is not supposed to run in a blacklisted browser - ' |
10 | 19 | + navigator.appName + ' ' + navigator.userAgent ); |
11 | | - } else { |
12 | | - ok( mw.webfonts, 'mw.webfonts is defined and the browser is supported' ); |
| 20 | + return; |
13 | 21 | } |
| 22 | + |
| 23 | + ok( mw.webfonts, 'mw.webfonts is defined and the browser is supported' ); |
14 | 24 | } ); |
15 | 25 | |
16 | 26 | test( '-- Application of a web font to the page and its removal', function() { |
— | — | @@ -17,7 +27,7 @@ |
18 | 28 | return; |
19 | 29 | } |
20 | 30 | |
21 | | - expect( 25 ); |
| 31 | + expect( 27 ); |
22 | 32 | |
23 | 33 | var invalidFont = 'NonExistingFont'; |
24 | 34 | assertTrue( mw.webfonts.set( invalidFont ) === undefined, 'A non-existent font is not initialized' ); |
— | — | @@ -57,6 +67,9 @@ |
58 | 68 | deepEqual( fontFamilyList( $textareaElement.css( 'font-family' ) ), |
59 | 69 | expectedFontFamilyValue, 'The web font was applied to font-family of textarea' ); |
60 | 70 | |
| 71 | + // Cookie set |
| 72 | + equals( $.cookie( 'webfonts-font' ), teluguFont, 'Correct cookie for the font was set' ); |
| 73 | + |
61 | 74 | // Reset everything |
62 | 75 | ok( mw.webfonts.set( false ) === undefined, 'Reset body after testing font application' ); |
63 | 76 | equals( $body.css( 'font-family' ), oldConfig.fontFamily, 'Previous font-family for body was restored' ); |
— | — | @@ -72,6 +85,9 @@ |
73 | 86 | ok( $selectElement.remove(), 'The select test element was removed from body' ); |
74 | 87 | ok( $textareaElement.remove(), 'The textarea test element was removed from body' ); |
75 | 88 | |
| 89 | + // Cookie set |
| 90 | + equals( $.cookie( 'webfonts-font' ), 'none', 'The cookie was removed' ); |
| 91 | + |
76 | 92 | $body.attr( 'lang', bodyLang ); |
77 | 93 | } ); |
78 | 94 | |
— | — | @@ -86,11 +102,11 @@ |
87 | 103 | mw.webfonts.fonts = []; |
88 | 104 | var cssRulesLength = document.styleSheets.length; |
89 | 105 | assertTrue( mw.webfonts.addFont( validFontName ), 'Add a Devanagari font' ); |
90 | | - assertTrue( $.inArray( validFontName, mw.webfonts.fonts ) >= 0 , 'Devanagari font loaded' ); |
| 106 | + assertTrue( $.inArray( validFontName, mw.webfonts.fonts ) >= 0, 'Devanagari font loaded' ); |
91 | 107 | assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'New css rule added to the document' ); |
92 | 108 | var loadedFontsSize = mw.webfonts.fonts.length; |
93 | 109 | assertTrue( mw.webfonts.addFont( validFontName ), 'Add the Devanagari font again' ); |
94 | | - assertTrue( loadedFontsSize === mw.webfonts.fonts.length , 'A font that is already loaded is not loaded again' ); |
| 110 | + assertTrue( loadedFontsSize === mw.webfonts.fonts.length, 'A font that is already loaded is not loaded again' ); |
95 | 111 | assertFalse( mw.webfonts.addFont( 'Some non-existing font' ), 'addFont returns false if the font was not found' ); |
96 | 112 | assertTrue( cssRulesLength + 1 === document.styleSheets.length, 'Loading the font does not add new css rules' ); |
97 | 113 | } ); |
— | — | @@ -117,15 +133,15 @@ |
118 | 134 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute' ); |
119 | 135 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The element has no webfonts-lang-attr class since there is no lang attribute' ); |
120 | 136 | |
121 | | - ok( $testElement.attr( 'lang' , 'en' ), 'The lang attribute of the test element was set to en (English)' ); |
| 137 | + ok( $testElement.attr( 'lang', 'en' ), 'The lang attribute of the test element was set to en (English)' ); |
122 | 138 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute en' ); |
123 | 139 | assertFalse( $testElement.hasClass( 'webfonts-lang-attr' ), 'The test element has no webfonts-lang-attr class since en lang has no fonts available' ); |
124 | 140 | |
125 | 141 | var tamilFont = mw.webfonts.config.languages.ta[0]; |
126 | | - ok( $testElement.attr( 'lang' , 'ta' ), 'Set lang attribute to ta (Tamil)' ); |
| 142 | + ok( $testElement.attr( 'lang', 'ta' ), 'Set lang attribute to ta (Tamil)' ); |
127 | 143 | ok( mw.webfonts.loadFontsForLangAttr(), 'Attempted to load fonts for the lang attribute ta' ); |
128 | 144 | assertTrue( $testElement.hasClass( 'webfonts-lang-attr' ), 'The test element has webfonts-lang-attr class' ); |
129 | | - assertTrue( $.inArray( tamilFont, mw.webfonts.fonts ) >= 0 , 'Tamil font loaded' ); |
| 145 | + assertTrue( $.inArray( tamilFont, mw.webfonts.fonts ) >= 0, 'Tamil font loaded' ); |
130 | 146 | assertTrue( isFontFaceLoaded( tamilFont ), 'New css rule font-face was added to the document for Tamil font' ); |
131 | 147 | |
132 | 148 | ok( mw.webfonts.reset(), 'Reset webfonts after testing application by lang' ); |
— | — | @@ -150,22 +166,22 @@ |
151 | 167 | var latinWebFont = 'RufScript'; |
152 | 168 | var fallbackFonts = 'Helvetica, Arial, sans-serif'; |
153 | 169 | $testElement.attr( 'style','font-family: ' + latinWebFont + ', ' + fallbackFonts ); |
154 | | - assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) === -1 , 'Latin font not loaded yet' ); |
| 170 | + assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) === -1, 'Latin font not loaded yet' ); |
155 | 171 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Loaded fonts from font-family' ); |
156 | | - assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) >= 0 , 'Latin font loaded' ); |
| 172 | + assertTrue( $.inArray( latinWebFont, mw.webfonts.fonts ) >= 0, 'Latin font loaded' ); |
157 | 173 | assertTrue( isFontFaceLoaded( latinWebFont ), 'New css rule added to the document for Latin' ); |
158 | 174 | |
159 | 175 | var invalidFont = 'NonExistingFont'; |
160 | 176 | $testElement.attr( 'style','font-family: ' + invalidFont + ', ' + fallbackFonts ); |
161 | 177 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Attempted to load non-existing fonts specified in font-family' ); |
162 | | - assertTrue( $.inArray( invalidFont, mw.webfonts.fonts ) === -1 , 'Font not loaded since it is not existing, including fallback fonts' ); |
| 178 | + assertTrue( $.inArray( invalidFont, mw.webfonts.fonts ) === -1, 'Font not loaded since it is not existing, including fallback fonts' ); |
163 | 179 | assertFalse( isFontFaceLoaded( invalidFont ), 'No new css rule added to the document since the font does not exist' ); |
164 | 180 | |
165 | 181 | var malayalamFont = mw.webfonts.config.languages.ml[0]; |
166 | 182 | $testElement.attr( 'style', 'font-family: ' + invalidFont + ', ' + malayalamFont + ', ' + fallbackFonts ); |
167 | | - assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) === -1 , 'Fallback font not loaded yet' ); |
| 183 | + assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) === -1, 'Fallback font not loaded yet' ); |
168 | 184 | ok( mw.webfonts.loadFontsForFontFamilyStyle(), 'Loading fonts from font-family' ); |
169 | | - assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) >= 0 , 'A fallback font was loaded' ); |
| 185 | + assertTrue( $.inArray( malayalamFont, mw.webfonts.fonts ) >= 0, 'A fallback font was loaded' ); |
170 | 186 | assertTrue( isFontFaceLoaded( malayalamFont ), 'New css rule added to the document for fallback font' ); |
171 | 187 | |
172 | 188 | ok( $testElement.remove() ); |
— | — | @@ -176,18 +192,19 @@ |
177 | 193 | return; |
178 | 194 | } |
179 | 195 | |
180 | | - expect( 8 ); |
| 196 | + expect( 9 ); |
181 | 197 | |
182 | 198 | var oldFonts = mw.webfonts.fonts; |
183 | 199 | var fonts = []; |
184 | 200 | assertFalse( mw.webfonts.buildMenu( fonts ), 'Build the menu with empty fonts list' ); |
185 | 201 | fonts = mw.webfonts.config.languages.hi; |
186 | | - ok( mw.webfonts.buildMenu( fonts ) , 'Build the menu with Hindi fonts list' ); |
187 | | - equals( $( 'li#pt-webfont' ).length , 1, 'There should be one and only one menu at any time' ); |
188 | | - ok( mw.webfonts.buildMenu( fonts ) , 'Build the menu with Hindi fonts list again' ); |
189 | | - equals( $( 'li#pt-webfont' ).length , 1, 'There should be one and only one menu at any time' ); |
| 202 | + ok( mw.webfonts.buildMenu( fonts ), 'Build the menu with Hindi fonts list' ); |
| 203 | + equals( $( 'li#pt-webfont' ).length, 1, 'There should be one and only one menu at any time' ); |
| 204 | + ok( mw.webfonts.buildMenu( fonts ), 'Build the menu with Hindi fonts list again' ); |
| 205 | + equals( $( 'li#pt-webfont' ).length, 1, 'There should be one and only one menu at any time' ); |
190 | 206 | equals( $( 'ul#webfonts-fontsmenu li' ).length, fonts.length + 2, 'Number of menu items is number of availables fonts, a help link and reset item' ); |
191 | | - equals ( $( 'li.webfont-help-item').length, 1, 'Help link exists' ); |
| 207 | + equals( $( 'li.webfont-help-item').length, 1, 'Help link exists' ); |
| 208 | + equals( $( 'input#webfont-none' ).length, 1, 'Reset link exists' ); |
192 | 209 | if (oldFonts.length) { |
193 | 210 | assertTrue( mw.webfonts.buildMenu( oldFonts ), 'Restore the menu' ); |
194 | 211 | } else { |