KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
50  * Tests for HtmlInput
51  *
52  * @version $Revision: 1.13 $
53  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54  */

55 public final class HtmlInputTest extends WebTestCase {
56     /**
57      * Create an instance
58      *
59      * @param name The name of the test
60      */

61     public HtmlInputTest( final String name ) {
62         super( name );
63     }
64
65
66     /**
67      * Test that selecting one radio button will deselect all the others
68      *
69      * @exception Exception If the test fails
70      */

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         // Test that only one value for the radio button is being passed back to the server
91
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     /**
105      * @throws Exception if the test fails
106      */

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     /**
129      * @throws Exception if the test fails
130      */

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     /**
152      * @throws Exception if the test fails
153      */

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     /**
173      * @throws Exception if the test fails
174      */

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     /**
189      * Test that clicking a radio button will select it
190      *
191      * @exception Exception If the test fails
192      */

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     /**
214      * Test that default type of input is text
215      *
216      * @exception Exception If the test fails
217      */

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