jaroslav@1282: /** jaroslav@1282: * Back 2 Browser Bytecode Translator jaroslav@1282: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1282: * jaroslav@1282: * This program is free software: you can redistribute it and/or modify jaroslav@1282: * it under the terms of the GNU General Public License as published by jaroslav@1282: * the Free Software Foundation, version 2 of the License. jaroslav@1282: * jaroslav@1282: * This program is distributed in the hope that it will be useful, jaroslav@1282: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1282: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1282: * GNU General Public License for more details. jaroslav@1282: * jaroslav@1282: * You should have received a copy of the GNU General Public License jaroslav@1282: * along with this program. Look for COPYING file in the top folder. jaroslav@1282: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1282: */ jaroslav@1282: package org.apidesign.bck2brwsr.vmtest.impl; jaroslav@1282: jaroslav@1722: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@1282: import org.apidesign.bck2brwsr.vmtest.BrwsrTest; jaroslav@1282: import org.apidesign.bck2brwsr.vmtest.VMTest; jaroslav@1282: import org.testng.annotations.Factory; jaroslav@1282: jaroslav@1282: /** Verify cooperation with net.java.html.js annotations. jaroslav@1282: * jaroslav@1282: * @author Jaroslav Tulach jaroslav@1282: */ jaroslav@1282: public class HtmlAnnotationsTest { jaroslav@1722: static int firstCheck; jaroslav@1722: jaroslav@1722: private void assertMulNotDefinedForTheFirstTime() { jaroslav@1722: if (firstCheck++ == 0) { jaroslav@1722: Object mul = windowMul(); jaroslav@1722: assert mul == null : "htmlannotations.js should not be processed before first call to HtmlAnnotations class"; jaroslav@1722: } jaroslav@1722: } jaroslav@1722: jaroslav@1282: @BrwsrTest public void fourtyTwo() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: assertEquals(HtmlAnnotations.fourtyTwo(), 42); jaroslav@1282: } jaroslav@1282: jaroslav@1282: @BrwsrTest public void externalMul() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42); jaroslav@1282: } jaroslav@1282: jaroslav@1282: @BrwsrTest public void callRunnableFromJS() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: assertEquals(HtmlAnnotations.callback(), 1); jaroslav@1282: } jaroslav@1282: jaroslav@1282: @BrwsrTest public void callStaticMethodFromJS() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: assertEquals(HtmlAnnotations.staticCallback(), 1); jaroslav@1282: } jaroslav@1282: jaroslav@1282: @BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: Object instance = HtmlAnnotations.create(); jaroslav@1282: assertNotNull(instance, "Instance created"); jaroslav@1282: assertEquals(HtmlAnnotations.first(instance, 42, 31), 42); jaroslav@1282: } jaroslav@1282: jaroslav@1282: @BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception { jaroslav@1722: assertMulNotDefinedForTheFirstTime(); jaroslav@1282: Object instance = HtmlAnnotations.create(); jaroslav@1282: assertNotNull(instance, "Instance created"); jaroslav@1282: assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0); jaroslav@1282: } jaroslav@1282: jaroslav@1282: private static void assertEquals(double real, double exp) { jaroslav@1282: if (real - exp < 0.01) { jaroslav@1282: return; jaroslav@1282: } jaroslav@1282: assert false : "Expecting " + exp + " but was " + real; jaroslav@1282: } jaroslav@1282: jaroslav@1282: private static void assertNotNull(Object obj, String msg) { jaroslav@1282: assert obj != null : msg; jaroslav@1282: } jaroslav@1282: jaroslav@1722: @JavaScriptBody(args = {}, body = "return window.mul ? window.mul : null;") jaroslav@1722: private static native Object windowMul(); jaroslav@1722: jaroslav@1282: @Factory public static Object[] create() { jaroslav@1282: return VMTest.create(HtmlAnnotationsTest.class); jaroslav@1282: } jaroslav@1282: }