From e212caf30e32d036278756ea223a02bf1557bfec Mon Sep 17 00:00:00 2001 From: Madhur Gupta <30932899+madhurgupta10@users.noreply.github.com> Date: Mon, 20 Sep 2021 12:30:53 +0530 Subject: [PATCH] Add SettingsFragment Unit Tests (#4634) * Add more Unit Tests for SettingsActivity * Add SettingsFragment Unit Tests --- .../settings/SettingsActivityUnitTests.kt | 39 +++++- .../settings/SettingsFragmentUnitTests.kt | 123 ++++++++++++++++++ 2 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsFragmentUnitTests.kt diff --git a/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsActivityUnitTests.kt b/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsActivityUnitTests.kt index 64552ebc8..20ea517be 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsActivityUnitTests.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsActivityUnitTests.kt @@ -1,37 +1,43 @@ package fr.free.nrw.commons.settings import android.content.Context +import android.os.Bundle +import android.os.Looper import android.view.MenuItem import fr.free.nrw.commons.TestCommonsApplication import org.junit.Assert import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.MockitoAnnotations import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows import org.robolectric.annotation.Config +import org.robolectric.annotation.LooperMode import org.robolectric.fakes.RoboMenuItem +import java.lang.reflect.Method @RunWith(RobolectricTestRunner::class) @Config(sdk = [21], application = TestCommonsApplication::class) +@LooperMode(LooperMode.Mode.PAUSED) class SettingsActivityUnitTests { private lateinit var activity: SettingsActivity - private lateinit var context: Context - private lateinit var menuItem: MenuItem + @Mock + private lateinit var savedInstanceState: Bundle + @Before fun setUp() { - + MockitoAnnotations.initMocks(this) context = RuntimeEnvironment.application.applicationContext - activity = Robolectric.buildActivity(SettingsActivity::class.java).create().get() - menuItem = RoboMenuItem(null) - } @Test @@ -48,8 +54,27 @@ class SettingsActivityUnitTests { @Test @Throws(Exception::class) - fun testOnOptionsItemSelected() { + fun testOnOptionsItemSelectedCaseDefault() { activity.onOptionsItemSelected(menuItem) } + @Test + @Throws(Exception::class) + fun testOnOptionsItemSelectedCaseHome() { + menuItem = RoboMenuItem(android.R.id.home) + activity.onOptionsItemSelected(menuItem) + } + + @Test + @Throws(Exception::class) + fun testSetTotalUploadCount() { + Shadows.shadowOf(Looper.getMainLooper()).idle() + val method: Method = SettingsActivity::class.java.getDeclaredMethod( + "onPostCreate", + Bundle::class.java + ) + method.isAccessible = true + method.invoke(activity, savedInstanceState) + } + } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsFragmentUnitTests.kt b/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsFragmentUnitTests.kt new file mode 100644 index 000000000..9d1b7172b --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/settings/SettingsFragmentUnitTests.kt @@ -0,0 +1,123 @@ +package fr.free.nrw.commons.settings + +import android.content.Context +import android.os.Looper +import android.view.LayoutInflater +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentTransaction +import fr.free.nrw.commons.TestCommonsApplication +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.MockitoAnnotations +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows +import org.robolectric.annotation.Config +import org.robolectric.annotation.LooperMode +import java.lang.reflect.Method + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [21], application = TestCommonsApplication::class) +@LooperMode(LooperMode.Mode.PAUSED) +class SettingsFragmentUnitTests { + + private lateinit var fragment: SettingsFragment + private lateinit var fragmentManager: FragmentManager + private lateinit var layoutInflater: LayoutInflater + private lateinit var context: Context + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + val activity = Robolectric.buildActivity(SettingsActivity::class.java).create().get() + context = RuntimeEnvironment.application.applicationContext + + fragment = SettingsFragment() + fragmentManager = activity.supportFragmentManager + val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() + fragmentTransaction.add(fragment, null) + fragmentTransaction.commitNowAllowingStateLoss() + + layoutInflater = LayoutInflater.from(activity) + } + + @Test + @Throws(Exception::class) + fun checkFragmentNotNull() { + Assert.assertNotNull(fragment) + } + + @Test + @Throws(Exception::class) + fun testRequestExternalStoragePermissions() { + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "requestExternalStoragePermissions" + ) + method.isAccessible = true + method.invoke(fragment) + } + + @Test + @Throws(Exception::class) + fun testTelemetryOptInOut() { + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "telemetryOptInOut", + Boolean::class.java + ) + method.isAccessible = true + method.invoke(fragment, true) + } + + @Test + @Throws(Exception::class) + fun testCheckPermissionsAndSendLogs() { + Shadows.shadowOf(Looper.getMainLooper()).idle() + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "checkPermissionsAndSendLogs" + ) + method.isAccessible = true + method.invoke(fragment) + } + + @Test + @Throws(Exception::class) + fun testGetCurrentLanguageCode() { + Shadows.shadowOf(Looper.getMainLooper()).idle() + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "getCurrentLanguageCode", + String::class.java + ) + method.isAccessible = true + method.invoke(fragment, "") + } + + @Test + @Throws(Exception::class) + fun testSaveLanguageValueCase_appUiDefaultLanguagePref() { + Shadows.shadowOf(Looper.getMainLooper()).idle() + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "saveLanguageValue", + String::class.java, + String::class.java + ) + method.isAccessible = true + method.invoke(fragment, "", "appUiDefaultLanguagePref") + } + + @Test + @Throws(Exception::class) + fun testSaveLanguageValueCase_descriptionDefaultLanguagePref() { + Shadows.shadowOf(Looper.getMainLooper()).idle() + val method: Method = SettingsFragment::class.java.getDeclaredMethod( + "saveLanguageValue", + String::class.java, + String::class.java + ) + method.isAccessible = true + method.invoke(fragment, "", "descriptionDefaultLanguagePref") + } + +} \ No newline at end of file