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

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

Merge "Add more tests for the devicepolicy processor" into main

parents a043ee83 d2d65814
Loading
Loading
Loading
Loading
+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<Integer> TEST_POLICY_1 = new PolicyIdentifier<>(
            TEST_POLICY_1_KEY);
}
+35 −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.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
    public static final PolicyIdentifier<Boolean>
            TEST_POLICY_1 = new PolicyIdentifier<>(
            TEST_POLICY_1_KEY);
}
+43 −1
Original line number Diff line number Diff line
@@ -41,6 +41,11 @@ class PolicyProcessorTest {
        const val POLICY_IDENTIFIER_JAVA = "$POLICY_IDENTIFIER.java"
        const val POLICY_IDENTIFIER_JSON = "$POLICY_IDENTIFIER.json"

        const val OTHER_CLASS_JAVA = "android/processor/devicepolicy/test/OtherClass.java"
        const val POLICY_IDENTIFIER_INVALID_TYPE_JAVA = "android/processor/devicepolicy/test/invalidtype/PolicyIdentifier.java"
        const val POLICY_IDENTIFIER_MISSING_METADATA_JAVA = "android/processor/devicepolicy/test/missingmetadata/PolicyIdentifier.java"


        fun loadTextResource(path: String): String {
            try {
                val url = Resources.getResource(path)
@@ -69,10 +74,47 @@ class PolicyProcessorTest {
    fun test_other_class_failsToCompile() {
        val compilation: Compilation =
            mCompiler.compile(
                JavaFileObjects.forResource("android/processor/devicepolicy/test/OtherClass.java"),
                JavaFileObjects.forResource(OTHER_CLASS_JAVA),
                JavaFileObjects.forResource(POLICY_IDENTIFIER_JAVA)
            )
        assertThat(compilation).failed()
        assertThat(compilation).hadErrorContaining("@PolicyDefinition can only be applied to fields in android.app.admin.PolicyIdentifier")
    }

    @Test
    fun test_invalidType_failsToCompile() {
        val compilation: Compilation = mCompiler.compile(
            JavaFileObjects.forResource(POLICY_IDENTIFIER_INVALID_TYPE_JAVA)
        )
        assertThat(compilation).failed()
        assertThat(compilation).hadErrorContaining("booleanValue in @PolicyDefinition can only be applied to policies of type java.lang.Boolean")
    }

    @Test
    fun test_missingMetadata_failsToCompile() {
        val compilation: Compilation = mCompiler.compile(
            JavaFileObjects.forResource(POLICY_IDENTIFIER_MISSING_METADATA_JAVA)
        )
        assertThat(compilation).failed()
        assertThat(compilation).hadErrorContaining("@PolicyDefinition has no type specific definition")
    }

    /**
     * Errors should only come from our processor.
     */
    @Test
    fun test_invalidTestData_compilesWithoutProcessor() {
        val plainCompiler = Compiler.javac()

        fun checkCompileSucceeds(vararg files: String) {
            val resources = files.map { JavaFileObjects.forResource(it) }

            val compilation = plainCompiler.compile(*resources.toTypedArray())
            assertThat(compilation).succeeded()
        }

        checkCompileSucceeds(OTHER_CLASS_JAVA, POLICY_IDENTIFIER_JAVA)
        checkCompileSucceeds(POLICY_IDENTIFIER_INVALID_TYPE_JAVA)
        checkCompileSucceeds(POLICY_IDENTIFIER_MISSING_METADATA_JAVA)
    }
}
 No newline at end of file