/* * Copyright (c) 2002-2008 Gargoyle Software Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gargoylesoftware.htmlunit.javascript; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.httpclient.NameValuePair; import org.junit.Assert; import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Script; import org.mozilla.javascript.Scriptable; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.CollectingAlertHandler; import com.gargoylesoftware.htmlunit.HttpMethod; import com.gargoylesoftware.htmlunit.MockWebConnection; import com.gargoylesoftware.htmlunit.ScriptException; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebRequestSettings; import com.gargoylesoftware.htmlunit.WebTestCase; import com.gargoylesoftware.htmlunit.html.ClickableElement; import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlButtonInput; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlFrame; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlScript; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; /** * Tests for the {@link JavaScriptEngine}. * * @version $Revision$ * @author Mike Bowler * @author Noboru Sinohara * @author Darrell DeBoer * @author Ben Curren * @author Marc Guillemot * @author Chris Erskine * @author David K. Taylor * @author Ahmed Ashour */ public class JavaScriptEngineTest extends WebTestCase { /** * @throws Exception if the test fails */ @Test public void setJavascriptEnabled_false() throws Exception { final WebClient client = new WebClient(); client.setJavaScriptEnabled(false); final MockWebConnection webConnection = new MockWebConnection(client); final String content = "foo\n" + "

hello world

\n" + "
\n" + " \n" + " \n" + "
\n" + ""; webConnection.setDefaultResponse(content); client.setWebConnection(webConnection); final HtmlPage page = (HtmlPage) client.getPage(URL_GARGOYLE); final HtmlTextInput textInput = (HtmlTextInput) page.getHtmlElementById("textfield1"); assertEquals("foo", textInput.getValueAttribute()); } /** * Regression test for bug https://sf.net/tracker/?func=detail&atid=448266&aid=1609944&group_id=47038. * @throws Exception if the test fails */ @Test public void onloadJavascriptFunction() throws Exception { final String content = "foo\n" + ""; final String[] expectedAlerts = {"foo"}; createTestPageForRealBrowserIfNeeded(content, expectedAlerts); final List collectedAlerts = new ArrayList(); loadPage(BrowserVersion.FIREFOX_2, content, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); } /** * Tries to set the value of a text input field. * @throws Exception if the test fails */ @Test public void setInputValue() throws Exception { final String content = "foo\n" + "

hello world

\n" + "
\n" + " \n" + " \n" + "
\n" + ""; final List collectedAlerts = null; final HtmlPage page = loadPage(content, collectedAlerts); final HtmlTextInput textInput = (HtmlTextInput) page.getHtmlElementById("textfield1"); assertEquals("blue", textInput.getValueAttribute()); } /** * @throws Exception if the test fails */ @Test public void alert() throws Exception { final String content = "foo\n" + "

hello world

\n" + "
\n" + " \n" + " \n" + "
\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(content, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"foo"}; assertEquals(expectedAlerts, collectedAlerts); } /** * Checks that a dynamically compiled function works in the scope of its birth. * @throws Exception if the test fails */ @Test public void scopeOfNewFunction() throws Exception { final String content = "\n" + ""; final String[] expectedAlerts = {"foo"}; createTestPageForRealBrowserIfNeeded(content, expectedAlerts); final List collectedAlerts = new ArrayList(); loadPage(content, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); } /** * @throws Exception if the test fails */ @Test public void scopeOfNestedNewFunction() throws Exception { final String[] expectedAlerts = {"foo"}; final String content = "\n" + "\n" + "\n" + "\n" + ""; createTestPageForRealBrowserIfNeeded(content, expectedAlerts); final List collectedAlerts = new ArrayList(); loadPage(content, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); } /** * Checks that a dynamically compiled function works in the scope of its birth and not the other window. * @throws Exception if the test fails */ @Test public void scopeOfNewFunctionCalledFormOtherWindow() throws Exception { final String firstContent = "\n" + "\n" + "\n" + "\n" + "