Search in sources :

Example 1 with BrowserFunction

use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.

the class Bug510905_Browser_JsConsole method main.

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(500, 600);
    GridLayout gridLayout = new GridLayout();
    shell.setLayout(gridLayout);
    final Text jsConsole = new Text(shell, SWT.BORDER);
    // jsConsole.setText("document.body.innerHTML = theJavaFunction(123, 'hello', null, true)");
    // Case where there are no paramaters.
    jsConsole.setText("document.body.innerHTML = theJavaFunction()");
    jsConsole.setSelection(jsConsole.getText().length());
    GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    jsConsole.setLayoutData(data);
    final Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("hello <b>world!</b>");
    data = new GridData(SWT.FILL, SWT.FILL, true, true);
    browser.setLayoutData(data);
    jsConsole.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == 13) {
                // 13 = Enter
                browser.execute(jsConsole.getText());
            }
        }
    });
    Button loadNewPage = new Button(shell, SWT.PUSH);
    loadNewPage.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    loadNewPage.setText("Load new Page");
    loadNewPage.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            browser.setText("New page!" + count++);
        }
    });
    // BrowserFunction Code
    @SuppressWarnings("unused") final BrowserFunction function = new CustomFunction(browser, "theJavaFunction");
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : KeyAdapter(org.eclipse.swt.events.KeyAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) KeyEvent(org.eclipse.swt.events.KeyEvent) Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser)

Example 2 with BrowserFunction

use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_with_boolean.

/**
 * Test that javascript can call java and pass a Boolean to java.
 * loosely based on Snippet307.
 */
@Test
public void test_BrowserFunction_callback_with_boolean() {
    // On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
    // culprit seems to be the main_context_iteration() call in shell.setVisible().
    // See Bug 509587.  Solution: Webkit2.
    assumeFalse(webkit1SkipMsg(), isWebkit1);
    AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
    class JavascriptCallback extends // Note: Local class defined inside method.
    BrowserFunction {

        JavascriptCallback(Browser browser, String name) {
            super(browser, name);
        }

        @Override
        public Object function(Object[] arguments) {
            Boolean returnBool = (Boolean) arguments[0];
            javaCallbackExecuted.set(returnBool);
            return null;
        }
    }
    String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
    "function callCustomFunction() {\n" + "     document.body.style.backgroundColor = 'red'\n" + // This calls the javafunction that we registered.
    "		jsCallbackToJava(true)\n" + "}" + "</script>\n" + "</head>\n" + "<body> I'm going to make a callback to java </body>\n" + "</html>\n";
    browser.setText(htmlWithScript);
    new JavascriptCallback(browser, "jsCallbackToJava");
    browser.addProgressListener(callCustomFunctionUponLoad);
    shell.open();
    boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
    String message = "Javascript did not pass a boolean back to java";
    assertTrue(message, passed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 3 with BrowserFunction

use of org.eclipse.swt.browser.BrowserFunction in project pentaho-kettle by pentaho.

the class ConnectionDialog method open.

public void open(String title, String connectionName) {
    StringBuilder clientPath = new StringBuilder();
    clientPath.append(getClientPath());
    if (connectionName != null) {
        clientPath.append("#!/summary");
        clientPath.append("?connection=").append(connectionName);
    } else {
        clientPath.append("#!/intro");
    }
    super.createDialog(title, getRepoURL(clientPath.toString()), OPTIONS, LOGO);
    super.dialog.setMinimumSize(630, 630);
    new BrowserFunction(browser, "close") {

        @Override
        public Object function(Object[] arguments) {
            browser.dispose();
            dialog.close();
            dialog.dispose();
            return true;
        }
    };
    new BrowserFunction(browser, "setTitle") {

        @Override
        public Object function(Object[] arguments) {
            dialog.setText((String) arguments[0]);
            return true;
        }
    };
    new BrowserFunction(browser, "browse") {

        @Override
        public Object function(Object[] arguments) {
            FileDialog dialog = new FileDialog(getParent().getShell(), SWT.OPEN);
            return dialog.open();
        }
    };
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}
Also used : BrowserFunction(org.eclipse.swt.browser.BrowserFunction) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 4 with BrowserFunction

use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_afterPageReload.

/**
 * Test that a callback works even after a new page is loaded.
 * I.e, BrowserFunctions should have to be re-initialized after a page load.
 *
 * Logic:
 * - load a page.
 * - Register java callback.
 * - call java callback from javascript. (exec)
 *
 * - java callback instantiates new page load.
 * - new page load triggers 'completed' listener
 * - completed listener calls the registered function again.
 *
 * - once regiseterd function is called a 2nd time, it sets the test to pass.
 */
@Test
public void test_BrowserFunction_callback_afterPageReload() {
    // On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
    // culprit seems to be the main_context_iteration() call in shell.setVisible().
    // See Bug 509587.  Solution: Webkit2.
    assumeFalse(webkit1SkipMsg(), isWebkit1);
    AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
    AtomicInteger callCount = new AtomicInteger(0);
    class JavascriptCallback extends // Note: Local class defined inside method.
    BrowserFunction {

        JavascriptCallback(Browser browser, String name) {
            super(browser, name);
        }

        @Override
        public Object function(Object[] arguments) {
            if (callCount.get() == 0) {
                callCount.set(1);
                browser.setText("2nd page load");
            } else {
                javaCallbackExecuted.set(true);
            }
            return null;
        }
    }
    browser.setText("1st (initial) page load");
    new JavascriptCallback(browser, "jsCallbackToJava");
    browser.execute("jsCallbackToJava()");
    // see if function still works after a page change:
    browser.addProgressListener(completedAdapter(e -> browser.execute("jsCallbackToJava()")));
    shell.open();
    boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
    String message = "A javascript callback should work after a page has been reloaded. But something went wrong";
    assertTrue(message, passed);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) URL(java.net.URL) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) TitleListener(org.eclipse.swt.browser.TitleListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressListener.completedAdapter(org.eclipse.swt.browser.ProgressListener.completedAdapter) Assert.fail(org.junit.Assert.fail) SWTException(org.eclipse.swt.SWTException) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) FillLayout(org.eclipse.swt.layout.FillLayout) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Browser(org.eclipse.swt.browser.Browser) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 5 with BrowserFunction

use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback.

/**
 * Test that javascript can call java.
 * loosely based on Snippet307.
 */
@Test
public void test_BrowserFunction_callback() {
    // On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
    // culprit seems to be the main_context_iteration() call in shell.setVisible().
    // See Bug 509587.  Solution: Webkit2.
    assumeFalse(webkit1SkipMsg(), isWebkit1);
    AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
    class JavascriptCallback extends // Note: Local class defined inside method.
    BrowserFunction {

        JavascriptCallback(Browser browser, String name) {
            super(browser, name);
        }

        @Override
        public Object function(Object[] arguments) {
            javaCallbackExecuted.set(true);
            return null;
        }
    }
    String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
    "function callCustomFunction() {\n" + "     document.body.style.backgroundColor = 'red'\n" + // This calls the javafunction that we registered.
    "		jsCallbackToJava()\n" + "}" + "</script>\n" + "</head>\n" + "<body> I'm going to make a callback to java </body>\n" + "</html>\n";
    browser.setText(htmlWithScript);
    new JavascriptCallback(browser, "jsCallbackToJava");
    browser.addProgressListener(callCustomFunctionUponLoad);
    shell.open();
    boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
    String message = "Java failed to get a callback from javascript. Test timed out";
    assertTrue(message, passed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Aggregations

BrowserFunction (org.eclipse.swt.browser.BrowserFunction)10 Browser (org.eclipse.swt.browser.Browser)7 Test (org.junit.Test)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Shell (org.eclipse.swt.widgets.Shell)3 IOException (java.io.IOException)2 OpenWindowListener (org.eclipse.swt.browser.OpenWindowListener)2 WindowEvent (org.eclipse.swt.browser.WindowEvent)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Instant (java.time.Instant)1 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1