From c7ad9b9ba835029d87b162ef08c9f4a0fbfc73c4 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 15 Dec 2020 15:19:29 +0500 Subject: [PATCH] Added tests --- opensrp-chw-hf/build.gradle | 2 +- .../chw/hf/BaseActivityUnitTest.java | 34 +++ .../smartregister/chw/hf/BaseUnitTest.java | 14 ++ .../chw/hf/activity/LoginActivityTest.java | 228 ++++++++++++++++++ 4 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseActivityUnitTest.java create mode 100644 opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseUnitTest.java create mode 100644 opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/activity/LoginActivityTest.java diff --git a/opensrp-chw-hf/build.gradle b/opensrp-chw-hf/build.gradle index 18e18c10f..bbd6c95d7 100644 --- a/opensrp-chw-hf/build.gradle +++ b/opensrp-chw-hf/build.gradle @@ -166,7 +166,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.android.support:multidex:1.0.3' //Do not upgrade to 24.jre-1 due to compatibility issues - implementation 'com.google.guava:guava:20.0' + implementation 'com.google.guava:guava:24.1-jre' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseActivityUnitTest.java b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseActivityUnitTest.java new file mode 100644 index 000000000..3b8d845c1 --- /dev/null +++ b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseActivityUnitTest.java @@ -0,0 +1,34 @@ +package org.smartregister.chw.hf; + +import android.app.Activity; +import android.content.Intent; + +import org.junit.Assert; +import org.robolectric.android.controller.ActivityController; +import org.robolectric.shadows.ShadowApplication; + +import timber.log.Timber; + +public abstract class BaseActivityUnitTest extends BaseUnitTest { + + protected void destroyController() { + try { + getActivity().finish(); + getActivityController().pause().stop().destroy(); //destroy controller if we can + + } catch (Exception e) { + Timber.e(e); + } + System.gc(); + } + + protected abstract Activity getActivity(); + + protected abstract ActivityController getActivityController(); + + protected void assertActivityStarted(Activity currActivity, Activity nextActivity) { + Intent expectedIntent = new Intent(currActivity, nextActivity.getClass()); + Intent actual = ShadowApplication.getInstance().getNextStartedActivity(); + Assert.assertEquals(expectedIntent.getComponent(), actual.getComponent()); + } +} diff --git a/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseUnitTest.java b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseUnitTest.java new file mode 100644 index 000000000..3c318f589 --- /dev/null +++ b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/BaseUnitTest.java @@ -0,0 +1,14 @@ +package org.smartregister.chw.hf; + +import android.os.Build; + +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@RunWith (RobolectricTestRunner.class) +@Config(application = HealthFacilityApplication.class, sdk = Build.VERSION_CODES.P) +public abstract class BaseUnitTest { + protected static final String DUMMY_USERNAME = "myusername"; + protected static final String DUMMY_PASSWORD = "mypassword"; +} diff --git a/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/activity/LoginActivityTest.java b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/activity/LoginActivityTest.java new file mode 100644 index 000000000..bb3e5fde7 --- /dev/null +++ b/opensrp-chw-hf/src/test/java/org/smartregister/chw/hf/activity/LoginActivityTest.java @@ -0,0 +1,228 @@ +package org.smartregister.chw.hf.activity; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.view.Menu; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; + +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.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.android.controller.ActivityController; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.chw.hf.BaseActivityUnitTest; +import org.smartregister.chw.hf.R; +import org.smartregister.view.contract.BaseLoginContract; + +public class LoginActivityTest extends BaseActivityUnitTest { + + private static final String STRING_SETTINGS = "Settings"; + private LoginActivity loginActivity; + private ActivityController controller; + + @Mock + private Menu menu; + + @Mock + private BaseLoginContract.Presenter presenter; + + @Mock + private ProgressDialog progressDialog; + + @Mock + private Button loginButton; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + controller = Robolectric.buildActivity(LoginActivity.class).create().start(); + loginActivity = controller.get(); + } + + @After + public void tearDown() { + destroyController(); + } + + @Test + public void testUserNameEditTextIsInitialized() { + EditText userNameEditText = ReflectionHelpers.getField(loginActivity, "userNameEditText"); + Assert.assertNotNull(userNameEditText); + } + + @Test + public void testPasswordEditTextIsInitialized() { + EditText userPasswordEditText = ReflectionHelpers.getField(loginActivity, "passwordEditText"); + Assert.assertNotNull(userPasswordEditText); + } + + + @Test + public void testShowPasswordCheckBoxIsInitialized() { + CheckBox showPasswordCheckBox = ReflectionHelpers.getField(loginActivity, "showPasswordCheckBox"); + Assert.assertNotNull(showPasswordCheckBox); + } + + @Test + public void testProgressDialogIsInitialized() { + ProgressDialog progressDialog = ReflectionHelpers.getField(loginActivity, "progressDialog"); + Assert.assertNotNull(progressDialog); + } + + + @Test + public void testOnCreateOptionsMenuShouldAddSettingsItem() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + spyActivity.onCreateOptionsMenu(menu); + Mockito.verify(menu).add(STRING_SETTINGS); + } + + + @Test + public void testOnDestroyShouldCallOnDestroyPresenterMethod() { + + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + spyActivity.onDestroy(); + Mockito.verify(presenter).onDestroy(Mockito.anyBoolean()); + } + + @Test + public void testShowProgressShouldShowProgressDialogWhenParamIsTrue() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "progressDialog", progressDialog); + spyActivity.showProgress(true); + Mockito.verify(progressDialog).show(); + } + + @Test + public void testShowProgressShouldDismissProgressDialogWhenParamIsFalse() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "progressDialog", progressDialog); + spyActivity.showProgress(false); + Mockito.verify(progressDialog).dismiss(); + } + + @Test + public void testEnableLoginShouldCallLoginButtonSetClickableMethodWithCorrectParameter() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "loginButton", loginButton); + spyActivity.enableLoginButton(false); + Mockito.verify(loginButton).setClickable(Mockito.anyBoolean()); + } + + @Test + public void testOnEditorActionShouldCallAttemptLoginMethodFromPresenterIfActionIsEnter() { + + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + + Mockito.verify(presenter, Mockito.times(0)).attemptLogin(DUMMY_USERNAME, DUMMY_PASSWORD); + + EditText userNameEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + userNameEditText.setText(DUMMY_USERNAME); + + EditText passwordEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + passwordEditText.setText(DUMMY_PASSWORD); + } + + @Test + public void testOnClickShouldInvokeAttemptLoginPresenterMethodIfLoginButtonClicked() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + spyActivity.onClick(new View(ApplicationProvider.getApplicationContext()));//default + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + EditText editTextUsername = spyActivity.findViewById(R.id.login_user_name_edit_text); + editTextUsername.setText(DUMMY_USERNAME); + EditText editTextPassword = spyActivity.findViewById(R.id.login_password_edit_text); + editTextPassword.setText(DUMMY_PASSWORD); + Button loginButton = spyActivity.findViewById(R.id.login_login_btn); + spyActivity.onClick(loginButton); + Mockito.verify(presenter, Mockito.times(1)).attemptLogin(DUMMY_USERNAME, DUMMY_PASSWORD); + } + + @Test + public void testResetPasswordErrorShouldInvokeSetUsernameErrorWithNull() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + EditText passwordEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + ReflectionHelpers.setField(spyActivity, "passwordEditText", passwordEditText); + spyActivity.resetPaswordError(); + Mockito.verify(passwordEditText).setError(null); + } + + @Test + public void testSetPasswordErrorShouldShowErrorDialogWithCorrectMessage() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + EditText passwordEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + ReflectionHelpers.setField(spyActivity, "passwordEditText", passwordEditText); + Mockito.doNothing().when(spyActivity) + .showErrorDialog(ApplicationProvider.getApplicationContext().getString(R.string.unauthorized)); + spyActivity.setUsernameError(R.string.unauthorized); + Mockito.verify(spyActivity).showErrorDialog(ApplicationProvider.getApplicationContext().getString(R.string.unauthorized)); + } + + @Test + public void testSetUsernameErrorShouldShowErrorDialogWithCorrectMessage() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + EditText userNameEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + ReflectionHelpers.setField(spyActivity, "userNameEditText", userNameEditText); + Mockito.doNothing().when(spyActivity) + .showErrorDialog(ApplicationProvider.getApplicationContext().getString(R.string.unauthorized)); + spyActivity.setPasswordError(R.string.unauthorized); + Mockito.verify(spyActivity).showErrorDialog(ApplicationProvider.getApplicationContext().getString(R.string.unauthorized)); + } + + @Test + public void testResetUsernameErrorShouldInvokeSetUsernameErrorWithNull() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + ReflectionHelpers.setField(spyActivity, "mLoginPresenter", presenter); + EditText userNameEditText = Mockito.spy(new EditText(ApplicationProvider.getApplicationContext())); + ReflectionHelpers.setField(spyActivity, "userNameEditText", userNameEditText); + spyActivity.resetUsernameError(); + Mockito.verify(userNameEditText).setError(null); + } + + @Test + public void testGetActivityContextReturnsCorrectInstance() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + Assert.assertEquals(spyActivity, spyActivity.getActivityContext()); + } + + @Override + protected Activity getActivity() { + return loginActivity; + } + + @Override + protected ActivityController getActivityController() { + return controller; + } + + @Test + public void testGoToHomeRegisterWithRemoteTrue() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + spyActivity.goToHome(true); + assertActivityStarted(spyActivity, new FamilyRegisterActivity()); + } + + @Test + public void testGoToHomeRegisterWithRemote() { + LoginActivity spyActivity = Mockito.spy(loginActivity); + spyActivity.goToHome(false); + assertActivityStarted(spyActivity, new FamilyRegisterActivity()); + } + +} \ No newline at end of file