From 37c9604d92b5e5948bcfbd417e6cde3ed396dd60 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 14 Aug 2024 14:40:26 +0100 Subject: [PATCH 1/4] Add test for instance upload action --- .../external/InstanceUploadActionTest.kt | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt index 1eebf01a831..11ea68091cb 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt @@ -1,40 +1,68 @@ package org.odk.collect.android.feature.external +import android.content.Context import android.content.Intent +import android.provider.BaseColumns._ID +import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo import org.junit.Rule import org.junit.Test import org.junit.rules.RuleChain import org.junit.runner.RunWith import org.odk.collect.android.external.InstancesContract -import org.odk.collect.android.instancemanagement.send.InstanceUploaderActivity +import org.odk.collect.android.support.TestDependencies +import org.odk.collect.android.support.pages.FormEntryPage import org.odk.collect.android.support.pages.OkDialog import org.odk.collect.android.support.rules.CollectTestRule import org.odk.collect.android.support.rules.TestRuleChain +import org.odk.collect.android.utilities.ApplicationConstants @RunWith(AndroidJUnit4::class) class InstanceUploadActionTest { - val collectTestRule = CollectTestRule() + private val rule = CollectTestRule(useDemoProject = false) + private val context = ApplicationProvider.getApplicationContext() + private val testDependencies = TestDependencies() @get:Rule - val rule: RuleChain = TestRuleChain.chain() - .around(collectTestRule) + val chain: RuleChain = TestRuleChain.chain(testDependencies) + .around(rule) @Test - fun whenInstanceDoesNotExist_showsError() { - val instanceIds = longArrayOf(11) - instanceUploadAction(instanceIds) + fun canUploadInstanceToDifferentServer() { + rule.startAtFirstLaunch().clickTryCollect() + .copyForm("one-question.xml") + .startBlankForm("One Question") + .fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("what is your age", "34")) - OkDialog() - .assertOnPage() - .assertText(org.odk.collect.strings.R.string.no_forms_uploaded) + val instanceId = + context.contentResolver.query(InstancesContract.getUri("DEMO"), null, null, null, null) + .use { + it!!.moveToFirst() + it.getLong(it.getColumnIndex(_ID)) + } + + val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD") + intent.type = InstancesContract.CONTENT_TYPE + intent.putExtra(ApplicationConstants.BundleKeys.URL, testDependencies.server.url) + intent.putExtra("instances", longArrayOf(instanceId)) + + rule.launch(intent, OkDialog()) + .assertTextInDialog("One Question - Success") + assertThat(testDependencies.server.submissions.size, equalTo(1)) } - private fun instanceUploadAction(instanceIds: LongArray) { + @Test + fun whenInstanceDoesNotExist_showsError() { + rule.startAtFirstLaunch().clickTryCollect() + val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD") intent.type = InstancesContract.CONTENT_TYPE - intent.putExtra("instances", instanceIds) - collectTestRule.launch(intent) + intent.putExtra("instances", longArrayOf(11)) + + rule.launch(intent, OkDialog()) + .assertText(org.odk.collect.strings.R.string.no_forms_uploaded) } } From 7b6984fac3b6cb2593244ccef4898ac1d7539ca8 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 14 Aug 2024 14:43:46 +0100 Subject: [PATCH 2/4] Add analytics for instance upload action --- .../android/analytics/AnalyticsEvents.kt | 5 ++++ .../android/tasks/InstanceUploaderTask.java | 27 +++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/analytics/AnalyticsEvents.kt b/collect_app/src/main/java/org/odk/collect/android/analytics/AnalyticsEvents.kt index 45f296d7bd6..b9a5030ad37 100644 --- a/collect_app/src/main/java/org/odk/collect/android/analytics/AnalyticsEvents.kt +++ b/collect_app/src/main/java/org/odk/collect/android/analytics/AnalyticsEvents.kt @@ -99,4 +99,9 @@ object AnalyticsEvents { const val DELETE_SAVED_FORM_FEW = "DeleteSavedFormFew" // < 10 const val DELETE_SAVED_FORM_TENS = "DeleteSavedFormTens" // >= 10 const val DELETE_SAVED_FORM_HUNDREDS = "DeleteSavedFormHundreds" // >= 100 + + /** + * Tracks how often the INSTANCE_UPLOAD action is used with a custom server URL + */ + const val INSTANCE_UPLOAD_CUSTOM_SERVER = "InstanceUploadCustomServer" } diff --git a/collect_app/src/main/java/org/odk/collect/android/tasks/InstanceUploaderTask.java b/collect_app/src/main/java/org/odk/collect/android/tasks/InstanceUploaderTask.java index 9f80bec7d8b..f33aaad70a5 100644 --- a/collect_app/src/main/java/org/odk/collect/android/tasks/InstanceUploaderTask.java +++ b/collect_app/src/main/java/org/odk/collect/android/tasks/InstanceUploaderTask.java @@ -14,21 +14,28 @@ package org.odk.collect.android.tasks; +import static org.odk.collect.android.analytics.AnalyticsEvents.SUBMISSION; +import static org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString; + +import android.net.Uri; +import android.os.AsyncTask; + import org.odk.collect.analytics.Analytics; +import org.odk.collect.android.analytics.AnalyticsEvents; import org.odk.collect.android.application.Collect; import org.odk.collect.android.instancemanagement.InstanceDeleter; import org.odk.collect.android.instancemanagement.InstancesDataService; import org.odk.collect.android.listeners.InstanceUploaderListener; +import org.odk.collect.android.openrosa.OpenRosaHttpInterface; import org.odk.collect.android.projects.ProjectsDataService; +import org.odk.collect.android.upload.FormUploadAuthRequestedException; +import org.odk.collect.android.upload.FormUploadException; +import org.odk.collect.android.upload.InstanceServerUploader; import org.odk.collect.android.utilities.InstanceAutoDeleteChecker; import org.odk.collect.android.utilities.InstancesRepositoryProvider; +import org.odk.collect.android.utilities.WebCredentialsUtils; import org.odk.collect.forms.FormsRepository; import org.odk.collect.forms.instances.Instance; -import org.odk.collect.android.openrosa.OpenRosaHttpInterface; -import org.odk.collect.android.upload.InstanceServerUploader; -import org.odk.collect.android.upload.FormUploadAuthRequestedException; -import org.odk.collect.android.upload.FormUploadException; -import org.odk.collect.android.utilities.WebCredentialsUtils; import org.odk.collect.forms.instances.InstancesRepository; import org.odk.collect.metadata.PropertyManager; import org.odk.collect.settings.SettingsProvider; @@ -41,12 +48,6 @@ import javax.inject.Inject; -import static org.odk.collect.android.analytics.AnalyticsEvents.SUBMISSION; -import static org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString; - -import android.net.Uri; -import android.os.AsyncTask; - /** * Background task for uploading completed forms. * @@ -100,6 +101,10 @@ public Outcome doInBackground(Long... instanceIdsToUpload) { publishProgress(i + 1, instancesToUpload.size()); + if (completeDestinationUrl != null) { + Analytics.log(AnalyticsEvents.INSTANCE_UPLOAD_CUSTOM_SERVER); + } + try { String destinationUrl = uploader.getUrlToSubmitTo(instance, deviceId, completeDestinationUrl, null); String customMessage = uploader.uploadOneSubmission(instance, destinationUrl); From a7f9312ea6b3bde1a777e2555fc2687814ebb63b Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 14 Aug 2024 16:37:26 +0100 Subject: [PATCH 3/4] Make test name more specific --- .../android/feature/external/InstanceUploadActionTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt index 11ea68091cb..4dd11ab419b 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt @@ -31,7 +31,7 @@ class InstanceUploadActionTest { .around(rule) @Test - fun canUploadInstanceToDifferentServer() { + fun whenIntentIncludesURLExtra_instancesAreUploadedToThatURL() { rule.startAtFirstLaunch().clickTryCollect() .copyForm("one-question.xml") .startBlankForm("One Question") From ba5ed931aaff18d1ba68c4e7e9943552f495a454 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Thu, 15 Aug 2024 08:25:15 +0100 Subject: [PATCH 4/4] Remove unneeded extra setup --- .../android/feature/external/InstanceUploadActionTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt index 4dd11ab419b..6e729b6ead6 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt @@ -22,7 +22,7 @@ import org.odk.collect.android.utilities.ApplicationConstants @RunWith(AndroidJUnit4::class) class InstanceUploadActionTest { - private val rule = CollectTestRule(useDemoProject = false) + private val rule = CollectTestRule() private val context = ApplicationProvider.getApplicationContext() private val testDependencies = TestDependencies() @@ -32,7 +32,7 @@ class InstanceUploadActionTest { @Test fun whenIntentIncludesURLExtra_instancesAreUploadedToThatURL() { - rule.startAtFirstLaunch().clickTryCollect() + rule.startAtMainMenu() .copyForm("one-question.xml") .startBlankForm("One Question") .fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("what is your age", "34")) @@ -56,7 +56,7 @@ class InstanceUploadActionTest { @Test fun whenInstanceDoesNotExist_showsError() { - rule.startAtFirstLaunch().clickTryCollect() + rule.startAtMainMenu() val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD") intent.type = InstancesContract.CONTENT_TYPE