blob: 246c0eb07e7272607ecd57558dbe8171b34cdafd [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"
Jooyung Han12df5fb2019-07-11 16:18:47 +090019 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070020
21 "android/soong/android"
Jooyung Han12df5fb2019-07-11 16:18:47 +090022)
23
Jooyung Han12df5fb2019-07-11 16:18:47 +090024func TestRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070025 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090026 java_library {
27 name: "foo",
28 srcs: ["a.java"],
29 required: ["libfoo"],
30 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070031 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090032
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070033 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070034 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070035
36 expected := []string{"libfoo"}
37 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
38 if !reflect.DeepEqual(expected, actual) {
39 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
40 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090041}
42
43func TestHostdex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070044 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090045 java_library {
46 name: "foo",
47 srcs: ["a.java"],
48 hostdex: true,
49 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070050 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090051
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070052 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070053 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090054 if len(entriesList) != 2 {
55 t.Errorf("two entries are expected, but got %d", len(entriesList))
56 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070057
Jiyong Park55bd98b2019-12-11 17:27:07 +090058 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070059 expected := []string{"foo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090060 actual := mainEntries.EntryMap["LOCAL_MODULE"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070061 if !reflect.DeepEqual(expected, actual) {
62 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
63 }
64
Jiyong Park55bd98b2019-12-11 17:27:07 +090065 subEntries := &entriesList[1]
66 expected = []string{"foo-hostdex"}
67 actual = subEntries.EntryMap["LOCAL_MODULE"]
68 if !reflect.DeepEqual(expected, actual) {
69 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070070 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090071}
72
73func TestHostdexRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070074 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090075 java_library {
76 name: "foo",
77 srcs: ["a.java"],
78 hostdex: true,
79 required: ["libfoo"],
80 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070081 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090082
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070083 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070084 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090085 if len(entriesList) != 2 {
86 t.Errorf("two entries are expected, but got %d", len(entriesList))
87 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070088
Jiyong Park55bd98b2019-12-11 17:27:07 +090089 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070090 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090091 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070092 if !reflect.DeepEqual(expected, actual) {
93 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
94 }
95
Jiyong Park55bd98b2019-12-11 17:27:07 +090096 subEntries := &entriesList[1]
97 expected = []string{"libfoo"}
98 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
99 if !reflect.DeepEqual(expected, actual) {
100 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700101 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900102}
103
104func TestHostdexSpecificRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700105 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900106 java_library {
107 name: "foo",
108 srcs: ["a.java"],
109 hostdex: true,
110 target: {
111 hostdex: {
112 required: ["libfoo"],
113 },
114 },
115 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700116 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900117
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700118 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -0700119 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +0900120 if len(entriesList) != 2 {
121 t.Errorf("two entries are expected, but got %d", len(entriesList))
122 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700123
Jiyong Park55bd98b2019-12-11 17:27:07 +0900124 mainEntries := &entriesList[0]
125 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700126 t.Errorf("Unexpected required modules: %q", r)
127 }
128
Jiyong Park55bd98b2019-12-11 17:27:07 +0900129 subEntries := &entriesList[1]
130 expected := []string{"libfoo"}
131 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
132 if !reflect.DeepEqual(expected, actual) {
133 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700134 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900135}
Anton Hansson78156ef2020-03-27 19:39:48 +0000136
Yo Chiang07d75072020-06-05 17:43:19 +0800137func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000138 result := android.GroupFixturePreparers(
139 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000140 PrepareForTestWithJavaSdkLibraryFiles,
141 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
142 ).RunTestWithBp(t, `
Yo Chiang07d75072020-06-05 17:43:19 +0800143 java_sdk_library {
144 name: "foo-shared_library",
145 srcs: ["a.java"],
146 }
147 java_sdk_library {
148 name: "foo-no_shared_library",
149 srcs: ["a.java"],
150 shared_library: false,
151 }
152 `)
153
154 // Verify the existence of internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +0000155 result.ModuleForTests("foo-shared_library.xml", "android_common")
Yo Chiang07d75072020-06-05 17:43:19 +0800156
157 testCases := []struct {
158 moduleName string
159 expected []string
160 }{
161 {"foo-shared_library", []string{"foo-shared_library.xml"}},
162 {"foo-no_shared_library", nil},
163 }
164 for _, tc := range testCases {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000165 mod := result.ModuleForTests(tc.moduleName, "android_common").Module()
166 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Yo Chiang07d75072020-06-05 17:43:19 +0800167 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
168 if !reflect.DeepEqual(tc.expected, actual) {
169 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
170 }
171 }
172}
Bill Peckhamfb04df42021-01-11 12:27:24 -0800173
174func TestImportSoongDexJar(t *testing.T) {
Paul Duffinf04311c2021-03-22 17:31:51 +0000175 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
Bill Peckhamfb04df42021-01-11 12:27:24 -0800176 java_import {
177 name: "my-java-import",
178 jars: ["a.jar"],
179 prefer: true,
180 compile_dex: true,
181 }
182 `)
183
Paul Duffinf04311c2021-03-22 17:31:51 +0000184 mod := result.Module("my-java-import", "android_common")
185 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
186 expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
Bill Peckhamfb04df42021-01-11 12:27:24 -0800187 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
188
Paul Duffinf04311c2021-03-22 17:31:51 +0000189 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
Bill Peckhamfb04df42021-01-11 12:27:24 -0800190}
Yuntao Xu7a318552021-05-27 10:30:26 -0700191
192func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
193 ctx, _ := testJava(t, `
194 android_test_helper_app {
195 name: "foo",
196 srcs: ["a.java"],
197 }
198 `)
199
200 mod := ctx.ModuleForTests("foo", "android_common").Module()
201 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
202
203 expected := []string{"true"}
204 actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
205 if !reflect.DeepEqual(expected, actual) {
206 t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
207 }
208}