Describing how to use Scripts inside of a test
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Tue, 08 Jul 2014 07:30:45 +0200
changeset 7160654466b2273
parent 714 f03fd6c065f2
child 717 1a929cfa1dd4
child 721 f8e1066fc8cd
Describing how to use Scripts inside of a test
boot-script/src/main/java/net/java/html/boot/script/Scripts.java
boot/src/main/java/org/apidesign/html/boot/spi/Fn.java
     1.1 --- a/boot-script/src/main/java/net/java/html/boot/script/Scripts.java	Fri Jul 04 09:24:57 2014 +0200
     1.2 +++ b/boot-script/src/main/java/net/java/html/boot/script/Scripts.java	Tue Jul 08 07:30:45 2014 +0200
     1.3 @@ -42,10 +42,12 @@
     1.4   */
     1.5  package net.java.html.boot.script;
     1.6  
     1.7 +import java.io.Closeable;
     1.8  import java.util.concurrent.Executor;
     1.9  import javax.script.ScriptEngine;
    1.10  import net.java.html.boot.BrowserBuilder;
    1.11  import net.java.html.js.JavaScriptBody;
    1.12 +import org.apidesign.html.boot.spi.Fn;
    1.13  import org.apidesign.html.boot.spi.Fn.Presenter;
    1.14  
    1.15  /** Implementations of {@link Presenter}s that delegate
    1.16 @@ -55,14 +57,29 @@
    1.17   * <pre>
    1.18   * 
    1.19   * {@link Runnable} <em>run</em> = ...; // your own init code
    1.20 - * {@link Presenter Fn.Presenter} <em>p</em> = Scripts.{@link Scripts#createPresenter()};
    1.21 - * BrowserBuilder.{@link BrowserBuilder#newBrowser(java.lang.Object...) newBrowser(<em>p</em>)}.
    1.22 + * {@link Presenter Fn.Presenter} <b>p</b> = Scripts.{@link Scripts#createPresenter()};
    1.23 + * BrowserBuilder.{@link BrowserBuilder#newBrowser(java.lang.Object...) newBrowser(<b>p</b>)}.
    1.24   *      {@link BrowserBuilder#loadFinished(java.lang.Runnable) loadFinished(run)}.
    1.25   *      {@link BrowserBuilder#showAndWait()};
    1.26   * </pre>
    1.27   * 
    1.28   * and your runnable can make extensive use of {@link JavaScriptBody} directly or
    1.29   * indirectly via APIs using {@link JavaScriptBody such annotation} themselves.
    1.30 + * <p>
    1.31 + * Alternatively one can manipulate the presenter manually, which is
    1.32 + * especially useful when writing tests:
    1.33 + * <pre>
    1.34 + * {@code @Test} public void runInASimulatedBrowser() throws Exception {
    1.35 + *   {@link Presenter Fn.Presenter} <b>p</b> = Scripts.{@link Scripts#createPresenter()};
    1.36 + *   try ({@link Closeable} c = {@link Fn#activate(org.apidesign.html.boot.spi.Fn.Presenter) Fn.activate}(<b>p</b>)) {
    1.37 + *     // your code operating in context of <b>p</b>
    1.38 + *   }
    1.39 + * }
    1.40 + * </pre>
    1.41 + * The previous code snippet requires Java 7 language syntax, as it relies
    1.42 + * on try-with-resources language syntactic sugar feature. The same block
    1.43 + * of code can be used on older versions of Java, but it is slightly more
    1.44 + * verbose.
    1.45   * 
    1.46   * @author Jaroslav Tulach
    1.47   */
     2.1 --- a/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Fri Jul 04 09:24:57 2014 +0200
     2.2 +++ b/boot/src/main/java/org/apidesign/html/boot/spi/Fn.java	Tue Jul 08 07:30:45 2014 +0200
     2.3 @@ -193,8 +193,9 @@
     2.4          return FnContext.currentPresenter(false);
     2.5      }
     2.6      
     2.7 -    /** Activates given presenter. Used by the code generated by 
     2.8 -     * {@link JavaScriptBody} annotation: 
     2.9 +    /** Activates given presenter. Used to associate the native 
    2.10 +     * JavaScript code specified by 
    2.11 +     * {@link JavaScriptBody} annotation with certain presenter:
    2.12       * <pre>
    2.13       * try ({@link Closeable} c = Fn.activate(presenter)) {
    2.14       *   doCallsInPresenterContext();