001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import com.gargoylesoftware.htmlunit.BrowserVersion;
044: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
045: import com.gargoylesoftware.htmlunit.MockWebConnection;
046: import com.gargoylesoftware.htmlunit.WebClient;
047: import com.gargoylesoftware.htmlunit.WebTestCase;
048:
049: /**
050: * Tests for {@link ClickableElement}.
051: *
052: * @version $Revision: 2132 $
053: * @author David K. Taylor
054: * @author Chris Erskine
055: * @author Marc Guillemot
056: * @author Ahmed Ashour
057: */
058: public class ClickableElementTest extends WebTestCase {
059: /**
060: * Create an instance
061: *
062: * @param name The name of the test
063: */
064: public ClickableElementTest(final String name) {
065: super (name);
066: }
067:
068: /**
069: * Full page driver for onClick tests.
070: *
071: * @param htmlContent HTML fragment for body of page with clickable element
072: * identified by clickId ID attribute. Must have onClick that raises
073: * an alert of "foo".
074: * @throws Exception if the test fails
075: */
076: private void onClickPageTest(final String htmlContent)
077: throws Exception {
078: final String[] expectedAlerts = { "foo" };
079: onClickPageTest(htmlContent, 1, expectedAlerts);
080: }
081:
082: /**
083: * Full page driver for onClick tests.
084: *
085: * @param htmlContent HTML fragment for body of page with clickable element
086: * identified by clickId ID attribute.
087: * @param numClicks number of times to click element
088: * @param expectedAlerts Array of expected popup values
089: * @throws Exception if the test fails
090: */
091: private void onClickPageTest(final String htmlContent,
092: final int numClicks, final String[] expectedAlerts)
093: throws Exception {
094: onClickPageTest(htmlContent, numClicks, expectedAlerts, false);
095: }
096:
097: /**
098: * Full page driver for onClick tests.
099: *
100: * @param htmlContent HTML fragment for body of page with clickable element
101: * identified by clickId ID attribute.
102: * @param numClicks number of times to click element
103: * @param expectedAlerts Array of expected popup values
104: * @param exceptionOnError
105: * @throws Exception if the test fails
106: */
107: private void onClickPageTest(final String htmlContent,
108: final int numClicks, final String[] expectedAlerts,
109: final boolean exceptionOnError) throws Exception {
110: final BrowserVersion bv = new BrowserVersion("Netscape", "7",
111: "", "1.2", 7);
112: final WebClient client = new WebClient(bv);
113:
114: final MockWebConnection webConnection = new MockWebConnection(
115: client);
116: webConnection.setDefaultResponse(htmlContent);
117: client.setWebConnection(webConnection);
118: client.setThrowExceptionOnScriptError(exceptionOnError);
119:
120: final List collectedAlerts = new ArrayList();
121: final CollectingAlertHandler alertHandler = new CollectingAlertHandler(
122: collectedAlerts);
123: client.setAlertHandler(alertHandler);
124:
125: final HtmlPage page = (HtmlPage) client.getPage(URL_GARGOYLE);
126: final ClickableElement clickable = (ClickableElement) page
127: .getHtmlElementById("clickId");
128:
129: for (int i = 0; i < numClicks; i++) {
130: clickable.click();
131: }
132:
133: assertEquals(expectedAlerts, collectedAlerts);
134: }
135:
136: /**
137: * Body driver for onClick tests.
138: *
139: * @param htmlBody HTML text body
140: * @throws Exception if the test fails
141: */
142: private void onClickBodyTest(final String htmlBody)
143: throws Exception {
144: onClickPageTest("<html><head><title>foo</title></head>\n"
145: + htmlBody + "</html>");
146: }
147:
148: /**
149: * Simple tag name driver for onClick tests.
150: *
151: * @param tagName HTML tag name for simple tag with text body
152: * @throws Exception if the test fails
153: */
154: private void onClickSimpleTest(final String tagName)
155: throws Exception {
156: onClickBodyTest("<body><" + tagName
157: + " id='clickId' onClick='alert(\"foo\")'>Text</"
158: + tagName + "></body>\n");
159: }
160:
161: /**
162: * Test onClick handler and click method of anchor element.
163: *
164: * @throws Exception if the test fails
165: */
166: public void testAnchor_onClick() throws Exception {
167: onClickSimpleTest("a");
168: }
169:
170: /**
171: * Test onClick handler and click method of abbreviation element.
172: *
173: * @throws Exception if the test fails
174: */
175: public void testAbbreviation_onClick() throws Exception {
176: onClickSimpleTest("abbr");
177: }
178:
179: /**
180: * Test onClick handler and click method of acronym element.
181: *
182: * @throws Exception if the test fails
183: */
184: public void testAcronym_onClick() throws Exception {
185: onClickSimpleTest("acronym");
186: }
187:
188: /**
189: * Test onClick handler and click method of address element.
190: *
191: * @throws Exception if the test fails
192: */
193: public void testAddress_onClick() throws Exception {
194: onClickSimpleTest("address");
195: }
196:
197: /**
198: * Test onClick handler and click method of area element.
199: *
200: * @throws Exception if the test fails
201: */
202: public void testArea_onClick() throws Exception {
203: onClickBodyTest("<body><map><area id='clickId' onClick='alert(\"foo\")'/></map></body>\n");
204: }
205:
206: /**
207: * Test onClick handler and click method of bold element.
208: *
209: * @throws Exception if the test fails
210: */
211: public void testBold_onClick() throws Exception {
212: onClickSimpleTest("b");
213: }
214:
215: /**
216: * Test onClick handler and click method of big element.
217: *
218: * @throws Exception if the test fails
219: */
220: public void testBig_onClick() throws Exception {
221: onClickSimpleTest("big");
222: }
223:
224: /**
225: * Test onClick handler and click method of blockquote element.
226: *
227: * @throws Exception if the test fails
228: */
229: public void testBlockquote_onClick() throws Exception {
230: onClickSimpleTest("blockquote");
231: }
232:
233: /**
234: * Test onClick handler and click method of body element.
235: *
236: * @throws Exception if the test fails
237: */
238: public void testBody_onClick() throws Exception {
239: onClickBodyTest("<body id='clickId' onClick='alert(\"foo\")'>Text</body>\n");
240: }
241:
242: /**
243: * Test onClick handler and click method of button element.
244: *
245: * @throws Exception if the test fails
246: */
247: public void testButton_onClick() throws Exception {
248: onClickBodyTest("<body><form><button id='clickId' onClick='alert(\"foo\")'>Item</button></form></body>\n");
249: }
250:
251: /**
252: * Test onClick handler can be called multiple times
253: *
254: * @throws Exception if the test fails
255: */
256: public void testButton_onClickTwice() throws Exception {
257: final String[] expectedAlerts = { "foo0", "foo1" };
258: onClickPageTest(
259: "<body><form>\n"
260: + "<button id='clickId' onClick='alert(\"foo\" + count++); return false;'>Item</button>\n"
261: + "<script> var count = 0 </script>\n"
262: + "</form></body>\n", 2, expectedAlerts);
263: }
264:
265: /**
266: * Test onClick handler and click method of cite element.
267: *
268: * @throws Exception if the test fails
269: */
270: public void testCite_onClick() throws Exception {
271: onClickSimpleTest("cite");
272: }
273:
274: /**
275: * Test onClick handler and click method of code element.
276: *
277: * @throws Exception if the test fails
278: */
279: public void testCode_onClick() throws Exception {
280: onClickSimpleTest("code");
281: }
282:
283: /**
284: * Test onClick handler and click method of table column element.
285: *
286: * @throws Exception if the test fails
287: */
288: public void testTableColumn_onClick() throws Exception {
289: onClickBodyTest("<body><table><caption>Caption</caption><colgroup>\n"
290: + "<col id='clickId' onClick='alert(\"foo\")'/></colgroup><thead><tr><th>\n"
291: + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>\n"
292: + "th>Header</th></tr></tfoot></table></body>\n");
293: }
294:
295: /**
296: * Test onClick handler and click method of table column group element.
297: *
298: * @throws Exception if the test fails
299: */
300: public void testTableColumnGroup_onClick() throws Exception {
301: onClickBodyTest("<body><table><caption>Caption</caption>\n"
302: + "<colgroup id='clickId' onClick='alert(\"foo\")'><col/></colgroup><thead>\n"
303: + "<tr><th>Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot>\n"
304: + "<tr><th>Header</th></tr></tfoot></table></body>\n");
305: }
306:
307: /**
308: * Test onClick handler and click method of center element.
309: *
310: * @throws Exception if the test fails
311: */
312: public void testCenter_onClick() throws Exception {
313: onClickSimpleTest("center");
314: }
315:
316: /**
317: * Test onClick handler and click method of table caption element.
318: *
319: * @throws Exception if the test fails
320: */
321: public void testTableCaption_onClick() throws Exception {
322: onClickBodyTest("<body><table><caption id='clickId' onClick='alert(\"foo\")'>\n"
323: + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>\n"
324: + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>\n"
325: + "</tfoot></table></body>\n");
326: }
327:
328: /**
329: * Test onClick handler and click method of definition description dd element.
330: *
331: * @throws Exception if the test fails
332: */
333: public void testDefinitionDescription_onClick() throws Exception {
334: onClickBodyTest("<body><dl><dt>Term</dt><dd id='clickId' onClick='alert(\"foo\")'>Definition</dd></dl></body>");
335: }
336:
337: /**
338: * Test that no NPE is thrown when JS fails on link click
339: * and WebClient.setThrowExceptionOnScriptError(false) is used.
340: * Test for bug 1385864.
341: * @throws Exception if the test fails
342: */
343: public void testJavaScriptError_onClick() throws Exception {
344: onClickPageTest(
345: "<html><head></head><body>\n"
346: + "<form method='POST'><input type='button' id='clickId' onclick='y()'></form>\n"
347: + "</body></html>", 1, new String[0], false);
348: }
349:
350: /**
351: * Test onClick handler and click method of definition element.
352: *
353: * @throws Exception if the test fails
354: */
355: public void testDefinition_onClick() throws Exception {
356: onClickSimpleTest("dfn");
357: }
358:
359: /**
360: * Test onClick handler and click method of directory element.
361: *
362: * @throws Exception if the test fails
363: */
364: public void testDirectory_onClick() throws Exception {
365: onClickSimpleTest("dir");
366: }
367:
368: /**
369: * Test onClick handler and click method of definition list dl element.
370: *
371: * @throws Exception if the test fails
372: */
373: public void testDefinitionList_onClick() throws Exception {
374: onClickBodyTest("<body><dl id='clickId' onClick='alert(\"foo\")'><dt>Term</dt><dd>Definition</dd></dl></body>");
375: }
376:
377: /**
378: * Test onClick handler and click method of definition term dt element.
379: *
380: * @throws Exception if the test fails
381: */
382: public void testDefinitionTerm_onClick() throws Exception {
383: onClickBodyTest("<body><dl><dt id='clickId' onClick='alert(\"foo\")'>Term</dt><dd>Definition</dd></dl></body>");
384: }
385:
386: /**
387: * Test onClick handler and click method of deleted text element.
388: *
389: * @throws Exception if the test fails
390: */
391: public void testDeletedText_onClick() throws Exception {
392: onClickSimpleTest("del");
393: }
394:
395: /**
396: * Test onClick handler and click method of Text Direction element.
397: *
398: * @throws Exception if the test fails
399: */
400: public void testTextDirection_onClick() throws Exception {
401: onClickSimpleTest("dir");
402: }
403:
404: /**
405: * Test onClick handler and click method of division element.
406: *
407: * @throws Exception if the test fails
408: */
409: public void testDivision_onClick() throws Exception {
410: onClickSimpleTest("div");
411: }
412:
413: /**
414: * Test onClick handler and click method of emphasis element.
415: *
416: * @throws Exception if the test fails
417: */
418: public void testEmphasis_onClick() throws Exception {
419: onClickSimpleTest("em");
420: }
421:
422: /**
423: * Test onClick handler and click method of field set element.
424: *
425: * @throws Exception if the test fails
426: */
427: public void testFieldSet_onClick() throws Exception {
428: onClickBodyTest("<body><form><fieldset id='clickId' onClick='alert(\"foo\")'>\n"
429: + "<legend>Legend</legend></fieldset></form></body>\n");
430: }
431:
432: /**
433: * Test onClick handler and click method of form element.
434: *
435: * @throws Exception if the test fails
436: */
437: public void testForm_onClick() throws Exception {
438: onClickSimpleTest("form");
439: }
440:
441: /**
442: * Test onClick handler and click method of italics element.
443: *
444: * @throws Exception if the test fails
445: */
446: public void testItalics_onClick() throws Exception {
447: onClickSimpleTest("i");
448: }
449:
450: /**
451: * Test onClick handler and click method of image element.
452: *
453: * @throws Exception if the test fails
454: */
455: public void testImage_onClick() throws Exception {
456: onClickSimpleTest("img");
457: }
458:
459: /**
460: * Test onClick handler and click method of header1 element.
461: *
462: * @throws Exception if the test fails
463: */
464: public void testHeader1_onClick() throws Exception {
465: onClickSimpleTest("h1");
466: }
467:
468: /**
469: * Test onClick handler and click method of header2 element.
470: *
471: * @throws Exception if the test fails
472: */
473: public void testHeader2_onClick() throws Exception {
474: onClickSimpleTest("h2");
475: }
476:
477: /**
478: * Test onClick handler and click method of header3 element.
479: *
480: * @throws Exception if the test fails
481: */
482: public void testHeader3_onClick() throws Exception {
483: onClickSimpleTest("h3");
484: }
485:
486: /**
487: * Test onClick handler and click method of header4 element.
488: *
489: * @throws Exception if the test fails
490: */
491: public void testHeader4_onClick() throws Exception {
492: onClickSimpleTest("h4");
493: }
494:
495: /**
496: * Test onClick handler and click method of header5 element.
497: *
498: * @throws Exception if the test fails
499: */
500: public void testHeader5_onClick() throws Exception {
501: onClickSimpleTest("h5");
502: }
503:
504: /**
505: * Test onClick handler and click method of header6 element.
506: *
507: * @throws Exception if the test fails
508: */
509: public void testHeader6_onClick() throws Exception {
510: onClickSimpleTest("h6");
511: }
512:
513: /**
514: * Test onClick handler and click method of horizontal rule element.
515: *
516: * @throws Exception if the test fails
517: */
518: public void testHorizontalRule_onClick() throws Exception {
519: onClickSimpleTest("hr");
520: }
521:
522: /**
523: * Test onClick handler and click method of input element.
524: *
525: * @throws Exception if the test fails
526: */
527: public void testInput_onClick() throws Exception {
528: onClickBodyTest("<body><form><input id='clickId' onClick='alert(\"foo\")'>Item</input></form></body>\n");
529: }
530:
531: /**
532: * Test onClick handler and click method of inserted text element.
533: *
534: * @throws Exception if the test fails
535: */
536: public void testInsertedText_onClick() throws Exception {
537: onClickSimpleTest("ins");
538: }
539:
540: /**
541: * Test onClick handler and click method of keyboard element.
542: *
543: * @throws Exception if the test fails
544: */
545: public void testKeyboard_onClick() throws Exception {
546: onClickSimpleTest("kbd");
547: }
548:
549: /**
550: * Test onClick handler and click method of label element.
551: *
552: * @throws Exception if the test fails
553: */
554: public void testLabel_onClick() throws Exception {
555: onClickBodyTest("<body><form><label id='clickId' onClick='alert(\"foo\")'>Item</label></form></body>\n");
556: }
557:
558: /**
559: * Test onClick handler and click method of legend element.
560: *
561: * @throws Exception if the test fails
562: */
563: public void testLegend_onClick() throws Exception {
564: onClickBodyTest("<body><form><fieldset><legend id='clickId' onClick='alert(\"foo\")'>\n"
565: + "Legend</legend></fieldset></form></body>\n");
566: }
567:
568: /**
569: * Test onClick handler and click method of link element.
570: *
571: * @throws Exception if the test fails
572: */
573: public void XXtestLink_onClick() throws Exception {
574: onClickPageTest("<html><head><title>foo</title><link id='clickId' onClick='alert(\"foo\")'/>\n"
575: + "</head><body></body></html>");
576: }
577:
578: /**
579: * Test onClick handler and click method of List Item element.
580: *
581: * @throws Exception if the test fails
582: */
583: public void testListItem_onClick() throws Exception {
584: onClickBodyTest("<body><ol><li id='clickId' onClick='alert(\"foo\")'>Item</li></ol></body>\n");
585: }
586:
587: /**
588: * Test onClick handler and click method of map element.
589: *
590: * @throws Exception if the test fails
591: */
592: public void testMap_onClick() throws Exception {
593: onClickBodyTest("<body><map id='clickId' onClick='alert(\"foo\")'><area/></map></body>\n");
594: }
595:
596: /**
597: * Test onClick handler and click method of menu element.
598: *
599: * @throws Exception if the test fails
600: */
601: public void testMenu_onClick() throws Exception {
602: onClickBodyTest("<body><menu id='clickId' onClick='alert(\"foo\")'><li>Item</li></menu></body>\n");
603: }
604:
605: /**
606: * Test onClick handler and click method of no frames element.
607: *
608: * @throws Exception if the test fails
609: */
610: public void XXtestNoFrames_onClick() throws Exception {
611: onClickPageTest("<html><head><title>foo</title></head><frameset><frame/>\n"
612: + "<noframes id='clickId' onClick='alert(\"foo\")'/></frameset></html>");
613: }
614:
615: /**
616: * Test onClick handler and click method of no script element.
617: *
618: * @throws Exception if the test fails
619: */
620: public void testNoScript_onClick() throws Exception {
621: onClickBodyTest("<body><script>var i=0;</script><noscript id='clickId' "
622: + "onClick='alert(\"foo\")'>Item</noscript></body>\n");
623: }
624:
625: /**
626: * Test onClick handler and click method of object element.
627: *
628: * @throws Exception if the test fails
629: */
630: public void testObject_onClick() throws Exception {
631: onClickSimpleTest("object");
632: }
633:
634: /**
635: * Test onClick handler and click method of option element.
636: *
637: * @throws Exception if the test fails
638: */
639: public void testOption_onClick() throws Exception {
640: onClickBodyTest("<body><form><select><option id='clickId' onClick='alert(\"foo\")'>\n"
641: + "Option</option></select></form></body>\n");
642: }
643:
644: /**
645: * Test onClick handler and click method of Option Group element.
646: *
647: * @throws Exception if the test fails
648: */
649: public void testOptionGroup_onClick() throws Exception {
650: onClickBodyTest("<body><form><select><optgroup id='clickId' onClick='alert(\"foo\")'>\n"
651: + "<option>Option</option></optgroup></select></form></body>\n");
652: }
653:
654: /**
655: * Test onClick handler and click method of Ordered List element.
656: *
657: * @throws Exception if the test fails
658: */
659: public void testOrderedList_onClick() throws Exception {
660: onClickBodyTest("<body><ol id='clickId' onClick='alert(\"foo\")'><li>Item</li></ol></body>\n");
661: }
662:
663: /**
664: * Test onClick handler and click method of paragraph element.
665: *
666: * @throws Exception if the test fails
667: */
668: public void testParagraph_onClick() throws Exception {
669: onClickSimpleTest("p");
670: }
671:
672: /**
673: * Test onClick handler and click method of pre element.
674: *
675: * @throws Exception if the test fails
676: */
677: public void testPre_onClick() throws Exception {
678: onClickSimpleTest("pre");
679: }
680:
681: /**
682: * Test onClick handler and click method of inline Quotation element.
683: *
684: * @throws Exception if the test fails
685: */
686: public void testQuotation_onClick() throws Exception {
687: onClickSimpleTest("q");
688: }
689:
690: /**
691: * Test onClick handler and click method of strikethrough element.
692: *
693: * @throws Exception if the test fails
694: */
695: public void testStrikethrough_onClick() throws Exception {
696: onClickSimpleTest("s");
697: }
698:
699: /**
700: * Test onClick handler and click method of sample element.
701: *
702: * @throws Exception if the test fails
703: */
704: public void testSample_onClick() throws Exception {
705: onClickSimpleTest("samp");
706: }
707:
708: /**
709: * Test onClick handler and click method of select element.
710: *
711: * @throws Exception if the test fails
712: */
713: public void testSelect_onClick() throws Exception {
714: onClickBodyTest("<body><form><select id='clickId' onClick='alert(\"foo\")'>\n"
715: + "<option>Option</option></select></form></body>\n");
716: }
717:
718: /**
719: * Test onClick handler and click method of small element.
720: *
721: * @throws Exception if the test fails
722: */
723: public void testSmall_onClick() throws Exception {
724: onClickSimpleTest("small");
725: }
726:
727: /**
728: * Test onClick handler and click method of span element.
729: *
730: * @throws Exception if the test fails
731: */
732: public void testSpan_onClick() throws Exception {
733: onClickSimpleTest("span");
734: }
735:
736: /**
737: * Test onClick handler and click method of strike element.
738: *
739: * @throws Exception if the test fails
740: */
741: public void testStrike_onClick() throws Exception {
742: onClickSimpleTest("strike");
743: }
744:
745: /**
746: * Test onClick handler and click method of subscript element.
747: *
748: * @throws Exception if the test fails
749: */
750: public void testSubscript_onClick() throws Exception {
751: onClickSimpleTest("sub");
752: }
753:
754: /**
755: * Test onClick handler and click method of superscript element.
756: *
757: * @throws Exception if the test fails
758: */
759: public void testSuperscript_onClick() throws Exception {
760: onClickSimpleTest("sup");
761: }
762:
763: /**
764: * Test onClick handler and click method of table element.
765: *
766: * @throws Exception if the test fails
767: */
768: public void testTable_onClick() throws Exception {
769: onClickBodyTest("<body><table id='clickId' onClick='alert(\"foo\")'><caption>\n"
770: + "Caption</caption><colgroup><col/></colgroup><thead><tr><th>Header</th></tr>\n"
771: + "</thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th></tr>\n"
772: + "</tfoot></table></body>\n");
773: }
774:
775: /**
776: * Test onClick handler and click method of table body element.
777: *
778: * @throws Exception if the test fails
779: */
780: public void testTableBody_onClick() throws Exception {
781: onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
782: + "</colgroup><thead><tr><th>Header</th></tr></thead>\n"
783: + "<tbody id='clickId' onClick='alert(\"foo\")'><tr><td>Data</td></tr>\n"
784: + "</tbody><tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
785: }
786:
787: /**
788: * Test onClick handler and click method of table data cell element.
789: *
790: * @throws Exception if the test fails
791: */
792: public void testTableDataCell_onClick() throws Exception {
793: onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
794: + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr>\n"
795: + "<td id='clickId' onClick='alert(\"foo\")'>Data</td></tr></tbody>\n"
796: + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
797: }
798:
799: /**
800: * Test onClick handler and click method of textarea element.
801: *
802: * @throws Exception if the test fails
803: */
804: public void testTextarea_onClick() throws Exception {
805: onClickBodyTest("<body><form><textarea id='clickId' onClick='alert(\"foo\")'>Item</textarea></form></body>\n");
806: }
807:
808: /**
809: * Test onClick handler and click method of table footer element.
810: *
811: * @throws Exception if the test fails
812: */
813: public void testTableFooter_onClick() throws Exception {
814: onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
815: + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody><tr><td>Data</td>\n"
816: + "</tr></tbody><tfoot id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>\n"
817: + "</tr></tfoot></table></body>\n");
818: }
819:
820: /**
821: * Test onClick handler and click method of table header cell element.
822: *
823: * @throws Exception if the test fails
824: */
825: public void testTableHeaderCell_onClick() throws Exception {
826: onClickBodyTest("<body><table><caption>Caption</caption><colgroup>\n"
827: + "<col/></colgroup><thead><tr><th id='clickId' onClick='alert(\"foo\")'>\n"
828: + "Header</th></tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr>\n"
829: + "<th>Header</th></tr></tfoot></table></body>\n");
830: }
831:
832: /**
833: * Test onClick handler and click method of table header element.
834: *
835: * @throws Exception if the test fails
836: */
837: public void testTableHeader_onClick() throws Exception {
838: onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
839: + "</colgroup><thead id='clickId' onClick='alert(\"foo\")'><tr><th>Header</th>\n"
840: + "</tr></thead><tbody><tr><td>Data</td></tr></tbody><tfoot><tr><th>Header</th>\n"
841: + "</tr></tfoot></table></body>\n");
842: }
843:
844: /**
845: * Test onClick handler and click method of table row element.
846: *
847: * @throws Exception if the test fails
848: */
849: public void testTableRow_onClick() throws Exception {
850: onClickBodyTest("<body><table><caption>Caption</caption><colgroup><col/>\n"
851: + "</colgroup><thead><tr><th>Header</th></tr></thead><tbody>\n"
852: + "<tr id='clickId' onClick='alert(\"foo\")'><td>Data</td></tr></tbody>\n"
853: + "<tfoot><tr><th>Header</th></tr></tfoot></table></body>\n");
854: }
855:
856: /**
857: * Test when HtmlTableRow.onclick is set by a javascript
858: *
859: * @throws Exception if the test fails
860: */
861: public void testTableRow_onClickSetOnLoad() throws Exception {
862: onClickPageTest("<html><head>\n"
863: + "<script language='JavaScript'>\n"
864: + "function doFoo() { alert('foo'); }"
865: + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}"
866: + "</script>\n"
867: + "</head><body onload=\"doOnload();\">\n"
868: + "<table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>\n"
869: + "</body></html>");
870: }
871:
872: /**
873: *
874: * @throws Exception if the test fails
875: */
876: public void testCheckbox_onClickUpdatesStateFirst()
877: throws Exception {
878: onClickPageTest("<html><head>\n"
879: + "<script language='JavaScript'>\n"
880: + "function doFoo(event) { if (this.checked) alert('foo'); else alert('bar'); }"
881: + "function doOnload() { document.getElementById('clickId').onclick = doFoo;}"
882: + "</script>\n"
883: + "</head><body onload=\"doOnload();\">\n"
884: + "<input type='checkbox' id='clickId'>\n"
885: + "</body></html>");
886: }
887:
888: /**
889: * Test when HtmlTableRow.onclick is set by a javascript
890: *
891: * @throws Exception if the test fails
892: */
893: public void testTableRow_onClickSetByNestedScript()
894: throws Exception {
895: onClickBodyTest("<body><table><tbody><tr id='clickId'><td>cell value</td></tr></tbody></table>\n"
896: + "<script language='JavaScript'>\n"
897: + "function doFoo(event) { alert('foo'); }"
898: + "document.getElementById('clickId').onclick = doFoo;</script></body>\n");
899: }
900:
901: /**
902: * Test onClick handler and click method of teletype element.
903: *
904: * @throws Exception if the test fails
905: */
906: public void testTeletype_onClick() throws Exception {
907: onClickSimpleTest("tt");
908: }
909:
910: /**
911: * Test onClick handler and click method of underline element.
912: *
913: * @throws Exception if the test fails
914: */
915: public void testUnderline_onClick() throws Exception {
916: onClickSimpleTest("u");
917: }
918:
919: /**
920: * Test onClick handler and click method of unordered List element.
921: *
922: * @throws Exception if the test fails
923: */
924: public void testUnorderedList_onClick() throws Exception {
925: onClickBodyTest("<body><ul id='clickId' onClick='alert(\"foo\")'><li>Item</li></ul></body>\n");
926: }
927:
928: /**
929: * Test onClick handler and click method of variable element.
930: *
931: * @throws Exception if the test fails
932: */
933: public void testVariable_onClick() throws Exception {
934: onClickSimpleTest("var");
935: }
936:
937: /**
938: * Test setting onClick handler from inside the onClick handler
939: *
940: * @throws Exception if the test fails
941: */
942: public void testSetOnClick() throws Exception {
943: final String[] expectedAlerts = { "foo" };
944: onClickPageTest(
945: "<html><body><form>\n"
946: + "<button type='button' id='clickId' onclick='alert(\"foo\"); onclick=null;'>Item</button>\n"
947: + "</form></body></html>", 2, expectedAlerts);
948: }
949:
950: /**
951: * @throws Exception If the test fails
952: */
953: public void testDblClick() throws Exception {
954: final String content = "<html>\n"
955: + "<head>\n"
956: + "<script>\n"
957: + " function clickMe() {\n"
958: + " document.getElementById('myTextarea').value+='click-';\n"
959: + " }\n"
960: + " function dblClickMe() {\n"
961: + " document.getElementById('myTextarea').value+='dblclick-';\n"
962: + " }\n"
963: + "</script>\n"
964: + "</head>\n"
965: + "<body id='myBody' onclick='clickMe()' ondblclick='dblClickMe()'>\n"
966: + "<textarea id='myTextarea'></textarea>\n"
967: + "</body></html>";
968:
969: final HtmlPage page = loadPage(content);
970: final HtmlBody body = (HtmlBody) page
971: .getHtmlElementById("myBody");
972: body.dblClick();
973: final HtmlTextArea textArea = (HtmlTextArea) page
974: .getHtmlElementById("myTextarea");
975: assertEquals("click-dblclick-", textArea.getText());
976: }
977:
978: }
|