You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jt...@apache.org on 2019/02/15 19:57:24 UTC

[incubator-netbeans-html4j] 01/15: Using the same model in two views with ko4j binding fails

This is an automated email from the ASF dual-hosted git repository.

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans-html4j.git

commit 000668ddfcc252e189a1fd5ecb09b025e9691949
Author: Jaroslav Tulach <ja...@apidesign.org>
AuthorDate: Mon Feb 11 11:11:44 2019 +0100

    Using the same model in two views with ko4j binding fails
---
 .../org/netbeans/html/ko4j/DoubleViewTest.java     | 157 +++++++++++++++++++++
 .../resources/org/netbeans/html/ko4j/double.html   |  34 +++++
 2 files changed, 191 insertions(+)

diff --git a/ko4j/src/test/java/org/netbeans/html/ko4j/DoubleViewTest.java b/ko4j/src/test/java/org/netbeans/html/ko4j/DoubleViewTest.java
new file mode 100644
index 0000000..7232bc2
--- /dev/null
+++ b/ko4j/src/test/java/org/netbeans/html/ko4j/DoubleViewTest.java
@@ -0,0 +1,157 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.html.ko4j;
+
+import java.awt.FlowLayout;
+import java.net.URL;
+import java.util.concurrent.CountDownLatch;
+import javafx.application.Platform;
+import javafx.embed.swing.JFXPanel;
+import javafx.scene.Scene;
+import javafx.scene.layout.BorderPane;
+import javafx.scene.web.WebView;
+import javax.swing.JFrame;
+import net.java.html.boot.fx.FXBrowsers;
+import net.java.html.json.Function;
+import net.java.html.json.Model;
+import net.java.html.json.Property;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Model(className = "DoubleView", targetId = "", properties = {
+    @Property(name = "message", type = String.class)
+})
+public class DoubleViewTest {
+    @Function
+    static void change(DoubleView model) {
+        model.setMessage("Modified");
+    }
+    
+    DoubleView doubleView;
+    private WebView view1;
+    private WebView view2;
+    
+    @BeforeMethod
+    public void initializeViews() throws Exception {
+        final JFXPanel panel = new JFXPanel();
+        final JFXPanel p2 = new JFXPanel();
+        
+        final CountDownLatch initViews = new CountDownLatch(1);
+        Platform.runLater(new Runnable() {
+            @Override
+            public void run() {
+                displayFrame(panel, p2);
+                initViews.countDown();
+            }
+        });
+        initViews.await();
+        
+        doubleView = new DoubleView();
+        doubleView.setMessage("Initialized");
+
+        final URL page = DoubleViewTest.class.getResource("double.html");
+        assertNotNull(page, "double.html found");
+
+        
+        
+        final CountDownLatch view1Init = new CountDownLatch(1);
+        final CountDownLatch view2Init = new CountDownLatch(1);
+        Platform.runLater(new Runnable() {
+            @Override
+            public void run() {
+                FXBrowsers.load(view1, page, new Runnable() {
+                    @Override
+                    public void run() {
+                        doubleView.applyBindings();
+                        view1Init.countDown();
+                    }
+                });
+
+                FXBrowsers.load(view2, page, new Runnable() {
+                    @Override
+                    public void run() {
+                        doubleView.applyBindings();
+                        view2Init.countDown();
+                    }
+                });
+            }
+        });
+        view1Init.await();
+        view2Init.await();
+    }
+    
+    private void displayFrame(JFXPanel panel, JFXPanel p2) {
+        view1 = displayWebView(panel);
+        view2 = displayWebView(p2);
+        
+        JFrame f = new JFrame();
+        f.getContentPane().setLayout(new FlowLayout());
+        f.getContentPane().add(panel);
+        f.getContentPane().add(p2);
+        f.pack();
+        f.setVisible(true);
+    }
+
+    private WebView displayWebView(JFXPanel panel) {
+        BorderPane pane = new BorderPane();
+        Scene scene = new Scene(pane, 800, 600);
+        WebView webView = new WebView();
+        pane.setCenter(webView);
+        panel.setScene(scene);
+        return webView;
+    }
+    
+    @Test
+    public void synchronizationOfViews() throws Throwable {
+        final CountDownLatch cdl = new CountDownLatch(1);
+        final Throwable[] arr = new Throwable[1];
+        Platform.runLater(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    assertMessages("In view one", view1, "Initialized");
+                    assertMessages("In view two", view2, "Initialized");
+                } catch (Throwable t) {
+                    arr[0] = t;
+                } finally {
+                    cdl.countDown();
+                }
+            }
+        });
+        cdl.await();
+        if (arr[0] != null) {
+            throw arr[0];
+        }
+    }
+    
+    @AfterMethod
+    public void waitABit() throws Exception {
+    }
+
+    private void assertMessages(String msg, WebView v, String expected) {
+        Object func = v.getEngine().executeScript("document.getElementById('function').innerHTML");
+        assertEquals(func, expected, msg + " 'function' check");
+
+        Object prop = v.getEngine().executeScript("document.getElementById('property').innerHTML");
+        assertEquals(prop, expected, msg + " 'property' check");
+    }
+}
diff --git a/ko4j/src/test/resources/org/netbeans/html/ko4j/double.html b/ko4j/src/test/resources/org/netbeans/html/ko4j/double.html
new file mode 100644
index 0000000..30d5883
--- /dev/null
+++ b/ko4j/src/test/resources/org/netbeans/html/ko4j/double.html
@@ -0,0 +1,34 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>Double Usage</title>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+        <meta name="viewport" content="width=device-width">
+    </head>
+    <body>
+        <h1>Double Usage</h1>
+        <div id='property' data-bind="text: message">Message as property</div>
+        <div id='function' data-bind="text: message()">Message as function</div>
+        <button id='change' data-bind="click: change">Change</button>
+    </body>
+</html>


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists