From d72bee523bf64d3bb9d149225899ee9616f096fb Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 23 Aug 2022 09:28:39 -0300 Subject: [PATCH] Mock ApplicationInfo as it now calls the native SystemClock.uptimeMillis() method which is hard to mock --- .../restore/install/ApkBackupRestoreTest.kt | 8 +------ .../restore/install/ApkRestoreTest.kt | 21 +++---------------- .../seedvault/transport/TransportTest.kt | 7 ++++--- .../transport/backup/BackupCoordinatorTest.kt | 5 +++-- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt index 97012e3d..d201d89e 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt @@ -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)) diff --git a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt index ad730402..bd920bc8 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt @@ -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 } diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/TransportTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/TransportTest.kt index 2f4e0cd2..35e81b28 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/TransportTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/TransportTest.kt @@ -37,12 +37,13 @@ internal abstract class TransportTest { protected val sigInfo: SigningInfo = mockk() protected val token = Random.nextLong() + protected val applicationInfo = mockk { + 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 { diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt index 9a58f3fb..d5facae7 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt @@ -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()