Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ooni test descriptors. #275

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
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,24 @@ data class Descriptor(
get() = allTests
.sumOf { it.test.runtime(it.inputs).inWholeSeconds }
.seconds

val settingsPrefix: String?
get() = when (isDefaultDescriptor()) {
true -> null
else -> (source as Source.Installed).value.id.value.toString()
}

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,11 +13,16 @@ import ooniprobe.composeapp.generated.resources.Common_Months
import ooniprobe.composeapp.generated.resources.Dashboard_Runv2_Overview_Description
import ooniprobe.composeapp.generated.resources.Dashboard_Runv2_Overview_LastUpdated
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.TestResults_NotAvailable
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.jetbrains.compose.resources.stringResource
import org.ooni.probe.data.TestDescriptor
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also check if they are in OoniTest now.


val key get() = if (isDefaultTestDescriptor) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more like a settings key now no? Just to make it clear why we still need this. Because for all other purposes now we can use the ID.

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 @@ -181,8 +181,7 @@ class PreferenceRepository(
isAutoRun: Boolean,
) = getPreferenceKey(
name = netTest.test.name,
prefix = (descriptor.source as? Descriptor.Source.Installed)
?.value?.id?.value?.toString(),
prefix = descriptor.settingsPrefix,
autoRun = isAutoRun,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import org.ooni.probe.domain.FinishInProgressData
import org.ooni.probe.domain.GetAutoRunSettings
import org.ooni.probe.domain.GetAutoRunSpecification
import org.ooni.probe.domain.GetBootstrapTestDescriptors
import org.ooni.probe.domain.GetDefaultTestDescriptors
import org.ooni.probe.domain.GetEnginePreferences
import org.ooni.probe.domain.GetFirstRun
import org.ooni.probe.domain.GetProxySettings
Expand Down Expand Up @@ -244,7 +243,6 @@ class Dependencies(
private val getBootstrapTestDescriptors by lazy {
GetBootstrapTestDescriptors(readAssetFile, json, backgroundContext)
}
private val getDefaultTestDescriptors by lazy { GetDefaultTestDescriptors() }
private val getProxySettings by lazy { GetProxySettings(preferenceRepository) }
private val getEnginePreferences by lazy {
GetEnginePreferences(
Expand Down Expand Up @@ -288,7 +286,7 @@ class Dependencies(
@VisibleForTesting
val getTestDescriptors by lazy {
GetTestDescriptors(
getDefaultTestDescriptors = getDefaultTestDescriptors::invoke,
getDefaultTestDescriptors = { emptyList() },
listInstalledTestDescriptors = testDescriptorRepository::list,
descriptorUpdates = getDescriptorUpdate::observeAvailableUpdatesState,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,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 @@ -81,9 +81,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this is empty, it can be removed no?

Empty file.

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