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.privacysandboxlibraryplugin
18 
19 import androidx.testutils.gradle.ProjectSetupRule
20 import java.io.File
21 import java.nio.file.Files
22 import org.gradle.testkit.runner.GradleRunner
23 import org.gradle.testkit.runner.TaskOutcome
24 import org.junit.Assert.assertEquals
25 import org.junit.Assert.assertTrue
26 import org.junit.Before
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.junit.runners.JUnit4
31 
32 @RunWith(JUnit4::class)
33 class PrivacySandboxLibraryPluginTest {
34 
35     @get:Rule val projectSetup = ProjectSetupRule()
36 
37     lateinit var gradleRunner: GradleRunner
38 
39     @Before
setUpnull40     fun setUp() {
41         File(projectSetup.rootDir, "settings.gradle")
42             .writeText("rootProject.name = \"test-privacysandbox-library\"")
43         projectSetup.writeDefaultBuildGradle(
44             prefix =
45                 """
46                 plugins {
47                     id("kotlin-android")
48                     id("androidx.privacysandbox.library")
49                 }
50             """
51                     .trimIndent(),
52             suffix =
53                 """
54                     android {
55                          namespace "test.privacysandboxlibrary"
56                          compileOptions {
57                             sourceCompatibility JavaVersion.VERSION_17
58                             targetCompatibility JavaVersion.VERSION_17
59                          }
60                         kotlinOptions {
61                             jvmTarget=17
62                         }
63                     }
64             """
65         )
66 
67         val myServiceSource =
68             File(projectSetup.rootDir, "src/main/java/test/privacysandboxlibraryplugintest").also {
69                 it.mkdirs()
70             }
71 
72         myServiceSource.resolve("MyService.kt").also {
73             Files.createFile(it.toPath())
74             it.writeText(
75                 """package test.privacysandboxlibrary
76 
77                     import androidx.privacysandbox.tools.PrivacySandboxService
78 
79                     @PrivacySandboxService
80                     interface MyService {
81                         suspend fun doStuff(x: Int, y: Int): String
82                     }
83                 """
84             )
85         }
86 
87         gradleRunner =
88             GradleRunner.create().withProjectDir(projectSetup.rootDir).withPluginClasspath()
89     }
90 
91     /* Test plugin applies successfully and produces KSP generated directory. The output of KSP
92      * is unit tested in :tools:apicompiler and integration tested in Android Gradle Plugin tests.
93      */
94     @Test
applyPluginnull95     fun applyPlugin() {
96         val output = gradleRunner.withArguments("build", "--stacktrace").build()
97         assertEquals(output.task(":build")!!.outcome, TaskOutcome.SUCCESS)
98         assertEquals(output.task(":kspDebugKotlin")!!.outcome, TaskOutcome.SUCCESS)
99         val build = File(projectSetup.rootDir, "build")
100         assertTrue(File(build, "generated/ksp/debug").exists())
101     }
102 }
103