From 3fc8d2db6d25d5215b0e147e447ab3bff5f8f998 Mon Sep 17 00:00:00 2001 From: SebaMutuku Date: Fri, 11 Aug 2023 21:11:39 +0300 Subject: [PATCH 1/2] Add tests for setUpOptiBpActivityResultListener --- .../jsonwizard/widgets/BarcodeFactory.java | 20 +- .../widgets/FingerPrintFactory.java | 26 +- .../widgets/OptiBpWidgetFactoryTest.java | 565 +++++++++++------- gradle.properties | 2 +- 4 files changed, 376 insertions(+), 237 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java index eeab9a1b4..409bdadfd 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/BarcodeFactory.java @@ -157,18 +157,14 @@ protected void addOnBarCodeResultListeners(final Context context, final Material if (context instanceof JsonApi) { JsonApi jsonApi = (JsonApi) context; jsonApi.addOnActivityResultListener(JsonFormConstants.BARCODE_CONSTANTS.BARCODE_REQUEST_CODE, - new OnActivityResultListener() { - @Override - public void onActivityResult(int requestCode, - int resultCode, Intent data) { - if (requestCode == JsonFormConstants.BARCODE_CONSTANTS.BARCODE_REQUEST_CODE && resultCode == RESULT_OK) { - if (data != null) { - Barcode barcode = data.getParcelableExtra(JsonFormConstants.BARCODE_CONSTANTS.BARCODE_KEY); - Timber.d("Scanned QR Code %s ", barcode.displayValue); - editText.setText(barcode.displayValue); - } else - Timber.i("NO RESULT FOR QR CODE"); - } + (requestCode, resultCode, data) -> { + if (requestCode == JsonFormConstants.BARCODE_CONSTANTS.BARCODE_REQUEST_CODE && resultCode == RESULT_OK) { + if (data != null) { + Barcode barcode = data.getParcelableExtra(JsonFormConstants.BARCODE_CONSTANTS.BARCODE_KEY); + Timber.d("Scanned QR Code %s ", barcode.displayValue); + editText.setText(barcode.displayValue); + } else + Timber.i("NO RESULT FOR QR CODE"); } }); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/FingerPrintFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/FingerPrintFactory.java index 05b927732..63e5b5428 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/FingerPrintFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/FingerPrintFactory.java @@ -83,21 +83,17 @@ private void addFingerprintResultsListener(final Context context, final ImageVie if (context instanceof JsonApi) { JsonApi jsonApi = (JsonApi) context; jsonApi.addOnActivityResultListener(JsonFormConstants.ACTIVITY_REQUEST_CODE.REQUEST_CODE_REGISTER, - new OnActivityResultListener() { - @Override - public void onActivityResult(int requestCode, - int resultCode, Intent data) { - if (requestCode == JsonFormConstants.ACTIVITY_REQUEST_CODE.REQUEST_CODE_REGISTER && resultCode == RESULT_OK) { - if (data != null) { - - SimPrintsRegistration registration = (SimPrintsRegistration) data.getSerializableExtra(SimPrintsConstantHelper.INTENT_DATA); - imageView.setTag(R.id.simprints_guid, registration.getGuid()); - setFingerprintDrawable(context, imageView, registration.getGuid(), true); - Timber.d("Scanned Fingerprint GUID %s ", registration.getGuid()); - } else { - Timber.i("NO RESULT FOR FINGERPRINT"); - setFingerprintDrawable(context, imageView, "", true); - } + (requestCode, resultCode, data) -> { + if (requestCode == JsonFormConstants.ACTIVITY_REQUEST_CODE.REQUEST_CODE_REGISTER && resultCode == RESULT_OK) { + if (data != null) { + + SimPrintsRegistration registration = (SimPrintsRegistration) data.getSerializableExtra(SimPrintsConstantHelper.INTENT_DATA); + imageView.setTag(R.id.simprints_guid, registration.getGuid()); + setFingerprintDrawable(context, imageView, registration.getGuid(), true); + Timber.d("Scanned Fingerprint GUID %s ", registration.getGuid()); + } else { + Timber.i("NO RESULT FOR FINGERPRINT"); + setFingerprintDrawable(context, imageView, "", true); } } }); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java index a4c9b043a..5e772301b 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java @@ -1,11 +1,11 @@ package com.vijay.jsonwizard.widgets; -import android.content.res.Resources; -import android.view.View; -import android.view.ViewTreeObserver; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.TextView; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; @@ -14,6 +14,7 @@ import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.interfaces.JsonApi; +import com.vijay.jsonwizard.interfaces.OnActivityResultListener; import com.vijay.jsonwizard.utils.FormUtils; import org.json.JSONException; @@ -21,21 +22,36 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; +import org.robolectric.Shadows; +import org.robolectric.shadows.ShadowLooper; +import org.robolectric.shadows.ShadowToast; +import java.time.Duration; import java.util.List; +import android.app.Activity; +import android.content.Intent; +import android.content.res.Resources; +import android.os.Looper; +import android.view.View; +import android.view.ViewTreeObserver; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; import timber.log.Timber; - public class OptiBpWidgetFactoryTest extends FactoryTest { private OptiBPWidgetFactory factory; @Mock private JsonFormActivity context; + @Mock private JsonFormFragment formFragment; @@ -56,6 +72,14 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { WidgetArgs widgetArgs; + @Mock + Intent mockIntent; + + EditText systolicBp; + + EditText diastolicBp; + + private final String optiBPWidgetString = "{\n" + " \"key\":\"optipb_widget1\",\n" + " \"openmrs_entity_parent\":\"\",\n" + @@ -77,120 +101,6 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { " }\n" + "}"; - private final String step1Json="{\n" + - " \"title\":\"OptiBp Widget Demo\",\n" + - " \"fields\":[\n" + - " {\n" + - " \"key\":\"enabled_label\",\n" + - " \"type\":\"label\",\n" + - " \"text\":\"OptiBP Scan\",\n" + - " \"hint_on_text\":false,\n" + - " \"text_color\":\"#FFC100\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"\",\n" + - " \"label_info_text\":\"Checking out the functionality for OptiBP widget\"\n" + - " },\n" + - " {\n" + - " \"key\":\"bp_systolic_label\",\n" + - " \"type\":\"label\",\n" + - " \"label_text_style\":\"bold\",\n" + - " \"text\":\"Systolic blood pressure (SBP) (mmHg)\",\n" + - " \"text_color\":\"#000000\",\n" + - " \"v_required\":{\n" + - " \"value\":true\n" + - " }\n" + - " },\n" + - " {\n" + - " \"key\":\"bp_systolic\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"5090\",\n" + - " \"type\":\"normal_edit_text\",\n" + - " \"edit_text_style\":\"bordered\",\n" + - " \"edit_type\":\"number\",\n" + - " \"v_required\":{\n" + - " \"value\":\"true\",\n" + - " \"err\":\"Please enter BP systolic value\"\n" + - " },\n" + - " \"v_numeric\":{\n" + - " \"value\":\"true\",\n" + - " \"err\":\"\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"key\":\"spacer\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"spacer\",\n" + - " \"type\":\"spacer\",\n" + - " \"spacer_height\":\"10sp\"\n" + - " },\n" + - " {\n" + - " \"key\":\"bp_diastolic_label\",\n" + - " \"type\":\"label\",\n" + - " \"label_text_style\":\"bold\",\n" + - " \"text\":\"Diastolic blood pressure (DBP) (mmHg)\",\n" + - " \"text_color\":\"#000000\",\n" + - " \"v_required\":{\n" + - " \"value\":true\n" + - " }\n" + - " },\n" + - " {\n" + - " \"key\":\"bp_diastolic\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"5089\",\n" + - " \"type\":\"normal_edit_text\",\n" + - " \"edit_text_style\":\"bordered\",\n" + - " \"edit_type\":\"number\",\n" + - " \"v_numeric\":{\n" + - " \"value\":\"true\",\n" + - " \"err\":\"\"\n" + - " },\n" + - " \"v_required\":{\n" + - " \"value\":\"true\",\n" + - " \"err\":\"Please enter the BP diastolic value\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"key\":\"spacer\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"spacer\",\n" + - " \"type\":\"spacer\",\n" + - " \"spacer_height\":\"10sp\"\n" + - " },\n" + - " {\n" + - " \"key\":\"optipb_widget1\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"\",\n" + - " \"type\":\"optibp\",\n" + - " \"label\":\"Measure the blood pressure using OptiBP\",\n" + - " \"optibp_button_bg_color\":\"#d32f2f\",\n" + - " \"optibp_button_text_color\":\"#FFFFFF\",\n" + - " \"read_only\":false,\n" + - " \"fields_to_use_value\":[\n" + - " \"bp_systolic\",\n" + - " \"bp_diastolic\"\n" + - " ],\n" + - " \"optibp_data\":{\n" + - " \"clientId\":\"sampleClientId\",\n" + - " \"clientOpenSRPId\":\"sampleClientOpenSRPId\",\n" + - " \"calibration\":\"\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"key\":\"optibp_client_calibration_data\",\n" + - " \"openmrs_entity_parent\":\"\",\n" + - " \"openmrs_entity\":\"\",\n" + - " \"openmrs_entity_id\":\"\",\n" + - " \"type\":\"hidden\",\n" + - " \"value\":\"[{\\\"date\\\":\\\"2019-03-26T11:20:33+0800\\\",\\\"model\\\":\\\"device model\\\",\\\"height\\\":70,\\\"weight\\\":180,\\\"comperatives\\\":[{\\\"systolic\\\":120,\\\"diastolic\\\":80,\\\"cuffSystolic\\\":120,\\\"cuffDiastolic\\\":80,\\\"features\\\":{\\\"$key\\\":\\\"0.2f\\\"}}]}]\"\n" + - " }\n" + - " ]\n" + - "}"; private final String formString = "{\n" + " \"show_errors_on_submit\": true,\n" + " \"encounter_type\": \"OptiBP Demo\",\n" + @@ -361,72 +271,85 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { "}"; private final String resultJson = "{\n" + - " \"resourceType\": \"DiagnosticReport\",\n" + - " \"identifier\": [\n" + - " {\n" + - " \"use\": \"official\",\n" + - " \"value\": \"uuid\" \n" + - " }\n" + - " ],\n" + - " \"subject\": \"patient-uuid\", \n" + - " \"result\": [\n" + - " {\n" + - " \"status\": \"final\",\n" + - " \"category\": [\n" + + " \"identifier\": [\n" + " {\n" + - " \"coding\": [\n" + - " {\n" + - " \"system\": \"http://terminology.hl7.org/CodeSystem/observation-category\",\n" + - " \"code\": \"vital-signs\",\n" + - " \"display\": \"vital-signs\"\n" + - " }\n" + - " ]\n" + + " \"use\": \"official\",\n" + + " \"value\": \"dc2122cf-213c-469f-a1a8-f42035bc3559\"\n" + " }\n" + - " ],\n" + - " \"code\": {\n" + - " \"coding\": [\n" + - " {\n" + - " \"system\": \"http://loinc.org\",\n" + - " \"code\": \"55284-4\",\n" + - " \"display\": \"Blood Pressure\"\n" + - " }\n" + - " ],\n" + - " \"text\": \"Blood Pressure\"\n" + - " },\n" + - " \"component\": [\n" + + " ],\n" + + " \"resourceType\": \"DiagnosticReport\",\n" + + " \"result\": [\n" + " {\n" + - " \"code\": {\n" + - " \"coding\": [\n" + - " {\n" + - " \"system\": \"http://loinc.org\",\n" + - " \"code\": \"8462-4\",\n" + - " \"display\": \"Diastolic Blood Pressure\"\n" + - " }\n" + + " \"category\": [\n" + + " {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"vital-signs\",\n" + + " \"display\": \"vital-signs\",\n" + + " \"system\": \"http://terminology.hl7.org/CodeSystem/observation-category\"\n" + + " }\n" + + " ]\n" + + " }\n" + " ],\n" + - " \"text\": \"Diastolic Blood Pressure\"\n" + - " },\n" + - " \"valueQuantity\": {\n" + - " \"value\": 70\n" + - " }\n" + - " },\n" + - " {\n" + - " \"code\": {\n" + - " \"coding\": [\n" + - " {\n" + - " \"system\": \"http://loinc.org\",\n" + - " \"code\": \"8480-6\",\n" + - " \"display\": \"Systolic Blood Pressure\"\n" + - " }\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"55284-4\",\n" + + " \"display\": \"Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Blood Pressure\"\n" + + " },\n" + + " \"component\": [\n" + + " {\n" + + " \"valueQuantity\": {\n" + + " \"value\": -10\n" + + " },\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"8462-4\",\n" + + " \"display\": \"Diastolic Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Diastolic Blood Pressure\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"valueQuantity\": {\n" + + " \"value\": 72\n" + + " },\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"8462-6\",\n" + + " \"display\": \"Systolic Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Systolic Blood Pressure\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"valueString\": \"[{\\\"comperatives\\\":[{\\\"cuffDiastolic\\\":100,\\\"cuffSystolic\\\":180,\\\"diastolic\\\":85,\\\"systolic\\\":180}],\\\"date\\\":\\\"2023-07-14T11:44:19+0500\\\",\\\"height\\\":100,\\\"model\\\":\\\"SM-T540\\\",\\\"version\\\":1,\\\"weight\\\":66}]\",\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"bp_calibration_data\",\n" + + " \"display\": \"Blood Pressure Calibration Data\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Blood Pressure Calibration Data\"\n" + + " }\n" + + " }\n" + " ],\n" + - " \"text\": \"Systolic Blood Pressure\"\n" + - " },\n" + - " \"valueQuantity\": {\n" + - " \"value\": 110\n" + - " }\n" + + " \"status\": \"final\"\n" + " }\n" + - " ]\n" + - " }\n" + - " ]\n" + + " ],\n" + + " \"subject\": \"c55e3d2e-398d-4b7b-aedd-5378b5113d71\"\n" + "}"; @Before @@ -437,84 +360,308 @@ public void setUp() { } catch (JSONException e) { Timber.e(e); } - EditText sbp = new EditText(jsonFormActivity); - sbp.setTag(R.id.address, "step1:bp_systolic"); - jsonFormActivity.addFormDataView(sbp); - EditText dbp = new EditText(jsonFormActivity); - dbp.setTag(R.id.address, "step1:bp_diastolic"); - jsonFormActivity.addFormDataView(dbp); + systolicBp = new EditText(jsonFormActivity); + systolicBp.setTag(R.id.address, "step1:bp_systolic"); + jsonFormActivity.addFormDataView(systolicBp); + diastolicBp = new EditText(jsonFormActivity); + diastolicBp.setTag(R.id.address, "step1:bp_diastolic"); + jsonFormActivity.addFormDataView(diastolicBp); factory = new OptiBPWidgetFactory(); - widgetArgs=new WidgetArgs(); + widgetArgs = new WidgetArgs(); } @Test public void testFactoryInstantiatesViewsCorrectly() throws Exception { - Assert.assertNotNull(factory); + assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); + assertNotNull(factorySpy); FormUtils formUtils = new FormUtils(); FormUtils formUtilsSpy = Mockito.spy(formUtils); - Assert.assertNotNull(formUtilsSpy); + assertNotNull(formUtilsSpy); Mockito.doReturn(rootLayout).when(factorySpy).getRootLayout(context); - Assert.assertNotNull(rootLayout); + assertNotNull(rootLayout); Mockito.doReturn(Mockito.mock(ViewTreeObserver.class)).when(rootLayout).getViewTreeObserver(); Mockito.doReturn(resources).when(context).getResources(); - Assert.assertNotNull(resources); + assertNotNull(resources); Mockito.doReturn(label).when(rootLayout).findViewById(R.id.optibp_label); - Assert.assertNotNull(label); + assertNotNull(label); Mockito.doReturn(launchButton).when(rootLayout).findViewById(R.id.optibp_launch_button); - Assert.assertNotNull(launchButton); + assertNotNull(launchButton); //noinspection ResultOfMethodCallIgnored Mockito.doReturn(jsonFormActivity).when(formFragment).getJsonApi(); List viewList = factorySpy.getViewsFromJson("step1", context, formFragment, new JSONObject(optiBPWidgetString), listener); - Assert.assertNotNull(viewList); + assertNotNull(viewList); Assert.assertTrue(viewList.size() > 0); } @Test public void testInputJson() throws JSONException { - Assert.assertNotNull(factory); + assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); + assertNotNull(factorySpy); WidgetArgs widgetArgs = Mockito.mock(WidgetArgs.class); JsonFormFragment formFragment = Mockito.mock(JsonFormFragment.class); JsonApi jsonApi = Mockito.mock(JsonApi.class); Mockito.doReturn(formFragment).when(widgetArgs).getFormFragment(); Mockito.doReturn(jsonApi).when(formFragment).getJsonApi(); + String step1Json = "{\n" + + " \"title\":\"OptiBp Widget Demo\",\n" + + " \"fields\":[\n" + + " {\n" + + " \"key\":\"enabled_label\",\n" + + " \"type\":\"label\",\n" + + " \"text\":\"OptiBP Scan\",\n" + + " \"hint_on_text\":false,\n" + + " \"text_color\":\"#FFC100\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"label_info_text\":\"Checking out the functionality for OptiBP widget\"\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_systolic_label\",\n" + + " \"type\":\"label\",\n" + + " \"label_text_style\":\"bold\",\n" + + " \"text\":\"Systolic blood pressure (SBP) (mmHg)\",\n" + + " \"text_color\":\"#000000\",\n" + + " \"v_required\":{\n" + + " \"value\":true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_systolic\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"5090\",\n" + + " \"type\":\"normal_edit_text\",\n" + + " \"edit_text_style\":\"bordered\",\n" + + " \"edit_type\":\"number\",\n" + + " \"v_required\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"Please enter BP systolic value\"\n" + + " },\n" + + " \"v_numeric\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"spacer\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"spacer\",\n" + + " \"type\":\"spacer\",\n" + + " \"spacer_height\":\"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_diastolic_label\",\n" + + " \"type\":\"label\",\n" + + " \"label_text_style\":\"bold\",\n" + + " \"text\":\"Diastolic blood pressure (DBP) (mmHg)\",\n" + + " \"text_color\":\"#000000\",\n" + + " \"v_required\":{\n" + + " \"value\":true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_diastolic\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"5089\",\n" + + " \"type\":\"normal_edit_text\",\n" + + " \"edit_text_style\":\"bordered\",\n" + + " \"edit_type\":\"number\",\n" + + " \"v_numeric\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"\"\n" + + " },\n" + + " \"v_required\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"Please enter the BP diastolic value\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"spacer\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"spacer\",\n" + + " \"type\":\"spacer\",\n" + + " \"spacer_height\":\"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\":\"optipb_widget1\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"type\":\"optibp\",\n" + + " \"label\":\"Measure the blood pressure using OptiBP\",\n" + + " \"optibp_button_bg_color\":\"#d32f2f\",\n" + + " \"optibp_button_text_color\":\"#FFFFFF\",\n" + + " \"read_only\":false,\n" + + " \"fields_to_use_value\":[\n" + + " \"bp_systolic\",\n" + + " \"bp_diastolic\"\n" + + " ],\n" + + " \"optibp_data\":{\n" + + " \"clientId\":\"sampleClientId\",\n" + + " \"clientOpenSRPId\":\"sampleClientOpenSRPId\",\n" + + " \"calibration\":\"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"optibp_client_calibration_data\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"type\":\"hidden\",\n" + + " \"value\":\"[{\\\"date\\\":\\\"2019-03-26T11:20:33+0800\\\",\\\"model\\\":\\\"device model\\\",\\\"height\\\":70,\\\"weight\\\":180,\\\"comperatives\\\":[{\\\"systolic\\\":120,\\\"diastolic\\\":80,\\\"cuffSystolic\\\":120,\\\"cuffDiastolic\\\":80,\\\"features\\\":{\\\"$key\\\":\\\"0.2f\\\"}}]}]\"\n" + + " }\n" + + " ]\n" + + "}"; Mockito.doReturn(new JSONObject(step1Json)).when(jsonApi).getStep(ArgumentMatchers.anyString()); String inputJson = factorySpy.getInputJsonString(jsonFormActivity, new JSONObject(optiBPWidgetString), widgetArgs); - Assert.assertEquals(inputJson, "{\"clientId\":\"sampleClientId\",\"clientOpenSRPId\":\"sampleClientOpenSRPId\"}"); + assertEquals(inputJson, "{\"clientId\":\"sampleClientId\",\"clientOpenSRPId\":\"sampleClientOpenSRPId\"}"); } @Test public void testResultJson() throws JSONException { - Assert.assertNotNull(factory); + assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); + assertNotNull(factorySpy); String systolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.SYSTOLIC_BP); String diastolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.DIASTOLIC_BP); - Assert.assertEquals(systolic, "110"); - Assert.assertEquals(diastolic, "70"); + assertEquals(systolic, "72"); + assertEquals(diastolic, "-10"); } @Test public void testPopulateETValues() throws JSONException { - Assert.assertNotNull(factory); + assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); - Assert.assertNotNull(factorySpy); + assertNotNull(factorySpy); EditText sbp = Mockito.mock(EditText.class); EditText dbp = Mockito.mock(EditText.class); - widgetArgs=Mockito.mock(WidgetArgs.class); - factorySpy.populateBPEditTextValues(resultJson, sbp, dbp,widgetArgs); - Mockito.verify(sbp).setEnabled(false); - Mockito.verify(dbp).setEnabled(false); + widgetArgs = Mockito.mock(WidgetArgs.class); + factorySpy.populateBPEditTextValues(resultJson, sbp, dbp, widgetArgs); + verify(sbp).setEnabled(false); + verify(dbp).setEnabled(false); + } + + @Test + public void testSetUpOptiBpActivityResultListenerReturnsResultOK() { + WidgetArgs widgetArgs = Mockito.mock(WidgetArgs.class); + when(widgetArgs.getContext()).thenReturn(jsonFormActivity); + when(mockIntent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn(resultJson); + int requestCode = 301; + factory.setUpOptiBpActivityResultListener(widgetArgs, requestCode, rootLayout, systolicBp, diastolicBp); + ArgumentCaptor resultListenerArgumentCaptor = ArgumentCaptor.forClass(OnActivityResultListener.class); + verify(jsonFormActivity).addOnActivityResultListener(anyInt(), resultListenerArgumentCaptor.capture()); + resultListenerArgumentCaptor.getValue().onActivityResult(requestCode,Activity.RESULT_OK,mockIntent); + + } + @Test + public void testSetUpOptiBpActivityResultListenerShowsToast() { + WidgetArgs widgetArgs = Mockito.mock(WidgetArgs.class); + when(widgetArgs.getContext()).thenReturn(jsonFormActivity); + String badJson="{\n" + + " \"identifier\": [\n" + + " {\n" + + " \"use\": \"official\",\n" + + " \"value\": \"dc2122cf-213c-469f-a1a8-f42035bc3559\"\n" + + " }\n" + + " ],\n" + + " \"resourceType\": \"DiagnosticReport\",\n" + + " \"result\": [\n" + + " {\n" + + " \"category\": [\n" + + " {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"vital-signs\",\n" + + " \"display\": \"vital-signs\",\n" + + " \"system\": \"http://terminology.hl7.org/CodeSystem/observation-category\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ],\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"55284-4\",\n" + + " \"display\": \"Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Blood Pressure\"\n" + + " },\n" + + " \"component\": [\n" + + " {\n" + + " \"valueQuantity\": {\n" + + " \"value\": -10\n" + + " },\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"8462-4\",\n" + + " \"display\": \"Diastolic Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Diastolic Blood Pressure\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"valueQuantity\": {\n" + + " \"value\": 72\n" + + " },\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"8462-6\",\n" + + " \"display\": \"Systolic Blood Pressure\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Systolic Blood Pressure\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"valueString\": \"\",\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"code\": \"bp_calibration_data\",\n" + + " \"display\": \"Blood Pressure Calibration Data\",\n" + + " \"system\": \"http://loinc.org\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Blood Pressure Calibration Data\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"status\": \"final\"\n" + + " }\n" + + " ],\n" + + " \"subject\": \"c55e3d2e-398d-4b7b-aedd-5378b5113d71\"\n" + + "}"; + when(mockIntent.getStringExtra(Intent.EXTRA_TEXT)).thenReturn(badJson); + int requestCode = 301; + factory.setUpOptiBpActivityResultListener(widgetArgs, requestCode, rootLayout, systolicBp, diastolicBp); + ArgumentCaptor resultListenerArgumentCaptor = ArgumentCaptor.forClass(OnActivityResultListener.class); + verify(jsonFormActivity).addOnActivityResultListener(anyInt(), resultListenerArgumentCaptor.capture()); + resultListenerArgumentCaptor.getValue().onActivityResult(requestCode,Activity.RESULT_OK,mockIntent); + ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); + shadowLooper.idleFor(Duration.ofDays(Toast.LENGTH_SHORT)); + Toast toast = ShadowToast.getLatestToast(); + assertEquals(Toast.LENGTH_SHORT, toast.getDuration()); + } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 5abb92c86..be834e97e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.1.1-SNAPSHOT +VERSION_NAME=3.1.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard From fd5b2a181e806ac2a9dd5d0d22a1a7b883192a41 Mon Sep 17 00:00:00 2001 From: SebaMutuku Date: Tue, 12 Sep 2023 11:33:27 +0300 Subject: [PATCH 2/2] Add tests for OptiBPWidgetFactory:getValueResult --- .../widgets/OptiBpWidgetFactoryTest.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java index 5e772301b..ddc50404f 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -17,6 +18,7 @@ import com.vijay.jsonwizard.interfaces.OnActivityResultListener; import com.vijay.jsonwizard.utils.FormUtils; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; @@ -564,14 +566,15 @@ public void testSetUpOptiBpActivityResultListenerReturnsResultOK() { factory.setUpOptiBpActivityResultListener(widgetArgs, requestCode, rootLayout, systolicBp, diastolicBp); ArgumentCaptor resultListenerArgumentCaptor = ArgumentCaptor.forClass(OnActivityResultListener.class); verify(jsonFormActivity).addOnActivityResultListener(anyInt(), resultListenerArgumentCaptor.capture()); - resultListenerArgumentCaptor.getValue().onActivityResult(requestCode,Activity.RESULT_OK,mockIntent); + resultListenerArgumentCaptor.getValue().onActivityResult(requestCode, Activity.RESULT_OK, mockIntent); } + @Test public void testSetUpOptiBpActivityResultListenerShowsToast() { WidgetArgs widgetArgs = Mockito.mock(WidgetArgs.class); when(widgetArgs.getContext()).thenReturn(jsonFormActivity); - String badJson="{\n" + + String badJson = "{\n" + " \"identifier\": [\n" + " {\n" + " \"use\": \"official\",\n" + @@ -664,4 +667,21 @@ public void testSetUpOptiBpActivityResultListenerShowsToast() { assertEquals(Toast.LENGTH_SHORT, toast.getDuration()); } + + @Test + public void testGetValueStringReturnsANonNullResult() throws JSONException { + String result = factory.getValueString(resultJson); + JSONArray jsonArray = new JSONArray(result); + assertNotNull(result); + String comperatives = jsonArray.optJSONObject(0).optString("comperatives"); + assertNotNull(comperatives); + JSONObject object = new JSONArray(comperatives).getJSONObject(0); + assertEquals(180, object.optInt("cuffSystolic")); + assertEquals(85, object.optInt("diastolic")); + } + @Test + public void testGetValueStringReturnsANullResult() throws JSONException { + String result = factory.getValueString(null); + assertNull(result); + } } \ No newline at end of file