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.javascript;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import com.gargoylesoftware.htmlunit.BrowserVersion;
044: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
045: import com.gargoylesoftware.htmlunit.MockWebConnection;
046: import com.gargoylesoftware.htmlunit.WebClient;
047: import com.gargoylesoftware.htmlunit.WebTestCase;
048:
049: /**
050: * Tests for {@link HTMLCollection}.
051: *
052: * @version $Revision: 2158 $
053: * @author Marc Guillemot
054: * @author Ahmed Ashour
055: */
056: public class HTMLCollectionTest extends WebTestCase {
057:
058: /**
059: * Create an instance
060: * @param name The name of the test
061: */
062: public HTMLCollectionTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * @throws Exception if the test fails
068: */
069: public void testImplicitToStringConversion() throws Exception {
070: final String content = "<html><head><title>foo</title><script>\n"
071: + "function test() {\n"
072: + " alert(document.links != 'foo')\n"
073: + "}\n"
074: + "</script></head><body onload='test()'>\n"
075: + "<a href='bla.html'>link</a>\n" + "</body></html>";
076:
077: final String[] expectedAlerts = { "true" };
078: createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
079:
080: final List collectedAlerts = new ArrayList();
081: loadPage(content, collectedAlerts);
082:
083: assertEquals(expectedAlerts, collectedAlerts);
084: }
085:
086: /**
087: * Test that toString is accessible
088: * @throws Exception if the test fails
089: */
090: public void testToStringFunction() throws Exception {
091: if (notYetImplemented()) {
092: return;
093: }
094:
095: final String content = "<html><head><title>foo</title><script>\n"
096: + "function test() {\n"
097: + " alert(typeof document.links.toString)\n"
098: + "}\n"
099: + "</script></head><body onload='test()'>\n"
100: + "<a href='bla.html'>link</a>\n" + "</body></html>";
101:
102: final String[] expectedAlerts = { "function" };
103: createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
104:
105: final List collectedAlerts = new ArrayList();
106: loadPage(content, collectedAlerts);
107:
108: assertEquals(expectedAlerts, collectedAlerts);
109: }
110:
111: /**
112: * @throws Exception if the test fails
113: */
114: public void testGetElements() throws Exception {
115: final String firstContent = "<html><head><title>foo</title><script>\n"
116: + "function doTest() {\n"
117: + " alert(document.all.length);\n"
118: + " document.appendChild(document.createElement('div'));\n"
119: + " alert(document.all.length);\n"
120: + "}\n"
121: + "</script></head><body onload='doTest()'>\n"
122: + "</body></html>";
123:
124: final List collectedAlerts = new ArrayList();
125: loadPage(firstContent, collectedAlerts);
126:
127: final String[] expectedAlerts = { "5", "6" };
128: assertEquals(expectedAlerts, collectedAlerts);
129: }
130:
131: /**
132: * @throws Exception if the test fails
133: */
134: public void testChildNodes() throws Exception {
135: testChildNodes(BrowserVersion.INTERNET_EXPLORER_7_0,
136: new String[] { "true", "1" });
137: testChildNodes(BrowserVersion.FIREFOX_2, new String[] { "true",
138: "1" });
139: }
140:
141: private void testChildNodes(final BrowserVersion browserVersion,
142: final String[] expectedAlerts) throws Exception {
143: final String firstContent = "<html><head><title>foo</title><script>\n"
144: + " function test() {\n"
145: + " var doc = createXmlDocument();\n"
146: + " doc.async = false;\n"
147: + " alert(doc.load('"
148: + URL_SECOND
149: + "'));\n"
150: + " alert(doc.documentElement.childNodes.length);\n"
151: + " }\n"
152: + " function createXmlDocument() {\n"
153: + " if (document.implementation && document.implementation.createDocument)\n"
154: + " return document.implementation.createDocument('', '', null);\n"
155: + " else if (window.ActiveXObject)\n"
156: + " return new ActiveXObject('Microsoft.XMLDOM');\n"
157: + " }\n"
158: + "</script></head><body onload='test()'>\n"
159: + "</body></html>";
160:
161: final String secondContent = "<title>Immortality</title>";
162:
163: final List collectedAlerts = new ArrayList();
164: final WebClient client = new WebClient(browserVersion);
165: client.setAlertHandler(new CollectingAlertHandler(
166: collectedAlerts));
167: final MockWebConnection conn = new MockWebConnection(client);
168: conn.setResponse(URL_FIRST, firstContent);
169: conn.setResponse(URL_SECOND, secondContent, "text/xml");
170: client.setWebConnection(conn);
171:
172: client.getPage(URL_FIRST);
173: assertEquals(expectedAlerts, collectedAlerts);
174: }
175:
176: /**
177: * @throws Exception if the test fails
178: */
179: public void testFor_in() throws Exception {
180: final String[] expectedAlertsIE = { "string length",
181: "string myForm" };
182: testFor_in(BrowserVersion.INTERNET_EXPLORER_7_0,
183: expectedAlertsIE);
184: final String[] expectedAlertsFF = { "string 0",
185: "string length", "string item", "string namedItem" };
186: testFor_in(BrowserVersion.FIREFOX_2, expectedAlertsFF);
187: }
188:
189: private void testFor_in(final BrowserVersion browserVersion,
190: final String[] expectedAlerts) throws Exception {
191: final String html = "<html><head><title>foo</title><script>\n"
192: + " function test() {\n"
193: + " for (i in document.forms) {\n"
194: + " alert((typeof i) + ' ' + i);\n" + " }\n"
195: + " }\n" + "</script></head><body onload='test()'>\n"
196: + "<form name='myForm'></form>\n" + "</body></html>";
197:
198: final List collectedAlerts = new ArrayList();
199: loadPage(browserVersion, html, collectedAlerts);
200: assertEquals(expectedAlerts, collectedAlerts);
201: }
202:
203: /**
204: * <code>document.all.tags</code> is different from <code>document.forms.tags</code>!
205: * @throws Exception if the test fails
206: */
207: public void testTags() throws Exception {
208: if (notYetImplemented()) {
209: return;
210: }
211: testTags(BrowserVersion.INTERNET_EXPLORER_7_0, new String[] {
212: "true", "true" });
213: testTags(BrowserVersion.FIREFOX_2, new String[] { "true",
214: "false" });
215: }
216:
217: private void testTags(final BrowserVersion browserVersion,
218: final String[] expectedAlerts) throws Exception {
219: final String html = "<html><head><title>foo</title><script>\n"
220: + " function test() {\n"
221: + " alert(document.all.tags != undefined);\n"
222: + " alert(document.forms.tags != undefined);\n"
223: + " }\n" + "</script></head><body onload='test()'>\n"
224: + "<form name='myForm'></form>\n" + "</body></html>";
225:
226: final List collectedAlerts = new ArrayList();
227: loadPage(browserVersion, html, collectedAlerts);
228: assertEquals(expectedAlerts, collectedAlerts);
229: }
230: }
|