Running some @JavaScriptBody tests directly on FX Web View
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 11 Jul 2013 11:23:54 +0200
changeset 18299c8145d1e4f
parent 181 c09d00808cab
child 183 19ee92613f00
Running some @JavaScriptBody tests directly on FX Web View
boot-fx/pom.xml
boot-fx/src/test/java/org/apidesign/html/boot/fx/BootstrapTest.java
boot-fx/src/test/java/org/apidesign/html/boot/fx/FXPresenterTst.java
boot-fx/src/test/java/org/apidesign/html/boot/fx/KOFx.java
boot-fx/src/test/resources/org/apidesign/html/boot/fx/empty.html
     1.1 --- a/boot-fx/pom.xml	Tue Jul 09 09:09:13 2013 +0200
     1.2 +++ b/boot-fx/pom.xml	Thu Jul 11 11:23:54 2013 +0200
     1.3 @@ -35,5 +35,11 @@
     1.4        <version>${project.version}</version>
     1.5        <type>jar</type>
     1.6      </dependency>
     1.7 +    <dependency>
     1.8 +      <groupId>org.testng</groupId>
     1.9 +      <artifactId>testng</artifactId>
    1.10 +      <scope>test</scope>
    1.11 +      <type>jar</type>
    1.12 +    </dependency>
    1.13    </dependencies>
    1.14  </project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-fx/src/test/java/org/apidesign/html/boot/fx/BootstrapTest.java	Thu Jul 11 11:23:54 2013 +0200
     2.3 @@ -0,0 +1,84 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +package org.apidesign.html.boot.fx;
    2.25 +
    2.26 +import java.lang.annotation.Annotation;
    2.27 +import java.lang.reflect.Method;
    2.28 +import java.util.ArrayList;
    2.29 +import java.util.List;
    2.30 +import java.util.concurrent.Executors;
    2.31 +import net.java.html.boot.BrowserBuilder;
    2.32 +import static org.testng.Assert.*;
    2.33 +import org.testng.annotations.Factory;
    2.34 +import org.testng.annotations.Test;
    2.35 +
    2.36 +/**
    2.37 + *
    2.38 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.39 + */
    2.40 +public class BootstrapTest {
    2.41 +    private static Class<?> browserClass;
    2.42 +    
    2.43 +    public BootstrapTest() {
    2.44 +    }
    2.45 +
    2.46 +    @Factory public static Object[] compatibilityTests() throws Exception {
    2.47 +        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(BootstrapTest.class).
    2.48 +            loadPage("empty.html").
    2.49 +            invoke("initialized");
    2.50 +
    2.51 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
    2.52 +            @Override
    2.53 +            public void run() {
    2.54 +                bb.showAndWait();
    2.55 +            }
    2.56 +        });
    2.57 +
    2.58 +        List<Object> res = new ArrayList<Object>();
    2.59 +        Class<? extends Annotation> test = 
    2.60 +            loadClass().getClassLoader().loadClass(Test.class.getName()).
    2.61 +            asSubclass(Annotation.class);
    2.62 +        for (Method m : loadClass().getMethods()) {
    2.63 +            if (m.getAnnotation(test) != null) {
    2.64 +                res.add(new KOFx(m));
    2.65 +            }
    2.66 +        }
    2.67 +        return res.toArray();
    2.68 +    }
    2.69 +
    2.70 +    static synchronized Class<?> loadClass() throws InterruptedException {
    2.71 +        while (browserClass == null) {
    2.72 +            BootstrapTest.class.wait();
    2.73 +        }
    2.74 +        return browserClass;
    2.75 +    }
    2.76 +    
    2.77 +    public static synchronized void ready(Class<?> browserCls) throws Exception {
    2.78 +        browserClass = browserCls;
    2.79 +        BootstrapTest.class.notifyAll();
    2.80 +    }
    2.81 +    
    2.82 +    public static void initialized() throws Exception {
    2.83 +        Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(BootstrapTest.class.getName());
    2.84 +        Method m = classpathClass.getMethod("ready", Class.class);
    2.85 +        m.invoke(null, FXPresenterTst.class);
    2.86 +    }
    2.87 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/boot-fx/src/test/java/org/apidesign/html/boot/fx/FXPresenterTst.java	Thu Jul 11 11:23:54 2013 +0200
     3.3 @@ -0,0 +1,49 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package org.apidesign.html.boot.fx;
    3.25 +
    3.26 +import net.java.html.js.JavaScriptBody;
    3.27 +import static org.testng.Assert.*;
    3.28 +import org.testng.annotations.Test;
    3.29 +
    3.30 +/**
    3.31 + *
    3.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.33 + */
    3.34 +public class FXPresenterTst {
    3.35 +    @Test public void showClassLoader() {
    3.36 +        R run = new R();
    3.37 +        callback(run);
    3.38 +        assertEquals(run.cnt, 1, "Can call even private implementation classes");
    3.39 +    }
    3.40 +    
    3.41 +    @JavaScriptBody(args = { "r" }, javacall = true, body = "r.@java.lang.Runnable::run()();")
    3.42 +    private static native void callback(Runnable r);
    3.43 +
    3.44 +    public static class R implements Runnable {
    3.45 +        int cnt;
    3.46 +
    3.47 +        @Override
    3.48 +        public void run() {
    3.49 +            cnt++;
    3.50 +        }
    3.51 +    }
    3.52 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/boot-fx/src/test/java/org/apidesign/html/boot/fx/KOFx.java	Thu Jul 11 11:23:54 2013 +0200
     4.3 @@ -0,0 +1,97 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.boot.fx;
    4.25 +
    4.26 +import java.lang.reflect.InvocationTargetException;
    4.27 +import java.lang.reflect.Method;
    4.28 +import javafx.application.Platform;
    4.29 +import org.testng.IHookCallBack;
    4.30 +import org.testng.IHookable;
    4.31 +import org.testng.ITest;
    4.32 +import org.testng.ITestResult;
    4.33 +import org.testng.annotations.Test;
    4.34 +
    4.35 +/**
    4.36 + *
    4.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.38 + */
    4.39 +public final class KOFx implements ITest, IHookable, Runnable {
    4.40 +    private final Method m;
    4.41 +    private Object result;
    4.42 +    private Object inst;
    4.43 +
    4.44 +    KOFx(Method m) {
    4.45 +        this.m = m;
    4.46 +    }
    4.47 +
    4.48 +    @Override
    4.49 +    public String getTestName() {
    4.50 +        return m.getName();
    4.51 +    }
    4.52 +
    4.53 +    @Test
    4.54 +    public synchronized void executeTest() throws Exception {
    4.55 +        if (result == null) {
    4.56 +            Platform.runLater(this);
    4.57 +            wait();
    4.58 +        }
    4.59 +        if (result instanceof Exception) {
    4.60 +            throw (Exception)result;
    4.61 +        }
    4.62 +        if (result instanceof Error) {
    4.63 +            throw (Error)result;
    4.64 +        }
    4.65 +    }
    4.66 +
    4.67 +    @Override
    4.68 +    public synchronized void run() {
    4.69 +        boolean notify = true;
    4.70 +        try {
    4.71 +            if (inst == null) {
    4.72 +                inst = m.getDeclaringClass().newInstance();
    4.73 +            }
    4.74 +            result = m.invoke(inst);
    4.75 +            if (result == null) {
    4.76 +                result = this;
    4.77 +            }
    4.78 +        } catch (InvocationTargetException ex) {
    4.79 +            Throwable r = ex.getTargetException();
    4.80 +            if (r instanceof InterruptedException) {
    4.81 +                notify = false;
    4.82 +                Platform.runLater(this);
    4.83 +                return;
    4.84 +            }
    4.85 +            result = r;
    4.86 +        } catch (Exception ex) {
    4.87 +            result = ex;
    4.88 +        } finally {
    4.89 +            if (notify) {
    4.90 +                notifyAll();
    4.91 +            }
    4.92 +        }
    4.93 +    }
    4.94 +
    4.95 +    @Override
    4.96 +    public void run(IHookCallBack ihcb, ITestResult itr) {
    4.97 +        ihcb.runTestMethod(itr);
    4.98 +    }
    4.99 +    
   4.100 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/boot-fx/src/test/resources/org/apidesign/html/boot/fx/empty.html	Thu Jul 11 11:23:54 2013 +0200
     5.3 @@ -0,0 +1,33 @@
     5.4 +<!--
     5.5 +
     5.6 +    HTML via Java(tm) Language Bindings
     5.7 +    Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.8 +
     5.9 +    This program is free software: you can redistribute it and/or modify
    5.10 +    it under the terms of the GNU General Public License as published by
    5.11 +    the Free Software Foundation, version 2 of the License.
    5.12 +
    5.13 +    This program is distributed in the hope that it will be useful,
    5.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.16 +    GNU General Public License for more details. apidesign.org
    5.17 +    designates this particular file as subject to the
    5.18 +    "Classpath" exception as provided by apidesign.org
    5.19 +    in the License file that accompanied this code.
    5.20 +
    5.21 +    You should have received a copy of the GNU General Public License
    5.22 +    along with this program. Look for COPYING file in the top folder.
    5.23 +    If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    5.24 +
    5.25 +-->
    5.26 +<!DOCTYPE html>
    5.27 +<html>
    5.28 +    <head>
    5.29 +        <title>FX Presenter Harness</title>
    5.30 +        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5.31 +        <meta name="viewport" content="width=device-width">
    5.32 +    </head>
    5.33 +    <body>
    5.34 +        <div>FX Presenter Harness</div>
    5.35 +    </body>
    5.36 +</html>