1 /* <lambda>null2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.android 6 7 import kotlinx.coroutines.* 8 import org.jf.dexlib2.* 9 import org.junit.Test 10 import java.io.* 11 import java.util.stream.* 12 import kotlin.test.* 13 14 class R8ServiceLoaderOptimizationTest : TestBase() { 15 private val r8Dex = File(System.getProperty("dexPath")!!).asDexFile() 16 private val r8DexNoOptim = File(System.getProperty("noOptimDexPath")!!).asDexFile() 17 18 @Test 19 fun testNoServiceLoaderCalls() { 20 val serviceLoaderInvocations = r8Dex.types.any { 21 it.type == "Ljava/util/ServiceLoader;" 22 } 23 assertEquals( 24 false, 25 serviceLoaderInvocations, 26 "References to the ServiceLoader class were found in the resulting DEX." 27 ) 28 } 29 30 @Test 31 fun testAndroidDispatcherIsKept() { 32 val hasAndroidDispatcher = r8DexNoOptim.classes.any { 33 it.type == "Lkotlinx/coroutines/android/AndroidDispatcherFactory;" 34 } 35 36 assertEquals(true, hasAndroidDispatcher) 37 } 38 39 @Test 40 @Ignore 41 fun testNoOptimRulesMatch() { 42 val paths = listOf( 43 "META-INF/com.android.tools/proguard/coroutines.pro", 44 "META-INF/proguard/coroutines.pro", 45 "META-INF/com.android.tools/r8-upto-1.6.0/coroutines.pro" 46 ) 47 paths.associateWith { path -> 48 val ruleSet = javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().lines().filter { line -> 49 line.isNotBlank() && !line.startsWith("#") 50 }.collect(Collectors.toSet()) 51 ruleSet 52 }.asSequence().reduce { acc, entry -> 53 assertEquals( 54 acc.value, 55 entry.value, 56 "Rule sets between ${acc.key} and ${entry.key} don't match." 57 ) 58 entry 59 } 60 } 61 } 62 asDexFilenull63private fun File.asDexFile() = DexFileFactory.loadDexFile(this, null) 64