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.WebTestCase;
044:
045: /**
046: * Tests for elements with onblur and onfocus attributes.
047: *
048: * @version $Revision: 2132 $
049: * @author David D. Kilzer
050: * @author Marc Guillemot
051: */
052: public class FocusableElementTest extends WebTestCase {
053:
054: private static final String COMMON_ID = " id='focusId'";
055: private static final String COMMON_EVENTS = " onblur=\"alert('foo onblur')\" onfocus=\"alert('foo onfocus')\"";
056: private static final String COMMON_ATTRIBUTES = COMMON_ID
057: + COMMON_EVENTS;
058:
059: /**
060: * Create an instance.
061: * @param name The name of the test.
062: */
063: public FocusableElementTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * Full page driver for onblur and onfocus tests.
069: *
070: * @param htmlContent HTML fragment for body of page with a focusable element identified by a focusId ID attribute.
071: * Must have onfocus event that raises an alert of "foo1 onfocus" and an onblur event that raises an alert of "foo
072: * onblur" on the second element.
073: * @throws Exception if the test fails
074: */
075: private void onClickPageTest(final String htmlContent)
076: throws Exception {
077: final List collectedAlerts = new ArrayList();
078: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
079: final HtmlElement element = (HtmlElement) page
080: .getHtmlElementById("focusId");
081:
082: element.focus();
083: element.blur();
084:
085: final String[] expectedAlerts = { "foo onfocus", "foo onblur",
086: "foo onfocus", "foo onblur" };
087: assertEquals(expectedAlerts, collectedAlerts);
088: }
089:
090: /**
091: * Body driver for onblur and onfocus tests.
092: *
093: * @param htmlBodyContent HTML tag name for simple tag with text body.
094: * @throws Exception if the test fails
095: */
096: private void onClickBodyTest(final String htmlBodyContent)
097: throws Exception {
098: onClickPageTest("<html><head><title>foo</title></head><body>\n"
099: + htmlBodyContent
100: + "<script type=\"text/javascript\" language=\"JavaScript\">\n"
101: + "document.getElementById('focusId').focus();\n"
102: + "document.getElementById('focusId').blur();\n"
103: + "</script></body></html>");
104: }
105:
106: /**
107: * Simple tag name driver for onblur and onfocus tests.
108: *
109: * @param tagName HTML tag name for simple tag with text body.
110: * @param tagAttributes Additional attribute(s) to add to the generated tag.
111: * @throws Exception if the test fails
112: */
113: private void onClickSimpleTest(final String tagName,
114: final String tagAttributes) throws Exception {
115: onClickBodyTest("<" + tagName + COMMON_ATTRIBUTES + " "
116: + tagAttributes + ">Text</" + tagName + ">");
117: }
118:
119: /**
120: * Test onblur and onfocus handlers and blur() and focus() methods of anchor element.
121: *
122: * @throws Exception if the test fails
123: */
124: public void testAnchor_onblur_onfocus() throws Exception {
125: onClickSimpleTest("a", "href=\".\"");
126: }
127:
128: /**
129: * Test onblur and onfocus handlers and blur() and focus() methods of area element.
130: *
131: * @throws Exception if the test fails
132: */
133: public void testArea_onblur_onfocus() throws Exception {
134: onClickBodyTest("<map><area " + COMMON_ATTRIBUTES
135: + " shape=\"rect\" coords=\"0,0,1,1\" href=\".\">\n"
136: + "</area></map>");
137: }
138:
139: /**
140: * Test onblur and onfocus handlers and blur() and focus() methods of button element.
141: *
142: * @throws Exception if the test fails
143: */
144: public void testButton_onblur_onfocus() throws Exception {
145: onClickSimpleTest("button",
146: "name=\"foo\" value=\"bar\" type=\"button\"");
147: }
148:
149: /**
150: * Test onblur and onfocus handlers and blur() and focus() methods of label element surrounding input element.
151: *
152: * @throws Exception if the test fails
153: */
154: public void testLabelContainsInput_onblur_onfocus()
155: throws Exception {
156: onClickBodyTest("<form><label " + COMMON_ID + ">"
157: + "Foo<input type=\"text\" name=\"foo\""
158: + COMMON_EVENTS + "></label></form>\n");
159: }
160:
161: /**
162: * Test onblur and onfocus handlers and blur() and focus() methods of label element referencing an input element.
163: *
164: * @throws Exception if the test fails
165: */
166: public void testLabelReferencesInput_onblur_onfocus()
167: throws Exception {
168: onClickBodyTest("<form><label " + COMMON_ID
169: + " for=\"fooId\">Foo</label>\n"
170: + "<input type=\"text\" name=\"foo\" id=\"fooId\""
171: + COMMON_EVENTS + "></form>\n");
172: }
173:
174: /**
175: * Test onblur and onfocus handlers and blur() and focus() methods of select element.
176: *
177: * @throws Exception if the test fails
178: */
179: public void testSelect_onblur_onfocus() throws Exception {
180: onClickBodyTest("<form><select " + COMMON_ATTRIBUTES
181: + "><option>1</option></select></form>\n");
182: }
183:
184: /**
185: * Test onblur and onfocus handlers and blur() and focus() methods of textarea element.
186: *
187: * @throws Exception if the test fails
188: */
189: public void testTextarea_onblur_onfocus() throws Exception {
190: onClickBodyTest("<form><textarea " + COMMON_ATTRIBUTES
191: + ">Text</textarea></form>\n");
192: }
193:
194: /**
195: * Regression test for bug
196: * https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1161705&group_id=47038
197: * @throws Exception if the test fails
198: */
199: public void testOnBlurWith2Pages() throws Exception {
200: final String html = "<html>\n"
201: + "<head>\n"
202: + "<script>\n"
203: + "var bCalled = false;\n"
204: + "function testOnBlur()\n"
205: + "{\n"
206: + " if (bCalled)\n"
207: + " throw 'problem!'; // to get the error immediately rather than an infinite loop\n"
208: + " bCalled = true;\n"
209: + " document.getElementById('text2').focus();\n"
210: + "}\n"
211: + "</script>\n"
212: + "</head>\n"
213: + "<body onLoad='document.getElementById(\"text1\").focus()'>\n"
214: + "<input type='text' id='text1' onblur='testOnBlur()'>\n"
215: + "<input type='text' id='text2'>\n"
216: + "<a href='foo'>this page again</a>\n"
217: + "</body></html>";
218:
219: final HtmlPage page = loadPage(html);
220: ((HtmlAnchor) page.getAnchors().get(0)).click();
221: }
222:
223: /**
224: * Test focus on all types of elements
225: * @throws Exception if the test fails
226: */
227: public void testOnAllElements() throws Exception {
228: testHTMLFile("FocusableElementTest_onAllElements.html");
229: }
230: }
|