buildSrc/src/main/kotlin/AndroidTests.kt
changeset 0 0a13dcabf7d3
child 22 d0190e5504a6
equal deleted inserted replaced
-1:000000000000 0:0a13dcabf7d3
       
     1 /*
       
     2  * AboutOss is a utility library to retrieve and display
       
     3  * opensource licenses in Android applications.
       
     4  *
       
     5  * Copyright (C) 2023 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of AboutOss.
       
     8  *
       
     9  * AboutOss is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * AboutOss is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.build
       
    23 
       
    24 import com.android.build.api.dsl.CommonExtension
       
    25 import com.android.build.api.dsl.DefaultConfig
       
    26 import com.android.build.gradle.BaseExtension
       
    27 import com.android.build.gradle.internal.dsl.TestOptions
       
    28 import org.gradle.api.Project
       
    29 import org.gradle.api.artifacts.Dependency
       
    30 import org.gradle.api.artifacts.DependencyConstraint
       
    31 import org.gradle.api.artifacts.ExternalModuleDependency
       
    32 import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
       
    33 import org.gradle.api.artifacts.dsl.DependencyHandler
       
    34 import org.gradle.kotlin.dsl.closureOf
       
    35 import org.gradle.kotlin.dsl.configure
       
    36 import org.gradle.kotlin.dsl.dependencies
       
    37 import org.gradle.kotlin.dsl.kotlin
       
    38 
       
    39 const val espressoVersion = "3.5.0-alpha07" // alpha for this bug https://github.com/robolectric/robolectric/issues/6593
       
    40 const val androidxTestRunnerVersion = "1.4.0"
       
    41 const val androidxTestCoreVersion = "1.4.0"
       
    42 const val robolectricVersion = "4.8.2"
       
    43 
       
    44 private typealias BaseExtension = CommonExtension<*, *, DefaultConfig, *>
       
    45 
       
    46 /*
       
    47  * Configuration for espresso and robolectric usage in an Android project
       
    48  */
       
    49 @Suppress("UnstableApiUsage")
       
    50 internal fun Project.configureTests() {
       
    51     extensions.configure<BaseExtension>("android") {
       
    52         defaultConfig {
       
    53             testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
       
    54             testInstrumentationRunnerArguments += mapOf(
       
    55                 "clearPackageData" to "true",
       
    56                 "disableAnalytics" to "true"
       
    57             )
       
    58         }
       
    59 
       
    60         testOptions {
       
    61             execution = "ANDROIDX_TEST_ORCHESTRATOR"
       
    62             animationsDisabled = true
       
    63 
       
    64             unitTests {
       
    65                 isIncludeAndroidResources = true
       
    66             }
       
    67         }
       
    68     }
       
    69 
       
    70 
       
    71     dependencies {
       
    72         dualTestImplementation(kotlin("test-junit"))
       
    73 
       
    74         androidTestUtil("androidx.test:orchestrator:$androidxTestRunnerVersion")
       
    75         androidTestImplementation("androidx.test:runner:$androidxTestRunnerVersion")
       
    76         dualTestImplementation("androidx.test.ext:junit-ktx:1.1.1")
       
    77 
       
    78         dualTestImplementation("androidx.test:core-ktx:$androidxTestCoreVersion")
       
    79         dualTestImplementation("androidx.test:rules:$androidxTestRunnerVersion")
       
    80         // fragment testing is usually declared on debugImplementation configuration and need these dependencies
       
    81         constraints {
       
    82             debugImplementation("androidx.test:core:$androidxTestCoreVersion")
       
    83             debugImplementation("androidx.test:monitor:$androidxTestRunnerVersion")
       
    84         }
       
    85 
       
    86         dualTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion")
       
    87         dualTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion")
       
    88         dualTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion")
       
    89 
       
    90         // assertions
       
    91         dualTestImplementation("com.google.truth:truth:1.0")
       
    92         dualTestImplementation("androidx.test.ext:truth:1.3.0-alpha01")
       
    93 
       
    94         // mock
       
    95         testImplementation("io.mockk:mockk:1.13.2")
       
    96         androidTestImplementation("io.mockk:mockk-android:1.13.2")
       
    97         testImplementation("org.robolectric:robolectric:$robolectricVersion")
       
    98 
       
    99         constraints {
       
   100             dualTestImplementation(kotlin("reflect")) {
       
   101                 because("Use the kotlin version that we use")
       
   102             }
       
   103             androidTestImplementation("org.objenesis:objenesis") {
       
   104                 because("3.x version use instructions only available with minSdk 26 (Android O)")
       
   105                 version {
       
   106                     strictly("2.6")
       
   107                 }
       
   108             }
       
   109         }
       
   110     }
       
   111 }
       
   112 
       
   113 fun DependencyHandler.dualTestImplementation(dependencyNotation: Any) {
       
   114     add("androidTestImplementation", dependencyNotation)
       
   115     add("testImplementation", dependencyNotation)
       
   116 }
       
   117 
       
   118 fun DependencyHandler.dualTestImplementation(dependencyNotation: Any, action: ExternalModuleDependency.() -> Unit) {
       
   119     val closure = closureOf(action)
       
   120     add("androidTestImplementation", dependencyNotation, closure)
       
   121     add("testImplementation", dependencyNotation, closure)
       
   122 }
       
   123 
       
   124 internal fun DependencyHandler.androidTestImplementation(dependencyNotation: Any): Dependency? =
       
   125     add("androidTestImplementation", dependencyNotation)
       
   126 
       
   127 internal fun DependencyHandler.androidTestImplementation(dependencyNotation: Any, action: ExternalModuleDependency.() -> Unit) {
       
   128     val closure = closureOf(action)
       
   129     add("androidTestImplementation", dependencyNotation, closure)
       
   130 }
       
   131 
       
   132 internal fun DependencyHandler.androidTestUtil(dependencyNotation: Any): Dependency? =
       
   133     add("androidTestUtil", dependencyNotation)
       
   134 
       
   135 internal fun DependencyHandler.testImplementation(dependencyNotation: Any): Dependency? =
       
   136     add("testImplementation", dependencyNotation)
       
   137 
       
   138 internal fun DependencyConstraintHandler.debugImplementation(dependencyConstraintNotation: Any): DependencyConstraint? =
       
   139     add("debugImplementation", dependencyConstraintNotation)