Debian Codegraph @

tests/unit/dialog/core.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/helper",
5 "ui/widgets/dialog"
6 ], function( QUnit, $, helper ) {
7 "use strict";
8
9 // TODO add afterEach callback to remove dialogs
10 QUnit.module( "dialog: core", { afterEach: helper.moduleAfterEach } );
11
12 QUnit.test( "markup structure", function( assert ) {
13 assert.expect( 11 );
14
15 var element = $( "<div>" ).dialog( {
16 buttons: [ {
17 text: "Ok",
18 click: $.noop
19 } ]
20 } ),
21 widget = element.dialog( "widget" ),
22 titlebar = widget.find( ".ui-dialog-titlebar" ),
23 title = titlebar.find( ".ui-dialog-title" ),
24 close = titlebar.find( ".ui-dialog-titlebar-close" ),
25 buttonpane = widget.find( ".ui-dialog-buttonpane" ),
26 buttonset = widget.find( ".ui-dialog-buttonset" ),
27 buttons = buttonset.find( ".ui-button" );
28
29 assert.hasClasses( widget, "ui-dialog ui-dialog-buttons ui-widget ui-widget-content" );
30 assert.hasClasses( titlebar, "ui-dialog-titlebar ui-widget-header" );
31 assert.equal( titlebar.length, 1, "Dialog has exactly one titlebar" );
32 assert.hasClasses( close, "ui-dialog-titlebar-close ui-widget" );
33 assert.equal( close.length, 1, "Titlebar has exactly one close button" );
34 assert.equal( title.length, 1, "Titlebar has exactly one title" );
35 assert.hasClasses( element, "ui-dialog-content ui-widget-content" );
36 assert.hasClasses( buttonpane, "ui-dialog-buttonpane ui-widget-content" );
37 assert.equal( buttonpane.length, 1, "Dialog has exactly one buttonpane" );
38 assert.equal( buttonset.length, 1, "Buttonpane has exactly one buttonset" );
39 assert.equal( buttons.length, 1, "Buttonset contains exactly 1 button when created with 1" );
40
41 } );
42
43 QUnit.test( "markup structure - no buttons", function( assert ) {
44 assert.expect( 7 );
45
46 var element = $( "<div>" ).dialog(),
47 widget = element.dialog( "widget" ),
48 titlebar = widget.find( ".ui-dialog-titlebar" ),
49 title = titlebar.find( ".ui-dialog-title" ),
50 close = titlebar.find( ".ui-dialog-titlebar-close" );
51
52 assert.hasClasses( widget, "ui-dialog ui-widget ui-widget-content" );
53 assert.hasClasses( titlebar, "ui-dialog-titlebar ui-widget-header" );
54 assert.equal( titlebar.length, 1, "Dialog has exactly one titlebar" );
55 assert.hasClasses( close, "ui-dialog-titlebar-close ui-widget" );
56 assert.equal( close.length, 1, "Titlebar has exactly one close button" );
57 assert.equal( title.length, 1, "Titlebar has exactly one title" );
58 assert.hasClasses( element, "ui-dialog-content ui-widget-content" );
59 } );
60
61 QUnit.test( "title id", function( assert ) {
62 assert.expect( 1 );
63
64 var titleId,
65 element = $( "<div>" ).dialog();
66
67 titleId = element.dialog( "widget" ).find( ".ui-dialog-title" ).attr( "id" );
68 assert.ok( /ui-id-\d+$/.test( titleId ), "auto-numbered title id" );
69 element.remove();
70 } );
71
72 QUnit.test( "ARIA", function( assert ) {
73 assert.expect( 4 );
74
75 var element = $( "<div>" ).dialog(),
76 wrapper = element.dialog( "widget" );
77 assert.equal( wrapper.attr( "role" ), "dialog", "dialog role" );
78 assert.equal( wrapper.attr( "aria-labelledby" ), wrapper.find( ".ui-dialog-title" ).attr( "id" ) );
79 assert.equal( wrapper.attr( "aria-describedby" ), element.attr( "id" ), "aria-describedby added" );
80 element.remove();
81
82 element = $( "<div><div aria-describedby='section2'><p id='section2'>descriotion</p></div></div>" ).dialog();
83 assert.equal( element.dialog( "widget" ).attr( "aria-describedby" ), null, "no aria-describedby added, as already present in markup" );
84 element.remove();
85 } );
86
87 QUnit.test( "aria-modal", function( assert ) {
88 assert.expect( 9 );
89
90 var element, wrapper;
91
92 element = $( "<div>" ).dialog( { modal: true } );
93 wrapper = element.dialog( "widget" );
94 assert.equal( wrapper.attr( "aria-modal" ), "true", "modal option set to true, aria-modal attribute added" );
95 element.dialog( "option", "modal", false );
96 assert.equal( wrapper.attr( "aria-modal" ), undefined, "modal option set to false, aria-modal attribute not added" );
97 element.dialog( "option", "modal", true );
98 assert.equal( wrapper.attr( "aria-modal" ), "true", "modal option set to true, aria-modal attribute added" );
99 element.remove();
100
101 element = $( "<div>" ).dialog( { modal: false } );
102 wrapper = element.dialog( "widget" );
103 assert.equal( wrapper.attr( "aria-modal" ), undefined, "modal option set to false, aria-modal attribute not added" );
104 element.dialog( "option", "modal", true );
105 assert.equal( wrapper.attr( "aria-modal" ), "true", "modal option set to true, aria-modal attribute added" );
106 element.dialog( "option", "modal", false );
107 assert.equal( wrapper.attr( "aria-modal" ), undefined, "modal option set to false, aria-modal attribute not added" );
108 element.remove();
109
110 element = $( "<div>" ).dialog();
111 wrapper = element.dialog( "widget" );
112 assert.equal( wrapper.attr( "aria-modal" ), undefined, "modal option not set, aria-modal attribute not added" );
113 element.dialog( "option", "modal", true );
114 assert.equal( wrapper.attr( "aria-modal" ), "true", "modal option set to true, aria-modal attribute added" );
115 element.dialog( "option", "modal", false );
116 assert.equal( wrapper.attr( "aria-modal" ), undefined, "modal option set to false, aria-modal attribute not added" );
117 element.remove();
118 } );
119
120 QUnit.test( "ui dialog title heading level", function( assert ) {
121 assert.expect( 8 );
122
123 var element, nodeName;
124
125 element = $( "<div>" ).dialog( { modal: true } );
126 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
127 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
128
129 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 0 } );
130 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
131 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
132
133 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 1 } );
134 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
135 assert.equal( nodeName, "h1", "Element wrapping the dialog title is h1" );
136
137 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 6 } );
138 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
139 assert.equal( nodeName, "h6", "Element wrapping the dialog title is h6" );
140
141 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 9 } );
142 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
143 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
144
145 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: -9 } );
146 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
147 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
148
149 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: 2.3 } );
150 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
151 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
152
153 element = $( "<div>" ).dialog( { modal: true, uiDialogTitleHeadingLevel: "foo" } );
154 nodeName = element.dialog( "widget" ).find( ".ui-dialog-title" ).get( 0 ).nodeName.toLowerCase();
155 assert.equal( nodeName, "span", "Element wrapping the dialog title is span" );
156 } );
157
158 QUnit.test( "widget method", function( assert ) {
159 assert.expect( 1 );
160 var dialog = $( "<div>" ).appendTo( "#qunit-fixture" ).dialog();
161 assert.deepEqual( dialog.parent()[ 0 ], dialog.dialog( "widget" )[ 0 ] );
162 dialog.remove();
163 } );
164
165 QUnit.test( "focus tabbable", function( assert ) {
166 var ready = assert.async();
167 assert.expect( 8 );
168 var element,
169 options = {
170 buttons: [ {
171 text: "Ok",
172 click: $.noop
173 } ]
174 };
175
176 function checkFocus( markup, options, testFn, next ) {
177 element = $( markup ).dialog( options );
178 setTimeout( function() {
179 testFn( function done() {
180 element.remove();
181 setTimeout( next );
182 } );
183 } );
184 }
185
186 function step1() {
187 checkFocus( "<div><input><input></div>", options, function( done ) {
188 var input = element.find( "input" ).last().trigger( "focus" ).trigger( "blur" );
189
190 element.dialog( "instance" )._focusTabbable();
191 setTimeout( function() {
192 assert.equal( document.activeElement, input[ 0 ],
193 "1. an element that was focused previously." );
194 done();
195 } );
196 }, step2 );
197 }
198
199 function step2() {
200 checkFocus( "<div><input><input autofocus></div>", options, function( done ) {
201 assert.equal( document.activeElement, element.find( "input" )[ 1 ],
202 "2. first element inside the dialog matching [autofocus]" );
203 done();
204 }, step3 );
205 }
206
207 function step3() {
208 checkFocus( "<div><input><input></div>", options, function( done ) {
209 assert.equal( document.activeElement, element.find( "input" )[ 0 ],
210 "3. tabbable element inside the content element" );
211 done();
212 }, step4 );
213 }
214
215 function step4() {
216 checkFocus( "<div>text</div>", options, function( done ) {
217 assert.equal( document.activeElement,
218 element.dialog( "widget" ).find( ".ui-dialog-buttonpane button" )[ 0 ],
219 "4. tabbable element inside the buttonpane" );
220 done();
221 }, step5 );
222 }
223
224 function step5() {
225 checkFocus( "<div>text</div>", {}, function( done ) {
226 assert.equal( document.activeElement,
227 element.dialog( "widget" ).find( ".ui-dialog-titlebar .ui-dialog-titlebar-close" )[ 0 ],
228 "5. the close button" );
229 done();
230 }, step6 );
231 }
232
233 function step6() {
234 checkFocus( "<div>text</div>", { autoOpen: false }, function( done ) {
235 element.dialog( "widget" ).find( ".ui-dialog-titlebar-close" ).hide();
236 element.dialog( "open" );
237 setTimeout( function() {
238 assert.equal( document.activeElement, element.parent()[ 0 ], "6. the dialog itself" );
239 done();
240 } );
241 }, step7 );
242 }
243
244 function step7() {
245 checkFocus(
246 "<div><input><input autofocus></div>",
247 {
248 open: function() {
249 var inputs = $( this ).find( "input" );
250 inputs.last().on( "keydown", function( event ) {
251 event.preventDefault();
252 inputs.first().trigger( "focus" );
253 } );
254 }
255 },
256 function( done ) {
257 var inputs = element.find( "input"
258 );
259 assert.equal( document.activeElement, inputs[ 1 ], "Focus starts on second input" );
260 inputs.last().simulate( "keydown", { keyCode: $.ui.keyCode.TAB } );
261 setTimeout( function() {
262 assert.equal( document.activeElement, inputs[ 0 ],
263 "Honor preventDefault, allowing custom focus management" );
264 done();
265 }, 50 );
266 },
267 ready
268 );
269 }
270
271 step1();
272 } );
273
274 QUnit.test( "#7960: resizable handles below modal overlays", function( assert ) {
275 assert.expect( 1 );
276
277 var resizable = $( "<div>" ).resizable(),
278 dialog = $( "<div>" ).dialog( { modal: true } ),
279 resizableZindex = parseInt( resizable.find( ".ui-resizable-handle" ).css( "zIndex" ), 10 ),
280 overlayZindex = parseInt( $( ".ui-widget-overlay" ).css( "zIndex" ), 10 );
281
282 assert.ok( resizableZindex < overlayZindex, "Resizable handles have lower z-index than modal overlay" );
283 dialog.dialog( "destroy" );
284 } );
285
286 QUnit.test( "Prevent tabbing out of dialogs", function( assert ) {
287 var ready = assert.async();
288 assert.expect( 3 );
289
290 var element = $( "<div><input name='0'><input name='1'></div>" ).dialog(),
291 inputs = element.find( "input" );
292
293 // Remove close button to test focus on just the two buttons
294 element.dialog( "widget" ).find( ".ui-button" ).remove();
295
296 function checkTab() {
297 assert.equal( document.activeElement, inputs[ 0 ], "Tab key event move d focus within the modal" );
298
299 // Check shift tab
300 $( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.TAB, shiftKey: true } );
301 setTimeout( checkShiftTab );
302 }
303
304 function checkShiftTab() {
305 assert.equal( document.activeElement, inputs[ 1 ], "Shift-Tab key event moved focus back to second input" );
306
307 element.remove();
308 setTimeout( ready );
309 }
310
311 inputs[ 1 ].focus();
312 setTimeout( function() {
313 assert.equal( document.activeElement, inputs[ 1 ], "Focus set on second input" );
314 inputs.eq( 1 ).simulate( "keydown", { keyCode: $.ui.keyCode.TAB } );
315
316 setTimeout( checkTab );
317 } );
318 } );
319
320 QUnit.test( "#9048: multiple modal dialogs opened and closed in different order",
321 function( assert ) {
322 var ready = assert.async();
323 assert.expect( 1 );
324 $( "#dialog1, #dialog2" ).dialog( { autoOpen: false, modal: true } );
325 $( "#dialog1" ).dialog( "open" );
326 $( "#dialog2" ).dialog( "open" );
327 $( "#dialog1" ).dialog( "close" );
328 setTimeout( function() {
329 $( "#dialog2" ).dialog( "close" );
330 $( "#favorite-animal" ).trigger( "focus" );
331 assert.ok( true, "event handlers cleaned up (no errors thrown)" );
332 ready();
333 } );
334 } );
335
336 QUnit.test( "interaction between overlay and other dialogs", function( assert ) {
337 var ready = assert.async();
338 $.widget( "ui.testWidget", $.ui.dialog, {
339 options: {
340 modal: true,
341 autoOpen: false
342 }
343 } );
344 assert.expect( 2 );
345 var first = $( "<div><input id='input-1'></div>" ).dialog( {
346 modal: true
347 } ),
348 firstInput = first.find( "input" ),
349 second = $( "<div><input id='input-2'></div>" ).testWidget(),
350 secondInput = second.find( "input" );
351
352 // Wait for the modal to init
353 setTimeout( function() {
354
355 second.testWidget( "open" );
356
357 // Simulate user tabbing from address bar to an element outside the dialog
358 $( "#favorite-animal" ).trigger( "focus" );
359 setTimeout( function() {
360 assert.equal( document.activeElement, secondInput[ 0 ] );
361
362 // Last active dialog must receive focus
363 firstInput.trigger( "focus" );
364 $( "#favorite-animal" ).trigger( "focus" );
365 setTimeout( function() {
366 assert.equal( document.activeElement, firstInput[ 0 ] );
367
368 // Cleanup
369 first.remove();
370 second.remove();
371 delete $.ui.testWidget;
372 delete $.fn.testWidget;
373 ready();
374 } );
375 } );
376 } );
377 } );
378
379 } );