From f0d0b371b9d7fbc44077f0e31933943d5e30f617 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Thu, 2 Mar 2023 18:51:27 +0500 Subject: [PATCH 1/3] Fix image loading issues --- .../helper/ImageRenderHelper.java | 2 +- .../util/OpenSRPImageLoader.java | 83 +++++++++---------- 2 files changed, 39 insertions(+), 46 deletions(-) diff --git a/opensrp-core/src/main/java/org/smartregister/helper/ImageRenderHelper.java b/opensrp-core/src/main/java/org/smartregister/helper/ImageRenderHelper.java index d9ff6c576..6b2dfb9bb 100644 --- a/opensrp-core/src/main/java/org/smartregister/helper/ImageRenderHelper.java +++ b/opensrp-core/src/main/java/org/smartregister/helper/ImageRenderHelper.java @@ -47,7 +47,7 @@ public void refreshProfileImage(String clientBaseEntityId, ImageView profileImag } profileImageView.setTag(org.smartregister.R.id.entity_id, clientBaseEntityId); - DrishtiApplication.getCachedImageLoaderInstance().getImageByClientId(clientBaseEntityId, OpenSRPImageLoader.getStaticImageListener(profileImageView, 0, 0)); + DrishtiApplication.getCachedImageLoaderInstance().getImageByClientId(clientBaseEntityId, OpenSRPImageLoader.getStaticImageListener(profileImageView, defaultProfileImage, defaultProfileImage)); } } diff --git a/opensrp-core/src/main/java/org/smartregister/util/OpenSRPImageLoader.java b/opensrp-core/src/main/java/org/smartregister/util/OpenSRPImageLoader.java index a86ba1ba8..2778964b9 100644 --- a/opensrp-core/src/main/java/org/smartregister/util/OpenSRPImageLoader.java +++ b/opensrp-core/src/main/java/org/smartregister/util/OpenSRPImageLoader.java @@ -5,18 +5,21 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.widget.ImageView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.load.model.LazyHeaders; -import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; import org.smartregister.AllConstants; import org.smartregister.CoreLibrary; @@ -68,8 +71,7 @@ public OpenSRPImageLoader(Service service, int defaultPlaceHolderResId) { mResources = service.getResources(); mPlaceHolderDrawables = new ArrayList<>(1); - mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null - : mResources.getDrawable(defaultPlaceHolderResId)); + mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId)); } public OpenSRPImageLoader(Context context, int defaultPlaceHolderResId) { @@ -77,8 +79,7 @@ public OpenSRPImageLoader(Context context, int defaultPlaceHolderResId) { mResources = DrishtiApplication.getInstance().getResources(); mPlaceHolderDrawables = new ArrayList<>(1); - mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null - : mResources.getDrawable(defaultPlaceHolderResId)); + mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId)); } /** @@ -89,8 +90,7 @@ public OpenSRPImageLoader(FragmentActivity activity, int defaultPlaceHolderResId this(activity); mPlaceHolderDrawables = new ArrayList<>(1); - mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null - : mResources.getDrawable(defaultPlaceHolderResId)); + mPlaceHolderDrawables.add(defaultPlaceHolderResId == -1 ? null : mResources.getDrawable(defaultPlaceHolderResId)); } /** @@ -140,20 +140,15 @@ public static void saveStaticImageToDisk(String entityId, Bitmap image) { try { if (entityId != null && !entityId.isEmpty()) { - final String absoluteFileName = - DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG"; + final String absoluteFileName = DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG"; File outputFile = new File(absoluteFileName); os = new FileOutputStream(outputFile); - CompressFormat compressFormat = OpenSRPImageLoader - .getCompressFormat(absoluteFileName); + CompressFormat compressFormat = OpenSRPImageLoader.getCompressFormat(absoluteFileName); if (compressFormat != null) { image.compress(compressFormat, 100, os); } else { - throw new IllegalArgumentException( - "Failed to save static image, could " + "not" - + " retrieve image compression format from name " - + absoluteFileName); + throw new IllegalArgumentException("Failed to save static image, could " + "not" + " retrieve image compression format from name " + absoluteFileName); } // insert into the db ProfileImage profileImage = new ProfileImage(); @@ -173,8 +168,7 @@ public static void saveStaticImageToDisk(String entityId, Bitmap image) { try { os.close(); } catch (IOException e) { - Timber.e("Failed to close static images output stream after attempting" - + " to write image"); + Timber.e("Failed to close static images output stream after attempting" + " to write image"); } } } @@ -194,8 +188,7 @@ public static boolean moveSyncedImageAndSaveProfilePic(@NonNull String syncStatu boolean successful = false; if (!entityId.isEmpty()) { - final String absoluteFileName = - DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG"; + final String absoluteFileName = DrishtiApplication.getAppDir() + File.separator + entityId + ".JPEG"; if (copyFile(imageFile, new File(absoluteFileName))) { // insert into the db @@ -205,8 +198,7 @@ public static boolean moveSyncedImageAndSaveProfilePic(@NonNull String syncStatu profileImage.setFilepath(absoluteFileName); profileImage.setFilecategory("profilepic"); profileImage.setSyncStatus(syncStatus); - ImageRepository imageRepo = CoreLibrary.getInstance().context(). - imageRepository(); + ImageRepository imageRepo = CoreLibrary.getInstance().context().imageRepository(); imageRepo.add(profileImage); successful = true; @@ -276,8 +268,7 @@ public void getImageByClientId(String entityId, OpenSRPImageListener opensrpImag try { final Context context = contextWeakReference.get(); if (context != null) { - if (CoreLibrary.getInstance().context().getAppProperties().isTrue(AllConstants.PROPERTY.DISABLE_PROFILE_IMAGES_FEATURE) - || (entityId == null || entityId.isEmpty())) { + if (CoreLibrary.getInstance().context().getAppProperties().isTrue(AllConstants.PROPERTY.DISABLE_PROFILE_IMAGES_FEATURE) || (entityId == null || entityId.isEmpty())) { Glide.with(context).load(opensrpImageListener.getDefaultImageResId()).into(opensrpImageListener.getImageView()); @@ -292,39 +283,41 @@ public void getImageByClientId(String entityId, OpenSRPImageListener opensrpImag if (imageRecord != null) { - Glide.with(context).load(imageRecord.getFilepath()) - .apply(new RequestOptions() - .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)) //Images already on device so skip caching - .transition(DrawableTransitionOptions.withCrossFade(mFadeInImage ? AllConstants.IMAGE_ANIMATION_FADE_IN_TIME : 0)) - .placeholder(opensrpImageListener.getDefaultImageResId()) - .error(opensrpImageListener.getErrorImageResId()) + Glide.with(context).load(imageRecord.getFilepath()).apply(new RequestOptions() + .skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .placeholder(opensrpImageListener.getDefaultImageResId()) + .error(opensrpImageListener.getErrorImageResId())) //Images already on device so skip caching .into(opensrpImageListener.getImageView()); - } else { String url = FileUtilities.getImageUrl(entityId); AccountAuthenticatorXml authenticatorXml = CoreLibrary.getInstance().getAccountAuthenticatorXml(); - String accessToken = AccountHelper.getCachedOAuthToken(CoreLibrary.getInstance().context().allSharedPreferences().fetchRegisteredANM(), - authenticatorXml.getAccountType(), - AccountHelper.TOKEN_TYPE.PROVIDER); - - GlideUrl glideUrl = new GlideUrl(url, - new LazyHeaders.Builder() - .addHeader(AllConstants.HTTP_REQUEST_HEADERS.AUTHORIZATION, new StringBuilder(AllConstants.HTTP_REQUEST_AUTH_TOKEN_TYPE.BEARER + " ") - .append(accessToken).toString()).build()); - - Glide.with(context).load(glideUrl) - .transition(DrawableTransitionOptions.withCrossFade(mFadeInImage ? AllConstants.IMAGE_ANIMATION_FADE_IN_TIME : 0)) - .placeholder(opensrpImageListener.getDefaultImageResId()) - .error(opensrpImageListener.getErrorImageResId()) - .into(opensrpImageListener.getImageView()); + String accessToken = AccountHelper.getCachedOAuthToken(CoreLibrary.getInstance().context().allSharedPreferences().fetchRegisteredANM(), authenticatorXml.getAccountType(), AccountHelper.TOKEN_TYPE.PROVIDER); + + GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder() + .addHeader(AllConstants.HTTP_REQUEST_HEADERS.AUTHORIZATION, new StringBuilder(AllConstants.HTTP_REQUEST_AUTH_TOKEN_TYPE.BEARER + " ") + .append(accessToken).toString()).build()); + Glide.with(context).load(glideUrl).diskCacheStrategy(DiskCacheStrategy.NONE).placeholder(opensrpImageListener.getDefaultImageResId()).error(opensrpImageListener.getErrorImageResId()).into(new CustomTarget() { // Use CustomViewTarget to load original size image + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + opensrpImageListener.getImageView().setImageDrawable(resource); + if (resource instanceof BitmapDrawable) + saveStaticImageToDisk(entityId, ((BitmapDrawable) resource).getBitmap()); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + opensrpImageListener.getImageView().setImageDrawable(placeholder); + } + }); } } } } catch (Exception e) { - Timber.e(e.getMessage(), e); + Timber.e(e); } } From 25d2d6c9c0426d53e3cc679fc2e009b5e4f8df84 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Thu, 2 Mar 2023 18:53:11 +0500 Subject: [PATCH 2/3] Bump up library version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index adc40ab8b..1f85ef9df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=6.1.1-SNAPSHOT +VERSION_NAME=6.1.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application From 17ab225c8a15ed3c4f32cabd7723208d9ba33bb0 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 3 Mar 2023 18:01:49 +0500 Subject: [PATCH 3/3] Increase code coverage --- .../org/smartregister/util/FormUtilsTest.java | 43 ++++++++ .../util/OpenSRPImageLoaderTest.java | 99 +++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 070704950..0b614b40a 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -1,5 +1,7 @@ package org.smartregister.util; +import static org.mockito.ArgumentMatchers.eq; + import android.content.res.AssetManager; import android.util.Xml; @@ -16,7 +18,9 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + import androidx.test.core.app.ApplicationProvider; + import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; @@ -27,6 +31,8 @@ import org.smartregister.repository.FormDataRepository; import org.smartregister.service.ANMService; import org.smartregister.util.mock.XmlSerializerMock; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import java.io.File; import java.io.FileInputStream; @@ -34,6 +40,7 @@ import java.io.InputStream; import java.net.URL; import java.util.HashMap; +import java.util.List; /** * Created by kaderchowdhury on 14/11/17. @@ -198,4 +205,40 @@ public void getJsonFieldFromArrayShouldReturnObjectWithCorrectNameProperty() thr Assert.assertEquals("Doe", resultJson.getString("value")); } + @Test + public void testGetSubForShouldReturnCOrrectValuem() throws JSONException { + String form = "{\"form_data_definition_version\":\"2\",\"form\":{\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp\",\"bind_type\":\"mcaremother\",\"ec_bind_type\":\"ec_mcaremother\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"bnf_current_formStatus\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/bnf_current_formStatus\"},{\"name\":\"relationalid\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/relationalid\",\"shouldLoadValue\":true},{\"name\":\"changes\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/changes\"},{\"name\":\"GOBHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_gobhhid\",\"shouldLoadValue\":true},{\"name\":\"JiVitAHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_jivhhid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMBID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_bid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMNID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_nid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_first_name\",\"shouldLoadValue\":true},{\"name\":\"FWHUSNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_husname\",\"shouldLoadValue\":true},{\"name\":\"FWPSRLMP\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_psrlmp\",\"shouldLoadValue\":true},{\"name\":\"existing_location\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_location\"},{\"name\":\"today\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/today\"},{\"name\":\"start\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/start\"},{\"name\":\"end\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/end\"},{\"name\":\"FWBNFDATE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFDATE\"},{\"name\":\"FWCONFIRMATION\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWCONFIRMATION\"},{\"name\":\"FWGESTATIONALAGE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWGESTATIONALAGE\"},{\"name\":\"FWEDD\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWEDD\"},{\"name\":\"FWBNFSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFSTS\"},{\"name\":\"FWDISPLAYTEXT1\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWDISPLAYTEXT1\"},{\"name\":\"Is_PNC\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/Is_PNC\"},{\"name\":\"user_type\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/user_type\"},{\"name\":\"external_user_ID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/external_user_ID\"},{\"name\":\"FWBNFWOMVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFWOMVITSTS\"},{\"name\":\"FWBNFDTOO\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFDTOO\"},{\"name\":\"FWBNFLB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFLB\"},{\"name\":\"FWBNFSMSRSN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFSMSRSN\"}],\"sub_forms\":[{\"name\":\"child_registration\",\"bind_type\":\"mcarechild\",\"ec_bind_type\":\"ec_mcarechild\",\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"relationalid\",\"shouldLoadValue\":true},{\"name\":\"FWBNFGEN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFGEN\"},{\"name\":\"FWBNFCHLDVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHLDVITSTS\"},{\"name\":\"FWBNFNAMECHECK\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAMECHECK\"},{\"name\":\"FWBNFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAME\"},{\"name\":\"FWBNFCHILDNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHILDNAME\"},{\"name\":\"FWBNFDOB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFDOB\"},{\"name\":\"mother_entity_id\",\"source\":\"mcaremother.id\",\"shouldLoadValue\":true}]}]}}"; + String entity = "{\n" + + " \"name\": \"relationalid\",\n" + + " \"relationalid\": \"123-123\",\n" + + " \"bind\": \"/model/instance/BirthNotificationPregnancyStatusFollowUp/relationalid\",\n" + + " \"shouldLoadValue\": true\n" + + " }"; + String NodeNameValue = ReflectionHelpers.callInstanceMethod(formUtils, "retrieveValueForNodeName" + , ReflectionHelpers.ClassParameter.from(String.class, "relationalid") + , ReflectionHelpers.ClassParameter.from(JSONObject.class, new JSONObject(entity)) + , ReflectionHelpers.ClassParameter.from(JSONObject.class, new JSONObject(form))); + Assert.assertEquals("123-123", NodeNameValue); + } + + @Test + public void testHasChildElementsShouldReturnTrueWhenNodeHasElement() { + Node node = Mockito.mock(Node.class); + Node childNode = Mockito.mock(Node.class); + NodeList nodeList = Mockito.mock(NodeList.class); + Mockito.doReturn(nodeList).when(node).getChildNodes(); + Mockito.doReturn(1).when(nodeList).getLength(); + Mockito.doReturn(childNode).when(nodeList).item(eq(0)); + Mockito.doReturn(Node.ELEMENT_NODE).when(childNode).getNodeType(); + Assert.assertTrue(FormUtils.hasChildElements(node)); + } + + @Test + public void testGetSubFormNamesShouldReturnCorrectSubFormNamesList() throws JSONException { + String form = "{\"form_data_definition_version\":\"2\",\"form\":{\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp\",\"bind_type\":\"mcaremother\",\"ec_bind_type\":\"ec_mcaremother\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"bnf_current_formStatus\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/bnf_current_formStatus\"},{\"name\":\"relationalid\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/relationalid\",\"shouldLoadValue\":true},{\"name\":\"changes\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/changes\"},{\"name\":\"GOBHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_gobhhid\",\"shouldLoadValue\":true},{\"name\":\"JiVitAHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_jivhhid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMBID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_bid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMNID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_nid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_first_name\",\"shouldLoadValue\":true},{\"name\":\"FWHUSNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_husname\",\"shouldLoadValue\":true},{\"name\":\"FWPSRLMP\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_psrlmp\",\"shouldLoadValue\":true},{\"name\":\"existing_location\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_location\"},{\"name\":\"today\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/today\"},{\"name\":\"start\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/start\"},{\"name\":\"end\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/end\"},{\"name\":\"FWBNFDATE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFDATE\"},{\"name\":\"FWCONFIRMATION\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWCONFIRMATION\"},{\"name\":\"FWGESTATIONALAGE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWGESTATIONALAGE\"},{\"name\":\"FWEDD\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWEDD\"},{\"name\":\"FWBNFSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFSTS\"},{\"name\":\"FWDISPLAYTEXT1\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWDISPLAYTEXT1\"},{\"name\":\"Is_PNC\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/Is_PNC\"},{\"name\":\"user_type\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/user_type\"},{\"name\":\"external_user_ID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/external_user_ID\"},{\"name\":\"FWBNFWOMVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFWOMVITSTS\"},{\"name\":\"FWBNFDTOO\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFDTOO\"},{\"name\":\"FWBNFLB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFLB\"},{\"name\":\"FWBNFSMSRSN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFSMSRSN\"}],\"sub_forms\":[{\"name\":\"child_registration\",\"bind_type\":\"mcarechild\",\"ec_bind_type\":\"ec_mcarechild\",\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"relationalid\",\"shouldLoadValue\":true},{\"name\":\"FWBNFGEN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFGEN\"},{\"name\":\"FWBNFCHLDVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHLDVITSTS\"},{\"name\":\"FWBNFNAMECHECK\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAMECHECK\"},{\"name\":\"FWBNFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAME\"},{\"name\":\"FWBNFCHILDNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHILDNAME\"},{\"name\":\"FWBNFDOB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFDOB\"},{\"name\":\"mother_entity_id\",\"source\":\"mcaremother.id\",\"shouldLoadValue\":true}]}]}}"; + + List subFormNames = ReflectionHelpers.callInstanceMethod(formUtils, "getSubFormNames" + , ReflectionHelpers.ClassParameter.from(JSONObject.class, new JSONObject(form))); + Assert.assertEquals(1, subFormNames.size()); + } } diff --git a/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java b/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java index 3972c6b1f..78ee1b28a 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java @@ -1,22 +1,39 @@ package org.smartregister.util; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; +import android.app.Service; import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; import androidx.test.core.app.ApplicationProvider; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import org.robolectric.Robolectric; import org.robolectric.android.controller.ActivityController; import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.domain.ProfileImage; +import org.smartregister.domain.SyncStatus; +import org.smartregister.repository.ImageRepository; import org.smartregister.util.mock.OpenSRPImageLoaderTestActivity; +import org.smartregister.view.activity.DrishtiApplication; + +import java.io.File; +import java.io.IOException; /** * Created by kaderchowdhury on 14/11/17. @@ -33,6 +50,8 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest { private ActivityController controller; + private File testFile; + @Before public void setUp() throws Exception { Intent intent = new Intent(ApplicationProvider.getApplicationContext(), OpenSRPImageLoaderTestActivity.class); @@ -59,4 +78,84 @@ public void assertFragmentActivityConstructorInitializationNotNull() throws Exce OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(activity, -1); Assert.assertNotNull(openSRPImageLoader); } + + @Test + public void testGetCompressFormatShouldReturnProperCompressFormat() { + String filePath = "/User/images/file.jpg"; + Bitmap.CompressFormat format = ReflectionHelpers.callStaticMethod(OpenSRPImageLoader.class, "getCompressFormat", + ReflectionHelpers.ClassParameter.from(String.class, filePath)); + Assert.assertEquals(Bitmap.CompressFormat.JPEG, format); + + filePath = "/User/images/file.png"; + format = ReflectionHelpers.callStaticMethod(OpenSRPImageLoader.class, "getCompressFormat", + ReflectionHelpers.ClassParameter.from(String.class, filePath)); + Assert.assertEquals(Bitmap.CompressFormat.PNG, format); + + } + + @Test + public void testMoveSyncedImageAndSaveProfilePicReturnsSuccessful() throws IOException { + String path = DrishtiApplication.getAppDir() + File.separator + "123-123.JPEG"; + testFile = new File(path); + testFile.createNewFile(); + + CoreLibrary library = Mockito.mock(CoreLibrary.class); + ReflectionHelpers.setField(CoreLibrary.getInstance(), "instance", library); + Context context = Mockito.mock(Context.class); + doReturn(context).when(library).context(); + ImageRepository imageRepository = Mockito.mock(ImageRepository.class); + doNothing().when(imageRepository).add(any()); + doReturn(imageRepository).when(context).imageRepository(); + + + boolean success = OpenSRPImageLoader.moveSyncedImageAndSaveProfilePic(SyncStatus.SYNCED.value(), "123-123", testFile); + Assert.assertTrue(success); + } + + @Test + public void testSaveStaticImageToDisk() throws IOException { + Bitmap bitmap = Mockito.mock(Bitmap.class); + doReturn(true).when(bitmap).compress(any(), anyInt(), any()); + + CoreLibrary library = Mockito.mock(CoreLibrary.class); + ReflectionHelpers.setField(CoreLibrary.getInstance(), "instance", library); + Context context = Mockito.mock(Context.class); + doReturn(context).when(library).context(); + ImageRepository imageRepository = Mockito.mock(ImageRepository.class); + doNothing().when(imageRepository).add(any()); + doReturn(imageRepository).when(context).imageRepository(); + + OpenSRPImageLoader.saveStaticImageToDisk( "123-123", bitmap); + Mockito.verify(imageRepository).add(any(ProfileImage.class)); + } + + @Test + public void testSetMaxImageSizeShouldSetCorrectImageSize() { + OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(activity.getInstance()); + openSRPImageLoader.setMaxImageSize(200); + int width = ReflectionHelpers.getField(openSRPImageLoader, "mMaxImageWidth"); + int height = ReflectionHelpers.getField(openSRPImageLoader, "mMaxImageHeight"); + Assert.assertEquals(200, width); + Assert.assertEquals(200, height); + } + + @Test + public void testConstructorWithServiceInitializedCorrectly() { + Service service = Mockito.mock(Service.class); + android.content.Context context = Mockito.mock(android.content.Context.class); + Resources resources = Mockito.mock(Resources.class); + doReturn(context).when(service).getApplicationContext(); + doReturn(resources).when(service).getResources(); + doReturn(Mockito.mock(Drawable.class)).when(resources).getDrawable(eq(0)); + OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(service, 0); + Assert.assertNotNull(ReflectionHelpers.getField(openSRPImageLoader, "contextWeakReference")); + Assert.assertNotNull(ReflectionHelpers.getField(openSRPImageLoader, "mPlaceHolderDrawables")); + } + + @After + public void destroy() { + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "imageRepository", null); + if (testFile != null && testFile.exists()) + testFile.delete(); + } }