blob: 1808290ee4020e1148594a3220d4d5f9755c5ec7 [file] [log] [blame]
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001// Copyright 2023 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 aconfig
16
17import (
18 "strings"
19 "testing"
20
21 "android/soong/android"
22 "android/soong/java"
23)
24
25// Note: These tests cover the code in the java package. It'd be ideal of that code could
26// be in the aconfig package.
27
28// With the bp parameter that defines a my_module, make sure it has the LOCAL_ACONFIG_FILES entries
29func runJavaAndroidMkTest(t *testing.T, bp string) {
30 result := android.GroupFixturePreparers(
31 PrepareForTestWithAconfigBuildComponents,
32 java.PrepareForTestWithJavaDefaultModules).
33 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
34 RunTestWithBp(t, bp+`
35 aconfig_declarations {
36 name: "my_aconfig_declarations",
37 package: "com.example.package",
38 srcs: ["foo.aconfig"],
39 }
40
41 java_aconfig_library {
42 name: "my_java_aconfig_library",
43 aconfig_declarations: "my_aconfig_declarations",
44 }
45 `)
46
47 module := result.ModuleForTests("my_module", "android_common").Module()
48
49 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
50
51 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
52 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
53 if !strings.HasSuffix(makeVar[0], "intermediate.pb") {
54 t.Errorf("LOCAL_ACONFIG_FILES should end with /intermediates.pb, instead it is: %s", makeVar[0])
55 }
56}
57
58func TestAndroidMkJavaLibrary(t *testing.T) {
59 bp := `
60 java_library {
61 name: "my_module",
62 srcs: [
63 "src/foo.java",
64 ],
65 static_libs: [
66 "my_java_aconfig_library",
67 ],
68 platform_apis: true,
69 }
70 `
71
72 runJavaAndroidMkTest(t, bp)
73}
74
75func TestAndroidMkAndroidApp(t *testing.T) {
76 bp := `
77 android_app {
78 name: "my_module",
79 srcs: [
80 "src/foo.java",
81 ],
82 static_libs: [
83 "my_java_aconfig_library",
84 ],
85 platform_apis: true,
86 }
87 `
88
89 runJavaAndroidMkTest(t, bp)
90}
91
92func TestAndroidMkBinary(t *testing.T) {
93 bp := `
94 java_binary {
95 name: "my_module",
96 srcs: [
97 "src/foo.java",
98 ],
99 static_libs: [
100 "my_java_aconfig_library",
101 ],
102 platform_apis: true,
103 main_class: "foo",
104 }
105 `
106
107 runJavaAndroidMkTest(t, bp)
108}
109
110func TestAndroidMkAndroidLibrary(t *testing.T) {
111 bp := `
112 android_library {
113 name: "my_module",
114 srcs: [
115 "src/foo.java",
116 ],
117 static_libs: [
118 "my_java_aconfig_library",
119 ],
120 platform_apis: true,
121 }
122 `
123
124 runJavaAndroidMkTest(t, bp)
125}
126
127func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) {
128 // Tests AndroidLibrary's propagation of flags through JavaInfo
129 bp := `
130 android_library {
131 name: "some_library",
132 srcs: [
133 "src/foo.java",
134 ],
135 static_libs: [
136 "my_java_aconfig_library",
137 ],
138 platform_apis: true,
139 }
140 java_binary {
141 name: "my_module",
142 srcs: [
143 "src/bar.java",
144 ],
145 static_libs: [
146 "some_library",
147 ],
148 platform_apis: true,
149 main_class: "foo",
150 }
151 `
152
153 runJavaAndroidMkTest(t, bp)
154}