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.libraries;
039:
040: import java.net.URL;
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import com.gargoylesoftware.htmlunit.BrowserVersion;
046: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
047: import com.gargoylesoftware.htmlunit.WebClient;
048: import com.gargoylesoftware.htmlunit.WebTestCase;
049: import com.gargoylesoftware.htmlunit.html.HtmlElement;
050: import com.gargoylesoftware.htmlunit.html.HtmlListItem;
051: import com.gargoylesoftware.htmlunit.html.HtmlOrderedList;
052: import com.gargoylesoftware.htmlunit.html.HtmlPage;
053:
054: /**
055: * Abstract base class for <a href="http://jquery.com/">jQuery JavaScript library</a> compatibility tests.
056: *
057: * @version $Revision: 2132 $
058: * @author Daniel Gredler
059: * @author Ahmed Ashour
060: */
061: public abstract class JQueryTestBase extends WebTestCase {
062:
063: /**
064: * Creates an instance.
065: *
066: * @param name The name of the test.
067: */
068: public JQueryTestBase(final String name) {
069: super (name);
070: }
071:
072: /**
073: * @throws Exception If an error occurs.
074: */
075: public void testJQueryWithIE6() throws Exception {
076: final Iterator i = loadPage(BrowserVersion.INTERNET_EXPLORER_6_0);
077: verify(i, true);
078: }
079:
080: /**
081: * @throws Exception If an error occurs.
082: */
083: public void testJQueryWithIE7() throws Exception {
084: final Iterator i = loadPage(BrowserVersion.INTERNET_EXPLORER_7_0);
085: verify(i, true);
086: }
087:
088: /**
089: * @throws Exception If an error occurs.
090: */
091: public void testJQueryWithFirefox2() throws Exception {
092: final Iterator i = loadPage(BrowserVersion.FIREFOX_2);
093: verify(i, false);
094: }
095:
096: /**
097: * Returns the jQuery version being tested.
098: *
099: * @return the jQuery version being tested
100: */
101: protected abstract String getVersion();
102:
103: /**
104: * Verifies that the specified test result iterator contains the expected results.
105: *
106: * @param i the test result iterator
107: * @param ie whether or not the browser used was MSIE
108: * @throws Exception If an error occurs.
109: */
110: protected abstract void verify(final Iterator i, final boolean ie)
111: throws Exception;
112:
113: /**
114: * Loads the jQuery unit test index page using the specified browser version, allows its
115: * JavaScript to run to completion, and returns a list item iterator containing the test
116: * results.
117: *
118: * @param version the browser version to use
119: * @return a list item iterator containing the test results
120: * @throws Exception if an error occurs
121: */
122: protected Iterator loadPage(final BrowserVersion version)
123: throws Exception {
124: final String resource = "jquery/" + getVersion()
125: + "/test/index.html";
126: final URL url = getClass().getClassLoader().getResource(
127: resource);
128: assertNotNull(url);
129:
130: final WebClient client = new WebClient(version);
131: final List alerts = new ArrayList();
132: client.setAlertHandler(new CollectingAlertHandler(alerts));
133:
134: final HtmlPage page = (HtmlPage) client.getPage(url);
135: page.getEnclosingWindow().getThreadManager().joinAll(
136: 2 * 60 * 1000);
137:
138: final HtmlElement doc = page.getDocumentHtmlElement();
139: final HtmlOrderedList tests = (HtmlOrderedList) doc
140: .getHtmlElementById("tests");
141: final Iterator i = tests.getChildElementsIterator();
142:
143: return i;
144: }
145:
146: /**
147: * Verifies that the next test group result list item has the specified name, the specified
148: * number of failed tests and the specified number of passed tests.
149: *
150: * @param i the iterator whose next element is the list item to be checked
151: * @param name the expected test group name
152: * @param failed the expected number of failed unit tests
153: * @param passed the expected number of passed unit tests
154: * @throws Exception if an error occurs
155: */
156: protected void ok(final Iterator i, final String name,
157: final int failed, final int passed) throws Exception {
158: final HtmlListItem li = (HtmlListItem) i.next();
159: final String n = li.getFirstByXPath("//text()").toString()
160: .trim();
161: assertEquals(name, n);
162:
163: final int f = Integer.parseInt(li.getFirstByXPath(
164: "//b[@class='fail']/text()").toString());
165: final int p = Integer.parseInt(li.getFirstByXPath(
166: "//b[@class='pass']/text()").toString());
167: if (f != failed || p != passed) {
168: fail("Expected " + passed + " passed and " + failed
169: + " failed for test: " + li.asText());
170: }
171: }
172:
173: }
|