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

Verified Commit 4a4b5b73 authored by Saalim Quadri's avatar Saalim Quadri
Browse files

feat: tests: Write a unit test to check archDownloadUrls

parent fb2b11d7
Loading
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2025
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * 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.data.cleanapk

import foundation.e.apps.data.cleanapk.repositories.CleanApkAppsRepository
import foundation.e.apps.data.cleanapk.repositories.HomeConverter
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations

/**
 * Unit tests for CleanApkAppsRepository.
 *
 * These unit tests verify the repository can be instantiated correctly.
 */
class CleanApkAppsRepositoryTest {

    @Mock
    private lateinit var cleanApkRetrofit: CleanApkRetrofit

    @Mock
    private lateinit var homeConverter: HomeConverter

    @Mock
    private lateinit var cleanApkSearchHelper: CleanApkSearchHelper

    private lateinit var repository: CleanApkAppsRepository

    @Before
    fun setup() {
        MockitoAnnotations.openMocks(this)

        repository = CleanApkAppsRepository(
            cleanApkRetrofit,
            homeConverter,
            cleanApkSearchHelper
        )
    }

    @Test
    fun `repository can be instantiated`() {
        assert(repository is CleanApkAppsRepository)
    }
}
+192 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2025
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * 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.data.gitlab

import android.content.Context
import android.text.format.Formatter
import foundation.e.apps.data.gitlab.models.SystemAppInfo
import foundation.e.apps.data.gitlab.models.toApplication
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import kotlin.test.assertFailsWith

/**
 * Unit tests for SystemAppInfo architecture-based URL selection.
 *
 */
class SystemAppInfoTest {

    @Mock
    private lateinit var context: Context

    private lateinit var formatterMocked: MockedStatic<Formatter>

    @Before
    fun setup() {
        MockitoAnnotations.openMocks(this)
        formatterMocked = Mockito.mockStatic(Formatter::class.java)
        formatterMocked.`when`<String> { Formatter.formatFileSize(context, 1000L) }
            .thenReturn("1 KB")
    }

    @After
    fun tearDown() {
        formatterMocked.close()
    }

    @Test
    fun `selectUrlForArchitecture falls back to generic downloadUrl when archDownloadUrls is null`() {
        val systemAppInfo = SystemAppInfo(
            name = "Example App",
            packageName = "com.example.app",
            versionCode = 100,
            minSdk = 21,
            versionName = "1.0.0",
            downloadUrl = "https://example.com/app.apk",
            archDownloadUrls = null,
            size = 1000L,
            authorName = "Author",
            priority = false,
            blockedAndroid = null,
            blockedDevices = null
        )

        val app = systemAppInfo.toApplication(context)

        assertEquals("https://example.com/app.apk", app.url)
    }

    @Test
    fun `selectUrlForArchitecture falls back to generic downloadUrl when archDownloadUrls is empty`() {
        val systemAppInfo = SystemAppInfo(
            name = "Example App",
            packageName = "com.example.app",
            versionCode = 100,
            minSdk = 21,
            versionName = "1.0.0",
            downloadUrl = "https://example.com/app-generic.apk",
            archDownloadUrls = emptyMap(),
            size = 1000L,
            authorName = "Author",
            priority = false,
            blockedAndroid = null,
            blockedDevices = null
        )

        val app = systemAppInfo.toApplication(context)

        assertEquals("https://example.com/app-generic.apk", app.url)
    }

    @Test
    fun `selectUrlForArchitecture throws error when no URLs are available`() {
        val systemAppInfo = SystemAppInfo(
            name = "Example App",
            packageName = "com.example.app",
            versionCode = 100,
            minSdk = 21,
            versionName = "1.0.0",
            downloadUrl = null,
            archDownloadUrls = null,
            size = 1000L,
            authorName = "Author",
            priority = false,
            blockedAndroid = null,
            blockedDevices = null
        )

        val exception = assertFailsWith<IllegalStateException> {
            systemAppInfo.toApplication(context)
        }

        assert(exception.message!!.contains("No download URL provided"))
    }

    @Test
    fun `toApplication creates valid Application object with generic URL`() {
        val systemAppInfo = SystemAppInfo(
            name = "Test App",
            packageName = "com.example.testapp",
            versionCode = 42,
            minSdk = 21,
            versionName = "2.0.0",
            downloadUrl = "https://example.com/app.apk",
            archDownloadUrls = null, // No architecture-specific URLs
            size = 1000L,
            authorName = "Test Author",
            priority = true,
            blockedAndroid = null,
            blockedDevices = null
        )

        val app = systemAppInfo.toApplication(context)

        assertEquals("com.example.testapp", app.package_name)
        assertEquals(42L, app.latest_version_code)
        assertEquals("2.0.0", app.latest_version_number)
        assertEquals("Test Author", app.author)
        assertEquals(1000L, app.originalSize)
        assertEquals("1 KB", app.appSize)
        assertEquals(true, app.isSystemApp)
        assertEquals("https://example.com/app.apk", app.url)
        assertNotNull(app.url)
    }

    @Test
    fun `SystemAppInfo data class holds correct values`() {
        val systemAppInfo = SystemAppInfo(
            name = "My App",
            packageName = "com.test.myapp",
            versionCode = 123,
            minSdk = 28,
            versionName = "3.0.0",
            downloadUrl = "https://example.com/myapp.apk",
            archDownloadUrls = mapOf(
                "arm64-v8a" to "https://example.com/myapp-arm64.apk",
                "x86_64" to "https://example.com/myapp-x86_64.apk"
            ),
            size = 5000L,
            authorName = "Test Publisher",
            priority = true,
            blockedAndroid = listOf(27, 28),
            blockedDevices = listOf("device1", "device2")
        )

        assertEquals("My App", systemAppInfo.name)
        assertEquals("com.test.myapp", systemAppInfo.packageName)
        assertEquals(123L, systemAppInfo.versionCode)
        assertEquals(28, systemAppInfo.minSdk)
        assertEquals("3.0.0", systemAppInfo.versionName)
        assertEquals("https://example.com/myapp.apk", systemAppInfo.downloadUrl)
        assertNotNull(systemAppInfo.archDownloadUrls)
        assertEquals(2, systemAppInfo.archDownloadUrls?.size)
        assertEquals(5000L, systemAppInfo.size)
        assertEquals("Test Publisher", systemAppInfo.authorName)
        assertEquals(true, systemAppInfo.priority)
        assertEquals(listOf(27, 28), systemAppInfo.blockedAndroid)
        assertEquals(listOf("device1", "device2"), systemAppInfo.blockedDevices)
    }
}