Search in sources :

Example 1 with JSRef

use of com.codename1.ui.BrowserComponent.JSRef in project CodenameOne by codenameone.

the class BrowserComponent method fireBrowserNavigationCallbacks.

/**
 * Fires all of the registered browser navigation callbacks against the provided URL.
 * @param url The URL to fire the navigation callbacks against.
 * @return True if all of the callbacks say that they can browse.  False otherwise.
 */
public boolean fireBrowserNavigationCallbacks(String url) {
    boolean shouldNavigate = true;
    if (browserNavigationCallback != null && !browserNavigationCallback.shouldNavigate(url)) {
        shouldNavigate = false;
    }
    if (browserNavigationCallbacks != null) {
        for (BrowserNavigationCallback cb : browserNavigationCallbacks) {
            if (!cb.shouldNavigate(url)) {
                shouldNavigate = false;
            }
        }
    }
    if (!url.startsWith("javascript:") && url.indexOf(RETURN_URL_PREFIX) != -1) {
        // System.out.println("Received browser navigation callback "+url);
        String result = decodeURL(url.substring(url.indexOf(RETURN_URL_PREFIX) + RETURN_URL_PREFIX.length()), "UTF-8");
        // System.out.println("After decode "+result);
        Result structResult = Result.fromContent(result, Result.JSON);
        int callbackId = structResult.getAsInteger("callbackId");
        final String value = structResult.getAsString("value");
        final String type = structResult.getAsString("type");
        final String errorMessage = structResult.getAsString("errorMessage");
        final SuccessCallback<JSRef> callback = popReturnValueCallback(callbackId);
        if (jsCallbacks != null && jsCallbacks.contains(callback)) {
            // If this is a registered callback, then we treat it more like
            // an event listener, and we retain it for future callbacks.
            returnValueCallbacks.put(callbackId, callback);
        }
        if (callback != null) {
            if (errorMessage != null) {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        if (callback instanceof Callback) {
                            ((Callback) callback).onError(this, new RuntimeException(errorMessage), 0, errorMessage);
                        }
                    }
                });
            } else {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        callback.onSucess(new JSRef(value, type));
                    }
                });
            }
        } else {
            Log.e(new RuntimeException("Received return value from javascript, but no callback could be found for that ID"));
        }
        shouldNavigate = false;
    }
    return shouldNavigate;
}
Also used : BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback) Callback(com.codename1.util.Callback) SuccessCallback(com.codename1.util.SuccessCallback) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback) Result(com.codename1.processing.Result)

Example 2 with JSRef

use of com.codename1.ui.BrowserComponent.JSRef in project CodenameOne by codenameone.

the class JavascriptTests method runTest.

@Override
public boolean runTest() throws Exception {
    Form f = new Form("Test Browser");
    f.setLayout(new BorderLayout());
    BrowserComponent bc = new BrowserComponent();
    bc.setPage("<!doctype html><html><head><title>Foo</title><body>Body</body></head></html>", "http://www.codenameone.com");
    final Res res = new Res();
    bc.addWebEventListener(BrowserComponent.onLoad, e -> {
        try {
            int timeout = 5000;
            bc.execute("window.person={name:'Steve', somefunc: function(a){this.someval = a}}");
            JSRef tmp = bc.executeAndWait(timeout, "callback.onSuccess(person.name)");
            TestUtils.assertEqual(JSType.STRING, tmp.getJSType(), "Wrong JSType for person.name");
            TestUtils.assertEqual("Steve", tmp.toString());
            bc.execute("person.age=${0}", new Object[] { 24 });
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(person.age)");
            TestUtils.assertEqual(JSType.NUMBER, tmp.getJSType(), "Wrong JSType for age");
            TestUtils.assertEqual(24, tmp.getInt(), "Age should be 24");
            bc.execute("person.enabled=${0}", new Object[] { true });
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(person.enabled)");
            TestUtils.assertEqual(JSType.BOOLEAN, tmp.getJSType(), "Wrong JSType for enabled");
            TestUtils.assertEqual(true, tmp.getBoolean(), "Wrong value for enabled");
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(person)");
            TestUtils.assertEqual(JSType.OBJECT, tmp.getJSType(), "Wrong type for person");
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(person.somefunc)");
            TestUtils.assertEqual(JSType.FUNCTION, tmp.getJSType(), "Wrong type for somefunc");
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(person.firstName)");
            TestUtils.assertEqual(JSType.UNDEFINED, tmp.getJSType(), "firstName should be undefined");
            bc.execute("window.person2={name:'Marlene'}");
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(48)");
            bc.execute("window.person2.age=${0}", new Object[] { tmp });
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(window.person2.age)");
            TestUtils.assertEqual(JSType.NUMBER, tmp.getJSType(), "Wrong type for age after setting with JSRef");
            TestUtils.assertEqual(48, tmp.getInt(), "Wrong value for age after setting with JSRef");
            tmp = bc.executeAndWait(timeout, "callback.onSuccess(${0} + ${1} + ${2} + ${3})", new Object[] { 1, 2, 3, 4 });
            TestUtils.assertEqual(JSType.NUMBER, tmp.getJSType(), "Wrong type for 1+2+3+4");
            TestUtils.assertEqual(10, tmp.getInt(), "Wrong value for 1+2+3+4");
            JSRef name1 = bc.executeAndWait(timeout, "callback.onSuccess(person.name)");
            JSRef name2 = bc.executeAndWait(timeout, "callback.onSuccess(person2.name)");
            JSRef name1name2 = bc.executeAndWait(timeout, "callback.onSuccess(${0} +' '+${1})", new Object[] { name1, name2 });
            TestUtils.assertEqual(JSType.STRING, name1name2.getJSType(), "Wrong type for name1name2");
            TestUtils.assertEqual("Steve Marlene", name1name2.toString(), "Wrong value for name1name2");
            // Steve
            JSProxy proxy = bc.createJSProxy("person.name");
            tmp = proxy.callAndWait(timeout, "indexOf", new Object[] { "e" });
            TestUtils.assertEqual(2, tmp.getInt(), "Wrong position for 'e'");
            tmp = proxy.getAndWait(timeout, "length");
            TestUtils.assertEqual(5, tmp.getInt(), "Wrong string length in proxy");
            synchronized (res) {
                res.complete = true;
                res.notifyAll();
            }
            res.complete = true;
        } catch (Throwable t) {
            synchronized (res) {
                res.complete = true;
                res.error = t;
                res.notifyAll();
            }
        }
    });
    f.add(BorderLayout.CENTER, bc);
    f.show();
    while (!res.complete) {
        Display.getInstance().invokeAndBlock(() -> {
            synchronized (res) {
                Util.wait(res, 1000);
            }
        });
    }
    if (res.error != null) {
        Log.e(res.error);
        throw new Exception(res.error.getMessage());
    }
    return true;
}
Also used : JSRef(com.codename1.ui.BrowserComponent.JSRef) BorderLayout(com.codename1.ui.layouts.BorderLayout) JSProxy(com.codename1.ui.BrowserComponent.JSProxy)

Aggregations

Result (com.codename1.processing.Result)1 JSProxy (com.codename1.ui.BrowserComponent.JSProxy)1 JSRef (com.codename1.ui.BrowserComponent.JSRef)1 BrowserNavigationCallback (com.codename1.ui.events.BrowserNavigationCallback)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 Callback (com.codename1.util.Callback)1 SuccessCallback (com.codename1.util.SuccessCallback)1