|
| 1 | +import { APP_ID, APP_NAME, MappedFS, makeAppDir, run, installPlatform } from './util'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { mkdirp, writeFile } from 'fs-extra'; |
| 4 | +import { runCommand } from '../src/util/subprocess'; |
| 5 | + |
| 6 | +const CAPACITOR_PLUGIN_ID = 'capacitor-resource-plugin'; |
| 7 | +const CAPACITOR_PLUGIN_JS = ` |
| 8 | +module.exports = { |
| 9 | + echo: function(options) { |
| 10 | + return Promise.resolve(options); |
| 11 | + } |
| 12 | +}; |
| 13 | +`; |
| 14 | + |
| 15 | +const CAPACITOR_PLUGIN_XML = ` |
| 16 | +<?xml version="1.0" encoding="UTF-8"?> |
| 17 | +<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" |
| 18 | + id="${CAPACITOR_PLUGIN_ID}" |
| 19 | + version="1.0.0"> |
| 20 | + <name>Capacitor Resource Plugin</name> |
| 21 | + <platform name="android"> |
| 22 | + <resource-file src="android/src/main/res/xml/automotive_app_desc.xml" target="res/xml/automotive_app_desc.xml" /> |
| 23 | + </platform> |
| 24 | +</plugin> |
| 25 | +`; |
| 26 | + |
| 27 | +const CAPACITOR_PLUGIN_PACKAGE = ` |
| 28 | +{ |
| 29 | + "name": "${CAPACITOR_PLUGIN_ID}", |
| 30 | + "version": "1.0.0", |
| 31 | + "description": "Capacitor Resource Plugin", |
| 32 | + "capacitor": { |
| 33 | + "android": { |
| 34 | + "src": "android" |
| 35 | + } |
| 36 | + }, |
| 37 | + "main": "plugin.js" |
| 38 | +} |
| 39 | +`; |
| 40 | + |
| 41 | +const AUTOMOTIVE_APP_DESC = ` |
| 42 | +<?xml version="1.0" encoding="utf-8"?> |
| 43 | +<automotiveApp> |
| 44 | + <uses name="media"/> |
| 45 | +</automotiveApp> |
| 46 | +`; |
| 47 | + |
| 48 | +async function makeCapacitorPlugin(pluginPath: string) { |
| 49 | + const androidPath = join(pluginPath, 'android/src/main'); |
| 50 | + const resPath = join(androidPath, 'res/xml'); |
| 51 | + await mkdirp(resPath); |
| 52 | + await writeFile(join(pluginPath, 'plugin.js'), CAPACITOR_PLUGIN_JS); |
| 53 | + await writeFile(join(pluginPath, 'plugin.xml'), CAPACITOR_PLUGIN_XML); |
| 54 | + await writeFile(join(pluginPath, 'package.json'), CAPACITOR_PLUGIN_PACKAGE); |
| 55 | + await writeFile(join(resPath, 'automotive_app_desc.xml'), AUTOMOTIVE_APP_DESC); |
| 56 | + |
| 57 | + // Create dummy android project structure for the plugin so it looks valid |
| 58 | + await writeFile(join(androidPath, 'AndroidManifest.xml'), '<manifest></manifest>'); |
| 59 | +} |
| 60 | + |
| 61 | +describe('Update: Android Resources', () => { |
| 62 | + let appDirObj: any; |
| 63 | + let appDir: string; |
| 64 | + let FS: MappedFS; |
| 65 | + |
| 66 | + beforeAll(async () => { |
| 67 | + jest.setTimeout(150000); |
| 68 | + appDirObj = await makeAppDir(); |
| 69 | + appDir = appDirObj.appDir; |
| 70 | + |
| 71 | + // Create and install custom capacitor plugin |
| 72 | + const pluginPath = join(appDirObj.path, CAPACITOR_PLUGIN_ID); |
| 73 | + await makeCapacitorPlugin(pluginPath); |
| 74 | + |
| 75 | + await runCommand('npm', ['install', '--save', pluginPath], { |
| 76 | + cwd: appDir, |
| 77 | + }); |
| 78 | + |
| 79 | + await run(appDir, `init "${APP_NAME}" "${APP_ID}"`); |
| 80 | + await installPlatform(appDir, 'android'); |
| 81 | + await run(appDir, `add android`); |
| 82 | + FS = new MappedFS(appDir); |
| 83 | + }); |
| 84 | + |
| 85 | + afterAll(() => { |
| 86 | + //appDirObj.cleanupCallback(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Should copy resource-file from Capacitor plugin', async () => { |
| 90 | + // Run update to trigger the copy |
| 91 | + await run(appDir, `update android`); |
| 92 | + |
| 93 | + const resourcePath = 'android/capacitor-cordova-android-plugins/src/main/res/xml/automotive_app_desc.xml'; |
| 94 | + const exists = await FS.exists(resourcePath); |
| 95 | + expect(exists).toBe(true); |
| 96 | + |
| 97 | + if (exists) { |
| 98 | + const content = await FS.read(resourcePath); |
| 99 | + expect(content).toContain('<uses name="media"/>'); |
| 100 | + } |
| 101 | + }); |
| 102 | +}); |
0 commit comments