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

Spinner, toaster notes, date & time picker factories unit tests updates #478

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
Binary file modified android-json-form-wizard/jacoco.exec
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.vijay.jsonwizard.widgets;

import android.content.res.Resources;
import android.view.View;
import android.widget.RelativeLayout;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.rey.material.widget.Button;
import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.utils.FormUtils;

import org.json.JSONObject;
import org.junit.Assert;
Expand All @@ -20,64 +17,56 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;

import java.util.List;
import java.util.Set;

public class BarcodeFactorTest extends BaseTest {
private BarcodeFactory factory;
@Mock
private JsonFormActivity context;
private JsonFormActivity jsonFormActivity;

@Mock
private JsonFormFragment formFragment;

@Mock
private Resources resources;

@Mock
private CommonListener listener;

@Mock
private RelativeLayout rootLayout;

@Mock
private MaterialEditText editText;

@Mock
private Button scanButton;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
factory = new BarcodeFactory();
jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get();
}

@Test
public void testBarCodeFactoryInstantiatesViewsCorrectly() throws Exception {
Assert.assertNotNull(factory);
BarcodeFactory factorySpy = Mockito.spy(factory);
Assert.assertNotNull(factorySpy);

FormUtils formUtils = new FormUtils();
FormUtils formUtilsSpy = Mockito.spy(formUtils);
Assert.assertNotNull(formUtilsSpy);
String gpsString = "{\"key\":\"user_qr_code\",\"openmrs_entity_parent\":\"no_parent\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"user_qr_code_id\",\"type\":\"barcode\",\"barcode_type\":\"qrcode\",\"hint\":\"User ID\",\"read_only\":true,\"scanButtonText\":\"Scan QR Code\",\"v_numeric\":{\"value\":\"true\",\"err\":\"Please enter a valid ID\"},\"value\":\"123455646\",\"v_required\":{\"value\":true,\"err\":\"Please enter the user ID\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}}}";
List<View> viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(gpsString), listener);
Assert.assertNotNull(viewList);
Assert.assertTrue(viewList.size() > 0);
Assert.assertEquals(1, viewList.size());

context.setTheme(R.style.NativeFormsAppTheme);
Mockito.doReturn(rootLayout).when(factorySpy).getRootLayout(context);
Assert.assertNotNull(rootLayout);
RelativeLayout relativeLayout = (RelativeLayout) viewList.get(0);
Assert.assertNotNull(relativeLayout);

Mockito.doReturn(resources).when(context).getResources();
Assert.assertNotNull(resources);
MaterialEditText materialEditText = (MaterialEditText) relativeLayout.getChildAt(0);

Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text);
Assert.assertNotNull(editText);
Assert.assertEquals("user_qr_code", materialEditText.getTag(R.id.key));
Assert.assertEquals("no_parent", materialEditText.getTag(R.id.openmrs_entity_parent));
Assert.assertEquals("concept", materialEditText.getTag(R.id.openmrs_entity));
Assert.assertEquals("user_qr_code_id", materialEditText.getTag(R.id.openmrs_entity_id));
}

Mockito.doReturn(scanButton).when(rootLayout).findViewById(R.id.scan_button);
Assert.assertNotNull(scanButton);
@Test
public void testGetCustomTranslatableWidgetFields() {
Assert.assertNotNull(factory);
BarcodeFactory factorySpy = Mockito.spy(factory);
Assert.assertNotNull(factorySpy);

String gpsString = "{\"key\":\"user_qr_code\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"barcode\",\"barcode_type\":\"qrcode\",\"hint\":\"User ID\",\"read_only\":true,\"scanButtonText\":\"Scan QR Code\",\"v_numeric\":{\"value\":\"true\",\"err\":\"Please enter a valid ID\"},\"value\":\"123455646\",\"v_required\":{\"value\":true,\"err\":\"Please enter the user ID\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"constraints\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-constraints-rules.yml\"}}}}";
List<View> viewList = factorySpy.getViewsFromJson("RandomStepName", context, formFragment, new JSONObject(gpsString), listener);
Assert.assertNotNull(viewList);
Assert.assertTrue(viewList.size() > 0);
Set<String> editableProperties = factorySpy.getCustomTranslatableWidgetFields();
Assert.assertEquals(1, editableProperties.size());
Assert.assertEquals("scanButtonText", editableProperties.iterator().next());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.Set;

import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1;

/**
* Created by Vincent Karuri on 25/08/2020
*/
public class ButtonFactoryTest extends FactoryTest {

@Mock
private JsonFormFragment jsonFormFragment;
@Mock
Expand Down Expand Up @@ -96,4 +97,14 @@ private JSONObject getJsonObject() throws JSONException {
jsonObject.put(JsonFormConstants.ACTION, action);
return jsonObject;
}

@Test
public void testGetCustomTranslatableWidgetFields() {
Assert.assertNotNull(buttonFactory);
ButtonFactory factorySpy = Mockito.spy(buttonFactory);
Assert.assertNotNull(factorySpy);

Set<String> editableProperties = factorySpy.getCustomTranslatableWidgetFields();
Assert.assertEquals(0, editableProperties.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.vijay.jsonwizard.widgets;

import android.app.FragmentManager;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Looper;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.vijay.jsonwizard.BaseTest;
Expand All @@ -15,124 +12,99 @@
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.shadow.ShadowDialogFragment;
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.FormUtils;

import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.reflect.Whitebox;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import static android.os.Looper.getMainLooper;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

public class DatePickerFactoryTest extends BaseTest {
private DatePickerFactory factory;
@Mock
private JsonFormActivity formActivity;

private FormUtils formUtils;
private JsonFormActivity jsonFormActivity;
@Mock
private JsonFormFragment formFragment;


private Resources resources = RuntimeEnvironment.application.getResources();

@Mock
private CommonListener listener;

@Mock
private MaterialEditText editText;

@Mock
private RelativeLayout rootLayout;

@Mock
private TextView duration;

@Mock
private FragmentManager fragmentManager;

@Captor
private ArgumentCaptor<View> viewArgumentCaptor;

@Captor
private ArgumentCaptor<DatePickerDialog> datePickerDialogArgumentCaptor;


@Before
public void setUp() {
AppExecutors appExecutors = new AppExecutors();
when(formActivity.getAppExecutors()).thenReturn(appExecutors);
factory = spy(new DatePickerFactory());
Mockito.doReturn(Locale.ENGLISH).when(factory).getCurrentLocale(formActivity);
Mockito.doReturn(Locale.ENGLISH).when(factory).getSetLanguage(formActivity);
Mockito.doReturn(resources).when(formActivity).getResources();
Mockito.doReturn("12 Age").when(factory).getDurationText(formActivity, "12-05-2010", Locale.ENGLISH);
MockitoAnnotations.initMocks(this);
factory = new DatePickerFactory();
formUtils = new FormUtils();
jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get();
}

@Test
public void testDatePickerFactoryInstantiatesViewsCorrectly() throws Exception {
Mockito.doReturn(resources).when(formActivity).getResources();

formActivity.setTheme(R.style.NativeFormsAppTheme);
Mockito.doReturn(rootLayout).when(factory).getRelativeLayout(formActivity);
Mockito.doReturn(editText).when(rootLayout).findViewById(R.id.edit_text);
Mockito.doReturn(duration).when(rootLayout).findViewById(R.id.duration);
Mockito.doReturn(duration).when(rootLayout).findViewById(R.id.duration);
String datePicker = "{\"key\":\"First_Health_Facility_Contact\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"text\",\"type\":\"date_picker\",\"hint\":\"Date first seen *\",\"expanded\":false,\"min_date\":\"today-5y\",\"max_date\":\"today\",\"v_required\":{\"value\":\"true\",\"err\":\"Enter the date that the child was first seen at a health facility for immunization services\"},\"constraints\":{\"type\":\"date\",\"ex\":\"greaterThanEqualTo(., step1:Date_Birth)\",\"err\":\"Date first seen can't occur before date of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"12-05-2020\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\",\"duration\":{\"label\":\"AGE\"}}";
List<View> viewList = factory.getViewsFromJson("RandomStepName", formActivity, formFragment, new JSONObject(datePicker), listener);
shadowOf(getMainLooper()).idle();
Assert.assertNotNull(formUtils);
FormUtils formUtilsSpy = Mockito.spy(formUtils);
Assert.assertNotNull(formUtilsSpy);
Whitebox.setInternalState(factory, "formUtils", formUtilsSpy);

List<View> viewList = factory.getViewsFromJson("RandomStepName", jsonFormActivity, formFragment, new JSONObject(datePicker), listener);
Assert.assertNotNull(viewList);
assertEquals(1, viewList.size());
Assert.assertEquals(1, viewList.size());

View rootLayout = viewList.get(0);
Assert.assertEquals(3, ((RelativeLayout) rootLayout).getChildCount());

MaterialEditText materialEditText = (MaterialEditText) ((RelativeLayout) rootLayout).getChildAt(0);

Assert.assertEquals("First_Health_Facility_Contact", materialEditText.getTag(R.id.key));
Assert.assertEquals("", materialEditText.getTag(R.id.openmrs_entity_parent));
Assert.assertEquals("concept", materialEditText.getTag(R.id.openmrs_entity));
Assert.assertEquals("163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", materialEditText.getTag(R.id.openmrs_entity_id));
}

@Test
public void testGetCustomTranslatableWidgetFields() {
Assert.assertNotNull(factory);
DatePickerFactory factorySpy = spy(factory);
DatePickerFactory factorySpy = Mockito.spy(factory);
Assert.assertNotNull(factorySpy);

Set<String> editableProperties = factorySpy.getCustomTranslatableWidgetFields();
assertEquals(1, editableProperties.size());
assertEquals("duration.label", editableProperties.iterator().next());
Assert.assertEquals(1, editableProperties.size());
Assert.assertEquals("duration.label", editableProperties.iterator().next());
}


@Config(shadows = {ShadowDialogFragment.class})
@Test
public void testShowDatePickerDialog() throws Exception {
when(formActivity.getFragmentManager()).thenReturn(fragmentManager);
AppCompatActivity appCompatActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get();
RelativeLayout view = (RelativeLayout) appCompatActivity.getLayoutInflater().inflate(factory.getLayout(), null);
Mockito.doReturn(view).when(factory).getRelativeLayout(formActivity);
JsonFormActivity jsonFormActivitySpy = Mockito.spy(jsonFormActivity);
DatePickerFactory datePickerFactory = Mockito.spy(factory);
String datePicker = "{\"key\":\"First_Health_Facility_Contact\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163260AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"text\",\"type\":\"date_picker\",\"hint\":\"Date first seen *\",\"expanded\":false,\"min_date\":\"today-5y\",\"max_date\":\"today\",\"v_required\":{\"value\":\"true\",\"err\":\"Enter the date that the child was first seen at a health facility for immunization services\"},\"constraints\":{\"type\":\"date\",\"ex\":\"greaterThanEqualTo(., step1:Date_Birth)\",\"err\":\"Date first seen can't occur before date of birth\"},\"relevance\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-relevance-rules.yml\"}}},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}},\"value\":\"12-05-2020\",\"read_only\":true,\"label_info_text\":\"Just testing\",\"label_info_title\":\"Just testing\",\"duration\":{\"label\":\"AGE\"}}";
List<View> viewList = factory.getViewsFromJson("RandomStepName", formActivity, formFragment, new JSONObject(datePicker), listener);
shadowOf(getMainLooper()).idle();
assertEquals(1, viewList.size());
verify(formActivity).addFormDataView(viewArgumentCaptor.capture());
List<View> viewList = datePickerFactory.getViewsFromJson("RandomStepName", jsonFormActivitySpy, formFragment, new JSONObject(datePicker), listener);
Shadows.shadowOf(Looper.getMainLooper()).idle();
Assert.assertEquals(1, viewList.size());
Mockito.verify(jsonFormActivitySpy).addFormDataView(viewArgumentCaptor.capture());
viewArgumentCaptor.getValue().performClick();
verify(factory).showDatePickerDialog(eq(formActivity), datePickerDialogArgumentCaptor.capture(), any(MaterialEditText.class));
verify(fragmentManager).beginTransaction();
verify(fragmentManager).executePendingTransactions();
Mockito.verify(datePickerFactory).showDatePickerDialog(Mockito.eq(jsonFormActivitySpy), datePickerDialogArgumentCaptor.capture(), ArgumentMatchers.any(MaterialEditText.class));
DatePickerDialog datePickerDialog = datePickerDialogArgumentCaptor.getValue();
assertEquals("12-05-2020", new SimpleDateFormat("dd-MM-yyyy").format(Whitebox.getInternalState(datePickerDialog, "date")));
Assert.assertEquals("12-05-2020", new SimpleDateFormat("dd-MM-yyyy").format(Whitebox.getInternalState(datePickerDialog, "date")));

}

Expand Down
Loading