1// Copyright 2022 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package java 16 17import ( 18 "testing" 19 20 "android/soong/android" 21) 22 23func TestAarImportProducesJniPackages(t *testing.T) { 24 ctx := android.GroupFixturePreparers( 25 PrepareForTestWithJavaDefaultModules, 26 ).RunTestWithBp(t, ` 27 android_library_import { 28 name: "aar-no-jni", 29 aars: ["aary.aar"], 30 } 31 android_library_import { 32 name: "aar-jni", 33 aars: ["aary.aar"], 34 extract_jni: true, 35 }`) 36 37 testCases := []struct { 38 name string 39 hasPackage bool 40 }{ 41 { 42 name: "aar-no-jni", 43 hasPackage: false, 44 }, 45 { 46 name: "aar-jni", 47 hasPackage: true, 48 }, 49 } 50 51 for _, tc := range testCases { 52 t.Run(tc.name, func(t *testing.T) { 53 appMod := ctx.Module(tc.name, "android_common") 54 appTestMod := ctx.ModuleForTests(tc.name, "android_common") 55 56 info, ok := android.SingletonModuleProvider(ctx, appMod, JniPackageProvider) 57 if !ok { 58 t.Errorf("expected android_library_import to have JniPackageProvider") 59 } 60 61 if !tc.hasPackage { 62 if len(info.JniPackages) != 0 { 63 t.Errorf("expected JniPackages to be empty, but got %v", info.JniPackages) 64 } 65 outputFile := "arm64-v8a_jni.zip" 66 jniOutputLibZip := appTestMod.MaybeOutput(outputFile) 67 if jniOutputLibZip.Rule != nil { 68 t.Errorf("did not expect an output file, but found %v", outputFile) 69 } 70 return 71 } 72 73 if len(info.JniPackages) != 1 { 74 t.Errorf("expected a single JniPackage, but got %v", info.JniPackages) 75 } 76 77 outputFile := info.JniPackages[0].String() 78 jniOutputLibZip := appTestMod.Output(outputFile) 79 if jniOutputLibZip.Rule == nil { 80 t.Errorf("did not find output file %v", outputFile) 81 } 82 }) 83 } 84} 85 86func TestLibraryFlagsPackages(t *testing.T) { 87 result := android.GroupFixturePreparers( 88 prepareForJavaTest, 89 ).RunTestWithBp(t, ` 90 android_library { 91 name: "foo", 92 srcs: ["a.java"], 93 sdk_version: "current", 94 flags_packages: [ 95 "bar", 96 "baz", 97 ], 98 } 99 aconfig_declarations { 100 name: "bar", 101 package: "com.example.package.bar", 102 container: "com.android.foo", 103 srcs: [ 104 "bar.aconfig", 105 ], 106 } 107 aconfig_declarations { 108 name: "baz", 109 package: "com.example.package.baz", 110 container: "com.android.foo", 111 srcs: [ 112 "baz.aconfig", 113 ], 114 } 115 `) 116 117 foo := result.ModuleForTests("foo", "android_common") 118 119 // android_library module depends on aconfig_declarations listed in flags_packages 120 android.AssertBoolEquals(t, "foo expected to depend on bar", true, 121 CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "bar")) 122 123 android.AssertBoolEquals(t, "foo expected to depend on baz", true, 124 CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "baz")) 125 126 aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link") 127 linkInFlags := aapt2LinkRule.Args["inFlags"] 128 android.AssertStringDoesContain(t, 129 "aapt2 link command expected to pass feature flags arguments", 130 linkInFlags, 131 "--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt", 132 ) 133} 134 135func TestAndroidLibraryOutputFilesRel(t *testing.T) { 136 result := android.GroupFixturePreparers( 137 PrepareForTestWithJavaDefaultModules, 138 ).RunTestWithBp(t, ` 139 android_library { 140 name: "foo", 141 srcs: ["a.java"], 142 java_resources: ["foo.txt"], 143 } 144 145 android_library_import { 146 name: "bar", 147 aars: ["bar_prebuilt.aar"], 148 149 } 150 151 android_library_import { 152 name: "baz", 153 aars: ["baz_prebuilt.aar"], 154 static_libs: ["foo", "bar"], 155 } 156 `) 157 158 foo := result.ModuleForTests("foo", "android_common") 159 bar := result.ModuleForTests("bar", "android_common") 160 baz := result.ModuleForTests("baz", "android_common") 161 162 fooOutputPath := android.OutputFileForModule(android.PathContext(nil), foo.Module(), "") 163 barOutputPath := android.OutputFileForModule(android.PathContext(nil), bar.Module(), "") 164 bazOutputPath := android.OutputFileForModule(android.PathContext(nil), baz.Module(), "") 165 166 android.AssertPathRelativeToTopEquals(t, "foo output path", 167 "out/soong/.intermediates/foo/android_common/withres/foo.jar", fooOutputPath) 168 android.AssertPathRelativeToTopEquals(t, "bar output path", 169 "out/soong/.intermediates/bar/android_common/aar/bar.jar", barOutputPath) 170 android.AssertPathRelativeToTopEquals(t, "baz output path", 171 "out/soong/.intermediates/baz/android_common/withres/baz.jar", bazOutputPath) 172 173 android.AssertStringEquals(t, "foo relative output path", 174 "foo.jar", fooOutputPath.Rel()) 175 android.AssertStringEquals(t, "bar relative output path", 176 "bar.jar", barOutputPath.Rel()) 177 android.AssertStringEquals(t, "baz relative output path", 178 "baz.jar", bazOutputPath.Rel()) 179} 180