• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 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	"fmt"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/cc"
23	"android/soong/dexpreopt"
24)
25
26func TestDexpreoptEnabled(t *testing.T) {
27	tests := []struct {
28		name    string
29		bp      string
30		enabled bool
31	}{
32		{
33			name: "app",
34			bp: `
35				android_app {
36					name: "foo",
37					srcs: ["a.java"],
38					sdk_version: "current",
39				}`,
40			enabled: true,
41		},
42		{
43			name: "installable java library",
44			bp: `
45				java_library {
46					name: "foo",
47					installable: true,
48					srcs: ["a.java"],
49				}`,
50			enabled: true,
51		},
52		{
53			name: "java binary",
54			bp: `
55				java_binary {
56					name: "foo",
57					srcs: ["a.java"],
58				}`,
59			enabled: true,
60		},
61		{
62			name: "app without sources",
63			bp: `
64				android_app {
65					name: "foo",
66					sdk_version: "current",
67				}`,
68			enabled: false,
69		},
70		{
71			name: "app with libraries",
72			bp: `
73				android_app {
74					name: "foo",
75					static_libs: ["lib"],
76					sdk_version: "current",
77				}
78
79				java_library {
80					name: "lib",
81					srcs: ["a.java"],
82					sdk_version: "current",
83				}`,
84			enabled: true,
85		},
86		{
87			name: "installable java library without sources",
88			bp: `
89				java_library {
90					name: "foo",
91					installable: true,
92				}`,
93			enabled: false,
94		},
95		{
96			name: "static java library",
97			bp: `
98				java_library {
99					name: "foo",
100					srcs: ["a.java"],
101				}`,
102			enabled: false,
103		},
104		{
105			name: "java test",
106			bp: `
107				java_test {
108					name: "foo",
109					srcs: ["a.java"],
110				}`,
111			enabled: false,
112		},
113		{
114			name: "android test",
115			bp: `
116				android_test {
117					name: "foo",
118					srcs: ["a.java"],
119				}`,
120			enabled: false,
121		},
122		{
123			name: "android test helper app",
124			bp: `
125				android_test_helper_app {
126					name: "foo",
127					srcs: ["a.java"],
128				}`,
129			enabled: false,
130		},
131		{
132			name: "compile_dex",
133			bp: `
134				java_library {
135					name: "foo",
136					srcs: ["a.java"],
137					compile_dex: true,
138				}`,
139			enabled: false,
140		},
141		{
142			name: "dex_import",
143			bp: `
144				dex_import {
145					name: "foo",
146					jars: ["a.jar"],
147				}`,
148			enabled: true,
149		},
150	}
151
152	for _, test := range tests {
153		t.Run(test.name, func(t *testing.T) {
154			ctx, _ := testJava(t, test.bp)
155
156			dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeRule("dexpreopt")
157			enabled := dexpreopt.Rule != nil
158
159			if enabled != test.enabled {
160				t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled))
161			}
162		})
163
164	}
165}
166
167func enabledString(enabled bool) string {
168	if enabled {
169		return "enabled"
170	} else {
171		return "disabled"
172	}
173}
174
175func TestDex2oatToolDeps(t *testing.T) {
176	if android.BuildOs != android.Linux {
177		// The host binary paths checked below are build OS dependent.
178		t.Skipf("Unsupported build OS %s", android.BuildOs)
179	}
180
181	preparers := android.GroupFixturePreparers(
182		cc.PrepareForTestWithCcDefaultModules,
183		PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd,
184		dexpreopt.PrepareForTestByEnablingDexpreopt)
185
186	testDex2oatToolDep := func(sourceEnabled, prebuiltEnabled, prebuiltPreferred bool,
187		expectedDex2oatPath string) {
188		name := fmt.Sprintf("sourceEnabled:%t,prebuiltEnabled:%t,prebuiltPreferred:%t",
189			sourceEnabled, prebuiltEnabled, prebuiltPreferred)
190		t.Run(name, func(t *testing.T) {
191			result := preparers.RunTestWithBp(t, fmt.Sprintf(`
192					cc_binary {
193						name: "dex2oatd",
194						enabled: %t,
195						host_supported: true,
196					}
197					cc_prebuilt_binary {
198						name: "dex2oatd",
199						enabled: %t,
200						prefer: %t,
201						host_supported: true,
202						srcs: ["x86_64/bin/dex2oatd"],
203					}
204					java_library {
205						name: "myjavalib",
206					}
207				`, sourceEnabled, prebuiltEnabled, prebuiltPreferred))
208			pathContext := android.PathContextForTesting(result.Config)
209			dex2oatPath := dexpreopt.GetCachedGlobalSoongConfig(pathContext).Dex2oat
210			android.AssertStringEquals(t, "Testing "+name, expectedDex2oatPath, android.NormalizePathForTesting(dex2oatPath))
211		})
212	}
213
214	sourceDex2oatPath := "host/linux-x86/bin/dex2oatd"
215	prebuiltDex2oatPath := ".intermediates/prebuilt_dex2oatd/linux_glibc_x86_64/dex2oatd"
216
217	testDex2oatToolDep(true, false, false, sourceDex2oatPath)
218	testDex2oatToolDep(true, true, false, sourceDex2oatPath)
219	testDex2oatToolDep(true, true, true, prebuiltDex2oatPath)
220	testDex2oatToolDep(false, true, false, prebuiltDex2oatPath)
221}
222