1 package com.android.server.pm.test.deviceside 2 3 import android.content.pm.PackageManager.MATCH_FACTORY_ONLY 4 import androidx.test.ext.junit.runners.AndroidJUnit4 5 import androidx.test.platform.app.InstrumentationRegistry 6 import com.google.common.truth.Truth 7 import org.junit.Test 8 import org.junit.runner.RunWith 9 10 @RunWith(AndroidJUnit4::class) 11 class DeviceSide { 12 companion object { 13 private const val TEST_PKG_NAME = "com.android.server.pm.test.test_app" 14 } 15 16 @Test testGetInstalledPackagesWithFactoryOnlynull17 fun testGetInstalledPackagesWithFactoryOnly() { 18 val instrumentation = InstrumentationRegistry.getInstrumentation() 19 val uiAutomation = instrumentation.uiAutomation 20 val ctx = instrumentation.context 21 22 uiAutomation.adoptShellPermissionIdentity() 23 try { 24 val packages1 = ctx.packageManager.getInstalledPackages(0) 25 .filter { it.packageName == TEST_PKG_NAME } 26 val packages2 = ctx.packageManager.getInstalledPackages(MATCH_FACTORY_ONLY) 27 .filter { it.packageName == TEST_PKG_NAME } 28 29 Truth.assertWithMessage("Incorrect number of packages found") 30 .that(packages1.size).isEqualTo(1) 31 Truth.assertWithMessage("Incorrect number of packages found") 32 .that(packages2.size).isEqualTo(1) 33 34 Truth.assertWithMessage("Incorrect version code for updated package") 35 .that(packages1[0].longVersionCode).isEqualTo(2) 36 Truth.assertWithMessage("Incorrect version code for factory package") 37 .that(packages2[0].longVersionCode).isEqualTo(1) 38 } finally { 39 uiAutomation.dropShellPermissionIdentity() 40 } 41 } 42 } 43