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

Commit 72e54636 authored by Aldi Fahrezi's avatar Aldi Fahrezi Committed by Android (Google) Code Review
Browse files

Merge "Move SidecarConverterTest into framework/base/libs/tests" into main

parents 8ae19c72 6c0efc28
Loading
Loading
Loading
Loading
+41 −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 {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

android_test {
    name: "AppFunctionsSidecarTestCases",
    team: "trendy_team_system_intelligence",
    static_libs: [
        "androidx.test.runner",
        "androidx.test.rules",
        "androidx.test.ext.junit",
        "androidx.core_core-ktx",
        "com.google.android.appfunctions.sidecar.impl",
        "junit",
        "kotlin-test",
        "mockito-target-extended-minus-junit4",
        "platform-test-annotations",
        "testables",
        "testng",
        "truth",
    ],
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],
}
+27 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.appfunctions.sidecar.tests">
    <application android:debuggable="true">
        <uses-library android:name="android.test.mock" />
        <uses-library android:name="android.test.runner" />
    </application>
    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.google.android.appfunctions.sidecar.tests">
    </instrumentation>
</manifest>
+27 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<configuration description="Config for AppFunctions Sidecar Tests">
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="AppFunctionsSidecarTestCases.apk" />
    </target_preparer>
    <option name="test-tag" value="AppFunctionsSidecarTestCases" />
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.google.android.appfunctions.sidecar.tests" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
        <option name="hidden-api-checks" value="false"/>
    </test>
</configuration>
+172 −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 com.google.android.appfunctions.sidecar.tests

import android.app.appfunctions.ExecuteAppFunctionRequest
import android.app.appfunctions.ExecuteAppFunctionResponse
import android.app.appsearch.GenericDocument
import android.os.Bundle
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.appfunctions.sidecar.SidecarConverter
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SidecarConverterTest {
    @Test
    fun getSidecarExecuteAppFunctionRequest_sameContents() {
        val extras = Bundle()
        extras.putString("extra", "value")
        val parameters: GenericDocument =
            GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
                .setPropertyLong("testLong", 23)
                .build()
        val platformRequest: ExecuteAppFunctionRequest =
            ExecuteAppFunctionRequest.Builder("targetPkg", "targetFunctionId")
                .setExtras(extras)
                .setParameters(parameters)
                .build()

        val sidecarRequest = SidecarConverter.getSidecarExecuteAppFunctionRequest(platformRequest)

        assertThat(sidecarRequest.targetPackageName).isEqualTo("targetPkg")
        assertThat(sidecarRequest.functionIdentifier).isEqualTo("targetFunctionId")
        assertThat(sidecarRequest.parameters).isEqualTo(parameters)
        assertThat(sidecarRequest.extras.size()).isEqualTo(1)
        assertThat(sidecarRequest.extras.getString("extra")).isEqualTo("value")
    }

    @Test
    fun getPlatformExecuteAppFunctionRequest_sameContents() {
        val extras = Bundle()
        extras.putString("extra", "value")
        val parameters: GenericDocument =
            GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
                .setPropertyLong("testLong", 23)
                .build()
        val sidecarRequest =
            com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest.Builder(
                "targetPkg",
                "targetFunctionId"
            )
                .setExtras(extras)
                .setParameters(parameters)
                .build()

        val platformRequest = SidecarConverter.getPlatformExecuteAppFunctionRequest(sidecarRequest)

        assertThat(platformRequest.targetPackageName).isEqualTo("targetPkg")
        assertThat(platformRequest.functionIdentifier).isEqualTo("targetFunctionId")
        assertThat(platformRequest.parameters).isEqualTo(parameters)
        assertThat(platformRequest.extras.size()).isEqualTo(1)
        assertThat(platformRequest.extras.getString("extra")).isEqualTo("value")
    }

    @Test
    fun getSidecarExecuteAppFunctionResponse_successResponse_sameContents() {
        val resultGd: GenericDocument =
            GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
                .setPropertyBoolean(ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE, true)
                .build()
        val platformResponse = ExecuteAppFunctionResponse.newSuccess(resultGd, null)

        val sidecarResponse = SidecarConverter.getSidecarExecuteAppFunctionResponse(
            platformResponse
        )

        assertThat(sidecarResponse.isSuccess).isTrue()
        assertThat(
            sidecarResponse.resultDocument.getProperty(
                ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE
            )
        )
            .isEqualTo(booleanArrayOf(true))
        assertThat(sidecarResponse.resultCode).isEqualTo(ExecuteAppFunctionResponse.RESULT_OK)
        assertThat(sidecarResponse.errorMessage).isNull()
    }

    @Test
    fun getSidecarExecuteAppFunctionResponse_errorResponse_sameContents() {
        val emptyGd = GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "").build()
        val platformResponse =
            ExecuteAppFunctionResponse.newFailure(
                ExecuteAppFunctionResponse.RESULT_INTERNAL_ERROR,
                null,
                null
            )

        val sidecarResponse = SidecarConverter.getSidecarExecuteAppFunctionResponse(
            platformResponse
        )

        assertThat(sidecarResponse.isSuccess).isFalse()
        assertThat(sidecarResponse.resultDocument.namespace).isEqualTo(emptyGd.namespace)
        assertThat(sidecarResponse.resultDocument.id).isEqualTo(emptyGd.id)
        assertThat(sidecarResponse.resultDocument.schemaType).isEqualTo(emptyGd.schemaType)
        assertThat(sidecarResponse.resultCode)
            .isEqualTo(ExecuteAppFunctionResponse.RESULT_INTERNAL_ERROR)
        assertThat(sidecarResponse.errorMessage).isNull()
    }

    @Test
    fun getPlatformExecuteAppFunctionResponse_successResponse_sameContents() {
        val resultGd: GenericDocument =
            GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
                .setPropertyBoolean(ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE, true)
                .build()
        val sidecarResponse = com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse
            .newSuccess(resultGd, null)

        val platformResponse = SidecarConverter.getPlatformExecuteAppFunctionResponse(
            sidecarResponse
        )

        assertThat(platformResponse.isSuccess).isTrue()
        assertThat(
            platformResponse.resultDocument.getProperty(
                ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE
            )
        )
            .isEqualTo(booleanArrayOf(true))
        assertThat(platformResponse.resultCode).isEqualTo(ExecuteAppFunctionResponse.RESULT_OK)
        assertThat(platformResponse.errorMessage).isNull()
    }

    @Test
    fun getPlatformExecuteAppFunctionResponse_errorResponse_sameContents() {
        val emptyGd = GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "").build()
        val sidecarResponse =
            com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse.newFailure(
                ExecuteAppFunctionResponse.RESULT_INTERNAL_ERROR,
                null,
                null
            )

        val platformResponse = SidecarConverter.getPlatformExecuteAppFunctionResponse(
            sidecarResponse
        )

        assertThat(platformResponse.isSuccess).isFalse()
        assertThat(platformResponse.resultDocument.namespace).isEqualTo(emptyGd.namespace)
        assertThat(platformResponse.resultDocument.id).isEqualTo(emptyGd.id)
        assertThat(platformResponse.resultDocument.schemaType).isEqualTo(emptyGd.schemaType)
        assertThat(platformResponse.resultCode)
            .isEqualTo(ExecuteAppFunctionResponse.RESULT_INTERNAL_ERROR)
        assertThat(platformResponse.errorMessage).isNull()
    }
}