1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.baselineprofile.gradle.wrapper 18 19 import androidx.baselineprofile.gradle.utils.BaselineProfileProjectSetupRule 20 import androidx.baselineprofile.gradle.utils.Module 21 import androidx.baselineprofile.gradle.utils.TestAgpVersion 22 import com.google.common.truth.IterableSubject 23 import com.google.common.truth.Truth.assertThat 24 import org.junit.Rule 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 import org.junit.runners.Parameterized 28 29 @RunWith(Parameterized::class) 30 class BaselineProfileWrapperPluginTest(agpVersion: TestAgpVersion) { 31 32 companion object { 33 @Parameterized.Parameters(name = "agpVersion={0}") 34 @JvmStatic parametersnull35 fun parameters() = TestAgpVersion.all() 36 } 37 38 @get:Rule 39 val projectSetup = BaselineProfileProjectSetupRule(forceAgpVersion = agpVersion.versionString) 40 41 @Test 42 fun testWrapperGeneratingForApplication() { 43 projectSetup.consumer.setBuildGradle( 44 """ 45 plugins { 46 id("com.android.application") 47 id("androidx.baselineprofile") 48 } 49 android { namespace 'com.example.namespace.test' } 50 dependencies { baselineProfile(project(":${projectSetup.producer.name}")) } 51 52 $taskPrintPlugins 53 """ 54 .trimIndent() 55 ) 56 projectSetup.producer.setBuildGradle( 57 """ 58 plugins { 59 id("com.android.test") 60 id("androidx.baselineprofile") 61 } 62 android { 63 targetProjectPath = ":${projectSetup.consumer.name}" 64 namespace 'com.example.namespace.test' 65 } 66 67 $taskPrintPlugins 68 """ 69 .trimIndent() 70 ) 71 72 projectSetup.consumer.printPluginsAndAssertOutput { 73 contains("class $CLASS_APP_TARGET_PLUGIN") 74 contains("class $CLASS_CONSUMER_PLUGIN") 75 } 76 projectSetup.producer.printPluginsAndAssertOutput { 77 contains("class $CLASS_PRODUCER_PLUGIN") 78 } 79 } 80 81 @Test testWrapperGeneratingForLibrariesnull82 fun testWrapperGeneratingForLibraries() { 83 projectSetup.appTarget.setBuildGradle( 84 """ 85 plugins { 86 id("com.android.application") 87 id("androidx.baselineprofile") 88 } 89 android { namespace 'com.example.namespace.test' } 90 91 $taskPrintPlugins 92 """ 93 .trimIndent() 94 ) 95 projectSetup.consumer.setBuildGradle( 96 """ 97 plugins { 98 id("com.android.library") 99 id("androidx.baselineprofile") 100 } 101 android { namespace 'com.example.namespace.test' } 102 dependencies { baselineProfile(project(":${projectSetup.producer.name}")) } 103 104 $taskPrintPlugins 105 """ 106 .trimIndent() 107 ) 108 projectSetup.producer.setBuildGradle( 109 """ 110 plugins { 111 id("com.android.test") 112 id("androidx.baselineprofile") 113 } 114 android { 115 targetProjectPath = ":${projectSetup.consumer.name}" 116 namespace 'com.example.namespace.test' 117 } 118 119 $taskPrintPlugins 120 """ 121 .trimIndent() 122 ) 123 124 projectSetup.appTarget.printPluginsAndAssertOutput { 125 contains("class $CLASS_APP_TARGET_PLUGIN") 126 contains("class $CLASS_CONSUMER_PLUGIN") 127 } 128 projectSetup.producer.printPluginsAndAssertOutput { 129 contains("class $CLASS_PRODUCER_PLUGIN") 130 } 131 projectSetup.consumer.printPluginsAndAssertOutput { 132 contains("class $CLASS_CONSUMER_PLUGIN") 133 } 134 } 135 Modulenull136 private fun Module.printPluginsAndAssertOutput(assertBlock: IterableSubject.() -> (Unit)) { 137 val output = 138 gradleRunner.withArguments("printPlugins", "--stacktrace").build().output.lines() 139 assertBlock(assertThat(output)) 140 } 141 } 142 143 private const val CLASS_CONSUMER_PLUGIN = 144 "androidx.baselineprofile.gradle.consumer.BaselineProfileConsumerPlugin" 145 private const val CLASS_APP_TARGET_PLUGIN = 146 "androidx.baselineprofile.gradle.apptarget.BaselineProfileAppTargetPlugin" 147 private const val CLASS_PRODUCER_PLUGIN = 148 "androidx.baselineprofile.gradle.producer.BaselineProfileProducerPlugin" 149 150 private val taskPrintPlugins = 151 """ 152 tasks.register("printPlugins", PrintTask) { t -> 153 def pluginsList = project.plugins.collect { it.class.toString() }.join("\n") 154 t.text.set(pluginsList) 155 } 156 """ 157 .trimIndent() 158