public void testRandomInterpretationWithFloatParameters() {

    String newXFormula = "random(9.9,1)";

    solo.clickOnView(solo.getView(X_POS_EDIT_TEXT_RID));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_9));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_decimal_mark));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_9));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(300);

    Formula formula = (Formula) Reflection.getPrivateField(placeAtBrick, "xPosition");
    float value = formula.interpretFloat(sprite);

    Log.i("info", "value: " + value);

    assertTrue(
        "random() interpretation of float parameter is wrong: " + newXFormula + " value=" + value,
        1 <= value && value <= 9.9f && (Math.abs(value) - (int) Math.abs(value)) > 0);

    String newYFormula = "random(7.0,1)";

    solo.clickOnView(solo.getView(Y_POS_EDIT_TEXT_RID));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_7));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_decimal_mark));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_0));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(300);

    Formula anotherFormula = (Formula) Reflection.getPrivateField(placeAtBrick, "yPosition");
    float anotherValue = anotherFormula.interpretFloat(sprite);

    Log.i("info", "value: " + value);

    assertTrue(
        "random() interpretation of float parameter is wrong: "
            + newYFormula
            + " anotherValue="
            + anotherValue,
        1 <= anotherValue
            && anotherValue <= 7.0f
            && (Math.abs(anotherValue) - (int) Math.abs(anotherValue)) > 0);
  }
 private void setAbsoluteCursorPosition(int position) {
   ((FormulaEditorEditText) solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID))
       .setDoNotMoveCursorOnTab(true);
   Reflection.setPrivateField(
       solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID), "absoluteCursorPosition", position);
   clickOnFormulaEditorEditText();
 }
  public void testUndoLimit() {

    solo.clickOnView(solo.getView(X_POS_EDIT_TEXT_RID));
    int maxHistoryElements =
        (Integer)
            Reflection.getPrivateField(new FormulaEditorHistory(null), "MAXIMUM_HISTORY_LENGTH");
    solo.clickOnView(solo.getView(R.id.formula_editor_edit_field_clear));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_1));

    String searchString = "";
    for (int i = 0; i < maxHistoryElements; i++) {
      solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_plus));
      searchString += " +";
    }
    solo.sleep(50);

    assertEquals(
        "Wrong text in field",
        "1" + searchString + " ",
        solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID).getText().toString());

    for (int i = 0; i < maxHistoryElements + 2; i++) {
      solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_undo));
    }

    assertEquals(
        "Wrong text in field",
        "1 ",
        solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID).getText().toString());

    for (int i = 0; i < maxHistoryElements + 2; i++) {
      solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_redo));
    }

    assertEquals(
        "Wrong text in field",
        "1" + searchString + " ",
        solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID).getText().toString());

    solo.goBack();
    solo.waitForText(solo.getString(R.string.formula_editor_discard_changes_dialog_title));
    solo.clickOnButton(solo.getString(R.string.no));
  }
  public void testRandomInterpretationWithIntegerParameters() {

    String newXFormula = "rand(rand(3),1)";

    solo.clickOnView(solo.getView(X_POS_EDIT_TEXT_RID));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_3));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(300);

    Formula formula = (Formula) Reflection.getPrivateField(placeAtBrick, "xPosition");
    float value = formula.interpretFloat(sprite);

    Log.i("info", "value: " + value);

    assertTrue(
        "random() interpretation of integer parameters is wrong: "
            + newXFormula
            + " anotherValue="
            + value,
        (value == 1 || value == 2 || value == 3));
    assertEquals(
        "random() interpretation of integer parameters is wrong: "
            + newXFormula
            + " anotherValue="
            + value,
        0,
        Math.abs(value) - (int) Math.abs(value),
        0);

    String newYFormula = "rand(4,1)";

    solo.clickOnView(solo.getView(Y_POS_EDIT_TEXT_RID));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_4));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(300);

    Formula anotherFormula = (Formula) Reflection.getPrivateField(placeAtBrick, "yPosition");
    float anotherValue = anotherFormula.interpretFloat(sprite);

    Log.i("info", "anotherValue: " + anotherValue);

    assertTrue(
        "random() interpretation of integer parameters is wrong: "
            + newYFormula
            + " anotherValue="
            + anotherValue,
        (anotherValue == 1 || anotherValue == 2 || anotherValue == 3 || anotherValue == 4));
    assertEquals(
        "random() interpretation of integer parameters is wrong: "
            + newYFormula
            + " anotherValue="
            + anotherValue,
        0,
        Math.abs(anotherValue) - (int) Math.abs(anotherValue),
        0);
  }
  public void testSimpleInterpretation() {
    String newXFormula = "10 + 12 - 2 × 3 - 4 ";
    int newXValue = 10 + 12 - 2 * 3 - 4;
    String newYFormula =
        getActivity().getString(R.string.formula_editor_function_rand)
            + "( "
            + getActivity().getString(R.string.formula_editor_function_cos)
            + "( 90 - - 30 ) , 1 ) ";

    solo.clickOnView(solo.getView(X_POS_EDIT_TEXT_RID));

    solo.sleep(250);
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_1));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_0));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_plus));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_1));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_2));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_minus));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_2));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_mult));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_3));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_minus));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_4));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(250);
    assertTrue(
        "Save failed toast not found",
        solo.searchText(solo.getString(R.string.formula_editor_changes_saved)));

    solo.clickOnView(solo.getView(Y_POS_EDIT_TEXT_RID));

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_random));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_math));
    solo.sleep(400);
    solo.clickOnText(getActivity().getString(R.string.formula_editor_function_cos));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_9));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_0));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_minus));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_minus));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_3));
    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_0));

    solo.clickOnView(solo.getView(X_POS_EDIT_TEXT_RID, 1));
    assertTrue(
        "Changes saved toast not found",
        solo.searchText(solo.getString(R.string.formula_editor_changes_saved)));

    assertEquals(
        "Wrong text in FormulaEditor",
        newXFormula,
        solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID).getText().toString());

    solo.clickOnView(solo.getView(Y_POS_EDIT_TEXT_RID, 1));
    solo.sleep(250);
    assertEquals(
        "Wrong text in FormulaEditor",
        newYFormula,
        solo.getEditText(FORMULA_EDITOR_EDIT_TEXT_ID).getText().toString());

    solo.clickOnView(solo.getView(R.id.formula_editor_keyboard_ok));
    solo.sleep(300);

    // Interpretation test
    Formula formula = (Formula) Reflection.getPrivateField(placeAtBrick, "xPosition");
    assertEquals("Wrong text in field", newXValue, formula.interpretInteger(sprite));

    formula = (Formula) Reflection.getPrivateField(placeAtBrick, "yPosition");

    float newYValue = formula.interpretFloat(sprite);
    assertTrue("Wrong text in field", newYValue >= -0.5f && newYValue <= 1f);
  }