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

Use relative paths to the storage #3444

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package org.odk.collect.android;

import android.os.Environment;

import org.javarosa.core.model.FormDef;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.listeners.FormLoaderListener;
import org.odk.collect.android.logic.FormController;
import org.odk.collect.android.tasks.FormLoaderTask;
import org.odk.collect.android.test.FormLoadingUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
Expand All @@ -47,8 +47,6 @@
@RunWith(Parameterized.class)
public class FormNavigationTestCase {

private static final String FORMS_DIRECTORY = "/odk/forms/";

@Parameters(name = "{0}")
public static Iterable<Object[]> data() {
// Expected indices when swiping forward until the end of the form and back once.
Expand Down Expand Up @@ -142,8 +140,6 @@ private void copyToSdCard(String formName) throws IOException {
}

private static String formPath(String formName) {
return Environment.getExternalStorageDirectory().getPath()
+ FORMS_DIRECTORY
+ formName;
return Collect.FORMS_PATH + File.separator + formName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.helpers.FormsDaoHelper;
import org.odk.collect.android.dto.Form;
import org.odk.collect.android.provider.FormsProviderAPI;
import org.odk.collect.android.utilities.ResetUtility;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void setUp() throws IOException {
@Test
public void getAllFormsCursorTest() {
Cursor cursor = formsDao.getFormsCursor();
List<Form> forms = formsDao.getFormsFromCursor(cursor);
List<Form> forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(7, forms.size());

assertEquals(biggestNOfSetForm, forms.get(0));
Expand All @@ -79,7 +80,7 @@ public void getAllFormsCursorTest() {
@Test
public void getFormsCursorForFormIdTest() {
Cursor cursor = formsDao.getFormsCursorForFormId("Birds");
List<Form> forms = formsDao.getFormsFromCursor(cursor);
List<Form> forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(2, forms.size());

assertEquals(birdsForm, forms.get(0));
Expand All @@ -88,7 +89,7 @@ public void getFormsCursorForFormIdTest() {
@Test
public void getFormsCursorTest() {
Cursor cursor = formsDao.getFormsCursor(null, null, null, null);
List<Form> forms = formsDao.getFormsFromCursor(cursor);
List<Form> forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(7, forms.size());

assertEquals(biggestNOfSetForm, forms.get(0));
Expand All @@ -102,7 +103,7 @@ public void getFormsCursorTest() {
String sortOrder = FormsProviderAPI.FormsColumns.DISPLAY_NAME + " COLLATE NOCASE DESC";

cursor = formsDao.getFormsCursor(null, null, null, sortOrder);
forms = formsDao.getFormsFromCursor(cursor);
forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(7, forms.size());

assertEquals(biggestNOfSetForm, forms.get(6));
Expand All @@ -117,7 +118,7 @@ public void getFormsCursorTest() {
String[] selectionArgs = {"Miramare"};

cursor = formsDao.getFormsCursor(null, selection, selectionArgs, null);
forms = formsDao.getFormsFromCursor(cursor);
forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(1, forms.size());

assertEquals(miramareForm, forms.get(0));
Expand All @@ -126,7 +127,7 @@ public void getFormsCursorTest() {
@Test
public void getFormsCursorForFormFilePathTest() {
Cursor cursor = formsDao.getFormsCursorForFormFilePath(Collect.FORMS_PATH + "/Miramare.xml");
List<Form> forms = formsDao.getFormsFromCursor(cursor);
List<Form> forms = FormsDaoHelper.getFormsFromCursor(cursor);
assertEquals(1, forms.size());

assertEquals(miramareForm, forms.get(0));
Expand All @@ -135,7 +136,7 @@ public void getFormsCursorForFormFilePathTest() {
@Test
public void updateInstanceTest() {
Cursor cursor = formsDao.getFormsCursorForFormFilePath(Collect.FORMS_PATH + "/Widgets.xml");
List<Form> forms = formsDao.getFormsFromCursor(cursor);
List<Form> forms = FormsDaoHelper.getFormsFromCursor(cursor);

assertEquals(1, forms.size());
assertEquals(widgetsForm, forms.get(0));
Expand All @@ -152,10 +153,10 @@ public void updateInstanceTest() {

String where = FormsProviderAPI.FormsColumns.DISPLAY_NAME + "=?";
String[] whereArgs = {"Widgets"};
assertEquals(formsDao.updateForm(formsDao.getValuesFromFormObject(widgetsForm), where, whereArgs), 1);
assertEquals(formsDao.updateForm(FormsDaoHelper.getValuesFromFormObject(widgetsForm), where, whereArgs), 1);

cursor = formsDao.getFormsCursorForFormFilePath(Collect.FORMS_PATH + "/Widgets.xml");
forms = formsDao.getFormsFromCursor(cursor);
forms = FormsDaoHelper.getFormsFromCursor(cursor);

assertEquals(1, forms.size());
assertEquals(widgetsForm, forms.get(0));
Expand All @@ -179,7 +180,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/ccce6015dd1b8f935f5f3058e81eeb43.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(biggestNOfSetForm));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(biggestNOfSetForm));

assertTrue(new File(Collect.FORMS_PATH + "/Birds.xml").createNewFile());
birdsForm = new Form.Builder()
Expand All @@ -193,7 +194,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/4cd980d50f884362afba842cbff3a798.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(birdsForm));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(birdsForm));

assertTrue(new File(Collect.FORMS_PATH + "/Miramare.xml").createNewFile());
miramareForm = new Form.Builder()
Expand All @@ -206,7 +207,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/e733627cdbf220929bf9c4899cb983ea.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(miramareForm));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(miramareForm));

assertTrue(new File(Collect.FORMS_PATH + "/Geo Tagger v2.xml").createNewFile());
geoTaggerV2Form = new Form.Builder()
Expand All @@ -219,7 +220,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/1d5e9109298c8ef02bc523b17d7c0451.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(geoTaggerV2Form));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(geoTaggerV2Form));

assertTrue(new File(Collect.FORMS_PATH + "/Widgets.xml").createNewFile());
widgetsForm = new Form.Builder()
Expand All @@ -232,7 +233,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/0eacc6333449e66826326eb5fcc75749.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(widgetsForm));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(widgetsForm));

assertTrue(new File(Collect.FORMS_PATH + "/sample.xml").createNewFile());
sampleForm = new Form.Builder()
Expand All @@ -245,7 +246,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/4f495fddd1f2544f65444ea83d25f425.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(sampleForm));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(sampleForm));

assertTrue(new File(Collect.FORMS_PATH + "/Birds_4.xml").createNewFile());
birds2Form = new Form.Builder()
Expand All @@ -259,7 +260,7 @@ private void setUpSampleForms() throws IOException {
.jrCacheFilePath(Collect.ODK_ROOT + "/.cache/4cd980d50f884362afba842cbff3a775.formdef")
.build();

formsDao.saveForm(formsDao.getValuesFromFormObject(birds2Form));
formsDao.saveForm(FormsDaoHelper.getValuesFromFormObject(birds2Form));
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.helpers.InstancesDaoHelper;
import org.odk.collect.android.dto.Instance;
import org.odk.collect.android.provider.InstanceProviderAPI;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

@RunWith(AndroidJUnit4.class)
/**
Expand Down Expand Up @@ -66,7 +69,7 @@ public void setUp() {
@Test
public void getUnsentInstancesCursorTest() {
Cursor cursor = instancesDao.getUnsentInstancesCursor();
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(4, instances.size());
assertEquals(cascadingSelectInstance, instances.get(0));
Expand All @@ -78,7 +81,7 @@ public void getUnsentInstancesCursorTest() {
@Test
public void getSentInstancesCursorTest() {
Cursor cursor = instancesDao.getSentInstancesCursor();
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(2, instances.size());
assertEquals(biggestNOfSetInstance, instances.get(0));
Expand All @@ -88,7 +91,7 @@ public void getSentInstancesCursorTest() {
@Test
public void getSavedInstancesCursorTest() {
Cursor cursor = instancesDao.getSavedInstancesCursor(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME + " ASC");
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(5, instances.size());
assertEquals(biggestNOfSetInstance, instances.get(0));
Expand All @@ -101,7 +104,7 @@ public void getSavedInstancesCursorTest() {
@Test
public void getFinalizedInstancesCursorTest() {
Cursor cursor = instancesDao.getFinalizedInstancesCursor();
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(1, instances.size());
assertEquals(biggestNOfSet2Instance, instances.get(0));
Expand All @@ -110,7 +113,7 @@ public void getFinalizedInstancesCursorTest() {
@Test
public void getInstancesCursorForFilePathTest() {
Cursor cursor = instancesDao.getInstancesCursorForFilePath(Collect.INSTANCES_PATH + "/Hypertension Screening_2017-02-20_14-03-53/Hypertension Screening_2017-02-20_14-03-53.xml");
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(1, instances.size());
assertEquals(hypertensionScreeningInstance, instances.get(0));
Expand All @@ -119,7 +122,7 @@ public void getInstancesCursorForFilePathTest() {
@Test
public void getAllCompletedUndeletedInstancesCursorTest() {
Cursor cursor = instancesDao.getAllCompletedUndeletedInstancesCursor();
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(2, instances.size());
assertEquals(biggestNOfSetInstance, instances.get(0));
Expand All @@ -129,7 +132,7 @@ public void getAllCompletedUndeletedInstancesCursorTest() {
@Test
public void getInstancesCursorForIdTest() {
Cursor cursor = instancesDao.getInstancesCursorForId("2");
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(1, instances.size());
assertEquals(cascadingSelectInstance, instances.get(0));
Expand All @@ -138,7 +141,7 @@ public void getInstancesCursorForIdTest() {
@Test
public void updateInstanceTest() {
Cursor cursor = instancesDao.getInstancesCursorForFilePath(Collect.INSTANCES_PATH + "/Biggest N of Set_2017-02-20_14-24-46/Biggest N of Set_2017-02-20_14-24-46.xml");
List<Instance> instances = instancesDao.getInstancesFromCursor(cursor);
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(1, instances.size());
assertEquals(biggestNOfSet2Instance, instances.get(0));
Expand All @@ -154,16 +157,35 @@ public void updateInstanceTest() {
String where = InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH + "=?";
String[] whereArgs = {Collect.INSTANCES_PATH + "/Biggest N of Set_2017-02-20_14-24-46/Biggest N of Set_2017-02-20_14-24-46.xml"};

assertEquals(instancesDao.updateInstance(instancesDao.getValuesFromInstanceObject(biggestNOfSet2Instance), where, whereArgs), 1);
assertEquals(instancesDao.updateInstance(InstancesDaoHelper.getValuesFromInstanceObject(biggestNOfSet2Instance), where, whereArgs), 1);

cursor = instancesDao.getInstancesCursorForFilePath(Collect.INSTANCES_PATH + "/Biggest N of Set_2017-02-20_14-24-46/Biggest N of Set_2017-02-20_14-24-46.xml");

instances = instancesDao.getInstancesFromCursor(cursor);
instances = InstancesDaoHelper.getInstancesFromCursor(cursor);

assertEquals(1, instances.size());
assertEquals(biggestNOfSet2Instance, instances.get(0));
}

@Test
public void deleteInstancesTest() {
List<Instance> instances = InstancesDaoHelper.getInstancesFromCursor(instancesDao.getInstancesCursor());
assertEquals(6, instances.size());

List<String> absoluteInstanceFilePaths = new ArrayList<>();
absoluteInstanceFilePaths.add(Collect.INSTANCES_PATH + "/Hypertension Screening_2017-02-20_14-03-53/Hypertension Screening_2017-02-20_14-03-53.xml");
absoluteInstanceFilePaths.add("/instances/Biggest N of Set_2017-02-20_14-06-51/Biggest N of Set_2017-02-20_14-06-51.xml");
instancesDao.deleteInstances(absoluteInstanceFilePaths);

instances = InstancesDaoHelper.getInstancesFromCursor(instancesDao.getInstancesCursor());
assertEquals(4, instances.size());

for (Instance instance : instances) {
assertFalse(instance.getInstanceFilePath().endsWith("Hypertension Screening_2017-02-20_14-03-53.xml"));
assertFalse(instance.getInstanceFilePath().endsWith("Biggest N of Set_2017-02-20_14-06-51.xml"));
}
}

private void fillDatabase() {
hypertensionScreeningInstance = new Instance.Builder()
.displayName("Hypertension Screening")
Expand All @@ -172,7 +194,7 @@ private void fillDatabase() {
.status(InstanceProviderAPI.STATUS_INCOMPLETE)
.lastStatusChangeDate(1487595836793L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(hypertensionScreeningInstance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(hypertensionScreeningInstance));

cascadingSelectInstance = new Instance.Builder()
.displayName("Cascading Select Form")
Expand All @@ -181,7 +203,7 @@ private void fillDatabase() {
.status(InstanceProviderAPI.STATUS_INCOMPLETE)
.lastStatusChangeDate(1487596015000L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(cascadingSelectInstance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(cascadingSelectInstance));

biggestNOfSetInstance = new Instance.Builder()
.displayName("Biggest N of Set")
Expand All @@ -190,7 +212,7 @@ private void fillDatabase() {
.status(InstanceProviderAPI.STATUS_SUBMITTED)
.lastStatusChangeDate(1487596015100L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(biggestNOfSetInstance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(biggestNOfSetInstance));

widgetsInstance = new Instance.Builder()
.displayName("Widgets")
Expand All @@ -200,7 +222,7 @@ private void fillDatabase() {
.lastStatusChangeDate(1487596020803L)
.deletedDate(1487596020803L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(widgetsInstance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(widgetsInstance));

sampleInstance = new Instance.Builder()
.displayName("sample")
Expand All @@ -209,7 +231,7 @@ private void fillDatabase() {
.status(InstanceProviderAPI.STATUS_INCOMPLETE)
.lastStatusChangeDate(1487596026373L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(sampleInstance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(sampleInstance));

biggestNOfSet2Instance = new Instance.Builder()
.displayName("Biggest N of Set")
Expand All @@ -218,7 +240,7 @@ private void fillDatabase() {
.status(InstanceProviderAPI.STATUS_COMPLETE)
.lastStatusChangeDate(1487597090653L)
.build();
instancesDao.saveInstance(instancesDao.getValuesFromInstanceObject(biggestNOfSet2Instance));
instancesDao.saveInstance(InstancesDaoHelper.getValuesFromInstanceObject(biggestNOfSet2Instance));
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.odk.collect.android.dao.FormsDao;
import org.odk.collect.android.dao.helpers.FormsDaoHelper;
import org.odk.collect.android.dto.Form;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.SQLiteUtils;
Expand Down Expand Up @@ -80,8 +81,7 @@ public void testMigration() throws IOException {
}

private void assertThatFormsAreKeptAfterUpgrading() {
FormsDao formsDao = new FormsDao();
List<Form> forms = formsDao.getFormsFromCursor(formsDao.getFormsCursor());
List<Form> forms = FormsDaoHelper.getFormsFromCursor(new FormsDao().getFormsCursor());
assertEquals(1, forms.size());
assertEquals("2019051302", forms.get(0).getJrVersion());
assertEquals("92ba8106dcb779943c1de163d73e1069", forms.get(0).getMD5Hash());
Expand Down
Loading