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

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

test: cover install worker result handling

Lock down the worker boundary from the install refactor so input validation and processor result mapping stay stable.
parent 281ad165
Loading
Loading
Loading
Loading
Loading
+107 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2026 e Foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.installProcessor

import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.work.Data
import com.google.common.truth.Truth.assertThat
import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.install.workmanager.AppInstallProcessor
import foundation.e.apps.data.install.workmanager.InstallAppWorker
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.R])
@OptIn(ExperimentalCoroutinesApi::class)
class InstallAppWorkerTest {
    private lateinit var appInstallProcessor: AppInstallProcessor

    @Before
    fun setup() {
        appInstallProcessor = mockk(relaxed = true)
    }

    @Test
    fun doWork_returnsFailureWhenFusedDownloadIdIsMissing() = runTest {
        val worker = createWorker(Data.EMPTY)

        val result = worker.doWork()

        assertThat(result).isEqualTo(androidx.work.ListenableWorker.Result.failure())
        coVerify(exactly = 0) { appInstallProcessor.processInstall(any(), any(), any()) }
    }

    @Test
    fun doWork_returnsSuccessWhenProcessorSucceeds() = runTest {
        coEvery {
            appInstallProcessor.processInstall("123", true, any())
        } returns Result.success(ResultStatus.OK)
        val worker = createWorker(
            Data.Builder()
                .putString(InstallAppWorker.INPUT_DATA_FUSED_DOWNLOAD, "123")
                .putBoolean(InstallAppWorker.IS_UPDATE_WORK, true)
                .build()
        )

        val result = worker.doWork()

        assertThat(result).isEqualTo(androidx.work.ListenableWorker.Result.success())
        coVerify { appInstallProcessor.processInstall("123", true, any()) }
    }

    @Test
    fun doWork_returnsFailureWhenProcessorFails() = runTest {
        coEvery {
            appInstallProcessor.processInstall("123", false, any())
        } returns Result.failure(IllegalStateException("boom"))
        val worker = createWorker(
            Data.Builder()
                .putString(InstallAppWorker.INPUT_DATA_FUSED_DOWNLOAD, "123")
                .putBoolean(InstallAppWorker.IS_UPDATE_WORK, false)
                .build()
        )

        val result = worker.doWork()

        assertThat(result).isEqualTo(androidx.work.ListenableWorker.Result.failure())
        coVerify { appInstallProcessor.processInstall("123", false, any()) }
    }

    private fun createWorker(inputData: Data): InstallAppWorker {
        val params = mockk<androidx.work.WorkerParameters>(relaxed = true)
        every { params.inputData } returns inputData

        return InstallAppWorker(
            ApplicationProvider.getApplicationContext(),
            params,
            appInstallProcessor
        )
    }
}