| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 /* 2 * dialog unit tests 3 */ 4 (function($) { 5 // 6 // Dialog Test Helper Functions 7 // 8 9 var defaults = { 10 autoOpen: true, 11 autoResize: true, 12 buttons: {}, 13 closeOnEscape: true, 14 closeText: 'close', 15 disabled: false, 16 dialogClass: undefined, 17 draggable: true, 18 height: 200, 19 maxHeight: undefined, 20 maxWidth: undefined, 21 minHeight: 100, 22 minWidth: 150, 23 modal: false, 24 overlay: {}, 25 position: 'center', 26 resizable: true, 27 stack: true, 28 title: '', 29 width: 300 30 }; 31 32 var el, 33 offsetBefore, offsetAfter, 34 heightBefore, heightAfter, 35 widthBefore, widthAfter, 36 dragged; 37 38 function dlg() { 39 return el.data("dialog").element.parents(".ui-dialog:first"); 40 } 41 42 function isOpen(why) { 43 ok(dlg().is(":visible"), why); 44 } 45 46 function isNotOpen(why) { 47 ok(!dlg().is(":visible"), why); 48 } 49 50 function drag(handle, dx, dy) { 51 var d = dlg(); 52 offsetBefore = d.offset(); 53 heightBefore = d.height(); 54 widthBefore = d.width(); 55 //this mouseover is to work around a limitation in resizable 56 //TODO: fix resizable so handle doesn't require mouseover in order to be used 57 $(handle, d).simulate("mouseover"); 58 $(handle, d).simulate("drag", { 59 dx: dx || 0, 60 dy: dy || 0 61 }); 62 dragged = { dx: dx, dy: dy }; 63 offsetAfter = d.offset(); 64 heightAfter = d.height(); 65 widthAfter = d.width(); 66 } 67 68 function moved(dx, dy, msg) { 69 msg = msg ? msg + "." : ""; 70 var actual = { left: offsetAfter.left, top: offsetAfter.top }; 71 var expected = { left: offsetBefore.left + dx, top: offsetBefore.top + dy }; 72 same(actual, expected, 'dragged[' + dragged.dx + ', ' + dragged.dy + '] ' + msg); 73 } 74 75 function shouldmove(why) { 76 var handle = $(".ui-dialog-titlebar", dlg()); 77 drag(handle, 50, 50); 78 moved(50, 50, why); 79 } 80 81 function shouldnotmove(why) { 82 var handle = $(".ui-dialog-titlebar", dlg()); 83 drag(handle, 50, 50); 84 moved(0, 0, why); 85 } 86 87 function resized(dw, dh, msg) { 88 msg = msg ? msg + "." : ""; 89 var actual = { width: widthAfter, height: heightAfter }; 90 var expected = { width: widthBefore + dw, height: heightBefore + dh }; 91 same(actual, expected, 'resized[' + dragged.dx + ', ' + dragged.dy + '] ' + msg); 92 } 93 94 function shouldresize(why) { 95 var handle = $(".ui-resizable-se", dlg()); 96 drag(handle, 50, 50); 97 resized(50, 50, why); 98 } 99 100 function shouldnotresize(why) { 101 var handle = $(".ui-resizable-se", dlg()); 102 drag(handle, 50, 50); 103 resized(0, 0, why); 104 } 105 106 function broder(el, side){ 107 return parseInt(el.css('border-' + side + '-width'), 10); 108 } 109 110 function margin(el, side) { 111 return parseInt(el.css('margin-' + side), 10); 112 } 113 114 // Dialog Tests 115 module("dialog"); 116 117 test("init", function() { 118 expect(6); 119 120 $("<div></div>").appendTo('body').dialog().remove(); 121 ok(true, '.dialog() called on element'); 122 123 $([]).dialog().remove(); 124 ok(true, '.dialog() called on empty collection'); 125 126 $('<div></div>').dialog().remove(); 127 ok(true, '.dialog() called on disconnected DOMElement'); 128 129 $('<div></div>').dialog().dialog("foo").remove(); 130 ok(true, 'arbitrary method called after init'); 131 132 el = $('<div></div>').dialog(); 133 var foo = el.data("foo.dialog"); 134 el.remove(); 135 ok(true, 'arbitrary option getter after init'); 136 137 $('<div></div>').dialog().data("foo.dialog", "bar").remove(); 138 ok(true, 'arbitrary option setter after init'); 139 }); 140 141 test("destroy", function() { 142 expect(6); 143 144 $("<div></div>").appendTo('body').dialog().dialog("destroy").remove(); 145 ok(true, '.dialog("destroy") called on element'); 146 147 $([]).dialog().dialog("destroy").remove(); 148 ok(true, '.dialog("destroy") called on empty collection'); 149 150 $('<div></div>').dialog().dialog("destroy").remove(); 151 ok(true, '.dialog("destroy") called on disconnected DOMElement'); 152 153 $('<div></div>').dialog().dialog("destroy").dialog("foo").remove(); 154 ok(true, 'arbitrary method called after destroy'); 155 156 el = $('<div></div>').dialog(); 157 var foo = el.dialog("destroy").data("foo.dialog"); 158 el.remove(); 159 ok(true, 'arbitrary option getter after destroy'); 160 161 $('<div></div>').dialog().dialog("destroy").data("foo.dialog", "bar").remove(); 162 ok(true, 'arbitrary option setter after destroy'); 163 }); 164 165 /* 166 //This one takes a while to run 167 168 test("element types", function() { 169 var typeNames = ('p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form' 170 + ',table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr' 171 + ',acronym,code,samp,kbd,var,img,object,hr' 172 + ',input,button,label,select,iframe').split(','); 173 174 $.each(typeNames, function(i) { 175 var typeName = typeNames[i]; 176 el = $(document.createElement(typeName)).appendTo('body'); 177 (typeName == 'table' && el.append("<tr><td>content</td></tr>")); 178 el.dialog(); 179 ok(true, '$("<' + typeName + '/>").dialog()'); 180 el.dialog("destroy"); 181 el.remove(); 182 }); 183 }); 184 185 */ 186 187 test("defaults", function() { 188 el = $('<div></div>').dialog(); 189 $.each(defaults, function(key, val) { 190 var actual = el.data(key + ".dialog"), expected = val; 191 same(actual, expected, key); 192 }); 193 el.remove(); 194 }); 195 196 test("title id", function() { 197 expect(3); 198 199 var titleId; 200 201 // reset the uuid so we know what values to expect 202 $.ui.dialog.uuid = 0; 203 204 el = $('<div></div>').dialog(); 205 titleId = dlg().find('.ui-dialog-title').attr('id'); 206 equals(titleId, 'ui-dialog-title-1', 'auto-numbered title id'); 207 el.remove(); 208 209 el = $('<div></div>').dialog(); 210 titleId = dlg().find('.ui-dialog-title').attr('id'); 211 equals(titleId, 'ui-dialog-title-2', 'auto-numbered title id'); 212 el.remove(); 213 214 el = $('<div id="foo"/>').dialog(); 215 titleId = dlg().find('.ui-dialog-title').attr('id'); 216 equals(titleId, 'ui-dialog-title-foo', 'carried over title id'); 217 el.remove(); 218 }); 219 220 test("ARIA", function() { 221 expect(4); 222 223 el = $('<div></div>').dialog(); 224 225 equals(dlg().attr('role'), 'dialog', 'dialog role'); 226 227 var labelledBy = dlg().attr('aria-labelledby'); 228 ok(labelledBy.length > 0, 'has aria-labelledby attribute'); 229 equals(dlg().find('.ui-dialog-title').attr('id'), labelledBy, 230 'proper aria-labelledby attribute'); 231 232 equals(dlg().find('.ui-dialog-titlebar-close').attr('role'), 'button', 233 'close link role'); 234 235 el.remove(); 236 }); 237 238 module("dialog: Options"); 239 240 test("autoOpen", function() { 241 expect(2); 242 243 el = $('<div></div>').dialog({ autoOpen: false }); 244 isNotOpen('.dialog({ autoOpen: false })'); 245 el.remove(); 246 247 el = $('<div></div>').dialog({ autoOpen: true }); 248 isOpen('.dialog({ autoOpen: true })'); 249 el.remove(); 250 }); 251 252 test("autoResize", function() { 253 expect(2); 254 255 var actual, 256 before, 257 expected, 258 handle; 259 260 el = $('<div>content<br>content<br>content<br>content<br>content</div>').dialog({ autoResize: false }); 261 expected = { height: el.height() }; 262 handle = $(".ui-resizable-se", dlg()); 263 drag(handle, 50, 50); 264 actual = { height: el.height() }; 265 same(actual, expected, '.dialog({ autoResize: false })'); 266 el.remove(); 267 el = $('<div>content<br>content<br>content<br>content<br>content</div>').dialog({ autoResize: true }); 268 before = { width: el.width(), height: el.height() }; 269 handle = $(".ui-resizable-se", dlg()); 270 drag(handle, 50, 50); 271 expected = { width: before.width + 50, height: before.height + 50 }; 272 actual = { width: el.width(), height: el.height() }; 273 same(actual, expected, '.dialog({ autoResize: true })'); 274 el.remove(); 275 }); 276 277 test("buttons", function() { 278 expect(17); 279 280 var buttons = { 281 "Ok": function(ev, ui) { 282 ok(true, "button click fires callback"); 283 equals(this, el[0], "context of callback"); 284 equals(ev.target, btn[0], "event target"); 285 }, 286 "Cancel": function(ev, ui) { 287 ok(true, "button click fires callback"); 288 equals(this, el[0], "context of callback"); 289 equals(ev.target, btn[1], "event target"); 290 } 291 }; 292 293 el = $('<div></div>').dialog({ buttons: buttons }); 294 var btn = $("button", dlg()); 295 equals(btn.length, 2, "number of buttons"); 296 297 var i = 0; 298 $.each(buttons, function(key, val) { 299 equals(btn.eq(i).text(), key, "text of button " + (i+1)); 300 i++; 301 }); 302 303 equals(btn.parent().attr('className'), 'ui-dialog-buttonpane', "buttons in container"); 304 btn.trigger("click"); 305 306 var newButtons = { 307 "Close": function(ev, ui) { 308 ok(true, "button click fires callback"); 309 equals(this, el[0], "context of callback"); 310 equals(ev.target, btn[0], "event target"); 311 } 312 }; 313 314 equals(el.data("buttons.dialog"), buttons, '.data("buttons.dialog") getter'); 315 el.data("buttons.dialog", newButtons); 316 equals(el.data("buttons.dialog"), newButtons, '.data("buttons.dialog", ...) setter'); 317 318 btn = $("button", dlg()); 319 equals(btn.length, 1, "number of buttons after setter"); 320 btn.trigger('click'); 321 322 i = 0; 323 $.each(newButtons, function(key, val) { 324 equals(btn.eq(i).text(), key, "text of button " + (i+1)); 325 i += 1; 326 }); 327 328 el.remove(); 329 }); 330 331 test("closeOnEscape", function() { 332 ok(false, 'missing test'); 333 }); 334 335 test("closeText", function() { 336 expect(3); 337 338 el = $('<div></div>').dialog(); 339 equals(dlg().find('.ui-dialog-titlebar-close span').text(), 'close', 340 'default close text'); 341 el.remove(); 342 343 el = $('<div></div>').dialog({ closeText: "foo" }); 344 equals(dlg().find('.ui-dialog-titlebar-close span').text(), 'foo', 345 'closeText on init'); 346 el.remove(); 347 348 el = $('<div></div>').dialog().dialog('option', 'closeText', 'bar'); 349 equals(dlg().find('.ui-dialog-titlebar-close span').text(), 'bar', 350 'closeText via option method'); 351 el.remove(); 352 }); 353 354 test("dialogClass", function() { 355 expect(4); 356 357 el = $('<div></div>').dialog(); 358 equals(dlg().is(".foo"), false, 'dialogClass not specified. foo class added'); 359 el.remove(); 360 361 el = $('<div></div>').dialog({ dialogClass: "foo" }); 362 equals(dlg().is(".foo"), true, 'dialogClass in init. foo class added'); 363 el.remove(); 364 365 el = $('<div></div>').dialog({ dialogClass: "foo bar" }); 366 equals(dlg().is(".foo"), true, 'dialogClass in init, two classes. foo class added'); 367 equals(dlg().is(".bar"), true, 'dialogClass in init, two classes. bar class added'); 368 el.remove(); 369 }); 370 371 test("draggable", function() { 372 expect(4); 373 374 el = $('<div></div>').dialog({ draggable: false }); 375 shouldnotmove(); 376 el.data('draggable.dialog', true); 377 shouldmove(); 378 el.remove(); 379 380 el = $('<div></div>').dialog({ draggable: true }); 381 shouldmove(); 382 el.data('draggable.dialog', false); 383 shouldnotmove(); 384 el.remove(); 385 }); 386 387 test("height", function() { 388 expect(3); 389 390 el = $('<div></div>').dialog(); 391 equals(dlg().height(), defaults.height, "default height"); 392 el.remove(); 393 394 el = $('<div></div>').dialog({ height: 437 }); 395 equals(dlg().height(), 437, "explicit height"); 396 el.remove(); 397 398 el = $('<div></div>').dialog(); 399 el.data('height.dialog', 438); 400 equals(dlg().height(), 438, "explicit height set after init"); 401 el.remove(); 402 }); 403 404 test("maxHeight", function() { 405 expect(3); 406 407 el = $('<div></div>').dialog({ maxHeight: 400 }); 408 drag('.ui-resizable-s', 1000, 1000); 409 equals(heightAfter, 400, "maxHeight"); 410 el.remove(); 411 412 el = $('<div></div>').dialog({ maxHeight: 400 }); 413 drag('.ui-resizable-n', -1000, -1000); 414 equals(heightAfter, 400, "maxHeight"); 415 el.remove(); 416 417 el = $('<div></div>').dialog({ maxHeight: 400 }).data('maxHeight.dialog', 600); 418 drag('.ui-resizable-n', -1000, -1000); 419 equals(heightAfter, 600, "maxHeight"); 420 el.remove(); 421 }); 422 423 test("maxWidth", function() { 424 expect(3); 425 426 el = $('<div></div>').dialog({ maxWidth: 400 }); 427 drag('.ui-resizable-e', 1000, 1000); 428 equals(widthAfter, 400, "maxWidth"); 429 el.remove(); 430 431 el = $('<div></div>').dialog({ maxWidth: 400 }); 432 drag('.ui-resizable-w', -1000, -1000); 433 equals(widthAfter, 400, "maxWidth"); 434 el.remove(); 435 436 el = $('<div></div>').dialog({ maxWidth: 400 }).data('maxWidth.dialog', 600); 437 drag('.ui-resizable-w', -1000, -1000); 438 equals(widthAfter, 600, "maxWidth"); 439 el.remove(); 440 }); 441 442 test("minHeight", function() { 443 expect(3); 444 445 el = $('<div></div>').dialog({ minHeight: 10 }); 446 drag('.ui-resizable-s', -1000, -1000); 447 equals(heightAfter, 10, "minHeight"); 448 el.remove(); 449 450 el = $('<div></div>').dialog({ minHeight: 10 }); 451 drag('.ui-resizable-n', 1000, 1000); 452 equals(heightAfter, 10, "minHeight"); 453 el.remove(); 454 455 el = $('<div></div>').dialog({ minHeight: 10 }).data('minHeight.dialog', 30); 456 drag('.ui-resizable-n', 1000, 1000); 457 equals(heightAfter, 30, "minHeight"); 458 el.remove(); 459 }); 460 461 test("minWidth", function() { 462 expect(3); 463 464 el = $('<div></div>').dialog({ minWidth: 10 }); 465 drag('.ui-resizable-e', -1000, -1000); 466 equals(widthAfter, 10, "minWidth"); 467 el.remove(); 468 469 el = $('<div></div>').dialog({ minWidth: 10 }); 470 drag('.ui-resizable-w', 1000, 1000); 471 equals(widthAfter, 10, "minWidth"); 472 el.remove(); 473 474 el = $('<div></div>').dialog({ minWidth: 30 }).data('minWidth.dialog', 30); 475 drag('.ui-resizable-w', 1000, 1000); 476 equals(widthAfter, 30, "minWidth"); 477 el.remove(); 478 }); 479 480 test("modal", function() { 481 ok(false, "missing test"); 482 }); 483 484 test("overlay", function() { 485 ok(false, "missing test"); 486 }); 487 488 test("position", function() { 489 ok(false, "missing test"); 490 }); 491 492 test("resizable", function() { 493 expect(4); 494 495 el = $('<div></div>').dialog(); 496 shouldresize("[default]"); 497 el.data('resizable.dialog', false); 498 shouldnotresize('disabled after init'); 499 el.remove(); 500 501 el = $('<div></div>').dialog({ resizable: false }); 502 shouldnotresize("disabled in init options"); 503 el.data('resizable.dialog', true); 504 shouldresize('enabled after init'); 505 el.remove(); 506 }); 507 508 test("stack", function() { 509 ok(false, "missing test"); 510 }); 511 512 test("title", function() { 513 expect(5); 514 515 function titleText() { 516 return dlg().find(".ui-dialog-title").html(); 517 } 518 519 el = $('<div></div>').dialog(); 520 equals(titleText(), " ", "[default]"); 521 el.remove(); 522 523 el = $('<div title="foo"/>').dialog(); 524 equals(titleText(), "foo", "title in element attribute"); 525 el.remove(); 526 527 el = $('<div></div>').dialog({ title: 'foo' }); 528 equals(titleText(), "foo", "title in init options"); 529 el.remove(); 530 531 el = $('<div title="foo"/>').dialog({ title: 'bar' }); 532 equals(titleText(), "bar", "title in init options should override title in element attribute"); 533 el.remove(); 534 535 el = $('<div></div>').dialog().data('title.dialog', 'foo'); 536 equals(titleText(), 'foo', 'title after init'); 537 el.remove(); 538 }); 539 540 test("width", function() { 541 expect(3); 542 543 el = $('<div></div>').dialog(); 544 equals(dlg().width(), defaults.width, "default width"); 545 el.remove(); 546 547 el = $('<div></div>').dialog({width: 437 }); 548 equals(dlg().width(), 437, "explicit width"); 549 el.data('width.dialog', 438); 550 equals(dlg().width(), 438, 'explicit width after init'); 551 el.remove(); 552 }); 553 554 module("dialog: Methods"); 555 556 test("isOpen", function() { 557 expect(4); 558 559 el = $('<div></div>').dialog(); 560 equals(el.dialog('isOpen'), true, "dialog is open after init"); 561 el.dialog('close'); 562 equals(el.dialog('isOpen'), false, "dialog is closed"); 563 el.remove(); 564 565 el = $('<div></div>').dialog({autoOpen: false}); 566 equals(el.dialog('isOpen'), false, "dialog is closed after init"); 567 el.dialog('open'); 568 equals(el.dialog('isOpen'), true, "dialog is open"); 569 el.remove(); 570 }); 571 572 module("dialog: Callbacks"); 573 574 test("open", function() { 575 expect(6); 576 577 el = $("<div></div>"); 578 el.dialog({ 579 open: function(ev, ui) { 580 ok(true, 'autoOpen: true fires open callback'); 581 equals(this, el[0], "context of callback"); 582 } 583 }); 584 el.remove(); 585 586 el = $("<div></div>"); 587 el.dialog({ 588 autoOpen: false, 589 open: function(ev, ui) { 590 ok(true, '.dialog("open") fires open callback'); 591 equals(this, el[0], "context of callback"); 592 } 593 }); 594 el.dialog("open"); 595 el.remove(); 596 597 el = $('<div></div>').dialog({ 598 autoOpen: false 599 }); 600 el.bind('dialogopen', function(ev, ui) { 601 ok(true, 'dialog("open") fires open event'); 602 equals(this, el[0], 'context of event'); 603 }); 604 el.dialog('open'); 605 el.remove(); 606 }); 607 608 test("dragStart", function() { 609 expect(2); 610 611 el = $("<div></div>"); 612 el.dialog({ 613 dragStart: function(ev, ui) { 614 ok(true, 'dragging fires dragStart callback'); 615 equals(this, el[0], "context of callback"); 616 } 617 }); 618 var handle = $(".ui-dialog-titlebar", dlg()); 619 drag(handle, 50, 50); 620 el.remove(); 621 }); 622 623 test("drag", function() { 624 var fired = false; 625 626 el = $("<div></div>"); 627 el.dialog({ 628 drag: function(ev, ui) { 629 fired = true; 630 equals(this, el[0], "context of callback"); 631 } 632 }); 633 var handle = $(".ui-dialog-titlebar", dlg()); 634 drag(handle, 50, 50); 635 ok(fired, "drag fired"); 636 el.remove(); 637 }); 638 639 test("dragStop", function() { 640 expect(2); 641 642 el = $("<div></div>"); 643 el.dialog({ 644 dragStop: function(ev, ui) { 645 ok(true, 'dragging fires dragStop callback'); 646 equals(this, el[0], "context of callback"); 647 } 648 }); 649 var handle = $(".ui-dialog-titlebar", dlg()); 650 drag(handle, 50, 50); 651 el.remove(); 652 }); 653 654 test("resizeStart", function() { 655 expect(2); 656 657 el = $("<div></div>"); 658 el.dialog({ 659 resizeStart: function(ev, ui) { 660 ok(true, 'resizing fires resizeStart callback'); 661 equals(this, el[0], "context of callback"); 662 } 663 }); 664 var handle = $(".ui-resizable-se", dlg()); 665 drag(handle, 50, 50); 666 el.remove(); 667 }); 668 669 test("resize", function() { 670 var fired = false; 671 672 el = $("<div></div>"); 673 el.dialog({ 674 resize: function(ev, ui) { 675 fired = true; 676 equals(this, el[0], "context of callback"); 677 } 678 }); 679 var handle = $(".ui-resizable-se", dlg()); 680 drag(handle, 50, 50); 681 ok(fired, "resize fired"); 682 el.remove(); 683 }); 684 685 test("resizeStop", function() { 686 expect(2); 687 688 el = $("<div></div>"); 689 el.dialog({ 690 resizeStop: function(ev, ui) { 691 ok(true, 'resizing fires resizeStop callback'); 692 equals(this, el[0], "context of callback"); 693 } 694 }); 695 var handle = $(".ui-resizable-se", dlg()); 696 drag(handle, 50, 50); 697 el.remove(); 698 }); 699 700 test("close", function() { 701 expect(4); 702 703 el = $('<div></div>').dialog({ 704 close: function(ev, ui) { 705 ok(true, '.dialog("close") fires close callback'); 706 equals(this, el[0], "context of callback"); 707 } 708 }); 709 el.dialog("close"); 710 el.remove(); 711 712 el = $('<div></div>').dialog().bind('dialogclose', function(ev, ui) { 713 ok(true, '.dialog("close") firse dialogclose event'); 714 equals(this, el[0], 'context of event'); 715 }); 716 el.dialog('close'); 717 el.remove(); 718 }); 719 720 test("beforeclose", function() { 721 expect(6); 722 723 el = $('<div></div>').dialog({ 724 beforeclose: function(ev, ui) { 725 ok(true, '.dialog("close") fires beforeclose callback'); 726 equals(this, el[0], "context of callback"); 727 return false; 728 } 729 }); 730 el.dialog('close'); 731 isOpen('beforeclose callback should prevent dialog from closing'); 732 el.remove(); 733 734 el = $('<div></div>').dialog().bind('dialogbeforeclose', function(ev, ui) { 735 ok(true, '.dialog("close") triggers dialogbeforeclose event'); 736 equals(this, el[0], "context of event"); 737 return false; 738 }); 739 el.dialog('close'); 740 isOpen('dialogbeforeclose event should prevent dialog from closing'); 741 el.remove(); 742 }); 743 744 module("dialog: Tickets"); 745 746 })(jQuery);
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |