1/* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.tools.build.jetifier.processor.transform.proguard 18 19import org.junit.Test 20 21class ClassSpecTest_FieldsSelector { 22 23 @Test fun proGuard_fieldsSelector_minimal() { 24 ProGuardTester() 25 .forGivenPrefixes( 26 "support/" 27 ) 28 .forGivenTypesMap( 29 "support/Activity" to "test/Activity" 30 ) 31 .testThatGivenProGuard( 32 "-keep public class * extends support.Activity { \n" + 33 " <fields>; \n" + 34 "}" 35 ) 36 .rewritesTo( 37 "-keep public class * extends test.Activity { \n" + 38 " <fields>; \n" + 39 "}" 40 ) 41 } 42 43 @Test fun proGuard_fieldsSelector_modifiers() { 44 ProGuardTester() 45 .forGivenPrefixes( 46 ) 47 .forGivenTypesMap( 48 ) 49 .testThatGivenProGuard( 50 "-keep public class * { \n" + 51 " public <fields>; \n" + 52 " public static <fields>; \n" + 53 " !private !protected <fields>; \n" + 54 "}" 55 ) 56 .rewritesTo( 57 "-keep public class * { \n" + 58 " public <fields>; \n" + 59 " public static <fields>; \n" + 60 " !private !protected <fields>; \n" + 61 "}" 62 ) 63 } 64 65 @Test fun proGuard_fieldsSelector_modifiers_annotation() { 66 ProGuardTester() 67 .forGivenPrefixes( 68 "support/" 69 ) 70 .forGivenTypesMap( 71 "support/Annotation" to "test/Annotation" 72 ) 73 .testThatGivenProGuard( 74 "-keep public class * { \n" + 75 " @support.Annotation public <fields>; \n" + 76 " @support.Annotation public static <fields>; \n" + 77 " @support.Annotation !private !protected <fields>; \n" + 78 "}" 79 ) 80 .rewritesTo( 81 "-keep public class * { \n" + 82 " @test.Annotation public <fields>; \n" + 83 " @test.Annotation public static <fields>; \n" + 84 " @test.Annotation !private !protected <fields>; \n" + 85 "}" 86 ) 87 } 88 89 @Test fun proGuard_fieldsSelector_modifiers_annotation_spaces() { 90 ProGuardTester() 91 .forGivenPrefixes( 92 "support/" 93 ) 94 .forGivenTypesMap( 95 "support/Annotation" to "test/Annotation" 96 ) 97 .testThatGivenProGuard( 98 "-keep public class * { \n" + 99 " @support.Annotation public \t <fields> ; \n" + 100 "}" 101 ) 102 .rewritesTo( 103 "-keep public class * { \n" + 104 " @test.Annotation public \t <fields> ; \n" + 105 "}" 106 ) 107 } 108 109 @Test fun proGuard_fieldsSelector_multiple() { 110 ProGuardTester() 111 .forGivenPrefixes( 112 "support/" 113 ) 114 .forGivenProGuardMapSet("support.**" to setOf("support.**", "androidx.**")) 115 .testThatGivenProGuard( 116 "-keep public class * extends support.** { \n" + 117 " <fields>; \n" + 118 "}" 119 ) 120 .rewritesTo( 121 "-keep public class * extends support.** { \n" + 122 " <fields>; \n" + 123 "}\n" + 124 "-keep public class * extends androidx.** { \n" + 125 " <fields>; \n" + 126 "}" 127 ) 128 } 129}