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

Setup default OONI tests #63

Merged
merged 1 commit into from
Aug 12, 2024
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
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@

logo.xml
logo.xml
test_experimental.xml
test_performance.xml
test_instant_messaging.xml
test_websites.xml
test_circumvention.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#000"
android:pathData="M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z"/>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<string name="dashboard">Dashboard</string>
<string name="settings">Settings</string>

<string name="Dashboard_RunV2_Ooni_Title">OONI Tests</string>
<string name="Dashboard_RunV2_Title">OONI Run Links</string>

<string name="TestResults_Overview_Title">Test Results</string>
<string name="TestResults_Overview_Tab_Label">Test Results</string>
<string name="TestResults_UnknownASN">Unknown</string>
<string name="TestResults_NotAvailable">N/A</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.ooni.probe.data.models

import androidx.compose.ui.graphics.Color
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource

data class DefaultTestDescriptor(
val label: String,
val title: StringResource,
val shortDescription: StringResource,
val description: StringResource,
val icon: DrawableResource,
val color: Color,
val animation: String,
val dataUsage: StringResource,
var netTests: List<NetTest>,
var longRunningTests: List<NetTest> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ooni.probe.data.models

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import org.jetbrains.compose.resources.DrawableResource

data class Descriptor(
val name: String,
val title: @Composable () -> String,
val shortDescription: @Composable () -> String?,
val description: @Composable () -> String?,
val icon: DrawableResource?,
val color: Color?,
val animation: String?,
val dataUsage: @Composable () -> String?,
val netTests: List<NetTest>,
val longRunningTests: List<NetTest>? = null,
val source: Source,
) {
sealed interface Source {
data class Default(val value: DefaultTestDescriptor) : Source

data class Installed(val value: InstalledTestDescriptorModel) : Source
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package org.ooni.probe.data.models

import kotlinx.datetime.Instant

data class TestDescriptorModel(
data class InstalledTestDescriptorModel(
val id: Id,
val name: String?,
val name: String,
val shortDescription: String?,
val description: String?,
val author: String?,
Expand All @@ -19,7 +19,6 @@ data class TestDescriptorModel(
val dateCreated: Instant?,
val dateUpdated: Instant?,
val revision: String?,
val previousRevision: String?,
val isExpired: Boolean,
val autoUpdate: Boolean,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package org.ooni.probe.data.models

import androidx.compose.ui.text.intl.Locale

typealias LocalizationString = Map<String, String>

fun LocalizationString.getCurrent() = get(Locale.current.language)
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import kotlinx.serialization.Serializable
data class NetTest(
@SerialName("test_name")
val name: String,
val inputs: List<String>?,
val inputs: List<String>? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class ResultModel(
val dataUsageDown: Long?,
val failureMessage: String?,
val networkId: NetworkModel.Id?,
val testDescriptorId: TestDescriptorModel.Id?,
val testDescriptorId: InstalledTestDescriptorModel.Id?,
) {
data class Id(
val value: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import org.ooni.probe.Database
import org.ooni.probe.data.Network
import org.ooni.probe.data.Result
import org.ooni.probe.data.SelectAllWithNetwork
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.NetworkModel
import org.ooni.probe.data.models.ResultListItem
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.models.TestDescriptorModel
import org.ooni.probe.shared.toEpoch
import org.ooni.probe.shared.toLocalDateTime

Expand Down Expand Up @@ -71,7 +71,7 @@ private fun Result.toModel(): ResultModel? {
dataUsageDown = data_usage_down,
failureMessage = failure_msg,
networkId = network_id?.let(NetworkModel::Id),
testDescriptorId = descriptor_runId?.let(TestDescriptorModel::Id),
testDescriptorId = descriptor_runId?.let(InstalledTestDescriptorModel::Id),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.ooni.probe.Database
import org.ooni.probe.data.TestDescriptor
import org.ooni.probe.data.models.TestDescriptorModel
import org.ooni.probe.data.models.InstalledTestDescriptorModel

class TestDescriptorRepository(
private val database: Database,
Expand All @@ -24,7 +24,7 @@ class TestDescriptorRepository(
.mapToList(backgroundDispatcher)
.map { list -> list.mapNotNull { it.toModel() } }

suspend fun create(model: TestDescriptorModel) {
suspend fun create(model: InstalledTestDescriptorModel) {
withContext(backgroundDispatcher) {
database.testDescriptorQueries.insert(
runId = model.id.value,
Expand All @@ -43,17 +43,17 @@ class TestDescriptorRepository(
date_created = model.dateCreated?.toEpochMilliseconds(),
date_updated = model.dateUpdated?.toEpochMilliseconds(),
revision = model.revision,
previous_revision = model.previousRevision,
previous_revision = null,
is_expired = if (model.isExpired) 1 else 0,
auto_update = if (model.autoUpdate) 1 else 0,
)
}
}

private fun TestDescriptor.toModel(): TestDescriptorModel? {
return TestDescriptorModel(
id = runId?.let(TestDescriptorModel::Id) ?: return null,
name = name,
private fun TestDescriptor.toModel(): InstalledTestDescriptorModel? {
return InstalledTestDescriptorModel(
id = runId?.let(InstalledTestDescriptorModel::Id) ?: return null,
name = name.orEmpty(),
shortDescription = short_description,
description = description,
author = author,
Expand All @@ -68,7 +68,6 @@ class TestDescriptorRepository(
dateCreated = date_created?.let(Instant::fromEpochMilliseconds),
dateUpdated = date_updated?.let(Instant::fromEpochMilliseconds),
revision = revision,
previousRevision = previous_revision,
isExpired = is_expired == 1L,
autoUpdate = auto_update == 1L,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import org.ooni.engine.TaskEventMapper
import org.ooni.probe.Database
import org.ooni.probe.data.models.ResultModel
import org.ooni.probe.data.repositories.ResultRepository
import org.ooni.probe.data.repositories.TestDescriptorRepository
import org.ooni.probe.domain.GetDefaultTestDescriptors
import org.ooni.probe.domain.GetResult
import org.ooni.probe.domain.GetResults
import org.ooni.probe.domain.GetTestDescriptors
import org.ooni.probe.shared.PlatformInfo
import org.ooni.probe.ui.dashboard.DashboardViewModel
import org.ooni.probe.ui.result.ResultViewModel
Expand All @@ -36,6 +39,9 @@ class Dependencies(
private val json by lazy { buildJson() }
private val database by lazy { buildDatabase(databaseDriverFactory) }
private val resultRepository by lazy { ResultRepository(database, backgroundDispatcher) }
private val testDescriptorRepository by lazy {
TestDescriptorRepository(database, json, backgroundDispatcher)
}

// Engine

Expand All @@ -56,12 +62,24 @@ class Dependencies(

// Domain

private val getDefaultTestDescriptors by lazy { GetDefaultTestDescriptors() }
private val getResults by lazy { GetResults(resultRepository) }
private val getResult by lazy { GetResult(resultRepository) }
private val getTestDescriptors by lazy {
GetTestDescriptors(
getDefaultTestDescriptors = getDefaultTestDescriptors::invoke,
listInstalledTestDescriptors = testDescriptorRepository::list,
)
}

// ViewModels

val dashboardViewModel get() = DashboardViewModel(engine)
val dashboardViewModel
get() =
DashboardViewModel(
engine = engine,
getTestDescriptors = getTestDescriptors::invoke,
)

fun resultsViewModel(goToResult: (ResultModel.Id) -> Unit) = ResultsViewModel(goToResult, getResults::invoke)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.ooni.probe.domain

import androidx.compose.ui.graphics.Color
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import org.jetbrains.compose.resources.stringResource
import org.ooni.probe.data.models.DefaultTestDescriptor
import org.ooni.probe.data.models.Descriptor
import org.ooni.probe.data.models.InstalledTestDescriptorModel
import org.ooni.probe.data.models.getCurrent

class GetTestDescriptors(
private val getDefaultTestDescriptors: () -> List<DefaultTestDescriptor>,
private val listInstalledTestDescriptors: () -> Flow<List<InstalledTestDescriptorModel>>,
) {
operator fun invoke(): Flow<List<Descriptor>> {
return suspend {
getDefaultTestDescriptors()
.map { it.toDescriptor() }
}.asFlow()
.flatMapLatest { defaultDescriptors ->
listInstalledTestDescriptors()
.map { list -> list.map { it.toDescriptor() } }
.map { defaultDescriptors + it }
}
}

private fun DefaultTestDescriptor.toDescriptor() =
Descriptor(
name = label,
title = { stringResource(title) },
shortDescription = { stringResource(shortDescription) },
description = { stringResource(description) },
icon = icon,
color = color,
animation = animation,
dataUsage = { stringResource(dataUsage) },
netTests = netTests,
longRunningTests = longRunningTests,
source = Descriptor.Source.Default(this),
)

private fun InstalledTestDescriptorModel.toDescriptor() =
Descriptor(
name = name,
title = { nameIntl?.getCurrent() ?: name },
shortDescription = { shortDescriptionIntl?.getCurrent() ?: shortDescription },
description = { descriptionIntl?.getCurrent() ?: description },
// TODO: fetch drawable resource from path
icon = null,
color = color?.filter { it != '#' }?.toIntOrNull()?.let { Color(it) },
animation = animation,
dataUsage = { null },
netTests = netTests.orEmpty(),
source = Descriptor.Source.Installed(this),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
Expand Down Expand Up @@ -49,6 +51,19 @@ fun DashboardScreen(
Text(stringResource(Res.string.run_tests))
}

LazyColumn {
state.tests.forEach { (type, tests) ->
if (state.tests.keys.size > 1 && tests.isNotEmpty()) {
item(type) {
TestDescriptorItem(type)
}
}
items(tests) { test ->
TestDescriptorItem(test)
}
}
}

Text(
text = state.log,
modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()),
Expand Down
Loading