diff options
5 files changed, 174 insertions, 0 deletions
diff --git a/services/tests/PackageManagerServiceTests/host/Android.bp b/services/tests/PackageManagerServiceTests/host/Android.bp index cffcdd8f94bd..e4e7e2288590 100644 --- a/services/tests/PackageManagerServiceTests/host/Android.bp +++ b/services/tests/PackageManagerServiceTests/host/Android.bp @@ -32,6 +32,7 @@ java_test_host { ":PackageManagerTestAppVersion3Invalid", ":PackageManagerTestAppVersion4", ":PackageManagerTestAppOriginalOverride", + ":PackageManagerServiceDeviceSideTests", ], } diff --git a/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/FactoryPackageTest.kt b/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/FactoryPackageTest.kt new file mode 100644 index 000000000000..3847658def6a --- /dev/null +++ b/services/tests/PackageManagerServiceTests/host/src/com/android/server/pm/test/FactoryPackageTest.kt @@ -0,0 +1,71 @@ +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) + } +} diff --git a/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/Android.bp b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/Android.bp new file mode 100644 index 000000000000..af0ac77eaadd --- /dev/null +++ b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/Android.bp @@ -0,0 +1,33 @@ +// +// 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, +} diff --git a/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/AndroidManifest.xml b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/AndroidManifest.xml new file mode 100644 index 000000000000..286ad56435fd --- /dev/null +++ b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/AndroidManifest.xml @@ -0,0 +1,27 @@ +<?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> + diff --git a/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/src/com/android/server/pm/cts/test/deviceside/DeviceSide.kt b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/src/com/android/server/pm/cts/test/deviceside/DeviceSide.kt new file mode 100644 index 000000000000..d140662a24c2 --- /dev/null +++ b/services/tests/PackageManagerServiceTests/host/test-apps/DeviceSide/src/com/android/server/pm/cts/test/deviceside/DeviceSide.kt @@ -0,0 +1,42 @@ +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() + } + } +} |