aboutsummaryrefslogtreecommitdiffstats
path: root/ui/kotlinx-coroutines-android/test/R8ServiceLoaderOptimizationTest.kt
blob: 47beb85bbf7d7fedb10627834665b9e70eacabd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.android

import kotlinx.coroutines.*
import org.jf.dexlib2.*
import org.junit.Test
import java.io.*
import java.util.stream.*
import kotlin.test.*

class R8ServiceLoaderOptimizationTest : TestBase() {
    private val r8Dex = File(System.getProperty("dexPath")!!).asDexFile()
    private val r8DexNoOptim = File(System.getProperty("noOptimDexPath")!!).asDexFile()

    @Test
    fun testNoServiceLoaderCalls() {
        val serviceLoaderInvocations = r8Dex.types.any {
            it.type == "Ljava/util/ServiceLoader;"
        }
        assertEquals(
                false,
                serviceLoaderInvocations,
                "References to the ServiceLoader class were found in the resulting DEX."
        )
    }

    @Test
    fun testAndroidDispatcherIsKept() {
        val hasAndroidDispatcher = r8DexNoOptim.classes.any {
            it.type == "Lkotlinx/coroutines/android/AndroidDispatcherFactory;"
        }

        assertEquals(true, hasAndroidDispatcher)
    }

    @Test
    @Ignore
    fun testNoOptimRulesMatch() {
        val paths = listOf(
                "META-INF/com.android.tools/proguard/coroutines.pro",
                "META-INF/proguard/coroutines.pro",
                "META-INF/com.android.tools/r8-upto-1.6.0/coroutines.pro"
        )
        paths.associateWith { path ->
            val ruleSet = javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().lines().filter { line ->
                line.isNotBlank() && !line.startsWith("#")
            }.collect(Collectors.toSet())
            ruleSet
        }.asSequence().reduce { acc, entry ->
            assertEquals(
                    acc.value,
                    entry.value,
                    "Rule sets between ${acc.key} and ${entry.key} don't match."
            )
            entry
        }
    }
}

private fun File.asDexFile() = DexFileFactory.loadDexFile(this, null)