From ecac5b3571db6de90698faa150eee820a281fbb3 Mon Sep 17 00:00:00 2001 From: Juan D Date: Wed, 15 Nov 2023 15:35:41 +0100 Subject: [PATCH] PIA-937: Update unit tests --- app/build.gradle | 12 ++-- .../pia/handler/PIAServerHandlerTest.kt | 58 ++++++++++++++++--- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8612fe8..1af20f0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,13 +54,13 @@ dependencies { // Tests testImplementation 'com.github.gmazzo:okhttp-mock:1.3.2' - testImplementation "org.json:json:20180813" + testImplementation "org.json:json:20231013" testImplementation 'junit:junit:4.13.2' - testImplementation 'androidx.test:core:1.4.0' - testImplementation 'org.robolectric:robolectric:4.4' - testImplementation 'org.mockito:mockito-inline:2.23.0' - testImplementation "org.mockito.kotlin:mockito-kotlin:3.2.0" - testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0" + testImplementation 'androidx.test:core:1.5.0' + testImplementation 'org.robolectric:robolectric:4.10.3' + testImplementation 'org.mockito:mockito-inline:4.4.0' + testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0" + testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" androidTestImplementation 'com.github.gmazzo:okhttp-mock:1.3.2' androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:core:1.4.0' diff --git a/app/src/test/java/com/privateinternetaccess/android/pia/handler/PIAServerHandlerTest.kt b/app/src/test/java/com/privateinternetaccess/android/pia/handler/PIAServerHandlerTest.kt index 5d6f256..428de1e 100644 --- a/app/src/test/java/com/privateinternetaccess/android/pia/handler/PIAServerHandlerTest.kt +++ b/app/src/test/java/com/privateinternetaccess/android/pia/handler/PIAServerHandlerTest.kt @@ -20,35 +20,65 @@ package com.privateinternetaccess.android.pia.handler import android.content.Context import androidx.test.core.app.ApplicationProvider +import com.privateinternetaccess.account.AndroidAccountAPI +import com.privateinternetaccess.android.pia.PIAFactory import com.privateinternetaccess.android.pia.handlers.PIAServerHandler +import com.privateinternetaccess.android.pia.impl.AccountImpl +import com.privateinternetaccess.android.pia.interfaces.IAccount +import com.privateinternetaccess.android.pia.interfaces.IFactory +import com.privateinternetaccess.android.pia.interfaces.IVPN import com.privateinternetaccess.android.pia.utils.Prefs import com.privateinternetaccess.android.utils.KeyStoreUtils import com.privateinternetaccess.regions.RegionLowerLatencyInformation import com.privateinternetaccess.regions.RegionsAPI -import com.privateinternetaccess.regions.model.RegionsResponse +import com.privateinternetaccess.regions.model.ShadowsocksRegionsResponse +import com.privateinternetaccess.regions.model.VpnRegionsResponse +import org.junit.After import org.junit.Assert import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.MockedStatic import org.mockito.Mockito import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations import org.mockito.kotlin.any +import org.mockito.kotlin.mock import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) class PIAServerHandlerTest { private lateinit var context: Context + private lateinit var mockedStatic: MockedStatic @Before fun setup() { MockitoAnnotations.initMocks(this) context = ApplicationProvider.getApplicationContext() Prefs.setKeyStoreUtils(Mockito.mock(KeyStoreUtils::class.java)) + + val accountMock = mock() + Mockito.`when`(accountMock.loggedIn()).thenReturn(true) + + val vpnMock = mock() + Mockito.`when`(vpnMock.isVPNActive()).thenReturn(false) + + val factoryMock = mock() + Mockito.`when`(factoryMock.getAccount(context)).thenReturn(accountMock) + Mockito.`when`(factoryMock.getVPN(context)).thenReturn(vpnMock) + + mockedStatic = Mockito.mockStatic(PIAFactory::class.java) + mockedStatic.`when` { PIAFactory.getInstance() }.thenReturn(factoryMock) + PIAServerHandler.releaseInstance() } + @After + fun cleanup() { + mockedStatic.close() + } + @Test fun testGen4WorkersAllocation() { PIAServerHandler.getInstance(context).fetchServers(context, true) @@ -62,7 +92,7 @@ class PIAServerHandlerTest { PIAServerHandler.setRegionModule(regionsSpy) PIAServerHandler.getInstance(context) - verify(regionsSpy).fetchRegions(any(), any()) + verify(regionsSpy).fetchVpnRegions(any(), any()) } @Test @@ -71,7 +101,7 @@ class PIAServerHandlerTest { PIAServerHandler.setRegionModule(regionsSpy) PIAServerHandler.getInstance(context) - verify(regionsSpy).fetchRegions(any(), any()) + verify(regionsSpy).fetchVpnRegions(any(), any()) } @Test @@ -88,15 +118,27 @@ class PIAServerHandlerTest { private class MockRegionsApi(private val mockResponse: Boolean) : RegionsAPI { - override fun fetchRegions(locale: String, callback: (response: RegionsResponse?, List) -> Unit) { + override fun fetchShadowsocksRegions( + locale: String, + callback: (response: List, error: Error?) -> Unit + ) { + callback(emptyList(), null) + } + + override fun fetchVpnRegions( + locale: String, + callback: (response: VpnRegionsResponse?, Error?) -> Unit + ) { if (mockResponse) { - callback(Mockito.mock(RegionsResponse::class.java), emptyList()) + callback(Mockito.mock(VpnRegionsResponse::class.java), null) } else { - callback(null, listOf(Error("Tests"))) + callback(null, Error("Tests")) } } - override fun pingRequests(callback: (response: List, List) -> Unit) { - callback(emptyList(), emptyList()) + override fun pingRequests( + callback: (response: List, Error?) -> Unit + ) { + callback(emptyList(), null) } } \ No newline at end of file