|
| 1 | +import { AndroidPluginBuildService } from "../../lib/services/android-plugin-build-service"; |
| 2 | +import { Yok } from "../../lib/common/yok"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import * as FsLib from "../../lib/common/file-system"; |
| 5 | +import * as path from "path"; |
| 6 | +import { ChildProcess } from "../../lib/common/child-process"; |
| 7 | +import { HostInfo } from "../../lib/common/host-info"; |
| 8 | +import { AndroidToolsInfo } from "../../lib/android-tools-info"; |
| 9 | +import { Logger } from "../../lib/common/logger"; |
| 10 | +import * as ErrorsLib from "../../lib/common/errors"; |
| 11 | +import temp = require("temp"); |
| 12 | +temp.track(); |
| 13 | + |
| 14 | +describe('androiPluginBuildService', () => { |
| 15 | + |
| 16 | + const createTestInjector = (): IInjector => { |
| 17 | + const testInjector = new Yok(); |
| 18 | + |
| 19 | + testInjector.register("fs", FsLib.FileSystem); |
| 20 | + testInjector.register("childProcess", ChildProcess); |
| 21 | + testInjector.register("hostInfo", HostInfo); |
| 22 | + testInjector.register("androidToolsInfo", AndroidToolsInfo); |
| 23 | + testInjector.register("logger", Logger); |
| 24 | + testInjector.register("errors", ErrorsLib); |
| 25 | + testInjector.register("options", {}); |
| 26 | + testInjector.register("config", {}); |
| 27 | + testInjector.register("staticConfig", {}); |
| 28 | + |
| 29 | + return testInjector; |
| 30 | + }; |
| 31 | + |
| 32 | + let testInjector: IInjector; |
| 33 | + let fs: IFileSystem; |
| 34 | + let androidBuildPluginService: AndroidPluginBuildService; |
| 35 | + let tempFolder: string; |
| 36 | + let pluginFolder: string; |
| 37 | + |
| 38 | + function setUpIncludeGradle() { |
| 39 | + fs = testInjector.resolve("fs"); |
| 40 | + pluginFolder = temp.mkdirSync("AndroidProjectPropertiesManager-temp"); |
| 41 | + |
| 42 | + const validIncludeGradleContent = `android { |
| 43 | + productFlavors { |
| 44 | + "nativescript-pro-ui" { |
| 45 | + dimension "nativescript-pro-ui" |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +
|
| 50 | +def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "23.3.0" |
| 51 | +
|
| 52 | +dependencies { |
| 53 | + compile "com.android.support:appcompat-v7:$supportVersion" |
| 54 | + compile "com.android.support:recyclerview-v7:$supportVersion" |
| 55 | + compile "com.android.support:design:$supportVersion" |
| 56 | +}`; |
| 57 | + |
| 58 | + fs.writeFile(path.join(pluginFolder, "include.gradle"), validIncludeGradleContent); |
| 59 | + } |
| 60 | + |
| 61 | + function setUpPluginNativeFolder(manifestFile: boolean, resFolder: boolean, assetsFolder: boolean) { |
| 62 | + fs = testInjector.resolve("fs"); |
| 63 | + tempFolder = temp.mkdirSync("AndroidProjectPropertiesManager"); |
| 64 | + pluginFolder = temp.mkdirSync("AndroidProjectPropertiesManager-temp"); |
| 65 | + |
| 66 | + const validAndroidManifestContent = `<?xml version="1.0" encoding="UTF-8"?> |
| 67 | +<manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| 68 | +</manifest>`; |
| 69 | + const validStringsXmlContent = `<?xml version="1.0" encoding="utf-8"?> |
| 70 | +<resources> |
| 71 | + <string |
| 72 | + name="string_name" |
| 73 | + >text_string</string> |
| 74 | +</resources>`; |
| 75 | + |
| 76 | + if (manifestFile) { |
| 77 | + fs.writeFile(path.join(tempFolder, "AndroidManifest.xml"), validAndroidManifestContent); |
| 78 | + } |
| 79 | + |
| 80 | + if (resFolder) { |
| 81 | + const valuesFolder = path.join(tempFolder, "res", "values"); |
| 82 | + fs.createDirectory(valuesFolder); |
| 83 | + fs.writeFile(path.join(valuesFolder, "strings.xml"), validStringsXmlContent); |
| 84 | + } |
| 85 | + |
| 86 | + if (assetsFolder) { |
| 87 | + const imagesFolder = path.join(tempFolder, "assets", "images"); |
| 88 | + fs.createDirectory(imagesFolder); |
| 89 | + fs.writeFile(path.join(imagesFolder, "myicon.png"), "123"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + before(() => { |
| 94 | + testInjector = createTestInjector(); |
| 95 | + androidBuildPluginService = testInjector.resolve<AndroidPluginBuildService>(AndroidPluginBuildService); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('builds aar', () => { |
| 99 | + |
| 100 | + it('if supported files are in plugin', async () => { |
| 101 | + setUpPluginNativeFolder(true, true, true); |
| 102 | + const config: IBuildOptions = { |
| 103 | + platformsAndroidDirPath: tempFolder, |
| 104 | + pluginName: "my-plugin", |
| 105 | + aarOutputDir: tempFolder, |
| 106 | + tempPluginDirPath: pluginFolder |
| 107 | + }; |
| 108 | + |
| 109 | + const expectedAarName = "my_plugin.aar"; |
| 110 | + await androidBuildPluginService.buildAar(config); |
| 111 | + const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName)); |
| 112 | + |
| 113 | + assert.equal(hasGeneratedAar, true); |
| 114 | + }); |
| 115 | + |
| 116 | + it('if android manifest is missing', async () => { |
| 117 | + setUpPluginNativeFolder(false, true, true); |
| 118 | + const config: IBuildOptions = { |
| 119 | + platformsAndroidDirPath: tempFolder, |
| 120 | + pluginName: "my-plugin", |
| 121 | + aarOutputDir: tempFolder, |
| 122 | + tempPluginDirPath: pluginFolder |
| 123 | + }; |
| 124 | + |
| 125 | + const expectedAarName = "my_plugin.aar"; |
| 126 | + await androidBuildPluginService.buildAar(config); |
| 127 | + const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName)); |
| 128 | + |
| 129 | + assert.equal(hasGeneratedAar, true); |
| 130 | + }); |
| 131 | + |
| 132 | + it('if there is only an android manifest file', async () => { |
| 133 | + setUpPluginNativeFolder(true, false, false); |
| 134 | + const config: IBuildOptions = { |
| 135 | + platformsAndroidDirPath: tempFolder, |
| 136 | + pluginName: "my-plugin", |
| 137 | + aarOutputDir: tempFolder, |
| 138 | + tempPluginDirPath: pluginFolder |
| 139 | + }; |
| 140 | + |
| 141 | + const expectedAarName = "my_plugin.aar"; |
| 142 | + await androidBuildPluginService.buildAar(config); |
| 143 | + const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName)); |
| 144 | + |
| 145 | + assert.equal(hasGeneratedAar, true); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe(`doesn't build aar `, () => { |
| 150 | + it('if there is only an android manifest file', async () => { |
| 151 | + setUpPluginNativeFolder(false, false, false); |
| 152 | + const config: IBuildOptions = { |
| 153 | + platformsAndroidDirPath: tempFolder, |
| 154 | + pluginName: "my-plugin", |
| 155 | + aarOutputDir: tempFolder, |
| 156 | + tempPluginDirPath: pluginFolder |
| 157 | + }; |
| 158 | + |
| 159 | + const expectedAarName = "my_plugin.aar"; |
| 160 | + await androidBuildPluginService.buildAar(config); |
| 161 | + const hasGeneratedAar = fs.exists(path.join(tempFolder, expectedAarName)); |
| 162 | + |
| 163 | + assert.equal(hasGeneratedAar, false); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe(`handles include.gradle`, () => { |
| 168 | + it('if there is a legacy include.gradle file', async () => { |
| 169 | + setUpIncludeGradle(); |
| 170 | + const config: IBuildOptions = { |
| 171 | + platformsAndroidDirPath: pluginFolder, |
| 172 | + pluginName: "my-plugin", |
| 173 | + aarOutputDir: pluginFolder, |
| 174 | + tempPluginDirPath: pluginFolder |
| 175 | + }; |
| 176 | + |
| 177 | + const includeGradleName = "include.gradle"; |
| 178 | + await androidBuildPluginService.migrateIncludeGradle(config); |
| 179 | + const includeGradleContent = fs.readText(path.join(pluginFolder, includeGradleName).toString()); |
| 180 | + const productFlavorsAreRemoved = includeGradleContent.indexOf("productFlavors") === -1; |
| 181 | + assert.equal(productFlavorsAreRemoved, true); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments