blob: d471fb7d5638d1c349b4b8c103334c193312b646 [file] [log] [blame]
Jooyung Han12df5fb2019-07-11 16:18:47 +09001// 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 (
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070018 "reflect"
Anton Hansson78156ef2020-03-27 19:39:48 +000019 "strings"
Jooyung Han12df5fb2019-07-11 16:18:47 +090020 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070021
22 "android/soong/android"
Jooyung Han12df5fb2019-07-11 16:18:47 +090023)
24
Jooyung Han12df5fb2019-07-11 16:18:47 +090025func TestRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070026 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090027 java_library {
28 name: "foo",
29 srcs: ["a.java"],
30 required: ["libfoo"],
31 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070032 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090033
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070034 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park0b0e1b92019-12-03 13:24:29 +090035 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070036
37 expected := []string{"libfoo"}
38 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
39 if !reflect.DeepEqual(expected, actual) {
40 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
41 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090042}
43
44func TestHostdex(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070045 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090046 java_library {
47 name: "foo",
48 srcs: ["a.java"],
49 hostdex: true,
50 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070051 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090052
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070053 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +090054 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
55 if len(entriesList) != 2 {
56 t.Errorf("two entries are expected, but got %d", len(entriesList))
57 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070058
Jiyong Park55bd98b2019-12-11 17:27:07 +090059 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070060 expected := []string{"foo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090061 actual := mainEntries.EntryMap["LOCAL_MODULE"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070062 if !reflect.DeepEqual(expected, actual) {
63 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
64 }
65
Jiyong Park55bd98b2019-12-11 17:27:07 +090066 subEntries := &entriesList[1]
67 expected = []string{"foo-hostdex"}
68 actual = subEntries.EntryMap["LOCAL_MODULE"]
69 if !reflect.DeepEqual(expected, actual) {
70 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070071 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090072}
73
74func TestHostdexRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070075 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090076 java_library {
77 name: "foo",
78 srcs: ["a.java"],
79 hostdex: true,
80 required: ["libfoo"],
81 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070082 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090083
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070084 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +090085 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
86 if len(entriesList) != 2 {
87 t.Errorf("two entries are expected, but got %d", len(entriesList))
88 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070089
Jiyong Park55bd98b2019-12-11 17:27:07 +090090 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070091 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090092 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070093 if !reflect.DeepEqual(expected, actual) {
94 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
95 }
96
Jiyong Park55bd98b2019-12-11 17:27:07 +090097 subEntries := &entriesList[1]
98 expected = []string{"libfoo"}
99 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
100 if !reflect.DeepEqual(expected, actual) {
101 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700102 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900103}
104
105func TestHostdexSpecificRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700106 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900107 java_library {
108 name: "foo",
109 srcs: ["a.java"],
110 hostdex: true,
111 target: {
112 hostdex: {
113 required: ["libfoo"],
114 },
115 },
116 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700117 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900118
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700119 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +0900120 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
121 if len(entriesList) != 2 {
122 t.Errorf("two entries are expected, but got %d", len(entriesList))
123 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700124
Jiyong Park55bd98b2019-12-11 17:27:07 +0900125 mainEntries := &entriesList[0]
126 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700127 t.Errorf("Unexpected required modules: %q", r)
128 }
129
Jiyong Park55bd98b2019-12-11 17:27:07 +0900130 subEntries := &entriesList[1]
131 expected := []string{"libfoo"}
132 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
133 if !reflect.DeepEqual(expected, actual) {
134 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700135 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900136}
Anton Hansson78156ef2020-03-27 19:39:48 +0000137
138func TestDistWithTag(t *testing.T) {
139 ctx, config := testJava(t, `
140 java_library {
141 name: "foo_without_tag",
142 srcs: ["a.java"],
143 compile_dex: true,
144 dist: {
145 targets: ["hi"],
146 },
147 }
148 java_library {
149 name: "foo_with_tag",
150 srcs: ["a.java"],
151 compile_dex: true,
152 dist: {
153 targets: ["hi"],
154 tag: ".jar",
155 },
156 }
157 `)
158
159 without_tag_entries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module())
160 with_tag_entries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module())
161
162 if len(without_tag_entries) != 2 || len(with_tag_entries) != 2 {
163 t.Errorf("two mk entries per module expected, got %d and %d", len(without_tag_entries), len(with_tag_entries))
164 }
165 if !with_tag_entries[0].DistFile.Valid() || !strings.Contains(with_tag_entries[0].DistFile.String(), "/javac/foo_with_tag.jar") {
166 t.Errorf("expected classes.jar DistFile, got %v", with_tag_entries[0].DistFile)
167 }
168 if without_tag_entries[0].DistFile.Valid() {
169 t.Errorf("did not expect explicit DistFile, got %v", without_tag_entries[0].DistFile)
170 }
171}
Yo Chiang07d75072020-06-05 17:43:19 +0800172
173func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
174 ctx, config := testJava(t, `
175 java_sdk_library {
176 name: "foo-shared_library",
177 srcs: ["a.java"],
178 }
179 java_sdk_library {
180 name: "foo-no_shared_library",
181 srcs: ["a.java"],
182 shared_library: false,
183 }
184 `)
185
186 // Verify the existence of internal modules
187 ctx.ModuleForTests("foo-shared_library.xml", "android_common")
188
189 testCases := []struct {
190 moduleName string
191 expected []string
192 }{
193 {"foo-shared_library", []string{"foo-shared_library.xml"}},
194 {"foo-no_shared_library", nil},
195 }
196 for _, tc := range testCases {
197 mod := ctx.ModuleForTests(tc.moduleName, "android_common").Module()
198 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
199 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
200 if !reflect.DeepEqual(tc.expected, actual) {
201 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
202 }
203 }
204}