blob: 6ccc5c1b17ace906f5bb5751f29cd7b5cbabe148 [file] [log] [blame]
Colin Cross3d7c9822019-03-01 13:46:24 -08001// 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"
Colin Cross5e87f342024-04-11 15:28:18 -070019 "slices"
Colin Cross3d7c9822019-03-01 13:46:24 -080020 "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
Jaewoong Jungf9a04432019-07-17 11:15:09 -070053 ctx, config := testJava(t, bp)
Colin Cross3d7c9822019-03-01 13:46:24 -080054
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
Colin Cross0f7d2ef2019-10-16 11:03:10 -070063 hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
Colin Cross3d7c9822019-03-01 13:46:24 -080064 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
Colin Cross5e87f342024-04-11 15:28:18 -070087 if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
Colin Cross3d7c9822019-03-01 13:46:24 -080088 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
Colin Cross5e87f342024-04-11 15:28:18 -070098 if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
Colin Cross3d7c9822019-03-01 13:46:24 -080099 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",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100127 sdk_version: "core_platform",
Colin Cross3d7c9822019-03-01 13:46:24 -0800128 srcs: ["b.java"],
129 java_resources: ["java-res/b/b"],
130 static_libs: ["host_for_device_module"],
131 }
132 `
133
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700134 ctx, config := testJava(t, bp)
Colin Cross3d7c9822019-03-01 13:46:24 -0800135
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700136 hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
Colin Cross3d7c9822019-03-01 13:46:24 -0800137 hostJavac := hostModule.Output("javac/host_module.jar")
Colin Crossf06d8dc2023-07-18 22:11:07 -0700138 hostJavacHeader := hostModule.Output("javac-header/host_module.jar")
Colin Cross3d7c9822019-03-01 13:46:24 -0800139 hostRes := hostModule.Output("res/host_module.jar")
140
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700141 hostImportModule := ctx.ModuleForTests("host_import_module", config.BuildOSCommonTarget.String())
Colin Cross3d7c9822019-03-01 13:46:24 -0800142 hostImportCombined := hostImportModule.Output("combined/host_import_module.jar")
143
144 deviceModule := ctx.ModuleForTests("device_module", "android_common")
145 deviceJavac := deviceModule.Output("javac/device_module.jar")
146 deviceRes := deviceModule.Output("res/device_module.jar")
147 combined := deviceModule.Output("combined/device_module.jar")
148 resCombined := deviceModule.Output("res-combined/device_module.jar")
149
150 // check classpath of device module with dependency on host_for_device_module
151 expectedClasspath := "-classpath " + strings.Join(android.Paths{
Colin Crossf06d8dc2023-07-18 22:11:07 -0700152 hostJavacHeader.Output,
Colin Cross3d7c9822019-03-01 13:46:24 -0800153 hostImportCombined.Output,
154 }.Strings(), ":")
155
156 if deviceJavac.Args["classpath"] != expectedClasspath {
157 t.Errorf("expected device_module javac classpath:\n%s\ngot:\n%s",
158 expectedClasspath, deviceJavac.Args["classpath"])
159 }
160
161 // check device module merged with static dependency implementation jars from host_for_device module
162 expectedInputs := android.Paths{
163 deviceJavac.Output,
164 hostJavac.Output,
165 hostImportCombined.Output,
166 }
167
Colin Cross5e87f342024-04-11 15:28:18 -0700168 if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
Colin Cross3d7c9822019-03-01 13:46:24 -0800169 t.Errorf("expected device_module combined inputs:\n%q\ngot:\n%q",
170 expectedInputs, combined.Inputs)
171 }
172
173 // check device module merged with static dependency resource jars from host_for_device module
174 expectedInputs = android.Paths{
175 deviceRes.Output,
176 hostRes.Output,
177 }
178
Colin Cross5e87f342024-04-11 15:28:18 -0700179 if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
Colin Cross3d7c9822019-03-01 13:46:24 -0800180 t.Errorf("expected device_module res combined inputs:\n%q\ngot:\n%q",
181 expectedInputs, resCombined.Inputs)
182 }
183}