From 74cb4ceacc6a01029aa2b5b88a8d8d680b41d05e Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 4 May 2021 12:40:36 +0300 Subject: [PATCH] :white_check_mark: Add CoreMalariaProfileActivity tests --- .../CoreMalariaProfileActivityTest.java | 89 ++++++++++++++++++- .../impl/CoreMalariaProfileActivityImpl.java | 2 +- ...amilyOtherMemberActivityPresenterImpl.java | 35 ++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/FamilyOtherMemberActivityPresenterImpl.java diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/CoreMalariaProfileActivityTest.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/CoreMalariaProfileActivityTest.java index b4959250cf..a08ecaaa83 100644 --- a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/CoreMalariaProfileActivityTest.java +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/CoreMalariaProfileActivityTest.java @@ -1,5 +1,10 @@ package org.smartregister.chw.core.activity; +import android.app.Activity; +import android.content.Intent; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.widget.ProgressBar; import android.widget.RelativeLayout; @@ -10,6 +15,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -17,18 +23,28 @@ import org.mockito.junit.MockitoRule; import org.robolectric.Robolectric; import org.robolectric.android.controller.ActivityController; +import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.chw.core.BaseUnitTest; import org.smartregister.chw.core.R; import org.smartregister.chw.core.activity.impl.CoreMalariaProfileActivityImpl; +import org.smartregister.chw.core.activity.impl.FamilyOtherMemberActivityPresenterImpl; +import org.smartregister.chw.core.presenter.CoreFamilyOtherMemberActivityPresenter; import org.smartregister.chw.core.presenter.CoreMalariaMemberProfilePresenter; +import org.smartregister.chw.core.shadows.UtilsShadowUtil; +import org.smartregister.chw.core.utils.CoreConstants; import org.smartregister.chw.malaria.domain.MemberObject; import org.smartregister.chw.malaria.presenter.BaseMalariaProfilePresenter; +import org.smartregister.family.domain.FamilyMetadata; +import org.smartregister.family.util.Constants; +import org.smartregister.family.util.JsonFormUtils; import timber.log.Timber; + +@Config(shadows = {UtilsShadowUtil.class}) public class CoreMalariaProfileActivityTest extends BaseUnitTest { @Rule @@ -81,11 +97,82 @@ public void initNotificationReferralRecyclerViewInitsCorrectLayouts() { public void initialisingPresenterRefreshesProfileBottomSection() { activity = Mockito.spy(activity); profilePresenter = Mockito.spy(Mockito.mock(CoreMalariaMemberProfilePresenter.class)); - Mockito.when(activity.getProfilePresenter()).thenReturn((CoreMalariaMemberProfilePresenter)profilePresenter); + Mockito.when(activity.getProfilePresenter()).thenReturn((CoreMalariaMemberProfilePresenter) profilePresenter); activity.initializePresenter(); Mockito.verify(profilePresenter, Mockito.times(1)).refreshProfileBottom(); } + /*@Test + public void changeCompletedActivityResultStartsFamilyProfileActivity() { + activity = Mockito.spy(activity); + int resultCode = Activity.RESULT_OK; + int requestCode = CoreConstants.ProfileActivityResults.CHANGE_COMPLETED; + Intent data = Mockito.mock(Intent.class); + + activity.onActivityResult(requestCode, resultCode, data); + + Mockito.verify(activity, Mockito.times(1)).startActivity(ArgumentMatchers.any(Intent.class)); + Mockito.verify(activity, Mockito.times(1)).finish(); + }*/ + + @Test + public void getJsonActivityResultUpdatesFamilyIfEncounterIsUpdate() { + activity = Mockito.spy(activity); + + int resultCode = Activity.RESULT_OK; + int requestCode = JsonFormUtils.REQUEST_CODE_GET_JSON; + + Intent data = Mockito.mock(Intent.class); + String json = "{\"encounter_type\": \"Update Family Member\"}"; + Mockito.doReturn(json).when(data).getStringExtra(Constants.JSON_FORM_EXTRA.JSON); + + FamilyMetadata metadata = Mockito.mock(FamilyMetadata.class, Mockito.CALLS_REAL_METHODS); + metadata.updateFamilyMemberRegister("family_member_register", + "tableName", + "Register Family Member", + "Update Family Member", + "config", + "familyRelationKey"); + + UtilsShadowUtil.setMetadata(metadata); + + CoreFamilyOtherMemberActivityPresenter memberActivityPresenter = Mockito.spy(Mockito.mock(FamilyOtherMemberActivityPresenterImpl.class)); + Mockito.when(activity.presenter()).thenReturn(memberActivityPresenter); + + activity.onActivityResult(requestCode, resultCode, data); + Mockito.verify(memberActivityPresenter, Mockito.times(1)). + updateFamilyMember( + ArgumentMatchers.eq(activity), + ArgumentMatchers.eq(json), + ArgumentMatchers.eq(false)); + } + + @Test + public void menuIsInflatedOnCreateOptionsMenu() { + activity = Mockito.spy(activity); + MenuInflater inflater = Mockito.spy(Mockito.mock(MenuInflater.class)); + Mockito.when(activity.getMenuInflater()).thenReturn(inflater); + Menu menu = Mockito.mock(Menu.class); + activity.onCreateOptionsMenu(menu); + Mockito.verify(inflater, Mockito.times(1)).inflate(R.menu.malaria_profile_menu, menu); + } + + + @Test + public void optionIsHandledOnOptionsItemSelected() { + activity = Mockito.spy(activity); + MenuItem menuItem = Mockito.mock(MenuItem.class); + + Mockito.doNothing().when(activity).startFormForEdit(R.string.registration_info, + CoreConstants.JSON_FORM.FAMILY_MEMBER_REGISTER); + + Mockito.doReturn(R.id.action_registration).when(menuItem).getItemId(); + activity.onOptionsItemSelected(menuItem); + Mockito.verify(activity).startFormForEdit(R.string.registration_info, + CoreConstants.JSON_FORM.FAMILY_MEMBER_REGISTER); + + } + @After public void tearDown() { try { diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/CoreMalariaProfileActivityImpl.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/CoreMalariaProfileActivityImpl.java index 01f4a3575d..b6df2e5406 100644 --- a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/CoreMalariaProfileActivityImpl.java +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/CoreMalariaProfileActivityImpl.java @@ -23,7 +23,7 @@ protected void removeMember() { @NonNull @Override public CoreFamilyOtherMemberActivityPresenter presenter() { - return Mockito.mock(CoreFamilyOtherMemberActivityPresenter.class); + return Mockito.mock(FamilyOtherMemberActivityPresenterImpl.class); } @Override diff --git a/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/FamilyOtherMemberActivityPresenterImpl.java b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/FamilyOtherMemberActivityPresenterImpl.java new file mode 100644 index 0000000000..bf8d1d4a47 --- /dev/null +++ b/opensrp-chw-core/src/test/java/org/smartregister/chw/core/activity/impl/FamilyOtherMemberActivityPresenterImpl.java @@ -0,0 +1,35 @@ +package org.smartregister.chw.core.activity.impl; + +import android.content.Context; + +import org.smartregister.chw.core.contract.FamilyOtherMemberProfileExtendedContract; +import org.smartregister.chw.core.interactor.CoreFamilyProfileInteractor; +import org.smartregister.chw.core.presenter.CoreFamilyOtherMemberActivityPresenter; +import org.smartregister.family.contract.FamilyOtherMemberContract; +import org.smartregister.family.contract.FamilyProfileContract; + +public class FamilyOtherMemberActivityPresenterImpl extends CoreFamilyOtherMemberActivityPresenter { + public FamilyOtherMemberActivityPresenterImpl(FamilyOtherMemberProfileExtendedContract.View view, FamilyOtherMemberContract.Model model, String viewConfigurationIdentifier, String familyBaseEntityId, String baseEntityId, String familyHead, String primaryCaregiver, String villageTown, String familyName) { + super(view, model, viewConfigurationIdentifier, familyBaseEntityId, baseEntityId, familyHead, primaryCaregiver, villageTown, familyName); + } + + @Override + protected CoreFamilyProfileInteractor getFamilyProfileInteractor() { + return null; + } + + @Override + protected FamilyProfileContract.Model getFamilyProfileModel(String familyName) { + return null; + } + + @Override + protected void setProfileInteractor() { + // Do nothing + } + + @Override + public void updateFamilyMember(Context context, String jsonString, boolean isIndependent) { + // Do nothing + } +}