|
| 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 | +}); |
0 commit comments