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.Collections;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import com.gargoylesoftware.htmlunit.BrowserVersion;
047: import com.gargoylesoftware.htmlunit.WebClient;
048: import com.gargoylesoftware.htmlunit.WebTestCase;
049: import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
050: import com.gargoylesoftware.htmlunit.html.HtmlElement;
051: import com.gargoylesoftware.htmlunit.html.HtmlPage;
052: import com.gargoylesoftware.htmlunit.html.HtmlSpan;
053:
054: /**
055: * Tests for compatibility with the <a href="http://developer.yahoo.com/yui/">YUI JavaScript library</a>.
056: *
057: * TODO: add tests for IE6 and IE7
058: *
059: * @version $Revision: 2132 $
060: * @author Rob Di Marco
061: */
062: public class YuiTest extends WebTestCase {
063:
064: private static final long DEFAULT_TIME_TO_WAIT = 2 * 60 * 1000L;
065: private static final String BASE_FILE_PATH = "yui/2.3.0/tests/";
066:
067: /**
068: * Creates an instance.
069: *
070: * @param name The name of the test.
071: */
072: public YuiTest(final String name) {
073: super (name);
074: }
075:
076: /**
077: * @throws Exception if an error occurs
078: */
079: public void testLogger() throws Exception {
080: doTest(BrowserVersion.FIREFOX_2, "logger.html",
081: Collections.EMPTY_LIST);
082: }
083:
084: /**
085: * @throws Exception if an error occurs
086: */
087: public void testAnimation() throws Exception {
088: final List l = new ArrayList();
089: l.add("test_onStart");
090: doTest(BrowserVersion.FIREFOX_2, "animation.html", l);
091: }
092:
093: /**
094: * @throws Exception if an error occurs
095: */
096: public void testTabView() throws Exception {
097: if (notYetImplemented()) {
098: return;
099: }
100: // The tabview YUI test has a background thread that runs. We want to set the
101: // maximum wait time to 5 seconds as that gives enough time for execution without
102: // causing the test to run forever.
103: doTest(BrowserVersion.FIREFOX_2, "tabview.html",
104: Collections.EMPTY_LIST, null, 5 * 1000);
105: }
106:
107: /**
108: * @throws Exception if an error occurs
109: */
110: public void testDateMath() throws Exception {
111: doTest(BrowserVersion.FIREFOX_2, "datemath.html",
112: Collections.EMPTY_LIST, "btnRun");
113: }
114:
115: /**
116: * @throws Exception if an error occurs
117: */
118: public void testCalendar() throws Exception {
119: if (notYetImplemented()) {
120: return;
121: }
122: doTest(BrowserVersion.FIREFOX_2, "calendar.html",
123: Collections.EMPTY_LIST);
124: }
125:
126: /**
127: * @throws Exception if an error occurs
128: */
129: public void testColorPicker() throws Exception {
130: if (notYetImplemented()) {
131: return;
132: }
133: doTest(BrowserVersion.FIREFOX_2, "colorpicker.html",
134: Collections.EMPTY_LIST);
135: }
136:
137: /**
138: * @throws Exception if an error occurs
139: */
140: public void testConfig() throws Exception {
141: if (notYetImplemented()) {
142: return;
143: }
144: // Test currently commented out as there are problems with the YUI test.
145: // A bug has been filed against YUI regarding the problems with the test.
146: // See http://sourceforge.net/tracker/index.php?func=detail&aid=1788014&group_id=165715&atid=836476
147: // for more details.
148: fail("YUI test has a bug that causes this to fail.");
149: //doTest(BrowserVersion.FIREFOX_2, "config.html", Collections.EMPTY_LIST);
150: }
151:
152: /**
153: * @throws Exception if an error occurs
154: */
155: public void testDataSource() throws Exception {
156: if (notYetImplemented()) {
157: return;
158: }
159: doTest(BrowserVersion.FIREFOX_2, "datasource.html",
160: Collections.EMPTY_LIST);
161: }
162:
163: /**
164: * @throws Exception if an error occurs
165: */
166: public void testDataTable() throws Exception {
167: if (notYetImplemented()) {
168: return;
169: }
170: doTest(BrowserVersion.FIREFOX_2, "datatable.html",
171: Collections.EMPTY_LIST);
172: }
173:
174: /**
175: * @throws Exception if an error occurs
176: */
177: public void testDom() throws Exception {
178: if (notYetImplemented()) {
179: return;
180: }
181: doTest(BrowserVersion.FIREFOX_2, "dom.html",
182: Collections.EMPTY_LIST);
183: }
184:
185: /**
186: * @throws Exception if an error occurs
187: */
188: public void testDragDrop() throws Exception {
189: if (notYetImplemented()) {
190: return;
191: }
192: doTest(BrowserVersion.FIREFOX_2, "dragdrop.html",
193: Collections.EMPTY_LIST);
194: }
195:
196: /**
197: * @throws Exception if an error occurs
198: */
199: public void testEditor() throws Exception {
200: if (notYetImplemented()) {
201: return;
202: }
203: doTest(BrowserVersion.FIREFOX_2, "editor.html",
204: Collections.EMPTY_LIST);
205: }
206:
207: /**
208: * @throws Exception if an error occurs
209: */
210: public void testYuiLoaderRollup() throws Exception {
211: if (notYetImplemented()) {
212: return;
213: }
214: doTest(BrowserVersion.FIREFOX_2, "yuiloader_rollup.html",
215: Collections.EMPTY_LIST);
216: }
217:
218: /**
219: * @throws Exception if an error occurs
220: */
221: public void testYuiLoaderConfig() throws Exception {
222: if (notYetImplemented()) {
223: return;
224: }
225: doTest(BrowserVersion.FIREFOX_2, "yuiloader_config.html",
226: Collections.EMPTY_LIST);
227: }
228:
229: /**
230: * @throws Exception if an error occurs
231: */
232: public void testYuiLoader() throws Exception {
233: doTest(BrowserVersion.FIREFOX_2, "yuiloader.html", Collections
234: .singletonList("test_calculate"));
235: }
236:
237: /**
238: * @throws Exception if an error occurs
239: */
240: public void testModule() throws Exception {
241: doTest(BrowserVersion.FIREFOX_2, "module.html", Collections
242: .singletonList("testConstructor"));
243: }
244:
245: /**
246: * @throws Exception if an error occurs
247: */
248: public void testImageLoader() throws Exception {
249: if (notYetImplemented()) {
250: return;
251: }
252: doTest(BrowserVersion.FIREFOX_2, "imageloader.html",
253: Collections.EMPTY_LIST);
254: }
255:
256: /**
257: * @throws Exception if an error occurs
258: */
259: public void testElement() throws Exception {
260: doTest(BrowserVersion.FIREFOX_2, "element.html",
261: Collections.EMPTY_LIST);
262: }
263:
264: /**
265: * TODO: get rid of the known failing test list, eventually
266: */
267: private void doTest(final BrowserVersion version,
268: final String fileName, final List knownFailingTests)
269: throws Exception {
270: doTest(version, fileName, knownFailingTests, null);
271: }
272:
273: /**
274: * TODO: get rid of the known failing test list, eventually
275: */
276: private void doTest(final BrowserVersion version,
277: final String fileName, final List knownFailingTests,
278: final String buttonToPush) throws Exception {
279: doTest(version, fileName, knownFailingTests, buttonToPush,
280: DEFAULT_TIME_TO_WAIT);
281: }
282:
283: /**
284: * TODO: get rid of the known failing test list, eventually
285: */
286: private void doTest(final BrowserVersion version,
287: final String fileName, final List knownFailingTests,
288: final String buttonToPush, final long timeToWait)
289: throws Exception {
290:
291: final URL url = getClass().getClassLoader().getResource(
292: BASE_FILE_PATH + fileName);
293: assertNotNull(url);
294:
295: final WebClient client = new WebClient(version);
296: final HtmlPage page = (HtmlPage) client.getPage(url);
297: final HtmlElement doc = page.getDocumentHtmlElement();
298:
299: if (buttonToPush != null) {
300: final HtmlButtonInput button = ((HtmlButtonInput) page
301: .getHtmlElementById(buttonToPush));
302: button.click();
303: }
304:
305: page.getEnclosingWindow().getThreadManager()
306: .joinAll(timeToWait);
307:
308: final List tests = doc
309: .getByXPath("//span[@class='pass' or @class='fail']");
310: if (tests.size() == 0) {
311: fail("No tests were executed!");
312: }
313:
314: for (final Iterator i = tests.iterator(); i.hasNext();) {
315: final HtmlSpan span = (HtmlSpan) i.next();
316: final String testResult = span.getNextDomSibling().asText();
317: final int colonIdx = testResult.indexOf(":");
318: assertTrue(colonIdx > 0
319: && colonIdx < testResult.length() - 1);
320: final String result = span.asText();
321: final String testName = testResult.substring(0, colonIdx)
322: .trim();
323: if (result.equalsIgnoreCase("pass")) {
324: assertTrue(
325: "Test case '"
326: + testName
327: + "' is in the known failing list, but passes!",
328: !knownFailingTests.contains(testName));
329: } else {
330: assertTrue(
331: "Test case '"
332: + testName
333: + "' is not in the known failing list, but fails!",
334: knownFailingTests.contains(testName));
335: }
336: }
337: }
338:
339: }
|