]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/qunit/suites/resources/jquery/jquery.accessKeyLabel.test.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.accessKeyLabel.test.js
1 ( function ( $ ) {
2         var getAccessKeyPrefixTestData, updateTooltipAccessKeysTestData;
3
4         QUnit.module( 'jquery.accessKeyLabel', QUnit.newMwEnvironment( {
5                 messages: {
6                         brackets: '[$1]',
7                         'word-separator': ' '
8                 }
9         } ) );
10
11         getAccessKeyPrefixTestData = [
12                 // ua string, platform string, expected prefix
13                 // Internet Explorer
14                 [ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)', 'Win32', 'alt-' ],
15                 [ 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)', 'Win32', 'alt-' ],
16                 [ 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like Gecko', 'Win64', 'alt-' ],
17                 [ 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136', 'Win64', 'alt-' ],
18                 // Firefox
19                 [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.19) Gecko/20110420 Firefox/3.5.19', 'MacIntel', 'ctrl-' ],
20                 [ 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17', 'Linux i686', 'alt-shift-' ],
21                 [ 'Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 'Win32', 'alt-shift-' ],
22                 [ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:50.0) Gecko/20100101 Firefox/50.0', 'MacIntel', 'ctrl-option-' ],
23                 [ 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121202 Firefox/17.0 Iceweasel/17.0.1', 'Linux 1686', 'alt-shift-' ],
24                 [ 'Mozilla/5.0 (Windows NT 5.2; U; de; rv:1.8.0) Gecko/20060728 Firefox/1.5.0', 'Win32', 'alt-' ],
25                 // Safari / Konqueror
26                 [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; nl-nl) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7', 'MacIntel', 'ctrl-option-' ],
27                 [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; de-de) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.28.3', 'MacIntel', 'ctrl-' ],
28                 [ 'Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29', 'Win32', 'alt-' ],
29                 [ 'Mozilla/5.0 (Windows; U; Windows NT 6.0; cs-CZ) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7', 'Win32', 'alt-' ],
30                 [ 'Mozilla/5.0 (X11; Linux i686) KHTML/4.9.1 (like Gecko) Konqueror/4.9', 'Linux i686', 'ctrl-' ],
31                 // Opera
32                 [ 'Opera/9.80 (Windows NT 5.1)', 'Win32', 'shift-esc-' ],
33                 [ 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.130', 'Win32', 'alt-shift-' ],
34                 // Chrome
35                 [ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30', 'MacIntel', 'ctrl-option-' ],
36                 [ 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30', 'Linux i686', 'alt-shift-' ],
37                 // Unknown! Note: These aren't necessarily *right*, this is just
38                 // testing that we're getting the expected output based on the
39                 // platform.
40                 [ 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021111 Chimera/0.6', 'MacPPC', 'ctrl-' ],
41                 [ 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5', 'Linux i686', 'alt-' ]
42         ];
43         // strings appended to title to make sure updateTooltipAccessKeys handles them correctly
44         updateTooltipAccessKeysTestData = [ '', ' [a]', ' [test-a]', ' [alt-b]' ];
45
46         function makeInput( title, accessKey ) {
47                 // The properties aren't escaped, so make sure you don't call this function with values that need to be escaped!
48                 return '<input title="' + title + '" ' + ( accessKey ? 'accessKey="' + accessKey + '" ' : '' ) + ' />';
49         }
50
51         QUnit.test( 'getAccessKeyPrefix', function ( assert ) {
52                 var i;
53                 for ( i = 0; i < getAccessKeyPrefixTestData.length; i++ ) {
54                         assert.equal( $.fn.updateTooltipAccessKeys.getAccessKeyPrefix( {
55                                 userAgent: getAccessKeyPrefixTestData[ i ][ 0 ],
56                                 platform: getAccessKeyPrefixTestData[ i ][ 1 ]
57                         } ), getAccessKeyPrefixTestData[ i ][ 2 ], 'Correct prefix for ' + getAccessKeyPrefixTestData[ i ][ 0 ] );
58                 }
59         } );
60
61         QUnit.test( 'updateTooltipAccessKeys - current browser', function ( assert ) {
62                 var title = $( makeInput( 'Title', 'a' ) ).updateTooltipAccessKeys().prop( 'title' ),
63                         // The new title should be something like "Title [alt-a]", but the exact label will depend on the browser.
64                         // The "a" could be capitalized, and the prefix could be anything, e.g. a simple "^" for ctrl-
65                         // (no browser is known using such a short prefix, though) or "Alt+Umschalt+" in German Firefox.
66                         result = /^Title \[(.+)[aA]\]$/.exec( title );
67                 assert.ok( result, 'title should match expected structure.' );
68                 assert.notEqual( result[ 1 ], 'test-', 'Prefix used for testing shouldn\'t be used in production.' );
69         } );
70
71         QUnit.test( 'updateTooltipAccessKeys - no access key', function ( assert ) {
72                 var i, oldTitle, $input, newTitle;
73                 for ( i = 0; i < updateTooltipAccessKeysTestData.length; i++ ) {
74                         oldTitle = 'Title' + updateTooltipAccessKeysTestData[ i ];
75                         $input = $( makeInput( oldTitle ) );
76                         $( '#qunit-fixture' ).append( $input );
77                         newTitle = $input.updateTooltipAccessKeys().prop( 'title' );
78                         assert.equal( newTitle, 'Title', 'title="' + oldTitle + '"' );
79                 }
80         } );
81
82         QUnit.test( 'updateTooltipAccessKeys - with access key', function ( assert ) {
83                 var i, oldTitle, $input, newTitle;
84                 $.fn.updateTooltipAccessKeys.setTestMode( true );
85                 for ( i = 0; i < updateTooltipAccessKeysTestData.length; i++ ) {
86                         oldTitle = 'Title' + updateTooltipAccessKeysTestData[ i ];
87                         $input = $( makeInput( oldTitle, 'a' ) );
88                         $( '#qunit-fixture' ).append( $input );
89                         newTitle = $input.updateTooltipAccessKeys().prop( 'title' );
90                         assert.equal( newTitle, 'Title [test-a]', 'title="' + oldTitle + '"' );
91                 }
92                 $.fn.updateTooltipAccessKeys.setTestMode( false );
93         } );
94
95         QUnit.test( 'updateTooltipAccessKeys with label element', function ( assert ) {
96                 var html, $label, $input;
97                 $.fn.updateTooltipAccessKeys.setTestMode( true );
98                 html = '<label for="testInput" title="Title">Label</label><input id="testInput" accessKey="a" />';
99                 $( '#qunit-fixture' ).html( html );
100                 $label = $( '#qunit-fixture label' );
101                 $input = $( '#qunit-fixture input' );
102                 $input.updateTooltipAccessKeys();
103                 assert.equal( $input.prop( 'title' ), '', 'No title attribute added to input element.' );
104                 assert.equal( $label.prop( 'title' ), 'Title [test-a]', 'title updated for associated label element.' );
105                 $.fn.updateTooltipAccessKeys.setTestMode( false );
106         } );
107
108         QUnit.test( 'updateTooltipAccessKeys with label element as parent', function ( assert ) {
109                 var html, $label, $input;
110                 $.fn.updateTooltipAccessKeys.setTestMode( true );
111                 html = '<label title="Title">Label<input id="testInput" accessKey="a" /></label>';
112                 $( '#qunit-fixture' ).html( html );
113                 $label = $( '#qunit-fixture label' );
114                 $input = $( '#qunit-fixture input' );
115                 $input.updateTooltipAccessKeys();
116                 assert.equal( $input.prop( 'title' ), '', 'No title attribute added to input element.' );
117                 assert.equal( $label.prop( 'title' ), 'Title [test-a]', 'title updated for associated label element.' );
118                 $.fn.updateTooltipAccessKeys.setTestMode( false );
119         } );
120
121 }( jQuery ) );