buildSrc/src/main/kotlin/AndroidTests.kt
changeset 100 9ec927c219b0
parent 75 534a19e25217
equal deleted inserted replaced
99:641b0bfe5292 100:9ec927c219b0
    19  * You should have received a copy of the GNU General Public License
    19  * You should have received a copy of the GNU General Public License
    20  * along with Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
    20  * along with Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
    21  */
    21  */
    22 package com.geekorum.build
    22 package com.geekorum.build
    23 
    23 
       
    24 import com.android.build.api.dsl.ApplicationExtension
    24 import com.android.build.api.dsl.CommonExtension
    25 import com.android.build.api.dsl.CommonExtension
    25 import com.android.build.api.dsl.DefaultConfig
    26 import com.android.build.api.dsl.LibraryExtension
    26 import com.android.build.gradle.BaseExtension
       
    27 import com.android.build.gradle.internal.dsl.TestOptions
       
    28 import org.gradle.api.Project
    27 import org.gradle.api.Project
    29 import org.gradle.api.artifacts.Dependency
    28 import org.gradle.api.artifacts.Dependency
    30 import org.gradle.api.artifacts.DependencyConstraint
    29 import org.gradle.api.artifacts.DependencyConstraint
    31 import org.gradle.api.artifacts.ExternalModuleDependency
    30 import org.gradle.api.artifacts.ExternalModuleDependency
    32 import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
    31 import org.gradle.api.artifacts.dsl.DependencyConstraintHandler
    33 import org.gradle.api.artifacts.dsl.DependencyHandler
    32 import org.gradle.api.artifacts.dsl.DependencyHandler
    34 import org.gradle.kotlin.dsl.*
    33 import org.gradle.kotlin.dsl.*
    35 
    34 
    36 const val espressoVersion = "3.5.1"
    35 const val espressoVersion = "3.6.1"
    37 const val androidxTestRunnerVersion = "1.5.2"
    36 const val androidxTestRunnerVersion = "1.6.2"
    38 const val androidxTestCoreVersion = "1.5.0"
    37 const val androidxTestCoreVersion = "1.6.1"
    39 const val robolectricVersion = "4.10.2"
    38 const val robolectricVersion = "4.14.1"
    40 
    39 
    41 private typealias BaseExtension = CommonExtension<*, *, DefaultConfig, *, *, *>
       
    42 
    40 
    43 /*
    41 /*
    44  * Configuration for espresso and robolectric usage in an Android project
    42  * Configuration for espresso and robolectric usage in an Android project
    45  */
    43  */
    46 @Suppress("UnstableApiUsage")
       
    47 internal fun Project.configureTests() {
    44 internal fun Project.configureTests() {
    48     extensions.configure<BaseExtension>("android") {
    45     extensions.configure<CommonExtension>("android") {
    49         defaultConfig {
    46         if (this is ApplicationExtension) {
       
    47             defaultConfig {
    50             testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    48             testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    51             testInstrumentationRunnerArguments += mapOf(
    49                 testInstrumentationRunnerArguments += mapOf(
    52                 "clearPackageData" to "true",
    50                     "clearPackageData" to "true",
    53                 "disableAnalytics" to "true"
    51                     "disableAnalytics" to "true"
    54             )
    52                 )
       
    53             }
       
    54         }
       
    55         if (this is LibraryExtension) {
       
    56             defaultConfig {
       
    57             testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
       
    58                 testInstrumentationRunnerArguments += mapOf(
       
    59                     "clearPackageData" to "true",
       
    60                     "disableAnalytics" to "true"
       
    61                 )
       
    62             }
    55         }
    63         }
    56 
    64 
    57         testOptions {
    65         testOptions.apply {
    58             execution = "ANDROIDX_TEST_ORCHESTRATOR"
    66             execution = "ANDROIDX_TEST_ORCHESTRATOR"
    59             animationsDisabled = true
    67             animationsDisabled = true
    60 
    68 
    61             unitTests {
    69             unitTests {
    62                 isIncludeAndroidResources = true
    70                 isIncludeAndroidResources = true
    63             }
    71             }
    64         }
    72         }
    65     }
    73     }
    66 
    74 
    67 
       
    68     dependencies {
    75     dependencies {
    69         dualTestImplementation(kotlin("test-junit"))
    76         dualTestImplementation(kotlin("test-junit"))
    70 
    77 
    71         androidTestUtil("androidx.test:orchestrator:1.4.2")
    78         androidTestUtil("androidx.test:orchestrator:1.5.1")
    72         androidTestImplementation("androidx.test:runner:$androidxTestRunnerVersion")
    79         androidTestImplementation("androidx.test:runner:$androidxTestRunnerVersion")
    73         dualTestImplementation("androidx.test.ext:junit-ktx:1.1.1")
    80         dualTestImplementation("androidx.test.ext:junit-ktx:1.2.1")
    74 
    81 
    75         dualTestImplementation("androidx.test:core-ktx:$androidxTestCoreVersion")
    82         dualTestImplementation("androidx.test:core-ktx:$androidxTestCoreVersion")
    76         dualTestImplementation("androidx.test:rules:1.5.0")
    83         dualTestImplementation("androidx.test:rules:1.6.1")
       
    84 
    77         // fragment testing is usually declared on debugImplementation configuration and need these dependencies
    85         // fragment testing is usually declared on debugImplementation configuration and need these dependencies
    78         constraints {
    86         constraints {
    79             debugImplementation("androidx.test:core:$androidxTestCoreVersion")
    87             debugImplementation("androidx.test:core:$androidxTestCoreVersion")
    80             debugImplementation("androidx.test:monitor:$androidxTestRunnerVersion")
    88             debugImplementation("androidx.test:monitor:1.7.2")
    81         }
    89         }
    82 
    90 
    83         dualTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion")
    91         dualTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion")
    84         dualTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion") {
    92         dualTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion") {
    85             exclude("com.google.protobuf", "protobuf-lite")
    93             exclude("com.google.protobuf", "protobuf-lite")
    86         }
    94         }
    87         dualTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion")
    95         dualTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion")
    88 
    96 
    89         // assertions
    97         // assertions
    90         dualTestImplementation("com.google.truth:truth:1.0")
    98         dualTestImplementation("com.google.truth:truth:1.4.4")
    91         dualTestImplementation("androidx.test.ext:truth:1.3.0-alpha01")
    99         dualTestImplementation("androidx.test.ext:truth:1.6.0")
    92 
   100 
    93         // mock
   101         // mock
    94         testImplementation("io.mockk:mockk:1.13.5")
   102         testImplementation("io.mockk:mockk:1.13.8")
    95         androidTestImplementation("io.mockk:mockk-android:1.13.5")
   103         androidTestImplementation("io.mockk:mockk-android:1.13.8")
    96         testImplementation("org.robolectric:robolectric:$robolectricVersion")
   104         testImplementation("org.robolectric:robolectric:$robolectricVersion")
    97 
   105 
    98         constraints {
   106         constraints {
    99             dualTestImplementation(kotlin("reflect")) {
   107             dualTestImplementation(kotlin("reflect")) {
   100                 because("Use the kotlin version that we use")
   108                 because("Use the kotlin version that we use")