use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.
the class GeoShapeAreaTest method testGeoShapeSupportForEnclosedArea.
public void testGeoShapeSupportForEnclosedArea() throws Exception {
// Read the form definition
String FORM_NAME = (new File(PathConst.getTestResourcePath(), "area.xml")).getAbsolutePath();
InputStream is = null;
FormDef formDef = null;
is = new FileInputStream(new File(FORM_NAME));
formDef = XFormUtils.getFormFromInputStream(is);
// trigger all calculations
formDef.initialize(true, new InstanceInitializationFactory());
// get the calculated area
IAnswerData areaResult = formDef.getMainInstance().getRoot().getChildAt(1).getValue();
assertTrue((int) Math.rint((Double) areaResult.getValue()) == 151452);
}
use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.
the class XFormParserTest method parseFormWithSetValueAction.
@Test
public void parseFormWithSetValueAction() throws IOException {
// Given & When
ParseResult parseResult = parse(r("form-with-setvalue-action.xml"));
FormDef formDef = parseResult.formDef;
// dispatch 'xforms-ready' action (Action.EVENT_XFORMS_READY)
formDef.initialize(true, new InstanceInitializationFactory());
// Then
assertEquals(formDef.getTitle(), "SetValue action");
assertNoParseErrors(parseResult);
assertEquals(1, formDef.getEventListeners(Action.EVENT_XFORMS_READY).size());
TreeElement textNode = formDef.getMainInstance().getRoot().getChildrenWithName("text").get(0);
assertEquals("Test Value", textNode.getValue().getValue());
}
use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber.
/**
* Indirectly means that the calculation - `concat(/rgwp/houseM/name)` - does not take the
* `/rgwp/houseM` nodeset (the repeat group) as an argument
* but since it takes one of its children (`name` children),
* the calculation must re-evaluated once after a repeat group deletion because one of the children
* has been deleted along with its parent (the repeat group instance).
*/
@Test
public void deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber() throws Exception {
// Given
final FormDef formDef = parse(r("calculation-indirectly-dependent-on-the-repeat-groups-number.xml")).formDef;
assertIDagImplUnderTest(formDef);
// trigger all calculations
formDef.initialize(false, new InstanceInitializationFactory());
// it's important to set the test event notifier now to avoid storing events from the above initialization
formDef.setEventNotifier(eventNotifier);
final FormInstance mainInstance = formDef.getMainInstance();
final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
// Index pointing to the second repeat group
final FormIndex indexToBeDeleted = new FormIndex(0, 2, elementToBeDeletedRef);
// When
TreeElement summaryNode = mainInstance.getRoot().getChildrenWithName("summary").get(0);
assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABCDE"));
// Safe2014DagImplTest.deleteRepeatGroup is called by the below method
formDef.deleteRepeat(indexToBeDeleted);
// Then
final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
assertThat(repeats.size(), equalTo(4));
assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("A"));
assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("B"));
assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("D"));
assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("E"));
assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABDE"));
// check that correct calculations were triggered
final String[] expectedMessages = { "Processing 'Deleted: houseM [3]: 0 triggerables were fired.' for ", "Processing 'Recalculate' for summary [1] (ABDE)", "Processing 'Deleted: name [3_1]: 1 triggerables were fired.' for ", "Processing 'Deleted: houseM [4]: 0 triggerables were fired.' for " };
assertThat(dagEvents.size(), equalTo(expectedMessages.length));
int messageIndex = 0;
for (String expectedMessage : expectedMessages) {
assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
}
}
use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition.
@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition() throws Exception {
// Given
final FormDef formDef = parse(r("repeat-group-with-children-calculations-dependent-on-the-parent.xml")).formDef;
assertIDagImplUnderTest(formDef);
// trigger all calculations
formDef.initialize(false, new InstanceInitializationFactory());
// it's important to set the test event notifier now to avoid storing events from the above initialization
formDef.setEventNotifier(eventNotifier);
final FormInstance mainInstance = formDef.getMainInstance();
final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
// Index pointing to the second repeat group
final FormIndex indexToBeDeleted = new FormIndex(0, 1, elementToBeDeletedRef);
// When
// Safe2014DagImplTest.deleteRepeatGroup is called by the below method
formDef.deleteRepeat(indexToBeDeleted);
// Then
final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
// check the values based on the position of the parents
assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("1"));
assertThat(repeats.get(0).getChildAt(2).getValue().getDisplayText(), equalTo("A1"));
assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
assertThat(repeats.get(1).getChildAt(2).getValue().getDisplayText(), equalTo("C2"));
assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
assertThat(repeats.get(2).getChildAt(2).getValue().getDisplayText(), equalTo("D3"));
assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
assertThat(repeats.get(3).getChildAt(2).getValue().getDisplayText(), equalTo("E4"));
// check that correct calculations were triggered
final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Recalculate' for name_and_no [2_1] (C2)", "Processing 'Deleted: houseM [2]: 2 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name_and_no [2_1]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Recalculate' for name_and_no [3_1] (D3)", "Processing 'Deleted: houseM [3]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Recalculate' for name_and_no [4_1] (E4)", "Processing 'Deleted: houseM [4]: 2 triggerables were fired.' for " };
assertThat(dagEvents.size(), equalTo(expectedMessages.length));
int messageIndex = 0;
for (String expectedMessage : expectedMessages) {
assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
}
}
use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings.
@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings() throws Exception {
// Given
final FormDef formDef = parse(r("repeat-group-with-children-position-calculation.xml")).formDef;
assertIDagImplUnderTest(formDef);
// trigger all calculations
formDef.initialize(false, new InstanceInitializationFactory());
// it's important to set the test event notifier now to avoid storing events from the above initialization
formDef.setEventNotifier(eventNotifier);
final FormInstance mainInstance = formDef.getMainInstance();
final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
// Index pointing to the second repeat group
final FormIndex indexToBeDeleted = new FormIndex(0, 1, elementToBeDeletedRef);
// When
// Safe2014DagImplTest.deleteRepeatGroup is called by the below method
formDef.deleteRepeat(indexToBeDeleted);
// Then
final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
// check the values based on the position of the parents
assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("1"));
assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
// check that correct calculations were triggered
final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Deleted: houseM [2]: 1 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Deleted: houseM [3]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Deleted: houseM [4]: 1 triggerables were fired.' for " };
assertThat(dagEvents.size(), equalTo(expectedMessages.length));
int messageIndex = 0;
for (String expectedMessage : expectedMessages) {
assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
}
}
Aggregations