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

Save current screen answers when quitting form #3899

Merged
merged 3 commits into from
Jun 3, 2020
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
24 changes: 24 additions & 0 deletions collect_app/src/androidTest/assets/forms/two-question.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>Two Question</h:title>
<model>
<instance>
<data id="two_question">
<name/>
<age/>
</data>
</instance>
<bind nodeset="name" type="string"/>
<bind nodeset="age" type="int"/>
</model>
</h:head>
<h:body>
<input ref="/data/name">
<label>What is your name?</label>
</input>
<input ref="/data/age">
<label>What is your age?</label>
</input>
</h:body>
</h:html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.formentry;
package org.odk.collect.android.feature.formentry;

import android.Manifest;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.odk.collect.android.feature.formentry;

import android.Manifest;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.GrantPermissionRule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.odk.collect.android.support.CollectTestRule;
import org.odk.collect.android.support.CopyFormRule;
import org.odk.collect.android.support.ResetStateRule;
import org.odk.collect.android.support.pages.MainMenuPage;
import org.odk.collect.android.support.pages.SaveOrIgnoreDialog;

@RunWith(AndroidJUnit4.class)
public class QuittingFormTest {

public CollectTestRule rule = new CollectTestRule();

@Rule
public RuleChain copyFormChain = RuleChain
.outerRule(GrantPermissionRule.grant(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
))
.around(new ResetStateRule())
.around(new CopyFormRule("two-question.xml"))
.around(rule);

@Test
public void partiallyFillingForm_andPressingBack_andClickingSaveChanges_savesCurrentAnswers() {
rule.mainMenu()
.startBlankForm("Two Question")
.answerQuestion("What is your name?", "Reuben")
.swipeToNextQuestion()
.answerQuestion("What is your age?", "10")
.pressBack(new SaveOrIgnoreDialog<>("Two Question", new MainMenuPage(rule), rule))
.clickSaveChanges()
.clickEditSavedForm()
.clickOnForm("Two Question")
.assertText("Reuben") // Previous answers are saved
.assertText("10"); // Current screen answers are saved
}

@Test
public void partiallyFillingForm_andPressingBack_andClickingIgnoreChanges_doesNotSaveForm() {
rule.mainMenu()
.startBlankForm("Two Question")
.answerQuestion("What is your name?", "Reuben")
.pressBack(new SaveOrIgnoreDialog<>("Two Question", new MainMenuPage(rule), rule))
.clickIgnoreChanges()
.assertNumberOfEditableForms(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,11 @@ public AddNewRepeatDialog swipeToNextQuestionWithRepeatGroup(String repeatName)

return new AddNewRepeatDialog(repeatName, rule);
}

public FormEntryPage answerQuestion(String question, String answer) {
assertText(question);
inputText(answer);
closeSoftKeyboard();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public MainMenuPage assertNumberOfFinalizedForms(int number) {
return this;
}

public MainMenuPage assertNumberOfEditableForms(int number) {
if (number == 0) {
onView(withText(getTranslatedString(R.string.review_data))).check(matches(isDisplayed()));
} else {
onView(withText(getTranslatedString(R.string.review_data, String.valueOf(number)))).check(matches(isDisplayed()));
}

return this;
}

public MainMenuPage assertStorageMigrationBannerIsDisplayed() {
onView(withText(R.string.scoped_storage_banner_text)).check(matches(isDisplayed()));
onView(withText(R.string.scoped_storage_learn_more)).check(matches(isDisplayed()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public class FormEntryActivity extends CollectAbstractActivity implements Animat
SaveFormIndexTask.SaveFormIndexListener, WidgetValueChangedListener,
ScreenContext, FormLoadingDialogFragment.FormLoadingDialogFragmentListener,
AudioControllerView.SwipableParent,
FormIndexAnimationHandler.Listener {
FormIndexAnimationHandler.Listener,
QuitFormDialogFragment.Listener {

// Defines for FormEntryActivity
private static final boolean EXIT = true;
Expand Down Expand Up @@ -1026,7 +1027,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

case R.id.menu_save:
// don't exit
saveForm(DO_NOT_EXIT, InstancesDaoHelper.isInstanceComplete(false), null);
saveForm(DO_NOT_EXIT, InstancesDaoHelper.isInstanceComplete(false), null, true);
return true;

case R.id.menu_goto:
Expand Down Expand Up @@ -1398,7 +1399,7 @@ public void onClick(View v) {
} else {
saveForm(EXIT, instanceComplete
.isChecked(), saveAs.getText()
.toString());
.toString(), true);
}
}
});
Expand Down Expand Up @@ -1807,12 +1808,6 @@ private void createDeleteRepeatConfirmDialog() {
* isntancs as complete. If updatedSaveName is non-null, the instances
* content provider is updated with the new name
*/
// by default, save the current screen
private boolean saveForm(boolean exit, boolean complete, String updatedSaveName) {
return saveForm(exit, complete, updatedSaveName, true);
}

// but if you want save in the background, can't be current screen
private boolean saveForm(boolean exit, boolean complete, String updatedSaveName,
boolean current) {
// save current answer
Expand Down Expand Up @@ -1916,6 +1911,11 @@ private void createQuitDialog() {
showIfNotShowing(QuitFormDialogFragment.class, getSupportFragmentManager());
}

@Override
public void onSaveChangesClicked() {
saveForm(EXIT, InstancesDaoHelper.isInstanceComplete(false), null, true);
}

@Nullable
private String getAbsoluteInstancePath() {
FormController formController = getFormController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class QuitFormDialogFragment extends DialogFragment {
Analytics analytics;

private FormSaveViewModel viewModel;
private Listener listener;

@Override
public void onAttach(@NonNull Context context) {
Expand All @@ -50,6 +51,10 @@ public void onAttach(@NonNull Context context) {

viewModel = new ViewModelProvider(requireActivity(), new FormSaveViewModel.Factory(analytics))
.get(FormSaveViewModel.class);

if (context instanceof Listener) {
listener = (Listener) context;
}
}

@NonNull
Expand All @@ -76,9 +81,9 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
IconMenuItem item = (IconMenuItem) adapter.getItem(position);

if (item.getTextResId() == R.string.keep_changes) {
viewModel.saveForm(getActivity().getIntent().getData(), InstancesDaoHelper.isInstanceComplete(false),
null, true);

if (listener != null) {
listener.onSaveChangesClicked();
}
} else {
ExternalDataManager manager = Collect.getInstance().getExternalDataManager();
if (manager != null) {
Expand Down Expand Up @@ -117,4 +122,8 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
.setView(listView)
.create();
}

public interface Listener {
void onSaveChangesClicked();
}
}