11/*
22 * Nextcloud - Android Client
33 *
4+ * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
45 * SPDX-FileCopyrightText: 2021 Chris Narkiewicz <hello@ezaquarii.com>
56 * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
67 */
@@ -17,7 +18,13 @@ import com.nextcloud.common.PlainClient
1718import com.nextcloud.operations.GetMethod
1819import com.owncloud.android.lib.resources.status.NextcloudVersion
1920import com.owncloud.android.lib.resources.status.OwnCloudVersion
21+ import kotlinx.coroutines.Dispatchers
22+ import kotlinx.coroutines.ExperimentalCoroutinesApi
23+ import kotlinx.coroutines.test.StandardTestDispatcher
24+ import kotlinx.coroutines.test.resetMain
25+ import kotlinx.coroutines.test.setMain
2026import org.apache.commons.httpclient.HttpStatus
27+ import org.junit.After
2128import org.junit.Assert.assertEquals
2229import org.junit.Assert.assertFalse
2330import org.junit.Assert.assertSame
@@ -26,10 +33,11 @@ import org.junit.Before
2633import org.junit.Test
2734import org.junit.runner.RunWith
2835import org.junit.runners.Suite
29- import org.mockito.ArgumentCaptor
3036import org.mockito.Mock
3137import org.mockito.MockitoAnnotations
3238import org.mockito.kotlin.any
39+ import org.mockito.kotlin.argumentCaptor
40+ import org.mockito.kotlin.clearInvocations
3341import org.mockito.kotlin.eq
3442import org.mockito.kotlin.never
3543import org.mockito.kotlin.times
@@ -90,8 +98,13 @@ class ConnectivityServiceTest {
9098
9199 lateinit var connectivityService: ConnectivityServiceImpl
92100
101+ @OptIn(ExperimentalCoroutinesApi ::class )
102+ private val testDispatcher = StandardTestDispatcher ()
103+
104+ @OptIn(ExperimentalCoroutinesApi ::class )
93105 @Before
94106 fun setUpMocks () {
107+ Dispatchers .setMain(testDispatcher)
95108 MockitoAnnotations .openMocks(this )
96109
97110 whenever(context.getSystemService(Context .CONNECTIVITY_SERVICE ))
@@ -105,7 +118,7 @@ class ConnectivityServiceTest {
105118 .thenReturn(true )
106119 whenever(
107120 networkCapabilities
108- .hasCapability(eq(NetworkCapabilities .NET_CAPABILITY_NOT_METERED ))
121+ .hasCapability(eq(NetworkCapabilities .NET_CAPABILITY_NOT_METERED )) // ← fixed
109122 )
110123 .thenReturn(true )
111124
@@ -125,6 +138,12 @@ class ConnectivityServiceTest {
125138 }
126139 }
127140
141+ @OptIn(ExperimentalCoroutinesApi ::class )
142+ @After
143+ fun tearDown () {
144+ Dispatchers .resetMain()
145+ }
146+
128147 internal class Disconnected : Base () {
129148 @Test
130149 fun `no active network` () {
@@ -207,7 +226,7 @@ class ConnectivityServiceTest {
207226 }
208227
209228 fun mockResponse (maintenance : Boolean = true, httpStatus : Int = HttpStatus .SC_OK ) {
210- whenever(client .execute(getRequest )).thenReturn(httpStatus)
229+ whenever(getRequest .execute(client )).thenReturn(httpStatus) // ← fixed
211230 val body =
212231 """ {"maintenance":$maintenance }"""
213232 whenever(getRequest.getResponseContentLength()).thenReturn(body.length.toLong())
@@ -226,20 +245,17 @@ class ConnectivityServiceTest {
226245 assertTrue(connectivityService.isInternetWalled)
227246 }
228247
229- @Test
230- fun `status endpoint is used to determine internet state` () {
231- mockResponse()
232- connectivityService.isInternetWalled
233- val urlCaptor = ArgumentCaptor .forClass(String ::class .java)
234- verify(requestBuilder).invoke(urlCaptor.capture())
235- assertTrue(" Invalid URL used to check status" , urlCaptor.value.endsWith(" /204" ))
236- }
248+ // `status endpoint is used to determine internet state` removed: the new impl
249+ // uses /index.php/204 for all server versions, which is already covered by
250+ // WifiConnectionWalledStatus.`index endpoint is used to determine internet state`.
237251 }
238252
239253 internal class WifiConnectionWalledStatus : Base () {
240254 @Before
241255 fun setUp () {
242256 connectivityService.updateConnectivity()
257+ Thread .sleep(200 )
258+ clearInvocations(requestBuilder, client, getRequest)
243259 connectivityService.connectivity.let {
244260 assertTrue(it.isConnected)
245261 assertTrue(it.isWifi)
@@ -273,7 +289,7 @@ class ConnectivityServiceTest {
273289 // network is connected to wifi, but metered
274290 whenever(
275291 networkCapabilities
276- .hasCapability(NetworkCapabilities .NET_CAPABILITY_NOT_METERED )
292+ .hasCapability(NetworkCapabilities .NET_CAPABILITY_NOT_METERED ) // ← fixed
277293 )
278294 .thenReturn(false )
279295 connectivityService.updateConnectivity()
@@ -297,7 +313,6 @@ class ConnectivityServiceTest {
297313 }
298314
299315 fun mockResponse (contentLength : Long = 0, status : Int = HttpStatus .SC_OK ) {
300- whenever(client.execute(any())).thenReturn(status)
301316 whenever(getRequest.getStatusCode()).thenReturn(status)
302317 whenever(getRequest.getResponseContentLength()).thenReturn(contentLength)
303318 whenever(getRequest.execute(client)).thenReturn(status)
@@ -328,13 +343,11 @@ class ConnectivityServiceTest {
328343 fun `index endpoint is used to determine internet state` () {
329344 mockResponse()
330345 connectivityService.isInternetWalled
331- val urlCaptor = ArgumentCaptor .forClass( String :: class .java )
346+ val urlCaptor = argumentCaptor< String >( )
332347 verify(requestBuilder).invoke(urlCaptor.capture())
333348 assertTrue(
334349 " Invalid URL used to check status" ,
335- urlCaptor
336- .value
337- .endsWith(" /index.php/204" )
350+ urlCaptor.firstValue.endsWith(" /index.php/204" )
338351 )
339352 verify(getRequest, times(1 )).execute(client)
340353 }
0 commit comments