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 ( |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 18 | "testing" |
Yu Liu | e916a2c | 2024-03-20 19:47:28 +0000 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func 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 | |
Colin Cross | 5a37718 | 2023-12-14 14:46:23 -0800 | [diff] [blame] | 56 | info, ok := android.SingletonModuleProvider(ctx, appMod, JniPackageProvider) |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 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 | } |
Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 85 | |
| 86 | func 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", |
Yu Liu | e916a2c | 2024-03-20 19:47:28 +0000 | [diff] [blame] | 102 | container: "com.android.foo", |
Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 103 | srcs: [ |
| 104 | "bar.aconfig", |
| 105 | ], |
| 106 | } |
| 107 | aconfig_declarations { |
| 108 | name: "baz", |
| 109 | package: "com.example.package.baz", |
Yu Liu | e916a2c | 2024-03-20 19:47:28 +0000 | [diff] [blame] | 110 | container: "com.android.foo", |
Jihoon Kang | 9049c27 | 2024-03-19 21:57:36 +0000 | [diff] [blame] | 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 | } |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 134 | |
| 135 | func TestAndroidLibraryOutputFilesRel(t *testing.T) { |
| 136 | result := android.GroupFixturePreparers( |
| 137 | PrepareForTestWithJavaDefaultModules, |
| 138 | ).RunTestWithBp(t, ` |
| 139 | android_library { |
| 140 | name: "foo", |
| 141 | srcs: ["a.java"], |
Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 142 | java_resources: ["foo.txt"], |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | android_library_import { |
| 146 | name: "bar", |
Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 147 | aars: ["bar_prebuilt.aar"], |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 148 | |
| 149 | } |
| 150 | |
| 151 | android_library_import { |
| 152 | name: "baz", |
Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 153 | aars: ["baz_prebuilt.aar"], |
| 154 | static_libs: ["foo", "bar"], |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 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", |
Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 167 | "out/soong/.intermediates/foo/android_common/withres/foo.jar", fooOutputPath) |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 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", |
Colin Cross | 607bbd6 | 2024-04-12 13:44:45 -0700 | [diff] [blame] | 171 | "out/soong/.intermediates/baz/android_common/withres/baz.jar", bazOutputPath) |
Colin Cross | 28ac2ff | 2024-04-02 12:21:34 -0700 | [diff] [blame] | 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 | } |