ko/kosample/js/src/test/java/org/apidesign/bck2brwsr/kosample/js/JsInteractionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Apr 2016 08:04:12 +0200
changeset 1941 621825e167d7
permissions -rw-r--r--
Adding sample module using knockout
     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.js;
    19 
    20 import java.io.Closeable;
    21 import net.java.html.boot.script.Scripts;
    22 import org.netbeans.html.boot.spi.Fn;
    23 import static org.testng.Assert.assertEquals;
    24 import org.testng.annotations.AfterMethod;
    25 import org.testng.annotations.BeforeMethod;
    26 import org.testng.annotations.Test;
    27 
    28 /** Tests for behavior of @JavaScriptBody methods. Set your JavaScript 
    29  * environment up (for example define <code>alert</code> or use some
    30  * emulation library like <em>env.js</em>), register script presenter 
    31  * and then you can call methods that deal with JavaScript in your tests.
    32  */
    33 public class JsInteractionTest {
    34     private Closeable jsEngine;
    35     @BeforeMethod public void initializeJSEngine() throws Exception {
    36         jsEngine = Fn.activate(Scripts.createPresenter());
    37     }
    38     
    39     @AfterMethod public void shutdownJSEngine() throws Exception {
    40         jsEngine.close();
    41     }
    42     @Test public void testCallbackFromJavaScript() throws Exception {
    43         class R implements Runnable {
    44             int called;
    45 
    46             @Override
    47             public void run() {
    48                 called++;
    49             }
    50         }
    51         R callback = new R();
    52         
    53         Dialogs.confirmByUser("Hello", callback);
    54         
    55         assertEquals(callback.called, 1, "One immediate callback");
    56     }
    57 }