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

Commit 19fbc5b3 authored by Evan Severson's avatar Evan Severson Committed by Android (Google) Code Review
Browse files

Merge "Test added support for MATCH_FACTORY_ONLY flag"

parents 41c0c487 6191f322
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ java_test_host {
        ":PackageManagerTestAppVersion3Invalid",
        ":PackageManagerTestAppVersion4",
        ":PackageManagerTestAppOriginalOverride",
        ":PackageManagerServiceDeviceSideTests",
    ],
}

+71 −0
Original line number Diff line number Diff line
package com.android.server.pm.test

import com.android.internal.util.test.SystemPreparer
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith

@RunWith(DeviceJUnit4ClassRunner::class)
class FactoryPackageTest : BaseHostJUnit4Test() {

    companion object {
        private const val TEST_PKG_NAME = "com.android.server.pm.test.test_app"

        private const val VERSION_ONE = "PackageManagerTestAppVersion1.apk"
        private const val VERSION_TWO = "PackageManagerTestAppVersion2.apk"
        private const val DEVICE_SIDE = "PackageManagerServiceDeviceSideTests.apk"

        @get:ClassRule
        val deviceRebootRule = SystemPreparer.TestRuleDelegate(true)
    }

    private val tempFolder = TemporaryFolder()
    private val preparer: SystemPreparer = SystemPreparer(tempFolder,
            SystemPreparer.RebootStrategy.FULL, deviceRebootRule) { this.device }

    @get:Rule
    val rules = RuleChain.outerRule(tempFolder).around(preparer)!!
    private val filePath =
            HostUtils.makePathForApk("PackageManagerTestApp.apk", Partition.SYSTEM)

    @Before
    @After
    fun removeApk() {
        device.uninstallPackage(TEST_PKG_NAME)
        device.deleteFile(filePath.parent.toString())
        device.reboot()
    }

    @Test
    fun testGetInstalledPackagesFactoryOnlyFlag() {
        // First, push a system app to the device and then update it so there's a data variant
        preparer.pushResourceFile(VERSION_ONE, filePath.toString())
                .reboot()

        val versionTwoFile = HostUtils.copyResourceToHostFile(VERSION_TWO, tempFolder.newFile())

        assertThat(device.installPackage(versionTwoFile, true)).isNull()

        runDeviceTest("testGetInstalledPackagesWithFactoryOnly")
    }

    /**
     * Run a device side test from com.android.server.pm.test.deviceside.DeviceSide
     *
     * @param method the method to run
     */
    fun runDeviceTest(method: String) {
        val deviceSideFile = HostUtils.copyResourceToHostFile(DEVICE_SIDE, tempFolder.newFile())
        assertThat(device.installPackage(deviceSideFile, true)).isNull()
        runDeviceTests(device, "com.android.server.pm.test.deviceside",
                "com.android.server.pm.test.deviceside.DeviceSide", method)
    }
}
+33 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2019 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.
//

android_test_helper_app {
    name: "PackageManagerServiceDeviceSideTests",
    sdk_version: "test_current",
    srcs: ["src/**/*.kt"],
    libs: [
        "android.test.base",
    ],
    static_libs: [
        "androidx.annotation_annotation",
        "junit",
        "junit-params",
        "androidx.test.ext.junit",
        "androidx.test.rules",
        "truth-prebuilt",
    ],
    platform_apis: true,
}
+27 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
 * Copyright (C) 2019 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.android.server.pm.test.deviceside">

    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.android.server.pm.test.deviceside" />
</manifest>
+42 −0
Original line number Diff line number Diff line
package com.android.server.pm.test.deviceside

import android.content.pm.PackageManager.MATCH_FACTORY_ONLY
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class DeviceSide {
    companion object {
        private const val TEST_PKG_NAME = "com.android.server.pm.test.test_app"
    }

    @Test
    fun testGetInstalledPackagesWithFactoryOnly() {
        val instrumentation = InstrumentationRegistry.getInstrumentation()
        val uiAutomation = instrumentation.uiAutomation
        val ctx = instrumentation.context

        uiAutomation.adoptShellPermissionIdentity()
        try {
            val packages1 = ctx.packageManager.getInstalledPackages(0)
                    .filter { it.packageName == TEST_PKG_NAME }
            val packages2 = ctx.packageManager.getInstalledPackages(MATCH_FACTORY_ONLY)
                    .filter { it.packageName == TEST_PKG_NAME }

            Truth.assertWithMessage("Incorrect number of packages found")
                    .that(packages1.size).isEqualTo(1)
            Truth.assertWithMessage("Incorrect number of packages found")
                    .that(packages2.size).isEqualTo(1)

            Truth.assertWithMessage("Incorrect version code for updated package")
                    .that(packages1[0].longVersionCode).isEqualTo(2)
            Truth.assertWithMessage("Incorrect version code for factory package")
                    .that(packages2[0].longVersionCode).isEqualTo(1)
        } finally {
            uiAutomation.dropShellPermissionIdentity()
        }
    }
}