diff --git a/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapper.kt b/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapper.kt new file mode 100644 index 000000000..0cacad347 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapper.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapperWithException.kt b/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapperWithException.kt new file mode 100644 index 000000000..9165b7b85 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/FakeContextWrapperWithException.kt @@ -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")) + } +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/auth/AccountUtilUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/auth/AccountUtilUnitTest.kt new file mode 100644 index 000000000..ca309344f --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/auth/AccountUtilUnitTest.kt @@ -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) + } +} \ No newline at end of file