KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > StyleTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44
45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
46 import com.gargoylesoftware.htmlunit.MockWebConnection;
47 import com.gargoylesoftware.htmlunit.WebClient;
48 import com.gargoylesoftware.htmlunit.WebTestCase;
49 import com.gargoylesoftware.htmlunit.html.HtmlPage;
50
51 /**
52  * Tests for Style.
53  *
54  * @version $Revision: 1.11 $
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  */

57 public class StyleTest extends WebTestCase {
58
59     /**
60      * Create an instance
61      * @param name The name of the test
62      */

63     public StyleTest( final String name ) {
64         super(name);
65     }
66
67
68     /**
69      * @throws Exception if the test fails
70      */

71     public void testStyle_OneCssAttribute() throws Exception {
72         final WebClient client = new WebClient();
73         final MockWebConnection webConnection = new MockWebConnection( client );
74
75         final String firstContent
76             = "<html><head><title>First</title><script>\n"
77             + "function doTest() {\n"
78             + " var style = document.getElementById('div1').style;\n"
79             + " alert(style.color);\n"
80             + " style.color = 'pink';\n"
81             + " alert(style.color);\n"
82             + "}\n</script></head>"
83             + "<body onload='doTest()'><div id='div1' style='color: black'>foo</div></body></html>";
84
85         webConnection.setResponse(
86             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
87         client.setWebConnection( webConnection );
88
89         final List collectedAlerts = new ArrayList();
90         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
91
92         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
93
94         final List expectedAlerts = Arrays.asList( new String[]{"black", "pink"} );
95         assertEquals( expectedAlerts, collectedAlerts );
96
97         assertEquals("color: pink; ", page.getHtmlElementById("div1").getAttributeValue("style") );
98     }
99
100
101     /**
102      * @throws Exception if the test fails
103      */

104     public void testStyle_MultipleCssAttributes() throws Exception {
105         final WebClient client = new WebClient();
106         final MockWebConnection webConnection = new MockWebConnection( client );
107
108         final String firstContent
109             = "<html><head><title>First</title><script>\n"
110             + "function doTest() {\n"
111             + " var style = document.getElementById('div1').style;\n"
112             + " alert(style.color);\n"
113             + " style.color = 'pink';\n"
114             + " alert(style.color);\n"
115             + "}\n</script></head>"
116             + "<body onload='doTest()'>"
117             + "<div id='div1' style='color: black;background:blue;foo:bar'>foo</div></body></html>";
118
119         webConnection.setResponse(
120             URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
121         client.setWebConnection( webConnection );
122
123         final List collectedAlerts = new ArrayList();
124         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
125
126         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
127
128         final List expectedAlerts = Arrays.asList( new String[]{"black", "pink"} );
129         assertEquals( expectedAlerts, collectedAlerts );
130
131         assertEquals(
132             "background: blue; color: pink; foo: bar; ",
133             page.getHtmlElementById("div1").getAttributeValue("style") );
134     }
135
136     /**
137      * @throws Exception if the test fails
138      */

139     public void testStyle_OneUndefinedCssAttribute() throws Exception {
140         final WebClient client = new WebClient();
141         final MockWebConnection webConnection = new MockWebConnection( client );
142
143         final String firstContent
144             = "<html><head><title>First</title><script>\n"
145             + "function doTest() {\n"
146             + " var style = document.getElementById('div1').style;\n"
147             + " alert(document.getElementById('nonexistingid'));\n"
148             + " alert(style.color);\n"
149             + " style.color = 'pink';\n"
150             + " alert(style.color);\n"
151             + "}\n</script></head>"
152             + "<body onload='doTest()'><div id='div1'>foo</div></body></html>";
153
154         webConnection.setResponse(
155                 URL_FIRST, firstContent, 200, "OK", "text/html", Collections.EMPTY_LIST );
156         client.setWebConnection( webConnection );
157
158         final List collectedAlerts = new ArrayList();
159         client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) );
160
161         final HtmlPage page = (HtmlPage)client.getPage(URL_FIRST);
162
163         final List expectedAlerts = Arrays.asList( new String[]{"null", "", "pink"} );
164         assertEquals( expectedAlerts, collectedAlerts );
165
166         assertEquals("color: pink; ", page.getHtmlElementById("div1").getAttributeValue("style") );
167     }
168 }
169
Popular Tags