ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 06:14:49 +0200
changeset 1947 12a252145892
parent 1944 644d4f4bc6e0
permissions -rw-r--r--
Automatic testing of sample knockout based bck2brwsr application
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.kosample;
    19 
    20 import java.util.Timer;
    21 import java.util.TimerTask;
    22 import net.java.html.json.ComputedProperty;
    23 import net.java.html.json.Function;
    24 import net.java.html.json.Model;
    25 import net.java.html.json.ModelOperation;
    26 import net.java.html.json.Property;
    27 import org.apidesign.bck2brwsr.kosample.js.Dialogs;
    28 
    29 /** Model annotation generates class Data with
    30  * one message property, boolean property and read only words property
    31  */
    32 @Model(className = "Data", targetId="", instance = true, properties = {
    33     @Property(name = "message", type = String.class),
    34     @Property(name = "rotating", type = boolean.class)
    35 })
    36 final class DataModel {
    37     @ComputedProperty static java.util.List<String> words(String message) {
    38         String[] arr = new String[6];
    39         String[] words = message == null ? new String[0] : message.split(" ", 6);
    40         for (int i = 0; i < 6; i++) {
    41             arr[i] = words.length > i ? words[i] : "!";
    42         }
    43         return java.util.Arrays.asList(arr);
    44     }
    45 
    46     @Function static void turnAnimationOn(Data model) {
    47         model.setRotating(true);
    48     }
    49 
    50     @Function static void turnAnimationOff(final Data model) {
    51         Dialogs.confirmByUser("Really turn off?", new Runnable() {
    52             @Override
    53             public void run() {
    54                 model.setRotating(false);
    55             }
    56         });
    57     }
    58 
    59     private static final Timer TIMER = new Timer("Pending tasks");
    60     private static void schedule(Runnable run, long delay) {
    61         TIMER.schedule(new TimerTask() {
    62             @Override
    63             public void run() {
    64                 run.run();
    65             }
    66         }, delay);
    67     }
    68 
    69     @Function static void rotate5s(final Data model) {
    70         model.setRotating(true);
    71         schedule(() -> model.setRotating(false), 5000);
    72     }
    73 
    74     @Function static void showScreenSize(Data model) {
    75         model.setMessage(Dialogs.screenSize());
    76     }
    77     private static Data ui;
    78     /**
    79      * Called when the page is ready.
    80      */
    81     static void onPageLoad() throws Exception {
    82         ui = new Data();
    83         ui.setMessage("Hello World from HTML and Java!");
    84         ui.applyBindings();
    85 
    86         schedule(() -> ui.startTest(), 1000);
    87     }
    88 
    89     //
    90     // testing
    91     //
    92 
    93     @ModelOperation
    94     static void startTest(Data model) {
    95         Dialogs.triggerClick("beginTest");
    96     }
    97 
    98     private boolean inTesting;
    99 
   100     @Function
   101     void beginTest(Data model) {
   102         if (inTesting) {
   103             model.setRotating(false);
   104             model.setMessage("Hello World from HTML and Java!");
   105             inTesting = false;
   106             return;
   107         }
   108 
   109         inTesting = true;
   110         model.setMessage("In testing mode stop Automatic testing?");
   111         model.setRotating(true);
   112         schedule(() -> {
   113             if (inTesting) {
   114                 model.setMessage("In testing mode count down 3s");
   115             }
   116         }, 3000);
   117         schedule(() -> {
   118             if (inTesting) {
   119                 model.setMessage("In testing mode count down 2s");
   120             }
   121         }, 4000);
   122         schedule(() -> {
   123             if (inTesting) {
   124                 model.setMessage("In testing mode count down 1s");
   125             }
   126         }, 5000);
   127         schedule(() -> {
   128             if (inTesting) {
   129                 model.setMessage("Finished testing mode close the browser");
   130                 model.setRotating(false);
   131                 System.exit(0);
   132             }
   133         }, 6000);
   134     }
   135 }