Mock ApplicationInfo

as it now calls the native SystemClock.uptimeMillis() method which is hard to mock
This commit is contained in:
Torsten Grote 2022-08-23 09:28:39 -03:00 committed by Aayush Gupta
parent 65fa49132e
commit d72bee523b
4 changed files with 11 additions and 30 deletions

View file

@ -134,13 +134,7 @@ internal class ApkBackupRestoreTest : TransportTest() {
every { crypto.getNameForApk(salt, packageName, "") } returns name
coEvery { storagePlugin.getInputStream(token, name) } returns inputStream
every { pm.getPackageArchiveInfo(capture(apkPath), any()) } returns packageInfo
every {
@Suppress("UNRESOLVED_REFERENCE")
pm.loadItemIcon(
packageInfo.applicationInfo,
packageInfo.applicationInfo
)
} returns icon
every { applicationInfo.loadIcon(pm) } returns icon
every { pm.getApplicationLabel(packageInfo.applicationInfo) } returns appName
every {
splitCompatChecker.isCompatible(metadata.deviceName, listOf(splitName))

View file

@ -1,7 +1,6 @@
package com.stevesoltys.seedvault.restore.install
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.ApplicationInfo.FLAG_INSTALLED
import android.content.pm.ApplicationInfo.FLAG_SYSTEM
import android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP
@ -177,13 +176,7 @@ internal class ApkRestoreTest : TransportTest() {
legacyStoragePlugin.getApkInputStream(token, packageName, "")
} returns apkInputStream
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
every {
@Suppress("UNRESOLVED_REFERENCE")
pm.loadItemIcon(
packageInfo.applicationInfo,
packageInfo.applicationInfo
)
} returns icon
every { applicationInfo.loadIcon(pm) } returns icon
every { pm.getApplicationLabel(packageInfo.applicationInfo) } returns appName
coEvery {
apkInstaller.install(match { it.size == 1 }, packageName, installerName, any())
@ -200,13 +193,11 @@ internal class ApkRestoreTest : TransportTest() {
runBlocking {
val packageMetadata = this@ApkRestoreTest.packageMetadata.copy(system = true)
packageMetadataMap[packageName] = packageMetadata
packageInfo.applicationInfo = mockk()
val installedPackageInfo: PackageInfo = mockk()
val willFail = Random.nextBoolean()
val isSystemApp = Random.nextBoolean()
cacheBaseApkAndGetInfo(tmpDir)
every { packageInfo.applicationInfo.loadIcon(pm) } returns icon
every { storagePlugin.providerPackageName } returns storageProviderPackageName
if (willFail) {
@ -214,7 +205,7 @@ internal class ApkRestoreTest : TransportTest() {
pm.getPackageInfo(packageName, 0)
} throws PackageManager.NameNotFoundException()
} else {
installedPackageInfo.applicationInfo = ApplicationInfo().apply {
installedPackageInfo.applicationInfo = mockk {
flags =
if (!isSystemApp) FLAG_INSTALLED else FLAG_SYSTEM or FLAG_UPDATED_SYSTEM_APP
}
@ -422,13 +413,7 @@ internal class ApkRestoreTest : TransportTest() {
every { crypto.getNameForApk(salt, packageName, "") } returns name
coEvery { storagePlugin.getInputStream(token, name) } returns apkInputStream
every { pm.getPackageArchiveInfo(any(), any()) } returns packageInfo
every {
@Suppress("UNRESOLVED_REFERENCE")
pm.loadItemIcon(
packageInfo.applicationInfo,
packageInfo.applicationInfo
)
} returns icon
every { applicationInfo.loadIcon(pm) } returns icon
every { pm.getApplicationLabel(packageInfo.applicationInfo) } returns appName
}

View file

@ -37,12 +37,13 @@ internal abstract class TransportTest {
protected val sigInfo: SigningInfo = mockk()
protected val token = Random.nextLong()
protected val applicationInfo = mockk<ApplicationInfo> {
flags = FLAG_ALLOW_BACKUP or FLAG_INSTALLED
}
protected val packageInfo = PackageInfo().apply {
packageName = "org.example"
longVersionCode = Random.nextLong()
applicationInfo = ApplicationInfo().apply {
flags = FLAG_ALLOW_BACKUP or FLAG_INSTALLED
}
applicationInfo = this@TransportTest.applicationInfo
signingInfo = sigInfo
}
protected val pmPackageInfo = PackageInfo().apply {

View file

@ -5,7 +5,6 @@ import android.app.backup.BackupTransport.TRANSPORT_NOT_INITIALIZED
import android.app.backup.BackupTransport.TRANSPORT_OK
import android.app.backup.BackupTransport.TRANSPORT_PACKAGE_REJECTED
import android.app.backup.BackupTransport.TRANSPORT_QUOTA_EXCEEDED
import android.content.pm.ApplicationInfo
import android.content.pm.ApplicationInfo.FLAG_STOPPED
import android.content.pm.PackageInfo
import android.net.Uri
@ -399,7 +398,9 @@ internal class BackupCoordinatorTest : BackupTest() {
PackageInfo().apply {
packageName = "org.example.2"
// the second package does not get backed up, because it is stopped
applicationInfo = ApplicationInfo().apply { flags = FLAG_STOPPED }
applicationInfo = mockk {
flags = FLAG_STOPPED
}
}
)
val packageMetadata: PackageMetadata = mockk()