KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > FocusableElementTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

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.WebTestCase;
45
46
47
48 /**
49  * Tests for elements with onblur and onfocus attributes.
50  *
51  * @author David D. Kilzer
52  * @version $Revision: 1.4 $
53  */

54 public class FocusableElementTest extends WebTestCase {
55
56     private static final String COMMON_ID = " id='focusId'";
57     private static final String COMMON_EVENTS = " onblur=\"alert('foo onblur')\" onfocus=\"alert('foo onfocus')\"";
58     private static final String COMMON_ATTRIBUTES = COMMON_ID + COMMON_EVENTS;
59
60     /**
61      * Create an instance.
62      * @param name The name of the test.
63      */

64     public FocusableElementTest(final String name) {
65         super(name);
66     }
67
68
69     /**
70      * Full page driver for onblur and onfocus tests.
71      *
72      * @param htmlContent HTML fragment for body of page with a focusable element identified by a focusId ID attribute.
73      * Must have onfocus event that raises an alert of "foo1 onfocus" and an onblur event that raises an alert of "foo
74      * onblur" on the second element.
75      * @throws Exception if the test fails
76      */

77     private void onClickPageTest(final String htmlContent) throws Exception {
78
79         final List collectedAlerts = new ArrayList();
80         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
81
82         final FocusableElement element = (FocusableElement) page.getHtmlElementById("focusId");
83
84         element.focus();
85         element.blur();
86
87         final List expectedAlerts =
88                 Arrays.asList(new String[]{"foo onfocus", "foo onblur", "foo onfocus", "foo onblur"});
89         assertEquals(expectedAlerts, collectedAlerts);
90     }
91
92
93     /**
94      * Body driver for onblur and onfocus tests.
95      *
96      * @param htmlBodyContent HTML tag name for simple tag with text body.
97      * @throws Exception if the test fails
98      */

99     private void onClickBodyTest(final String htmlBodyContent) throws Exception {
100         onClickPageTest(
101                 "<html><head><title>foo</title></head><body>" +
102                 htmlBodyContent +
103                 "<script type=\"text/javascript\" language=\"JavaScript\">\n" +
104                 "<!--\n" +
105                 "document.getElementById('focusId').focus();\n" +
106                 "document.getElementById('focusId').blur();\n" +
107                 "// -->\n" +
108                 "</script></body></html>");
109     }
110
111
112     /**
113      * Simple tag name driver for onblur and onfocus tests.
114      *
115      * @param tagName HTML tag name for simple tag with text body.
116      * @param tagAttributes Additional attribute(s) to add to the generated tag.
117      * @throws Exception if the test fails
118      */

119     private void onClickSimpleTest(final String tagName, final String tagAttributes) throws Exception {
120         onClickBodyTest(
121                 "<" + tagName + COMMON_ATTRIBUTES +
122                 " " + tagAttributes + ">Text</" + tagName + ">");
123     }
124
125
126     /**
127      * Test onblur and onfocus handlers and blur() and focus() methods of anchor element.
128      *
129      * @throws Exception if the test fails
130      */

131     public void testAnchor_onblur_onfocus() throws Exception {
132         onClickSimpleTest("a", "href=\".\"");
133     }
134
135
136     /**
137      * Test onblur and onfocus handlers and blur() and focus() methods of area element.
138      *
139      * @throws Exception if the test fails
140      */

141     public void testArea_onblur_onfocus() throws Exception {
142         onClickBodyTest(
143                 "<map><area " + COMMON_ATTRIBUTES +
144                 " shape=\"rect\" coords=\"0,0,1,1\" HREF=\".\">" +
145                 "</area></map>");
146     }
147
148
149     /**
150      * Test onblur and onfocus handlers and blur() and focus() methods of button element.
151      *
152      * @throws Exception if the test fails
153      */

154     public void testButton_onblur_onfocus() throws Exception {
155         onClickSimpleTest("button", "name=\"foo\" value=\"bar\" type=\"button\"");
156     }
157
158
159     /**
160      * Test onblur and onfocus handlers and blur() and focus() methods of label element surrounding input element.
161      *
162      * @throws Exception if the test fails
163      */

164     public void testLabelContainsInput_onblur_onfocus() throws Exception {
165         onClickBodyTest(
166                 "<form><label " + COMMON_ID + ">" +
167                 "Foo<input type=\"text\" name=\"foo\"" + COMMON_EVENTS + "></label></form>");
168     }
169
170
171     /**
172      * Test onblur and onfocus handlers and blur() and focus() methods of label element referencing an input element.
173      *
174      * @throws Exception if the test fails
175      */

176     public void testLabelReferencesInput_onblur_onfocus() throws Exception {
177         onClickBodyTest(
178                 "<form><label " + COMMON_ID + " for=\"fooId\">Foo</label>" +
179                 "<input type=\"text\" name=\"foo\" id=\"fooId\"" + COMMON_EVENTS + "></form>");
180     }
181
182
183     /**
184      * Test onblur and onfocus handlers and blur() and focus() methods of select element.
185      *
186      * @throws Exception if the test fails
187      */

188     public void testSelect_onblur_onfocus() throws Exception {
189         onClickBodyTest("<form><select " + COMMON_ATTRIBUTES + "><option>1</option></select></form>");
190     }
191
192
193     /**
194      * Test onblur and onfocus handlers and blur() and focus() methods of textarea element.
195      *
196      * @throws Exception if the test fails
197      */

198     public void testTextarea_onblur_onfocus() throws Exception {
199         onClickBodyTest("<form><textarea " + COMMON_ATTRIBUTES + ">Text</textarea></form>");
200     }
201 }
202
Popular Tags