1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.BrowserVersion; 45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 46 import com.gargoylesoftware.htmlunit.MockWebConnection; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 50 56 public class ClickableElementTest extends WebTestCase { 57 62 public ClickableElementTest( final String name ) { 63 super( name ); 64 } 65 66 67 75 private void onClickPageTest(final String htmlContent) throws Exception { 76 final List expectedAlerts = Arrays.asList( new String[]{"foo"} ); 77 onClickPageTest(htmlContent, 1, expectedAlerts); 78 } 79 80 89 private void onClickPageTest(final String htmlContent, final int numClicks, final List expectedAlerts) 90 throws Exception { 91 final BrowserVersion bv = new BrowserVersion("Netscape", "7", "", "1.2"); 92 final WebClient client = new WebClient(bv); 93 94 final MockWebConnection webConnection = new MockWebConnection( client ); 95 webConnection.setDefaultResponse( htmlContent ); 96 client.setWebConnection( webConnection ); 97 98 final List collectedAlerts = new ArrayList(); 99 final CollectingAlertHandler alertHandler = new CollectingAlertHandler(collectedAlerts); 100 client.setAlertHandler(alertHandler); 101 102 final HtmlPage page = ( HtmlPage ) client.getPage(URL_GARGOYLE); 103 final ClickableElement clickable = ( ClickableElement )page.getHtmlElementById( "clickId" ); 104 105 for (int i=0; i<numClicks; i++) { 106 clickable.click(); 107 } 108 109 assertEquals( expectedAlerts, collectedAlerts ); 110 } 111 112 113 119 private void onClickBodyTest(final String htmlBody) throws Exception { 120 onClickPageTest("<html><head><title>foo</title></head>" + htmlBody 121 + "</html>"); 122 } 123 124 125 131 private void onClickSimpleTest(final String tagName) throws Exception { 132 onClickBodyTest("<body><" + tagName + " id='clickId' onClick='alert(\"foo\")'>Text</" + tagName + "></body>"); 133 } 134 135 136 141 public void testAnchor_onClick() throws Exception { 142 onClickSimpleTest("a"); 143 } 144 145 146 151 public void testAbbreviation_onClick() throws Exception { 152 onClickSimpleTest("abbr"); 153 } 154 155 156 161 public void testAcronym_onClick() throws Exception { 162 onClickSimpleTest("acronym"); 163 } 164 165 166 171 public void testAddress_onClick() throws Exception { 172 onClickSimpleTest("address"); 173 } 174 175 176 181 public void testArea_onClick() throws Exception { 182 onClickBodyTest("<body><map><area id='clickId' onClick='alert(\"foo\")'/></map></body>"); 183 } 184 185 186 191 public void testBold_onClick() throws Exception { 192 onClickSimpleTest("b"); 193 } 194 195 196 201 public void testBig_onClick() throws Exception { 202 onClickSimpleTest("big"); 203 } 204 205 206 211 public void testBlockquote_onClick() throws Exception { 212 onClickSimpleTest("blockquote"); 213 } 214 215 216 221 public void testBody_onClick() throws Exception { 222 onClickBodyTest("<body id='clickId' onClick='alert(\"foo\")'>Text</body>"); 223 } 224 225 226 231 public void testButton_onClick() throws Exception { 232 onClickBodyTest("<body><form><button id='clickId' onClick='alert(\"foo\")'>Item</button></form></body>"); 233 } 234 235 240 public void testButton_onClickTwice() throws Exception { 241 final List expectedAlerts = Arrays.asList(new String[] {"foo0", "foo1"}); 242 onClickPageTest("<body><form>" + 243 "<button id='clickId' onClick='alert(\"foo\" + count++)'>Item</button>" + 244 "<script> var count = 0 </script>" + 245 "</form></body>", 2, expectedAlerts); 246 } 247 248 253 public void testCite_onClick() throws Exception { 254 onClickSimpleTest("cite"); 255 } 256 257 258 263 public void testCode_onClick() throws Exception { 264 onClickSimpleTest("code"); 265 } 266 267 268 273 public void testTableColumn_onClick() throws Exception { 274 onClickBodyTest("<body><table><caption>Caption</caption><colgroup>" 275 + "<col id='clickId' onClick='alert(\"foo\")'/></colgroup><thead><tr><th>" 276 + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>" 277 + "th>Header</th></tr></tfoot></table></body>"); 278 } 279 280 281 286 public void testTableColumnGroup_onClick() throws Exception { 287 onClickBodyTest("<body><table><caption>Caption</caption>" 288 + "<colgroup id='clickId' onClick='alert(\"foo\")'><col/></colgroup><thead>" 289 + "<tr><th>Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot>" 290 + "<tr><th>Header</th></tr></tfoot></table></body>"); 291 } 292 293 294 299 public void testCenter_onClick() throws Exception { 300 onClickSimpleTest("center"); 301 } 302 303 304 309 public void testTableCaption_onClick() throws Exception { 310 onClickBodyTest("<body><table><caption id='clickId' onClick='alert(\"foo\")'>" 311 + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>" 312 + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>" 313 + "</tfoot></table></body>"); 314 } 315 316 317 322 public void testDefinitionDescription_onClick() throws Exception { 323 onClickBodyTest("<body><dl><dt>Term</dt><dd id='clickId' onClick='alert(\"foo\")'>Definition</dd></dl></body>"); 324 } 325 326 327 332 public void testDefinition_onClick() throws Exception { 333 onClickSimpleTest("dfn"); 334 } 335 336 337 342 public void testDirectory_onClick() throws Exception { 343 onClickSimpleTest("dir"); 344 } 345 346 347 352 public void testDefinitionList_onClick() throws Exception { 353 onClickBodyTest("<body><dl id='clickId' onClick='alert(\"foo\")'><dt>Term</dt><dd>Definition</dd></dl></body>"); 354 } 355 356 357 362 public void testDefinitionTerm_onClick() throws Exception { 363 onClickBodyTest("<body><dl><dt id='clickId' onClick='alert(\"foo\")'>Term</dt><dd>Definition</dd></dl></body>"); 364 } 365 366 367 372 public void testDeletedText_onClick() throws Exception { 373 onClickSimpleTest("del"); 374 } 375 376 377 382 public void testTextDirection_onClick() throws Exception { 383 onClickSimpleTest("dir"); 384 } 385 386 387 392 public void testDivision_onClick() throws Exception { 393 onClickSimpleTest("div"); 394 } 395 396 397 402 public void testEmphasis_onClick() throws Exception { 403 onClickSimpleTest("em"); 404 } 405 406 407 412 public void testFieldSet_onClick() throws Exception { 413 onClickBodyTest("<body><form><fieldset id='clickId' onClick='alert(\"foo\")'>" 414 + "<legend>Legend</legend></fieldset></form></body>"); 415 } 416 417 418 423 public void testForm_onClick() throws Exception { 424 onClickSimpleTest("form"); 425 } 426 427 428 433 public void testItalics_onClick() throws Exception { 434 onClickSimpleTest("i"); 435 } 436 437 438 443 public void testImage_onClick() throws Exception { 444 onClickSimpleTest("img"); 445 } 446 447 448 453 public void testHeader1_onClick() throws Exception { 454 onClickSimpleTest("h1"); 455 } 456 457 458 463 public void testHeader2_onClick() throws Exception { 464 onClickSimpleTest("h2"); 465 } 466 467 468 473 public void testHeader3_onClick() throws Exception { 474 onClickSimpleTest("h3"); 475 } 476 477 478 483 public void testHeader4_onClick() throws Exception { 484 onClickSimpleTest("h4"); 485 } 486 487 488 493 public void testHeader5_onClick() throws Exception { 494 onClickSimpleTest("h5"); 495 } 496 497 498 503 public void testHeader6_onClick() throws Exception { 504 onClickSimpleTest("h6"); 505 } 506 507 508 513 public void testHorizontalRule_onClick() throws Exception { 514 onClickSimpleTest("hr"); 515 } 516 517 522 public void testInput_onClick() throws Exception { 523 onClickBodyTest("<body><form><input id='clickId' onClick='alert(\"foo\")'>Item</input></form></body>"); 524 } 525 526 527 532 public void testInsertedText_onClick() throws Exception { 533 onClickSimpleTest("ins"); 534 } 535 536 537 542 public void testKeyboard_onClick() throws Exception { 543 onClickSimpleTest("kbd"); 544 } 545 546 547 552 public void testLabel_onClick() throws Exception { 553 onClickBodyTest("<body><form><label id='clickId' onClick='alert(\"foo\")'>Item</label></form></body>"); 554 } 555 556 557 562 public void testLegend_onClick() throws Exception { 563 onClickBodyTest("<body><form><fieldset><legend id='clickId' onClick='alert(\"foo\")'>" 564 + "Legend</legend></fieldset></form></body>"); 565 } 566 567 568 573 public void XXtestLink_onClick() throws Exception { 574 onClickPageTest("<html><head><title>foo</title><link id='clickId' onClick='alert(\"foo\")'/>" 575 + "</head><body></body></html>"); 576 } 577 578 579 584 public void testListItem_onClick() throws Exception { 585 onClickBodyTest("<body><ol><li id='clickId' onClick='alert(\"foo\")'>Item</li></ol></body>"); 586 } 587 588 589 594 public void testMap_onClick() throws Exception { 595 onClickBodyTest("<body><map id='clickId' onClick='alert(\"foo\")'><area/></map></body>"); 596 } 597 598 599 604 public void testMenu_onClick() throws Exception { 605 onClickBodyTest("<body><menu id='clickId' onClick='alert(\"foo\")'><li>Item</li></menu></body>"); 606 } 607 608 609 614 public void XXtestNoFrames_onClick() throws Exception { 615 onClickPageTest("<html><head><title>foo</title></head><frameset><frame/>" 616 + "<noframes id='clickId' onClick='alert(\"foo\")'/></frameset></html>"); 617 } 618 619 620 625 public void testNoScript_onClick() throws Exception { 626 onClickBodyTest("<body><script>var i=0;</script><noscript id='clickId' " 627 + "onClick='alert(\"foo\")'>Item</noscript></body>"); 628 } 629 630 631 636 public void testObject_onClick() throws Exception { 637 onClickSimpleTest("object"); 638 } 639 640 641 646 public void testOption_onClick() throws Exception { 647 onClickBodyTest("<body><form><select><option id='clickId' onClick='alert(\"foo\")'>" 648 + "Option</option></select></form></body>"); 649 } 650 651 652 657 public void testOptionGroup_onClick() throws Exception { 658 onClickBodyTest("<body><form><select><optgroup id='clickId' onClick='alert(\"foo\")'>" 659 + "<option>Option</option></optgroup></select></form></body>"); 660 } 661 662 663 668 public void testOrderedList_onClick() throws Exception { 669 onClickBodyTest("<body><ol id='clickId' onClick='alert(\"foo\")'><li>Item</li></ol></body>"); 670 } 671 672 673 678 public void testParagraph_onClick() throws Exception { 679 onClickSimpleTest("p"); 680 } 681 682 683 688 public void testPre_onClick() throws Exception { 689 onClickSimpleTest("pre"); 690 } 691 692 693 698 public void testQuotation_onClick() throws Exception { 699 onClickSimpleTest("q"); 700 } 701 702 703 708 public void testStrikethrough_onClick() throws Exception { 709 onClickSimpleTest("s"); 710 } 711 712 713 718 public void testSample_onClick() throws Exception { 719 onClickSimpleTest("samp"); 720 } 721 722 723 728 public void testSelect_onClick() throws Exception { 729 onClickBodyTest("<body><form><select id='clickId' onClick='alert(\"foo\")'>" 730 + "<option>Option</option></select></form></body>"); 731 } 732 733 734 739 public void testSmall_onClick() throws Exception { 740 onClickSimpleTest("small"); 741 } 742 743 744 749 public void testSpan_onClick() throws Exception { 750 onClickSimpleTest("span"); 751 } 752 753 754 759 public void testStrike_onClick() throws Exception { 760 onClickSimpleTest("strike"); 761 } 762 763 764 769 public void testSubscript_onClick() throws Exception { 770 onClickSimpleTest("sub"); 771 } 772 773 774 779 public void testSuperscript_onClick() throws Exception { 780 onClickSimpleTest("sup"); 781 } 782 783 784 789 public void testTable_onClick() throws Exception { 790 onClickBodyTest("<body><table id='clickId' onClick='alert(\"foo\")'><caption>" 791 + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>" 792 + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>" 793 + "</tfoot></table></body>"); 794 } 795 796 797 802 public void testTableBody_onClick() throws Exception { 803 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>" 804 + "</colgroup><thead><tr><th>Header</th></tr></thead>" 805 + "<tbody id='clickId' onClick='alert(\"foo\")'><tr><td>Data</td></tr>" 806 + "</tbody><tfoot><tr><th>Header</th></tr></tfoot></table></body>"); 807 } 808 809 810 815 public void testTableDataCell_onClick() throws Exception { 816 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>" 817 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr>" 818 + "<td id='clickId' onClick='alert(\"foo\")'>Data</td></tr></tbody>" 819 + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>"); 820 } 821 822 823 828 public void testTextarea_onClick() throws Exception { 829 onClickBodyTest("<body><form><textarea id='clickId' onClick='alert(\"foo\")'>Item</textarea></form></body>"); 830 } 831 832 833 838 public void testTableFooter_onClick() throws Exception { 839 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>" 840 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr><td>Data</td>" 841 + "</tr></tbody><tfoot id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>" 842 + "</tr></tfoot></table></body>"); 843 } 844 845 846 851 public void testTableHeaderCell_onClick() throws Exception { 852 onClickBodyTest("<body><table><caption>Caption</caption><colgroup>" 853 + "<col/></colgroup><thead><tr><th id='clickId' onClick='alert(\"foo\")'>" 854 + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>" 855 + "<th>Header</th></tr></tfoot></table></body>"); 856 } 857 858 859 864 public void testTableHeader_onClick() throws Exception { 865 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>" 866 + "</colgroup><thead id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>" 867 + "</tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th>" 868 + "</tr></tfoot></table></body>"); 869 } 870 871 872 877 public void testTableRow_onClick() throws Exception { 878 onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>" 879 + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody>" 880 + "<tr id='clickId' onClick='alert(\"foo\")'><td>Data</td></tr></tbody>" 881 + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>"); 882 } 883 884 889 public void testTableRow_onClickSetOnLoad() throws Exception { 890 onClickPageTest("<html><head>" 891 + "<script language='JavaScript'>" 892 + "function doFoo() { alert('foo'); }" 893 + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}" 894 + "</script>" 895 + "</head><body onload=\"doOnload();\">" 896 + "<table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>" 897 + "</body></html>"); 898 } 899 900 904 public void testCheckbox_onClickUpdatesStateFirst() throws Exception { 905 onClickPageTest("<html><head>" 906 + "<script language='JavaScript'>" 907 + "function doFoo(event) { if (this.checked) alert('foo'); else alert('bar'); }" 908 + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}" 909 + "</script>" 910 + "</head><body onload=\"doOnload();\">" 911 + "<input type='checkbox' id='clickId'>" 912 + "</body></html>"); 913 } 914 915 920 public void testTableRow_onClickSetByNestedScript() throws Exception { 921 onClickBodyTest("<body><table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>" 922 + "<script language='JavaScript'>" 923 + "function doFoo(event) { alert('foo'); }" 924 + "document.getElementById('clickId').onclick = doFoo;</script></body>"); 925 } 926 927 932 public void testTeletype_onClick() throws Exception { 933 onClickSimpleTest("tt"); 934 } 935 936 937 942 public void testUnderline_onClick() throws Exception { 943 onClickSimpleTest("u"); 944 } 945 946 947 952 public void testUnorderedList_onClick() throws Exception { 953 onClickBodyTest("<body><ul id='clickId' onClick='alert(\"foo\")'><li>Item</li></ul></body>"); 954 } 955 956 957 962 public void testVariable_onClick() throws Exception { 963 onClickSimpleTest("var"); 964 } 965 } 966 | Popular Tags |