From 61b6a7a1bec06ebb92595c8e87a4fd2df239286c Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Wed, 19 Jan 2022 17:24:33 +0500 Subject: [PATCH 1/5] Increase memory heap size --- opensrp-child/build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/opensrp-child/build.gradle b/opensrp-child/build.gradle index 94904fcee..4c77df07c 100644 --- a/opensrp-child/build.gradle +++ b/opensrp-child/build.gradle @@ -79,6 +79,9 @@ android { includeAndroidResources = true returnDefaultValues = true } + unitTests.all { + jvmArgs "-Xmx4g" + } } compileOptions { From 2dd3edcede3a397092374142b9569ccda1172014 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Wed, 19 Jan 2022 17:25:56 +0500 Subject: [PATCH 2/5] Fix few memory leaks --- .../child/view/BCGNotificationDialogTest.java | 18 +++++++++++++++++- .../AdvanceSearchDatePickerDialogTest.java | 11 +++++++++-- .../widgets/ChildEditTextFactoryTest.java | 3 +-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/opensrp-child/src/test/java/org/smartregister/child/view/BCGNotificationDialogTest.java b/opensrp-child/src/test/java/org/smartregister/child/view/BCGNotificationDialogTest.java index ca8352a67..eab0cd279 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/view/BCGNotificationDialogTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/view/BCGNotificationDialogTest.java @@ -7,6 +7,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -15,6 +16,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) @@ -29,11 +31,14 @@ public class BCGNotificationDialogTest { @Mock private DialogInterface.OnClickListener negativeDialogListener; - private final Activity activity = Robolectric.setupActivity(AppCompatActivity.class); + private Activity activity; + private ActivityController controller; @Before public void setUp() { MockitoAnnotations.initMocks(this); + controller = Robolectric.buildActivity(AppCompatActivity.class).create().resume(); + activity = controller.get(); bcgNotificationDialog = new BCGNotificationDialog(activity, positiveDialogListener, negativeDialogListener); } @@ -50,4 +55,15 @@ public void testThatAlertDialogIsShown() { Assert.assertEquals(negativeButton.getText().toString(), "DISMISS"); Assert.assertEquals(String.valueOf(negativeButton.getTextSize()), String.valueOf(20f)); } + + @After + public void tearDown() { + try { + bcgNotificationDialog.getAlertDialog().dismiss(); + controller.pause().stop().destroy(); + activity.finish(); + } catch (Exception e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/opensrp-child/src/test/java/org/smartregister/child/widgets/AdvanceSearchDatePickerDialogTest.java b/opensrp-child/src/test/java/org/smartregister/child/widgets/AdvanceSearchDatePickerDialogTest.java index 88d4cc26d..fec85b534 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/widgets/AdvanceSearchDatePickerDialogTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/widgets/AdvanceSearchDatePickerDialogTest.java @@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -23,14 +24,15 @@ public class AdvanceSearchDatePickerDialogTest { private AdvanceSearchDatePickerDialog datePickerDialog; + private AppCompatActivity startActivity; @Before public void setUp() { - AppCompatActivity startActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get(); + startActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get(); ChildLibrary childLibrary = Mockito.mock(ChildLibrary.class); Context context = Mockito.mock(Context.class); AllSharedPreferences allSharedPreferences = Mockito.mock(AllSharedPreferences.class); - AppProperties appProperties = Mockito.spy(AppProperties.class); + AppProperties appProperties = Mockito.mock(AppProperties.class); Mockito.doReturn(context).when(childLibrary).context(); Mockito.doReturn(allSharedPreferences).when(context).allSharedPreferences(); Mockito.doReturn(appProperties).when(childLibrary).getProperties(); @@ -44,4 +46,9 @@ public void testGetDatePickerDialog() { datePickerDialog.showDialog(); Assert.assertNotNull(datePickerDialog.getDatePickerDialog()); } + + @After + public void tearDown() { + startActivity.finish(); + } } \ No newline at end of file diff --git a/opensrp-child/src/test/java/org/smartregister/child/widgets/ChildEditTextFactoryTest.java b/opensrp-child/src/test/java/org/smartregister/child/widgets/ChildEditTextFactoryTest.java index 0197c267c..5fde4cb3b 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/widgets/ChildEditTextFactoryTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/widgets/ChildEditTextFactoryTest.java @@ -14,7 +14,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.mockito.Spy; public class ChildEditTextFactoryTest { @@ -23,7 +22,7 @@ public class ChildEditTextFactoryTest { @Mock private JsonFormFragment formFragment; - @Spy + @Mock private JsonFormActivity formActivity; private MaterialEditText materialEditText; From 90064802ddce93de8e001472b009c2b2b785e42f Mon Sep 17 00:00:00 2001 From: Roy Munge Date: Tue, 15 Feb 2022 11:32:41 +0300 Subject: [PATCH 3/5] Update to latest version 0.7.3-ALPHA3-SNAPSHOT --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index bd99fbe08..681fcad5a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=0.7.3-SNAPSHOT +VERSION_NAME=0.7.3-ALPHA3-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Child Library From 7f86fb22643a6a0ad945dd0258966b8e84912b3e Mon Sep 17 00:00:00 2001 From: Roy Munge Date: Tue, 15 Feb 2022 17:10:34 +0300 Subject: [PATCH 4/5] Fix memory leaks Finish Activities --- .../activity/BaseChildFormActivityTest.java | 7 +++++ .../fragment/ChildUnderFiveFragmentTest.java | 11 ++++++- .../ExtraVaccineEditDialogFragmentTest.java | 10 ++++++ .../fragment/NoMatchDialogFragmentTest.java | 14 ++++++++- .../StatusEditDialogFragmentTest.java | 31 ++++++++++++++----- .../child/util/VaccineCalculatorTest.java | 6 ++-- .../child/watchers/LookUpTextWatcherTest.java | 9 +++++- 7 files changed, 73 insertions(+), 15 deletions(-) diff --git a/opensrp-child/src/test/java/org/smartregister/child/activity/BaseChildFormActivityTest.java b/opensrp-child/src/test/java/org/smartregister/child/activity/BaseChildFormActivityTest.java index bcaaf75ad..26aa32156 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/activity/BaseChildFormActivityTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/activity/BaseChildFormActivityTest.java @@ -64,6 +64,13 @@ public void setUp() { @After public void tearDown() { ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", null); + + try { + childFormActivity.finish(); + } catch (Exception e){ + e.printStackTrace(); + } + } @Test diff --git a/opensrp-child/src/test/java/org/smartregister/child/fragment/ChildUnderFiveFragmentTest.java b/opensrp-child/src/test/java/org/smartregister/child/fragment/ChildUnderFiveFragmentTest.java index 12298117b..2c8498d2f 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/fragment/ChildUnderFiveFragmentTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/fragment/ChildUnderFiveFragmentTest.java @@ -12,6 +12,7 @@ import org.apache.commons.lang3.tuple.Triple; import org.joda.time.DateTime; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -76,6 +77,15 @@ public void setUp() { boosterImmunizationsLayout = new LinearLayout(appCompatActivity); } + @After + public void tearDown(){ + try { + appCompatActivity.finish(); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test public void testNewInstance() { Bundle bundle = new Bundle(); @@ -206,7 +216,6 @@ public void testThatLayoutIsNotChangedAfterUndoingVaccine(){ ChildUnderFiveFragment fragment = initFragment(); fragment.onVaccineUpdated(new ExtraVaccineUpdateEvent(entityId, vaccine, vaccineDate, true)); assertEquals(((ViewGroup) boosterImmunizationsLayout).getChildCount(), 0); - } private ChildUnderFiveFragment initFragment() { diff --git a/opensrp-child/src/test/java/org/smartregister/child/fragment/ExtraVaccineEditDialogFragmentTest.java b/opensrp-child/src/test/java/org/smartregister/child/fragment/ExtraVaccineEditDialogFragmentTest.java index b84878006..023aaf259 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/fragment/ExtraVaccineEditDialogFragmentTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/fragment/ExtraVaccineEditDialogFragmentTest.java @@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatActivity; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; @@ -50,6 +51,15 @@ public void setUp() { extraVaccineFragment = ExtraVaccineEditDialogFragment.newInstance(); } + @After + public void tearDown(){ + try { + activity.finish(); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test @Ignore("TODO: Resolve out of memory error") public void testThatViewsAreInitializedCorrectly() { diff --git a/opensrp-child/src/test/java/org/smartregister/child/fragment/NoMatchDialogFragmentTest.java b/opensrp-child/src/test/java/org/smartregister/child/fragment/NoMatchDialogFragmentTest.java index 614df0463..45473cf87 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/fragment/NoMatchDialogFragmentTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/fragment/NoMatchDialogFragmentTest.java @@ -11,6 +11,7 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -63,6 +64,8 @@ public class NoMatchDialogFragmentTest extends BaseUnitTest { private Context context; + private AppCompatActivity activity; + @Before public void setUp() { MockitoAnnotations.initMocks(this); @@ -70,10 +73,19 @@ public void setUp() { fragment = new NoMatchDialogFragment(baseRegisterActivity, "123"); context = RuntimeEnvironment.application; - AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get(); + activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get(); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Tasks").commit(); } + @After + public void tearDown(){ + try { + activity.finish(); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test public void testInstantiation() { assertNotNull(Whitebox.getInternalState(fragment,"uniqueId")); diff --git a/opensrp-child/src/test/java/org/smartregister/child/fragment/StatusEditDialogFragmentTest.java b/opensrp-child/src/test/java/org/smartregister/child/fragment/StatusEditDialogFragmentTest.java index 6e09526a2..13582493a 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/fragment/StatusEditDialogFragmentTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/fragment/StatusEditDialogFragmentTest.java @@ -2,6 +2,7 @@ import androidx.appcompat.app.AppCompatActivity; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -32,11 +33,28 @@ public class StatusEditDialogFragmentTest extends BaseUnitTest { private StatusEditDialogFragment fragment; + private AppCompatActivity appCompatActivity; + + private TestAppCompactActivity testAppCompactActivity; + + @Before public void setUp() { + appCompatActivity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get(); + testAppCompactActivity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get(); MockitoAnnotations.initMocks(this); } + @After + public void tearDown(){ + try { + appCompatActivity.finish(); + testAppCompactActivity.finish(); + } catch (Exception e){ + e.printStackTrace(); + } + } + @Test public void testInstantiation() { Map details = new HashMap<>(); @@ -60,8 +78,7 @@ public void testOnAttachWithCorrectActivity() { assertNull(Whitebox.getInternalState(fragment, "listener")); - TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get(); - fragment.show(activity.getFragmentManager(), "DIALOG_TAG"); + fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG"); assertNotNull(Whitebox.getInternalState(fragment, "listener")); } @@ -77,8 +94,7 @@ public void testOnAttachWithIncorrectActivityThrowsException() { assertNull(Whitebox.getInternalState(fragment, "listener")); - AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get(); - fragment.show(activity.getFragmentManager(), "DIALOG_TAG"); + fragment.show(appCompatActivity.getFragmentManager(), "DIALOG_TAG"); } @Test @@ -92,8 +108,7 @@ public void testOnCreateViewWhenLostToFollowUpIsSet() { fragment = StatusEditDialogFragment.newInstance(detailsMock); - TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get(); - fragment.show(activity.getFragmentManager(), "DIALOG_TAG"); + fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG"); Mockito.verify(detailsMock).containsKey(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP); Mockito.verify(detailsMock, Mockito.atLeastOnce()).get(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP); @@ -110,10 +125,10 @@ public void testOnCreateViewWhenInactiveAndLostToFollowUpAreNotSet() { fragment = StatusEditDialogFragment.newInstance(detailsMock); - TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get(); - fragment.show(activity.getFragmentManager(), "DIALOG_TAG"); + fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG"); Mockito.verify(detailsMock, Mockito.never()).get(Constants.CHILD_STATUS.INACTIVE); Mockito.verify(detailsMock, Mockito.never()).get(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP); } + } diff --git a/opensrp-child/src/test/java/org/smartregister/child/util/VaccineCalculatorTest.java b/opensrp-child/src/test/java/org/smartregister/child/util/VaccineCalculatorTest.java index 236a5a84c..7f7bc4bca 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/util/VaccineCalculatorTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/util/VaccineCalculatorTest.java @@ -55,8 +55,7 @@ public class VaccineCalculatorTest extends BaseUnitTest { public void setUp() { MockitoAnnotations.initMocks(this); mockImmunizationLibrary(immunizationLibrary, context, vaccineRepository, alertService); - Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD) - ; + Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD); } @Test @@ -199,7 +198,6 @@ private void mockImmunicationLibraryToUseHardcodedVaccines() { ImmunizationLibrary immunizationLibrary = Mockito.mock(ImmunizationLibrary.class); ReflectionHelpers.setStaticField(ImmunizationLibrary.class, "instance", immunizationLibrary); - Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD) - ; + Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD); } } \ No newline at end of file diff --git a/opensrp-child/src/test/java/org/smartregister/child/watchers/LookUpTextWatcherTest.java b/opensrp-child/src/test/java/org/smartregister/child/watchers/LookUpTextWatcherTest.java index 8ccd8f866..bd665ee05 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/watchers/LookUpTextWatcherTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/watchers/LookUpTextWatcherTest.java @@ -38,6 +38,8 @@ public class LookUpTextWatcherTest extends BaseUnitTest { private JsonFormFragment formFragment; + private ChildFormActivityShadow childFormActivityShadow; + @Mock private ChildLibrary childLibrary; @@ -63,7 +65,7 @@ public void setUp() throws JSONException { ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", childLibrary); formFragment = Mockito.spy(ChildFormFragment.class); - ChildFormActivityShadow childFormActivityShadow = Robolectric.buildActivity(ChildFormActivityShadow.class).get(); + childFormActivityShadow = Robolectric.buildActivity(ChildFormActivityShadow.class).get(); String formJson = "{\"count\":\"1\",\"encounter_type\":\"Birth Registration\",\"step1\":{\"title\":\"{{child_enrollment.step1.title}}\"," + "\"fields\":[{\"key\":\"first_name\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"person\",\"openmrs_entity_id\":\"first_name\"," + "\"entity_id\":\"mother\",\"look_up\":\"true\",\"type\":\"edit_text\",\"hint\":\"First name\",\"edit_type\":\"name\"}," + @@ -135,6 +137,11 @@ public void testAfterTextChangedShouldNotLookUpAfterIfAfterLookUpIsTrue() { @After public void tearDown() { ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", null); + try { + childFormActivityShadow.finish(); + } catch (Exception e) { + e.printStackTrace(); + } } } \ No newline at end of file From 0b0b8b7b1021688fa5674b854f07331d49324236 Mon Sep 17 00:00:00 2001 From: Roy Munge Date: Wed, 16 Feb 2022 16:47:01 +0300 Subject: [PATCH 5/5] revert version name no code changes. Just tests --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 681fcad5a..bd99fbe08 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=0.7.3-ALPHA3-SNAPSHOT +VERSION_NAME=0.7.3-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Child Library