Skip to content

Commit

Permalink
Fire on-load events for all pre-existing repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Feb 5, 2021
1 parent 5fcb88a commit fc3250f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.util.List;
import java.util.Set;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.GroupDef;
import org.javarosa.core.model.IFormElement;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
Expand Down Expand Up @@ -80,7 +82,14 @@ public void triggerActionsFromEvent(String event, Set<IFormElement> nestedElemen
triggerActionsFromEvent(event, model, null, null);

for (IFormElement element : nestedElements) {
element.getActionController().triggerActionsFromEvent(event, model, ((TreeReference) element.getBind().getReference()).getParentRef(), null);
TreeReference elementReference = (TreeReference) element.getBind().getReference();
TreeReference unqualifiedContext = element instanceof GroupDef ? elementReference : elementReference.getParentRef();
EvaluationContext context = new EvaluationContext(model.getEvaluationContext(), unqualifiedContext);
List<TreeReference> allContextRefs = context.expandReference(unqualifiedContext);

for (TreeReference contextRef : allContextRefs) {
element.getActionController().triggerActionsFromEvent(event, model, contextRef, null);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,32 @@ public void instanceLoadEvent_triggersNestedActions() throws IOException {
scenario.createNewRepeat("/data/repeat");
assertThat(scenario.answerOf("/data/repeat[1]/q1"), CoreMatchers.is(nullValue()));
}

@Test
public void instanceLoadEvent_triggeredForAllPreExistingRepeatInstances() throws IOException {
Scenario scenario = Scenario.init("Nested instance load", html(
head(
title("Nested instance load"),
model(
mainInstance(
t("data id=\"nested-instance-load\"",
t("repeat",
t("q1")),
t("repeat",
t("q1"))
)),
bind("/data/repeat/q1").type("string"))),
body(
repeat("/data/repeat",
setvalue("odk-instance-load", "/data/repeat/q1", "4*4"),
input("/data/repeat/q1"))
)
));

assertThat(scenario.answerOf("/data/repeat[0]/q1"), CoreMatchers.is(stringAnswer("16")));
assertThat(scenario.answerOf("/data/repeat[1]/q1"), CoreMatchers.is(stringAnswer("16")));

scenario.createNewRepeat("/data/repeat");
assertThat(scenario.answerOf("/data/repeat[2]/q1"), CoreMatchers.is(nullValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.javarosa.core.util.XFormsElement.input;
import static org.javarosa.core.util.XFormsElement.mainInstance;
import static org.javarosa.core.util.XFormsElement.model;
import static org.javarosa.core.util.XFormsElement.repeat;
import static org.javarosa.core.util.XFormsElement.t;
import static org.javarosa.core.util.XFormsElement.title;

Expand Down Expand Up @@ -63,6 +64,32 @@ public void recordAudioAction_callsListenerActionTriggeredWhenTriggered() throws
assertThat(listener.getAbsoluteTargetRef(), is(getRef("/data/recording")));
}

@Test
public void targetReferenceInRepeat_isContextualized() throws IOException {
CapturingXFormsActionListener listener = new CapturingXFormsActionListener();
Actions.registerActionListener(RecordAudioActionHandler.ELEMENT_NAME, listener);

Scenario.init("Record audio form", html(
head(
title("Record audio form"),
model(
mainInstance(
t("data id=\"record-audio-form\"",
t("repeat",
t("recording"),
t("q1"))
)))),
body(
repeat("/data/repeat",
t("odk:recordaudio event=\"odk-instance-load\" ref=\"/data/repeat/recording\""),
input("/data/repeat/q1"))
)
));

assertThat(listener.getActionName(), is(RecordAudioActionHandler.ELEMENT_NAME));
assertThat(listener.getAbsoluteTargetRef(), is(getRef("/data/repeat[0]/recording")));
}

@Test
public void serializationAndDeserialization_maintainsFields() throws IOException, DeserializationException {
Scenario scenario = Scenario.init("Record audio form", html(
Expand Down

0 comments on commit fc3250f

Please sign in to comment.