Home | History | Annotate | Download | only in bytecode
      1 /*
      2  * Copyright 2017 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 
     17 package com.android.tools.build.jetifier.processor.transform.bytecode
     18 
     19 import com.android.tools.build.jetifier.core.config.Config
     20 import com.android.tools.build.jetifier.core.type.JavaType
     21 import com.android.tools.build.jetifier.core.type.TypesMap
     22 import com.android.tools.build.jetifier.processor.transform.TransformationContext
     23 import com.google.common.truth.Truth
     24 import org.junit.Test
     25 import org.objectweb.asm.ClassWriter
     26 
     27 class CoreRemapperImplTest {
     28 
     29     @Test
     30     fun remapString_shouldUseFallbackForField() {
     31         val remapper = prepareRemapper(
     32             TypesMap(mapOf(
     33                 JavaType.fromDotVersion("androidx.test.InputConnectionCompat")
     34                     to JavaType.fromDotVersion(
     35                     "android.support.test.InputConnectionCompat")
     36             )),
     37             "androidx/")
     38 
     39         val given = "androidx.test.InputConnectionCompat.CONTENT_URI"
     40         val expected = "android.support.test.InputConnectionCompat.CONTENT_URI"
     41 
     42         Truth.assertThat(remapper.rewriteString(given)).isEqualTo(expected)
     43     }
     44 
     45     private fun prepareRemapper(
     46         typesMap: TypesMap,
     47         restrictToPackagePrefix: String? = null
     48     ): CoreRemapperImpl {
     49         val prefixes = if (restrictToPackagePrefix == null) {
     50             emptySet()
     51         } else {
     52             setOf(restrictToPackagePrefix)
     53         }
     54 
     55         val config = Config.fromOptional(
     56             restrictToPackagePrefixes = prefixes,
     57             typesMap = typesMap)
     58 
     59         val context = TransformationContext(config, isInReversedMode = true)
     60 
     61         val writer = ClassWriter(0 /* flags */)
     62         return CoreRemapperImpl(context, writer)
     63     }
     64 }