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: * Test for {@link IEConditionalCompilationScriptPreProcessor}.
051: *
052: * @version $Revision: 2132 $
053: * @author Ahmed Ashour
054: * @author Marc Guillemot
055: */
056: public class IEConditionalCompilationTest extends WebTestCase {
057:
058: /**
059: * Create an instance
060: *
061: * @param name The name of the test
062: */
063: public IEConditionalCompilationTest(final String name) {
064: super (name);
065: }
066:
067: /**
068: * @throws Exception If the test fails.
069: */
070: public void testSimple() throws Exception {
071: final String script = "/*@cc_on alert('testing @cc_on'); @*/";
072:
073: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
074: new String[] { "testing @cc_on" });
075: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
076: }
077:
078: /**
079: * @throws Exception If the test fails.
080: */
081: public void testIf() throws Exception {
082: final String script = "/*@cc_on@if(@_jscript_version>=5){alert(@_jscript_version)}@end@*/";
083:
084: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
085: new String[] { "5.6" });
086: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
087: }
088:
089: /**
090: * @throws Exception If the test fails.
091: */
092: public void testVariables_jscript_version() throws Exception {
093: final String script = "/*@cc_on alert(@_jscript_version) @*/";
094:
095: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
096: new String[] { "5.6" });
097: testScript(BrowserVersion.INTERNET_EXPLORER_7_0, script,
098: new String[] { "5.7" });
099: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
100: }
101:
102: /**
103: * @throws Exception If the test fails.
104: */
105: public void testVariables_jscript_build() throws Exception {
106: final String script = "/*@cc_on alert(@_jscript_build) @*/";
107:
108: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
109: new String[] { "6626" });
110: testScript(BrowserVersion.INTERNET_EXPLORER_7_0, script,
111: new String[] { "5730" });
112: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
113: }
114:
115: /**
116: * @throws Exception If the test fails.
117: */
118: public void testReservedString() throws Exception {
119: final String script = "/*@cc_on alert('testing /*@cc_on'); @*/";
120:
121: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
122: new String[] { "testing /*@cc_on" });
123: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
124: }
125:
126: /**
127: * @throws Exception If the test fails.
128: */
129: public void testSet() throws Exception {
130: final String script = "/*@cc_on @set @mine = 12 alert(@mine); @*/";
131:
132: testScript(BrowserVersion.INTERNET_EXPLORER_6_0, script,
133: new String[] { "12" });
134: testScript(BrowserVersion.FIREFOX_2, script, new String[] {});
135: }
136:
137: private void testScript(final BrowserVersion browserVersion,
138: final String script, final String[] expectedAlerts)
139: throws Exception {
140: final String htmlContent = "<html><head><title>foo</title></head>\n"
141: + "<script>\n"
142: + script
143: + "</script>\n"
144: + "<body>\n"
145: + "</body></html>";
146:
147: final List collectedAlerts = new ArrayList();
148: final WebClient client = new WebClient(browserVersion);
149: client.setAlertHandler(new CollectingAlertHandler(
150: collectedAlerts));
151: final MockWebConnection webConnection = new MockWebConnection(
152: client);
153: webConnection.setDefaultResponse(htmlContent);
154: client.setWebConnection(webConnection);
155: client.getPage(URL_FIRST);
156: assertEquals(expectedAlerts, collectedAlerts);
157: }
158:
159: }
|