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