diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0b4b5e9f5..03676c9e1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -53,6 +53,7 @@ android:name=".activity.OverviewActivity" android:launchMode="singleTop" android:parentActivityName=".activity.MainActivity" + android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar" android:screenOrientation="userPortrait" /> editTexts; private ArrayList deletes; @Inject PreferenceManager preferenceManager; + + @Inject + TestDescriptorManager descriptorManager; private ActivityCustomwebsiteBinding binding; @Override @@ -47,11 +56,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { if (Patterns.WEB_URL.matcher(sanitizedUrl).matches() && sanitizedUrl.length() < 2084) urls.add(Url.checkExistingUrl(sanitizedUrl).toString()); } - WebsitesSuite suite = new WebsitesSuite(); - suite.getTestList(preferenceManager)[0].setInputs(urls); + DynamicTestSuite suite = descriptorManager.getTestByDescriptorName(OONITests.WEBSITES.getLabel()); + if (suite != null) { + suite.getTestList(preferenceManager)[0].setInputs(urls); - RunningActivity.runAsForegroundService(CustomWebsiteActivity.this, suite.asArray(), this::finish, preferenceManager); - return true; + RunningActivity.runAsForegroundService(CustomWebsiteActivity.this, suite.asArray(), this::finish, preferenceManager); + return true; + } + return false; }); binding.add.setOnClickListener(v -> add()); add(); @@ -74,7 +86,7 @@ public void onBackPressed() { super.onBackPressed(); } - public boolean checkPrefix(){ + public boolean checkPrefix() { boolean prefix = true; for (EditText editText : editTexts) if (!editText.getText().toString().contains("http://") diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java index 9eada3e2d..d1843a4f8 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java @@ -15,6 +15,7 @@ import com.google.gson.Gson; import localhost.toolkit.app.fragment.ConfirmDialogFragment; import org.openobservatory.ooniprobe.R; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ResubmitTask; import org.openobservatory.ooniprobe.databinding.ActivityMeasurementDetailBinding; @@ -25,7 +26,6 @@ import org.openobservatory.ooniprobe.fragment.resultHeader.ResultHeaderDetailFragment; import org.openobservatory.ooniprobe.model.database.Measurement; import org.openobservatory.ooniprobe.model.database.Network; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; import org.openobservatory.ooniprobe.test.test.*; import io.noties.markwon.Markwon; @@ -70,8 +70,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { measurement.result.load(); setTheme(measurement.is_failed ? R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Failed : - measurement.result.test_group_name.equals(PerformanceSuite.NAME) ? - measurement.result.getTestSuite().getThemeLight() : + measurement.result.test_group_name.equals(OONITests.PERFORMANCE.getLabel()) ? + measurement.result.getTestSuite(this).getThemeLight() : measurement.is_anomaly ? R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Failure : R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Success); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java index 296232913..8f96db785 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java @@ -5,20 +5,21 @@ import android.os.Bundle; import android.text.format.DateUtils; import android.view.View; +import android.view.Window; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.core.text.TextUtilsCompat; import androidx.core.view.ViewCompat; +import org.openobservatory.engine.BaseNettest; import org.openobservatory.ooniprobe.R; +import org.openobservatory.ooniprobe.common.OONIDescriptor; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReadMorePlugin; import org.openobservatory.ooniprobe.databinding.ActivityOverviewBinding; import org.openobservatory.ooniprobe.model.database.Result; -import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; -import org.openobservatory.ooniprobe.test.test.AbstractTest; import java.util.Locale; @@ -27,77 +28,75 @@ import io.noties.markwon.Markwon; public class OverviewActivity extends AbstractActivity { - private static final String TEST = "test"; - - ActivityOverviewBinding binding; - private AbstractSuite testSuite; - - @Inject - PreferenceManager preferenceManager; - - public static Intent newIntent(Context context, AbstractSuite testSuite) { - return new Intent(context, OverviewActivity.class).putExtra(TEST, testSuite); - } - - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getActivityComponent().inject(this); - testSuite = (AbstractSuite) getIntent().getSerializableExtra(TEST); - setTheme(testSuite.getThemeLight()); - binding = ActivityOverviewBinding.inflate(getLayoutInflater()); - setContentView(binding.getRoot()); - setSupportActionBar(binding.toolbar); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - setTitle(testSuite.getTitle()); - binding.icon.setImageResource(testSuite.getIcon()); - binding.customUrl.setVisibility(testSuite.getName().equals(WebsitesSuite.NAME) ? View.VISIBLE : View.GONE); - Markwon markwon = Markwon.builder(this) - .usePlugin(new ReadMorePlugin( - getString(R.string.OONIRun_ReadMore), - getString(R.string.OONIRun_ReadLess)) - ).build(); - if (testSuite.getName().equals(ExperimentalSuite.NAME)) { - String experimentalLinks = - "\n\n* [STUN Reachability](https://github.com/ooni/spec/blob/master/nettests/ts-025-stun-reachability.md)" + - "\n\n* [DNS Check](https://github.com/ooni/spec/blob/master/nettests/ts-028-dnscheck.md)" + - "\n\n* [ECH Check](https://github.com/ooni/spec/blob/master/nettests/ts-039-echcheck.md)" + - "\n\n* [Tor Snowflake](https://ooni.org/nettest/tor-snowflake/) "+ String.format(" ( %s )",getString(R.string.Settings_TestOptions_LongRunningTest))+ - "\n\n* [Vanilla Tor](https://github.com/ooni/spec/blob/master/nettests/ts-016-vanilla-tor.md) " + String.format(" ( %s )",getString(R.string.Settings_TestOptions_LongRunningTest)); - markwon.setMarkdown(binding.desc, getString(testSuite.getDesc1(), experimentalLinks)); - if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL) { + private static final String TEST = "test"; + + ActivityOverviewBinding binding; + + @Inject + PreferenceManager preferenceManager; + private OONIDescriptor descriptor; + + public static Intent newIntent(Context context, OONIDescriptor descriptor) { + return new Intent(context, OverviewActivity.class).putExtra(TEST, descriptor); + } + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getActivityComponent().inject(this); + descriptor = (OONIDescriptor) getIntent().getSerializableExtra(TEST); + binding = ActivityOverviewBinding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); + setSupportActionBar(binding.toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setTitle(descriptor.getTitle()); + setThemeColor(ContextCompat.getColor(this, descriptor.getColor())); + binding.icon.setImageResource(descriptor.getDisplayIcon(this)); + binding.customUrl.setVisibility(descriptor.getName().equals(OONITests.WEBSITES.name()) ? View.VISIBLE : View.GONE); + Markwon markwon = Markwon.builder(this).usePlugin(new ReadMorePlugin(getString(R.string.OONIRun_ReadMore), getString(R.string.OONIRun_ReadLess))).build(); + if (descriptor.getName().equals(OONITests.EXPERIMENTAL.name())) { + markwon.setMarkdown(binding.desc, descriptor.getDescription()); + if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL) { binding.desc.setTextDirection(View.TEXT_DIRECTION_RTL); } - } - else { - markwon.setMarkdown(binding.desc, getString(testSuite.getDesc1())); + } else { + markwon.setMarkdown(binding.desc, descriptor.getDescription()); } - Result lastResult = Result.getLastResult(testSuite.getName()); - if (lastResult == null) - binding.lastTime.setText(R.string.Dashboard_Overview_LastRun_Never); - else - binding.lastTime.setText(DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime())); - - setUpOnCLickListeners(); - } - - private void setUpOnCLickListeners() { - binding.customUrl.setOnClickListener(view -> customUrlClick()); - } - - @Override protected void onResume() { - super.onResume(); - testSuite.setTestList((AbstractTest[]) null); - testSuite.getTestList(preferenceManager); - binding.runtime.setText(getString(R.string.twoParam, getString(testSuite.getDataUsage()), getString(R.string.Dashboard_Card_Seconds, testSuite.getRuntime(preferenceManager).toString()))); - } - - @Override - public boolean onSupportNavigateUp() { - onBackPressed(); - return true; - } - - void customUrlClick() { - startActivity(new Intent(this, CustomWebsiteActivity.class)); - } + Result lastResult = Result.getLastResult(descriptor.getName()); + if (lastResult == null) { + binding.lastTime.setText(R.string.Dashboard_Overview_LastRun_Never); + } else { + binding.lastTime.setText(DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime())); + } + + setUpOnCLickListeners(); + } + + public void setThemeColor(int color) { + Window window = getWindow(); + window.setStatusBarColor(color); + binding.toolbar.setBackgroundColor(color); + binding.appbarLayout.setBackgroundColor(color); + binding.collapsingToolbar.setBackgroundColor(color); + } + + private void setUpOnCLickListeners() { + binding.customUrl.setOnClickListener(view -> customUrlClick()); + } + + @Override + protected void onResume() { + super.onResume(); + binding.runtime.setText(getString(R.string.twoParam, getString(descriptor.getDataUsage()), getString(R.string.Dashboard_Card_Seconds, String.valueOf(descriptor.getRuntime(this, preferenceManager))))); + } + + @Override + public boolean onSupportNavigateUp() { + onBackPressed(); + return true; + } + + void customUrlClick() { + startActivity(new Intent(this, CustomWebsiteActivity.class)); + } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.java index e80a6fafb..c9cb40274 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.java @@ -21,6 +21,7 @@ import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerAdapter; import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem; import org.openobservatory.ooniprobe.R; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ResubmitTask; import org.openobservatory.ooniprobe.databinding.ActivityResultDetailBinding; @@ -71,14 +72,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { getActivityComponent().inject(this); result = getResults.get(getIntent().getIntExtra(ID, 0)); assert result != null; - setTheme(result.getTestSuite().getThemeLight()); + setTheme(result.getTestSuite(this).getThemeLight()); ActivityResultDetailBinding binding = ActivityResultDetailBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); setSupportActionBar(binding.toolbar); ActionBar bar = getSupportActionBar(); if (bar != null) { bar.setDisplayHomeAsUpEnabled(true); - bar.setTitle(result.getTestSuite().getTitle()); + bar.setTitle(result.getTestSuite(this).getTitle()); } binding.pager.setAdapter(new ResultHeaderAdapter(this)); new TabLayoutMediator(binding.tabLayout, binding.pager, (tab, position) -> @@ -111,7 +112,7 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onPrepareOptionsMenu(Menu menu) { invalidateOptionsMenu(); - if (!result.test_group_name.equals(WebsitesSuite.NAME)) + if (!result.test_group_name.equals(OONITests.WEBSITES.getLabel())) menu.findItem(R.id.reRun).setVisible(false); return super.onPrepareOptionsMenu(menu); } @@ -142,7 +143,7 @@ private void runAsyncTask() { private void load() { result = getResults.get(result.id); assert result != null; - boolean isPerf = result.test_group_name.equals(PerformanceSuite.NAME); + boolean isPerf = result.test_group_name.equals(OONITests.PERFORMANCE.getLabel()); items.clear(); List measurements = result.getMeasurementsSorted(); for (Measurement measurement : measurements) @@ -159,7 +160,7 @@ private void load() { @Override public void onClick(View v) { Measurement measurement = (Measurement) v.getTag(); - if (result.test_group_name.equals(ExperimentalSuite.NAME)) + if (result.test_group_name.equals(OONITests.EXPERIMENTAL.getLabel())) startActivity(TextActivity.newIntent(this, TextActivity.TYPE_JSON, measurement)); else ActivityCompat.startActivity(this, MeasurementDetailActivity.newIntent(this, measurement.id), null); @@ -206,7 +207,7 @@ private class ResultHeaderAdapter extends FragmentStateAdapter { @Override public Fragment createFragment(int position) { - if (result.test_group_name.equals(ExperimentalSuite.NAME)){ + if (result.test_group_name.equals(OONITests.EXPERIMENTAL.getLabel())){ if (position == 0) return ResultHeaderDetailFragment.newInstance(false, result.getFormattedDataUsageUp(), result.getFormattedDataUsageDown(), result.start_time, result.getRuntime(), true, null, null); else if (position == 1) @@ -216,26 +217,22 @@ else if (position == 1) return ResultHeaderDetailFragment.newInstance(false, result.getFormattedDataUsageUp(), result.getFormattedDataUsageDown(), result.start_time, result.getRuntime(), true, null, null); else if (position == 2) return ResultHeaderDetailFragment.newInstance(false, null, null, null, null, null, Network.getCountry(ResultDetailActivity.this, result.network), result.network); - else switch (result.test_group_name) { - default: //Default can no longer be null, so we have to default to something... - // NOTE: Perhaps set up a test page? - case WebsitesSuite.NAME: - return ResultHeaderTBAFragment.newInstance(result); - case InstantMessagingSuite.NAME: - return ResultHeaderTBAFragment.newInstance(result); - case MiddleBoxesSuite.NAME: - return ResultHeaderMiddleboxFragment.newInstance(result.countAnomalousMeasurements() > 0); - case PerformanceSuite.NAME: - return ResultHeaderPerformanceFragment.newInstance(result); - case CircumventionSuite.NAME: - return ResultHeaderTBAFragment.newInstance(result); + else { + if (result.test_group_name.equals(OONITests.INSTANT_MESSAGING.getLabel())) { + return ResultHeaderTBAFragment.newInstance(result); + } else if (result.test_group_name.equals(OONITests.PERFORMANCE.getLabel())) { + return ResultHeaderPerformanceFragment.newInstance(result); + } else if (result.test_group_name.equals(OONITests.CIRCUMVENTION.getLabel())) { + return ResultHeaderTBAFragment.newInstance(result); } + return ResultHeaderTBAFragment.newInstance(result); + } } @Override public int getItemCount() { - if (result.test_group_name.equals(ExperimentalSuite.NAME)) + if (result.test_group_name.equals(OONITests.EXPERIMENTAL.getLabel())) return 2; return 3; } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java index 4c82c2834..d5401b045 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java @@ -20,6 +20,7 @@ import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReachabilityManager; import org.openobservatory.ooniprobe.common.TestProgressRepository; @@ -28,7 +29,6 @@ import org.openobservatory.ooniprobe.databinding.ActivityRunningBinding; import org.openobservatory.ooniprobe.receiver.TestRunBroadRequestReceiver; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; import java.io.Serializable; import java.util.ArrayList; @@ -169,7 +169,7 @@ private void applyUIChanges(RunTestService service) { binding.eta.setText(R.string.Dashboard_Running_CalculatingETA); } - if (service.task.currentSuite.getName().equals(ExperimentalSuite.NAME)) + if (service.task.currentSuite.getName().equals(OONITests.EXPERIMENTAL.name())) binding.name.setText(service.task.currentTest.getName()); else binding.name.setText(getString(service.task.currentTest.getLabelResId())); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt index 382873559..8f7db1347 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import android.text.TextUtils import android.view.* +import org.openobservatory.engine.BaseNettest import org.openobservatory.ooniprobe.R import org.openobservatory.ooniprobe.activity.AbstractActivity import org.openobservatory.ooniprobe.activity.RunningActivity @@ -14,6 +15,7 @@ import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Compani import org.openobservatory.ooniprobe.activity.runtests.adapter.RunTestsExpandableListViewAdapter 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.PreferenceManager import org.openobservatory.ooniprobe.databinding.ActivityRunTestsBinding import org.openobservatory.ooniprobe.test.suite.* @@ -34,12 +36,12 @@ class RunTestsActivity : AbstractActivity() { companion object { const val TESTS: String = "tests" - @JvmStatic - fun newIntent(context: Context, testSuites: List): Intent { - return Intent(context, RunTestsActivity::class.java).putExtras(Bundle().apply { - putSerializable(TESTS, testSuites as Serializable) - }) - } + @JvmStatic + fun newIntent(context: Context, testSuites: List>): Intent { + return Intent(context, RunTestsActivity::class.java).putExtras(Bundle().apply { + putSerializable(TESTS, testSuites as Serializable) + }) + } } override fun onCreate(savedInstanceState: Bundle?) { @@ -52,14 +54,16 @@ class RunTestsActivity : AbstractActivity() { activityComponent?.inject(this) - val testSuites: List? = intent.extras?.getParcelableArrayList(TESTS) - testSuites?.let { ts -> - val tsGroups: List = ts.map { testSuite -> - return@map testSuite.runTestsGroupItem(preferenceManager) - } - - adapter = RunTestsExpandableListViewAdapter(tsGroups, viewModel) + val descriptors: List>? = + intent.extras?.getSerializable(TESTS) as List>? + descriptors?.let { _descriptors -> + adapter = RunTestsExpandableListViewAdapter( + _descriptors.map { descriptor -> + descriptor.toRunTestsGroupItem(preferenceManager = preferenceManager) + }, + viewModel + ) binding.expandableListView.setAdapter(adapter) for (i in 0 until adapter.groupCount) { binding.expandableListView.expandGroup(i) @@ -102,9 +106,7 @@ class RunTestsActivity : AbstractActivity() { val selectedChildItems: List = getChildItemsSelectedIdList() if (selectedChildItems.isNotEmpty()) { val testSuitesToRun = getGroupItemsAtLeastOneChildEnabled().map { groupItem -> - return@map AbstractSuite.getTestSuiteByName(groupItem.name).dynamicTestSuite( - nettests = groupItem.nettests.filter { nattest -> nattest.selected } - ) + return@map groupItem.getTest(this) } RunningActivity.runAsForegroundService( this@RunTestsActivity, diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsViewModel.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsViewModel.kt index aa9be5488..61ec6df97 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsViewModel.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsViewModel.kt @@ -2,13 +2,10 @@ package org.openobservatory.ooniprobe.activity.runtests import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import org.openobservatory.ooniprobe.common.OONITests import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.common.disableTest import org.openobservatory.ooniprobe.common.enableTest -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite import javax.inject.Inject @@ -30,29 +27,29 @@ class RunTestsViewModel() : ViewModel() { selectedAllBtnStatus.postValue(selectedStatus) when (selectedStatus) { SELECT_ALL -> { - InstantMessagingSuite().getTestList(preferenceManager).forEach { + OONITests.INSTANT_MESSAGING.nettests.forEach { enableTest(it.name) } - CircumventionSuite().getTestList(preferenceManager).forEach { + OONITests.CIRCUMVENTION.nettests.forEach { enableTest(it.name) } - PerformanceSuite().getTestList(preferenceManager).forEach { + OONITests.PERFORMANCE.nettests.forEach { enableTest(it.name) } - enableTest(ExperimentalSuite.NAME) + enableTest(OONITests.EXPERIMENTAL.label) } SELECT_NONE -> { - InstantMessagingSuite().getTestList(preferenceManager).forEach { + OONITests.INSTANT_MESSAGING.nettests.forEach { disableTest(it.name) } - CircumventionSuite().getTestList(preferenceManager).forEach { + OONITests.CIRCUMVENTION.nettests.forEach { disableTest(it.name) } - PerformanceSuite().getTestList(preferenceManager).forEach { + OONITests.PERFORMANCE.nettests.forEach { disableTest(it.name) } - disableTest(ExperimentalSuite.NAME) + disableTest(OONITests.EXPERIMENTAL.label) } } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt index 58ac76a9d..ac28d98fb 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt @@ -13,8 +13,7 @@ import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Compani import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.SELECT_SOME import org.openobservatory.ooniprobe.activity.runtests.models.ChildItem import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem -import org.openobservatory.ooniprobe.test.suite.AbstractSuite -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite +import org.openobservatory.ooniprobe.common.OONITests import org.openobservatory.ooniprobe.test.test.AbstractTest @@ -83,9 +82,8 @@ class RunTestsExpandableListViewAdapter( var convertView = convertView ?: LayoutInflater.from(parent.context).inflate(R.layout.run_tests_group_list_item, parent, false) val groupItem = getGroup(groupPosition) - val testSuite = AbstractSuite.getTestSuiteByName(groupItem.name) - convertView.findViewById(R.id.group_name).text = parent.context.resources.getText(testSuite.title) - convertView.findViewById(R.id.group_icon).setImageResource(testSuite.icon) + convertView.findViewById(R.id.group_name).text = groupItem.title + convertView.findViewById(R.id.group_icon).setImageResource(groupItem.getDisplayIcon(parent.context)) val groupIndicator = convertView.findViewById(R.id.group_indicator) val groupSelectionIndicator = convertView.findViewById(R.id.group_select_indicator) val selectedAllBtnStatus = viewModel.selectedAllBtnStatus.getValue() @@ -107,19 +105,19 @@ class RunTestsExpandableListViewAdapter( groupSelectionIndicator.setImageResource(R.drawable.check_box) // NOTE: This is the only place where ExperimentalSuite.NAME is used. // This doesn't follow the normal rule where the component tests make up the suite. - if (groupItem.name == ExperimentalSuite.NAME) { - viewModel.enableTest(ExperimentalSuite.NAME) + if (groupItem.name == OONITests.EXPERIMENTAL.label) { + viewModel.enableTest(OONITests.EXPERIMENTAL.label) } } else { groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank) - if (groupItem.name == ExperimentalSuite.NAME) { - viewModel.disableTest(ExperimentalSuite.NAME) + if (groupItem.name == OONITests.EXPERIMENTAL.label) { + viewModel.disableTest(OONITests.EXPERIMENTAL.label) } } } else { groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank) - if (groupItem.name == ExperimentalSuite.NAME) { - viewModel.disableTest(ExperimentalSuite.NAME) + if (groupItem.name == OONITests.EXPERIMENTAL.label) { + viewModel.disableTest(OONITests.EXPERIMENTAL.label) } } groupSelectionIndicator.setOnClickListener { @@ -176,7 +174,7 @@ class RunTestsExpandableListViewAdapter( val nettest = AbstractTest.getTestByName(childItem.name) convertView.findViewById(R.id.child_name)?.apply { text = when (groupItem.name) { - ExperimentalSuite.NAME -> { + OONITests.EXPERIMENTAL.label -> { childItem.name } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/models/GroupItems.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/models/GroupItems.kt index 35e9f39e4..4904e1ad6 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/models/GroupItems.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/models/GroupItems.kt @@ -1,16 +1,33 @@ package org.openobservatory.ooniprobe.activity.runtests.models -import org.openobservatory.engine.BaseDescriptor import org.openobservatory.engine.BaseNettest +import org.openobservatory.ooniprobe.common.OONIDescriptor class ChildItem( - var selected: Boolean, - override var name: String, - override var inputs: List? + var selected: Boolean, + override var name: String, + override var inputs: List?, ) : BaseNettest(name = name, inputs = inputs) class GroupItem( - var selected: Boolean, - override var name: String, - override var nettests: List -) : BaseDescriptor(name = name, nettests = nettests) + var selected: Boolean, + override var name: String, + override var title: String, + override var shortDescription: String, + override var description: String, + override var icon: String, + override var color: Int, + override var animation: String, + override var dataUsage: Int, + override var nettests: List +) : OONIDescriptor( + name = name, + title = title, + shortDescription = shortDescription, + description = description, + icon = icon, + color = color, + animation = animation, + dataUsage = dataUsage, + nettests = nettests +) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/adapters/DashboardAdapter.kt b/app/src/main/java/org/openobservatory/ooniprobe/adapters/DashboardAdapter.kt index 4c7efef84..b8c4e6044 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/adapters/DashboardAdapter.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/adapters/DashboardAdapter.kt @@ -8,10 +8,10 @@ import android.view.ViewGroup import androidx.cardview.widget.CardView import androidx.recyclerview.widget.RecyclerView import org.openobservatory.ooniprobe.R +import org.openobservatory.ooniprobe.common.OONIDescriptor import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.databinding.ItemSeperatorBinding import org.openobservatory.ooniprobe.databinding.ItemTestsuiteBinding -import org.openobservatory.ooniprobe.test.suite.AbstractSuite class DashboardAdapter( private val items: List, @@ -54,14 +54,14 @@ class DashboardAdapter( VIEW_TYPE_CARD -> { val cardHolder = holder as CardViewHolder - if (item is AbstractSuite) { + if (item is OONIDescriptor<*>) { cardHolder.binding.apply { title.setText(item.title) - desc.setText(item.cardDesc) - icon.setImageResource(item.icon) + desc.setText(item.shortDescription) + icon.setImageResource(item.getDisplayIcon(holder.itemView.context)) } holder.itemView.tag = item - if (item.isTestEmpty(preferenceManager)) { + if (!item.isEnabled(preferenceManager)) { holder.setIsRecyclable(false) holder.itemView.apply { elevation = 0f diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/OONIDescriptor.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/OONIDescriptor.kt new file mode 100644 index 000000000..535455176 --- /dev/null +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/OONIDescriptor.kt @@ -0,0 +1,297 @@ +package org.openobservatory.ooniprobe.common + +import android.annotation.SuppressLint +import android.content.Context +import androidx.annotation.ColorRes +import androidx.annotation.StringRes +import org.openobservatory.engine.BaseDescriptor +import org.openobservatory.engine.BaseNettest +import org.openobservatory.ooniprobe.R +import org.openobservatory.ooniprobe.activity.runtests.RunTestsActivity +import org.openobservatory.ooniprobe.activity.runtests.models.ChildItem +import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem +import org.openobservatory.ooniprobe.test.suite.AbstractSuite +import org.openobservatory.ooniprobe.test.suite.DynamicTestSuite +import org.openobservatory.ooniprobe.test.test.* +import java.io.Serializable + +open class OONIDescriptor( + override var name: String, + open var title: String, + open var shortDescription: String, + open var description: String, + open var icon: String, + @ColorRes open var color: Int, + open var animation: String, + @StringRes open var dataUsage: Int, + override var nettests: List, + var longRunningTests: List? = null +) : Serializable, BaseDescriptor(name = name, nettests = nettests) { + + /** + * Checks if any of the nettests are enabled based on the preferences stored in the provided [PreferenceManager]. + * + * @param preferenceManager The [PreferenceManager] instance used to resolve the status of each nettest. + * @return Boolean Returns true if at least one nettest is enabled, false otherwise. + */ + fun isEnabled(preferenceManager: PreferenceManager): Boolean { + return nettests.any { + preferenceManager.resolveStatus( + name = it.name, + prefix = preferencePrefix() + ) + } + } + + /** + * Returns the display icon for the current descriptor. + * + * @return Int representing the display icon for the current descriptor. + */ + fun getDisplayIcon(context: Context): Int { + return context.resources.getIdentifier(icon, "drawable", context.packageName) + } + + /** + * Converts the current descriptor to a [GroupItem] to be used in the [RunTestsActivity]. + * + * @return [GroupItem] representing the current descriptor. + */ + fun toRunTestsGroupItem(preferenceManager: PreferenceManager): GroupItem { + return GroupItem(selected = false, + name = this.name, + title = this.title, + shortDescription = this.shortDescription, + description = this.description, + icon = this.icon, + color = this.color, + animation = this.animation, + dataUsage = this.dataUsage, + nettests = this.nettests.map { nettest -> + ChildItem( + selected = when (this.name == OONITests.EXPERIMENTAL.label) { + true -> preferenceManager.isExperimentalOn + false -> preferenceManager.resolveStatus(nettest.name) + }, name = nettest.name, inputs = nettest.inputs + ) + }) + } + + /** + * Returns the runtime of the current descriptor. + * + * @return Int representing the runtime of the current descriptor. + */ + fun getRuntime(context: Context, preferenceManager: PreferenceManager): Int { + return getTest(context).getRuntime(preferenceManager) + } + + /** + * Returns the test suite for the current descriptor. + * + * @return [DynamicTestSuite] representing the test suite for the current descriptor. + */ + fun getTest(context: Context): DynamicTestSuite { + return DynamicTestSuite( + name = this.name, + title = this.title, + shortDescription = this.shortDescription, + description = this.description, + icon = getDisplayIcon(context), + icon_24 = getDisplayIcon(context), + color = this.color, + animation = this.animation, + dataUsage = this.dataUsage, + nettest = this.nettests + ) + } + + /** + * Returns the preference prefix to be used for the current descriptor. + * + * This function iterates over all OONI tests and finds the one that matches the current test name. + * If a match is found (the test is an OONI provided test), it returns an empty string as the preference prefix. + * If no match is found, it returns "descriptor_id_" as the default preference prefix. + * + * @return String representing the preference prefix. + */ + private fun preferencePrefix(): String { + return OONITests.values().find { it.label == name }?.let { "" } ?: "descriptor_id_" + } +} + +/** + * Enum class representing the OONI tests. + * Rational + * + * @param label String representing the label of the OONI test. + * @param title Int representing the title of the OONI test. + * @param shortDescription Int representing the short description of the OONI test. + * @param description Int representing the description of the OONI test. + * @param icon String representing the icon of the OONI test. + * @param color Int representing the color of the OONI test. + * @param animation String representing the animation of the OONI test. + * @param dataUsage Int representing the data usage of the OONI test. + * @param nettests List of [BaseNettest] representing the nettests of the OONI test. + * @param longRunningTests List of [BaseNettest] representing the long-running nettests of the OONI test. + */ +enum class OONITests( + val label: String, + @StringRes val title: Int, + @StringRes val shortDescription: Int, + @StringRes val description: Int, + val icon: String, + val color: Int, + val animation: String, + @StringRes val dataUsage: Int, + var nettests: List, + var longRunningTests: List? = null +) { + WEBSITES( + label = "websites", + title = R.string.Test_Websites_Fullname, + shortDescription = R.string.Dashboard_Websites_Card_Description, + description = R.string.Dashboard_Websites_Overview_Paragraph, + icon = "test_websites", + color = R.color.color_indigo6, + animation = "anim/websites.json", + dataUsage = R.string.websites_datausage, + nettests = listOf( + BaseNettest(name = WebConnectivity.NAME) + ) + ), + INSTANT_MESSAGING( + label = "instant_messaging", + title = R.string.Test_InstantMessaging_Fullname, + shortDescription = R.string.Dashboard_InstantMessaging_Card_Description, + description = R.string.Dashboard_InstantMessaging_Overview_Paragraph, + icon = "test_instant_messaging", + color = R.color.color_cyan6, + animation = "anim/instant_messaging.json", + dataUsage = R.string.small_datausage, + nettests = listOf( + BaseNettest(name = Whatsapp.NAME), + BaseNettest(name = Telegram.NAME), + BaseNettest(name = FacebookMessenger.NAME), + BaseNettest(name = Signal.NAME), + ) + ), + CIRCUMVENTION( + label = "circumvention", + title = R.string.Test_Circumvention_Fullname, + shortDescription = R.string.Dashboard_Circumvention_Card_Description, + description = R.string.Dashboard_Circumvention_Overview_Paragraph, + icon = "test_circumvention", + color = R.color.color_pink6, + animation = "anim/circumvention.json", + dataUsage = R.string.small_datausage, + nettests = listOf( + BaseNettest(name = Psiphon.NAME), + BaseNettest(name = Tor.NAME), + ) + ), + PERFORMANCE( + label = "performance", + title = R.string.Test_Performance_Fullname, + shortDescription = R.string.Dashboard_Performance_Card_Description, + description = R.string.Dashboard_Performance_Overview_Paragraph, + icon = "test_performance", + color = R.color.color_fuchsia6, + animation = "anim/performance.json", + dataUsage = R.string.performance_datausage, + nettests = listOf( + BaseNettest(name = Ndt.NAME), + BaseNettest(name = Dash.NAME), + BaseNettest(name = HttpHeaderFieldManipulation.NAME), + BaseNettest(name = HttpInvalidRequestLine.NAME), + ) + ), + EXPERIMENTAL( + label = "experimental", + title = R.string.Test_Experimental_Fullname, + shortDescription = R.string.Dashboard_Experimental_Card_Description, + description = R.string.Dashboard_Experimental_Overview_Paragraph, + icon = "test_experimental", + color = R.color.color_gray7_1, + animation = "anim/experimental.json", + dataUsage = R.string.TestResults_NotAvailable, + nettests = listOf( + BaseNettest(name = "stunreachability"), + BaseNettest(name = "dnscheck"), + BaseNettest(name = "echcheck"), + ), + longRunningTests = listOf( + BaseNettest(name = "torsf"), + BaseNettest(name = "vanilla_tor"), + ) + ); + + /** + * Converts the current OONI test to an [OONIDescriptor]. + * + * @return [OONIDescriptor] representing the current OONI test. + */ + fun toOONIDescriptor(context: Context): OONIDescriptor { + val r = context.resources + val experimentalLinks = """ + * [STUN Reachability](https://github.com/ooni/spec/blob/master/nettests/ts-025-stun-reachability.md) + + * [DNS Check](https://github.com/ooni/spec/blob/master/nettests/ts-028-dnscheck.md) + + * [ECH Check](https://github.com/ooni/spec/blob/master/nettests/ts-039-echcheck.md) + + * [Tor Snowflake](https://ooni.org/nettest/tor-snowflake/) ${ + String.format( + " ( %s )", r.getString(R.string.Settings_TestOptions_LongRunningTest) + ) + } + + * [Vanilla Tor](https://github.com/ooni/spec/blob/master/nettests/ts-016-vanilla-tor.md) ${ + String.format( + " ( %s )", r.getString(R.string.Settings_TestOptions_LongRunningTest) + ) + } + """.trimIndent() + this.run { + return OONIDescriptor( + name = label, + title = context.getString(title), + shortDescription = context.getString(shortDescription), + description = when (label) { + EXPERIMENTAL.label -> context.getString( + description, + experimentalLinks + ) + + else -> context.getString(description) + }, + icon = icon, + color = color, + animation = animation, + dataUsage = dataUsage, + nettests = nettests, + longRunningTests = longRunningTests, + ) + } + } + + override fun toString(): String { + return label + } +} + +/** + * Creates a list of [OONIDescriptor] representing the OONI tests. + * + * @return List of [OONIDescriptor] representing the OONI tests. + */ +@SuppressLint("StringFormatInvalid") +fun ooniDescriptors(context: Context): MutableList> { + return mutableListOf( + OONITests.WEBSITES.toOONIDescriptor(context), + OONITests.INSTANT_MESSAGING.toOONIDescriptor(context), + OONITests.CIRCUMVENTION.toOONIDescriptor(context), + OONITests.PERFORMANCE.toOONIDescriptor(context), + OONITests.EXPERIMENTAL.toOONIDescriptor(context) + ) +} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManagerExtension.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManagerExtension.kt index 822a29546..d325f8566 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManagerExtension.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManagerExtension.kt @@ -1,125 +1,126 @@ package org.openobservatory.ooniprobe.common import org.openobservatory.ooniprobe.R -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite import org.openobservatory.ooniprobe.test.test.* -private fun PreferenceManager.exclusionList(): MutableList { - val exclusionList = mutableListOf() - ExperimentalSuite().run { - exclusionList.addAll(getTestList(this@exclusionList).map { it.name }) - exclusionList.addAll(longRunningTests().map { it.name }) - } +private fun PreferenceManager.experimentalTestList(): MutableList { + val exclusionList = mutableListOf() + OONITests.EXPERIMENTAL.run { + exclusionList.addAll(nettests.map { it.name }) + exclusionList.addAll(nettests.map { it.name }) + } - return exclusionList + return exclusionList } +/** + * This function is used to resolve the status of a given test. + * @param name The name of the test. + * @param prefix The **[OONIDescriptor.preferencePrefix]** of the descriptor. + * @return The status of the test. + */ +fun PreferenceManager.resolveStatus(name: String, prefix: String): Boolean { + if (name == WebConnectivity.NAME) { + return true + } else if (experimentalTestList().contains(name)) { + return isExperimentalOn + } + return sp.getBoolean(getPreferenceKey(name = name, prefix = prefix), false) +} + +/** + * This function is used to resolve the preference key for a given test. + * The preference key is the name of the test prefixed with the **[OONIDescriptor.preferencePrefix]** of the + * descriptor. + * + * For example, the preference key for the test "web_connectivity" in the + * descriptor "websites" is "websites_web_connectivity". + * + * @param name The name of the test. + * @param prefix The **[OONIDescriptor.preferencePrefix]** of the descriptor. + * @return The preference key. + */ +fun PreferenceManager.getPreferenceKey(name: String, prefix: String): String { + return "$prefix${getPreferenceKey(name)}" +} + + +/** + * This function returns the name of the preference key for a given test name. + * @param name The name of the test. + * @return The base preference key. + */ +fun PreferenceManager.getPreferenceKey(name: String): String { + return when (name) { + Dash.NAME -> r.getString(R.string.run_dash) + + FacebookMessenger.NAME -> r.getString(R.string.test_facebook_messenger) + + HttpHeaderFieldManipulation.NAME -> r.getString(R.string.run_http_header_field_manipulation) + + HttpInvalidRequestLine.NAME -> r.getString(R.string.run_http_invalid_request_line) + + Ndt.NAME -> r.getString(R.string.run_ndt) + + Psiphon.NAME -> r.getString(R.string.test_psiphon) + + RiseupVPN.NAME -> r.getString(R.string.test_riseupvpn) + + Signal.NAME -> r.getString(R.string.test_signal) + + Telegram.NAME -> r.getString(R.string.test_telegram) + + Tor.NAME -> r.getString(R.string.test_tor) + + Whatsapp.NAME -> r.getString(R.string.test_whatsapp) + + OONITests.EXPERIMENTAL.label -> r.getString(R.string.experimental) + + else -> throw IllegalArgumentException("Unknown preference for: $name") + } +} + +/** + * Enables a test in the [PreferenceManager]. + * + * @param name The name of the test to enable. + * @return true if the test was successfully enabled, false otherwise. + */ fun PreferenceManager.enableTest(name: String): Boolean { - if (!exclusionList().contains(name)) { - this.setValue(name, true) - return true - } - return false + if (!experimentalTestList().contains(name)) { + return this.setValue(name, true) + } + return false } + +/** + * Disables a test in the [PreferenceManager]. + * + * @param name The name of the test to disable. + * @return true if the test was successfully disabled, false otherwise. + */ fun PreferenceManager.disableTest(name: String): Boolean { - if (!exclusionList().contains(name)) { - this.setValue(name, false) - return true - } - return false + if (!experimentalTestList().contains(name)) { + return this.setValue(name, false) + } + return false } -fun PreferenceManager.setValue(name: String, value: Boolean) { - when (name) { - WebConnectivity.NAME -> {} - Dash.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.run_dash), value) - apply() - } - } - - FacebookMessenger.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_facebook_messenger), value) - apply() - } - } - - HttpHeaderFieldManipulation.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.run_http_header_field_manipulation), value) - apply() - } - } - - HttpInvalidRequestLine.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.run_http_invalid_request_line), value) - apply() - } - } - - Ndt.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.run_ndt), value) - apply() - } - } - - Psiphon.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_psiphon), value) - apply() - } - } - - RiseupVPN.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_riseupvpn), value) - apply() - } - } - - Signal.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_signal), value) - apply() - } - } - - Telegram.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_telegram), value) - apply() - } - } - - Tor.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_tor), value) - apply() - } - } - - Whatsapp.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.test_whatsapp), value) - apply() - } - } - - ExperimentalSuite.NAME -> { - with(sp.edit()) { - putBoolean(r.getString(R.string.experimental), value) - apply() - } - } - - else -> { - throw IllegalArgumentException("Unknown preference for: $name") - } - } +/** + * Sets the value of a preference in the [PreferenceManager]. + * + * @param name The name of the preference to set. + * @param value The value to set the preference to. + * @return true if the preference was successfully set, false otherwise. + */ +fun PreferenceManager.setValue(name: String, value: Boolean): Boolean { + if (name == WebConnectivity.NAME || experimentalTestList().contains(name)) { + return false + } + return with(sp.edit()) { + putBoolean(getPreferenceKey(name), value) + commit() + } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt b/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt new file mode 100644 index 000000000..8f6a663c3 --- /dev/null +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/TestDescriptorManager.kt @@ -0,0 +1,25 @@ +package org.openobservatory.ooniprobe.common + +import android.content.Context +import org.openobservatory.engine.BaseNettest +import org.openobservatory.ooniprobe.test.suite.DynamicTestSuite +import org.openobservatory.ooniprobe.test.test.AbstractTest +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class TestDescriptorManager @Inject constructor(private val context: Context) { + private val descriptors: MutableList> = ooniDescriptors(context) + + fun getDescriptors(): MutableList> { + return descriptors + } + + fun getDescriptorByName(name: String): OONIDescriptor? { + return descriptors.find { it.name == name } + } + + fun getTestByDescriptorName(name: String): DynamicTestSuite? { + return getDescriptorByName(name)?.getTest(context) + } +} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/service/ServiceUtil.java b/app/src/main/java/org/openobservatory/ooniprobe/common/service/ServiceUtil.java index 1f7c0e9e7..2356f3eb4 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/service/ServiceUtil.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/service/ServiceUtil.java @@ -15,16 +15,15 @@ import org.openobservatory.engine.OONICheckInConfig; import org.openobservatory.ooniprobe.BuildConfig; import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReachabilityManager; import org.openobservatory.ooniprobe.domain.GenerateAutoRunServiceSuite; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; +import org.openobservatory.ooniprobe.test.suite.DynamicTestSuite; import java.util.ArrayList; +import java.util.List; import javax.inject.Inject; @@ -47,15 +46,16 @@ public static void scheduleJob(Context context) { builder.setRequiresCharging(d.preferenceManager.testChargingOnly()); /* - * Specify that this job should recur with the provided interval, not more than once per period. - * You have no control over when within this interval - * this job will be executed, only the guarantee that it will be executed at most once within this interval. - */ + * Specify that this job should recur with the provided interval, not more than once per period. + * You have no control over when within this interval + * this job will be executed, only the guarantee that it will be executed at most once within this interval. + */ builder.setPeriodic(60 * 60 * 1000); builder.setPersisted(true); //Job scheduled to work after reboot //JobScheduler is specifically designed for inexact timing, so it can combine jobs from multiple apps, to try to reduce power consumption. - JobScheduler jobScheduler = ContextCompat.getSystemService(context, JobScheduler.class);; + JobScheduler jobScheduler = ContextCompat.getSystemService(context, JobScheduler.class); + ; if (jobScheduler != null) { jobScheduler.schedule(builder.build()); } @@ -75,17 +75,18 @@ public static void startRunTestServiceUnattended(Application app) { OONICheckInConfig config = app.getOONICheckInConfig(); - if (!d.generateAutoRunServiceSuite.shouldStart(config.isOnWiFi(),config.isCharging(), isVPNInUse)) { + if (!d.generateAutoRunServiceSuite.shouldStart(config.isOnWiFi(), config.isCharging(), isVPNInUse)) { return; } AbstractSuite suite = d.generateAutoRunServiceSuite.generate(); ArrayList testSuites = new ArrayList<>(); testSuites.add(suite); - testSuites.add(InstantMessagingSuite.initForAutoRun()); - testSuites.add(CircumventionSuite.initForAutoRun()); - testSuites.add(PerformanceSuite.initForAutoRun()); - testSuites.add(ExperimentalSuite.initForAutoRun()); + testSuites.addAll(Lists.transform(List.of(OONITests.INSTANT_MESSAGING, OONITests.CIRCUMVENTION, OONITests.PERFORMANCE, OONITests.EXPERIMENTAL), item -> { + DynamicTestSuite testSuite = item.toOONIDescriptor(app).getTest(app); + testSuite.setAutoRun(true); + return testSuite; + })); ServiceUtil.startRunTestServiceCommon(app, testSuites, false, true); d.generateAutoRunServiceSuite.markAsRan(); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/domain/GenerateAutoRunServiceSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/domain/GenerateAutoRunServiceSuite.java index a511b847e..0913b7393 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/domain/GenerateAutoRunServiceSuite.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/domain/GenerateAutoRunServiceSuite.java @@ -1,22 +1,13 @@ package org.openobservatory.ooniprobe.domain; -import org.openobservatory.engine.LoggerArray; -import org.openobservatory.engine.OONICheckInConfig; -import org.openobservatory.engine.OONICheckInResults; -import org.openobservatory.engine.OONIContext; -import org.openobservatory.engine.OONISession; -import org.openobservatory.engine.OONIURLInfo; -import org.openobservatory.ooniprobe.BuildConfig; +import static org.openobservatory.ooniprobe.test.suite.AbstractSuiteExtensionsKt.getSuite; + import org.openobservatory.ooniprobe.common.Application; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReachabilityManager; -import org.openobservatory.ooniprobe.common.ThirdPartyServices; -import org.openobservatory.ooniprobe.test.EngineProvider; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; import org.openobservatory.ooniprobe.test.test.AbstractTest; - -import java.util.ArrayList; -import java.util.List; +import org.openobservatory.ooniprobe.test.test.WebConnectivity; import javax.inject.Inject; @@ -32,13 +23,7 @@ public class GenerateAutoRunServiceSuite { } public AbstractSuite generate() { - - return AbstractSuite.getSuite( - app, - "web_connectivity", - null, - AbstractTest.AUTORUN - ); + return getSuite(app, WebConnectivity.NAME, null, AbstractTest.AUTORUN); } @@ -47,7 +32,7 @@ public boolean shouldStart(Boolean isWifi, Boolean isCharging, Boolean isVPNInUs return false; if (pm.testChargingOnly() && !isCharging) return false; - if(ReachabilityManager.getChargingLevel(app) < 20 && !isCharging) + if (ReachabilityManager.getChargingLevel(app) < 20 && !isCharging) return false; if (isVPNInUse) { return false; diff --git a/app/src/main/java/org/openobservatory/ooniprobe/domain/GetTestSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/domain/GetTestSuite.java index 59edf17cb..043202335 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/domain/GetTestSuite.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/domain/GetTestSuite.java @@ -1,15 +1,17 @@ package org.openobservatory.ooniprobe.domain; +import static org.openobservatory.ooniprobe.test.suite.AbstractSuiteExtensionsKt.getSuite; + import androidx.annotation.Nullable; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.model.database.Url; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.WebConnectivity; import java.util.List; @@ -27,13 +29,11 @@ public class GetTestSuite { } public AbstractSuite get(String testName, @Nullable List urls) { - return AbstractSuite.getSuite(application, testName, - urls, - "ooni-run"); + return getSuite(application, testName, urls, "ooni-run"); } public AbstractSuite getFrom(Result result) { - AbstractSuite testSuite = result.getTestSuite(); + AbstractSuite testSuite = result.getTestSuite(application); WebConnectivity test = new WebConnectivity(); // possible NPE from measurements whose url's are null. @@ -57,8 +57,8 @@ public AbstractSuite getFrom(Result result) { } public AbstractSuite getForWebConnectivityReRunFrom(Result result, List inputs) { - if (Objects.equals(result.getTestSuite().getName(), WebsitesSuite.NAME)){ - AbstractSuite testSuite = result.getTestSuite(); + if (Objects.equals(result.getTestSuite(application).getName(), OONITests.WEBSITES.name())) { + AbstractSuite testSuite = result.getTestSuite(application); WebConnectivity test = new WebConnectivity(); // possible NPE from measurements whose url's are null. diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt index a973b1cee..c5857f0f3 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.kt @@ -9,12 +9,15 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager +import org.openobservatory.engine.BaseNettest import org.openobservatory.ooniprobe.R import org.openobservatory.ooniprobe.activity.AbstractActivity import org.openobservatory.ooniprobe.activity.OverviewActivity import org.openobservatory.ooniprobe.activity.RunningActivity +import org.openobservatory.ooniprobe.activity.runtests.RunTestsActivity import org.openobservatory.ooniprobe.adapters.DashboardAdapter import org.openobservatory.ooniprobe.common.Application +import org.openobservatory.ooniprobe.common.OONIDescriptor import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.common.ReachabilityManager import org.openobservatory.ooniprobe.common.ThirdPartyServices @@ -30,7 +33,7 @@ class DashboardFragment : Fragment(), View.OnClickListener { @Inject lateinit var viewModel: DashboardViewModel - private var testSuites: ArrayList = ArrayList() + private var descriptors: ArrayList> = ArrayList() private lateinit var binding: FragmentDashboardBinding override fun onCreateView( @@ -59,7 +62,7 @@ class DashboardFragment : Fragment(), View.OnClickListener { } viewModel.items.observe(viewLifecycleOwner) { items -> - testSuites.apply { + descriptors.apply { clear() addAll(items) } @@ -86,12 +89,7 @@ class DashboardFragment : Fragment(), View.OnClickListener { } private fun runAll() { - RunningActivity.runAsForegroundService( - activity as AbstractActivity?, - testSuites, - { onTestServiceStartedListener() }, - preferenceManager - ) + ActivityCompat.startActivity(requireContext(), RunTestsActivity.newIntent(requireContext(), descriptors), null) } private fun onTestServiceStartedListener() = try { @@ -102,19 +100,11 @@ class DashboardFragment : Fragment(), View.OnClickListener { } override fun onClick(v: View) { - val testSuite = v.tag as AbstractSuite - when (v.id) { - R.id.run -> RunningActivity.runAsForegroundService( - activity as AbstractActivity?, - testSuite.asArray(), { onTestServiceStartedListener() }, - preferenceManager - ) - - else -> ActivityCompat.startActivity( - requireActivity(), - OverviewActivity.newIntent(activity, testSuite), - null - ) - } + val descriptor = v.tag as OONIDescriptor + ActivityCompat.startActivity( + requireActivity(), + OverviewActivity.newIntent(activity, descriptor), + null + ) } } \ No newline at end of file diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/ProgressFragment.kt b/app/src/main/java/org/openobservatory/ooniprobe/fragment/ProgressFragment.kt index 347ff72fe..7d98f5d75 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/ProgressFragment.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/ProgressFragment.kt @@ -16,12 +16,12 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager import org.openobservatory.ooniprobe.R import org.openobservatory.ooniprobe.activity.RunningActivity import org.openobservatory.ooniprobe.common.Application +import org.openobservatory.ooniprobe.common.OONITests import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.common.TestProgressRepository import org.openobservatory.ooniprobe.common.service.RunTestService import org.openobservatory.ooniprobe.databinding.FragmentProgressBinding import org.openobservatory.ooniprobe.receiver.TestRunBroadRequestReceiver -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite import javax.inject.Inject /** @@ -132,7 +132,7 @@ class ProgressFragment : Fragment() { biding.testImage.visibility = View.GONE } } - biding.name.text = when (task.currentSuite is ExperimentalSuite) { + biding.name.text = when (task.currentSuite.name.equals(OONITests.EXPERIMENTAL.label)) { true -> SpannableStringBuilder().bold { append(currentTest.name) } false -> SpannableStringBuilder().bold { append(getString(currentTest.labelResId)) } }.append(" ") diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/ResultListFragment.java b/app/src/main/java/org/openobservatory/ooniprobe/fragment/ResultListFragment.java index 8b881c25e..583e75b28 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/ResultListFragment.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/ResultListFragment.java @@ -3,8 +3,14 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; -import android.view.*; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; @@ -12,35 +18,46 @@ import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; + import com.google.android.material.snackbar.Snackbar; import com.raizlabs.android.dbflow.sql.language.Method; import com.raizlabs.android.dbflow.sql.language.SQLite; -import localhost.toolkit.app.fragment.ConfirmDialogFragment; -import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerAdapter; -import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem; + import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.activity.AbstractActivity; import org.openobservatory.ooniprobe.activity.ResultDetailActivity; import org.openobservatory.ooniprobe.activity.TextActivity; import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ResubmitTask; +import org.openobservatory.ooniprobe.common.TestDescriptorManager; import org.openobservatory.ooniprobe.databinding.FragmentResultListBinding; import org.openobservatory.ooniprobe.domain.GetResults; import org.openobservatory.ooniprobe.domain.MeasurementsManager; import org.openobservatory.ooniprobe.domain.models.DatedResults; -import org.openobservatory.ooniprobe.item.*; +import org.openobservatory.ooniprobe.item.CircumventionItem; +import org.openobservatory.ooniprobe.item.DateItem; +import org.openobservatory.ooniprobe.item.ExperimentalItem; +import org.openobservatory.ooniprobe.item.FailedItem; +import org.openobservatory.ooniprobe.item.InstantMessagingItem; +import org.openobservatory.ooniprobe.item.PerformanceItem; +import org.openobservatory.ooniprobe.item.WebsiteItem; import org.openobservatory.ooniprobe.model.database.Network; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.model.database.Result_Table; -import org.openobservatory.ooniprobe.test.suite.*; -import javax.inject.Inject; import java.io.Serializable; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + +import localhost.toolkit.app.fragment.ConfirmDialogFragment; +import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerAdapter; +import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem; + public class ResultListFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, ConfirmDialogFragment.OnConfirmedListener { private FragmentResultListBinding binding; @@ -58,6 +75,9 @@ public class ResultListFragment extends Fragment implements View.OnClickListener @Inject PreferenceManager pm; + @Inject + TestDescriptorManager descriptorManager; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { @@ -165,25 +185,18 @@ void queryList() { if (result.countTotalMeasurements() == 0) items.add(new FailedItem(result, this, this)); else { - switch (result.test_group_name) { - case WebsitesSuite.NAME: - items.add(new WebsiteItem(result, this, this)); - break; - case InstantMessagingSuite.NAME: - items.add(new InstantMessagingItem(result, this, this)); - break; - case MiddleBoxesSuite.NAME: - items.add(new MiddleboxesItem(result, this, this)); - break; - case PerformanceSuite.NAME: - items.add(new PerformanceItem(result, this, this)); - break; - case CircumventionSuite.NAME: - items.add(new CircumventionItem(result, this, this)); - break; - case ExperimentalSuite.NAME: - items.add(new ExperimentalItem(result, this, this)); - break; + if (result.test_group_name.equals(OONITests.WEBSITES.toString())) { + items.add(new WebsiteItem(result, this, this)); + } else if (result.test_group_name.equals(OONITests.INSTANT_MESSAGING.toString())) { + items.add(new InstantMessagingItem(result, this, this)); + } else if (result.test_group_name.equals(OONITests.PERFORMANCE.toString())) { + items.add(new PerformanceItem(result, this, this)); + } else if (result.test_group_name.equals(OONITests.CIRCUMVENTION.toString())) { + items.add(new CircumventionItem(result, this, this)); + } else if (result.test_group_name.equals(OONITests.EXPERIMENTAL.toString())) { + items.add(new ExperimentalItem(result, this, this)); + } else { + items.add(new FailedItem(result, this, this)); } } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/dashboard/DashboardViewModel.kt b/app/src/main/java/org/openobservatory/ooniprobe/fragment/dashboard/DashboardViewModel.kt index 892990cc6..d95467354 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/dashboard/DashboardViewModel.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/dashboard/DashboardViewModel.kt @@ -3,16 +3,20 @@ package org.openobservatory.ooniprobe.fragment.dashboard import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import org.openobservatory.engine.BaseNettest +import org.openobservatory.ooniprobe.common.OONIDescriptor import org.openobservatory.ooniprobe.common.PreferenceManager -import org.openobservatory.ooniprobe.test.TestAsyncTask -import org.openobservatory.ooniprobe.test.suite.AbstractSuite +import org.openobservatory.ooniprobe.common.TestDescriptorManager import javax.inject.Inject -class DashboardViewModel @Inject constructor(private val preferenceManager: PreferenceManager) : ViewModel() { - private val oonTestsTitle: String = "OONI Tests" - private val oonTests = TestAsyncTask.getSuites() +class DashboardViewModel @Inject constructor( + private val preferenceManager: PreferenceManager, + private val descriptorManager: TestDescriptorManager +) : ViewModel() { + private val oonTestsTitle: String = "OONI Tests" + private val oonTests = descriptorManager.getDescriptors() private val groupedItemList = MutableLiveData>() - val items = MutableLiveData>(oonTests) + val items = MutableLiveData>>(oonTests) fun getGroupedItemList(): LiveData> { if (groupedItemList.value == null) { @@ -23,7 +27,7 @@ class DashboardViewModel @Inject constructor(private val preferenceManager: Pref private fun fetchItemList() { - val groupedItems = items.value!!.sortedBy { it.getTestList(preferenceManager).isEmpty() } + val groupedItems = items.value!!.sortedBy { !it.isEnabled(preferenceManager) } .groupBy { return@groupBy if (oonTests.contains(it)) { oonTestsTitle diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/measurement/FailedFragment.java b/app/src/main/java/org/openobservatory/ooniprobe/fragment/measurement/FailedFragment.java index ba9f6f98a..1517bacd4 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/measurement/FailedFragment.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/measurement/FailedFragment.java @@ -54,7 +54,7 @@ void tryAgainClick(View view) { abstractTest.setIsRerun(true); if (failedMeasurement.url != null) abstractTest.setInputs(Collections.singletonList(failedMeasurement.url.url)); - AbstractSuite testSuite = failedMeasurement.result.getTestSuite(); + AbstractSuite testSuite = failedMeasurement.result.getTestSuite(getContext()); testSuite.setTestList(abstractTest); testSuite.setResult(failedMeasurement.result); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/item/FailedItem.java b/app/src/main/java/org/openobservatory/ooniprobe/item/FailedItem.java index 4566b4341..9606014e6 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/item/FailedItem.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/item/FailedItem.java @@ -1,5 +1,8 @@ package org.openobservatory.ooniprobe.item; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; + import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; @@ -8,13 +11,14 @@ import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; +import org.openobservatory.engine.BaseNettest; import org.openobservatory.ooniprobe.R; +import org.openobservatory.ooniprobe.common.OONIDescriptor; import org.openobservatory.ooniprobe.databinding.ItemFailedBinding; import org.openobservatory.ooniprobe.model.database.Result; import java.util.Date; import java.util.Locale; -import static java.util.concurrent.TimeUnit.*; import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem; @@ -38,8 +42,9 @@ public FailedItem(Result extra, View.OnClickListener onClickListener, View.OnLon viewHolder.itemView.setOnLongClickListener(onLongClickListener); viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(viewHolder.itemView.getContext(), R.color.color_gray2)); viewHolder.binding.testName.setTextColor(ContextCompat.getColor(viewHolder.itemView.getContext(), R.color.color_gray6)); - viewHolder.binding.icon.setImageResource(extra.getTestSuite().getIcon()); - viewHolder.binding.testName.setText(extra.getTestSuite().getTitle()); + OONIDescriptor descriptor = extra.getDescriptor(viewHolder.itemView.getContext()); + viewHolder.binding.icon.setImageResource(descriptor.getDisplayIcon(viewHolder.itemView.getContext())); + viewHolder.binding.testName.setText(descriptor.getTitle()); String failure_msg = viewHolder.itemView.getContext().getString(R.string.TestResults_Overview_Error); if (extra.failure_msg != null) { failure_msg += " - " + extra.failure_msg; diff --git a/app/src/main/java/org/openobservatory/ooniprobe/model/database/Result.java b/app/src/main/java/org/openobservatory/ooniprobe/model/database/Result.java index 73356fe67..d0af38a01 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/model/database/Result.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/model/database/Result.java @@ -12,14 +12,11 @@ import com.raizlabs.android.dbflow.structure.BaseModel; import org.apache.commons.io.FileUtils; +import org.openobservatory.engine.BaseNettest; import org.openobservatory.ooniprobe.common.AppDatabase; +import org.openobservatory.ooniprobe.common.OONIDescriptor; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.MiddleBoxesSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.Dash; import org.openobservatory.ooniprobe.test.test.FacebookMessenger; import org.openobservatory.ooniprobe.test.test.HttpHeaderFieldManipulation; @@ -100,7 +97,7 @@ public List getMeasurements() { * Psiphon - Tor for Circumvention */ public List getMeasurementsSorted() { - if (WebsitesSuite.NAME.equals(test_group_name)) + if (OONITests.WEBSITES.getLabel().equals(test_group_name)) return SQLite.select().from(Measurement.class) .where(Measurement_Table.result_id.eq(id), Measurement_Table.is_rerun.eq(false), Measurement_Table.is_done.eq(true)) .orderBy(Measurement_Table.is_anomaly, false) @@ -176,39 +173,20 @@ public String getFormattedDataUsageDown() { return readableFileSize(this.data_usage_down); } - public AbstractSuite getTestSuite() { - switch (test_group_name) { - case WebsitesSuite.NAME: - return new WebsitesSuite(); - case InstantMessagingSuite.NAME: - return new InstantMessagingSuite(); - case MiddleBoxesSuite.NAME: - return new MiddleBoxesSuite(); - case PerformanceSuite.NAME: - return new PerformanceSuite(); - case CircumventionSuite.NAME: - return new CircumventionSuite(); - case ExperimentalSuite.NAME: - return new ExperimentalSuite(); - default: - return null; - } + public AbstractSuite getTestSuite(Context context) { + return getDescriptor(context).getTest(context); } private String[] getTestOrder() { - switch (test_group_name) { - case WebsitesSuite.NAME: - return new String[]{WebConnectivity.NAME}; - case InstantMessagingSuite.NAME: - return new String[]{Whatsapp.NAME, Telegram.NAME, FacebookMessenger.NAME, Signal.NAME}; - case MiddleBoxesSuite.NAME: - return new String[]{HttpInvalidRequestLine.NAME, HttpHeaderFieldManipulation.NAME}; - case PerformanceSuite.NAME: - return new String[]{Ndt.NAME, Dash.NAME, HttpInvalidRequestLine.NAME, HttpHeaderFieldManipulation.NAME}; - default: - return null; - } - } + if (test_group_name.equals(OONITests.WEBSITES.getLabel())) { + return new String[]{WebConnectivity.NAME}; + } else if (test_group_name.equals(OONITests.INSTANT_MESSAGING.getLabel())) { + return new String[]{Whatsapp.NAME, Telegram.NAME, FacebookMessenger.NAME, Signal.NAME}; + } else if (test_group_name.equals(OONITests.PERFORMANCE.getLabel())) { + return new String[]{Ndt.NAME, Dash.NAME, HttpInvalidRequestLine.NAME, HttpHeaderFieldManipulation.NAME}; + } + return null; + } public void delete(Context c) { for (Measurement measurement : getAllMeasurements()) { @@ -221,4 +199,8 @@ public void delete(Context c) { if (this.network != null) this.network.delete(); } + + public OONIDescriptor getDescriptor(Context context) { + return OONITests.valueOf(test_group_name).toOONIDescriptor(context); + } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/receiver/TestRunBroadRequestReceiver.java b/app/src/main/java/org/openobservatory/ooniprobe/receiver/TestRunBroadRequestReceiver.java index 668d8933f..02664e13b 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/receiver/TestRunBroadRequestReceiver.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/receiver/TestRunBroadRequestReceiver.java @@ -35,8 +35,9 @@ public class TestRunBroadRequestReceiver extends BroadcastReceiver implements Se /** * Instantiates a new Test run broad request receiver. - * @param preferenceManager the preference manager - * @param listener the listener + * + * @param preferenceManager the preference manager + * @param listener the listener * @param testProgressRepository */ public TestRunBroadRequestReceiver(PreferenceManager preferenceManager, EventListener listener, TestProgressRepository testProgressRepository) { @@ -51,10 +52,10 @@ public void onReceive(Context context, Intent intent) { String value = intent.getStringExtra("value"); switch (key) { case TestAsyncTask.START: - listener.onStart(service); + if (listener != null) listener.onStart(service); break; case TestAsyncTask.RUN: - listener.onRun(value); + if (listener != null) listener.onRun(value); break; case TestAsyncTask.PRG: try { @@ -108,7 +109,7 @@ public void onServiceConnected(ComponentName cname, IBinder binder) { RunTestService.TestBinder b = (RunTestService.TestBinder) binder; service = b.getService(); isBound = true; - listener.onStart(service); + if (listener != null) listener.onStart(service); runtime = ListUtility.sum(Lists.transform(service.task.testSuites, input -> input.getRuntime(preferenceManager))); } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/TestAsyncTask.java b/app/src/main/java/org/openobservatory/ooniprobe/test/TestAsyncTask.java index f7e642221..a2f88f3f7 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/TestAsyncTask.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/test/TestAsyncTask.java @@ -29,11 +29,6 @@ import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.model.database.Url; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.WebConnectivity; @@ -65,10 +60,6 @@ public class TestAsyncTask extends AsyncTask implements Abst private boolean store_db = true; private boolean unattended; - public static List getSuites() { - return Arrays.asList(new WebsitesSuite(), - new InstantMessagingSuite(), new CircumventionSuite(), new PerformanceSuite(), new ExperimentalSuite()); - } public TestAsyncTask(Application app, ArrayList testSuites) { this.app = app; diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuite.java index e3f5fc94d..134d77d9b 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuite.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuite.java @@ -21,14 +21,14 @@ import java.util.List; public abstract class AbstractSuite implements Serializable { - private final int title; - private final int cardDesc; + private final String title; + private final String cardDesc; private final int icon; private final int icon_24; private final int color; private final int themeLight; private final int themeDark; - private final int desc1; + private final String desc1; private final String anim; private final String name; private final int dataUsage; @@ -36,7 +36,7 @@ public abstract class AbstractSuite implements Serializable { private Result result; private boolean autoRun; - AbstractSuite(String name, @StringRes int title, @StringRes int cardDesc, @DrawableRes int icon, @DrawableRes int icon_24, @ColorRes int color, @StyleRes int themeLight, @StyleRes int themeDark, @StringRes int desc1, String anim, int dataUsage) { + AbstractSuite(String name, String title, String cardDesc, @DrawableRes int icon, @DrawableRes int icon_24, @ColorRes int color, @StyleRes int themeLight, @StyleRes int themeDark, String desc1, String anim, int dataUsage) { this.title = title; this.cardDesc = cardDesc; this.icon = icon; @@ -68,11 +68,11 @@ public void setResult(Result result) { this.result = result; } - public int getTitle() { + public String getTitle() { return title; } - public int getCardDesc() { + public String getCardDesc() { return cardDesc; } @@ -104,7 +104,7 @@ public int getThemeDark() { return themeDark; } - public int getDesc1() { + public String getDesc1() { return desc1; } @@ -145,38 +145,4 @@ public boolean getAutoRun() { public void setAutoRun(boolean autoRun) { this.autoRun = autoRun; } - - public static AbstractSuite getSuite(Application app, - String tn, - @Nullable List urls, - String origin) { - for (AbstractSuite suite : TestAsyncTask.getSuites()) - for (AbstractTest test : suite.getTestList(app.getPreferenceManager())) - if (test.getName().equals(tn)) { - if (urls != null) - for (String url : urls) - Url.checkExistingUrl(url); - test.setInputs(urls); - test.setOrigin(origin); - suite.setTestList(test); - return suite; - } - return null; - } - public static AbstractSuite getTestSuiteByName(String name){ - switch (name){ - case WebsitesSuite.NAME: - return new WebsitesSuite(); - case InstantMessagingSuite.NAME: - return new InstantMessagingSuite(); - case CircumventionSuite.NAME: - return new CircumventionSuite(); - case PerformanceSuite.NAME: - return new PerformanceSuite(); - case ExperimentalSuite.NAME: - return new ExperimentalSuite(); - default: - return new ExperimentalSuite(); - } - } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteExtensions.kt b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteExtensions.kt index 9431b6856..e879a3863 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteExtensions.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteExtensions.kt @@ -1,56 +1,30 @@ package org.openobservatory.ooniprobe.test.suite -import org.openobservatory.ooniprobe.activity.runtests.models.ChildItem -import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem -import org.openobservatory.ooniprobe.common.PreferenceManager +import org.openobservatory.ooniprobe.common.Application +import org.openobservatory.ooniprobe.common.ooniDescriptors +import org.openobservatory.ooniprobe.model.database.Url -fun AbstractSuite.runTestsGroupItem(preferenceManager: PreferenceManager): GroupItem { - if (this is ExperimentalSuite) { - return GroupItem( - selected = false, - name = this.name, - nettests = this.getTestList(preferenceManager).map { nettest -> - ChildItem( - selected = preferenceManager.isExperimentalOn, - name = nettest.name, - inputs = nettest.inputs - ) - }.toMutableList().apply { - addAll(longRunningTests().map { - ChildItem( - selected = false, - name = it.name, - inputs = null - ) - }) - }) - } else { - return GroupItem( - selected = false, - name = this.name, - nettests = this.getTestList(preferenceManager).map { nettest -> - ChildItem( - selected = preferenceManager.resolveStatus(nettest.name), - name = nettest.name, - inputs = nettest.inputs - ) - }) - } -} -fun AbstractSuite.dynamicTestSuite(nettests: List): DynamicTestSuite { - return DynamicTestSuite( - name = this.name, - title = this.title, - cardDesc = this.cardDesc, - icon = this.icon, - icon_24 = this.iconGradient, - color = this.color, - themeLight = this.themeLight, - themeDark = this.themeDark, - desc1 = this.desc1, - anim = this.anim, - dataUsage = this.dataUsage, - nettest = nettests - ) -} +fun getSuite( + app: Application, + tn: String, + urls: List?, + origin: String? +): AbstractSuite? { + for (suite in ooniDescriptors(app).map { return@map it.getTest(app) }) { + for (test in suite.getTestList(app.preferenceManager)) { + if (test.name == tn) { + if (urls != null) { + for (url in urls) { + Url.checkExistingUrl(url) + } + } + test.inputs = urls + test.setOrigin(origin) + suite.setTestList(test) + return suite + } + } + } + return null +} \ No newline at end of file diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuite.java deleted file mode 100644 index f077ed2ca..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuite.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import com.google.common.collect.Lists; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.Psiphon; -import org.openobservatory.ooniprobe.test.test.Tor; - -import java.util.ArrayList; - -public class CircumventionSuite extends AbstractSuite { - public static final String NAME = "circumvention"; - - public CircumventionSuite(){ - super(NAME, - R.string.Test_Circumvention_Fullname, - R.string.Dashboard_Circumvention_Card_Description, - R.drawable.test_circumvention, - R.drawable.test_circumvention_24, - R.color.color_pink6, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Circumvention, - R.style.Theme_MaterialComponents_NoActionBar_App_Circumvention, - R.string.Dashboard_Circumvention_Overview_Paragraph, - "anim/circumvention.json", - R.string.small_datausage); - } - - - public static CircumventionSuite initForAutoRun() { - CircumventionSuite suite = new CircumventionSuite(); - suite.setAutoRun(true); - return suite; - } - - @Override public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - ArrayList list = new ArrayList<>(); - // if (pm == null || pm.isTestPsiphon()) - list.add(new Psiphon()); - // if (pm == null || pm.isTestTor()) - list.add(new Tor()); - super.setTestList(Lists.transform(list, test -> { - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - return test; - }).toArray(new AbstractTest[0])); - } - return super.getTestList(pm); - } - -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/DynamicTestSuite.kt b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/DynamicTestSuite.kt index 51ce83521..46f20b3cb 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/DynamicTestSuite.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/DynamicTestSuite.kt @@ -6,43 +6,44 @@ import androidx.annotation.StringRes import androidx.annotation.StyleRes import com.google.common.collect.Lists import org.openobservatory.engine.BaseNettest +import org.openobservatory.ooniprobe.R import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.test.test.AbstractTest class DynamicTestSuite( - name: String, - @StringRes title: Int, - @StringRes cardDesc: Int, - @DrawableRes icon: Int, - @DrawableRes icon_24: Int, - @ColorRes color: Int, - @StyleRes themeLight: Int, - @StyleRes themeDark: Int, - @StringRes desc1: Int, - anim: String, - dataUsage: Int, - var nettest: List + name: String, + title: String, + shortDescription: String, + @DrawableRes icon: Int, + @DrawableRes icon_24: Int, + @ColorRes color: Int, + description: String, + animation: String, + dataUsage: Int, + var nettest: List ) : AbstractSuite( - name, - title, - cardDesc, - icon, - icon_24, - color, - themeLight, - themeDark, - desc1, - anim, - dataUsage + name, + title, + shortDescription, + icon, + icon_24, + color, + R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Experimental, + R.style.Theme_MaterialComponents_NoActionBar_App_Experimental, + description, + animation, + dataUsage ) { - override fun getTestList(pm: PreferenceManager?): Array { - super.setTestList(*Lists.transform( - nettest.map { AbstractTest.getTestByName(it.name) } - ) { test: AbstractTest? -> - if (autoRun) test!!.setOrigin(AbstractTest.AUTORUN) - test - }.toTypedArray() - ) - return super.getTestList(pm) - } + override fun getTestList(pm: PreferenceManager?): Array { + val tests = nettest.map { + AbstractTest.getTestByName(it.name).apply { + if (autoRun) { + setOrigin(AbstractTest.AUTORUN) + } + } + }.toTypedArray() + + super.setTestList(*tests) + return super.getTestList(pm) + } } diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuite.java deleted file mode 100644 index 04defe633..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuite.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import com.google.common.collect.Lists; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.Experimental; - -import java.util.ArrayList; -import java.util.Collections; - -public class ExperimentalSuite extends AbstractSuite { - public static final String NAME = "experimental"; - - public ExperimentalSuite() { - super(NAME, - R.string.Test_Experimental_Fullname, - R.string.Dashboard_Experimental_Card_Description, - R.drawable.test_experimental, - R.drawable.test_experimental_24, - R.color.color_gray7_1, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Experimental, - R.style.Theme_MaterialComponents_NoActionBar_App_Experimental, - R.string.Dashboard_Experimental_Overview_Paragraph, - "anim/experimental.json", - R.string.TestResults_NotAvailable); - } - - public static ExperimentalSuite initForAutoRun() { - ExperimentalSuite suite = new ExperimentalSuite(); - suite.setAutoRun(true); - return suite; - } - - - @Override - public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - ArrayList list = new ArrayList<>(); - // if (pm == null || pm.isExperimentalOn()){ - list.add(new Experimental("stunreachability")); - list.add(new Experimental("dnscheck")); - list.add(new Experimental("echcheck")); - if ((pm == null || pm.isLongRunningTestsInForeground()) || getAutoRun()){ - Collections.addAll(list, longRunningTests()); - } - // } - super.setTestList(Lists.transform(list, test -> { - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - return test; - }).toArray(new AbstractTest[0])); - } - return super.getTestList(pm); - } - - public AbstractTest[] longRunningTests(){ - return new AbstractTest[]{new Experimental("torsf"), new Experimental("vanilla_tor")}; - } - -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuite.java deleted file mode 100644 index 76fac59a9..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuite.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import com.google.common.collect.Lists; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.FacebookMessenger; -import org.openobservatory.ooniprobe.test.test.Signal; -import org.openobservatory.ooniprobe.test.test.Telegram; -import org.openobservatory.ooniprobe.test.test.Whatsapp; - -import java.util.ArrayList; - -public class InstantMessagingSuite extends AbstractSuite { - public static final String NAME = "instant_messaging"; - - public InstantMessagingSuite() { - super(NAME, - R.string.Test_InstantMessaging_Fullname, - R.string.Dashboard_InstantMessaging_Card_Description, - R.drawable.test_instant_messaging, - R.drawable.test_instant_messaging_24, - R.color.color_cyan6, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_InstantMessaging, - R.style.Theme_MaterialComponents_NoActionBar_App_InstantMessaging, - R.string.Dashboard_InstantMessaging_Overview_Paragraph, - "anim/instant_messaging.json", - R.string.small_datausage); - } - - public static InstantMessagingSuite initForAutoRun() { - InstantMessagingSuite suite = new InstantMessagingSuite(); - suite.setAutoRun(true); - return suite; - } - - @Override public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - ArrayList list = new ArrayList<>(); - // if (pm == null || pm.isTestWhatsapp()) - list.add(new Whatsapp()); - // if (pm == null || pm.isTestTelegram()) - list.add(new Telegram()); - // if (pm == null || pm.isTestFacebookMessenger()) - list.add(new FacebookMessenger()); - // if (pm == null || pm.isTestSignal()) - list.add(new Signal()); - super.setTestList(Lists.transform(list, test->{ - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - return test; - }).toArray(new AbstractTest[0])); - } - return super.getTestList(pm); - } -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuite.java deleted file mode 100644 index c53805a5b..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuite.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import com.google.common.collect.Lists; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.HttpHeaderFieldManipulation; -import org.openobservatory.ooniprobe.test.test.HttpInvalidRequestLine; - -import java.util.ArrayList; - -/** - * @deprecated - * It is not possible to run a MiddleBoxesSuite anymore - * The HttpHeaderFieldManipulation and HttpInvalidRequestLine tests - * are being ran inside the PerformanceSuite - */ -@Deprecated -public class MiddleBoxesSuite extends AbstractSuite { - public static final String NAME = "middle_boxes"; - - public MiddleBoxesSuite() { - super(NAME, - R.string.Test_Middleboxes_Fullname, - R.string.Dashboard_Middleboxes_Card_Description, - R.drawable.test_middle_boxes, - R.drawable.test_middle_boxes_24, - R.color.color_violet8, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_MiddleBoxes, - R.style.Theme_MaterialComponents_NoActionBar_App_MiddleBoxes, - R.string.Dashboard_Middleboxes_Overview_Paragraph, - "anim/middle_boxes.json", - R.string.small_datausage); - } - - @Override public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - ArrayList list = new ArrayList<>(); - // if (pm == null || pm.isRunHttpHeaderFieldManipulation()) - list.add(new HttpHeaderFieldManipulation()); - // if (pm == null || pm.isRunHttpInvalidRequestLine()) - list.add(new HttpInvalidRequestLine()); - super.setTestList(Lists.transform(list, test->{ - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - return test; - }).toArray(new AbstractTest[0])); - } - return super.getTestList(pm); - } -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuite.java deleted file mode 100644 index 36824adb9..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuite.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import com.google.common.collect.Lists; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.Dash; -import org.openobservatory.ooniprobe.test.test.HttpHeaderFieldManipulation; -import org.openobservatory.ooniprobe.test.test.HttpInvalidRequestLine; -import org.openobservatory.ooniprobe.test.test.Ndt; - -import java.util.ArrayList; - -public class PerformanceSuite extends AbstractSuite { - public static final String NAME = "performance"; - - public PerformanceSuite() { - super(NAME, - R.string.Test_Performance_Fullname, - R.string.Dashboard_Performance_Card_Description, - R.drawable.test_performance, - R.drawable.test_performance_24, - R.color.color_fuchsia6, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Performance, - R.style.Theme_MaterialComponents_NoActionBar_App_Performance, - R.string.Dashboard_Performance_Overview_Paragraph_Updated, - "anim/performance.json", - R.string.performance_datausage); - } - - public static PerformanceSuite initForAutoRun() { - PerformanceSuite suite = new PerformanceSuite(); - suite.setAutoRun(true); - return suite; - } - - @Override - public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - ArrayList list = new ArrayList<>(); - // if ((pm == null || pm.isRunNdt()) && !getAutoRun()) - list.add(new Ndt()); - // if ((pm == null || pm.isRunDash()) && !getAutoRun()) - list.add(new Dash()); - // if ((pm == null || pm.isRunHttpHeaderFieldManipulation())) - list.add(new HttpHeaderFieldManipulation()); - // if ((pm == null || pm.isRunHttpInvalidRequestLine())) - list.add(new HttpInvalidRequestLine()); - super.setTestList(Lists.transform(list, test -> { - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - return test; - }).toArray(new AbstractTest[0])); - } - return super.getTestList(pm); - } -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/WebsitesSuite.java b/app/src/main/java/org/openobservatory/ooniprobe/test/suite/WebsitesSuite.java deleted file mode 100644 index 4496152a5..000000000 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/suite/WebsitesSuite.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import androidx.annotation.Nullable; - -import org.openobservatory.ooniprobe.R; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.WebConnectivity; - -public class WebsitesSuite extends AbstractSuite { - public static final String NAME = "websites"; - - public WebsitesSuite() { - super(NAME, - R.string.Test_Websites_Fullname, - R.string.Dashboard_Websites_Card_Description, - R.drawable.test_websites, - R.drawable.test_websites_24, - R.color.color_indigo6, - R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Websites, - R.style.Theme_MaterialComponents_NoActionBar_App_Websites, - R.string.Dashboard_Websites_Overview_Paragraph, - "anim/websites.json", - R.string.websites_datausage); - } - - @Override public AbstractTest[] getTestList(@Nullable PreferenceManager pm) { - if (super.getTestList(pm) == null) { - WebConnectivity test = new WebConnectivity(); - if (getAutoRun()) test.setOrigin(AbstractTest.AUTORUN); - super.setTestList(test); - } - return super.getTestList(pm); - } -} diff --git a/app/src/main/java/org/openobservatory/ooniprobe/test/test/AbstractTest.java b/app/src/main/java/org/openobservatory/ooniprobe/test/test/AbstractTest.java index 09a283114..10782b4c9 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/test/test/AbstractTest.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/test/test/AbstractTest.java @@ -18,9 +18,9 @@ import org.openobservatory.ooniprobe.common.AppLogger; import org.openobservatory.ooniprobe.common.Application; import org.openobservatory.ooniprobe.common.MKException; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReachabilityManager; -import org.openobservatory.ooniprobe.common.ResubmitTask; import org.openobservatory.ooniprobe.common.ThirdPartyServices; import org.openobservatory.ooniprobe.model.database.Measurement; import org.openobservatory.ooniprobe.model.database.Network; @@ -30,7 +30,6 @@ import org.openobservatory.ooniprobe.model.jsonresult.JsonResult; import org.openobservatory.ooniprobe.model.settings.Settings; import org.openobservatory.ooniprobe.test.EngineProvider; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; import java.io.File; import java.io.Serializable; @@ -39,8 +38,6 @@ import java.util.List; import java.util.Objects; -import javax.inject.Inject; - public abstract class AbstractTest implements Serializable { private static final String UNUSED_KEY = "UNUSED_KEY"; private static final String TAG = "MK_EVENT"; @@ -90,11 +87,11 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings } catch (Exception exc) { //TODO call setFailureMsg here and in other point of (non) return exc.printStackTrace(); - logger.e(TAG,exc.getMessage()); + logger.e(TAG, exc.getMessage()); ThirdPartyServices.logException(exc); return; } - while (!task.isDone()){ + while (!task.isDone()) { try { File logFile = null; if (result != null) { @@ -111,7 +108,7 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings switch (event.key) { case "status.started": if (result != null && - result.test_group_name.equals(ExperimentalSuite.NAME)) + result.test_group_name.equals(OONITests.EXPERIMENTAL.getLabel())) testCallback.onStart(name); else testCallback.onStart(c.getString(labelResId)); @@ -126,7 +123,7 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings network.country_code = event.value.probe_cc; network.network_type = ReachabilityManager.getNetworkType(c); } else { - this.network=null; + this.network = null; } saveNetworkInfo(event.value, result, c); break; @@ -134,7 +131,7 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings reportId = event.value.report_id; break; case "status.measurement_start": - if (result != null){ + if (result != null) { Measurement measurement = new Measurement(result, name, reportId); if (event.value.input.length() > 0) measurement.url = Url.getUrl(event.value.input); @@ -182,7 +179,7 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings m.is_failed = true; else onEntry(c, pm, jr, m); - if (network!=null ){ + if (network != null) { m.rerun_network = gson.toJson(network); } m.save(); @@ -233,7 +230,7 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings break; default: Log.w(UNUSED_KEY, event.key); - logger.w(UNUSED_KEY,event.key); + logger.w(UNUSED_KEY, event.key); break; } } catch (Exception e) { @@ -250,12 +247,12 @@ void run(Context c, PreferenceManager pm, AppLogger logger, Gson gson, Settings } } - public boolean canInterrupt(){ - return task == null ? false : task.canInterrupt(); + public boolean canInterrupt() { + return task == null ? false : task.canInterrupt(); } - public void interrupt(){ - if(task.canInterrupt()) { + public void interrupt() { + if (task.canInterrupt()) { task.interrupt(); } } @@ -302,10 +299,10 @@ private void setDone(EventResult.Value value) { } } - protected void onTaskTerminated(EventResult.Value value, Context context){ + protected void onTaskTerminated(EventResult.Value value, Context context) { Measurement measurement = measurements.get(value.idx); if (measurement != null) { - if(measurement.is_uploaded){ + if (measurement.is_uploaded) { measurement.deleteReportFile(context); } } @@ -320,13 +317,13 @@ private void setDataUsage(EventResult.Value value, Result result) { private void setFailureMsg(EventResult.Value value, Result result) { if (result == null) { - return; - } - if (result.failure_msg == null) { - result.failure_msg = value.failure; - } else { - result.failure_msg = String.format("%s\n\n%s", result.failure_msg, value.failure); - } + return; + } + if (result.failure_msg == null) { + result.failure_msg = value.failure; + } else { + result.failure_msg = String.format("%s\n\n%s", result.failure_msg, value.failure); + } result.save(); } @@ -374,7 +371,7 @@ public boolean isAutoRun() { return Objects.equals(origin, AUTORUN); } - public static AbstractTest getTestByName(String name){ + public static AbstractTest getTestByName(String name) { return switch (name) { case Dash.NAME -> new Dash(); case FacebookMessenger.NAME -> new FacebookMessenger(); @@ -391,7 +388,8 @@ public static AbstractTest getTestByName(String name){ case Experimental.NAME -> new Experimental(name); default -> new Experimental(name); }; - } + } + public interface TestCallback { void onStart(String name); diff --git a/app/src/main/res/layout/activity_overview.xml b/app/src/main/res/layout/activity_overview.xml index ea5263718..ee29d3472 100644 --- a/app/src/main/res/layout/activity_overview.xml +++ b/app/src/main/res/layout/activity_overview.xml @@ -8,11 +8,13 @@ tools:context=".activity.OverviewActivity"> resubmitTask = build(testResult.id); when(managerMock.reSubmit(any(), any())).thenReturn(true); @@ -59,8 +58,8 @@ public void notUploadedMeasurementsTest() { @Test public void notUploadedByResultIdMeasurementsTest() { // Arrange - Result testResult = ResultFactory.createAndSaveWithEntryFiles(c, new WebsitesSuite(), 5, 0, false); - ResultFactory.createAndSaveWithEntryFiles(c, new WebsitesSuite(), 5, 0, false); + Result testResult = ResultFactory.createAndSaveWithEntryFiles(c, OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 5, 0, false); + ResultFactory.createAndSaveWithEntryFiles(c, OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 5, 0, false); ResubmitTask resubmitTask = build(testResult.id); when(managerMock.reSubmit(any(), any())).thenReturn(true); @@ -77,7 +76,7 @@ public void notUploadedByResultIdMeasurementsTest() { @Test public void notUploadedByMeasurementIdMeasurementsTest() { // Arrange - Result testResult = ResultFactory.createAndSaveWithEntryFiles(c, new WebsitesSuite(), 5, 0, false); + Result testResult = ResultFactory.createAndSaveWithEntryFiles(c, OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 5, 0, false); ResubmitTask resubmitTask = build(testResult.id); when(managerMock.reSubmit(any(), any())).thenReturn(true); diff --git a/app/src/test/java/org/openobservatory/ooniprobe/domain/GetResultsTest.java b/app/src/test/java/org/openobservatory/ooniprobe/domain/GetResultsTest.java index b01083f49..e6d50dc75 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/domain/GetResultsTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/domain/GetResultsTest.java @@ -2,13 +2,10 @@ import org.junit.Test; import org.openobservatory.ooniprobe.RobolectricAbstractTest; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.domain.models.DatedResults; import org.openobservatory.ooniprobe.factory.ResultFactory; import org.openobservatory.ooniprobe.model.database.Result; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.utils.DatabaseUtils; import java.util.Calendar; @@ -29,7 +26,7 @@ public void setUp() { @Test public void getterTest() { // Arrange - Result result = ResultFactory.createAndSave(new WebsitesSuite()); + Result result = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)); GetResults getResults = build(); // Act @@ -49,10 +46,10 @@ public void getterTest() { // Act List all = getResults.getOrderedByTime(null); - List web = getResults.getOrderedByTime(WebsitesSuite.NAME); - List messaging = getResults.getOrderedByTime(InstantMessagingSuite.NAME); - List circumvention = getResults.getOrderedByTime(CircumventionSuite.NAME); - List performance = getResults.getOrderedByTime(PerformanceSuite.NAME); + List web = getResults.getOrderedByTime(OONITests.WEBSITES.getLabel()); + List messaging = getResults.getOrderedByTime(OONITests.INSTANT_MESSAGING.getLabel()); + List circumvention = getResults.getOrderedByTime(OONITests.CIRCUMVENTION.getLabel()); + List performance = getResults.getOrderedByTime(OONITests.PERFORMANCE.getLabel()); // Assert assertEquals(all.size(), 4); @@ -60,10 +57,10 @@ public void getterTest() { assertEquals(messaging.size(), 1); assertEquals(circumvention.size(), 1); assertEquals(performance.size(), 1); - assertEquals(all.get(0).test_group_name, PerformanceSuite.NAME); - assertEquals(all.get(1).test_group_name, CircumventionSuite.NAME); - assertEquals(all.get(2).test_group_name, InstantMessagingSuite.NAME); - assertEquals(all.get(3).test_group_name, WebsitesSuite.NAME); + assertEquals(all.get(0).test_group_name, OONITests.PERFORMANCE.getLabel()); + assertEquals(all.get(1).test_group_name, OONITests.CIRCUMVENTION.getLabel()); + assertEquals(all.get(2).test_group_name, OONITests.INSTANT_MESSAGING.getLabel()); + assertEquals(all.get(3).test_group_name, OONITests.INSTANT_MESSAGING.getLabel()); } @Test public void groupedByMonth() { @@ -73,10 +70,10 @@ public void getterTest() { // Act List all = getResults.getGroupedByMonth(null); - List web = getResults.getGroupedByMonth(WebsitesSuite.NAME); - List messaging = getResults.getGroupedByMonth(InstantMessagingSuite.NAME); - List circumvention = getResults.getGroupedByMonth(CircumventionSuite.NAME); - List performance = getResults.getGroupedByMonth(PerformanceSuite.NAME); + List web = getResults.getGroupedByMonth(OONITests.WEBSITES.getLabel()); + List messaging = getResults.getGroupedByMonth(OONITests.INSTANT_MESSAGING.getLabel()); + List circumvention = getResults.getGroupedByMonth(OONITests.CIRCUMVENTION.getLabel()); + List performance = getResults.getGroupedByMonth(OONITests.PERFORMANCE.getLabel()); // Assert assertEquals(all.size(), 4); @@ -95,27 +92,27 @@ public void getterTest() { assertEquals(all.get(2).getResultsList().size(), 1); assertEquals(all.get(3).getResultsList().size(), 1); - assertEquals(all.get(0).getResultsList().get(0).test_group_name, PerformanceSuite.NAME); - assertEquals(all.get(1).getResultsList().get(0).test_group_name, CircumventionSuite.NAME); - assertEquals(all.get(2).getResultsList().get(0).test_group_name, InstantMessagingSuite.NAME); - assertEquals(all.get(3).getResultsList().get(0).test_group_name, WebsitesSuite.NAME); + assertEquals(all.get(0).getResultsList().get(0).test_group_name, OONITests.PERFORMANCE.getLabel()); + assertEquals(all.get(1).getResultsList().get(0).test_group_name, OONITests.CIRCUMVENTION.getLabel()); + assertEquals(all.get(2).getResultsList().get(0).test_group_name, OONITests.INSTANT_MESSAGING.getLabel()); + assertEquals(all.get(3).getResultsList().get(0).test_group_name, OONITests.WEBSITES.getLabel()); } private void createDatedResults() { - Result websites = ResultFactory.createAndSave(new WebsitesSuite()); + Result websites = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)); websites.start_time = getDateFrom(1, Calendar.JANUARY, 2020); websites.save(); - Result messaging = ResultFactory.createAndSave(new InstantMessagingSuite()); + Result messaging = ResultFactory.createAndSave(OONITests.INSTANT_MESSAGING.toOONIDescriptor(c).getTest(c)); messaging.start_time = getDateFrom(1, Calendar.FEBRUARY, 2020); messaging.save(); - Result circumvention = ResultFactory.createAndSave(new CircumventionSuite(), 1, 2); + Result circumvention = ResultFactory.createAndSave(OONITests.CIRCUMVENTION.toOONIDescriptor(c).getTest(c), 1, 2); circumvention.start_time = getDateFrom(1, Calendar.MARCH, 2020); circumvention.save(); - Result performance = ResultFactory.createAndSave(new PerformanceSuite()); + Result performance = ResultFactory.createAndSave(OONITests.PERFORMANCE.toOONIDescriptor(c).getTest(c)); performance.start_time = getDateFrom(1, Calendar.APRIL, 2020); performance.save(); } diff --git a/app/src/test/java/org/openobservatory/ooniprobe/domain/GetTestSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/domain/GetTestSuiteTest.java index faaa0b555..eb085395b 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/domain/GetTestSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/domain/GetTestSuiteTest.java @@ -3,12 +3,12 @@ import org.junit.Assert; import org.junit.Test; import org.openobservatory.ooniprobe.RobolectricAbstractTest; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.domain.models.Attribute; import org.openobservatory.ooniprobe.factory.ResultFactory; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.WebConnectivity; @@ -37,7 +37,7 @@ public void testWithAttributes() { AbstractTest[] tests = suite.getTestList(pm); // Assert - Assert.assertEquals(WebsitesSuite.NAME, suite.getName()); + Assert.assertEquals(OONITests.WEBSITES.getLabel(), suite.getName()); Assert.assertEquals(1, tests.length); Assert.assertEquals(urls.get(0), tests[0].getInputs().get(0)); Assert.assertEquals(urls.get(1), tests[0].getInputs().get(1)); @@ -55,7 +55,7 @@ public void testWithoutAttributes() { AbstractTest[] tests = suite.getTestList(pm); // Assert - Assert.assertEquals(WebsitesSuite.NAME, suite.getName()); + Assert.assertEquals(OONITests.WEBSITES.getLabel(), suite.getName()); Assert.assertEquals(1, tests.length); Assert.assertNull(tests[0].getInputs()); } @@ -64,7 +64,7 @@ public void testWithoutAttributes() { public void testGetFromResult() { // Arrange GetTestSuite getTestSuite = build(); - Result result = ResultFactory.createAndSave(new WebsitesSuite()); + Result result = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)); int measurementsUrls = result.getMeasurements().size(); PreferenceManager pm = mock(PreferenceManager.class); diff --git a/app/src/test/java/org/openobservatory/ooniprobe/domain/MeasurementsManagerTest.java b/app/src/test/java/org/openobservatory/ooniprobe/domain/MeasurementsManagerTest.java index 6c91f338e..02c524a8f 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/domain/MeasurementsManagerTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/domain/MeasurementsManagerTest.java @@ -11,6 +11,7 @@ import org.openobservatory.ooniprobe.client.OONIAPIClient; import org.openobservatory.ooniprobe.client.callback.CheckReportIdCallback; import org.openobservatory.ooniprobe.common.JsonPrinter; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.domain.callback.DomainCallback; import org.openobservatory.ooniprobe.domain.callback.GetMeasurementsCallback; import org.openobservatory.ooniprobe.factory.MeasurementFactory; @@ -20,7 +21,6 @@ import org.openobservatory.ooniprobe.model.database.Measurement; import org.openobservatory.ooniprobe.model.database.Measurement_Table; import org.openobservatory.ooniprobe.model.database.Result; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import java.io.File; import java.io.IOException; @@ -80,7 +80,7 @@ public void testGetMeasurement() { public void testCanUpload() { // Arrange List measurements = - ResultFactory.createAndSave(new WebsitesSuite(), 1, 0, false) + ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 1, 0, false) .getMeasurements(); MeasurementFactory.addEntryFiles(c, measurements, false); @@ -223,7 +223,7 @@ public void checkAndDeleteReportFailTest() { @Test public void testUploadableReports() { // Arrange - Result testResult = ResultFactory.createAndSave(new WebsitesSuite(), 5, 0, false); + Result testResult = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 5, 0, false); MeasurementFactory.addEntryFiles(c, testResult.getMeasurements(), false); testResult.save(); @@ -237,7 +237,7 @@ public void testUploadableReports() { @Test public void testNoUploadableReports() { // Arrange - ResultFactory.createAndSave(new WebsitesSuite()); + ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)); // Act boolean hasReports = manager.hasUploadables(); @@ -276,7 +276,7 @@ public void getReadableLog() throws IOException { public void getReadableEntry() throws IOException { // Arrange Measurement measurement = - ResultFactory.createAndSaveWithEntryFiles(c, new WebsitesSuite(), 1, 0, false) + ResultFactory.createAndSaveWithEntryFiles(c, OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 1, 0, false) .getMeasurements().get(0); when(jsonPrinter.prettyText("test")).thenReturn("pretty test"); @@ -336,7 +336,7 @@ public void downloadReportSuccessTest() { @Test public void downloadReportFailTest() { // Arrange - Measurement measurement = ResultFactory.createAndSave(new WebsitesSuite()).getMeasurements().get(0); + Measurement measurement = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)).getMeasurements().get(0); ApiMeasurement.Result result = new ApiMeasurement.Result(); result.measurement_url = faker.internet.url(); String failedCallbackResponse = "Something went wrong"; @@ -424,7 +424,7 @@ public void reSubmitTest() { String fileContent = "{}"; Measurement measurement = ResultFactory.createAndSaveWithEntryFiles( c, - new WebsitesSuite(), + OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), 5, 0, false @@ -462,7 +462,7 @@ public void reSubmitTest() { } private Measurement buildMeasurement() { - return ResultFactory.createAndSave(new WebsitesSuite()) + return ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)) .getMeasurements() .get(0); } diff --git a/app/src/test/java/org/openobservatory/ooniprobe/model/database/NetworkTest.java b/app/src/test/java/org/openobservatory/ooniprobe/model/database/NetworkTest.java index 115f3385d..9ab383c6f 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/model/database/NetworkTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/model/database/NetworkTest.java @@ -8,10 +8,10 @@ import org.junit.Test; import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.RobolectricAbstractTest; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.ReachabilityManager; import org.openobservatory.ooniprobe.factory.NetworkFactory; import org.openobservatory.ooniprobe.factory.ResultFactory; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -88,7 +88,7 @@ public void deleteTest() { // Arrange Network noResultNetwork = NetworkFactory.build(); noResultNetwork.save(); - Network resultNetwork = ResultFactory.createAndSave(new WebsitesSuite()).network; + Network resultNetwork = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)).network; // Act boolean noResultDelete = noResultNetwork.delete(); diff --git a/app/src/test/java/org/openobservatory/ooniprobe/model/database/ResultTest.java b/app/src/test/java/org/openobservatory/ooniprobe/model/database/ResultTest.java index 75d1e134b..ab1533a88 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/model/database/ResultTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/model/database/ResultTest.java @@ -2,12 +2,7 @@ import org.junit.Test; import org.openobservatory.ooniprobe.RobolectricAbstractTest; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.MiddleBoxesSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.test.test.HttpHeaderFieldManipulation; import org.openobservatory.ooniprobe.test.test.HttpInvalidRequestLine; import org.openobservatory.ooniprobe.test.test.Ndt; @@ -99,7 +94,7 @@ public void getMeasurements() { @Test public void getMeasurementsSorted_websites() { - Result result = new Result(WebsitesSuite.NAME); + Result result = new Result(OONITests.WEBSITES.getLabel()); result.save(); Measurement measurementAnomaly = new Measurement(result, "a"); measurementAnomaly.is_done = true; @@ -118,7 +113,7 @@ public void getMeasurementsSorted_websites() { @Test public void getMeasurementsSorted_instantMessaging() { - Result result = new Result(InstantMessagingSuite.NAME); + Result result = new Result(OONITests.INSTANT_MESSAGING.getLabel()); result.save(); Measurement signal = new Measurement(result, Signal.NAME); signal.is_done = true; @@ -134,9 +129,9 @@ public void getMeasurementsSorted_instantMessaging() { assertEquals(Signal.NAME, measurements.get(1).test_name); } - @Test + /*@Test public void getMeasurementsSorted_middleBoxes() { - Result result = new Result(MiddleBoxesSuite.NAME); + Result result = new Result(OONITests.MiddleBox.getLabel()); result.save(); Measurement httpHeader = new Measurement(result, HttpHeaderFieldManipulation.NAME); httpHeader.is_done = true; @@ -150,11 +145,11 @@ public void getMeasurementsSorted_middleBoxes() { assertEquals(2, measurements.size()); assertEquals(HttpInvalidRequestLine.NAME, measurements.get(0).test_name); assertEquals(HttpHeaderFieldManipulation.NAME, measurements.get(1).test_name); - } + }*/ @Test public void getMeasurementsSorted_performance() { - Result result = new Result(PerformanceSuite.NAME); + Result result = new Result(OONITests.PERFORMANCE.getLabel()); result.save(); Measurement httpHeader = new Measurement(result, HttpHeaderFieldManipulation.NAME); httpHeader.is_done = true; @@ -232,13 +227,12 @@ public void getFormattedDataUsageDown() { @Test public void getTestSuite() { - assertEquals(WebsitesSuite.class, new Result(WebsitesSuite.NAME).getTestSuite().getClass()); - assertEquals(InstantMessagingSuite.class, new Result(InstantMessagingSuite.NAME).getTestSuite().getClass()); - assertEquals(MiddleBoxesSuite.class, new Result(MiddleBoxesSuite.NAME).getTestSuite().getClass()); - assertEquals(PerformanceSuite.class, new Result(PerformanceSuite.NAME).getTestSuite().getClass()); - assertEquals(CircumventionSuite.class, new Result(CircumventionSuite.NAME).getTestSuite().getClass()); - assertEquals(ExperimentalSuite.class, new Result(ExperimentalSuite.NAME).getTestSuite().getClass()); - assertNull(new Result("invalid").getTestSuite()); + assertEquals(OONITests.WEBSITES.getLabel(), new Result(OONITests.WEBSITES.getLabel()).getTestSuite(c).getName()); + assertEquals(OONITests.INSTANT_MESSAGING.getLabel(), new Result(OONITests.INSTANT_MESSAGING.getLabel()).getTestSuite(c).getName()); + assertEquals(OONITests.PERFORMANCE.getLabel(), new Result(OONITests.PERFORMANCE.getLabel()).getTestSuite(c).getName()); + assertEquals(OONITests.CIRCUMVENTION.getLabel(), new Result(OONITests.CIRCUMVENTION.getLabel()).getTestSuite(c).getName()); + assertEquals(OONITests.EXPERIMENTAL.getLabel(), new Result(OONITests.EXPERIMENTAL.getLabel()).getTestSuite(c).getName()); + assertNull(new Result("invalid").getTestSuite(c)); } @Test diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/TestAsyncTaskTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/TestAsyncTaskTest.java index 0e8c69813..92bf03d62 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/TestAsyncTaskTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/TestAsyncTaskTest.java @@ -16,14 +16,13 @@ import org.openobservatory.engine.OONIURLInfo; import org.openobservatory.ooniprobe.BuildConfig; import org.openobservatory.ooniprobe.RobolectricAbstractTest; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.service.RunTestService; import org.openobservatory.ooniprobe.common.service.ServiceUtil; import org.openobservatory.ooniprobe.engine.TestEngineInterface; import org.openobservatory.ooniprobe.factory.ResultFactory; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.WebConnectivity; import org.openobservatory.ooniprobe.utils.DatabaseUtils; @@ -73,10 +72,10 @@ public void setUp() { public void storesTestSuitInDb() { // Arrange ArrayList suiteList = new ArrayList<>(); - AbstractSuite mockedSuite = mock(WebsitesSuite.class); + AbstractSuite mockedSuite = mock(AbstractSuite.class); suiteList.add(mockedSuite); TestAsyncTask task = new TestAsyncTask(a, suiteList); - Result testResult = ResultFactory.build(new WebsitesSuite(), true, true); + Result testResult = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, true); when(mockedSuite.getTestList(any())).thenReturn(new AbstractTest[0]); @@ -100,10 +99,10 @@ public void downloadsWebSuitUrls() throws Exception { // Arrange Faker faker = new Faker(); ArrayList suiteList = new ArrayList<>(); - AbstractSuite mockedSuite = mock(WebsitesSuite.class); + AbstractSuite mockedSuite = mock(AbstractSuite.class); suiteList.add(mockedSuite); TestAsyncTask task = new TestAsyncTask(a, suiteList); - Result testResult = ResultFactory.build(new WebsitesSuite(), true, true); + Result testResult = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, true); WebConnectivity test = new WebConnectivity(); test.setInputs(null); @@ -156,7 +155,7 @@ public void downloadsWebSuitUrls() throws Exception { public void runTest_withProgress() { // Arrange ArrayList suiteList = new ArrayList<>(); - AbstractSuite mockedSuite = mock(ExperimentalSuite.class); + AbstractSuite mockedSuite = mock(AbstractSuite.class); suiteList.add(mockedSuite); AbstractTest test = mock(AbstractTest.class); when(mockedSuite.getTestList(any())).thenReturn(new AbstractTest[]{test}); @@ -183,7 +182,7 @@ public void runTest_withProgress() { public void runTest_withError() { // Arrange ArrayList suiteList = new ArrayList<>(); - AbstractSuite mockedSuite = mock(ExperimentalSuite.class); + AbstractSuite mockedSuite = mock(AbstractSuite.class); suiteList.add(mockedSuite); AbstractTest test = mock(AbstractTest.class); when(mockedSuite.getTestList(any())).thenReturn(new AbstractTest[]{test}); @@ -203,7 +202,7 @@ public void runTest_withError() { public void runTest_interrupt() { // Arrange ArrayList suiteList = new ArrayList<>(); - AbstractSuite mockedSuite = mock(ExperimentalSuite.class); + AbstractSuite mockedSuite = mock(AbstractSuite.class); suiteList.add(mockedSuite); AbstractTest test = mock(AbstractTest.class); when(mockedSuite.getTestList(any())).thenReturn(new AbstractTest[]{test}); diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteTest.java index 4f67b922d..8f6d5fb9d 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/AbstractSuiteTest.java @@ -1,9 +1,12 @@ package org.openobservatory.ooniprobe.test.suite; +import static org.openobservatory.ooniprobe.test.suite.AbstractSuiteExtensionsKt.getSuite; + import org.junit.Before; import org.junit.Test; import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.test.test.AbstractTest; @@ -25,14 +28,14 @@ public class AbstractSuiteTest { private AbstractSuite suite = new AbstractSuite( "test", - R.string.Test_Middleboxes_Fullname, - R.string.Dashboard_Middleboxes_Card_Description, + app.getResources().getString(R.string.Test_Middleboxes_Fullname), + app.getResources().getString(R.string.Dashboard_Middleboxes_Card_Description), R.drawable.test_middle_boxes, R.drawable.test_middle_boxes_24, R.color.color_violet8, R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_MiddleBoxes, R.style.Theme_MaterialComponents_NoActionBar_App_MiddleBoxes, - R.string.Dashboard_Middleboxes_Overview_Paragraph, + app.getResources().getString(R.string.Dashboard_Middleboxes_Overview_Paragraph), "anim/middle_boxes.json", R.string.small_datausage ) { @@ -49,13 +52,13 @@ public void setUp() { @Test public void fields() { assertEquals("test", suite.getName()); - assertEquals(R.string.Test_Middleboxes_Fullname, suite.getTitle()); - assertEquals(R.string.Dashboard_Middleboxes_Card_Description, suite.getCardDesc()); + assertEquals(app.getResources().getString(R.string.Test_Middleboxes_Fullname), suite.getTitle()); + assertEquals(app.getResources().getString(R.string.Dashboard_Middleboxes_Card_Description), suite.getCardDesc()); assertEquals(R.drawable.test_middle_boxes, suite.getIcon()); assertEquals(R.color.color_violet8, suite.getColor()); assertEquals(R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_MiddleBoxes, suite.getThemeLight()); assertEquals(R.style.Theme_MaterialComponents_NoActionBar_App_MiddleBoxes, suite.getThemeDark()); - assertEquals(R.string.Dashboard_Middleboxes_Overview_Paragraph, suite.getDesc1()); + assertEquals(app.getResources().getString(R.string.Dashboard_Middleboxes_Overview_Paragraph), suite.getDesc1()); assertEquals("anim/middle_boxes.json", suite.getAnim()); assertEquals(R.string.small_datausage, suite.getDataUsage()); } @@ -95,13 +98,13 @@ public void asArray() { } @Test - public void getSuite() { + public void getSuiteTest() { when(pm.isRunDash()).thenReturn(true); - AbstractSuite s = AbstractSuite.getSuite(app, "dash", null, ""); + AbstractSuite s = getSuite(app, "dash", null, ""); assertNotNull(s); - assertEquals(PerformanceSuite.class, s.getClass()); + assertEquals(OONITests.PERFORMANCE.getLabel(), s.getName()); assertEquals(1, s.getTestList(pm).length); assertEquals(Dash.class, s.getTestList(pm)[0].getClass()); } diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuiteTest.java index 44271c373..7518c13d3 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/CircumventionSuiteTest.java @@ -1,6 +1,8 @@ package org.openobservatory.ooniprobe.test.suite; import org.junit.Test; +import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.Psiphon; @@ -15,10 +17,9 @@ import static org.mockito.Mockito.when; public class CircumventionSuiteTest { - - private final CircumventionSuite suite = new CircumventionSuite(); + private final Application app = mock(Application.class); + private final AbstractSuite suite = OONITests.CIRCUMVENTION.toOONIDescriptor(app).getTest(app); private final PreferenceManager pm = mock(PreferenceManager.class); - @Test public void getTestList_empty() { when(pm.isTestPsiphon()).thenReturn(false); diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuiteTest.java index 8b9fbd37d..a15589301 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/ExperimentalSuiteTest.java @@ -1,6 +1,9 @@ package org.openobservatory.ooniprobe.test.suite; +import org.junit.BeforeClass; import org.junit.Test; +import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.Experimental; @@ -13,11 +16,16 @@ import static org.mockito.Mockito.when; public class ExperimentalSuiteTest { + private final Application app = mock(Application.class); - private final ExperimentalSuite suite = new ExperimentalSuite(); - private final ExperimentalSuite autoRunSuite = ExperimentalSuite.initForAutoRun(); + private final AbstractSuite suite = OONITests.EXPERIMENTAL.toOONIDescriptor(app).getTest(app); + private final AbstractSuite autoRunSuite = OONITests.EXPERIMENTAL.toOONIDescriptor(app).getTest(app); private final PreferenceManager pm = mock(PreferenceManager.class); + @BeforeClass + public void setUp() { + autoRunSuite.setAutoRun(true); + } @Test public void getTestList_empty() { diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuiteTest.java index f25ad5613..f4d1eef14 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/InstantMessagingSuiteTest.java @@ -1,6 +1,8 @@ package org.openobservatory.ooniprobe.test.suite; import org.junit.Test; +import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.FacebookMessenger; @@ -18,7 +20,9 @@ public class InstantMessagingSuiteTest { - private final InstantMessagingSuite suite = new InstantMessagingSuite(); + private final Application app = mock(Application.class); + + private final AbstractSuite suite = OONITests.INSTANT_MESSAGING.toOONIDescriptor(app).getTest(app); private final PreferenceManager pm = mock(PreferenceManager.class); @Test diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuiteTest.java deleted file mode 100644 index bf5eb535b..000000000 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/MiddleBoxesSuiteTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.openobservatory.ooniprobe.test.suite; - -import org.junit.Test; -import org.openobservatory.ooniprobe.common.PreferenceManager; -import org.openobservatory.ooniprobe.test.test.AbstractTest; -import org.openobservatory.ooniprobe.test.test.HttpHeaderFieldManipulation; -import org.openobservatory.ooniprobe.test.test.HttpInvalidRequestLine; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class MiddleBoxesSuiteTest { - - private final MiddleBoxesSuite suite = new MiddleBoxesSuite(); - private final PreferenceManager pm = mock(PreferenceManager.class); - - @Test - public void getTestList_empty() { - when(pm.isRunHttpHeaderFieldManipulation()).thenReturn(false); - when(pm.isRunHttpInvalidRequestLine()).thenReturn(false); - - AbstractTest[] tests = suite.getTestList(pm); - - assertEquals(0, tests.length); - } - - @Test - public void getTestList_full() { - when(pm.isRunHttpHeaderFieldManipulation()).thenReturn(true); - when(pm.isRunHttpInvalidRequestLine()).thenReturn(true); - - List tests = Arrays.asList(suite.getTestList(pm)); - - assertEquals(2, tests.size()); - assertTrue(findTestClass(tests, HttpHeaderFieldManipulation.class)); - assertTrue(findTestClass(tests, HttpInvalidRequestLine.class)); - } - - private boolean findTestClass(List tests, Class klass) { - return tests.stream().map(AbstractTest::getClass).anyMatch(c -> c == klass); - } -} diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuiteTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuiteTest.java index 14602923d..17ab23bd5 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuiteTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/suite/PerformanceSuiteTest.java @@ -1,6 +1,8 @@ package org.openobservatory.ooniprobe.test.suite; import org.junit.Test; +import org.openobservatory.ooniprobe.common.Application; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.Dash; @@ -17,8 +19,9 @@ import static org.mockito.Mockito.when; public class PerformanceSuiteTest { + private final Application app = mock(Application.class); - private final PerformanceSuite suite = new PerformanceSuite(); + private final AbstractSuite suite = OONITests.PERFORMANCE.toOONIDescriptor(app).getTest(app); private final PreferenceManager pm = mock(PreferenceManager.class); @Test diff --git a/app/src/test/java/org/openobservatory/ooniprobe/test/test/AbstractTestTest.java b/app/src/test/java/org/openobservatory/ooniprobe/test/test/AbstractTestTest.java index b38590b49..a57a8875d 100644 --- a/app/src/test/java/org/openobservatory/ooniprobe/test/test/AbstractTestTest.java +++ b/app/src/test/java/org/openobservatory/ooniprobe/test/test/AbstractTestTest.java @@ -9,6 +9,7 @@ import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.RobolectricAbstractTest; import org.openobservatory.ooniprobe.common.AppLogger; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; import org.openobservatory.ooniprobe.common.ReachabilityManager; import org.openobservatory.ooniprobe.engine.TestEngineInterface; @@ -25,7 +26,6 @@ import org.openobservatory.ooniprobe.model.jsonresult.TestKeys; import org.openobservatory.ooniprobe.model.settings.Settings; import org.openobservatory.ooniprobe.test.EngineProvider; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import java.io.File; import java.io.IOException; @@ -79,7 +79,7 @@ public void setUp() { public void testStartFinish() { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.createAndSave(new WebsitesSuite()); + Result result = ResultFactory.createAndSave(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c)); // Act testEngine.sendNextEvent(EventResultFactory.buildStarted()); @@ -96,7 +96,7 @@ public void testStartFinish() { public void testCreateNewMeasurement() { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); Url ulr = UrlFactory.createAndSave(); @@ -150,7 +150,7 @@ public void testCreateNewMeasurement() { public void testNetworkEvent() { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); String networkName = faker.internet.domainName(); @@ -179,7 +179,7 @@ public void testNetworkEvent() { public void testLogEvent() throws IOException { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); String message = "Ipsum"; @@ -203,7 +203,7 @@ public void testLogEvent() throws IOException { public void testProgressEvent() throws IOException { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); String message = "Ipsum"; @@ -228,7 +228,7 @@ public void testProgressEvent() throws IOException { public void testResolverFailureEvent() { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); String message = "Ipsum"; @@ -250,7 +250,7 @@ public void testResolverFailureEvent() { public void testTaskInterrupt() { // Arrange AbstractTest test = new WebConnectivity(); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); TestEngineInterface.TestOONIMKTask task = mock(TestEngineInterface.TestOONIMKTask.class); @@ -612,7 +612,7 @@ public void testExperimental() { public void testExperimentalFail() { // Arrange AbstractTest test = new Experimental(""); - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); // Act @@ -631,7 +631,7 @@ public void testExperimentalFail() { } private Result setupTestRun(AbstractTest test, boolean success) { - Result result = ResultFactory.build(new WebsitesSuite(), true, false); + Result result = ResultFactory.build(OONITests.WEBSITES.toOONIDescriptor(c).getTest(c), true, false); result.save(); JsonResult jsonResult = JsonResultFactory.build(test, success); diff --git a/engine/src/main/java/org/openobservatory/engine/BaseNettest.kt b/engine/src/main/java/org/openobservatory/engine/BaseNettest.kt index 694c5728b..06fb8e8fc 100644 --- a/engine/src/main/java/org/openobservatory/engine/BaseNettest.kt +++ b/engine/src/main/java/org/openobservatory/engine/BaseNettest.kt @@ -4,7 +4,7 @@ import java.io.Serializable open class BaseNettest( open var name: String, - open var inputs: List? + open var inputs: List? = null ) : Serializable open class BaseDescriptor( diff --git a/shared-test/src/main/java/org/openobservatory/ooniprobe/factory/ResultFactory.java b/shared-test/src/main/java/org/openobservatory/ooniprobe/factory/ResultFactory.java index 8b88bbc8f..c82c7bc72 100644 --- a/shared-test/src/main/java/org/openobservatory/ooniprobe/factory/ResultFactory.java +++ b/shared-test/src/main/java/org/openobservatory/ooniprobe/factory/ResultFactory.java @@ -2,13 +2,10 @@ import android.content.Context; +import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.model.database.Network; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; -import org.openobservatory.ooniprobe.test.suite.CircumventionSuite; -import org.openobservatory.ooniprobe.test.suite.InstantMessagingSuite; -import org.openobservatory.ooniprobe.test.suite.PerformanceSuite; -import org.openobservatory.ooniprobe.test.suite.WebsitesSuite; import org.openobservatory.ooniprobe.test.test.AbstractTest; import org.openobservatory.ooniprobe.test.test.Dash; import org.openobservatory.ooniprobe.test.test.FacebookMessenger; @@ -107,7 +104,7 @@ public static Result createAndSave( List measurementTestTypes = new ArrayList<>(); - if (suite instanceof InstantMessagingSuite) { + if (suite.getName().equals(OONITests.INSTANT_MESSAGING.getLabel())) { measurementTestTypes = Arrays.asList( new FacebookMessenger(), new Telegram(), @@ -116,7 +113,7 @@ public static Result createAndSave( ); } - if (suite instanceof CircumventionSuite) { + if (suite.getName().equals(OONITests.CIRCUMVENTION.getLabel())) { measurementTestTypes = Arrays.asList( new Psiphon(), new Tor(), @@ -124,13 +121,13 @@ public static Result createAndSave( ); } - if (suite instanceof WebsitesSuite) { + if (suite.getName().equals(OONITests.WEBSITES.getLabel())) { for (int i = 0; i < accessibleMeasurements + blockedMeasurements; i++) { measurementTestTypes.add(new WebConnectivity()); } } - if (suite instanceof PerformanceSuite) { + if (suite.getName().equals(OONITests.PERFORMANCE.getLabel())) { measurementTestTypes = Arrays.asList( new Ndt(), new Dash(),