Skip to content

Commit 4220226

Browse files
committed
fix tests
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent b0a2092 commit 4220226

4 files changed

Lines changed: 34 additions & 18 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ dependencies {
396396
androidTestImplementation(libs.core.testing)
397397
// endregion
398398

399+
testImplementation(libs.kotlinx.coroutines.test)
400+
399401
// region other libraries
400402
compileOnly(libs.org.jbundle.util.osgi.wrapped.org.apache.http.client)
401403
implementation(libs.commons.httpclient.commons.httpclient) // remove after entire switch to lib v2

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ class FileUploadHelper {
648648
files: List<OCFile>,
649649
accountName: String
650650
): Pair<List<SyncedFolderEntity>, List<OCFile>> {
651-
652651
val autoUploadFolders = mutableListOf<SyncedFolderEntity>()
653652
val nonAutoUploadFiles = mutableListOf<OCFile>()
654653

app/src/test/java/com/nextcloud/client/network/ConnectivityServiceTest.kt

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
1718
import com.nextcloud.operations.GetMethod
1819
import com.owncloud.android.lib.resources.status.NextcloudVersion
1920
import 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
2026
import org.apache.commons.httpclient.HttpStatus
27+
import org.junit.After
2128
import org.junit.Assert.assertEquals
2229
import org.junit.Assert.assertFalse
2330
import org.junit.Assert.assertSame
@@ -26,10 +33,11 @@ import org.junit.Before
2633
import org.junit.Test
2734
import org.junit.runner.RunWith
2835
import org.junit.runners.Suite
29-
import org.mockito.ArgumentCaptor
3036
import org.mockito.Mock
3137
import org.mockito.MockitoAnnotations
3238
import org.mockito.kotlin.any
39+
import org.mockito.kotlin.argumentCaptor
40+
import org.mockito.kotlin.clearInvocations
3341
import org.mockito.kotlin.eq
3442
import org.mockito.kotlin.never
3543
import 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
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ junit = "4.13.2"
4949
junitVersion = "1.3.0"
5050
juniversalchardetVersion = "2.5.0"
5151
kotlin = "2.3.10"
52+
kotlinxCoroutinesTestVersion = "1.10.2"
5253
kotlinxSerializationJson = "1.10.0"
5354
ksp = "2.3.6"
5455
leakcanary = "2.14"
@@ -99,6 +100,7 @@ core-ktx = { module = "androidx.test:core-ktx", version.ref = "androidxTestVersi
99100
document-scanning-android-sdk = { module = "com.github.Hazzatur:Document-Scanning-Android-SDK", version.ref = "documentScannerVersion" }
100101
fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtxVersion" }
101102
exifinterface = { module = "androidx.exifinterface:exifinterface", version.ref = "exifinterfaceVersion" }
103+
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutinesTestVersion" }
102104
material-icons-core = { module = "androidx.compose.material:material-icons-core", version.ref = "materialIconsCoreVersion" }
103105
webkit = { module = "androidx.webkit:webkit", version.ref = "webkitVersion" }
104106
splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "splash-screen" }

0 commit comments

Comments
 (0)