|
1 /* |
|
2 * Geekttrss is a RSS feed reader application on the Android Platform. |
|
3 * |
|
4 * Copyright (C) 2017-2020 by Frederic-Charles Barthelery. |
|
5 * |
|
6 * This file is part of Geekttrss. |
|
7 * |
|
8 * Geekttrss is free software: you can redistribute it and/or modify |
|
9 * it under the terms of the GNU General Public License as published by |
|
10 * the Free Software Foundation, either version 3 of the License, or |
|
11 * (at your option) any later version. |
|
12 * |
|
13 * Geekttrss is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 * GNU General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with Geekttrss. If not, see <http://www.gnu.org/licenses/>. |
|
20 */ |
|
21 package com.geekorum.build |
|
22 |
|
23 import com.android.build.gradle.AppPlugin |
|
24 import com.android.build.gradle.DynamicFeaturePlugin |
|
25 import com.android.build.gradle.LibraryPlugin |
|
26 import com.android.build.gradle.TestedExtension |
|
27 import com.android.build.gradle.api.TestVariant |
|
28 import com.geekorum.gradle.avdl.AvdlExtension |
|
29 import com.geekorum.gradle.avdl.providers.flydroid.FlydroidPlugin |
|
30 import com.geekorum.gradle.avdl.providers.flydroid.flydroid |
|
31 import com.geekorum.gradle.avdl.tasks.LaunchDeviceTask |
|
32 import com.geekorum.gradle.avdl.tasks.StopDeviceTask |
|
33 import com.geekorum.gradle.avdl.tasks.orderForTask |
|
34 import com.geekorum.gradle.avdl.tasks.registerAvdlDevicesTask |
|
35 import org.gradle.api.Project |
|
36 import org.gradle.api.Task |
|
37 import org.gradle.api.provider.Provider |
|
38 import org.gradle.api.services.BuildService |
|
39 import org.gradle.api.services.BuildServiceParameters |
|
40 import org.gradle.api.tasks.TaskContainer |
|
41 import org.gradle.api.tasks.TaskProvider |
|
42 import org.gradle.kotlin.dsl.* |
|
43 |
|
44 |
|
45 fun Project.configureAvdlDevices(flydroidUrl: String, flydroidKey: String) { |
|
46 apply<FlydroidPlugin>() |
|
47 |
|
48 val oneInstrumentedTestService = gradle.sharedServices.registerIfAbsent( |
|
49 "oneInstrumentedTest", OneInstrumentedTestService::class.java) { |
|
50 maxParallelUsages.set(1) |
|
51 } |
|
52 |
|
53 rootProject.serializeInstrumentedTestTask(oneInstrumentedTestService) |
|
54 |
|
55 val android = the<TestedExtension>() |
|
56 configure<AvdlExtension> { |
|
57 devices { |
|
58 android.testVariants.all { |
|
59 register("android-n-${project.path}-$baseName") { |
|
60 setup = flydroid { |
|
61 url = flydroidUrl |
|
62 this.flydroidKey = flydroidKey |
|
63 // android-q images fail, don't manage to start the tests |
|
64 image = "android-n" |
|
65 useTunnel = true |
|
66 } |
|
67 } |
|
68 } |
|
69 } |
|
70 } |
|
71 |
|
72 tasks { |
|
73 var lastStopTask: TaskProvider<out Task>? = null |
|
74 var lastTestTask: TaskProvider<out Task>? = null |
|
75 android.testVariants.all { |
|
76 val (startTask, stopTask ) = |
|
77 registerAvdlDevicesTaskForVariant(this, listOf("android-n-${project.path}-$baseName")) |
|
78 listOf(startTask, stopTask).forEach { |
|
79 it.configure { |
|
80 usesService(oneInstrumentedTestService) |
|
81 } |
|
82 } |
|
83 |
|
84 lastStopTask?.let { |
|
85 startTask.configure { |
|
86 mustRunAfter(it) |
|
87 } |
|
88 } |
|
89 lastTestTask?.let { |
|
90 startTask.configure { |
|
91 mustRunAfter(it) |
|
92 } |
|
93 } |
|
94 lastStopTask = stopTask |
|
95 lastTestTask = connectedInstrumentTestProvider |
|
96 } |
|
97 } |
|
98 } |
|
99 |
|
100 private fun TaskContainer.registerAvdlDevicesTaskForVariant( |
|
101 variant: TestVariant, devices: List<String> |
|
102 ): Pair<TaskProvider<LaunchDeviceTask>, TaskProvider<StopDeviceTask>> { |
|
103 val tasks = |
|
104 registerAvdlDevicesTask(variant.name, devices) |
|
105 tasks.orderForTask(variant.connectedInstrumentTestProvider) |
|
106 return tasks |
|
107 } |
|
108 |
|
109 |
|
110 private fun Project.serializeInstrumentedTestTask(oneInstrumentedTestService: Provider<OneInstrumentedTestService>) { |
|
111 fun Project.configureTestTasks() { |
|
112 extensions.configure<TestedExtension> { |
|
113 testVariants.all { |
|
114 connectedInstrumentTestProvider.configure { |
|
115 usesService(oneInstrumentedTestService) |
|
116 } |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 allprojects { |
|
122 val project = this |
|
123 plugins.withType<AppPlugin> { project.configureTestTasks() } |
|
124 plugins.withType<DynamicFeaturePlugin> { project.configureTestTasks() } |
|
125 plugins.withType<LibraryPlugin> { project.configureTestTasks() } |
|
126 } |
|
127 } |
|
128 |
|
129 abstract class OneInstrumentedTestService : BuildService<BuildServiceParameters.None> |