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

Commit 38a90aef authored by Oluwarotimi Adesina's avatar Oluwarotimi Adesina
Browse files

Add util to get package name from runtime metadata schema

Context: This will be used to obtain the corresponding package that owns the schema from a getSchemaResponse.
Flag: android.app.appfunctions.flags.enable_app_function_manager
Test: atest FrameworksAppFunctionsTests -c
Bug: 357551503
Change-Id: I80200320c228e621bdff969e7be2e28f9b63d0c7
parent a13d2873
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -61,6 +61,20 @@ public class AppFunctionRuntimeMetadata extends GenericDocument {
        return RUNTIME_SCHEMA_TYPE + RUNTIME_SCHEMA_TYPE_SEPARATOR + Objects.requireNonNull(pkg);
    }

    /** Returns the package name from the runtime metadata schema name. */
    @NonNull
    public static String getPackageNameFromSchema(String metadataSchemaType) {
        String[] split = metadataSchemaType.split(RUNTIME_SCHEMA_TYPE_SEPARATOR);
        if (split.length > 2) {
            throw new IllegalArgumentException(
                    "Invalid schema type: " + metadataSchemaType + " for app function runtime");
        }
        if (split.length < 2) {
            return APP_FUNCTION_INDEXER_PACKAGE;
        }
        return split[1];
    }

    /** Returns the document id for an app function's runtime metadata. */
    public static String getDocumentIdForAppFunction(
            @NonNull String pkg, @NonNull String functionId) {
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.appfunctions

import android.app.appsearch.AppSearchSchema
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class AppFunctionRuntimeMetadataTest {

    @Test
    fun getRuntimeSchemaNameForPackage() {
        val actualSchemaName =
            AppFunctionRuntimeMetadata.getRuntimeSchemaNameForPackage("com.example.app")

        assertThat(actualSchemaName).isEqualTo("AppFunctionRuntimeMetadata-com.example.app")
    }

    @Test
    fun testCreateChildRuntimeSchema() {
        val runtimeSchema: AppSearchSchema =
            AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema("com.example.app")

        assertThat(runtimeSchema.schemaType).isEqualTo("AppFunctionRuntimeMetadata-com.example.app")
        val propertyNameSet = runtimeSchema.properties.map { it.name }.toSet()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_FUNCTION_ID))
            .isTrue()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_PACKAGE_NAME))
            .isTrue()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_ENABLED)).isTrue()
        assertThat(
                propertyNameSet.contains(
                    AppFunctionRuntimeMetadata.PROPERTY_APP_FUNCTION_STATIC_METADATA_QUALIFIED_ID
                )
            )
            .isTrue()
    }

    @Test
    fun testCreateParentRuntimeSchema() {
        val runtimeSchema: AppSearchSchema =
            AppFunctionRuntimeMetadata.createParentAppFunctionRuntimeSchema()

        assertThat(runtimeSchema.schemaType).isEqualTo("AppFunctionRuntimeMetadata")
        val propertyNameSet = runtimeSchema.properties.map { it.name }.toSet()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_FUNCTION_ID))
            .isTrue()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_PACKAGE_NAME))
            .isTrue()
        assertThat(propertyNameSet.contains(AppFunctionRuntimeMetadata.PROPERTY_ENABLED)).isTrue()
        assertThat(
                propertyNameSet.contains(
                    AppFunctionRuntimeMetadata.PROPERTY_APP_FUNCTION_STATIC_METADATA_QUALIFIED_ID
                )
            )
            .isTrue()
    }

    @Test
    fun testGetPackageNameFromSchema() {
        val expectedPackageName = "com.foo.test"
        val expectedPackageName2 = "com.bar.test"
        val testSchemaType =
            AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema(expectedPackageName)
                .schemaType
        val testSchemaType2 =
            AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema(expectedPackageName2)
                .schemaType

        val actualPackageName = AppFunctionRuntimeMetadata.getPackageNameFromSchema(testSchemaType)
        val actualPackageName2 =
            AppFunctionRuntimeMetadata.getPackageNameFromSchema(testSchemaType2)

        assertThat(actualPackageName).isEqualTo(expectedPackageName)
        assertThat(actualPackageName2).isEqualTo(expectedPackageName2)
    }

    @Test
    fun testGetPackageNameFromParentSchema() {
        val expectedPackageName = AppFunctionRuntimeMetadata.APP_FUNCTION_INDEXER_PACKAGE
        val testSchemaType =
            AppFunctionRuntimeMetadata.createParentAppFunctionRuntimeSchema().schemaType

        val actualPackageName = AppFunctionRuntimeMetadata.getPackageNameFromSchema(testSchemaType)

        assertThat(actualPackageName).isEqualTo(expectedPackageName)
    }
}