1// Copyright 2019 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	"reflect"
20	"strings"
21	"testing"
22)
23
24func TestDeviceForHost(t *testing.T) {
25	bp := `
26		java_library {
27			name: "device_module",
28			srcs: ["a.java"],
29			java_resources: ["java-res/a/a"],
30		}
31
32		java_import {
33			name: "device_import_module",
34			jars: ["a.jar"],
35		}
36
37		java_device_for_host {
38			name: "device_for_host_module",
39			libs: [
40				"device_module",
41				"device_import_module",
42			],
43		}
44
45		java_library_host {
46			name: "host_module",
47			srcs: ["b.java"],
48			java_resources: ["java-res/b/b"],
49			static_libs: ["device_for_host_module"],
50		}
51	`
52
53	ctx, config := testJava(t, bp)
54
55	deviceModule := ctx.ModuleForTests("device_module", "android_common")
56	deviceTurbineCombined := deviceModule.Output("turbine-combined/device_module.jar")
57	deviceJavac := deviceModule.Output("javac/device_module.jar")
58	deviceRes := deviceModule.Output("res/device_module.jar")
59
60	deviceImportModule := ctx.ModuleForTests("device_import_module", "android_common")
61	deviceImportCombined := deviceImportModule.Output("combined/device_import_module.jar")
62
63	hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
64	hostJavac := hostModule.Output("javac/host_module.jar")
65	hostRes := hostModule.Output("res/host_module.jar")
66	combined := hostModule.Output("combined/host_module.jar")
67	resCombined := hostModule.Output("res-combined/host_module.jar")
68
69	// check classpath of host module with dependency on device_for_host_module
70	expectedClasspath := "-classpath " + strings.Join(android.Paths{
71		deviceTurbineCombined.Output,
72		deviceImportCombined.Output,
73	}.Strings(), ":")
74
75	if hostJavac.Args["classpath"] != expectedClasspath {
76		t.Errorf("expected host_module javac classpath:\n%s\ngot:\n%s",
77			expectedClasspath, hostJavac.Args["classpath"])
78	}
79
80	// check host module merged with static dependency implementation jars from device_for_host module
81	expectedInputs := android.Paths{
82		hostJavac.Output,
83		deviceJavac.Output,
84		deviceImportCombined.Output,
85	}
86
87	if !reflect.DeepEqual(combined.Inputs, expectedInputs) {
88		t.Errorf("expected host_module combined inputs:\n%q\ngot:\n%q",
89			expectedInputs, combined.Inputs)
90	}
91
92	// check host module merged with static dependency resource jars from device_for_host module
93	expectedInputs = android.Paths{
94		hostRes.Output,
95		deviceRes.Output,
96	}
97
98	if !reflect.DeepEqual(resCombined.Inputs, expectedInputs) {
99		t.Errorf("expected host_module res combined inputs:\n%q\ngot:\n%q",
100			expectedInputs, resCombined.Inputs)
101	}
102}
103
104func TestHostForDevice(t *testing.T) {
105	bp := `
106		java_library_host {
107			name: "host_module",
108			srcs: ["a.java"],
109			java_resources: ["java-res/a/a"],
110		}
111
112		java_import_host {
113			name: "host_import_module",
114			jars: ["a.jar"],
115		}
116
117		java_host_for_device {
118			name: "host_for_device_module",
119			libs: [
120				"host_module",
121				"host_import_module",
122			],
123		}
124
125		java_library {
126			name: "device_module",
127			sdk_version: "core_platform",
128			srcs: ["b.java"],
129			java_resources: ["java-res/b/b"],
130			static_libs: ["host_for_device_module"],
131		}
132	`
133
134	ctx, config := testJava(t, bp)
135
136	hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
137	hostJavac := hostModule.Output("javac/host_module.jar")
138	hostRes := hostModule.Output("res/host_module.jar")
139
140	hostImportModule := ctx.ModuleForTests("host_import_module", config.BuildOSCommonTarget.String())
141	hostImportCombined := hostImportModule.Output("combined/host_import_module.jar")
142
143	deviceModule := ctx.ModuleForTests("device_module", "android_common")
144	deviceJavac := deviceModule.Output("javac/device_module.jar")
145	deviceRes := deviceModule.Output("res/device_module.jar")
146	combined := deviceModule.Output("combined/device_module.jar")
147	resCombined := deviceModule.Output("res-combined/device_module.jar")
148
149	// check classpath of device module with dependency on host_for_device_module
150	expectedClasspath := "-classpath " + strings.Join(android.Paths{
151		hostJavac.Output,
152		hostImportCombined.Output,
153	}.Strings(), ":")
154
155	if deviceJavac.Args["classpath"] != expectedClasspath {
156		t.Errorf("expected device_module javac classpath:\n%s\ngot:\n%s",
157			expectedClasspath, deviceJavac.Args["classpath"])
158	}
159
160	// check device module merged with static dependency implementation jars from host_for_device module
161	expectedInputs := android.Paths{
162		deviceJavac.Output,
163		hostJavac.Output,
164		hostImportCombined.Output,
165	}
166
167	if !reflect.DeepEqual(combined.Inputs, expectedInputs) {
168		t.Errorf("expected device_module combined inputs:\n%q\ngot:\n%q",
169			expectedInputs, combined.Inputs)
170	}
171
172	// check device module merged with static dependency resource jars from host_for_device module
173	expectedInputs = android.Paths{
174		deviceRes.Output,
175		hostRes.Output,
176	}
177
178	if !reflect.DeepEqual(resCombined.Inputs, expectedInputs) {
179		t.Errorf("expected device_module res combined inputs:\n%q\ngot:\n%q",
180			expectedInputs, resCombined.Inputs)
181	}
182}
183