xref: /aosp_15_r20/build/soong/java/jdeps_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2018 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	"reflect"
19	"testing"
20
21	"android/soong/android"
22)
23
24func TestCollectJavaLibraryPropertiesAddLibsDeps(t *testing.T) {
25	ctx, _ := testJava(t,
26		`
27		java_library {name: "Foo"}
28		java_library {name: "Bar"}
29		java_library {
30			name: "javalib",
31			libs: ["Foo", "Bar"],
32		}
33	`)
34	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
35	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
36
37	for _, expected := range []string{"Foo", "Bar"} {
38		if !android.InList(expected, dpInfo.Deps) {
39			t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected)
40		}
41	}
42}
43
44func TestCollectJavaLibraryPropertiesAddStaticLibsDeps(t *testing.T) {
45	ctx, _ := testJava(t,
46		`
47		java_library {name: "Foo"}
48		java_library {name: "Bar"}
49		java_library {
50			name: "javalib",
51			static_libs: ["Foo", "Bar"],
52		}
53	`)
54	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
55	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
56
57	for _, expected := range []string{"Foo", "Bar"} {
58		if !android.InList(expected, dpInfo.Deps) {
59			t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected)
60		}
61	}
62}
63
64func TestCollectJavaLibraryPropertiesAddScrs(t *testing.T) {
65	ctx, _ := testJava(t,
66		`
67		java_library {
68			name: "javalib",
69			srcs: ["Foo.java", "Bar.java"],
70		}
71	`)
72	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
73	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
74
75	expected := []string{"Foo.java", "Bar.java"}
76	if !reflect.DeepEqual(dpInfo.Srcs, expected) {
77		t.Errorf("Library.IDEInfo() Srcs = %v, want %v", dpInfo.Srcs, expected)
78	}
79}
80
81func TestCollectJavaLibraryPropertiesAddAidlIncludeDirs(t *testing.T) {
82	ctx, _ := testJava(t,
83		`
84		java_library {
85			name: "javalib",
86			aidl: {
87				include_dirs: ["Foo", "Bar"],
88			},
89		}
90	`)
91	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
92	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
93
94	expected := []string{"Foo", "Bar"}
95	if !reflect.DeepEqual(dpInfo.Aidl_include_dirs, expected) {
96		t.Errorf("Library.IDEInfo() Aidl_include_dirs = %v, want %v", dpInfo.Aidl_include_dirs, expected)
97	}
98}
99
100func TestCollectJavaLibraryWithJarJarRules(t *testing.T) {
101	ctx, _ := testJava(t,
102		`
103		java_library {
104			name: "javalib",
105			srcs: ["foo.java"],
106			jarjar_rules: "jarjar_rules.txt",
107		}
108	`)
109	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
110	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
111
112	android.AssertStringEquals(t, "IdeInfo.Srcs of repackaged library should not be empty", "foo.java", dpInfo.Srcs[0])
113	android.AssertStringEquals(t, "IdeInfo.Jar_rules of repackaged library should not be empty", "jarjar_rules.txt", dpInfo.Jarjar_rules[0])
114	if !android.SubstringInList(dpInfo.Jars, "soong/.intermediates/javalib/android_common/jarjar/turbine/javalib.jar") {
115		t.Errorf("IdeInfo.Jars of repackaged library should contain the output of jarjar-ing. All outputs: %v\n", dpInfo.Jars)
116	}
117}
118
119func TestCollectJavaLibraryLinkingAgainstVersionedSdk(t *testing.T) {
120	ctx := android.GroupFixturePreparers(
121		prepareForJavaTest,
122		FixtureWithPrebuiltApis(map[string][]string{
123			"29": {},
124		})).RunTestWithBp(t,
125		`
126		java_library {
127			name: "javalib",
128			srcs: ["foo.java"],
129			sdk_version: "29",
130		}
131	`)
132	module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
133	dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
134
135	android.AssertStringListContains(t, "IdeInfo.Deps should contain versioned sdk module", dpInfo.Deps, "sdk_public_29_android")
136}
137
138func TestDoNotAddNoneSystemModulesToDeps(t *testing.T) {
139	ctx := android.GroupFixturePreparers(
140		prepareForJavaTest,
141		android.FixtureMergeEnv(
142			map[string]string{
143				"DISABLE_STUB_VALIDATION": "true",
144			},
145		),
146	).RunTestWithBp(t,
147		`
148		java_library {
149			name: "javalib",
150			srcs: ["foo.java"],
151			sdk_version: "none",
152			system_modules: "none",
153		}
154
155		java_api_library {
156			name: "javalib.stubs",
157			stubs_type: "everything",
158			api_contributions: ["javalib-current.txt"],
159			api_surface: "public",
160			system_modules: "none",
161		}
162		java_api_contribution {
163			name: "javalib-current.txt",
164			api_file: "javalib-current.txt",
165			api_surface: "public",
166		}
167	`)
168	javalib := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
169	dpInfo, _ := android.OtherModuleProvider(ctx, javalib, android.IdeInfoProviderKey)
170	android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none")
171
172	javalib_stubs := ctx.ModuleForTests("javalib.stubs", "android_common").Module().(*ApiLibrary)
173	dpInfo, _ = android.OtherModuleProvider(ctx, javalib_stubs, android.IdeInfoProviderKey)
174	android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none")
175}
176