Add SettingsFragment Unit Tests (#4634)

* Add more Unit Tests for SettingsActivity

* Add SettingsFragment Unit Tests
This commit is contained in:
Madhur Gupta 2021-09-20 12:30:53 +05:30 committed by GitHub
parent 121119a358
commit e212caf30e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 155 additions and 7 deletions

View file

@ -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)
}
}

View file

@ -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")
}
}