Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add analytics for INSTANCE_UPLOAD #6346

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,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()
private val context = ApplicationProvider.getApplicationContext<Context>()
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 whenIntentIncludesURLExtra_instancesAreUploadedToThatURL() {
rule.startAtMainMenu()
.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.startAtMainMenu()

val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD")
intent.type = InstancesContract.CONTENT_TYPE
intent.putExtra("instances", instanceIds)
collectTestRule.launch<InstanceUploaderActivity>(intent)
intent.putExtra("instances", longArrayOf(11))

rule.launch(intent, OkDialog())
.assertText(org.odk.collect.strings.R.string.no_forms_uploaded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down