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 HtmlTextInput}.
050: *
051: * @version $Revision: 2166 $
052: * @author Ahmed Ashour
053: */
054: public class HtmlTextInputTest extends WebTestCase {
055:
056: /**
057: * Create an instance
058: *
059: * @param name The name of the test
060: */
061: public HtmlTextInputTest(final String name) {
062: super (name);
063: }
064:
065: /**
066: * @throws Exception if the test fails
067: */
068: public void testType() throws Exception {
069: final String html = "<html><head></head>\n" + "<body>\n"
070: + "<input id='text1'/>\n" + "</body></html>";
071: final HtmlPage page = (HtmlPage) loadPage(html);
072: final HtmlTextInput text1 = (HtmlTextInput) page
073: .getHtmlElementById("text1");
074: text1.type("abcd");
075: assertEquals("abcd", text1.getValueAttribute());
076: }
077:
078: /**
079: * @throws Exception if an error occurs
080: */
081: public void testPreventDefault() throws Exception {
082: testPreventDefault(BrowserVersion.FIREFOX_2);
083: testPreventDefault(BrowserVersion.INTERNET_EXPLORER_7_0);
084: }
085:
086: private void testPreventDefault(final BrowserVersion browserVersion)
087: throws Exception {
088: final String html = "<html><head><script>\n"
089: + " function handler(e) {\n"
090: + " if (e && e.target.value.length > 2)\n"
091: + " e.preventDefault();\n"
092: + " else if (!e && window.event.srcElement.value.length > 2)\n"
093: + " return false;\n"
094: + " }\n"
095: + " function init() {\n"
096: + " document.getElementById('text1').onkeydown = handler;\n"
097: + " }\n" + "</script></head>\n"
098: + "<body onload='init()'>\n" + "<input id='text1'/>\n"
099: + "</body></html>";
100:
101: final HtmlPage page = (HtmlPage) loadPage(browserVersion, html,
102: null);
103: final HtmlTextInput text1 = (HtmlTextInput) page
104: .getHtmlElementById("text1");
105: text1.type("abcd");
106: assertEquals("abc", text1.getValueAttribute());
107: }
108:
109: /**
110: * @throws Exception if the test fails
111: */
112: public void testTypeNewLine() throws Exception {
113: final String firstContent = "<html><head><title>First</title></head><body>\n"
114: + "<form action='"
115: + URL_SECOND
116: + "'>\n"
117: + "<input name='myText' id='myText'>\n"
118: + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
119: + "</body></html>";
120: final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
121:
122: final WebClient client = new WebClient();
123:
124: final MockWebConnection webConnection = new MockWebConnection(
125: client);
126: webConnection.setResponse(URL_FIRST, firstContent);
127: webConnection.setDefaultResponse(secondContent);
128:
129: client.setWebConnection(webConnection);
130:
131: final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
132:
133: final HtmlTextInput textInput = (HtmlTextInput) firstPage
134: .getHtmlElementById("myText");
135:
136: final HtmlPage secondPage = (HtmlPage) textInput.type('\n');
137: assertEquals("Second", secondPage.getTitleText());
138:
139: }
140:
141: /**
142: * @throws Exception if an error occurs
143: */
144: public void testSelection() throws Exception {
145: if (notYetImplemented()) {
146: return;
147: }
148: final String html = "<html><head><script>\n"
149: + " function test() {\n"
150: + " alert(getSelection(document.getElementById('text1')).length);\n"
151: + " }\n"
152: + " function getSelection(element) {\n"
153: + " if (typeof element.selectionStart == 'number') {\n"
154: + " return element.value.substring(element.selectionStart, element.selectionEnd);\n"
155: + " } else if (document.selection && document.selection.createRange) {\n"
156: + " return document.selection.createRange().text;\n"
157: + " }\n" + " }\n" + "</script></head>\n"
158: + "<body onload='test()'>\n" + "<input id='text1'/>\n"
159: + "</body></html>";
160:
161: final String[] expectedAlerts = { "0" };
162: final List collectedAlerts = new ArrayList();
163: loadPage(html, collectedAlerts);
164: assertEquals(expectedAlerts, collectedAlerts);
165: }
166: }
|