diff -r fef46dce2812 -r 831cffa9c991 buildSrc/src/main/kotlin/Avdl.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buildSrc/src/main/kotlin/Avdl.kt Fri May 08 21:33:19 2020 -0400 @@ -0,0 +1,129 @@ +/* + * Geekttrss is a RSS feed reader application on the Android Platform. + * + * Copyright (C) 2017-2020 by Frederic-Charles Barthelery. + * + * This file is part of Geekttrss. + * + * Geekttrss is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Geekttrss is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Geekttrss. If not, see . + */ +package com.geekorum.build + +import com.android.build.gradle.AppPlugin +import com.android.build.gradle.DynamicFeaturePlugin +import com.android.build.gradle.LibraryPlugin +import com.android.build.gradle.TestedExtension +import com.android.build.gradle.api.TestVariant +import com.geekorum.gradle.avdl.AvdlExtension +import com.geekorum.gradle.avdl.providers.flydroid.FlydroidPlugin +import com.geekorum.gradle.avdl.providers.flydroid.flydroid +import com.geekorum.gradle.avdl.tasks.LaunchDeviceTask +import com.geekorum.gradle.avdl.tasks.StopDeviceTask +import com.geekorum.gradle.avdl.tasks.orderForTask +import com.geekorum.gradle.avdl.tasks.registerAvdlDevicesTask +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.provider.Provider +import org.gradle.api.services.BuildService +import org.gradle.api.services.BuildServiceParameters +import org.gradle.api.tasks.TaskContainer +import org.gradle.api.tasks.TaskProvider +import org.gradle.kotlin.dsl.* + + +fun Project.configureAvdlDevices(flydroidUrl: String, flydroidKey: String) { + apply() + + val oneInstrumentedTestService = gradle.sharedServices.registerIfAbsent( + "oneInstrumentedTest", OneInstrumentedTestService::class.java) { + maxParallelUsages.set(1) + } + + rootProject.serializeInstrumentedTestTask(oneInstrumentedTestService) + + val android = the() + configure { + devices { + android.testVariants.all { + register("android-n-${project.path}-$baseName") { + setup = flydroid { + url = flydroidUrl + this.flydroidKey = flydroidKey + // android-q images fail, don't manage to start the tests + image = "android-n" + useTunnel = true + } + } + } + } + } + + tasks { + var lastStopTask: TaskProvider? = null + var lastTestTask: TaskProvider? = null + android.testVariants.all { + val (startTask, stopTask ) = + registerAvdlDevicesTaskForVariant(this, listOf("android-n-${project.path}-$baseName")) + listOf(startTask, stopTask).forEach { + it.configure { + usesService(oneInstrumentedTestService) + } + } + + lastStopTask?.let { + startTask.configure { + mustRunAfter(it) + } + } + lastTestTask?.let { + startTask.configure { + mustRunAfter(it) + } + } + lastStopTask = stopTask + lastTestTask = connectedInstrumentTestProvider + } + } +} + +private fun TaskContainer.registerAvdlDevicesTaskForVariant( + variant: TestVariant, devices: List +): Pair, TaskProvider> { + val tasks = + registerAvdlDevicesTask(variant.name, devices) + tasks.orderForTask(variant.connectedInstrumentTestProvider) + return tasks +} + + +private fun Project.serializeInstrumentedTestTask(oneInstrumentedTestService: Provider) { + fun Project.configureTestTasks() { + extensions.configure { + testVariants.all { + connectedInstrumentTestProvider.configure { + usesService(oneInstrumentedTestService) + } + } + } + } + + allprojects { + val project = this + plugins.withType { project.configureTestTasks() } + plugins.withType { project.configureTestTasks() } + plugins.withType { project.configureTestTasks() } + } +} + +abstract class OneInstrumentedTestService : BuildService