Skip to content

Commit

Permalink
PIA-937: Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-juan-docal committed Nov 15, 2023
1 parent 1a037d7 commit ecac5b3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PIAFactory>

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
context = ApplicationProvider.getApplicationContext()
Prefs.setKeyStoreUtils(Mockito.mock(KeyStoreUtils::class.java))

val accountMock = mock<IAccount>()
Mockito.`when`(accountMock.loggedIn()).thenReturn(true)

val vpnMock = mock<IVPN>()
Mockito.`when`(vpnMock.isVPNActive()).thenReturn(false)

val factoryMock = mock<IFactory>()
Mockito.`when`(factoryMock.getAccount(context)).thenReturn(accountMock)
Mockito.`when`(factoryMock.getVPN(context)).thenReturn(vpnMock)

mockedStatic = Mockito.mockStatic(PIAFactory::class.java)
mockedStatic.`when`<IFactory> { PIAFactory.getInstance() }.thenReturn(factoryMock)

PIAServerHandler.releaseInstance()
}

@After
fun cleanup() {
mockedStatic.close()
}

@Test
fun testGen4WorkersAllocation() {
PIAServerHandler.getInstance(context).fetchServers(context, true)
Expand All @@ -62,7 +92,7 @@ class PIAServerHandlerTest {
PIAServerHandler.setRegionModule(regionsSpy)

PIAServerHandler.getInstance(context)
verify(regionsSpy).fetchRegions(any(), any())
verify(regionsSpy).fetchVpnRegions(any(), any())
}

@Test
Expand All @@ -71,7 +101,7 @@ class PIAServerHandlerTest {
PIAServerHandler.setRegionModule(regionsSpy)

PIAServerHandler.getInstance(context)
verify(regionsSpy).fetchRegions(any(), any())
verify(regionsSpy).fetchVpnRegions(any(), any())
}

@Test
Expand All @@ -88,15 +118,27 @@ class PIAServerHandlerTest {

private class MockRegionsApi(private val mockResponse: Boolean) : RegionsAPI {

override fun fetchRegions(locale: String, callback: (response: RegionsResponse?, List<Error>) -> Unit) {
override fun fetchShadowsocksRegions(
locale: String,
callback: (response: List<ShadowsocksRegionsResponse>, 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<RegionLowerLatencyInformation>, List<Error>) -> Unit) {
callback(emptyList(), emptyList())
override fun pingRequests(
callback: (response: List<RegionLowerLatencyInformation>, Error?) -> Unit
) {
callback(emptyList(), null)
}
}

0 comments on commit ecac5b3

Please sign in to comment.