Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6887ab2a authored by Victor Gabriel Savu's avatar Victor Gabriel Savu Committed by Android (Google) Code Review
Browse files

Merge "Add tests for the devicepolicy processor" into main

parents ba023ba7 0d1d745d
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -30,3 +30,30 @@ java_library {
    sdk_version: "core_current",
    host_supported: true,
}

java_test_host {
    name: "DevicePolicyAnnotationProcessorTests",

    srcs: ["test/src/**/*.kt"],

    static_libs: [
        "compile-testing-prebuilt",
        "junit",
        "truth",
        "kotlin-reflect",
        "DevicePolicyAnnotationProcessorHostLibrary",
    ],

    java_resources: [":DevicePolicyAnnotationProcessorTestSource"],
    test_suites: ["general-tests"],
}

filegroup {
    name: "DevicePolicyAnnotationProcessorTestSource",
    srcs: [
        "test/resources/**/*.java",
        "test/resources/**/*.json",
    ],
    path: "test/resources/",
    visibility: ["//visibility:private"],
}
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.admin;

import android.processor.devicepolicy.BooleanPolicyDefinition;
import android.processor.devicepolicy.PolicyDefinition;

public final class OtherClass {
    // We don't actually do anything with this.
    public OtherClass(String id) {
    }

    private static final String TEST_POLICY_1_KEY = "test_policy_1_key";

    /**
     * Test policy 1
     */
    @PolicyDefinition
    @BooleanPolicyDefinition
    public static final PolicyIdentifier<Boolean> TEST_POLICY_1 = new PolicyIdentifier<>(
            TEST_POLICY_1_KEY);
}
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.admin;

import android.processor.devicepolicy.BooleanPolicyDefinition;
import android.processor.devicepolicy.PolicyDefinition;

public final class PolicyIdentifier<T> {
    // We don't actually do anything with this.
    public PolicyIdentifier(String id) {
    }

    private static final String TEST_POLICY_1_KEY = "test_policy_1_key";

    /**
     * Test policy 1
     */
    @PolicyDefinition
    @BooleanPolicyDefinition
    public static final PolicyIdentifier<Boolean> TEST_POLICY_1 = new PolicyIdentifier<>(
            TEST_POLICY_1_KEY);
}
+9 −0
Original line number Diff line number Diff line
{
  "policies": [
    {
      "name": "android.app.admin.PolicyIdentifier<T>.TEST_POLICY_1",
      "type": "java.lang.Boolean",
      "documentation": " Test policy 1\n"
    }
  ]
}
 No newline at end of file
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.processor.devicepolicy.test

import android.processor.devicepolicy.PolicyProcessor
import com.google.common.base.Charsets
import com.google.common.io.ByteSource
import com.google.common.io.Resources
import com.google.testing.compile.Compilation
import com.google.testing.compile.CompilationSubject.assertThat
import com.google.testing.compile.Compiler
import com.google.testing.compile.JavaFileObjects
import java.io.IOException
import javax.tools.StandardLocation
import org.junit.Assert.assertNotNull
import org.junit.Assert.fail

import org.junit.Test

class PolicyProcessorTest {
    private val mCompiler = Compiler.javac().withProcessors(PolicyProcessor())

    private companion object {
        const val POLICIES_JSON_LOCATION = "android/processor/devicepolicy/policies.json"

        const val POLICY_IDENTIFIER = "android/processor/devicepolicy/test/PolicyIdentifier"
        const val POLICY_IDENTIFIER_JAVA = "$POLICY_IDENTIFIER.java"
        const val POLICY_IDENTIFIER_JSON = "$POLICY_IDENTIFIER.json"

        fun loadTextResource(path: String): String {
            try {
                val url = Resources.getResource(path)
                assertNotNull(String.format("Resource file not found: %s", path), url)
                return Resources.toString(url, Charsets.UTF_8)
            } catch (e: IOException) {
                fail(e.message)
                return ""
            }
        }
    }

    @Test
    fun test_PolicyIdendifierFake_generates() {
        val expectedOutput = loadTextResource(POLICY_IDENTIFIER_JSON)

        val compilation: Compilation =
            mCompiler.compile(JavaFileObjects.forResource(POLICY_IDENTIFIER_JAVA))
        assertThat(compilation).succeeded()
        assertThat(compilation).generatedFile(
            StandardLocation.SOURCE_OUTPUT, POLICIES_JSON_LOCATION
        ).hasContents(ByteSource.wrap(expectedOutput.toByteArray()))
    }

    @Test
    fun test_other_class_failsToCompile() {
        val compilation: Compilation =
            mCompiler.compile(
                JavaFileObjects.forResource("android/processor/devicepolicy/test/OtherClass.java"),
                JavaFileObjects.forResource(POLICY_IDENTIFIER_JAVA)
            )
        assertThat(compilation).failed()
        assertThat(compilation).hadErrorContaining("@PolicyDefinition can only be applied to fields in android.app.admin.PolicyIdentifier")
    }
}
 No newline at end of file