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.host;
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: * Unit tests for {@link StyleSheetList}.
051: *
052: * @version $Revision: 2145 $
053: * @author Daniel Gredler
054: * @author Ahmed Ashour
055: */
056: public class StyleSheetListTest extends WebTestCase {
057:
058: /**
059: * Creates an instance.
060: * @param name the name of the test
061: */
062: public StyleSheetListTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * @throws Exception if an error occurs
068: */
069: public void testLength() throws Exception {
070: final String html = "<html>\n"
071: + " <head>\n"
072: + " <link href='style1.css'></link>\n"
073: + " <link href='style2.css' rel='stylesheet'></link>\n"
074: + " <link href='style3.css' type='text/css'></link>\n"
075: + " <link href='style4.css' rel='stylesheet' type='text/css'></link>\n"
076: + " <style>div.x { color: red; }</style>\n"
077: + " </head>\n"
078: + " <body onload='alert(document.styleSheets.length)'>\n"
079: + " <style>div.y { color: green; }</style>\n"
080: + " </body>\n" + "</html>";
081: final List actual = new ArrayList();
082: loadPage(html, actual);
083: final String[] expected = { "4" };
084: assertEquals(expected, actual);
085: }
086:
087: /**
088: * @throws Exception if an error occurs
089: */
090: public void testgetComputedStyle_Link() throws Exception {
091: final String html = "<html>\n"
092: + " <head>\n"
093: + " <link rel='stylesheet' type='text/css' href='"
094: + URL_SECOND
095: + "'/>\n"
096: + " <script>\n"
097: + " function test() {\n"
098: + " var div = document.getElementById('myDiv');\n"
099: + " alert(window.getComputedStyle(div, null).color);\n"
100: + " }\n" + " </script>\n" + " </head>\n"
101: + " <body onload='test()'>\n"
102: + " <div id='myDiv'/>\n" + " </body>\n" + "</html>";
103:
104: final String css = "div {color:red}";
105:
106: final String[] expectedAlerts = { "red" };
107: final List collectedAlerts = new ArrayList();
108: final WebClient webClient = new WebClient(
109: BrowserVersion.FIREFOX_2);
110: webClient.setAlertHandler(new CollectingAlertHandler(
111: collectedAlerts));
112: final MockWebConnection webConnection = new MockWebConnection(
113: webClient);
114: webConnection.setResponse(URL_FIRST, html);
115: webConnection.setResponse(URL_SECOND, css, "text/css");
116: webClient.setWebConnection(webConnection);
117:
118: webClient.getPage(URL_FIRST);
119: assertEquals(expectedAlerts, collectedAlerts);
120: }
121: }
|