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 2020/12/26 14:40:03 UTC
[netbeans-html4j] 08/25: Run the asynch JavaScript action test with
and without JavaScript callback
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/netbeans-html4j.git
commit 698659d9501e5bc7da247e3cf9fac4927b616701
Author: Jaroslav Tulach <ja...@apidesign.org>
AuthorDate: Tue Dec 22 06:52:16 2020 +0100
Run the asynch JavaScript action test with and without JavaScript callback
---
.../java/html/js/tests/AsyncJavaScriptAction.java | 36 +++++++++++++++-------
.../net/java/html/js/tests/JavaScriptBodyTest.java | 12 ++++++--
.../main/java/net/java/html/js/tests/JsUtils.java | 24 ++++++++++-----
.../org/netbeans/html/json/tck/JavaScriptTCK.java | 10 ++++--
4 files changed, 58 insertions(+), 24 deletions(-)
diff --git a/json-tck/src/main/java/net/java/html/js/tests/AsyncJavaScriptAction.java b/json-tck/src/main/java/net/java/html/js/tests/AsyncJavaScriptAction.java
index a9f20af..b885f75 100644
--- a/json-tck/src/main/java/net/java/html/js/tests/AsyncJavaScriptAction.java
+++ b/json-tck/src/main/java/net/java/html/js/tests/AsyncJavaScriptAction.java
@@ -19,16 +19,18 @@
package net.java.html.js.tests;
import java.util.List;
+import java.util.function.Function;
import net.java.html.js.JavaScriptBody;
import static net.java.html.js.tests.JavaScriptBodyTest.assertEquals;
final class AsyncJavaScriptAction {
- List<Integer> collected = new java.util.ArrayList<>();
+ private List<Integer> collected = new java.util.ArrayList<>();
+ private boolean successStoringLater;
@JavaScriptBody(args = { "n" }, javacall = true, body = """
- return this.@net.java.html.js.tests.AsyncJavaScriptAction::performTheTest(I)(n);
+ return this.@net.java.html.js.tests.AsyncJavaScriptAction::performIteration(I)(n);
""")
- private native int r(int n);
+ private native int enterJavaScriptAndPerformIteration(int n);
@JavaScriptBody(args = {}, javacall = true, body = """
var self = this;
@@ -48,32 +50,44 @@ final class AsyncJavaScriptAction {
collected.add(value);
}
- int performTheTest(int from) throws Exception {
+ int performIteration(int from) {
for (int i = 0; i < 5; i++) {
jsStore(from++);
}
final String n = "" + from++;
- JsUtils.executeNow(AsyncJavaScriptAction.class, """
- storeLater({n});
- """.replace("{n}", n));
+ successStoringLater = JsUtils.executeNow(AsyncJavaScriptAction.class, "storeLater(" + n + ");");
for (int i = 6; i < 11; i++) {
jsStore(from++);
}
return from;
}
- public void runTheWholeTest() {
+ private void performTheTest(Function<Integer,Integer> iteration) {
defineStore();
- assertEquals(r(0), 11);
+ assertEquals(iteration.apply(0), 11);
+ if (!successStoringLater) {
+ return;
+ }
assertEquals(collected.size(), 11, "11 items: " + collected);
for (int i = 0; i < 11; i++) {
assertEquals(collected.get(i).intValue(), i, i + "th out of order: " + collected);
}
- assertEquals(r(11), 22);
-
+ assertEquals(iteration.apply(11), 22);
+ if (!successStoringLater) {
+ return;
+ }
assertEquals(collected.size(), 22, "22 items: " + collected);
for (int i = 0; i < 22; i++) {
assertEquals(collected.get(i).intValue(), i, i + "th out of order: " + collected);
}
}
+
+ public void testWithCallback() {
+ performTheTest(this::enterJavaScriptAndPerformIteration);
+ }
+
+ public void testWithoutCallback() {
+ performTheTest(this::performIteration);
+ }
+
}
diff --git a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
index 3a0e8cf..c2f8eef 100644
--- a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
+++ b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
@@ -411,12 +411,12 @@ public class JavaScriptBodyTest {
return;
}
if (l == null) {
- JsUtils.executeNow(JavaScriptBodyTest.class,
+ JsUtils.execute(JavaScriptBodyTest.class,
"if (typeof window === 'undefined') window = {};"
);
l = new Later();
l.register();
- JsUtils.executeNow(JavaScriptBodyTest.class,
+ JsUtils.execute(JavaScriptBodyTest.class,
"window.later();"
);
}
@@ -429,7 +429,13 @@ public class JavaScriptBodyTest {
@KOTest
public void asynchCallFromJavaScriptInMiddleOfDefferedProcessing() {
AsyncJavaScriptAction t = new AsyncJavaScriptAction();
- t.runTheWholeTest();
+ t.testWithoutCallback();
+ }
+
+ @KOTest
+ public void asynchCallFromJavaScriptInMiddleOfDefferedProcessingFromCallback() {
+ AsyncJavaScriptAction t = new AsyncJavaScriptAction();
+ t.testWithCallback();
}
@KOTest
diff --git a/json-tck/src/main/java/net/java/html/js/tests/JsUtils.java b/json-tck/src/main/java/net/java/html/js/tests/JsUtils.java
index de99cca..2a26248 100644
--- a/json-tck/src/main/java/net/java/html/js/tests/JsUtils.java
+++ b/json-tck/src/main/java/net/java/html/js/tests/JsUtils.java
@@ -32,15 +32,25 @@ public class JsUtils {
instantiatedJsTCK = tck;
}
- static void executeNow(Class<?> clazz, String script) throws Exception {
- if (instantiatedJsTCK != null) {
- instantiatedJsTCK.executeNow(script);
- } else {
- Fn.Presenter p = Fn.activePresenter();
- if (p != null) {
- p.loadScript(new StringReader(script));
+ static void execute(Class<?> clazz, String script) throws Exception {
+ Fn.Presenter p = Fn.activePresenter();
+ p.loadScript(new StringReader(script));
+ }
+
+ static boolean executeNow(Class<?> clazz, String script) {
+ try {
+ if (instantiatedJsTCK != null) {
+ return instantiatedJsTCK.executeNow(script);
+ } else {
+ execute(clazz, script);
+ return true;
}
+ } catch (Exception ex) {
+ throw raise(RuntimeException.class, ex);
}
}
+ private static <E extends Throwable> E raise(Class<E> e, Throwable t) throws E {
+ throw (E)t;
+ }
}
diff --git a/json-tck/src/main/java/org/netbeans/html/json/tck/JavaScriptTCK.java b/json-tck/src/main/java/org/netbeans/html/json/tck/JavaScriptTCK.java
index 13c5350..0b2f543 100644
--- a/json-tck/src/main/java/org/netbeans/html/json/tck/JavaScriptTCK.java
+++ b/json-tck/src/main/java/org/netbeans/html/json/tck/JavaScriptTCK.java
@@ -85,14 +85,18 @@ public abstract class JavaScriptTCK {
/** Executes JavaScript now. Simulates that something suddenly happens
* in the JavaScript while Java code may already be doing something
- * different.
+ * different. If it is not possible to execute the JavaScript - for
+ * example the JavaScript engine is blocked by currently running request,
+ * then return {@code false} and let the TCK test terminate gracefully.
*
* @param script the script to execute in the JavaScript
- * @throws Exception if something goes wrong
+ * @return {@code true} if the script was executed, {@code false} if it couldn't be
+ * @throws Exception if the script contains an error
* @since 1.7.1
*/
- public void executeNow(String script) throws Exception {
+ public boolean executeNow(String script) throws Exception {
Presenter p = Fn.activePresenter();
p.loadScript(new StringReader(script));
+ return true;
}
}
---------------------------------------------------------------------
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