Skip to content

Commit 494b8c7

Browse files
committed
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.
1 parent a121063 commit 494b8c7

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Whether this runtime runs a Java->JS call on the Java thread that makes it, which is what
2+
// the app's android.enableMultithreadedJavascript turns on. Found by trying it once: without
3+
// it, a call from a background Java thread is posted to the runtime's own thread instead.
4+
// The result is reported on the main looper, so Jasmine never continues on another thread.
5+
var detected;
6+
7+
module.exports = function multithreadedJavascript() {
8+
if (detected === undefined) {
9+
detected = new Promise(function (resolve) {
10+
var mainThread = java.lang.Thread.currentThread().getId();
11+
var mainHandler = new android.os.Handler(android.os.Looper.getMainLooper());
12+
new java.lang.Thread(new java.lang.Runnable({
13+
run: function () {
14+
var enabled = java.lang.Thread.currentThread().getId() !== mainThread;
15+
mainHandler.post(new java.lang.Runnable({
16+
run: function () {
17+
resolve(enabled);
18+
}
19+
}));
20+
}
21+
})).start();
22+
});
23+
}
24+
return detected;
25+
};

‎test-app/app/src/main/assets/app/tests/testEventLoop.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ describe("event loop foreground tasks", function () {
7272
// The ordered lane rides the Java MessageQueue, so these callbacks must be
7373
// strict macrotasks: after the current turn's microtasks, FIFO with timers.
7474
describe("event loop ordered macrotasks", function () {
75+
let multithreaded;
76+
beforeAll(function (done) {
77+
require("./multithreadedJavascript")().then(function (enabled) {
78+
multithreaded = enabled;
79+
done();
80+
});
81+
});
82+
7583
it("__ns__queueMacrotask runs the callback asynchronously", function (done) {
7684
let ran = false;
7785
__ns__queueMacrotask(() => {
@@ -108,6 +116,9 @@ describe("event loop ordered macrotasks", function () {
108116
});
109117

110118
it("runs on the main thread when posted from a background JS thread", function (done) {
119+
if (!multithreaded) {
120+
pending("needs android.enableMultithreadedJavascript");
121+
}
111122
const mainThreadId = java.lang.Thread.currentThread().getId();
112123
new java.lang.Thread(new java.lang.Runnable({
113124
run() {

‎test-app/app/src/main/assets/app/tests/testMultithreadedJavascript.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
describe("Test multithreaded javascript ", () => {
2+
let multithreaded;
3+
beforeAll(done => {
4+
require("./multithreadedJavascript")().then(enabled => {
5+
multithreaded = enabled;
6+
done();
7+
});
8+
});
9+
210
it("Should execute callbacks on specified native thread", done => {
11+
if (!multithreaded) {
12+
pending("needs android.enableMultithreadedJavascript");
13+
}
314
const currentThreadId = java.lang.Thread.currentThread().getId();
415
new java.lang.Thread(new java.lang.Runnable({
516
run() {

0 commit comments

Comments
 (0)