Skip to content

Commit

Permalink
chore: start migration to ooni tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Nov 18, 2024
1 parent 447b5b8 commit 6687847
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data class Descriptor(
val key: String
get() = when (source) {
is Source.Default -> name
is Source.Installed -> source.value.id.value.toString()
is Source.Installed -> source.value.key
}

val allTests get() = netTests + longRunningTests
Expand All @@ -45,4 +45,18 @@ data class Descriptor(
get() = allTests
.sumOf { it.test.runtime(it.inputs).inWholeSeconds }
.seconds

fun isDefaultDescriptor(): Boolean {
return when (source) {
is Source.Default -> true
is Source.Installed -> source.value.isDefaultTestDescriptor
}
}

fun isInstalledNonDefaultDescriptor(): Boolean {
return when (source) {
is Source.Installed -> !source.value.isDefaultTestDescriptor
else -> false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import kotlinx.datetime.format
import kotlinx.datetime.format.MonthNames
import kotlinx.datetime.format.char
import ooniprobe.composeapp.generated.resources.Dashboard_Runv2_Overview_LastUpdatd
import ooniprobe.composeapp.generated.resources.TestResults_NotAvailable
import ooniprobe.composeapp.generated.resources.months
import ooniprobe.composeapp.generated.resources.performance_datausage
import ooniprobe.composeapp.generated.resources.small_datausage
import ooniprobe.composeapp.generated.resources.test_circumvention
import ooniprobe.composeapp.generated.resources.test_experimental
import ooniprobe.composeapp.generated.resources.test_instant_messaging
import ooniprobe.composeapp.generated.resources.test_performance
import ooniprobe.composeapp.generated.resources.test_websites
import ooniprobe.composeapp.generated.resources.websites_datausage
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.stringArrayResource
import org.ooni.probe.data.TestDescriptor
import org.ooni.probe.shared.InstalledDescriptorIcons
Expand Down Expand Up @@ -53,6 +58,21 @@ data class InstalledTestDescriptorModel(

val isExpired get() = expirationDate != null && expirationDate < LocalDateTime.now()

val isDefaultTestDescriptor get() = id.value in 10470..10474 // TODO(aanorbel): switch to OONI reserved namespace

val key get() = if (isDefaultTestDescriptor) {
when(id.value) {
10470L -> "websites"
10471L -> "instant_messaging"
10472L -> "circumvention"
10473L -> "performance"
10474L -> "experimental"
else -> id.value.toString()
}
} else {
id.value.toString()
}

fun shouldUpdate(other: InstalledTestDescriptorModel): Boolean {
return dateUpdated != null && other.dateUpdated != null && other.dateUpdated > dateUpdated
}
Expand Down Expand Up @@ -80,13 +100,24 @@ fun InstalledTestDescriptorModel.toDescriptor(updateStatus: UpdateStatus = Updat
icon = icon?.let(InstalledDescriptorIcons::getIconFromValue),
color = color?.hexToColor(),
animation = icon?.let { determineAnimation(it) } ?: animation?.let(Animation::fromFileName),
dataUsage = { null },
dataUsage = { if (isDefaultTestDescriptor) stringResource(getDataUsage()) else null },
expirationDate = expirationDate,
netTests = netTests.orEmpty(),
source = Descriptor.Source.Installed(this),
updateStatus = updateStatus,
)

fun InstalledTestDescriptorModel.getDataUsage(): StringResource {
return when(this.key) {
"websites" -> Res.string.websites_datausage
"instant_messaging" -> Res.string.small_datausage
"circumvention" -> Res.string.small_datausage
"performance" -> Res.string.performance_datausage
"experimental" -> Res.string.TestResults_NotAvailable
else -> Res.string.TestResults_NotAvailable
}
}

private val iconAnimationMap = mapOf(
Res.drawable.test_websites to Animation.Websites,
Res.drawable.test_instant_messaging to Animation.InstantMessaging,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ class DashboardViewModel(

private fun List<Descriptor>.groupByType() =
mapOf(
DescriptorType.Default to filter { it.source is Descriptor.Source.Default },
DescriptorType.Installed to filter { it.source is Descriptor.Source.Installed },
DescriptorType.Default to filter { it.isDefaultDescriptor() },
DescriptorType.Installed to filter { it.isInstalledNonDefaultDescriptor() },
)

data class State(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ fun DescriptorScreen(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 16.dp),
)

if (descriptor.source is Descriptor.Source.Installed) {
ConfigureUpdates(onEvent, descriptor.source.value.autoUpdate)
if (descriptor.isInstalledNonDefaultDescriptor()) {
(descriptor.source as Descriptor.Source.Installed?)?.let {
ConfigureUpdates(onEvent, it.value.autoUpdate)
}
}
Text(
stringResource(Res.string.AddDescriptor_Settings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class RunViewModel(
}
mapOf(
DescriptorType.Default to descriptorsWithTests
.filter { it.key.item.source is Descriptor.Source.Default },
.filter { (key, _) -> key.item.isDefaultDescriptor() },
DescriptorType.Installed to descriptorsWithTests
.filter { it.key.item.source is Descriptor.Source.Installed },
.filter { (key, _) -> key.item.isInstalledNonDefaultDescriptor() },
)
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import org.ooni.probe.data.models.DefaultTestDescriptor
import org.ooni.probe.data.models.NetTest

class GetDefaultTestDescriptors {
operator fun invoke(): List<DefaultTestDescriptor> =
listOf(
operator fun invoke(): List<DefaultTestDescriptor> = emptyList()
/*listOf(
WEBSITES,
INSTANT_MESSAGING,
CIRCUMVENTION,
PERFORMANCE,
EXPERIMENTAL,
)
)*/

companion object {
private val WEBSITES =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ fun InstalledDescriptorActionsView(
}
}
}
Button(
onClick = { showDialog = true },
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.error,
contentColor = Color.White,
),
) {
Text(text = stringResource(Res.string.Dashboard_Runv2_Overview_UninstallLink))
if (descriptor.isDefaultTestDescriptor.not()) {
Button(
onClick = { showDialog = true },
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.error,
contentColor = Color.White,
),
) {
Text(text = stringResource(Res.string.Dashboard_Runv2_Overview_UninstallLink))
}
}
}
}
Expand Down
Loading

0 comments on commit 6687847

Please sign in to comment.