• 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	"testing"
19)
20
21func TestDexpreoptEnabled(t *testing.T) {
22	tests := []struct {
23		name    string
24		bp      string
25		enabled bool
26	}{
27		{
28			name: "app",
29			bp: `
30				android_app {
31					name: "foo",
32					srcs: ["a.java"],
33				}`,
34			enabled: true,
35		},
36		{
37			name: "installable java library",
38			bp: `
39				java_library {
40					name: "foo",
41					installable: true,
42					srcs: ["a.java"],
43				}`,
44			enabled: true,
45		},
46		{
47			name: "java binary",
48			bp: `
49				java_binary {
50					name: "foo",
51					srcs: ["a.java"],
52				}`,
53			enabled: true,
54		},
55
56		{
57			name: "app without sources",
58			bp: `
59				android_app {
60					name: "foo",
61				}`,
62			// TODO(ccross): this should probably be false
63			enabled: true,
64		},
65		{
66			name: "installable java library without sources",
67			bp: `
68				java_library {
69					name: "foo",
70					installable: true,
71				}`,
72			// TODO(ccross): this should probably be false
73			enabled: true,
74		},
75
76		{
77			name: "static java library",
78			bp: `
79				java_library {
80					name: "foo",
81					srcs: ["a.java"],
82				}`,
83			enabled: false,
84		},
85		{
86			name: "java test",
87			bp: `
88				java_test {
89					name: "foo",
90					srcs: ["a.java"],
91				}`,
92			enabled: false,
93		},
94		{
95			name: "android test",
96			bp: `
97				android_test {
98					name: "foo",
99					srcs: ["a.java"],
100				}`,
101			enabled: false,
102		},
103		{
104			name: "android test helper app",
105			bp: `
106				android_test_helper_app {
107					name: "foo",
108					srcs: ["a.java"],
109				}`,
110			enabled: false,
111		},
112		{
113			name: "compile_dex",
114			bp: `
115				java_library {
116					name: "foo",
117					srcs: ["a.java"],
118					compile_dex: true,
119				}`,
120			enabled: false,
121		},
122		{
123			name: "dex_import",
124			bp: `
125				dex_import {
126					name: "foo",
127					jars: ["a.jar"],
128				}`,
129			enabled: true,
130		},
131	}
132
133	for _, test := range tests {
134		t.Run(test.name, func(t *testing.T) {
135			ctx := testJava(t, test.bp)
136
137			dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeDescription("dexpreopt")
138			enabled := dexpreopt.Rule != nil
139
140			if enabled != test.enabled {
141				t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled))
142			}
143		})
144
145	}
146}
147
148func enabledString(enabled bool) string {
149	if enabled {
150		return "enabled"
151	} else {
152		return "disabled"
153	}
154}
155