Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 31877a96 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

test: add more test coverage for InstallationEnqueuer

parent 17506e7d
Loading
Loading
Loading
Loading
Loading
+104 −0
Original line number Diff line number Diff line
@@ -19,8 +19,10 @@
package foundation.e.apps.installProcessor

import android.content.Context
import androidx.work.Operation
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.exceptions.InternalException
import com.google.common.util.concurrent.Futures
import foundation.e.apps.R
import foundation.e.apps.data.application.ApplicationRepository
import foundation.e.apps.data.enums.Source
@@ -52,12 +54,14 @@ import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkObject
import io.mockk.verify
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFailsWith

@OptIn(ExperimentalCoroutinesApi::class)
class InstallationEnqueuerTest {
@@ -210,6 +214,100 @@ class InstallationEnqueuerTest {
        }
    }

    @Test
    fun enqueue_returnsFalseWhenCanEnqueueFails() = runTest {
        val appInstall = createPwaInstall()

        mockkObject(InstallWorkManager)
        try {
            coEvery { appManagerWrapper.addDownload(appInstall) } returns false

            val result = enqueuer.enqueue(appInstall, isAnUpdate = true)

            assertFalse(result)
            coVerify(exactly = 0) { appManagerWrapper.updateAwaiting(appInstall) }
            verify(exactly = 0) { InstallWorkManager.enqueueWork(any(), any(), any()) }
        } finally {
            unmockkObject(InstallWorkManager)
        }
    }

    @Test
    fun enqueue_enqueuesUpdateWorkWhenUpdateAndChecksPass() = runTest {
        val appInstall = createPwaInstall()

        mockkObject(InstallWorkManager)
        try {
            coEvery { appManagerWrapper.addDownload(appInstall) } returns true
            coEvery { ageLimiter.allow(appInstall) } returns true
            every { networkStatusChecker.isNetworkAvailable() } returns true
            every { storageSpaceChecker.spaceMissing(appInstall) } returns 0L
            every { InstallWorkManager.getUniqueWorkName(appInstall.packageName) } answers { callOriginal() }
            every { InstallWorkManager.enqueueWork(context, appInstall, true) } returns successfulOperation()

            val result = enqueuer.enqueue(appInstall, isAnUpdate = true)

            assertTrue(result)
            coVerify { appManagerWrapper.updateAwaiting(appInstall) }
            verify(exactly = 1) { InstallWorkManager.enqueueWork(context, appInstall, true) }
        } finally {
            unmockkObject(InstallWorkManager)
        }
    }

    @Test
    fun enqueue_handlesGenericExceptionAndMarksInstallationIssue() = runTest {
        val appInstall = createPwaInstall()

        coEvery { appManagerWrapper.addDownload(appInstall) } returns true
        coEvery { ageLimiter.allow(appInstall) } returns true
        every { networkStatusChecker.isNetworkAvailable() } returns true
        every { storageSpaceChecker.spaceMissing(appInstall) } returns 0L
        coEvery { appManagerWrapper.updateAwaiting(appInstall) } throws IllegalStateException("boom")

        val result = enqueuer.enqueue(appInstall)

        assertFalse(result)
        coVerify { appManagerWrapper.installationIssue(appInstall) }
    }

    @Test
    fun enqueue_rethrowsCancellationException() = runTest {
        val appInstall = createPwaInstall()

        coEvery { appManagerWrapper.addDownload(appInstall) } returns true
        coEvery { ageLimiter.allow(appInstall) } returns true
        every { networkStatusChecker.isNetworkAvailable() } returns true
        every { storageSpaceChecker.spaceMissing(appInstall) } returns 0L
        coEvery { appManagerWrapper.updateAwaiting(appInstall) } throws CancellationException("cancelled")

        assertFailsWith<CancellationException> {
            enqueuer.enqueue(appInstall)
        }
    }

    @Test
    fun enqueue_doesNotWarnAnonymousPaidUsersForSystemApps() = runTest {
        val appInstall = createPwaInstall(isFree = false)

        coEvery { sessionRepository.awaitUser() } returns User.ANONYMOUS
        coEvery {
            playStoreAuthStore.awaitAuthData()
        } returns AuthData(email = "anon@example.com", isAnonymous = true)
        coEvery { appManagerWrapper.addDownload(appInstall) } returns true
        coEvery { ageLimiter.allow(appInstall) } returns true
        every { networkStatusChecker.isNetworkAvailable() } returns true
        every { storageSpaceChecker.spaceMissing(appInstall) } returns 0L

        val result = enqueuer.enqueue(appInstall, isSystemApp = true)

        assertTrue(result)
        assertTrue(appEventDispatcher.events.none {
            it is AppEvent.ErrorMessageEvent && it.data == R.string.paid_app_anonymous_message
        })
        coVerify(exactly = 0) { playStoreAuthStore.awaitAuthData() }
    }

    @Test
    fun canEnqueue_handlesFreeAppNotPurchasedAsRestricted() = runTest {
        val appInstall = createNativeInstall(isFree = true)
@@ -309,6 +407,12 @@ class InstallationEnqueuerTest {
        coVerify(exactly = 0) { appManagerWrapper.addDownload(appInstall) }
    }

    private fun successfulOperation(): Operation {
        val operation = mockk<Operation>()
        every { operation.result } returns Futures.immediateFuture(Operation.SUCCESS)
        return operation
    }

    private fun createPwaInstall(isFree: Boolean = true) = AppInstall(
        type = Type.PWA,
        id = "123",