Merge "Test added support for MATCH_FACTORY_ONLY flag"

This commit is contained in:
Evan Severson
2020-08-07 21:09:25 +00:00
committed by Android (Google) Code Review
5 changed files with 174 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ java_test_host {
":PackageManagerTestAppVersion3Invalid",
":PackageManagerTestAppVersion4",
":PackageManagerTestAppOriginalOverride",
":PackageManagerServiceDeviceSideTests",
],
}

View File

@@ -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)
}
}

View File

@@ -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,
}

View File

@@ -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>

View File

@@ -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()
}
}
}