Skip to content

Commit

Permalink
Make database copy optional in CopyForm rule
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg authored and lognaturel committed Dec 20, 2019
1 parent dc1f29f commit 028c85a
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void beforeAll() {
Manifest.permission.WRITE_EXTERNAL_STORAGE
))
.around(new ResetStateRule())
.around(new CopyFormRule(GUIDANCE_SAMPLE_FORM));
.around(new CopyFormRule(GUIDANCE_SAMPLE_FORM, true));

@Before
public void resetPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public FillBlankFormPage clickFillBlankForm() {
}

private void goToBlankForm(String formName) {
onView(withId(R.id.enter_data)).perform(click());
clickFillBlankForm();
onData(withRowString(FormsColumns.DISPLAY_NAME, formName)).perform(click());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DeletingRepeatGroupsTest {
)
)
.around(new ResetStateRule())
.around(new CopyFormRule(TEST_FORM, null));
.around(new CopyFormRule(TEST_FORM, true));

@Test
public void requestingDeletionOfFirstRepeat_deletesFirstRepeat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ExternalCsvSearchTest {
Manifest.permission.WRITE_EXTERNAL_STORAGE)
)
.around(new ResetStateRule())
.around(new CopyFormRule(EXTERNAL_CSV_SEARCH_FORM, Collections.singletonList("external-csv-search-produce.csv")));
.around(new CopyFormRule(EXTERNAL_CSV_SEARCH_FORM, Collections.singletonList("external-csv-search-produce.csv"), true));

@Test
public void simpleSearchStatement_ShouldDisplayAllCsvChoices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ExternalDataFileNotFoundTest {
Manifest.permission.WRITE_EXTERNAL_STORAGE)
)
.around(new ResetStateRule())
.around(new CopyFormRule(EXTERNAL_DATA_QUESTIONS));
.around(new CopyFormRule(EXTERNAL_DATA_QUESTIONS, true));

@Test
public void questionsThatUseExternalFiles_ShouldDisplayFriendlyMessageWhenFilesAreMissing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class FieldListUpdateTest {
Manifest.permission.CAMERA)
)
.around(new ResetStateRule())
.around(new CopyFormRule(FIELD_LIST_TEST_FORM, Collections.singletonList("fruits.csv")));
.around(new CopyFormRule(FIELD_LIST_TEST_FORM, Collections.singletonList("fruits.csv"), true));

@Test
public void relevanceChangeAtEnd_ShouldToggleLastWidgetVisibility() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class FormNavigationButtonTest {
Manifest.permission.WRITE_EXTERNAL_STORAGE)
)
.around(new ResetStateRule())
.around(new CopyFormRule(ALL_WIDGETS_FORM));
.around(new CopyFormRule(ALL_WIDGETS_FORM, true));

@Before
public void resetAllPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class IntentGroupTest {
Manifest.permission.WRITE_EXTERNAL_STORAGE
))
.around(new ResetStateRule())
.around(new CopyFormRule(INTENT_GROUP_FORM));
.around(new CopyFormRule(INTENT_GROUP_FORM, true));

// Verifies that a value given to the label text with form buttonText is used as the button text.
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class LikertTest {
Manifest.permission.CAMERA)
)
.around(new ResetStateRule())
.around(new CopyFormRule(LIKERT_TEST_FORM, Collections.singletonList("famous.jpg")));
.around(new CopyFormRule(LIKERT_TEST_FORM, Collections.singletonList("famous.jpg"), true));

@Test
public void allText_canClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SelectMinimalTest {
Manifest.permission.WRITE_EXTERNAL_STORAGE)
)
.around(new ResetStateRule())
.around(new CopyFormRule("select_minimal.xml"));
.around(new CopyFormRule("select_minimal.xml", true));

@Test
public void longLabelsShouldBeDisplayed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class LocationAuditTest {
Manifest.permission.ACCESS_FINE_LOCATION)
)
.around(new ResetStateRule())
.around(new CopyFormRule(LOCATION_AUDIT_FORM));
.around(new CopyFormRule(LOCATION_AUDIT_FORM, true));

@Test
public void locationCollectionSnackbar_ShouldBeDisplayedAtFormLaunch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SetGeopointActionTest {
Manifest.permission.ACCESS_FINE_LOCATION)
)
.around(new ResetStateRule())
.around(new CopyFormRule(SETGEOPOINT_ACTION_FORM));
.around(new CopyFormRule(SETGEOPOINT_ACTION_FORM, true));

@Before
@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,49 @@ public class CopyFormRule implements TestRule {

private final String fileName;
private final List<String> mediaFilenames;
private final boolean copyToDatabase;

public CopyFormRule(String fileName) {
this(fileName, null);
this(fileName, null, false);
}

public CopyFormRule(String fileName, List<String> mediaFilenames) {
this(fileName, mediaFilenames, false);
}

public CopyFormRule(String fileName, boolean copyToDatabase) {
this(fileName, null, copyToDatabase);
}

public CopyFormRule(String fileName, List<String> mediaFilenames, boolean copyToDatabase) {
this.fileName = fileName;
this.mediaFilenames = mediaFilenames;
this.copyToDatabase = copyToDatabase;
}

@Override
public Statement apply(final Statement base, Description description) {
return new CopyFormStatement(fileName, mediaFilenames, base);
return new CopyFormStatement(fileName, mediaFilenames, copyToDatabase, base);
}

private class CopyFormStatement extends Statement {

private final String fileName;
private final List<String> mediaFilenames;

private final boolean copyToDatabase;
private final Statement base;

CopyFormStatement(String fileName, List<String> mediaFilenames, Statement base) {
CopyFormStatement(String fileName, List<String> mediaFilenames, boolean copyToDatabase, Statement base) {
this.fileName = fileName;
this.mediaFilenames = mediaFilenames;
this.copyToDatabase = copyToDatabase;
this.base = base;
}

@Override
public void evaluate() throws Throwable {
FormLoadingUtils.copyFormToSdCard(fileName, mediaFilenames);
FormLoadingUtils.copyFormToSdCard(fileName, mediaFilenames, copyToDatabase);
base.evaluate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,41 @@ private FormLoadingUtils() {
* Copies a form with the given file name and given associated media from the given assets
* folder to the SD Card where it will be loaded by {@link FormLoaderTask}.
*/
public static void copyFormToSdCard(String formFilename, List<String> mediaFilenames) throws IOException {
public static void copyFormToSdCard(String formFilename, List<String> mediaFilenames, boolean copyToDatabase) throws IOException {
Collect.createODKDirs();

String pathname = copyForm(formFilename);
if (mediaFilenames != null) {
copyFormMediaFiles(formFilename, mediaFilenames);
}

setupReferenceManagerForForm(ReferenceManager.instance(), FileUtils.getFormMediaDir(new File(pathname)));
saveFormToDatabase(new File(pathname));
if (copyToDatabase) {
setupReferenceManagerForForm(ReferenceManager.instance(), FileUtils.getFormMediaDir(new File(pathname)));
saveFormToDatabase(new File(pathname));
}
}

/**
* Copies a form with the given file name from the from the given assets folder to the SD Card
* where it will be loaded by {@link FormLoaderTask}.
*/
public static void copyFormToSdCard(String formFilename) throws IOException {
copyFormToSdCard(formFilename, null);
copyFormToSdCard(formFilename, null, false);
}

private static void saveFormToDatabase(File outFile) {
Map<String, String> formInfo = FileUtils.getMetadataFromFormDefinition(outFile);
final ContentValues v = new ContentValues();
v.put(FormsColumns.FORM_FILE_PATH, outFile.getAbsolutePath());
v.put(FormsColumns.FORM_MEDIA_PATH, FileUtils.constructMediaPath(outFile.getAbsolutePath()));
v.put(FormsColumns.DISPLAY_NAME, formInfo.get(FileUtils.TITLE));
v.put(FormsColumns.JR_VERSION, formInfo.get(FileUtils.VERSION));
v.put(FormsColumns.JR_FORM_ID, formInfo.get(FileUtils.FORMID));
v.put(FormsColumns.SUBMISSION_URI, formInfo.get(FileUtils.SUBMISSIONURI));
v.put(FormsColumns.BASE64_RSA_PUBLIC_KEY, formInfo.get(FileUtils.BASE64_RSA_PUBLIC_KEY));
v.put(FormsColumns.AUTO_DELETE, formInfo.get(FileUtils.AUTO_DELETE));
v.put(FormsColumns.AUTO_SEND, formInfo.get(FileUtils.AUTO_SEND));
v.put(FormsColumns.GEOMETRY_XPATH, formInfo.get(FileUtils.GEOMETRY_XPATH));
v.put(FormsColumns.FORM_FILE_PATH, outFile.getAbsolutePath());
v.put(FormsColumns.FORM_MEDIA_PATH, FileUtils.constructMediaPath(outFile.getAbsolutePath()));
v.put(FormsColumns.DISPLAY_NAME, formInfo.get(FileUtils.TITLE));
v.put(FormsColumns.JR_VERSION, formInfo.get(FileUtils.VERSION));
v.put(FormsColumns.JR_FORM_ID, formInfo.get(FileUtils.FORMID));
v.put(FormsColumns.SUBMISSION_URI, formInfo.get(FileUtils.SUBMISSIONURI));
v.put(FormsColumns.BASE64_RSA_PUBLIC_KEY, formInfo.get(FileUtils.BASE64_RSA_PUBLIC_KEY));
v.put(FormsColumns.AUTO_DELETE, formInfo.get(FileUtils.AUTO_DELETE));
v.put(FormsColumns.AUTO_SEND, formInfo.get(FileUtils.AUTO_SEND));
v.put(FormsColumns.GEOMETRY_XPATH, formInfo.get(FileUtils.GEOMETRY_XPATH));

new FormsDao().saveForm(v);
}
Expand Down

0 comments on commit 028c85a

Please sign in to comment.