Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame^] | 1 | // Copyright 2020 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 | "android/soong/android" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func testConfigWithBootJars(bp string, bootJars []string) android.Config { |
| 24 | config := testConfig(nil, bp, nil) |
| 25 | config.TestProductVariables.BootJars = bootJars |
| 26 | return config |
| 27 | } |
| 28 | |
| 29 | func testContextWithHiddenAPI() *android.TestContext { |
| 30 | ctx := testContext() |
| 31 | ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory) |
| 32 | return ctx |
| 33 | } |
| 34 | |
| 35 | func testHiddenAPI(t *testing.T, bp string, bootJars []string) (*android.TestContext, android.Config) { |
| 36 | t.Helper() |
| 37 | |
| 38 | config := testConfigWithBootJars(bp, bootJars) |
| 39 | ctx := testContextWithHiddenAPI() |
| 40 | |
| 41 | run(t, ctx, config) |
| 42 | |
| 43 | return ctx, config |
| 44 | } |
| 45 | |
| 46 | func TestHiddenAPISingleton(t *testing.T) { |
| 47 | ctx, _ := testHiddenAPI(t, ` |
| 48 | java_library { |
| 49 | name: "foo", |
| 50 | srcs: ["a.java"], |
| 51 | compile_dex: true, |
| 52 | } |
| 53 | `, []string{":foo"}) |
| 54 | |
| 55 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 56 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 57 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 58 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 59 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { |
| 64 | ctx, _ := testHiddenAPI(t, ` |
| 65 | java_import { |
| 66 | name: "foo", |
| 67 | jars: ["a.jar"], |
| 68 | compile_dex: true, |
| 69 | } |
| 70 | `, []string{":foo"}) |
| 71 | |
| 72 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 73 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 74 | want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 75 | if !strings.Contains(hiddenapiRule.RuleParams.Command, want) { |
| 76 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
| 81 | ctx, _ := testHiddenAPI(t, ` |
| 82 | java_library { |
| 83 | name: "foo", |
| 84 | srcs: ["a.java"], |
| 85 | compile_dex: true, |
| 86 | } |
| 87 | |
| 88 | java_import { |
| 89 | name: "foo", |
| 90 | jars: ["a.jar"], |
| 91 | compile_dex: true, |
| 92 | prefer: false, |
| 93 | } |
| 94 | `, []string{":foo"}) |
| 95 | |
| 96 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 97 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 98 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 99 | if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 100 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 101 | } |
| 102 | |
| 103 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar" |
| 104 | if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 105 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
| 110 | ctx, _ := testHiddenAPI(t, ` |
| 111 | java_library { |
| 112 | name: "foo", |
| 113 | srcs: ["a.java"], |
| 114 | compile_dex: true, |
| 115 | } |
| 116 | |
| 117 | java_import { |
| 118 | name: "foo", |
| 119 | jars: ["a.jar"], |
| 120 | compile_dex: true, |
| 121 | prefer: true, |
| 122 | } |
| 123 | `, []string{":foo"}) |
| 124 | |
| 125 | hiddenAPI := ctx.SingletonForTests("hiddenapi") |
| 126 | hiddenapiRule := hiddenAPI.Rule("hiddenapi") |
| 127 | prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
| 128 | if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) { |
| 129 | t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command) |
| 130 | } |
| 131 | |
| 132 | fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar" |
| 133 | if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) { |
| 134 | t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command) |
| 135 | } |
| 136 | } |