Search in sources :

Example 26 with FormInstance

use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.

the class QuestionDataGroupTests method testAcceptsVisitor.

public void testAcceptsVisitor() {
    final MutableBoolean visitorAccepted = new MutableBoolean(false);
    final MutableBoolean dispatchedWrong = new MutableBoolean(false);
    ITreeVisitor sampleVisitor = new ITreeVisitor() {

        public void visit(FormInstance tree) {
            dispatchedWrong.setValue(true);
        }

        public void visit(AbstractTreeElement element) {
            visitorAccepted.setValue(true);
        }
    };
    stringElement.accept(sampleVisitor);
    assertTrue("The visitor's visit method was not called correctly by the QuestionDataElement", visitorAccepted.getValue());
    assertTrue("The visitor was dispatched incorrectly by the QuestionDataElement", !dispatchedWrong.getValue());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) FormInstance(org.javarosa.core.model.instance.FormInstance) ITreeVisitor(org.javarosa.core.model.instance.utils.ITreeVisitor)

Example 27 with FormInstance

use of org.javarosa.core.model.instance.FormInstance 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));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 28 with FormInstance

use of org.javarosa.core.model.instance.FormInstance 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));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 29 with FormInstance

use of org.javarosa.core.model.instance.FormInstance 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));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 30 with FormInstance

use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.

the class QuestionDataElementTests method testAcceptsVisitor.

public void testAcceptsVisitor() {
    final MutableBoolean visitorAccepted = new MutableBoolean(false);
    final MutableBoolean dispatchedWrong = new MutableBoolean(false);
    ITreeVisitor sampleVisitor = new ITreeVisitor() {

        public void visit(FormInstance tree) {
            dispatchedWrong.bool = true;
        }

        public void visit(AbstractTreeElement element) {
            visitorAccepted.bool = true;
        }
    };
    stringElement.accept(sampleVisitor);
    assertTrue("The visitor's visit method was not called correctly by the QuestionDataElement", visitorAccepted.getValue());
    assertTrue("The visitor was dispatched incorrectly by the QuestionDataElement", !dispatchedWrong.getValue());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) FormInstance(org.javarosa.core.model.instance.FormInstance) ITreeVisitor(org.javarosa.core.model.instance.utils.ITreeVisitor)

Aggregations

FormInstance (org.javarosa.core.model.instance.FormInstance)32 TreeElement (org.javarosa.core.model.instance.TreeElement)16 TreeReference (org.javarosa.core.model.instance.TreeReference)8 Test (org.junit.Test)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)6 FormDef (org.javarosa.core.model.FormDef)5 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)5 IOException (java.io.IOException)4 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)3 StringData (org.javarosa.core.model.data.StringData)3 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)3 XFormSerializingVisitor (org.javarosa.model.xform.XFormSerializingVisitor)3 XPathNodeset (org.javarosa.xpath.XPathNodeset)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 IFormElement (org.javarosa.core.model.IFormElement)2 ITreeVisitor (org.javarosa.core.model.instance.utils.ITreeVisitor)2 XPathTypeMismatchException (org.javarosa.xpath.XPathTypeMismatchException)2 XPathUnhandledException (org.javarosa.xpath.XPathUnhandledException)2 Cursor (android.database.Cursor)1