Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId = AppConfig.appId
minSdk = AppConfig.minSdkVersion
targetSdk = AppConfig.targetSdkVersion
versionCode = 20
versionName = "3.0.1"
versionCode = 21
versionName = "3.0.2"
vectorDrawables.useSupportLibrary = true
androidResources {
localeFilters += listOf("en", "de", "pt-rBR", "tr")
Expand Down
2 changes: 2 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ dependencies {
ksp(libs.androidx.room.compiler)

testImplementation(libs.junit)
testImplementation(libs.mockk.android)
testImplementation(libs.kotlinx.coroutines.test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.androidvip.sysctlgui.domain.models.KernelParam
import com.androidvip.sysctlgui.domain.models.ParamDocumentation
import com.androidvip.sysctlgui.domain.repository.AppPrefs
import com.androidvip.sysctlgui.domain.repository.DocumentationRepository
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull

/**
Expand Down Expand Up @@ -36,6 +35,6 @@ class DocumentationRepositoryImpl(
}

companion object {
private const val REQUEST_TIMEOUT_MS = 3000L
internal const val REQUEST_TIMEOUT_MS = 3000L
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.androidvip.sysctlgui.data.repository

import com.androidvip.sysctlgui.data.source.DocumentationDataSource
import com.androidvip.sysctlgui.domain.models.KernelParam
import com.androidvip.sysctlgui.domain.models.ParamDocumentation
import io.mockk.called
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
import org.junit.Test

class DocumentationRepositoryImplTest {
private val offlineDataSource: DocumentationDataSource = mockk(relaxed = true)
private val onlineDataSource: DocumentationDataSource = mockk(relaxed = true)
private lateinit var repository: DocumentationRepositoryImpl

private val testParam = KernelParam(
name = "vm.swappiness",
path = "/proc/sys/vm/swappiness",
value = "100"
)

private val expectedDocumentation = ParamDocumentation(
title = "Swappiness",
documentationText = "Controls swappiness",
url = "https://docs.kernel.org/admin-guide/sysctl/vm.html#swappiness"
)

@Before
fun setUp() {
repository = DocumentationRepositoryImpl(offlineDataSource, onlineDataSource)
}

@Test
fun `getDocumentation when online is true and online source succeeds then return online documentation`() =
runTest {
// Given
coEvery { onlineDataSource.getDocumentation(testParam) } returns expectedDocumentation

// When
val result = repository.getDocumentation(testParam, online = true)

// Then
coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) }
verify { offlineDataSource wasNot called }

Assert.assertEquals(expectedDocumentation, result)
}

@Test
fun `getDocumentation when online is true and online source times out then return offline documentation`() = runTest {
// Given
coEvery { onlineDataSource.getDocumentation(testParam) } coAnswers {
delay(DocumentationRepositoryImpl.REQUEST_TIMEOUT_MS + 100)
null
}

coEvery { onlineDataSource.getDocumentation(testParam) } returns null
coEvery { offlineDataSource.getDocumentation(testParam) } returns expectedDocumentation

// When
val result = repository.getDocumentation(testParam, online = true)

// Then
coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) }
coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) }
Assert.assertEquals(expectedDocumentation, result)
}

@Test
fun `getDocumentation when online is false then return offline documentation`() = runTest {
// Given
coEvery { offlineDataSource.getDocumentation(testParam) } returns expectedDocumentation

// When
val result = repository.getDocumentation(testParam, online = false)

// Then
coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) }
verify { onlineDataSource wasNot called }
Assert.assertEquals(expectedDocumentation, result)
}

@Test
fun `getDocumentation when online is true and both sources return null then return null`() = runTest {
// Given
coEvery { onlineDataSource.getDocumentation(testParam) } returns null
coEvery { offlineDataSource.getDocumentation(testParam) } returns null

// When
val result = repository.getDocumentation(testParam, online = true)

// Then
coVerify(exactly = 1) { onlineDataSource.getDocumentation(testParam) }
coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) }
Assert.assertNull(result)
}

@Test
fun `getDocumentation when online is false and offline source returns null then return null`() = runTest {
// Given
coEvery { offlineDataSource.getDocumentation(testParam) } returns null

// When
val result = repository.getDocumentation(testParam, online = false)

// Then
coVerify(exactly = 1) { offlineDataSource.getDocumentation(testParam) }
verify { onlineDataSource wasNot called }
Assert.assertNull(result)
}
}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ composeBom = "2025.07.00"
appcompat = "1.7.1"
materialIconsCoreCompose = "1.7.8"
material = "1.12.0"
mockkAndroid = "1.14.5"
multidex = "2.0.1"
navigationCompose = "2.9.3"
preference = "1.2.1"
Expand All @@ -39,6 +40,7 @@ androidx-navigation-compose = { module = "androidx.navigation:navigation-compose
androidx-preference = { module = "androidx.preference:preference", version.ref = "preference" }
androidx-window = { module = "androidx.window:window", version.ref = "window" }
androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutinesAndroid" }
libsu-core = { module = "com.github.topjohnwu.libsu:core", version.ref = "libsu" }
libsu-nio = { module = "com.github.topjohnwu.libsu:nio", version.ref = "libsu" }
jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" }
Expand Down Expand Up @@ -73,6 +75,7 @@ ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "k
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
material = { module = "com.google.android.material:material", version.ref = "material" }
mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockkAndroid" }

[bundles]
ktor-clients = ["ktor-client-core", "ktor-client-android", "ktor-client-logging"]
Expand Down