|
| 1 | +package app.gamenative.utils.launchdependencies |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.test.core.app.ApplicationProvider |
| 5 | +import app.gamenative.data.GameSource |
| 6 | +import app.gamenative.service.SteamService |
| 7 | +import com.winlator.container.Container |
| 8 | +import com.winlator.core.TarCompressorUtils |
| 9 | +import com.winlator.xenvironment.ImageFs |
| 10 | +import io.mockk.coEvery |
| 11 | +import io.mockk.every |
| 12 | +import io.mockk.mockk |
| 13 | +import io.mockk.mockkObject |
| 14 | +import io.mockk.mockkStatic |
| 15 | +import io.mockk.slot |
| 16 | +import io.mockk.unmockkAll |
| 17 | +import io.mockk.verify |
| 18 | +import kotlinx.coroutines.Deferred |
| 19 | +import kotlinx.coroutines.runBlocking |
| 20 | +import org.junit.After |
| 21 | +import org.junit.Assert.assertEquals |
| 22 | +import org.junit.Assert.assertFalse |
| 23 | +import org.junit.Assert.assertTrue |
| 24 | +import org.junit.Before |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import org.robolectric.RobolectricTestRunner |
| 28 | +import java.io.File |
| 29 | + |
| 30 | +@RunWith(RobolectricTestRunner::class) |
| 31 | +class BionicDefaultProtonDependencyTest { |
| 32 | + private lateinit var context: Context |
| 33 | + private lateinit var container: Container |
| 34 | + |
| 35 | + @Before |
| 36 | + fun setUp() { |
| 37 | + context = ApplicationProvider.getApplicationContext() |
| 38 | + container = mockk(relaxed = true) |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + fun tearDown() { |
| 43 | + unmockkAll() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun appliesTo_returnsFalse_whenContainerIsNotBionic() { |
| 48 | + every { container.containerVariant } returns "glibc" |
| 49 | + every { container.wineVersion } returns "proton-9.0-arm64ec" |
| 50 | + |
| 51 | + val result = BionicDefaultProtonDependency.appliesTo(container, GameSource.STEAM, 1) |
| 52 | + |
| 53 | + assertFalse(result) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun appliesTo_returnsTrue_whenBionicAndSupportedProtonVersion() { |
| 58 | + every { container.containerVariant } returns Container.BIONIC |
| 59 | + every { container.wineVersion } returns "proton-9.0-arm64ec" |
| 60 | + |
| 61 | + val armResult = BionicDefaultProtonDependency.appliesTo(container, GameSource.STEAM, 2) |
| 62 | + |
| 63 | + every { container.wineVersion } returns "proton-9.0-x86_64" |
| 64 | + val x64Result = BionicDefaultProtonDependency.appliesTo(container, GameSource.STEAM, 3) |
| 65 | + |
| 66 | + assertTrue(armResult) |
| 67 | + assertTrue(x64Result) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun isSatisfied_returnsTrue_whenProtonBinDirectoryExists() { |
| 72 | + every { container.wineVersion } returns "proton-9.0-arm64ec" |
| 73 | + |
| 74 | + val imageFsRoot = ImageFs.find(context).rootDir |
| 75 | + val binDir = File(imageFsRoot, "opt/proton-9.0-arm64ec/bin") |
| 76 | + binDir.mkdirs() |
| 77 | + |
| 78 | + val result = BionicDefaultProtonDependency.isSatisfied(context, container, GameSource.STEAM, 4) |
| 79 | + |
| 80 | + assertTrue(result) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun getLoadingMessage_returnsExpectedMessagePerVersion() { |
| 85 | + every { container.wineVersion } returns "proton-9.0-arm64ec" |
| 86 | + val armMessage = BionicDefaultProtonDependency.getLoadingMessage(context, container, GameSource.STEAM, 5) |
| 87 | + |
| 88 | + every { container.wineVersion } returns "proton-9.0-x86_64" |
| 89 | + val x64Message = BionicDefaultProtonDependency.getLoadingMessage(context, container, GameSource.STEAM, 6) |
| 90 | + |
| 91 | + every { container.wineVersion } returns "wine-ge-custom" |
| 92 | + val fallbackMessage = BionicDefaultProtonDependency.getLoadingMessage(context, container, GameSource.STEAM, 7) |
| 93 | + |
| 94 | + assertEquals("Downloading arm64ec Proton", armMessage) |
| 95 | + assertEquals("Downloading x86_64 Proton", x64Message) |
| 96 | + assertEquals("Extracting Proton", fallbackMessage) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun install_returnsEarly_whenProtonVersionIsUnsupported() = runBlocking { |
| 101 | + every { container.wineVersion } returns "wine-ge-custom" |
| 102 | + mockkObject(SteamService.Companion) |
| 103 | + mockkStatic(TarCompressorUtils::class) |
| 104 | + |
| 105 | + BionicDefaultProtonDependency.install( |
| 106 | + context = context, |
| 107 | + container = container, |
| 108 | + callbacks = LaunchDependencyCallbacks({}, {}), |
| 109 | + gameSource = GameSource.STEAM, |
| 110 | + gameId = 8, |
| 111 | + ) |
| 112 | + |
| 113 | + verify(exactly = 0) { |
| 114 | + SteamService.downloadFile( |
| 115 | + onDownloadProgress = any(), |
| 116 | + parentScope = any(), |
| 117 | + context = any(), |
| 118 | + fileName = any(), |
| 119 | + ) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun install_downloadsArchive_andForwardsProgress() = runBlocking { |
| 125 | + every { container.wineVersion } returns "proton-9.0-arm64ec" |
| 126 | + mockkObject(SteamService.Companion) |
| 127 | + |
| 128 | + val imageFsRoot = ImageFs.find(context).rootDir |
| 129 | + val binDir = File(imageFsRoot, "opt/proton-9.0-arm64ec/bin") |
| 130 | + binDir.mkdirs() |
| 131 | + |
| 132 | + every { SteamService.isFileInstallable(context, "proton-9.0-arm64ec.txz") } returns false |
| 133 | + |
| 134 | + val onProgressSlot = slot<(Float) -> Unit>() |
| 135 | + val deferred = mockk<Deferred<Unit>>() |
| 136 | + every { |
| 137 | + SteamService.downloadFile( |
| 138 | + onDownloadProgress = capture(onProgressSlot), |
| 139 | + parentScope = any(), |
| 140 | + context = context, |
| 141 | + fileName = "proton-9.0-arm64ec.txz", |
| 142 | + ) |
| 143 | + } returns deferred |
| 144 | + coEvery { deferred.await() } returns Unit |
| 145 | + |
| 146 | + val progressValues = mutableListOf<Float>() |
| 147 | + BionicDefaultProtonDependency.install( |
| 148 | + context = context, |
| 149 | + container = container, |
| 150 | + callbacks = LaunchDependencyCallbacks( |
| 151 | + setLoadingMessage = {}, |
| 152 | + setLoadingProgress = { progressValues += it }, |
| 153 | + ), |
| 154 | + gameSource = GameSource.STEAM, |
| 155 | + gameId = 9, |
| 156 | + ) |
| 157 | + |
| 158 | + onProgressSlot.captured(0.42f) |
| 159 | + |
| 160 | + verify(exactly = 1) { |
| 161 | + SteamService.downloadFile( |
| 162 | + onDownloadProgress = any(), |
| 163 | + parentScope = any(), |
| 164 | + context = context, |
| 165 | + fileName = "proton-9.0-arm64ec.txz", |
| 166 | + ) |
| 167 | + } |
| 168 | + assertEquals(listOf(0.42f), progressValues) |
| 169 | + } |
| 170 | +} |
0 commit comments