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.WebTestCase;
044:
045: /**
046: * Tests for {@link HtmlResetInput}.
047: *
048: * @version $Revision: 2132 $
049: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
050: * @author Ahmed Ashour
051: */
052: public class HtmlResetInputTest extends WebTestCase {
053: /**
054: * Create an instance
055: *
056: * @param name The name of the test
057: */
058: public HtmlResetInputTest(final String name) {
059: super (name);
060: }
061:
062: /**
063: * @throws Exception if the test fails
064: */
065: public void testReset() throws Exception {
066: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
067: + "<form id='form1'>\n"
068: + "<input type='text' name='textfield1' id='textfield1' value='foo'/>\n"
069: + "<input type='password' name='password1' id='password1' value='foo'/>\n"
070: + "<input type='hidden' name='hidden1' id='hidden1' value='foo'/>\n"
071: + "<input type='radio' name='radioButton' value='foo' checked/>\n"
072: + "<input type='radio' name='radioButton' value='bar'/>\n"
073: + "<input type='checkbox' name='checkBox' value='check'/>\n"
074: + "<select id='select1'>\n"
075: + " <option id='option1' selected value='1'>Option1</option>\n"
076: + " <option id='option2' value='2'>Option2</option>\n"
077: + "</select>\n"
078: + "<textarea id='textarea1'>Foobar</textarea>\n"
079: + "<isindex prompt='Enter some text' id='isindex1'>\n"
080: + "<input type='reset' name='resetButton' value='pushme'/>\n"
081: + "</form></body></html>";
082: final HtmlPage page = loadPage(htmlContent);
083:
084: final HtmlForm form = (HtmlForm) page
085: .getHtmlElementById("form1");
086: final HtmlResetInput resetInput = (HtmlResetInput) form
087: .getInputByName("resetButton");
088:
089: // change all the values to something else
090: ((HtmlRadioButtonInput) form
091: .getFirstByXPath("//input[@type='radio' and @name='radioButton' and @value='bar']"))
092: .setChecked(true);
093: ((HtmlCheckBoxInput) form.getInputByName("checkBox"))
094: .setChecked(true);
095: ((HtmlOption) page.getHtmlElementById("option1"))
096: .setSelected(false);
097: ((HtmlOption) page.getHtmlElementById("option2"))
098: .setSelected(true);
099: ((HtmlTextArea) page.getHtmlElementById("textarea1"))
100: .setText("Flintstone");
101: ((HtmlTextInput) page.getHtmlElementById("textfield1"))
102: .setValueAttribute("Flintstone");
103: ((HtmlHiddenInput) page.getHtmlElementById("hidden1"))
104: .setValueAttribute("Flintstone");
105: ((HtmlPasswordInput) page.getHtmlElementById("password1"))
106: .setValueAttribute("Flintstone");
107: ((HtmlIsIndex) page.getHtmlElementById("isindex1"))
108: .setValue("Flintstone");
109:
110: // Check to make sure they did get changed
111: assertEquals("bar", form.getCheckedRadioButton("radioButton")
112: .getValueAttribute());
113: assertTrue(((HtmlCheckBoxInput) form.getInputByName("checkBox"))
114: .isChecked());
115: assertFalse(((HtmlOption) page.getHtmlElementById("option1"))
116: .isSelected());
117: assertTrue(((HtmlOption) page.getHtmlElementById("option2"))
118: .isSelected());
119: assertEquals("Flintstone", ((HtmlTextArea) page
120: .getHtmlElementById("textarea1")).getText());
121: assertEquals("Flintstone", ((HtmlTextInput) page
122: .getHtmlElementById("textfield1")).getValueAttribute());
123: assertEquals("Flintstone", ((HtmlHiddenInput) page
124: .getHtmlElementById("hidden1")).getValueAttribute());
125: assertEquals("Flintstone", ((HtmlIsIndex) page
126: .getHtmlElementById("isindex1")).getValue());
127:
128: final HtmlPage secondPage = (HtmlPage) resetInput.click();
129: assertSame(page, secondPage);
130:
131: // Check to make sure all the values have been set back to their original values.
132: assertEquals("foo", form.getCheckedRadioButton("radioButton")
133: .getValueAttribute());
134: assertFalse(((HtmlCheckBoxInput) form
135: .getInputByName("checkBox")).isChecked());
136: assertTrue(((HtmlOption) page.getHtmlElementById("option1"))
137: .isSelected());
138: assertFalse(((HtmlOption) page.getHtmlElementById("option2"))
139: .isSelected());
140: assertEquals("Foobar", ((HtmlTextArea) page
141: .getHtmlElementById("textarea1")).getText());
142: assertEquals("foo", ((HtmlTextInput) page
143: .getHtmlElementById("textfield1")).getValueAttribute());
144: assertEquals("foo", ((HtmlHiddenInput) page
145: .getHtmlElementById("hidden1")).getValueAttribute());
146: assertEquals("foo", ((HtmlPasswordInput) page
147: .getHtmlElementById("password1")).getValueAttribute());
148: assertEquals("", ((HtmlIsIndex) page
149: .getHtmlElementById("isindex1")).getValue());
150: }
151:
152: /**
153: * @throws Exception If the test fails
154: */
155: public void testOutsideForm() throws Exception {
156: final String html = "<html><head></head>\n"
157: + "<body>\n"
158: + "<input id='myInput' type='reset' onclick='alert(1)'>\n"
159: + "</body></html>";
160:
161: final String[] expectedAlerts = { "1" };
162: final List collectedAlerts = new ArrayList();
163: final HtmlPage page = loadPage(html, collectedAlerts);
164: final HtmlResetInput input = (HtmlResetInput) page
165: .getHtmlElementById("myInput");
166: input.click();
167:
168: assertEquals(expectedAlerts, collectedAlerts);
169: }
170: }
|