|
| 1 | +package com.tns.tests; |
| 2 | + |
| 3 | +import com.tns.AppFileLayer; |
| 4 | +import com.tns.AppFileLayers; |
| 5 | +import com.tns.AppFileLayersCustomizer; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +// Backs tests/testAppFileLayers.js; the layers answer only paths under tests/appFileLayers/. |
| 15 | +public class TestAppFileLayersCustomizer implements AppFileLayersCustomizer { |
| 16 | + static final String MAP_LAYER = "test-map"; |
| 17 | + static final String DIRECTORY_LAYER = "test-directory"; |
| 18 | + static final String ASSETS_LAYER = "test-assets"; |
| 19 | + |
| 20 | + @Override |
| 21 | + public int priority() { |
| 22 | + return 0; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void customize(AppFileLayers.Builder builder) { |
| 27 | + File dir = new File(builder.context().getNoBackupFilesDir(), "appFileLayersTest"); |
| 28 | + write(new File(dir, "tests/appFileLayers/fromDirectory.js"), "module.exports.value = 'directory';\n"); |
| 29 | + write(new File(dir, "tests/appFileLayers/fromDirectory.mjs"), "export const value = 'directory-esm';\n"); |
| 30 | + write(new File(dir, "tests/appFileLayers/whitedOut.js"), "module.exports.value = 'should be hidden';\n"); |
| 31 | + |
| 32 | + Map<String, AppFileLayer.Entry> entries = new HashMap<>(); |
| 33 | + entries.put("tests/appFileLayers/remapped.js", AppFileLayer.Entry.asset("app/tests/appFileLayers/target.js")); |
| 34 | + entries.put("tests/appFileLayers/whitedOut.js", AppFileLayer.Entry.whiteout()); |
| 35 | + |
| 36 | + builder.insertBelow(AppFileLayers.EXTRACTED, AppFileLayer.map(MAP_LAYER, entries)); |
| 37 | + builder.insertBelow(MAP_LAYER, AppFileLayer.directory(DIRECTORY_LAYER, dir)); |
| 38 | + builder.insertBelow(DIRECTORY_LAYER, AppFileLayer.apkAssets(ASSETS_LAYER, "layertest")); |
| 39 | + } |
| 40 | + |
| 41 | + private static void write(File file, String text) { |
| 42 | + file.getParentFile().mkdirs(); |
| 43 | + try (FileOutputStream out = new FileOutputStream(file)) { |
| 44 | + out.write(text.getBytes(StandardCharsets.UTF_8)); |
| 45 | + } catch (IOException e) { |
| 46 | + throw new RuntimeException(e); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments