xref: /aosp_15_r20/build/soong/java/container_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 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	"android/soong/android"
19	"fmt"
20	"testing"
21)
22
23var checkContainerMatch = func(t *testing.T, name string, container string, expected bool, actual bool) {
24	errorMessage := fmt.Sprintf("module %s container %s value differ", name, container)
25	android.AssertBoolEquals(t, errorMessage, expected, actual)
26}
27
28func TestJavaContainersModuleProperties(t *testing.T) {
29	result := android.GroupFixturePreparers(
30		prepareForJavaTest,
31	).RunTestWithBp(t, `
32		java_library {
33			name: "foo",
34			srcs: ["A.java"],
35		}
36		java_library {
37			name: "foo_vendor",
38			srcs: ["A.java"],
39			vendor: true,
40			sdk_version: "current",
41		}
42		java_library {
43			name: "foo_soc_specific",
44			srcs: ["A.java"],
45			soc_specific: true,
46			sdk_version: "current",
47		}
48		java_library {
49			name: "foo_product_specific",
50			srcs: ["A.java"],
51			product_specific: true,
52			sdk_version: "current",
53		}
54		java_test {
55			name: "foo_cts_test",
56			srcs: ["A.java"],
57			test_suites: [
58				"cts",
59			],
60		}
61		java_test {
62			name: "foo_non_cts_test",
63			srcs: ["A.java"],
64			test_suites: [
65				"general-tests",
66			],
67		}
68		java_library {
69			name: "bar",
70			static_libs: [
71				"framework-minus-apex",
72			],
73		}
74		java_library {
75			name: "baz",
76			static_libs: [
77				"bar",
78			],
79		}
80	`)
81
82	testcases := []struct {
83		moduleName         string
84		isSystemContainer  bool
85		isVendorContainer  bool
86		isProductContainer bool
87		isCts              bool
88		isUnstable         bool
89	}{
90		{
91			moduleName:         "foo",
92			isSystemContainer:  true,
93			isVendorContainer:  false,
94			isProductContainer: false,
95			isCts:              false,
96			isUnstable:         false,
97		},
98		{
99			moduleName:         "foo_vendor",
100			isSystemContainer:  false,
101			isVendorContainer:  true,
102			isProductContainer: false,
103			isCts:              false,
104			isUnstable:         false,
105		},
106		{
107			moduleName:         "foo_soc_specific",
108			isSystemContainer:  false,
109			isVendorContainer:  true,
110			isProductContainer: false,
111			isCts:              false,
112			isUnstable:         false,
113		},
114		{
115			moduleName:         "foo_product_specific",
116			isSystemContainer:  false,
117			isVendorContainer:  false,
118			isProductContainer: true,
119			isCts:              false,
120			isUnstable:         false,
121		},
122		{
123			moduleName:         "foo_cts_test",
124			isSystemContainer:  false,
125			isVendorContainer:  false,
126			isProductContainer: false,
127			isCts:              true,
128			isUnstable:         false,
129		},
130		{
131			moduleName:         "foo_non_cts_test",
132			isSystemContainer:  false,
133			isVendorContainer:  false,
134			isProductContainer: false,
135			isCts:              false,
136			isUnstable:         false,
137		},
138		{
139			moduleName:         "bar",
140			isSystemContainer:  true,
141			isVendorContainer:  false,
142			isProductContainer: false,
143			isCts:              false,
144			isUnstable:         true,
145		},
146		{
147			moduleName:         "baz",
148			isSystemContainer:  true,
149			isVendorContainer:  false,
150			isProductContainer: false,
151			isCts:              false,
152			isUnstable:         true,
153		},
154	}
155
156	for _, c := range testcases {
157		m := result.ModuleForTests(c.moduleName, "android_common")
158		containers, _ := android.OtherModuleProvider(result.TestContext.OtherModuleProviderAdaptor(), m.Module(), android.ContainersInfoProvider)
159		belongingContainers := containers.BelongingContainers()
160		checkContainerMatch(t, c.moduleName, "system", c.isSystemContainer, android.InList(android.SystemContainer, belongingContainers))
161		checkContainerMatch(t, c.moduleName, "vendor", c.isVendorContainer, android.InList(android.VendorContainer, belongingContainers))
162		checkContainerMatch(t, c.moduleName, "product", c.isProductContainer, android.InList(android.ProductContainer, belongingContainers))
163		checkContainerMatch(t, c.moduleName, "cts", c.isCts, android.InList(android.CtsContainer, belongingContainers))
164		checkContainerMatch(t, c.moduleName, "unstable", c.isUnstable, android.InList(android.UnstableContainer, belongingContainers))
165	}
166}
167