1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Collections; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.KeyValuePair; 45 import com.gargoylesoftware.htmlunit.MockWebConnection; 46 import com.gargoylesoftware.htmlunit.SubmitMethod; 47 import com.gargoylesoftware.htmlunit.WebTestCase; 48 49 55 public final class HtmlInputTest extends WebTestCase { 56 61 public HtmlInputTest( final String name ) { 62 super( name ); 63 } 64 65 66 71 public void testRadioButtonsAreMutuallyExclusive() throws Exception { 72 final String htmlContent 73 = "<html><head><title>foo</title></head><body>" 74 + "<form id='form1'>" 75 + "<input type='radio' name='foo' value='1' selected='selected'/>" 76 + "<input type='radio' name='foo' value='2'/>" 77 + "<input type='radio' name='foo' value='3'/>" 78 + "<input type='submit' name='button' value='foo'/>" 79 + "</form></body></html>"; 80 final HtmlPage page = loadPage(htmlContent); 81 final MockWebConnection webConnection = getMockConnection(page); 82 83 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 84 85 final HtmlRadioButtonInput radioButton = form.getRadioButtonInput( "foo", "2" ); 86 final HtmlSubmitInput pushButton = ( HtmlSubmitInput )form.getInputByName( "button" ); 87 88 radioButton.setChecked( true ); 89 90 final HtmlPage secondPage = ( HtmlPage )pushButton.click(); 92 93 final List expectedParameters = new ArrayList(); 94 expectedParameters.add( new KeyValuePair( "foo", "2" ) ); 95 expectedParameters.add( new KeyValuePair( "button", "foo" ) ); 96 97 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 98 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 99 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 100 assertNotNull( secondPage ); 101 } 102 103 104 107 public void testSetChecked_CheckBox() throws Exception { 108 final String htmlContent 109 = "<html><head><title>foo</title></head><body>" 110 + "<form id='form1'>" 111 + "<input type='checkbox' name='foo'/>" 112 + "<input type='checkbox' name='bar'/>" 113 + "<input type='submit' name='button' value='foo'/>" 114 + "</form></body></html>"; 115 final HtmlPage page = loadPage(htmlContent); 116 117 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 118 119 final HtmlCheckBoxInput checkbox = ( HtmlCheckBoxInput )form.getInputByName( "foo" ); 120 assertFalse( "Initial state", checkbox.isChecked() ); 121 checkbox.setChecked( true ); 122 assertTrue( "After setSelected(true)", checkbox.isChecked() ); 123 checkbox.setChecked( false ); 124 assertFalse( "After setSelected(false)", checkbox.isChecked() ); 125 } 126 127 128 131 public void testGetChecked_RadioButton() throws Exception { 132 final String htmlContent 133 = "<html><head><title>foo</title></head><body>" 134 + "<form id='form1'>" 135 + "<input type='radio' name='radio1'>" 136 + "<input type='RADIO' name='radio1' value='bar' checked>" 137 + "<input type='submit' name='button' value='foo'>" 138 + "</form></body></html>"; 139 final HtmlPage page = loadPage(htmlContent); 140 141 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 142 143 final List radioButtons = form.getRadioButtonsByName("radio1"); 144 assertEquals( 2, radioButtons.size() ); 145 146 assertFalse( ((HtmlRadioButtonInput)radioButtons.get(0)).isChecked() ); 147 assertTrue( ((HtmlRadioButtonInput)radioButtons.get(1)).isChecked() ); 148 } 149 150 151 154 public void testOnChangeHandler() throws Exception { 155 156 final String htmlContent 157 = "<html><head><title>foo</title></head><body>" 158 + "<form id='form1'>" 159 + "<input type='text' name='text1' onchange='alert(\"changed\")')>" 160 + "</form></body></html>"; 161 final List collectedAlerts = new ArrayList(); 162 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 163 164 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 165 final HtmlTextInput input = (HtmlTextInput)form.getInputByName("text1"); 166 167 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 168 input.setValueAttribute("foo"); 169 assertEquals( Collections.singletonList("changed"), collectedAlerts ); 170 } 171 172 175 public void testCheckboxDefaultValue() throws Exception { 176 final String htmlContent 177 = "<html><head><title>foo</title></head><body>" 178 + "<form id='form1'>" 179 + "<input type='checkbox' name='checkbox1')>" 180 + "</form></body></html>"; 181 final HtmlPage page = loadPage(htmlContent); 182 183 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 184 final HtmlCheckBoxInput input = (HtmlCheckBoxInput)form.getInputByName("checkbox1"); 185 assertEquals( "on", input.getValueAttribute() ); 186 } 187 188 193 public void testClickRadioButton() throws Exception { 194 final String htmlContent 195 = "<html><head><title>foo</title></head><body>" 196 + "<form id='form1'>" 197 + "<input type='radio' name='foo' value='1' selected='selected'/>" 198 + "<input type='radio' name='foo' value='2'/>" 199 + "<input type='radio' name='foo' value='3'/>" 200 + "<input type='submit' name='button' value='foo'/>" 201 + "</form></body></html>"; 202 final HtmlPage page = loadPage(htmlContent); 203 204 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 205 206 final HtmlRadioButtonInput radioButton = form.getRadioButtonInput( "foo", "2" ); 207 208 assertFalse("Should not be checked before click", radioButton.isChecked()); 209 radioButton.click(); 210 assertTrue("Should be checked after click", radioButton.isChecked()); 211 } 212 213 218 public void testInputNoType() throws Exception { 219 final String htmlContent 220 = "<html><head><title>foo</title></head><body>" 221 + "<form id='form1'>" 222 + "<input name='foo'/>" 223 + "</form></body></html>"; 224 225 final HtmlPage page = loadPage(htmlContent); 226 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 227 228 assertEquals("text", form.getInputByName("foo").getTypeAttribute()); 229 } 230 } 231 232 | Popular Tags |