From 494b8c700da5ee255ff8e51062eba2604bae9366 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 16:17:26 -0300 Subject: [PATCH] test: skip multithreaded-JS specs when the runtime does not run them The two specs that run JS on a background Java thread need the app's android.enableMultithreadedJavascript. A shared helper finds out by trying it once (a background thread calls a JS Runnable and reports which thread ran it, on the main looper), and the specs are pending when it is off. --- .../app/tests/multithreadedJavascript.js | 25 +++++++++++++++++++ .../main/assets/app/tests/testEventLoop.js | 11 ++++++++ .../app/tests/testMultithreadedJavascript.js | 11 ++++++++ 3 files changed, 47 insertions(+) create mode 100644 test-app/app/src/main/assets/app/tests/multithreadedJavascript.js diff --git a/test-app/app/src/main/assets/app/tests/multithreadedJavascript.js b/test-app/app/src/main/assets/app/tests/multithreadedJavascript.js new file mode 100644 index 000000000..98d8c3a4f --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/multithreadedJavascript.js @@ -0,0 +1,25 @@ +// Whether this runtime runs a Java->JS call on the Java thread that makes it, which is what +// the app's android.enableMultithreadedJavascript turns on. Found by trying it once: without +// it, a call from a background Java thread is posted to the runtime's own thread instead. +// The result is reported on the main looper, so Jasmine never continues on another thread. +var detected; + +module.exports = function multithreadedJavascript() { + if (detected === undefined) { + detected = new Promise(function (resolve) { + var mainThread = java.lang.Thread.currentThread().getId(); + var mainHandler = new android.os.Handler(android.os.Looper.getMainLooper()); + new java.lang.Thread(new java.lang.Runnable({ + run: function () { + var enabled = java.lang.Thread.currentThread().getId() !== mainThread; + mainHandler.post(new java.lang.Runnable({ + run: function () { + resolve(enabled); + } + })); + } + })).start(); + }); + } + return detected; +}; diff --git a/test-app/app/src/main/assets/app/tests/testEventLoop.js b/test-app/app/src/main/assets/app/tests/testEventLoop.js index e74493d31..05d515fc8 100644 --- a/test-app/app/src/main/assets/app/tests/testEventLoop.js +++ b/test-app/app/src/main/assets/app/tests/testEventLoop.js @@ -72,6 +72,14 @@ describe("event loop foreground tasks", function () { // The ordered lane rides the Java MessageQueue, so these callbacks must be // strict macrotasks: after the current turn's microtasks, FIFO with timers. describe("event loop ordered macrotasks", function () { + let multithreaded; + beforeAll(function (done) { + require("./multithreadedJavascript")().then(function (enabled) { + multithreaded = enabled; + done(); + }); + }); + it("__ns__queueMacrotask runs the callback asynchronously", function (done) { let ran = false; __ns__queueMacrotask(() => { @@ -108,6 +116,9 @@ describe("event loop ordered macrotasks", function () { }); it("runs on the main thread when posted from a background JS thread", function (done) { + if (!multithreaded) { + pending("needs android.enableMultithreadedJavascript"); + } const mainThreadId = java.lang.Thread.currentThread().getId(); new java.lang.Thread(new java.lang.Runnable({ run() { diff --git a/test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js b/test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js index 1aabb3daf..06b380046 100644 --- a/test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js +++ b/test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js @@ -1,5 +1,16 @@ describe("Test multithreaded javascript ", () => { + let multithreaded; + beforeAll(done => { + require("./multithreadedJavascript")().then(enabled => { + multithreaded = enabled; + done(); + }); + }); + it("Should execute callbacks on specified native thread", done => { + if (!multithreaded) { + pending("needs android.enableMultithreadedJavascript"); + } const currentThreadId = java.lang.Thread.currentThread().getId(); new java.lang.Thread(new java.lang.Runnable({ run() {