| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | "testing" | 
|  | 20 | ) | 
|  | 21 |  | 
|  | 22 | func TestAarImportProducesJniPackages(t *testing.T) { | 
|  | 23 | ctx := android.GroupFixturePreparers( | 
|  | 24 | PrepareForTestWithJavaDefaultModules, | 
|  | 25 | ).RunTestWithBp(t, ` | 
|  | 26 | android_library_import { | 
|  | 27 | name: "aar-no-jni", | 
|  | 28 | aars: ["aary.aar"], | 
|  | 29 | } | 
|  | 30 | android_library_import { | 
|  | 31 | name: "aar-jni", | 
|  | 32 | aars: ["aary.aar"], | 
|  | 33 | extract_jni: true, | 
|  | 34 | }`) | 
|  | 35 |  | 
|  | 36 | testCases := []struct { | 
|  | 37 | name       string | 
|  | 38 | hasPackage bool | 
|  | 39 | }{ | 
|  | 40 | { | 
|  | 41 | name:       "aar-no-jni", | 
|  | 42 | hasPackage: false, | 
|  | 43 | }, | 
|  | 44 | { | 
|  | 45 | name:       "aar-jni", | 
|  | 46 | hasPackage: true, | 
|  | 47 | }, | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | for _, tc := range testCases { | 
|  | 51 | t.Run(tc.name, func(t *testing.T) { | 
|  | 52 | appMod := ctx.Module(tc.name, "android_common") | 
|  | 53 | appTestMod := ctx.ModuleForTests(tc.name, "android_common") | 
|  | 54 |  | 
| Colin Cross | 5a37718 | 2023-12-14 14:46:23 -0800 | [diff] [blame] | 55 | info, ok := android.SingletonModuleProvider(ctx, appMod, JniPackageProvider) | 
| Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 56 | if !ok { | 
|  | 57 | t.Errorf("expected android_library_import to have JniPackageProvider") | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | if !tc.hasPackage { | 
|  | 61 | if len(info.JniPackages) != 0 { | 
|  | 62 | t.Errorf("expected JniPackages to be empty, but got %v", info.JniPackages) | 
|  | 63 | } | 
|  | 64 | outputFile := "arm64-v8a_jni.zip" | 
|  | 65 | jniOutputLibZip := appTestMod.MaybeOutput(outputFile) | 
|  | 66 | if jniOutputLibZip.Rule != nil { | 
|  | 67 | t.Errorf("did not expect an output file, but found %v", outputFile) | 
|  | 68 | } | 
|  | 69 | return | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | if len(info.JniPackages) != 1 { | 
|  | 73 | t.Errorf("expected a single JniPackage, but got %v", info.JniPackages) | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | outputFile := info.JniPackages[0].String() | 
|  | 77 | jniOutputLibZip := appTestMod.Output(outputFile) | 
|  | 78 | if jniOutputLibZip.Rule == nil { | 
|  | 79 | t.Errorf("did not find output file %v", outputFile) | 
|  | 80 | } | 
|  | 81 | }) | 
|  | 82 | } | 
|  | 83 | } | 
| Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 84 |  | 
|  | 85 | func TestLibraryFlagsPackages(t *testing.T) { | 
|  | 86 | result := android.GroupFixturePreparers( | 
|  | 87 | prepareForJavaTest, | 
|  | 88 | ).RunTestWithBp(t, ` | 
|  | 89 | android_library { | 
|  | 90 | name: "foo", | 
|  | 91 | srcs: ["a.java"], | 
|  | 92 | sdk_version: "current", | 
|  | 93 | flags_packages: [ | 
|  | 94 | "bar", | 
|  | 95 | "baz", | 
|  | 96 | ], | 
|  | 97 | } | 
|  | 98 | aconfig_declarations { | 
|  | 99 | name: "bar", | 
|  | 100 | package: "com.example.package.bar", | 
|  | 101 | srcs: [ | 
|  | 102 | "bar.aconfig", | 
|  | 103 | ], | 
|  | 104 | } | 
|  | 105 | aconfig_declarations { | 
|  | 106 | name: "baz", | 
|  | 107 | package: "com.example.package.baz", | 
|  | 108 | srcs: [ | 
|  | 109 | "baz.aconfig", | 
|  | 110 | ], | 
|  | 111 | } | 
|  | 112 | `) | 
|  | 113 |  | 
|  | 114 | foo := result.ModuleForTests("foo", "android_common") | 
|  | 115 |  | 
|  | 116 | // android_library module depends on aconfig_declarations listed in flags_packages | 
|  | 117 | android.AssertBoolEquals(t, "foo expected to depend on bar", true, | 
|  | 118 | CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "bar")) | 
|  | 119 |  | 
|  | 120 | android.AssertBoolEquals(t, "foo expected to depend on baz", true, | 
|  | 121 | CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "baz")) | 
|  | 122 |  | 
|  | 123 | aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link") | 
|  | 124 | linkInFlags := aapt2LinkRule.Args["inFlags"] | 
|  | 125 | android.AssertStringDoesContain(t, | 
|  | 126 | "aapt2 link command expected to pass feature flags arguments", | 
|  | 127 | linkInFlags, | 
|  | 128 | "--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt", | 
|  | 129 | ) | 
|  | 130 | } | 
| Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | func TestAndroidLibraryOutputFilesRel(t *testing.T) { | 
|  | 133 | result := android.GroupFixturePreparers( | 
|  | 134 | PrepareForTestWithJavaDefaultModules, | 
|  | 135 | ).RunTestWithBp(t, ` | 
|  | 136 | android_library { | 
|  | 137 | name: "foo", | 
|  | 138 | srcs: ["a.java"], | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | android_library_import { | 
|  | 142 | name: "bar", | 
|  | 143 | aars: ["bar.aar"], | 
|  | 144 |  | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | android_library_import { | 
|  | 148 | name: "baz", | 
|  | 149 | aars: ["baz.aar"], | 
|  | 150 | static_libs: ["bar"], | 
|  | 151 | } | 
|  | 152 | `) | 
|  | 153 |  | 
|  | 154 | foo := result.ModuleForTests("foo", "android_common") | 
|  | 155 | bar := result.ModuleForTests("bar", "android_common") | 
|  | 156 | baz := result.ModuleForTests("baz", "android_common") | 
|  | 157 |  | 
|  | 158 | fooOutputPath := android.OutputFileForModule(android.PathContext(nil), foo.Module(), "") | 
|  | 159 | barOutputPath := android.OutputFileForModule(android.PathContext(nil), bar.Module(), "") | 
|  | 160 | bazOutputPath := android.OutputFileForModule(android.PathContext(nil), baz.Module(), "") | 
|  | 161 |  | 
|  | 162 | android.AssertPathRelativeToTopEquals(t, "foo output path", | 
|  | 163 | "out/soong/.intermediates/foo/android_common/javac/foo.jar", fooOutputPath) | 
|  | 164 | android.AssertPathRelativeToTopEquals(t, "bar output path", | 
|  | 165 | "out/soong/.intermediates/bar/android_common/aar/bar.jar", barOutputPath) | 
|  | 166 | android.AssertPathRelativeToTopEquals(t, "baz output path", | 
|  | 167 | "out/soong/.intermediates/baz/android_common/combined/baz.jar", bazOutputPath) | 
|  | 168 |  | 
|  | 169 | android.AssertStringEquals(t, "foo relative output path", | 
|  | 170 | "foo.jar", fooOutputPath.Rel()) | 
|  | 171 | android.AssertStringEquals(t, "bar relative output path", | 
|  | 172 | "bar.jar", barOutputPath.Rel()) | 
|  | 173 | android.AssertStringEquals(t, "baz relative output path", | 
|  | 174 | "baz.jar", bazOutputPath.Rel()) | 
|  | 175 | } |