jaroslav@530: /** jaroslav@530: * Back 2 Browser Bytecode Translator jaroslav@530: * Copyright (C) 2012 Jaroslav Tulach jaroslav@530: * jaroslav@530: * This program is free software: you can redistribute it and/or modify jaroslav@530: * it under the terms of the GNU General Public License as published by jaroslav@530: * the Free Software Foundation, version 2 of the License. jaroslav@530: * jaroslav@530: * This program is distributed in the hope that it will be useful, jaroslav@530: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@530: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@530: * GNU General Public License for more details. jaroslav@530: * jaroslav@530: * You should have received a copy of the GNU General Public License jaroslav@530: * along with this program. Look for COPYING file in the top folder. jaroslav@530: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@530: */ jaroslav@530: package org.apidesign.bck2brwsr.htmlpage; jaroslav@530: jaroslav@767: import java.util.List; jaroslav@763: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@530: import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty; jaroslav@530: import org.apidesign.bck2brwsr.htmlpage.api.OnEvent; jaroslav@879: import org.apidesign.bck2brwsr.htmlpage.api.OnFunction; jaroslav@530: import org.apidesign.bck2brwsr.htmlpage.api.Page; jaroslav@530: import org.apidesign.bck2brwsr.htmlpage.api.Property; jaroslav@530: import org.apidesign.bck2brwsr.vmtest.BrwsrTest; jaroslav@530: import org.apidesign.bck2brwsr.vmtest.HtmlFragment; jaroslav@530: import org.apidesign.bck2brwsr.vmtest.VMTest; jaroslav@530: import org.testng.annotations.Factory; jaroslav@530: jaroslav@530: /** jaroslav@530: * jaroslav@530: * @author Jaroslav Tulach jaroslav@530: */ jaroslav@530: @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={ jaroslav@763: @Property(name="name", type=String.class), jaroslav@879: @Property(name="results", type=String.class, array = true), jaroslav@879: @Property(name="callbackCount", type=int.class) jaroslav@530: }) jaroslav@530: public class KnockoutTest { jaroslav@530: jaroslav@530: @HtmlFragment( jaroslav@530: "

Loading Bck2Brwsr's Hello World...

\n" + jaroslav@530: "Your name: \n" + jaroslav@530: "\n" jaroslav@530: ) jaroslav@530: @BrwsrTest public void modifyValueAssertChangeInModel() { jaroslav@530: KnockoutModel m = new KnockoutModel(); jaroslav@530: m.setName("Kukuc"); jaroslav@530: m.applyBindings(); jaroslav@892: assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue(); jaroslav@892: m.input.setValue("Jardo"); jaroslav@892: m.triggerEvent(m.input, OnEvent.CHANGE); jaroslav@530: assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName(); jaroslav@530: } jaroslav@530: jaroslav@763: @HtmlFragment( jaroslav@763: "\n" jaroslav@763: ) jaroslav@763: @BrwsrTest public void displayContentOfArray() { jaroslav@763: KnockoutModel m = new KnockoutModel(); jaroslav@763: m.getResults().add("Ahoj"); jaroslav@763: m.applyBindings(); jaroslav@763: jaroslav@763: int cnt = countChildren("ul"); jaroslav@763: assert cnt == 1 : "One child, but was " + cnt; jaroslav@763: jaroslav@879: m.getResults().add("Hi"); jaroslav@763: jaroslav@763: cnt = countChildren("ul"); jaroslav@763: assert cnt == 2 : "Two children now, but was " + cnt; jaroslav@879: jaroslav@879: triggerChildClick("ul", 1); jaroslav@879: jaroslav@879: assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount(); jaroslav@879: assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName(); jaroslav@763: } jaroslav@763: jaroslav@767: @HtmlFragment( jaroslav@767: "\n" jaroslav@767: ) jaroslav@767: @BrwsrTest public void displayContentOfDerivedArray() { jaroslav@767: KnockoutModel m = new KnockoutModel(); jaroslav@767: m.getResults().add("Ahoj"); jaroslav@767: m.applyBindings(); jaroslav@767: jaroslav@767: int cnt = countChildren("ul"); jaroslav@767: assert cnt == 1 : "One child, but was " + cnt; jaroslav@767: jaroslav@767: m.getResults().add("hello"); jaroslav@767: jaroslav@767: cnt = countChildren("ul"); jaroslav@767: assert cnt == 2 : "Two children now, but was " + cnt; jaroslav@767: } jaroslav@767: jaroslav@879: @OnFunction jaroslav@879: static void call(KnockoutModel m, String data) { jaroslav@879: m.setName(data); jaroslav@879: m.setCallbackCount(m.getCallbackCount() + 1); jaroslav@879: } jaroslav@879: jaroslav@530: @ComputedProperty jaroslav@530: static String helloMessage(String name) { jaroslav@530: return "Hello " + name + "!"; jaroslav@530: } jaroslav@530: jaroslav@767: @ComputedProperty jaroslav@767: static List cmpResults(List results) { jaroslav@767: return results; jaroslav@767: } jaroslav@767: jaroslav@530: @Factory jaroslav@530: public static Object[] create() { jaroslav@530: return VMTest.create(KnockoutTest.class); jaroslav@530: } jaroslav@763: jaroslav@763: @JavaScriptBody(args = { "id" }, body = jaroslav@763: "var e = window.document.getElementById(id);\n " jaroslav@763: + "if (typeof e === 'undefined') return -2;\n " jaroslav@763: + "return e.children.length;\n " jaroslav@763: ) jaroslav@763: private static native int countChildren(String id); jaroslav@879: jaroslav@879: @JavaScriptBody(args = { "id", "pos" }, body = jaroslav@879: "var e = window.document.getElementById(id);\n " jaroslav@879: + "var ev = window.document.createEvent('MouseEvents');\n " jaroslav@879: + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " jaroslav@879: + "e.children[pos].dispatchEvent(ev);\n " jaroslav@879: ) jaroslav@879: private static native void triggerChildClick(String id, int pos); jaroslav@530: }