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.Arrays;
042: import java.util.List;
043:
044: import com.gargoylesoftware.htmlunit.KeyValuePair;
045: import com.gargoylesoftware.htmlunit.MockWebConnection;
046: import com.gargoylesoftware.htmlunit.WebTestCase;
047:
048: /**
049: * Tests for {@link HtmlImageInput}.
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 class HtmlImageInputTest extends WebTestCase {
057: /**
058: * Create an instance
059: *
060: * @param name The name of the test
061: */
062: public HtmlImageInputTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * @throws Exception if the test fails
068: */
069: public void testClick_NoPosition() throws Exception {
070: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
071: + "<form id='form1' method='post'>\n"
072: + "<input type='image' name='aButton' value='foo'/>\n"
073: + "<input type='image' name='button' value='foo'/>\n"
074: + "<input type='image' name='anotherButton' value='foo'/>\n"
075: + "</form></body></html>";
076: final HtmlPage page = loadPage(htmlContent);
077: final MockWebConnection webConnection = getMockConnection(page);
078:
079: final HtmlForm form = (HtmlForm) page
080: .getHtmlElementById("form1");
081:
082: final HtmlImageInput imageInput = (HtmlImageInput) form
083: .getInputByName("button");
084: final HtmlPage secondPage = (HtmlPage) imageInput.click();
085: assertNotNull(secondPage);
086:
087: final List expectedPairs = Arrays.asList(new Object[] {
088: new KeyValuePair("button.x", "0"),
089: new KeyValuePair("button.y", "0") });
090:
091: assertEquals(expectedPairs, webConnection.getLastParameters());
092: }
093:
094: /**
095: * @throws Exception if the test fails
096: */
097: public void testClick_WithPosition() throws Exception {
098: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
099: + "<form id='form1' method='post'>\n"
100: + "<input type='image' name='aButton' value='foo'/>\n"
101: + "<input type='image' name='button' value='foo'/>\n"
102: + "<input type='image' name='anotherButton' value='foo'/>\n"
103: + "</form></body></html>";
104: final HtmlPage page = loadPage(htmlContent);
105: final MockWebConnection webConnection = getMockConnection(page);
106:
107: final HtmlForm form = (HtmlForm) page
108: .getHtmlElementById("form1");
109:
110: final HtmlImageInput imageInput = (HtmlImageInput) form
111: .getInputByName("button");
112: final HtmlPage secondPage = (HtmlPage) imageInput.click(100,
113: 200);
114: assertNotNull(secondPage);
115:
116: final List expectedPairs = Arrays.asList(new Object[] {
117: new KeyValuePair("button.x", "100"),
118: new KeyValuePair("button.y", "200") });
119:
120: assertEquals(expectedPairs, webConnection.getLastParameters());
121: }
122:
123: /**
124: * If an image button without name is clicked, it should send only "x" and "y" parameters
125: * Regression test for bug 1118877
126: * @throws Exception if the test fails
127: */
128: public void testNoNameClick_WithPosition() throws Exception {
129: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
130: + "<form id='form1' method='post'>\n"
131: + "<input type='image' value='foo'/>\n"
132: + "</form></body></html>";
133: final HtmlPage page = loadPage(htmlContent);
134: final MockWebConnection webConnection = getMockConnection(page);
135:
136: final HtmlForm form = (HtmlForm) page
137: .getHtmlElementById("form1");
138:
139: final HtmlImageInput imageInput = (HtmlImageInput) form
140: .getInputByValue("foo");
141: final HtmlPage secondPage = (HtmlPage) imageInput.click(100,
142: 200);
143: assertNotNull(secondPage);
144:
145: final List expectedPairs = Arrays.asList(new Object[] {
146: new KeyValuePair("x", "100"),
147: new KeyValuePair("y", "200") });
148:
149: assertEquals(expectedPairs, webConnection.getLastParameters());
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='image' src='test.png' 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 HtmlImageInput input = (HtmlImageInput) page
165: .getHtmlElementById("myInput");
166: input.click();
167:
168: assertEquals(expectedAlerts, collectedAlerts);
169: }
170: }
|