From 53d7e1195ec9a73b6f19d17d3f71b4e497889a3e Mon Sep 17 00:00:00 2001 From: Denis Rwelamila Date: Tue, 2 Jul 2019 09:27:55 +0300 Subject: [PATCH] Test for malaria due state --- gradle.properties | 2 +- opensrp-chw-malaria/build.gradle | 1 + .../activity/BaseMalariaProfileActivity.java | 46 ++++++++----------- .../BaseMalariaProfilePresenter.java | 26 +++++++++++ .../BaseMalariaProfilePresenterTest.java | 31 +++++++++++++ 5 files changed, 78 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index 96d8bb7..63344cd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.1.9-SNAPSHOT +VERSION_NAME=1.0.0-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client CHW Malaria Library diff --git a/opensrp-chw-malaria/build.gradle b/opensrp-chw-malaria/build.gradle index a3ec19e..f4d368c 100644 --- a/opensrp-chw-malaria/build.gradle +++ b/opensrp-chw-malaria/build.gradle @@ -111,6 +111,7 @@ dependencies { testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.+' + testImplementation 'org.robolectric:robolectric:3.8' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java index 9f0b997..7be1ddb 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java @@ -20,11 +20,17 @@ import org.smartregister.malaria.R; import org.smartregister.view.activity.BaseProfileActivity; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + public class BaseMalariaProfileActivity extends BaseProfileActivity implements MalariaProfileContract.View { protected MemberObject MEMBER_OBJECT; private BaseMalariaProfilePresenter profilePresenter; - private TextView textViewName, textViewGender, textViewLocation, textViewUniqueID; + private TextView textViewName, textViewGender, textViewLocation, textViewUniqueID, textViewRecordMalaria; + private View recordMalariaView; public static void startProfileActivity(Activity activity, MemberObject memberObject) { Intent intent = new Intent(activity, BaseMalariaProfileActivity.class); @@ -60,6 +66,8 @@ public void onClick(View v) { textViewGender = findViewById(R.id.textview_gender); textViewLocation = findViewById(R.id.textview_address); textViewUniqueID = findViewById(R.id.textview_id); + textViewRecordMalaria = findViewById(R.id.textview_record_malaria); + recordMalariaView = findViewById(R.id.record_visit_malaria); MEMBER_OBJECT = (MemberObject) getIntent().getSerializableExtra(Constants.MALARIA_MEMBER_OBJECT.MEMBER_OBJECT); @@ -79,27 +87,17 @@ public void setProfileViewWithData() { textViewLocation.setText(MEMBER_OBJECT.getAddress()); textViewUniqueID.setText(MEMBER_OBJECT.getUniqueId()); - recordMalariaButton(MEMBER_OBJECT.getMalariaTestDate()); - - } - - private void recordMalariaButton(String malaria_test_date) { - try { - Date date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).parse(malaria_test_date); - int malaria_test_date_processed = new Period(new DateTime(date), new DateTime()).getDays(); - if(malaria_test_date_processed >= 7 && malaria_test_date_processed < 10) { - //do nothing - //malaria follow up is overdue - textViewRecordMalaria.setBackgroundColor(ContextCompat.getColor(context, R.color.due_profile_blue)); - } else if(malaria_test_date_processed >= 10) { - //malaria follow up is overdue - textViewRecordMalaria.setBackgroundColor(ContextCompat.getColor(context, R.color.visit_status_over_due)); - } else { - recordMalariaView.setVisibility(View.GONE); + if (MEMBER_OBJECT.getMalariaTestDate() != null) { + try { + Date date = + new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).parse((MEMBER_OBJECT.getMalariaTestDate())); + int malaria_test_date_processed = new Period(new DateTime(date), new DateTime()).getDays(); + profilePresenter.recordMalariaButton(malaria_test_date_processed, textViewRecordMalaria, this); + } catch (ParseException e) { + e.printStackTrace(); } - } catch (ParseException e) { - e.printStackTrace(); } + } @@ -124,13 +122,7 @@ public void onClick(View view) { if (id == R.id.title_layout) { onBackPressed(); } else if (id == R.id.textview_record_malaria) { - try { - Date date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).parse(MEMBER_OBJECT.getMalariaTestDate()); - int malaria_test_date_processed = new Period(new DateTime(date), new DateTime()).getDays(); - Toast.makeText(context, "Record Malaria" + malaria_test_date_processed, Toast.LENGTH_SHORT).show(); - } catch (ParseException e) { - e.printStackTrace(); - } + profilePresenter.recordMalariaFollowUp(this); } } diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java index 19d2419..059a47a 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java @@ -1,9 +1,14 @@ package org.smartregister.chw.malaria.presenter; import android.content.Context; +import android.graphics.Color; +import android.support.v4.content.ContextCompat; +import android.view.View; +import android.widget.Toast; import org.smartregister.chw.malaria.contract.MalariaProfileContract; import org.smartregister.chw.malaria.domain.MemberObject; +import org.smartregister.malaria.R; public class BaseMalariaProfilePresenter { @@ -31,4 +36,25 @@ public void fillProfileData(MemberObject memberObject) { view.setProfileViewWithData(); } } + + public void recordMalariaButton(int days_from_malaria_test_date, View view, Context context) { + if(days_from_malaria_test_date >= 7 && days_from_malaria_test_date < 10) { + changeViewColor(view, context, R.color.due_profile_blue); + } else if (days_from_malaria_test_date >= 10){ + changeViewColor(view, context, R.color.visit_status_over_due); + } + } + + + public void recordMalariaFollowUp(Context context) { + //call the form from this function + Toast.makeText(context, "Record Malaria", Toast.LENGTH_SHORT).show(); + } + + public void changeViewColor(View view, Context context, int color) { + view.setBackgroundColor(ContextCompat.getColor(context, + color)); + + } + } diff --git a/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java b/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java index 9520354..75d8b7b 100644 --- a/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java +++ b/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java @@ -1,11 +1,21 @@ package org.smartregister.presenter; +import android.content.Context; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.view.View; +import android.widget.TextView; + import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; +import static org.junit.Assert.*; + +import org.robolectric.RuntimeEnvironment; import org.smartregister.chw.malaria.contract.MalariaProfileContract; import org.smartregister.chw.malaria.domain.MemberObject; import org.smartregister.chw.malaria.presenter.BaseMalariaProfilePresenter; +import org.smartregister.malaria.R; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -14,14 +24,35 @@ public class BaseMalariaProfilePresenterTest { @Mock private MalariaProfileContract.View view = Mockito.mock(MalariaProfileContract.View.class); @Mock + private View androidView = Mockito.mock(View.class); + @Mock BaseMalariaProfilePresenter profilePresenter = Mockito.mock(BaseMalariaProfilePresenter.class); @Mock MemberObject memberObject = Mockito.mock(MemberObject.class); + @Mock + private Context context = Mockito.mock(Context.class); + @Test public void fillProfileData_doesntCallsSetProfileViewWithDataIfMemberObjectEmpty() { profilePresenter.fillProfileData(memberObject); verify(view, never()).setProfileViewWithData(); } + + @Test + public void malariaTestDatePeriodIsBetweenSevenAndTenSevenInclusive_recordMalariaButtonHasDueColor() { + profilePresenter.recordMalariaButton(7, androidView, context); + Drawable recordMalariaButton = Mockito.spy(new TextView(RuntimeEnvironment.application)).getBackground(); + ColorDrawable recordMalariaButtonColor = (ColorDrawable) recordMalariaButton; + assertEquals(R.color.due_profile_blue, recordMalariaButtonColor.getColor()); + } + + @Test + public void malariaTestDatePeriodIsTenOrGreater_recordMalariaButtonHasOverDueColor() { + profilePresenter.recordMalariaButton(10, androidView, context); + Drawable recordMalariaButton = Mockito.spy(new TextView(RuntimeEnvironment.application)).getBackground(); + ColorDrawable recordMalariaButtonColor = (ColorDrawable) recordMalariaButton; + assertEquals(R.color.visit_status_over_due, recordMalariaButtonColor.getColor()); + } }