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.lang.reflect.Method;
041: import java.lang.reflect.Modifier;
042: import java.text.MessageFormat;
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import com.gargoylesoftware.htmlunit.WebTestCase;
050:
051: /**
052: * Tests the <code>isDisabled()</code> method on all of the elements that must implement the <code>disabled</code>
053: * attribute: <code>button</code>, <code>input</code>, <code>optgroup</code>, <code>option</code>, <code>select</code>
054: * and <code>textarea</code>.
055: *
056: * @version $Revision: 2132 $
057: * @author David D. Kilzer
058: */
059: public class DisabledElementTest extends WebTestCase {
060:
061: private final String htmlContent_;
062:
063: /**
064: * Generate a <code>TestSuite</code> for every HTML element that implements {@link DisabledElement}.
065: *
066: * @return The test suite to run
067: */
068: public static Test suite() {
069: final TestSuite suite = new TestSuite();
070: addTestCases(suite, "button",
071: "<button id='element1' {0}>foo</button>");
072: addTestCases(suite, "input_button",
073: "<input type='button' id='element1' {0}>");
074: addTestCases(suite, "input_checkbox",
075: "<input type='checkbox' id='element1' {0}>");
076: addTestCases(suite, "input_file",
077: "<input type='file' id='element1' {0}>");
078: addTestCases(suite, "input_hidden",
079: "<input type='hidden' id='element1' {0}>");
080: addTestCases(suite, "input_image",
081: "<input type='image' id='element1' {0}>");
082: addTestCases(suite, "input_password",
083: "<input type='password' id='element1' {0}>");
084: addTestCases(suite, "input_radio",
085: "<input type='radio' id='element1' {0}>");
086: addTestCases(suite, "input_reset",
087: "<input type='reset' id='element1' {0}>");
088: addTestCases(suite, "input_submit",
089: "<input type='submit' id='element1' {0}>");
090: addTestCases(suite, "input_text",
091: "<input type='text' id='element1' {0}>");
092: addTestCases(
093: suite,
094: "optgroup",
095: "<select><optgroup id='element1' {0}><option value='1'></option></optgroup></select>");
096: addTestCases(suite, "option",
097: "<select><option id='element1' value='1' {0}></option></select>");
098: addTestCases(suite, "select",
099: "<select id='element1' {0}><option value='1'></option></select>");
100: addTestCases(suite, "textarea",
101: "<textarea id='element1' {0}></textarea>");
102: return suite;
103: }
104:
105: /**
106: * Adds test cases to the <code>suite</code> argument for the given parameters.
107: *
108: * @param suite The <code>TestSuite</code> to which to add tests
109: * @param elementName The name of the HTML element being tested
110: * @param elementHtml The html representing the element to test with attribute <code>id='element1'</code>
111: */
112: private static void addTestCases(final TestSuite suite,
113: final String elementName, final String elementHtml) {
114:
115: final TestSuite subsuite = new TestSuite(
116: DisabledElementTest.class.getName() + '_' + elementName);
117:
118: final Method[] methods = DisabledElementTest.class.getMethods();
119: for (int i = 0; i < methods.length; i++) {
120: final Method method = methods[i];
121: if (Modifier.isPublic(method.getModifiers())
122: && method.getName().startsWith("test")) {
123: subsuite.addTest(new DisabledElementTest(method
124: .getName(), elementHtml));
125: }
126: }
127:
128: suite.addTest(subsuite);
129: }
130:
131: /**
132: * Creates an instance of the test class for testing <em>one</em> of the test methods.
133: *
134: * @param testName The name of the test method to run
135: * @param elementHtml The html representing the element to test with attribute <code>id='element1'</code>
136: */
137: public DisabledElementTest(final String testName,
138: final String elementHtml) {
139: super (testName);
140: final String htmlContent = "<html><body><form id='form1'>{0}</form></body></html>";
141: htmlContent_ = MessageFormat.format(htmlContent,
142: new String[] { elementHtml });
143: }
144:
145: /**
146: * Tests that the <code>isDisabled()</code> method returns <code>false</code> when the <code>disabled</code>
147: * attribute does not exist.
148: *
149: * @throws Exception If the test fails
150: */
151: public void testNoDisabledAttribute() throws Exception {
152: executeDisabledTest("", false);
153: }
154:
155: /**
156: * Tests that the <code>isDisabled()</code> method returns <code>true</code> when the <code>disabled</code>
157: * attribute exists and is blank.
158: *
159: * @throws Exception If the test fails
160: */
161: public void testBlankDisabledAttribute() throws Exception {
162: executeDisabledTest("disabled=''", true);
163: }
164:
165: /**
166: * Tests that the <code>isDisabled()</code> method returns <code>false</code> when the <code>disabled</code>
167: * attribute exists and is <em>not</em> blank.
168: *
169: * @throws Exception If the test fails
170: */
171: public void testPopulatedDisabledAttribute() throws Exception {
172: executeDisabledTest("disabled='disabled'", true);
173: }
174:
175: /**
176: * Tests the <code>isDisabled()</code> method with the given parameters.
177: *
178: * @param disabledAttribute The definition of the <code>disabled</code> attribute
179: * @param expectedIsDisabled The expected return value of the <code>isDisabled()</code> method
180: * @throws Exception If test fails
181: */
182: private void executeDisabledTest(final String disabledAttribute,
183: final boolean expectedIsDisabled) throws Exception {
184:
185: final String htmlContent = MessageFormat.format(htmlContent_,
186: new String[] { disabledAttribute });
187: final List collectedAlerts = new ArrayList();
188: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
189:
190: final HtmlForm form = (HtmlForm) page
191: .getHtmlElementById("form1");
192:
193: final DisabledElement element = (DisabledElement) form
194: .getHtmlElementById("element1");
195:
196: assertEquals(expectedIsDisabled, element.isDisabled());
197: }
198: }
|