1// Copyright 2021 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	"testing"
19
20	"android/soong/android"
21)
22
23func getModuleHeaderJarsAsRelativeToTopPaths(result *android.TestResult, moduleNames ...string) []string {
24	paths := []string{}
25	for _, moduleName := range moduleNames {
26		module := result.Module(moduleName, "android_common")
27		info := result.ModuleProvider(module, JavaInfoProvider).(JavaInfo)
28		paths = append(paths, info.HeaderJars.RelativeToTop().Strings()...)
29	}
30	return paths
31}
32
33var addSourceSystemModules = android.FixtureAddTextFile("source/Android.bp", `
34		java_system_modules {
35			name: "system-modules",
36			libs: ["system-module1", "system-module2"],
37		}
38		java_library {
39			name: "system-module1",
40			srcs: ["a.java"],
41			sdk_version: "none",
42			system_modules: "none",
43		}
44		java_library {
45			name: "system-module2",
46			srcs: ["b.java"],
47			sdk_version: "none",
48			system_modules: "none",
49		}
50`)
51
52func TestJavaSystemModules(t *testing.T) {
53	result := android.GroupFixturePreparers(prepareForJavaTest, addSourceSystemModules).RunTest(t)
54
55	// check the existence of the source module
56	sourceSystemModules := result.ModuleForTests("system-modules", "android_common")
57	sourceInputs := sourceSystemModules.Rule("jarsTosystemModules").Inputs
58
59	// The expected paths are the header jars from the source input modules.
60	expectedSourcePaths := getModuleHeaderJarsAsRelativeToTopPaths(result, "system-module1", "system-module2")
61	android.AssertArrayString(t, "source system modules inputs", expectedSourcePaths, sourceInputs.RelativeToTop().Strings())
62}
63
64var addPrebuiltSystemModules = android.FixtureAddTextFile("prebuilts/Android.bp", `
65		java_system_modules_import {
66			name: "system-modules",
67			libs: ["system-module1", "system-module2"],
68		}
69		java_import {
70			name: "system-module1",
71			jars: ["a.jar"],
72		}
73		java_import {
74			name: "system-module2",
75			jars: ["b.jar"],
76		}
77`)
78
79func TestJavaSystemModulesImport(t *testing.T) {
80	result := android.GroupFixturePreparers(prepareForJavaTest, addPrebuiltSystemModules).RunTest(t)
81
82	// check the existence of the renamed prebuilt module
83	prebuiltSystemModules := result.ModuleForTests("system-modules", "android_common")
84	prebuiltInputs := prebuiltSystemModules.Rule("jarsTosystemModules").Inputs
85
86	// The expected paths are the header jars from the renamed prebuilt input modules.
87	expectedPrebuiltPaths := getModuleHeaderJarsAsRelativeToTopPaths(result, "system-module1", "system-module2")
88	android.AssertArrayString(t, "renamed prebuilt system modules inputs", expectedPrebuiltPaths, prebuiltInputs.RelativeToTop().Strings())
89}
90
91func TestJavaSystemModulesMixSourceAndPrebuilt(t *testing.T) {
92	result := android.GroupFixturePreparers(
93		prepareForJavaTest,
94		addSourceSystemModules,
95		addPrebuiltSystemModules,
96	).RunTest(t)
97
98	// check the existence of the source module
99	sourceSystemModules := result.ModuleForTests("system-modules", "android_common")
100	sourceInputs := sourceSystemModules.Rule("jarsTosystemModules").Inputs
101
102	// The expected paths are the header jars from the source input modules.
103	expectedSourcePaths := getModuleHeaderJarsAsRelativeToTopPaths(result, "system-module1", "system-module2")
104	android.AssertArrayString(t, "source system modules inputs", expectedSourcePaths, sourceInputs.RelativeToTop().Strings())
105
106	// check the existence of the renamed prebuilt module
107	prebuiltSystemModules := result.ModuleForTests("prebuilt_system-modules", "android_common")
108	prebuiltInputs := prebuiltSystemModules.Rule("jarsTosystemModules").Inputs
109
110	// The expected paths are the header jars from the renamed prebuilt input modules.
111	expectedPrebuiltPaths := getModuleHeaderJarsAsRelativeToTopPaths(result, "prebuilt_system-module1", "prebuilt_system-module2")
112	android.AssertArrayString(t, "prebuilt system modules inputs", expectedPrebuiltPaths, prebuiltInputs.RelativeToTop().Strings())
113}
114