Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Context#initStandardObjects()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Context#initStandardObjects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlUnitRegExpProxy2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if custom patch is still needed.
 */
@Test
public void needCustomFix() {
    final WebClient client = getWebClient();
    final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Context ctx = cf.enterContext();
    try {
        final ScriptableObject topScope = ctx.initStandardObjects();
        topScope.put("str", topScope, str_);
        topScope.put("text", topScope, text_);
        topScope.put("expected", topScope, expected_);
        assertEquals(begin_ + end_, text_.replaceAll(str_, ""));
        try {
            ctx.evaluateString(topScope, src_, "test script", 0, null);
        }
        catch (final JavaScriptException e) {
            assertTrue(e.getMessage().indexOf("Expected >") == 0);
        }
    }
    finally {
        Context.exit();
    }
}
 
Example 2
Source File: HtmlUnitRegExpProxy2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test if the custom fix is needed or not. When this test fails, then it means that the problem is solved in
 * Rhino and that custom fix for String.match in {@link HtmlUnitRegExpProxy} is not needed anymore (and that
 * this test can be removed, or turned positive).
 * @throws Exception if the test fails
 */
@Test
public void matchFixNeeded() throws Exception {
    final WebClient client = getWebClient();
    final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Context cx = cf.enterContext();
    try {
        final ScriptableObject topScope = cx.initStandardObjects();
        cx.evaluateString(topScope, scriptTestMatch_, "test script String.match", 0, null);
        try {
            cx.evaluateString(topScope, scriptTestMatch_, "test script String.match", 0, null);
        }
        catch (final JavaScriptException e) {
            assertTrue(e.getMessage().indexOf("Expected >") == 0);
        }
    }
    finally {
        Context.exit();
    }
}
 
Example 3
Source File: DedicatedWorkerGlobalScope.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 * @param browserVersion the simulated browser version
 * @param worker the started worker
 * @throws Exception in case of problem
 */
DedicatedWorkerGlobalScope(final Window owningWindow, final Context context, final BrowserVersion browserVersion,
        final Worker worker) throws Exception {
    context.initStandardObjects(this);

    final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(
            DedicatedWorkerGlobalScope.class, browserVersion);
    final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, null, browserVersion);
    setPrototype(prototype);

    owningWindow_ = owningWindow;
    final URL currentURL = owningWindow.getWebWindow().getEnclosedPage().getUrl();
    origin_ = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort();

    worker_ = worker;
}
 
Example 4
Source File: ProxyAutoConfig.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the <tt>FindProxyForURL</tt> method of the specified content.
 * @param content the JavaScript content
 * @param url the URL to be retrieved
 * @return semicolon-separated result
 */
public static String evaluate(final String content, final URL url) {
    final Context cx = Context.enter();
    try {
        final ProxyAutoConfig config = new ProxyAutoConfig();
        final Scriptable scope = cx.initStandardObjects();

        config.defineMethod("isPlainHostName", scope);
        config.defineMethod("dnsDomainIs", scope);
        config.defineMethod("localHostOrDomainIs", scope);
        config.defineMethod("isResolvable", scope);
        config.defineMethod("isInNet", scope);
        config.defineMethod("dnsResolve", scope);
        config.defineMethod("myIpAddress", scope);
        config.defineMethod("dnsDomainLevels", scope);
        config.defineMethod("shExpMatch", scope);
        config.defineMethod("weekdayRange", scope);
        config.defineMethod("dateRange", scope);
        config.defineMethod("timeRange", scope);

        cx.evaluateString(scope, "var ProxyConfig = function() {}; ProxyConfig.bindings = {}", "<init>", 1, null);
        cx.evaluateString(scope, content, "<Proxy Auto-Config>", 1, null);
        final Object[] functionArgs = {url.toExternalForm(), url.getHost()};
        final Object fObj = scope.get("FindProxyForURL", scope);

        final NativeFunction f = (NativeFunction) fObj;
        final Object result = f.call(cx, scope, scope, functionArgs);
        return Context.toString(result);
    }
    finally {
        Context.exit();
    }
}