KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method;
41 import java.lang.reflect.Modifier;
42 import java.text.MessageFormat;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import junit.framework.Test;
47 import junit.framework.TestSuite;
48
49 import com.gargoylesoftware.htmlunit.WebTestCase;
50
51
52
53 /**
54  * Tests the <code>isDisabled()</code> method on all of the elements that must implement the <code>disabled</code>
55  * attribute: <code>button</code>, <code>input</code>, <code>optgroup</code>, <code>option</code>, <code>select</code>
56  * and <code>textarea</code>.
57  *
58  * @author David D. Kilzer
59  * @version $Revision: 1.6 $
60  */

61 public class DisabledElementTest extends WebTestCase {
62
63     private final String htmlContent_;
64
65
66     /**
67      * Generate a <code>TestSuite</code> for every HTML element that implements {@link DisabledElement}.
68      *
69      * @return The test suite to run
70      */

71     public static Test suite() {
72
73         final TestSuite suite = new TestSuite();
74         addTestCases(suite, "button", "<button id='element1' {0}>foo</button>");
75         addTestCases(suite, "input_button", "<input type='button' id='element1' {0}>");
76         addTestCases(suite, "input_checkbox", "<input type='checkbox' id='element1' {0}>");
77         addTestCases(suite, "input_file", "<input type='file' id='element1' {0}>");
78         addTestCases(suite, "input_hidden", "<input type='hidden' id='element1' {0}>");
79         addTestCases(suite, "input_image", "<input type='image' id='element1' {0}>");
80         addTestCases(suite, "input_password", "<input type='password' id='element1' {0}>");
81         addTestCases(suite, "input_radio", "<input type='radio' id='element1' {0}>");
82         addTestCases(suite, "input_reset", "<input type='reset' id='element1' {0}>");
83         addTestCases(suite, "input_submit", "<input type='submit' id='element1' {0}>");
84         addTestCases(suite, "input_text", "<input type='text' id='element1' {0}>");
85         addTestCases(
86                 suite, "optgroup",
87                 "<select><optgroup id='element1' {0}><option value='1'></option></optgroup></select>");
88         addTestCases(suite, "option", "<select><option id='element1' value='1' {0}></option></select>");
89         addTestCases(suite, "select", "<select id='element1' {0}><option value='1'></option></select>");
90         addTestCases(suite, "textarea", "<textarea id='element1' {0}></textarea>");
91         return suite;
92     }
93
94
95     /**
96      * Adds test cases to the <code>suite</code> argument for the given parameters.
97      *
98      * @param suite The <code>TestSuite</code> to which to add tests
99      * @param elementName The name of the HTML element being tested
100      * @param elementHtml The html representing the element to test with attribute <code>id='element1'</code>
101      */

102     private static void addTestCases(
103             final TestSuite suite, final String elementName,
104             final String elementHtml) {
105
106         final TestSuite subsuite = new TestSuite(DisabledElementTest.class.getName() + "_" + elementName);
107
108         final Method[] methods = DisabledElementTest.class.getMethods();
109         for (int i = 0; i < methods.length; i++) {
110             final Method method = methods[i];
111             if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("test")) {
112                 subsuite.addTest(new DisabledElementTest(method.getName(), elementHtml));
113             }
114         }
115
116         suite.addTest(subsuite);
117     }
118
119
120     /**
121      * Creates an instance of the test class for testing <em>one</em> of the test methods.
122      *
123      * @param testName The name of the test method to run
124      * @param elementHtml The html representing the element to test with attribute <code>id='element1'</code>
125      */

126     public DisabledElementTest(final String testName, final String elementHtml) {
127         super(testName);
128         final String htmlContent = "<html><body><form id='form1'>{0}</form></body></html>";
129         htmlContent_ = MessageFormat.format(htmlContent, new String[]{elementHtml});
130     }
131
132
133     /**
134      * Tests that the <code>isDisabled()</code> method returns <code>false</code> when the <code>disabled</code>
135      * attribute does not exist.
136      *
137      * @throws Exception If the test fails
138      */

139     public void testNoDisabledAttribute() throws Exception {
140         executeDisabledTest("", false);
141     }
142
143
144     /**
145      * Tests that the <code>isDisabled()</code> method returns <code>true</code> when the <code>disabled</code>
146      * attribute exists and is blank.
147      *
148      * @throws Exception If the test fails
149      */

150     public void testBlankDisabledAttribute() throws Exception {
151         executeDisabledTest("disabled=''", true);
152     }
153
154
155     /**
156      * Tests that the <code>isDisabled()</code> method returns <code>false</code> when the <code>disabled</code>
157      * attribute exists and is <em>not</em> blank.
158      *
159      * @throws Exception If the test fails
160      */

161     public void testPopulatedDisabledAttribute() throws Exception {
162         executeDisabledTest("disabled='disabled'", true);
163     }
164
165
166     /**
167      * Tests the <code>isDisabled()</code> method with the given parameters.
168      *
169      * @param disabledAttribute The definition of the <code>disabled</code> attribute
170      * @param expectedIsDisabled The expected return value of the <code>isDisabled()</code> method
171      * @throws Exception If test fails
172      */

173     private void executeDisabledTest(final String disabledAttribute, final boolean expectedIsDisabled)
174         throws Exception {
175
176         final String htmlContent = MessageFormat.format(htmlContent_, new String[]{disabledAttribute});
177         final List collectedAlerts = new ArrayList();
178         final HtmlPage page = loadPage(htmlContent, collectedAlerts);
179         
180         final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
181
182         final DisabledElement element = (DisabledElement) form.getHtmlElementById("element1");
183
184         assertEquals(expectedIsDisabled, element.isDisabled());
185     }
186 }
187
Popular Tags