1 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 61 public class DisabledElementTest extends WebTestCase { 62 63 private final String htmlContent_; 64 65 66 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 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 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 139 public void testNoDisabledAttribute() throws Exception { 140 executeDisabledTest("", false); 141 } 142 143 144 150 public void testBlankDisabledAttribute() throws Exception { 151 executeDisabledTest("disabled=''", true); 152 } 153 154 155 161 public void testPopulatedDisabledAttribute() throws Exception { 162 executeDisabledTest("disabled='disabled'", true); 163 } 164 165 166 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 |