Automatic testing of sample knockout based bck2brwsr application
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 06:14:49 +0200
changeset 194712a252145892
parent 1946 bafdddd2a0cf
child 1948 5b127bc8163e
Automatic testing of sample knockout based bck2brwsr application
ko/kosample/client-web/pom.xml
ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java
ko/kosample/client/src/main/webapp/pages/index.html
ko/kosample/js/src/main/java/org/apidesign/bck2brwsr/kosample/js/Dialogs.java
ko/kosample/pom.xml
     1.1 --- a/ko/kosample/client-web/pom.xml	Wed Apr 27 06:13:59 2016 +0200
     1.2 +++ b/ko/kosample/client-web/pom.xml	Wed Apr 27 06:14:49 2016 +0200
     1.3 @@ -295,5 +295,31 @@
     1.4                  </plugins>
     1.5              </build>
     1.6          </profile>
     1.7 +        <profile>
     1.8 +            <id>test-run</id>
     1.9 +            <activation>
    1.10 +                <property>
    1.11 +                    <name>!skipTests</name>
    1.12 +                </property>
    1.13 +            </activation>
    1.14 +            <build>
    1.15 +                <plugins>
    1.16 +                    <plugin>
    1.17 +                        <groupId>org.apidesign.bck2brwsr</groupId>
    1.18 +                        <artifactId>bck2brwsr-maven-plugin</artifactId>
    1.19 +                        <version>${project.version}</version>
    1.20 +                        <executions>
    1.21 +                            <execution>
    1.22 +                                <id>show</id>
    1.23 +                                <goals>
    1.24 +                                    <goal>show</goal>
    1.25 +                                </goals>
    1.26 +                                <phase>integration-test</phase>
    1.27 +                            </execution>
    1.28 +                        </executions>
    1.29 +                    </plugin>
    1.30 +                </plugins>
    1.31 +            </build>
    1.32 +        </profile>
    1.33      </profiles>
    1.34  </project>
     2.1 --- a/ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java	Wed Apr 27 06:13:59 2016 +0200
     2.2 +++ b/ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java	Wed Apr 27 06:14:49 2016 +0200
     2.3 @@ -17,17 +17,19 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.kosample;
     2.6  
     2.7 +import java.util.Timer;
     2.8  import java.util.TimerTask;
     2.9  import net.java.html.json.ComputedProperty;
    2.10  import net.java.html.json.Function;
    2.11  import net.java.html.json.Model;
    2.12 +import net.java.html.json.ModelOperation;
    2.13  import net.java.html.json.Property;
    2.14  import org.apidesign.bck2brwsr.kosample.js.Dialogs;
    2.15  
    2.16  /** Model annotation generates class Data with
    2.17   * one message property, boolean property and read only words property
    2.18   */
    2.19 -@Model(className = "Data", targetId="", properties = {
    2.20 +@Model(className = "Data", targetId="", instance = true, properties = {
    2.21      @Property(name = "message", type = String.class),
    2.22      @Property(name = "rotating", type = boolean.class)
    2.23  })
    2.24 @@ -54,7 +56,7 @@
    2.25          });
    2.26      }
    2.27  
    2.28 -    private static java.util.Timer TIMER = new java.util.Timer("Pending tasks");
    2.29 +    private static final Timer TIMER = new Timer("Pending tasks");
    2.30      private static void schedule(Runnable run, long delay) {
    2.31          TIMER.schedule(new TimerTask() {
    2.32              @Override
    2.33 @@ -80,5 +82,54 @@
    2.34          ui = new Data();
    2.35          ui.setMessage("Hello World from HTML and Java!");
    2.36          ui.applyBindings();
    2.37 +
    2.38 +        schedule(() -> ui.startTest(), 1000);
    2.39 +    }
    2.40 +
    2.41 +    //
    2.42 +    // testing
    2.43 +    //
    2.44 +
    2.45 +    @ModelOperation
    2.46 +    static void startTest(Data model) {
    2.47 +        Dialogs.triggerClick("beginTest");
    2.48 +    }
    2.49 +
    2.50 +    private boolean inTesting;
    2.51 +
    2.52 +    @Function
    2.53 +    void beginTest(Data model) {
    2.54 +        if (inTesting) {
    2.55 +            model.setRotating(false);
    2.56 +            model.setMessage("Hello World from HTML and Java!");
    2.57 +            inTesting = false;
    2.58 +            return;
    2.59 +        }
    2.60 +
    2.61 +        inTesting = true;
    2.62 +        model.setMessage("In testing mode stop Automatic testing?");
    2.63 +        model.setRotating(true);
    2.64 +        schedule(() -> {
    2.65 +            if (inTesting) {
    2.66 +                model.setMessage("In testing mode count down 3s");
    2.67 +            }
    2.68 +        }, 3000);
    2.69 +        schedule(() -> {
    2.70 +            if (inTesting) {
    2.71 +                model.setMessage("In testing mode count down 2s");
    2.72 +            }
    2.73 +        }, 4000);
    2.74 +        schedule(() -> {
    2.75 +            if (inTesting) {
    2.76 +                model.setMessage("In testing mode count down 1s");
    2.77 +            }
    2.78 +        }, 5000);
    2.79 +        schedule(() -> {
    2.80 +            if (inTesting) {
    2.81 +                model.setMessage("Finished testing mode close the browser");
    2.82 +                model.setRotating(false);
    2.83 +                System.exit(0);
    2.84 +            }
    2.85 +        }, 6000);
    2.86      }
    2.87  }
     3.1 --- a/ko/kosample/client/src/main/webapp/pages/index.html	Wed Apr 27 06:13:59 2016 +0200
     3.2 +++ b/ko/kosample/client/src/main/webapp/pages/index.html	Wed Apr 27 06:14:49 2016 +0200
     3.3 @@ -80,6 +80,7 @@
     3.4          <button data-bind="enable: rotating, click: $root.turnAnimationOff">Stop</button>
     3.5          <button data-bind="enable: !rotating(), click: $root.rotate5s">Rotate Few Seconds</button>
     3.6          <button data-bind="click: $root.showScreenSize">Screen Size!</button>
     3.7 +        <button id='beginTest' data-bind="click: $root.beginTest">Automatic Test</button>
     3.8  
     3.9          <div id="scene">
    3.10              <span id="words" data-bind="foreach: words">
     4.1 --- a/ko/kosample/js/src/main/java/org/apidesign/bck2brwsr/kosample/js/Dialogs.java	Wed Apr 27 06:13:59 2016 +0200
     4.2 +++ b/ko/kosample/js/src/main/java/org/apidesign/bck2brwsr/kosample/js/Dialogs.java	Wed Apr 27 06:14:49 2016 +0200
     4.3 @@ -54,4 +54,12 @@
     4.4          "return 'Screen size is ' + x + ' times ' + y;\n"
     4.5      )
     4.6      public static native String screenSize();
     4.7 +
     4.8 +    @JavaScriptBody(args = { "id" }, body =
     4.9 +          "var e = window.document.getElementById(id);\n "
    4.10 +        + "var ev = window.document.createEvent('MouseEvents');\n "
    4.11 +        + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
    4.12 +        + "e.dispatchEvent(ev);\n "
    4.13 +    )
    4.14 +    public static native void triggerClick(String id);
    4.15  }
     5.1 --- a/ko/kosample/pom.xml	Wed Apr 27 06:13:59 2016 +0200
     5.2 +++ b/ko/kosample/pom.xml	Wed Apr 27 06:14:49 2016 +0200
     5.3 @@ -20,7 +20,7 @@
     5.4      </modules>
     5.5      <properties>
     5.6          <bck2brwsr.version>${project.version}</bck2brwsr.version>
     5.7 -        <bck2brwsr.obfuscationlevel>FULL</bck2brwsr.obfuscationlevel>
     5.8 +        <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
     5.9          <jersey.version>2.13</jersey.version>
    5.10          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    5.11      </properties>