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.net.URL;
041:
042: import com.gargoylesoftware.htmlunit.Page;
043: import com.gargoylesoftware.htmlunit.WebTestCase;
044:
045: /**
046: * Tests for {@link HtmlImage}.
047: *
048: * @version $Revision: 2132 $
049: * @author Marc Guillemot
050: * @author Ahmed Ashour
051: */
052: public class HtmlImageTest extends WebTestCase {
053: /**
054: * Create an instance
055: *
056: * @param name The name of the test
057: */
058: public HtmlImageTest(final String name) {
059: super (name);
060: }
061:
062: /**
063: * @throws Exception if the test fails
064: */
065: public void testIsMapClick() throws Exception {
066: testIsMapClick("img1", false, "?0,0", "?25,30");
067: testIsMapClick("img2", false, "", "");
068: testIsMapClick("img3", true, "", "");
069: testIsMapClick("img3", true, "", "");
070: }
071:
072: private void testIsMapClick(final String imgId,
073: final boolean samePage, final String urlSuffixClick,
074: final String urlSuffixClickXY) throws Exception {
075:
076: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
077: + "<a href='http://server/foo'>\n"
078: + "<img id='img1' src='foo.png' ismap>\n"
079: + "<img id='img2' src='foo.png'>\n"
080: + "</a>\n"
081: + "<img id='img3' src='foo.png' ismap>\n"
082: + "<img id='img4' src='foo.png'>\n" + "</body></html>";
083: final HtmlPage page = loadPage(htmlContent);
084:
085: final HtmlImage img = (HtmlImage) page
086: .getHtmlElementById(imgId);
087:
088: final Page page2 = img.click();
089: assertEquals("same page after click", samePage, (page == page2));
090: if (!samePage) {
091: assertEquals("http://server/foo" + urlSuffixClick, page2
092: .getWebResponse().getUrl());
093: }
094:
095: final Page page3 = img.click(25, 30);
096: assertEquals("same page after click(25, 30)", samePage,
097: (page == page3));
098: if (!samePage) {
099: assertEquals("http://server/foo" + urlSuffixClickXY, page3
100: .getWebResponse().getUrl());
101: }
102: }
103:
104: /**
105: * @throws Exception if the test fails
106: */
107: public void testUseMapClick() throws Exception {
108: testUseMapClick(0, 0, "/");
109: testUseMapClick(10, 10, "a.html");
110: testUseMapClick(20, 10, "a.html");
111: testUseMapClick(29, 10, "b.html");
112: testUseMapClick(50, 50, "/");
113: }
114:
115: /**
116: * @throws Exception if the test fails
117: */
118: private void testUseMapClick(final int x, final int y,
119: final String urlSuffix) throws Exception {
120: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
121: + "<img id='myImg' src='foo.png' usemap='#map1'>\n"
122: + "<map name='map1'>\n"
123: + "<area href='a.html' shape='rect' coords='5,5,20,20'>\n"
124: + "<area href='b.html' shape='circle' coords='25,10,10'>\n"
125: + "</map>\n" + "</body></html>";
126: final HtmlPage page = loadPage(htmlContent);
127:
128: final HtmlImage img = (HtmlImage) page
129: .getHtmlElementById("myImg");
130:
131: final Page page2 = img.click(x, y);
132: final URL url = page2.getWebResponse().getUrl();
133: assertTrue(url.toExternalForm(), url.toExternalForm().endsWith(
134: urlSuffix));
135: }
136:
137: /**
138: * Tests circle radius of percentage value, Not Yet Implemented
139: * @throws Exception if the test fails
140: */
141: public void testUseMapClick_CircleRadiusPercentage()
142: throws Exception {
143: if (notYetImplemented()) {
144: return;
145: }
146:
147: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
148: + "<img id='myImg' src='foo.png' usemap='#map1'>\n"
149: + "<map name='map1'>\n"
150: + "<area href='a.html' shape='rect' coords='5,5,20,20'>\n"
151: + "<area href='b.html' shape='circle' coords='25,10,10%'>\n"
152: + "</map>\n" + "</body></html>";
153: final HtmlPage page = loadPage(htmlContent);
154:
155: final HtmlImage img = (HtmlImage) page
156: .getHtmlElementById("myImg");
157:
158: img.click(0, 0);
159: }
160: }
|