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.List;
042:
043: import com.gargoylesoftware.htmlunit.MockWebConnection;
044: import com.gargoylesoftware.htmlunit.WebClient;
045: import com.gargoylesoftware.htmlunit.WebTestCase;
046:
047: /**
048: * Tests for {@link HtmlRadioButtonInput}.
049: *
050: * @version $Revision: 2132 $
051: * @author Mike Bresnahan
052: * @author Marc Guillemot
053: * @author Bruce Faulkner
054: * @author Ahmed Ashour
055: */
056: public class HtmlRadioButtonInputTest extends WebTestCase {
057: /**
058: * Create an instance
059: *
060: * @param name The name of the test
061: */
062: public HtmlRadioButtonInputTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * Verifies that a asText() returns "checked" or "unchecked" according to the state of the radio.
068: * @throws Exception if the test fails
069: */
070: public void test_asTextWhenNotChecked() throws Exception {
071: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
072: + "<form id='form1'>\n"
073: + " <input type='radio' name='radio' id='radio'>Check me</input>\n"
074: + "</form></body></html>";
075:
076: final HtmlPage page = loadPage(htmlContent);
077:
078: final HtmlRadioButtonInput radio = (HtmlRadioButtonInput) page
079: .getHtmlElementById("radio");
080: assertEquals("unchecked", radio.asText());
081: radio.setChecked(true);
082: assertEquals("checked", radio.asText());
083: }
084:
085: /**
086: * @throws Exception if the test fails
087: */
088: public void testOnchangeHandlerInvoked() throws Exception {
089: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
090: + "<form id='form1'>\n"
091: + " <input type='radio' name='radio' id='radio'"
092: + "onchange='this.value=\"new\" + this.checked'>Check me</input>\n"
093: + "</form></body></html>";
094:
095: final HtmlPage page = loadPage(htmlContent);
096:
097: final HtmlRadioButtonInput radio = (HtmlRadioButtonInput) page
098: .getHtmlElementById("radio");
099:
100: assertFalse(radio.isChecked());
101:
102: radio.setChecked(true);
103:
104: assertTrue(radio.isChecked());
105:
106: assertEquals("newtrue", radio.getValueAttribute());
107: }
108:
109: /**
110: * @throws Exception if the test fails
111: */
112: public void testOnchangeHandlerNotInvokedIfNotChanged()
113: throws Exception {
114: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
115: + "<form id='form1'>\n"
116: + " <input type='radio' name='radio' id='radio'"
117: + "onchange='this.value=\"new\" + this.checked'>Check me</input>\n"
118: + "</form></body></html>";
119:
120: final HtmlPage page = loadPage(htmlContent);
121:
122: final HtmlRadioButtonInput radio = (HtmlRadioButtonInput) page
123: .getHtmlElementById("radio");
124:
125: assertFalse(radio.isChecked());
126:
127: radio.setChecked(false);
128:
129: assertFalse(radio.isChecked());
130:
131: assertEquals("on", radio.getValueAttribute());
132: }
133:
134: /**
135: * @throws Exception if the test fails
136: */
137: public void testUpdateStateFirstForOnclickHandler()
138: throws Exception {
139: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
140: + "<script type='text/javascript'>\n"
141: + " function itemOnClickHandler() {"
142: + " var oneItem = document.getElementById('oneItem');\n"
143: + " var twoItems = document.getElementById('twoItems');\n"
144: + " alert('oneItem.checked: ' + oneItem.checked + ' twoItems.checked: ' + twoItems.checked);\n"
145: + " }"
146: + "</script>\n"
147: + "<form name='testForm'>\n"
148: + "Number of items:"
149: + "<input type='radio' name='numOfItems' value='1' checked='checked' "
150: + " onclick='itemOnClickHandler()' id='oneItem'>\n"
151: + "<label for='oneItem'>1</label>\n"
152: + "<input type='radio' name='numOfItems' value='2' onclick='itemOnClickHandler()' id='twoItems'>\n"
153: + "<label for='twoItems'>2</label>\n"
154: + "</form></body></html>";
155:
156: final List collectedAlerts = new ArrayList();
157: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
158:
159: final HtmlRadioButtonInput oneItem = (HtmlRadioButtonInput) page
160: .getHtmlElementById("oneItem");
161: final HtmlRadioButtonInput twoItems = (HtmlRadioButtonInput) page
162: .getHtmlElementById("twoItems");
163:
164: assertTrue(oneItem.isChecked());
165: assertFalse(twoItems.isChecked());
166:
167: twoItems.click();
168:
169: assertTrue(twoItems.isChecked());
170: assertFalse(oneItem.isChecked());
171:
172: oneItem.click();
173:
174: assertTrue(oneItem.isChecked());
175: assertFalse(twoItems.isChecked());
176:
177: final String[] expectedAlerts = {
178: "oneItem.checked: false twoItems.checked: true",
179: "oneItem.checked: true twoItems.checked: false" };
180: assertEquals(expectedAlerts, collectedAlerts);
181: }
182:
183: /**
184: * @throws Exception if the test fails
185: */
186: public void testSetChecked() throws Exception {
187: final String firstContent = "<html><head><title>First</title></head><body>\n"
188: + "<form>\n"
189: + "<input id='myRadio' type='radio' onchange=\"window.location.href='"
190: + URL_SECOND
191: + "'\">\n"
192: + "</form>\n"
193: + "</body></html>";
194: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
195:
196: final WebClient client = new WebClient();
197:
198: final MockWebConnection webConnection = new MockWebConnection(
199: client);
200: webConnection.setResponse(URL_FIRST, firstContent);
201: webConnection.setResponse(URL_SECOND, secondContent);
202: client.setWebConnection(webConnection);
203:
204: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
205: final HtmlRadioButtonInput radio = (HtmlRadioButtonInput) page
206: .getHtmlElementById("myRadio");
207:
208: final HtmlPage secondPage = (HtmlPage) radio.setChecked(true);
209:
210: assertEquals("Second", secondPage.getTitleText());
211: }
212:
213: /**
214: * Test <code>input.checked</code> if the radio <code><input></code> do not have distinct 'value'
215: * @throws Exception If the test fails
216: */
217: public void testRadioInputChecked() throws Exception {
218: final String content = "<html><head>\n" + "</head>\n"
219: + "<body>\n" + "<form name='myForm'>\n"
220: + " <input type='radio' name='myRadio'>\n"
221: + " <input type='radio' name='myRadio'>\n"
222: + "</form>\n" + "<script>\n"
223: + " var r1 = document.forms.myForm.myRadio[0];\n"
224: + " var r2 = document.forms.myForm.myRadio[1];\n"
225: + " alert(r1.checked + ',' + r2.checked);\n"
226: + " r1.checked = true;\n"
227: + " alert(r1.checked + ',' + r2.checked);\n"
228: + " r2.checked = true;\n"
229: + " alert(r1.checked + ',' + r2.checked);\n"
230: + "</script>\n" + "</body></html>";
231:
232: final String[] expectedAlerts = { "false,false", "true,false",
233: "false,true" };
234: final List collectedAlerts = new ArrayList();
235: loadPage(content, collectedAlerts);
236: assertEquals(expectedAlerts, collectedAlerts);
237: }
238:
239: /**
240: * @throws Exception If the test fails
241: */
242: public void testSetCheckedOutsideForm() throws Exception {
243: final String content = "<html><head>\n" + "</head>\n"
244: + "<body>\n"
245: + "<input type='radio' id='radio1' name='myRadio'>\n"
246: + "<input type='radio' id='radio2' name='myRadio'>\n"
247: + "<form name='myForm'>\n"
248: + " <input type='radio' id='radio3' name='myRadio'>\n"
249: + " <input type='radio' id='radio4' name='myRadio'>\n"
250: + "</form>\n" + "</body></html>";
251:
252: final List collectedAlerts = new ArrayList();
253: final HtmlPage page = loadPage(content, collectedAlerts);
254:
255: final HtmlRadioButtonInput radio1 = (HtmlRadioButtonInput) page
256: .getHtmlElementById("radio1");
257: final HtmlRadioButtonInput radio2 = (HtmlRadioButtonInput) page
258: .getHtmlElementById("radio2");
259: final HtmlRadioButtonInput radio3 = (HtmlRadioButtonInput) page
260: .getHtmlElementById("radio3");
261: final HtmlRadioButtonInput radio4 = (HtmlRadioButtonInput) page
262: .getHtmlElementById("radio4");
263:
264: assertFalse(radio1.isChecked());
265: assertFalse(radio2.isChecked());
266: assertFalse(radio3.isChecked());
267: assertFalse(radio4.isChecked());
268:
269: radio1.setChecked(true);
270:
271: assertTrue(radio1.isChecked());
272: assertFalse(radio2.isChecked());
273: assertFalse(radio3.isChecked());
274: assertFalse(radio4.isChecked());
275:
276: radio2.setChecked(true);
277:
278: assertFalse(radio1.isChecked());
279: assertTrue(radio2.isChecked());
280: assertFalse(radio3.isChecked());
281: assertFalse(radio4.isChecked());
282:
283: radio3.setChecked(true);
284:
285: assertFalse(radio1.isChecked());
286: assertTrue(radio2.isChecked());
287: assertTrue(radio3.isChecked());
288: assertFalse(radio4.isChecked());
289:
290: radio4.setChecked(true);
291:
292: assertFalse(radio1.isChecked());
293: assertTrue(radio2.isChecked());
294: assertFalse(radio3.isChecked());
295: assertTrue(radio4.isChecked());
296: }
297:
298: }
|