Skip to content

Commit b83a681

Browse files
committed
test: app file modes and loadCodeFromApk specs
The suite can now be run in three modes from the repo root: ./gradlew runtestsAndVerifyResults ./gradlew runtestsAndVerifyResults -PloadCodeFromApk ./gradlew runtestsAndVerifyResults -PloadCodeFromApk -PstoreCodeUncompressed The properties are forwarded to the test app build, where tools/appfiles-test-mode.init.gradle sets the flags in the packaged app/package.json (and applies the exported noCompress list) without touching the source package.json. The init script is always applied and adds the mode as an input of the asset merge, so switching modes rebuilds the packaged assets. tests/testLoadCodeFromApk.js covers, in both modes: runtime files, a getFilesDir()-spelled runScript, require and import under both /data/data and /data/user/0 spellings (one module each), and the android.alwaysExtract escape hatch, which the test app now uses. With the flag on: no code under files/app beyond the alwaysExtract match, JSON and images still extracted, no files/internal or files/metadata, a worker loading APK-served code, and a file written into files/app under the runtime lock (as LiveSync does) winning over the APK copy until deleted.
1 parent 51d3a8e commit b83a681

9 files changed

Lines changed: 221 additions & 1 deletion

File tree

‎build.gradle‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ def getRunTestsBuildArguments = { taskName ->
465465
if (onlyX86) {
466466
arguments.add("-PonlyX86")
467467
}
468+
// App file modes, see test-app/tools/appfiles-test-mode.init.gradle
469+
if (project.hasProperty("loadCodeFromApk")) {
470+
arguments.add("-PloadCodeFromApk")
471+
}
472+
if (project.hasProperty("storeCodeUncompressed")) {
473+
arguments.add("-PstoreCodeUncompressed")
474+
}
468475
if (project.hasProperty("abis")) {
469476
arguments.add("-Pabis=${project.property('abis')}")
470477
}

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ require('./tests/testImportMetaResolution');
117117
require('./tests/testWorkerEsmEntry');
118118
require('./tests/testCodeCache');
119119
require('./tests/testAppFileLayers');
120+
require('./tests/testLoadCodeFromApk');
120121
// Worker wrapper reachability across GC (strong while running, collectable after)
121122
require('./tests/testWorkerLifetime');
122123
// Java->JS calls route to the runtime that created the object

‎test-app/app/src/main/assets/app/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"suppressCallJSMethodExceptions": false,
1212
"enableLineBreakpoints": false,
1313
"enableMultithreadedJavascript": true,
14-
"codeCache": true
14+
"codeCache": true,
15+
"alwaysExtract": ["tests/alwaysExtracted/**"]
1516
},
1617
"discardUncaughtJsExceptions": false,
1718
"security": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.value = 'kept';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 'alias';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.value = 'apk';
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Runs in both modes; the suite is run once as built and once with
2+
// -PloadCodeFromApk (see tools/appfiles-test-mode.init.gradle).
3+
describe("App files", function () {
4+
var File = java.io.File;
5+
var loadsCodeFromApk = com.tns.AppFiles.loadsCodeFromApk();
6+
var filesRoot = __approot;
7+
var appRoot = filesRoot + "/app";
8+
9+
function otherSpelling(path) {
10+
if (path.indexOf("/data/data/") === 0) {
11+
return "/data/user/0/" + path.substring("/data/data/".length);
12+
}
13+
if (path.indexOf("/data/user/0/") === 0) {
14+
return "/data/data/" + path.substring("/data/user/0/".length);
15+
}
16+
return path;
17+
}
18+
19+
function loadInWorker(path, callback) {
20+
var worker = new Worker("./codeCacheLoadWorker.js");
21+
worker.onmessage = function (msg) {
22+
worker.terminate();
23+
callback(msg.data);
24+
};
25+
worker.postMessage({ path: path, isModule: false });
26+
}
27+
28+
describe("under either spelling of the files directory", function () {
29+
var aliasRoot = otherSpelling(filesRoot);
30+
31+
it("has two spellings to test on this device", function () {
32+
expect(aliasRoot).not.toBe(filesRoot);
33+
});
34+
35+
it("finds runtime files", function () {
36+
expect(com.tns.AppFiles.isFile(aliasRoot + "/internal/livesync.js")).toBe(true);
37+
expect(com.tns.AppFiles.isFile(aliasRoot + "/app/tests/appFileLayers/target.js")).toBe(true);
38+
});
39+
40+
it("runs a script named through getFilesDir()", function () {
41+
var context = com.tns.NativeScriptApplication.getInstance();
42+
var previous = global.__onLiveSync;
43+
var calls = 0;
44+
global.__onLiveSync = function () {
45+
calls++;
46+
};
47+
try {
48+
com.tns.Runtime.getCurrentRuntime().runScript(new File(context.getFilesDir(), "internal/livesync.js"));
49+
com.tns.Runtime.getCurrentRuntime().runScript(new File(aliasRoot, "internal/livesync.js"));
50+
} finally {
51+
global.__onLiveSync = previous;
52+
}
53+
expect(calls).toBe(2);
54+
});
55+
56+
it("requires one CommonJS module", function () {
57+
var viaAlias = require(aliasRoot + "/app/tests/appFileLayers/target.js");
58+
var viaRoot = require(appRoot + "/tests/appFileLayers/target.js");
59+
expect(viaAlias).toBe(viaRoot);
60+
expect(viaAlias.filename).toBe(appRoot + "/tests/appFileLayers/target.js");
61+
});
62+
63+
it("imports one ES module", function (done) {
64+
Promise.all([
65+
import(aliasRoot + "/app/tests/loadCodeFromApk/aliasTarget.mjs"),
66+
import(appRoot + "/tests/loadCodeFromApk/aliasTarget.mjs")
67+
]).then(function (namespaces) {
68+
expect(namespaces[0].value).toBe("alias");
69+
expect(namespaces[0]).toBe(namespaces[1]);
70+
done();
71+
}, function (e) {
72+
expect(String(e)).toBe("");
73+
done();
74+
});
75+
});
76+
});
77+
78+
it("extracts what android.alwaysExtract names", function () {
79+
expect(new File(appRoot + "/tests/alwaysExtracted/kept.js").isFile()).toBe(true);
80+
expect(require("./alwaysExtracted/kept.js").value).toBe("kept");
81+
});
82+
83+
if (!loadsCodeFromApk) {
84+
it("extracts code when android.loadCodeFromApk is off", function () {
85+
expect(new File(appRoot + "/mainpage.js").isFile()).toBe(true);
86+
expect(new File(filesRoot + "/internal/ts_helpers.js").isFile()).toBe(true);
87+
expect(new File(filesRoot + "/metadata/treeNodeStream.dat").isFile()).toBe(true);
88+
});
89+
return;
90+
}
91+
92+
describe("with android.loadCodeFromApk", function () {
93+
function countFiles(dir, extensions) {
94+
var count = 0;
95+
var children = dir.listFiles();
96+
for (var i = 0; children && i < children.length; i++) {
97+
var child = children[i];
98+
if (child.isDirectory()) {
99+
count += countFiles(child, extensions);
100+
} else if (extensions.test(child.getName())) {
101+
count++;
102+
}
103+
}
104+
return count;
105+
}
106+
107+
it("serves code from the APK without extracting it", function () {
108+
expect(countFiles(new File(appRoot + "/tests"), /\.(js|mjs|cjs|map)$/)).toBe(
109+
// the one alwaysExtract match
110+
1);
111+
expect(new File(appRoot + "/mainpage.js").exists()).toBe(false);
112+
expect(com.tns.AppFiles.isFile(appRoot + "/mainpage.js")).toBe(true);
113+
expect(require("./appFileLayers/target.js").value).toBe("target");
114+
});
115+
116+
it("still extracts JSON, images and other files", function () {
117+
expect(new File(appRoot + "/package.json").isFile()).toBe(true);
118+
expect(new File(appRoot + "/tests/image.jpg").isFile()).toBe(true);
119+
});
120+
121+
it("serves internal files and metadata from the APK, removing extracted copies", function () {
122+
expect(new File(filesRoot + "/internal").exists()).toBe(false);
123+
expect(new File(filesRoot + "/metadata").exists()).toBe(false);
124+
expect(com.tns.AppFiles.isFile(filesRoot + "/internal/ts_helpers.js")).toBe(true);
125+
expect(com.tns.AppFiles.isFile(filesRoot + "/metadata/treeNodeStream.dat")).toBe(true);
126+
});
127+
128+
it("loads APK-served code in a worker", function (done) {
129+
loadInWorker(appRoot + "/tests/loadCodeFromApk/liveSyncOverride.js", function (result) {
130+
expect(result.error).toBeUndefined();
131+
expect(result.value).toBe("apk");
132+
done();
133+
});
134+
});
135+
136+
it("lets a file LiveSync writes into files/app win over the APK copy", function (done) {
137+
var target = new File(appRoot + "/tests/loadCodeFromApk/liveSyncOverride.js");
138+
var runtime = com.tns.Runtime.getCurrentRuntime();
139+
target.getParentFile().mkdirs();
140+
runtime.lock();
141+
try {
142+
var out = new java.io.FileOutputStream(target);
143+
out.write(new java.lang.String("module.exports.value = 'disk';\n").getBytes("UTF-8"));
144+
out.close();
145+
} finally {
146+
runtime.unlock();
147+
}
148+
loadInWorker(target.getAbsolutePath(), function (overridden) {
149+
target.delete();
150+
loadInWorker(target.getAbsolutePath(), function (restored) {
151+
expect(overridden.value).toBe("disk");
152+
expect(restored.value).toBe("apk");
153+
done();
154+
});
155+
});
156+
});
157+
});
158+
});

‎test-app/runtests.gradle‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def getBuildArguments = { ->
7575

7676
arguments.add("-PuseKotlin=true")
7777

78+
arguments += ["-I", "$rootDir/tools/appfiles-test-mode.init.gradle"]
79+
if (project.hasProperty("loadCodeFromApk")) {
80+
arguments.add("-PloadCodeFromApk")
81+
}
82+
if (project.hasProperty("storeCodeUncompressed")) {
83+
arguments.add("-PstoreCodeUncompressed")
84+
}
85+
7886
return arguments
7987
}
8088

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Test-only. Builds the test app as if its app/package.json set the app file
2+
// flags, without editing the source package.json:
3+
// -PloadCodeFromApk sets android.loadCodeFromApk in the packaged app/package.json
4+
// -PstoreCodeUncompressed also stores code uncompressed, using the suffix list app/build.gradle exports
5+
// Always applied by runtests.gradle, so switching modes reruns the patched tasks.
6+
7+
def loadCodeFromApk = gradle.startParameter.projectProperties.containsKey("loadCodeFromApk")
8+
def storeCodeUncompressed = gradle.startParameter.projectProperties.containsKey("storeCodeUncompressed")
9+
10+
gradle.beforeProject { project ->
11+
if (project.path != ":app") {
12+
return
13+
}
14+
15+
project.plugins.withId("com.android.application") {
16+
project.androidComponents.finalizeDsl { android ->
17+
if (storeCodeUncompressed) {
18+
android.androidResources.noCompress.addAll(project.ext.nsUncompressedCodeSuffixes)
19+
}
20+
}
21+
}
22+
23+
project.tasks.matching { it.name ==~ /merge\w*Assets/ }.configureEach { task ->
24+
task.inputs.property("nsTestLoadCodeFromApk", loadCodeFromApk)
25+
task.inputs.property("nsTestStoreCodeUncompressed", storeCodeUncompressed)
26+
if (!loadCodeFromApk && !storeCodeUncompressed) {
27+
return
28+
}
29+
task.doLast {
30+
def packageJson = new File(task.outputDir.get().asFile, "app/package.json")
31+
def json = new groovy.json.JsonSlurper().parseText(packageJson.getText("UTF-8"))
32+
json.android = json.android ?: [:]
33+
if (loadCodeFromApk) {
34+
json.android.loadCodeFromApk = true
35+
}
36+
if (storeCodeUncompressed) {
37+
json.android.storeCodeUncompressed = true
38+
}
39+
packageJson.setText(groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(json)), "UTF-8")
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)