diff options
Diffstat (limited to 'android/src/test')
| -rw-r--r-- | android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsIconManagerTest.kt | 98 | ||||
| -rw-r--r-- | android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsProviderTest.kt | 94 |
2 files changed, 192 insertions, 0 deletions
diff --git a/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsIconManagerTest.kt b/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsIconManagerTest.kt new file mode 100644 index 0000000000..6f871a28ee --- /dev/null +++ b/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsIconManagerTest.kt @@ -0,0 +1,98 @@ +package net.mullvad.mullvadvpn.applist + +import android.content.pm.PackageManager +import android.graphics.drawable.Drawable +import android.os.Looper +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import io.mockk.verify +import kotlin.test.assertEquals +import kotlin.test.assertFails +import org.junit.After +import org.junit.Before +import org.junit.Test + +class ApplicationsIconManagerTest { + private val mockedPackageManager = mockk<PackageManager>() + private val mockedMainLooper = mockk<Looper>() + private val testSubject = ApplicationsIconManager(mockedPackageManager) + + @Before + fun setUp() { + mockkStatic(Looper::class) + every { Looper.getMainLooper() } returns mockedMainLooper + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun test_first_time_load_icon_from_PM() { + val testPackageName = "test" + val mockedDrawable = mockk<Drawable>() + every { mockedPackageManager.getApplicationIcon(testPackageName) } returns mockedDrawable + every { mockedMainLooper.isCurrentThread } returns false + + val result = testSubject.getAppIcon(testPackageName) + + assertEquals(mockedDrawable, result) + verify { + mockedMainLooper.isCurrentThread + mockedPackageManager.getApplicationIcon(testPackageName) + } + } + + @Test + fun test_second_time_load_icon_from_cache() { + val testPackageName = "test" + val mockedDrawable = mockk<Drawable>() + every { mockedPackageManager.getApplicationIcon(testPackageName) } returns mockedDrawable + every { mockedMainLooper.isCurrentThread } returns false + + val result = testSubject.getAppIcon(testPackageName) + val result2 = testSubject.getAppIcon(testPackageName) + + assertEquals(mockedDrawable, result) + assertEquals(mockedDrawable, result2) + verify(exactly = 2) { + mockedMainLooper.isCurrentThread + } + verify(exactly = 1) { + mockedPackageManager.getApplicationIcon(testPackageName) + } + } + + @Test + fun test_second_time_load_icon_from_PM_after_clear() { + val testPackageName = "test" + val mockedDrawable = mockk<Drawable>() + every { mockedPackageManager.getApplicationIcon(testPackageName) } returns mockedDrawable + every { mockedMainLooper.isCurrentThread } returns false + + val result = testSubject.getAppIcon(testPackageName) + testSubject.dispose() + val result2 = testSubject.getAppIcon(testPackageName) + + assertEquals(mockedDrawable, result) + assertEquals(mockedDrawable, result2) + verify(exactly = 2) { + mockedMainLooper.isCurrentThread + mockedPackageManager.getApplicationIcon(testPackageName) + } + } + + @Test + fun throw_exception_when_invoke_from_MainThread() { + val testPackageName = "test" + every { mockedMainLooper.isCurrentThread } returns true + + assertFails("Should not be called from MainThread") { + testSubject.getAppIcon(testPackageName) + } + verify { mockedMainLooper.isCurrentThread } + } +} diff --git a/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsProviderTest.kt b/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsProviderTest.kt new file mode 100644 index 0000000000..cb24158422 --- /dev/null +++ b/android/src/test/kotlin/net/mullvad/mullvadvpn/applist/ApplicationsProviderTest.kt @@ -0,0 +1,94 @@ +package net.mullvad.mullvadvpn.applist + +import android.Manifest +import android.content.pm.ApplicationInfo +import android.content.pm.PackageManager +import io.mockk.every +import io.mockk.mockk +import io.mockk.unmockkAll +import io.mockk.verifyAll +import org.junit.After +import org.junit.Test + +class ApplicationsProviderTest { + private val mockedPackageManager = mockk<PackageManager>() + private val selfPackageName = "self_package_name" + private val testSubject = ApplicationsProvider(mockedPackageManager, selfPackageName) + private val internet = Manifest.permission.INTERNET + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun test_get_apps() { + val launchWithInternetPackageName = "launch_with_internet_package_name" + val launchWithoutInternetPackageName = "launch_without_internet_package_name" + val nonLaunchWithInternetPackageName = "non_launch_with_internet_package_name" + val nonLaunchWithoutInternetPackageName = "non_launch_without_internet_package_name" + + every { + mockedPackageManager.getInstalledApplications(PackageManager.GET_META_DATA) + } returns listOf( + createApplicationInfo(launchWithInternetPackageName, launch = true, internet = true), + createApplicationInfo(launchWithoutInternetPackageName, launch = true), + createApplicationInfo(nonLaunchWithInternetPackageName, internet = true), + createApplicationInfo(nonLaunchWithoutInternetPackageName), + createApplicationInfo(selfPackageName, internet = true, launch = true) + ) + + val result = testSubject.getAppsList() + val expected = listOf( + AppData(launchWithInternetPackageName, 0, launchWithInternetPackageName) + ) + assert( + expected.size == result.size && + expected.containsAll(result) && + result.containsAll(expected) + ) + + verifyAll { + mockedPackageManager.getInstalledApplications(PackageManager.GET_META_DATA) + + mockedPackageManager.checkPermission(internet, launchWithInternetPackageName) + mockedPackageManager.checkPermission(internet, launchWithoutInternetPackageName) + mockedPackageManager.checkPermission(internet, nonLaunchWithInternetPackageName) + mockedPackageManager.checkPermission(internet, nonLaunchWithoutInternetPackageName) + mockedPackageManager.checkPermission(internet, selfPackageName) + + mockedPackageManager.getLaunchIntentForPackage(launchWithInternetPackageName) + mockedPackageManager.getLaunchIntentForPackage(nonLaunchWithInternetPackageName) + mockedPackageManager.getLaunchIntentForPackage(selfPackageName) + } + } + + private fun createApplicationInfo( + packageName: String, + launch: Boolean = false, + internet: Boolean = false + ): ApplicationInfo { + val mockApplicationInfo = mockk<ApplicationInfo>() + + mockApplicationInfo.packageName = packageName + mockApplicationInfo.icon = 0 + + every { mockApplicationInfo.loadLabel(mockedPackageManager) } returns packageName + + every { + mockedPackageManager.getLaunchIntentForPackage(packageName) + } returns if (launch) + mockk() + else + null + + every { + mockedPackageManager.checkPermission(Manifest.permission.INTERNET, packageName) + } returns if (internet) + PackageManager.PERMISSION_GRANTED + else + PackageManager.PERMISSION_DENIED + + return mockApplicationInfo + } +} |
