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

Verified Commit 2ea0b020 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: simplify InstallationEnqueuer and add more test coverage

parent 3b342b55
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -91,11 +91,14 @@ class InstallationEnqueuer @Inject constructor(
        isSystemApp: Boolean
    ): Boolean {
        val user = sessionRepository.awaitUser()
        if (!isSystemApp && (user == User.GOOGLE || user == User.ANONYMOUS)) {
        return when {
            isSystemApp -> false
            user == User.ANONYMOUS -> {
                val authData = playStoreAuthStore.awaitAuthData()
            return !appInstall.isFree && authData?.isAnonymous == true
        } else {
            return false
                !appInstall.isFree && authData?.isAnonymous == true
            }

            else -> false
        }
    }

+63 −0
Original line number Diff line number Diff line
@@ -308,6 +308,69 @@ class InstallationEnqueuerTest {
        coVerify(exactly = 0) { playStoreAuthStore.awaitAuthData() }
    }

    @Test
    fun enqueue_allowsPaidAppForGoogleUser() = runTest {
        val appInstall = createNativeInstall(isFree = false)

        coEvery { sessionRepository.awaitUser() } returns User.GOOGLE
        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)

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

    @Test
    fun enqueue_allowsFreeAppForAnonymousUser() = runTest {
        val appInstall = createNativeInstall()

        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)

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

    @Test
    fun enqueue_doesNotLookupAuthForNoGoogleUser() = runTest {
        val appInstall = createNativeInstall()

        coEvery { sessionRepository.awaitUser() } returns User.NO_GOOGLE
        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)

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

    @Test
    fun canEnqueue_handlesFreeAppNotPurchasedAsRestricted() = runTest {
        val appInstall = createNativeInstall(isFree = true)