Skip to content

Commit

Permalink
Update RunTestsActivity to save preferences just before running the…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
aanorbel committed Feb 5, 2024
1 parent 8a1f8b1 commit fd04b3c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import org.openobservatory.ooniprobe.activity.runtests.adapter.RunTestsExpandabl
import org.openobservatory.ooniprobe.activity.runtests.models.ChildItem
import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem
import org.openobservatory.ooniprobe.common.OONIDescriptor
import org.openobservatory.ooniprobe.common.OONITests
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.enableTest
import org.openobservatory.ooniprobe.common.disableTest
import org.openobservatory.ooniprobe.databinding.ActivityRunTestsBinding
import java.io.Serializable
import javax.inject.Inject
Expand Down Expand Up @@ -104,6 +107,7 @@ class RunTestsActivity : AbstractActivity() {
private fun onMenuItemClickListener(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.runButton -> {
updatePreferences()
val selectedChildItems: List<String> = getChildItemsSelectedIdList()
if (selectedChildItems.isNotEmpty()) {
val testSuitesToRun = getGroupItemsAtLeastOneChildEnabled().map { groupItem ->
Expand All @@ -124,6 +128,31 @@ class RunTestsActivity : AbstractActivity() {
}
}

/**
* Update the preferences based on the selected tests.
* This method is used to update the preferences when the user has selected the tests that they want to run.
*/
private fun updatePreferences() {
for (i in 0 until adapter.groupCount) {
val group = adapter.getGroup(i)
when (group.name) {
OONITests.EXPERIMENTAL.label -> {
val testNames = OONITests.EXPERIMENTAL.nettests.map { it.name };
when(group.nettests.filter { testNames.contains(it.name) }.map { it.selected }.all { it }) {
true -> preferenceManager.enableTest(OONITests.EXPERIMENTAL.label)
false -> preferenceManager.disableTest(OONITests.EXPERIMENTAL.label)
}
}
else -> group.nettests.forEach { nettest ->
when(nettest.selected) {
true -> preferenceManager.enableTest(nettest.name)
false -> preferenceManager.disableTest(nettest.name)
}
}
}
}
}

private fun selectAllBtnStatusObserver(selectAllBtnStatus: String?) {
if (!TextUtils.isEmpty(selectAllBtnStatus)) {
when (selectAllBtnStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,6 @@ class RunTestsViewModel() : ViewModel() {

fun setSelectedAllBtnStatus(selectedStatus: String) {
selectedAllBtnStatus.postValue(selectedStatus)
when (selectedStatus) {
SELECT_ALL -> {
OONITests.INSTANT_MESSAGING.nettests.forEach {
enableTest(it.name)
}
OONITests.CIRCUMVENTION.nettests.forEach {
enableTest(it.name)
}
OONITests.PERFORMANCE.nettests.forEach {
enableTest(it.name)
}
enableTest(OONITests.EXPERIMENTAL.label)
}

SELECT_NONE -> {
OONITests.INSTANT_MESSAGING.nettests.forEach {
disableTest(it.name)
}
OONITests.CIRCUMVENTION.nettests.forEach {
disableTest(it.name)
}
OONITests.PERFORMANCE.nettests.forEach {
disableTest(it.name)
}
disableTest(OONITests.EXPERIMENTAL.label)
}
}
}

fun disableTest(name: String) {
preferenceManager.disableTest(name)
}

fun enableTest(name: String) {
preferenceManager.enableTest(name)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,11 @@ class RunTestsExpandableListViewAdapter(
if (groupItem.selected) {
if (isSelectAllChildItems(groupItem.nettests)) {
groupSelectionIndicator.setImageResource(R.drawable.check_box)
// NOTE: This is the only place where OONITests.EXPERIMENTAL.label is used.
// This doesn't follow the normal rule where the component tests make up the suite.
if (groupItem.name == OONITests.EXPERIMENTAL.label) {
viewModel.enableTest(OONITests.EXPERIMENTAL.label)
}
} else {
groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank)
if (groupItem.name == OONITests.EXPERIMENTAL.label) {
viewModel.disableTest(OONITests.EXPERIMENTAL.label)
}
}
} else {
groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank)
if (groupItem.name == OONITests.EXPERIMENTAL.label) {
viewModel.disableTest(OONITests.EXPERIMENTAL.label)
}
}
groupSelectionIndicator.setOnClickListener {
if (groupItem.selected && isSelectAllChildItems(groupItem.nettests)) {
Expand Down Expand Up @@ -193,7 +182,6 @@ class RunTestsExpandableListViewAdapter(
setOnClickListener {
if (childItem.selected) {
childItem.selected = false
viewModel.disableTest(childItem.name)
if (isNotSelectedAnyChildItems(groupItem.nettests)) {
groupItem.selected = false
}
Expand All @@ -204,7 +192,6 @@ class RunTestsExpandableListViewAdapter(
}
} else {
childItem.selected = true
viewModel.enableTest(childItem.name)
groupItem.selected = true
if (isSelectedAllItems(groupedListData)) {
viewModel.setSelectedAllBtnStatus(SELECT_ALL)
Expand Down

0 comments on commit fd04b3c

Please sign in to comment.