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.util.ArrayList;
041: import java.util.Collections;
042: import java.util.List;
043:
044: import com.gargoylesoftware.htmlunit.MockWebConnection;
045: import com.gargoylesoftware.htmlunit.SubmitMethod;
046: import com.gargoylesoftware.htmlunit.WebTestCase;
047:
048: /**
049: * Tests for {@link HtmlInput}.
050: *
051: * @version $Revision: 2132 $
052: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
053: * @author Marc Guillemot
054: * @author Ahmed Ashour
055: */
056: public final class HtmlInputTest extends WebTestCase {
057: /**
058: * Create an instance
059: *
060: * @param name The name of the test
061: */
062: public HtmlInputTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * Test that selecting one radio button will deselect all the others
068: *
069: * @exception Exception If the test fails
070: */
071: public void testRadioButtonsAreMutuallyExclusive() throws Exception {
072: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
073: + "<form id='form1'>\n"
074: + "<input type='radio' name='foo' value='1' selected='selected'/>\n"
075: + "<input type='radio' name='foo' value='2'/>\n"
076: + "<input type='radio' name='foo' value='3'/>\n"
077: + "<input type='submit' name='button' value='foo'/>\n"
078: + "</form></body></html>";
079: final HtmlPage page = loadPage(htmlContent);
080: final MockWebConnection webConnection = getMockConnection(page);
081:
082: final HtmlForm form = (HtmlForm) page
083: .getHtmlElementById("form1");
084:
085: final HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) form
086: .getFirstByXPath("//input[@type='radio' and @name='foo' and @value='2']");
087:
088: final HtmlSubmitInput pushButton = (HtmlSubmitInput) form
089: .getInputByName("button");
090:
091: radioButton.setChecked(true);
092:
093: // Test that only one value for the radio button is being passed back to the server
094: final HtmlPage secondPage = (HtmlPage) pushButton.click();
095:
096: assertEquals("url", URL_GARGOYLE.toExternalForm()
097: + "?foo=2&button=foo", secondPage.getWebResponse()
098: .getUrl());
099: assertEquals("method", SubmitMethod.GET, webConnection
100: .getLastMethod());
101: assertNotNull(secondPage);
102: }
103:
104: /**
105: * @throws Exception if the test fails
106: */
107: public void testSetChecked_CheckBox() throws Exception {
108: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
109: + "<form id='form1'>\n"
110: + "<input type='checkbox' name='foo'/>\n"
111: + "<input type='checkbox' name='bar'/>\n"
112: + "<input type='submit' name='button' value='foo'/>\n"
113: + "</form></body></html>";
114: final HtmlPage page = loadPage(htmlContent);
115:
116: final HtmlForm form = (HtmlForm) page
117: .getHtmlElementById("form1");
118:
119: final HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) form
120: .getInputByName("foo");
121: assertFalse("Initial state", checkbox.isChecked());
122: checkbox.setChecked(true);
123: assertTrue("After setSelected(true)", checkbox.isChecked());
124: checkbox.setChecked(false);
125: assertFalse("After setSelected(false)", checkbox.isChecked());
126: }
127:
128: /**
129: * @throws Exception if the test fails
130: */
131: public void testGetChecked_RadioButton() throws Exception {
132: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
133: + "<form id='form1'>\n"
134: + "<input type='radio' name='radio1'>\n"
135: + "<input type='RADIO' name='radio1' value='bar' checked>\n"
136: + "<input type='submit' name='button' value='foo'>\n"
137: + "</form></body></html>";
138: final HtmlPage page = loadPage(htmlContent);
139:
140: final HtmlForm form = (HtmlForm) page
141: .getHtmlElementById("form1");
142:
143: final List radioButtons = form.getRadioButtonsByName("radio1");
144: assertEquals(2, radioButtons.size());
145:
146: assertFalse(((HtmlRadioButtonInput) radioButtons.get(0))
147: .isChecked());
148: assertTrue(((HtmlRadioButtonInput) radioButtons.get(1))
149: .isChecked());
150: }
151:
152: /**
153: * @throws Exception if the test fails
154: */
155: public void testOnChangeHandler() throws Exception {
156: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
157: + "<form id='form1'>\n"
158: + "<input type='text' name='text1' onchange='alert(\"changed\")')>\n"
159: + "</form></body></html>";
160: final List collectedAlerts = new ArrayList();
161: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
162:
163: final HtmlForm form = (HtmlForm) page
164: .getHtmlElementById("form1");
165: final HtmlTextInput input = (HtmlTextInput) form
166: .getInputByName("text1");
167:
168: assertEquals(Collections.EMPTY_LIST, collectedAlerts);
169: input.setValueAttribute("foo");
170: assertEquals(new String[] { "changed" }, collectedAlerts);
171: }
172:
173: /**
174: * @throws Exception if the test fails
175: */
176: public void testCheckboxDefaultValue() throws Exception {
177: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
178: + "<form id='form1'>\n"
179: + "<input type='checkbox' name='checkbox1')>\n"
180: + "</form></body></html>";
181: final HtmlPage page = loadPage(htmlContent);
182:
183: final HtmlForm form = (HtmlForm) page
184: .getHtmlElementById("form1");
185: final HtmlCheckBoxInput input = (HtmlCheckBoxInput) form
186: .getInputByName("checkbox1");
187: assertEquals("on", input.getValueAttribute());
188: }
189:
190: /**
191: * Test that clicking a radio button will select it
192: *
193: * @exception Exception If the test fails
194: */
195: public void testClickRadioButton() throws Exception {
196: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
197: + "<form id='form1'>\n"
198: + "<input type='radio' name='foo' value='1' selected='selected'/>\n"
199: + "<input type='radio' name='foo' value='2'/>\n"
200: + "<input type='radio' name='foo' value='3'/>\n"
201: + "<input type='submit' name='button' value='foo'/>\n"
202: + "</form></body></html>";
203: final HtmlPage page = loadPage(htmlContent);
204:
205: final HtmlForm form = (HtmlForm) page
206: .getHtmlElementById("form1");
207:
208: final HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) form
209: .getFirstByXPath("//input[@type='radio' and @name='foo' and @value='2']");
210:
211: assertFalse("Should not be checked before click", radioButton
212: .isChecked());
213: radioButton.click();
214: assertTrue("Should be checked after click", radioButton
215: .isChecked());
216: }
217:
218: /**
219: * Test that default type of input is text
220: *
221: * @exception Exception If the test fails
222: */
223: public void testInputNoType() throws Exception {
224: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
225: + "<form id='form1'>\n"
226: + "<input name='foo'/>\n"
227: + "</form></body></html>";
228:
229: final HtmlPage page = loadPage(htmlContent);
230: final HtmlForm form = (HtmlForm) page
231: .getHtmlElementById("form1");
232:
233: assertEquals("text", form.getInputByName("foo")
234: .getTypeAttribute());
235: }
236:
237: /**
238: * @throws Exception if the test fails
239: */
240: public void testOnChangeHandlerNotFiredOnLoad() throws Exception {
241: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
242: + "<form id='form1'>\n"
243: + "<input type='file' name='text1' onchange='alert(\"changed\")')>\n"
244: + "</form></body></html>";
245: final List collectedAlerts = new ArrayList();
246: loadPage(htmlContent, collectedAlerts);
247: assertEquals(Collections.EMPTY_LIST, collectedAlerts);
248: }
249:
250: /**
251: * @throws Exception if the test fails
252: */
253: public void testBadInputType() throws Exception {
254: final String htmlContent = "<html><head><title>foo</title></head>\n"
255: + "<body onload='alert(document.form1.text1.type)'>\n"
256: + "<form name='form1'>\n"
257: + "<input type='foo' name='text1'>\n"
258: + "</form></body></html>";
259: final String[] expectedAlerts = { "text" };
260: createTestPageForRealBrowserIfNeeded(htmlContent,
261: expectedAlerts);
262:
263: final List collectedAlerts = new ArrayList();
264: loadPage(htmlContent, collectedAlerts);
265: }
266:
267: /**
268: * @throws Exception If the test fails.
269: */
270: public void testOnchangeNull() throws Exception {
271: final String html = "<html><head>\n"
272: + "<script>\n"
273: + " function handler() {}\n"
274: + " function test() {\n"
275: + " var elem = document.getElementById('myInput');\n"
276: + " elem.onchange = handler;\n"
277: + " alert(elem.onchange);\n"
278: + " elem.onchange = null;\n"
279: + " alert(elem.onchange);\n" + " }\n"
280: + "</script>\n" + "<body onload=test()>\n"
281: + " <input id='myInput'>\n" + "</body></html>";
282: final String[] expectedAlerts = {
283: "\nfunction handler() {\n}\n", "null" };
284: final List collectedAlerts = new ArrayList();
285: loadPage(html, collectedAlerts);
286: assertEquals(expectedAlerts, collectedAlerts);
287: }
288: }
|