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.BrowserVersion;
044: import com.gargoylesoftware.htmlunit.MockWebConnection;
045: import com.gargoylesoftware.htmlunit.WebClient;
046: import com.gargoylesoftware.htmlunit.WebTestCase;
047:
048: /**
049: * Tests for {@link HtmlCheckBoxInput}.
050: *
051: * @version $Revision: 2132 $
052: * @author Mike Bresnahan
053: * @author Marc Guillemot
054: * @author Ahmed Ashour
055: */
056: public class HtmlCheckBoxInputTest extends WebTestCase {
057: /**
058: * Create an instance
059: *
060: * @param name The name of the test
061: */
062: public HtmlCheckBoxInputTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * Verifies that a HtmlCheckBox is unchecked by default.
068: * The onClick tests make this assumption.
069: * @throws Exception if the test fails
070: */
071: public void test_defaultState() throws Exception {
072: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
073: + "<form id='form1'>\n"
074: + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
075: + "</form></body></html>";
076: final HtmlPage page = loadPage(htmlContent);
077: final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) page
078: .getHtmlElementById("checkbox");
079:
080: assertFalse(checkBox.isChecked());
081: }
082:
083: /**
084: * Given a onclick handler that does not cause the form to submit, this test
085: * verifies that HtmlCheckBix.click()
086: * <ul>
087: * <li>sets the checkbox to the "checked" state</li>
088: * <li>returns the same page</li>
089: * </ul>
090: * @throws Exception if the test fails
091: */
092: public void test_onClick() throws Exception {
093: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
094: + "<form id='form1' onSubmit='alert(\"bar\")' onReset='alert(\"reset\")'>\n"
095: + " <input type='checkbox' name='checkbox' id='checkbox' "
096: + "onClick='alert(\"foo\");alert(event.type);'>Check me</input>\n"
097: + "</form></body></html>";
098:
099: final List collectedAlerts = new ArrayList();
100: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
101: final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) page
102: .getHtmlElementById("checkbox");
103: final HtmlPage secondPage = (HtmlPage) checkBox.click();
104:
105: final String[] expectedAlerts = { "foo", "click" };
106: assertEquals(expectedAlerts, collectedAlerts);
107:
108: assertSame(page, secondPage);
109: assertTrue(checkBox.isChecked());
110: }
111:
112: /**
113: * Given a onclick handler that causes the form to submit, this test verifies that HtmlCheckBix.click()
114: * <ul>
115: * <li>sets the checkbox to the "checked" state</li>
116: * <li>returns the new page</li>
117: * </ul>
118: * @throws Exception if the test fails
119: */
120: public void test_onClickThatSubmitsForm() throws Exception {
121: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
122: + "<form id='form1' name='form1'>\n"
123: + " <input type='checkbox' name='checkbox' id='checkbox' "
124: + "onClick='document.form1.submit()'>Check me</input>\n"
125: + "</form></body></html>";
126: final HtmlPage page = loadPage(htmlContent);
127: final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) page
128: .getHtmlElementById("checkbox");
129:
130: final HtmlPage secondPage = (HtmlPage) checkBox.click();
131:
132: assertNotSame(page, secondPage);
133: assertTrue(checkBox.isChecked());
134: }
135:
136: /**
137: * Verifies that a asText() returns "checked" or "unchecked" according to the state of the checkbox.
138: * @throws Exception if the test fails
139: */
140: public void testAsText() throws Exception {
141: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
142: + "<form id='form1'>\n"
143: + " <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
144: + "</form></body></html>";
145:
146: final HtmlPage page = loadPage(htmlContent);
147:
148: final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) page
149: .getHtmlElementById("checkbox");
150: assertEquals("unchecked", checkBox.asText());
151: checkBox.setChecked(true);
152: assertEquals("checked", checkBox.asText());
153: }
154:
155: /**
156: * @throws Exception if the test fails
157: */
158: public void testOnchangeFires() throws Exception {
159: final String content = "<html><head><title>foo</title>\n"
160: + "</head><body>\n"
161: + "<form>\n"
162: + "<input type='checkbox' id='chkbox' onchange='alert(\"foo\");' />\n"
163: + "</form>\n" + "</body></html>";
164:
165: final String[] expectedAlerts = { "foo" };
166:
167: final List collectedAlerts = new ArrayList();
168: final HtmlPage page = loadPage(content, collectedAlerts);
169: final HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page
170: .getHtmlElementById("chkbox");
171: checkbox.setChecked(true);
172:
173: assertEquals(expectedAlerts, collectedAlerts);
174: }
175:
176: /**
177: * @throws Exception if the test fails
178: */
179: public void testSetChecked() throws Exception {
180: final String firstContent = "<html><head><title>First</title></head><body>\n"
181: + "<form>\n"
182: + "<input id='myCheckbox' type='checkbox' onchange=\"window.location.href='"
183: + URL_SECOND
184: + "'\">\n"
185: + "</form>\n"
186: + "</body></html>";
187: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
188:
189: final WebClient client = new WebClient();
190:
191: final MockWebConnection webConnection = new MockWebConnection(
192: client);
193: webConnection.setResponse(URL_FIRST, firstContent);
194: webConnection.setResponse(URL_SECOND, secondContent);
195: client.setWebConnection(webConnection);
196:
197: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
198: final HtmlCheckBoxInput radio = (HtmlCheckBoxInput) page
199: .getHtmlElementById("myCheckbox");
200:
201: final HtmlPage secondPage = (HtmlPage) radio.setChecked(true);
202:
203: assertEquals("Second", secondPage.getTitleText());
204: }
205:
206: /**
207: * @throws Exception if an error occurs
208: */
209: public void testPreventDefault() throws Exception {
210: testPreventDefault(BrowserVersion.FIREFOX_2);
211: testPreventDefault(BrowserVersion.INTERNET_EXPLORER_7_0);
212: }
213:
214: private void testPreventDefault(final BrowserVersion browserVersion)
215: throws Exception {
216: final String html = "<html><head><script>\n"
217: + " function handler(e) {\n"
218: + " if (e)\n"
219: + " e.preventDefault();\n"
220: + " else\n"
221: + " return false;\n"
222: + " }\n"
223: + " function init() {\n"
224: + " document.getElementById('checkbox1').onclick = handler;\n"
225: + " }\n" + "</script></head>\n"
226: + "<body onload='init()'>\n"
227: + "<input type='checkbox' id='checkbox1'/>\n"
228: + "</body></html>";
229: final HtmlPage page = (HtmlPage) loadPage(browserVersion, html,
230: null);
231: final HtmlCheckBoxInput checkbox1 = (HtmlCheckBoxInput) page
232: .getHtmlElementById("checkbox1");
233: checkbox1.click();
234: assertFalse(checkbox1.isChecked());
235: }
236: }
|