diff --git a/gradle.properties b/gradle.properties index 0103686b7..39246ac47 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.3.0-SNAPSHOT +VERSION_NAME=3.3.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java index b582a6ed2..c832f8896 100644 --- a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java +++ b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java @@ -4,10 +4,10 @@ import android.accounts.AccountManager; import android.accounts.OnAccountsUpdateListener; import android.content.Intent; -import androidx.annotation.NonNull; +import android.text.TextUtils; + import androidx.annotation.Nullable; import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import android.text.TextUtils; import org.apache.commons.lang3.StringUtils; import org.smartregister.account.AccountAuthenticatorXml; @@ -57,19 +57,14 @@ public static void init(Context context) { } public static void init(Context context, SyncConfiguration syncConfiguration) { - if (instance == null) { - instance = new CoreLibrary(context, syncConfiguration, null); - } + init(context, syncConfiguration, BuildConfig.BUILD_TIMESTAMP); } public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp) { - if (instance == null) { - instance = new CoreLibrary(context, syncConfiguration, null); - buildTimeStamp = buildTimestamp; - } + init(context, syncConfiguration, buildTimestamp, null); } - public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @NonNull P2POptions options) { + public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @Nullable P2POptions options) { if (instance == null) { instance = new CoreLibrary(context, syncConfiguration, options); buildTimeStamp = buildTimestamp; diff --git a/opensrp-app/src/main/java/org/smartregister/util/Utils.java b/opensrp-app/src/main/java/org/smartregister/util/Utils.java index 147186fb6..3145d555c 100644 --- a/opensrp-app/src/main/java/org/smartregister/util/Utils.java +++ b/opensrp-app/src/main/java/org/smartregister/util/Utils.java @@ -546,7 +546,7 @@ public static void hideKeyboard(Context context, View view) { inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } } catch (Exception e) { - logError("Error encountered while hiding keyboard " + e); + Timber.e(e, "Error encountered while hiding keyboard"); } } diff --git a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java index 81a85d902..98df62040 100644 --- a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java @@ -18,6 +18,7 @@ import org.powermock.modules.junit4.rule.PowerMockRule; import org.powermock.reflect.Whitebox; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; @@ -65,10 +66,13 @@ public class ClientProcessorTest extends BaseUnitTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - CoreLibrary.init(context, new TestSyncConfiguration()); - when(context.detailsRepository()).thenReturn(detailsRepository); - when(coreLibrary.context()).thenReturn(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(new TestSyncConfiguration()).when(coreLibrary).getSyncConfiguration(); + doReturn(context).when(coreLibrary).context(); + doReturn("ec_client_fields.json").when(coreLibrary).getEcClientFieldsFile(); + + when(context.detailsRepository()).thenReturn(detailsRepository); when(context.commonrepository(Mockito.anyString())).thenReturn(cr); when(cr.executeInsertStatement(Mockito.any(ContentValues.class), Mockito.anyString())).thenReturn(1l); context.updateApplicationContext(RuntimeEnvironment.application); diff --git a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java index df6ab7a05..264ed2329 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java @@ -2,7 +2,6 @@ import android.app.Service; import android.content.Intent; -import android.content.res.Resources; import com.android.volley.RequestQueue; import com.android.volley.toolbox.HurlStack; @@ -22,11 +21,13 @@ import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; 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.util.mock.OpenSRPImageLoaderTestActivity; -import org.smartregister.view.activity.DrishtiApplication; + +import static org.mockito.Mockito.doReturn; /** * Created by kaderchowdhury on 14/11/17. @@ -39,12 +40,13 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest { public PowerMockRule rule = new PowerMockRule(); private OpenSRPImageLoaderTestActivity activity; + @Mock private Context context; + @Mock - Resources res; - @Mock - private DrishtiApplication drishtiApplication; + private CoreLibrary coreLibrary; + private ActivityController controller; @Before @@ -53,7 +55,8 @@ public void setUp() throws Exception { Intent intent = new Intent(RuntimeEnvironment.application, OpenSRPImageLoaderTestActivity.class); controller = Robolectric.buildActivity(OpenSRPImageLoaderTestActivity.class, intent); activity = controller.get(); - CoreLibrary.init(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(context).when(coreLibrary).context(); controller.setup(); } @@ -81,5 +84,4 @@ public void assertServiceConstructorInitializationNotNull() throws Exception { OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(Mockito.mock(Service.class), -1); Assert.assertNotNull(openSRPImageLoader); } - } diff --git a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java index 3f756cffc..2c9af2739 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java @@ -4,12 +4,14 @@ import android.content.pm.PackageManager; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.BaseUnitTest; import org.smartregister.CoreLibrary; import org.smartregister.domain.Setting; import org.smartregister.repository.AllSettings; @@ -28,9 +30,8 @@ * Created by Vincent Karuri on 10/03/2020 */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({Utils.class}) -public class SyncUtilsTest { +@PrepareForTest(Utils.class) +public class SyncUtilsTest extends BaseUnitTest { @Mock private Context context; @@ -43,10 +44,17 @@ public class SyncUtilsTest { private SyncUtils syncUtils; + @Rule + public PowerMockRule rule = new PowerMockRule(); + + @Mock + private CoreLibrary coreLibrary; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - CoreLibrary.init(opensrpContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(opensrpContext).when(coreLibrary).context(); syncUtils = new SyncUtils(context); doReturn(settingsRepository).when(opensrpContext).allSettings(); } diff --git a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java index 671d1e3b0..9ad262b03 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -1,8 +1,11 @@ package org.smartregister.util; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.view.View; +import android.view.inputmethod.InputMethodManager; import android.widget.TableRow; import androidx.test.core.app.ApplicationProvider; @@ -31,6 +34,7 @@ import org.smartregister.domain.jsonmapping.util.TeamMember; import org.smartregister.receiver.SyncStatusBroadcastReceiver; import org.smartregister.repository.AllSharedPreferences; +import org.smartregister.service.UserService; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -46,6 +50,16 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.smartregister.TestUtils.getContext; import static org.smartregister.util.Utils.getDefaultLocale; @@ -449,5 +463,34 @@ public void testGetAppIdShouldReturnAppId() { public void testGetAppVersionShouldReturnAppVersion() { assertNull(Utils.getAppVersion(RuntimeEnvironment.application)); } + + @Test + public void testLogoutUserShouldInvokeRequiredMethods() { + org.smartregister.Context opensrpContext = spy(CoreLibrary.getInstance().context()); + Context context = spy(opensrpContext.applicationContext()); + doReturn(context).when(opensrpContext).applicationContext(); + UserService mockUserService = mock(UserService.class); + doReturn(mockUserService).when(opensrpContext).userService(); + Utils.logoutUser(opensrpContext, "logged out"); + verify(mockUserService, times(1)).forceRemoteLogin(anyString()); + verify(mockUserService, times(1)).logoutSession(); + verify(context, times(1)).startActivity(any(Intent.class)); + } + + @Test + public void testHideKeyboardShouldInvokeRequireMethods() { + Activity activity = mock(Activity.class); + + View view = mock(View.class); + doReturn(view).when(activity).getCurrentFocus(); + + InputMethodManager keyboard = mock(InputMethodManager.class); + + doReturn(keyboard).when(activity).getSystemService(Context.INPUT_METHOD_SERVICE); + + Utils.hideKeyboard(activity); + + verify(keyboard, only()).hideSoftInputFromWindow(isNull(), eq(0)); + } } diff --git a/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java b/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java index 7896ec8f2..1f7132787 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java @@ -2,6 +2,8 @@ import android.os.Build; +import com.evernote.android.job.ShadowJobManager; + import net.sqlcipher.database.SQLiteDatabase; import org.junit.Assert; @@ -16,11 +18,11 @@ import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.TestSyncConfiguration; import org.smartregister.customshadows.FontTextViewShadow; import org.smartregister.repository.Repository; import org.smartregister.shadows.ShadowAppDatabase; import org.smartregister.shadows.ShadowDrawableResourcesImpl; -import com.evernote.android.job.ShadowJobManager; import org.smartregister.shadows.ShadowSQLiteDatabase; import org.smartregister.util.CredentialsHelper; @@ -115,7 +117,7 @@ public void onCreate() { context = Context.getInstance(); context.updateApplicationContext(getApplicationContext()); - CoreLibrary.init(context, null, 1588062490000l); + CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l); } } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java index 9af1064d1..3b38dbe6d 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java @@ -10,16 +10,18 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.smartregister.BaseUnitTest; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.R; import java.util.Arrays; +import static org.mockito.Mockito.doReturn; + @RunWith(PowerMockRunner.class) @PrepareForTest(CoreLibrary.class) -public class FPClientTest { +public class FPClientTest { @Mock private Context context; @@ -30,19 +32,18 @@ public class FPClientTest { CoreLibrary coreLibrary; @Before - public void setUp() { + public void setUp() { MockitoAnnotations.initMocks(this); - CoreLibrary.init(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(context).when(coreLibrary).context(); - PowerMockito.mockStatic(CoreLibrary.class); - PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary); Mockito.doReturn(context).when(coreLibrary).context(); fpClient = new FPClient("entity id 1", "woman name", "husband name", "village name", "ec no 1"); } @Test - public void shouldReturnTheReferralFollowUpAlert() { + public void shouldReturnTheReferralFollowUpAlert() { fpClient.withAlerts(Arrays.asList(new AlertDTO("FP Referral Followup", "normal", "2013-02-02") , new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02") @@ -69,7 +70,7 @@ public void shouldReturnTheReferralFollowUpAlert() { } @Test - public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() { + public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() { fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("FP Followup", "normal", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) @@ -89,7 +90,7 @@ public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExi } @Test - public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() { + public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() { fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) .withFPMethod("female_sterilization"); @@ -108,7 +109,7 @@ public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAler } @Test - public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() { + public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() { fpClient.withAlerts(Arrays.asList(new AlertDTO("Male Sterilization Followup", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) .withFPMethod("female_sterilization"); @@ -145,7 +146,7 @@ public void shouldSetCondomRefillDataOnlyIfFPMethodIsAlsoCondom() { } @Test - public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() { + public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() { fpClient.withAlerts(Arrays.asList(new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02") , new AlertDTO("Condom Refill", "urgent", "2013-02-02"))) .withFPMethod("condom"); diff --git a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java index 7c99c9f53..04c064c86 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java @@ -119,6 +119,9 @@ public class BaseRegisterFragmentTest extends BaseUnitTest { @Mock private LoaderManager loaderManager; + @Mock + private CoreLibrary coreLibrary; + @Before public void setUp() { @@ -138,8 +141,9 @@ public void setUp() { AppCompatActivity activitySpy = Mockito.spy(activity); doReturn(activitySpy).when(baseRegisterFragment).getActivity(); - ReflectionHelpers.setStaticField(CoreLibrary.class,"instance",null); - CoreLibrary.init(opensrpContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(opensrpContext).when(coreLibrary).context(); + doReturn(appProperties).when(opensrpContext).getAppProperties(); doReturn(opensrpContext).when(baseRegisterFragment).context(); @@ -266,7 +270,7 @@ public void assertOnQRCodeSucessfullyScannedInvokesFilterWithCorrectParams() { doReturn(activity).when(baseRegisterFragment).getActivity(); doReturn(loaderManager).when(baseRegisterFragment).getLoaderManager(); - doReturn(null).when(loaderManager).restartLoader(anyInt(),any(),any()); + doReturn(null).when(loaderManager).restartLoader(anyInt(), any(), any()); baseRegisterFragment.onQRCodeSucessfullyScanned(OPENSRP_ID); @@ -296,7 +300,7 @@ public void assertOnQRCodeSucessfullyScannedInvokessetUniqueIDWithCorrectParams( doReturn(activity).when(baseRegisterFragment).getActivity(); doReturn(loaderManager).when(baseRegisterFragment).getLoaderManager(); - doReturn(null).when(loaderManager).restartLoader(anyInt(),any(),any()); + doReturn(null).when(loaderManager).restartLoader(anyInt(), any(), any()); baseRegisterFragment.onQRCodeSucessfullyScanned(OPENSRP_ID); diff --git a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java index 33af1c121..6134a9bcc 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java @@ -4,6 +4,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.R; @@ -23,17 +24,22 @@ import java.util.List; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; public class PNCClientPreProcessorTest { @Mock - Context mockedContext; + private Context mockedContext; + + @Mock + private CoreLibrary coreLibrary; @Before public void setUp() throws Exception { initMocks(this); - CoreLibrary.init(mockedContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(mockedContext).when(coreLibrary).context(); when(mockedContext.getStringResource(R.string.str_pnc_circle_type_expected)).thenReturn("expected"); when(mockedContext.getStringResource(R.string.str_pnc_circle_type_actual)).thenReturn("actual"); }