| Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 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 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "testing" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | func 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 | { | 
| Colin Cross | e302687 | 2019-01-05 22:30:13 -0800 | [diff] [blame] | 86 | name: "java test", | 
|  | 87 | bp: ` | 
|  | 88 | java_test { | 
|  | 89 | name: "foo", | 
|  | 90 | srcs: ["a.java"], | 
|  | 91 | }`, | 
|  | 92 | enabled: false, | 
|  | 93 | }, | 
|  | 94 | { | 
| Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 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 | }, | 
| Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 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 | }, | 
| Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | for _, test := range tests { | 
|  | 125 | t.Run(test.name, func(t *testing.T) { | 
|  | 126 | ctx := testJava(t, test.bp) | 
|  | 127 |  | 
|  | 128 | dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeDescription("dexpreopt") | 
|  | 129 | enabled := dexpreopt.Rule != nil | 
|  | 130 |  | 
|  | 131 | if enabled != test.enabled { | 
|  | 132 | t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled)) | 
|  | 133 | } | 
|  | 134 | }) | 
|  | 135 |  | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | func enabledString(enabled bool) string { | 
|  | 140 | if enabled { | 
|  | 141 | return "enabled" | 
|  | 142 | } else { | 
|  | 143 | return "disabled" | 
|  | 144 | } | 
|  | 145 | } |