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

Validate on save function on tests from 2.1.16 #642

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vijay.jsonwizard.customviews;

import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
Expand Down Expand Up @@ -37,8 +39,6 @@

import timber.log.Timber;

import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS;

/**
* Performs the expansion panel's {@link com.vijay.jsonwizard.widgets.ExpansionPanelFactory} functionality, which includes
* Reading and assigning values on load
Expand Down Expand Up @@ -208,10 +208,12 @@ private void attachOkDialogButton(ViewGroup dialogView) {
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passData();
getJsonApi().setGenericPopup(null);
getJsonApi().updateGenericPopupSecondaryValues(new JSONArray(), getStepName());
ExpansionPanelGenericPopupDialog.this.dismissAllowingStateLoss();
if (getFormFragment().saveWithoutClosingForm(false)) {
passTestData();
getJsonApi().setGenericPopup(null);
getJsonApi().updateGenericPopupSecondaryValues(new JSONArray(), getStepName());
ExpansionPanelGenericPopupDialog.this.dismissAllowingStateLoss();
}
}
});
}
Expand Down Expand Up @@ -350,7 +352,7 @@ public void setContext(Context context) throws IllegalStateException {
}

@Override
protected void passData() {
protected void passTestData() {
if (!TextUtils.isEmpty(getWidgetType()) && getWidgetType().equals(JsonFormConstants.EXPANSION_PANEL)) {
onDataPass(getParentKey(), getChildKey());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,12 @@ private void attachDialogOkButton(ViewGroup dialogView) {
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passData();
getJsonApi().setGenericPopup(null);
getJsonApi().updateGenericPopupSecondaryValues(null, getStepName());
GenericPopupDialog.this.dismiss();
if (getFormFragment().saveWithoutClosingForm(false)) {
passTestData();
getJsonApi().setGenericPopup(null);
getJsonApi().updateGenericPopupSecondaryValues(null, getStepName());
GenericPopupDialog.this.dismiss();
}
}
});
}
Expand All @@ -431,7 +433,7 @@ public void setContext(Context context) throws IllegalStateException {
this.context = context;
}

protected void passData() {
protected void passTestData() {
onGenericDataPass(getParentKey(), getChildKey());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ public boolean save(boolean skipValidation) {

return false;
}
public boolean saveWithoutClosingForm(boolean skipValidation) {
try {
mMainView.setTag(R.id.skip_validation, skipValidation);
return presenter.onSaveWithoutClosingForm(mMainView);
} catch (Exception e) {
Timber.e(e, " --> save");
return false;
}


}

public boolean shouldSkipStep() {
return shouldSkipStep;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.vijay.jsonwizard.presenters;

import static com.vijay.jsonwizard.utils.FormUtils.dpToPixels;
import static com.vijay.jsonwizard.utils.FormUtils.getDynamicLabelInfoList;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -92,9 +95,6 @@

import timber.log.Timber;

import static com.vijay.jsonwizard.utils.FormUtils.dpToPixels;
import static com.vijay.jsonwizard.utils.FormUtils.getDynamicLabelInfoList;

/**
* Created by vijay on 5/14/15.
*/
Expand Down Expand Up @@ -657,6 +657,32 @@ public void onSaveClick(LinearLayout mainView) {
}
}

public boolean onSaveWithoutClosingForm(LinearLayout mainView) {
validateAndWriteValues();
checkAndStopCountdownAlarm();
boolean isFormValid = isFormValid();
if (isFormValid || Boolean.parseBoolean(mainView.getTag(R.id.skip_validation).toString())) {
Utils.removeGeneratedDynamicRules(formFragment);
Intent returnIntent = new Intent();
getView().onFormFinish();
returnIntent.putExtra("json", formUtils.addFormDetails(getView().getCurrentJsonState()));
returnIntent.putExtra(JsonFormConstants.SKIP_VALIDATION,
Boolean.valueOf(mainView.getTag(R.id.skip_validation).toString()));
return true;
} else {
if (showErrorsOnSubmit()) {
launchErrorDialog();
getView().showToast(getView().getContext().getResources()
.getString(R.string.json_form_error_msg, getInvalidFields().size()));
} else {
getView().showSnackBar(getView().getContext().getResources()
.getString(R.string.json_form_error_msg, getInvalidFields().size()));

}
return false;
}
}

public boolean showErrorsOnSubmit() {
JSONObject entireJsonForm = formFragment.getJsonApi().getmJSONObject();
return entireJsonForm.optBoolean(JsonFormConstants.SHOW_ERRORS_ON_SUBMIT, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.vijay.jsonwizard.fragments;


import android.view.View;
import android.widget.LinearLayout;

import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.presenters.JsonFormFragmentPresenter;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;

Expand All @@ -13,6 +18,9 @@ public class JsonFormFragmentTest extends BaseTest {
@Mock
private JsonFormFragmentPresenter presenter;

@Mock
private LinearLayout mView;

@Test
public void testOnDestroyInvokesPresenterCleanUpAtleastOnce() {

Expand All @@ -23,5 +31,22 @@ public void testOnDestroyInvokesPresenterCleanUpAtleastOnce() {

Mockito.verify(presenter, Mockito.atLeastOnce()).cleanUp();

}

@Test
public void testSave()
{
JsonFormFragment jsonFormFragment = Mockito.spy(new JsonFormFragment());
jsonFormFragment.mMainView= mView;
Mockito.doReturn(presenter).when(jsonFormFragment).getPresenter();
Mockito.doNothing().when(mView).setTag(ArgumentMatchers.any());
Mockito.doNothing().when(presenter).onSaveClick(ArgumentMatchers.any());

boolean result = jsonFormFragment.save(true);
Assert.assertEquals(result,false);




}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
Expand Down Expand Up @@ -520,6 +519,42 @@ public void testGenerateTranslatableValueWithoutOptionsField () throws Exception
JSONObject item = new JSONObject(jsonForm);
Assert.assertEquals(expectedJson.toString(), Utils.generateTranslatableValue(item.optString(JsonFormConstants.KEY), item).toString());
}

@Test
public void testValueAfterTranslation() throws JSONException
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("value","translationValue");
Assert.assertEquals(Utils.getValueAfterTranslation(jsonObject),"translationValue");
JSONObject valueObject = new JSONObject(jsonObject.toString());
jsonObject.put("value",valueObject);
Assert.assertEquals(Utils.getValueAfterTranslation(jsonObject),valueObject.get("value"));


}

@Test
public void getJsonObjectFromArrayTest() throws JSONException
{
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("testKey", "keyValue");
jsonArray.put(jsonObject);

Assert.assertEquals(Utils.getJsonObjectFromJsonArray("testKey",jsonArray),jsonObject);
}

@Test
public void getValueTest() throws JSONException
{
Utils utils = new Utils();
JSONObject jsonObject = new JSONObject();
jsonObject.put("value","translationValue");
jsonObject.put(JsonFormConstants.EDIT_TYPE,JsonFormConstants.EDIT_TEXT_TYPE.NUMBER);
Assert.assertEquals(utils.getValue(jsonObject),"translationValue");

}

@Test
public void testExtractValueFromJson() throws JSONException
{
Expand Down