From 272f59b2177a22baf36097277ded5dac8150849f Mon Sep 17 00:00:00 2001 From: Madhur Gupta <30932899+madhurgupta10@users.noreply.github.com> Date: Tue, 15 Feb 2022 15:09:50 +0530 Subject: [PATCH] Add tests for CommonsAppAdapter (#4818) * Add tests for CommonsAppAdapter * Remove robolectric dependency --- .../nrw/commons/CommonsAppAdapterUnitTest.kt | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/CommonsAppAdapterUnitTest.kt diff --git a/app/src/test/kotlin/fr/free/nrw/commons/CommonsAppAdapterUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/CommonsAppAdapterUnitTest.kt new file mode 100644 index 000000000..2faea3990 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/CommonsAppAdapterUnitTest.kt @@ -0,0 +1,110 @@ +package fr.free.nrw.commons + +import com.nhaarman.mockitokotlin2.verify +import com.nhaarman.mockitokotlin2.whenever +import fr.free.nrw.commons.auth.SessionManager +import fr.free.nrw.commons.kvstore.JsonKvStore +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.wikipedia.dataclient.SharedPreferenceCookieManager +import org.wikipedia.json.GsonMarshaller +import org.wikipedia.login.LoginResult + +class CommonsAppAdapterUnitTest { + + private lateinit var adapter: CommonsAppAdapter + + @Mock + private lateinit var sessionManager: SessionManager + + @Mock + private lateinit var preferences: JsonKvStore + + @Mock + private lateinit var result: LoginResult + + @Mock + private lateinit var cookies: SharedPreferenceCookieManager + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + adapter = CommonsAppAdapter(sessionManager, preferences) + } + + @Test + @Throws(Exception::class) + fun checkAdapterNotNull() { + Assert.assertNotNull(adapter) + } + + @Test + @Throws(Exception::class) + fun testGetMediaWikiBaseUrl() { + Assert.assertEquals(adapter.mediaWikiBaseUrl, BuildConfig.COMMONS_URL) + } + + @Test + @Throws(Exception::class) + fun testGetRestbaseUriFormat() { + Assert.assertEquals(adapter.restbaseUriFormat, BuildConfig.COMMONS_URL) + } + + @Test + @Throws(Exception::class) + fun testGetDesiredLeadImageDp() { + Assert.assertEquals(adapter.desiredLeadImageDp, 640) + } + + @Test + @Throws(Exception::class) + fun testIsLoggedIn() { + whenever(sessionManager.isUserLoggedIn).thenReturn(true) + Assert.assertEquals(adapter.isLoggedIn, true) + } + + @Test + @Throws(Exception::class) + fun testGetUserName() { + whenever(sessionManager.userName).thenReturn("test") + Assert.assertEquals(adapter.userName, "test") + } + + @Test + @Throws(Exception::class) + fun testGetPassword() { + whenever(sessionManager.password).thenReturn("test") + Assert.assertEquals(adapter.password, "test") + } + + @Test + @Throws(Exception::class) + fun testUpdateAccount() { + adapter.updateAccount(result) + verify(sessionManager).updateAccount(result) + } + + @Test + @Throws(Exception::class) + fun testSetCookies() { + adapter.cookies = cookies + verify(preferences).putString("cookie_store", GsonMarshaller.marshal(cookies)) + } + + @Test + @Throws(Exception::class) + fun testLogErrorsInsteadOfCrashing() { + Assert.assertEquals(adapter.logErrorsInsteadOfCrashing(), false) + } + + @Test + @Throws(Exception::class) + fun testGetCookiesCaseNull() { + whenever(preferences.contains("cookie_store")).thenReturn(false) + Assert.assertEquals(adapter.cookies, null) + } + +} \ No newline at end of file