2 QUnit
.module( 'jquery.accessKeyLabel', QUnit
.newMwEnvironment( {
9 var getAccessKeyPrefixTestData
= [
10 // ua string, platform string, expected prefix
12 [ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)', 'Win32', 'alt-' ],
13 [ 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)', 'Win32', 'alt-' ],
14 [ 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like Gecko', 'Win64', 'alt-' ],
16 [ '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-' ],
17 [ '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-' ],
18 [ 'Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1', 'Win32', 'alt-shift-' ],
20 [ '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-alt-' ],
21 [ '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-' ],
22 [ 'Mozilla/5.0 (X11; Linux i686) KHTML/4.9.1 (like Gecko) Konqueror/4.9', 'Linux i686', 'ctrl-' ],
24 [ 'Opera/9.80 (Windows NT 5.1)', 'Win32', 'shift-esc-' ],
25 [ '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-' ],
27 [ '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-' ],
28 [ 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30', 'Linux i686', 'alt-shift-' ]
30 // strings appended to title to make sure updateTooltipAccessKeys handles them correctly
31 updateTooltipAccessKeysTestData
= [ '', ' [a]', ' [test-a]', ' [alt-b]' ];
33 function makeInput( title
, accessKey
) {
34 // The properties aren't escaped, so make sure you don't call this function with values that need to be escaped!
35 return '<input title="' + title
+ '" ' + ( accessKey
? 'accessKey="' + accessKey
+ '" ' : '' ) + ' />';
38 QUnit
.test( 'getAccessKeyPrefix', getAccessKeyPrefixTestData
.length
, function ( assert
) {
40 for ( i
= 0; i
< getAccessKeyPrefixTestData
.length
; i
++ ) {
41 assert
.equal( $.fn
.updateTooltipAccessKeys
.getAccessKeyPrefix( {
42 userAgent
: getAccessKeyPrefixTestData
[ i
][ 0 ],
43 platform
: getAccessKeyPrefixTestData
[ i
][ 1 ]
44 } ), getAccessKeyPrefixTestData
[ i
][ 2 ], 'Correct prefix for ' + getAccessKeyPrefixTestData
[ i
][ 0 ] );
48 QUnit
.test( 'updateTooltipAccessKeys - current browser', 2, function ( assert
) {
49 var title
= $( makeInput( 'Title', 'a' ) ).updateTooltipAccessKeys().prop( 'title' ),
50 // The new title should be something like "Title [alt-a]", but the exact label will depend on the browser.
51 // The "a" could be capitalized, and the prefix could be anything, e.g. a simple "^" for ctrl-
52 // (no browser is known using such a short prefix, though) or "Alt+Umschalt+" in German Firefox.
53 result
= /^Title \[(.+)[aA]\]$/.exec( title
);
54 assert
.ok( result
, 'title should match expected structure.' );
55 assert
.notEqual( result
[ 1 ], 'test-', 'Prefix used for testing shouldn\'t be used in production.' );
58 QUnit
.test( 'updateTooltipAccessKeys - no access key', updateTooltipAccessKeysTestData
.length
, function ( assert
) {
59 var i
, oldTitle
, $input
, newTitle
;
60 for ( i
= 0; i
< updateTooltipAccessKeysTestData
.length
; i
++ ) {
61 oldTitle
= 'Title' + updateTooltipAccessKeysTestData
[ i
];
62 $input
= $( makeInput( oldTitle
) );
63 $( '#qunit-fixture' ).append( $input
);
64 newTitle
= $input
.updateTooltipAccessKeys().prop( 'title' );
65 assert
.equal( newTitle
, 'Title', 'title="' + oldTitle
+ '"' );
69 QUnit
.test( 'updateTooltipAccessKeys - with access key', updateTooltipAccessKeysTestData
.length
, function ( assert
) {
70 $.fn
.updateTooltipAccessKeys
.setTestMode( true );
71 var i
, oldTitle
, $input
, newTitle
;
72 for ( i
= 0; i
< updateTooltipAccessKeysTestData
.length
; i
++ ) {
73 oldTitle
= 'Title' + updateTooltipAccessKeysTestData
[ i
];
74 $input
= $( makeInput( oldTitle
, 'a' ) );
75 $( '#qunit-fixture' ).append( $input
);
76 newTitle
= $input
.updateTooltipAccessKeys().prop( 'title' );
77 assert
.equal( newTitle
, 'Title [test-a]', 'title="' + oldTitle
+ '"' );
79 $.fn
.updateTooltipAccessKeys
.setTestMode( false );
82 QUnit
.test( 'updateTooltipAccessKeys with label element', 2, function ( assert
) {
83 $.fn
.updateTooltipAccessKeys
.setTestMode( true );
84 var html
= '<label for="testInput" title="Title">Label</label><input id="testInput" accessKey="a" />',
86 $( '#qunit-fixture' ).html( html
);
87 $label
= $( '#qunit-fixture label' );
88 $input
= $( '#qunit-fixture input' );
89 $input
.updateTooltipAccessKeys();
90 assert
.equal( $input
.prop( 'title' ), '', 'No title attribute added to input element.' );
91 assert
.equal( $label
.prop( 'title' ), 'Title [test-a]', 'title updated for associated label element.' );
92 $.fn
.updateTooltipAccessKeys
.setTestMode( false );
95 QUnit
.test( 'updateTooltipAccessKeys with label element as parent', 2, function ( assert
) {
96 $.fn
.updateTooltipAccessKeys
.setTestMode( true );
97 var html
= '<label title="Title">Label<input id="testInput" accessKey="a" /></label>',
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 );