Add AccountUtil Unit Tests (#4814)

This commit is contained in:
Madhur Gupta 2022-02-15 15:10:13 +05:30 committed by GitHub
parent dcf3aa8670
commit fd0dbf4cfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,33 @@
package fr.free.nrw.commons
import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.content.ContextWrapper
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
class FakeContextWrapper(base: Context?) : ContextWrapper(base) {
@Mock
private lateinit var mMockAccountManager: AccountManager
override fun getSystemService(name: String): Any {
return if (ACCOUNT_SERVICE == name) {
mMockAccountManager
} else super.getSystemService(name)
}
companion object {
private val ACCOUNT = Account("test@example.com", BuildConfig.ACCOUNT_TYPE)
private val ACCOUNTS = arrayOf(ACCOUNT)
}
init {
MockitoAnnotations.initMocks(this)
Mockito.`when`(mMockAccountManager.accounts).thenReturn(ACCOUNTS)
Mockito.`when`(mMockAccountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE))
.thenReturn(ACCOUNTS)
}
}

View file

@ -0,0 +1,26 @@
package fr.free.nrw.commons
import android.accounts.AccountManager
import android.content.Context
import android.content.ContextWrapper
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
class FakeContextWrapperWithException(base: Context?) : ContextWrapper(base) {
@Mock
private lateinit var mMockAccountManager: AccountManager
override fun getSystemService(name: String): Any {
return if (ACCOUNT_SERVICE == name) {
mMockAccountManager
} else super.getSystemService(name)
}
init {
MockitoAnnotations.initMocks(this)
Mockito.`when`(mMockAccountManager.getAccountsByType(BuildConfig.ACCOUNT_TYPE))
.thenThrow(SecurityException("Permission Denied"))
}
}

View file

@ -0,0 +1,61 @@
package fr.free.nrw.commons.auth
import fr.free.nrw.commons.FakeContextWrapper
import fr.free.nrw.commons.FakeContextWrapperWithException
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.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
class AccountUtilUnitTest {
private lateinit var context: FakeContextWrapper
private lateinit var accountUtil: AccountUtil
@Before
@Throws(Exception::class)
fun setUp() {
context = FakeContextWrapper(RuntimeEnvironment.application.applicationContext)
accountUtil = AccountUtil()
}
@Test
@Throws(Exception::class)
fun checkNotNull() {
Assert.assertNotNull(accountUtil)
}
@Test
@Throws(Exception::class)
fun testGetUserName() {
Assert.assertEquals(AccountUtil.getUserName(context), "test@example.com")
}
@Test
@Throws(Exception::class)
fun testGetUserNameWithException() {
val context =
FakeContextWrapperWithException(RuntimeEnvironment.application.applicationContext)
Assert.assertEquals(AccountUtil.getUserName(context), null)
}
@Test
@Throws(Exception::class)
fun testAccount() {
Assert.assertEquals(AccountUtil.account(context)?.name, "test@example.com")
}
@Test
@Throws(Exception::class)
fun testAccountWithException() {
val context =
FakeContextWrapperWithException(RuntimeEnvironment.application.applicationContext)
Assert.assertEquals(AccountUtil.account(context), null)
}
}