Skip to content

Commit

Permalink
modernize tests (#4777)
Browse files Browse the repository at this point in the history
- use `runTest` instead of `runBlocking`, where possible
- run all Robolectric tests on Api 34 (where we have most users)
- some new testcase for `TimestampUtilsTest`
- move our only instrumented Android Test, `MigrationsTest`, to unit
test so it runs in CI and expand it to test all migrations
- upgrade Robolectric
- removed truth and espresso as they are no longer needed
  • Loading branch information
connyduck authored Dec 3, 2024
1 parent 555ff1e commit 29914f8
Show file tree
Hide file tree
Showing 29 changed files with 554 additions and 360 deletions.
10 changes: 4 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ android {
}
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
// workaround to have migrations available in unit tests
// https://github.com/robolectric/robolectric/issues/3928#issuecomment-395309991
debug.assets.srcDirs += files("$projectDir/schemas".toString())
}

// Exclude unneeded files added by libraries
Expand Down Expand Up @@ -183,12 +185,8 @@ dependencies {
testImplementation libs.bundles.mockito
testImplementation libs.mockwebserver
testImplementation libs.androidx.core.testing
testImplementation libs.androidx.room.testing
testImplementation libs.kotlinx.coroutines.test
testImplementation libs.androidx.work.testing
testImplementation libs.truth
testImplementation libs.turbine

androidTestImplementation libs.espresso.core
androidTestImplementation libs.androidx.room.testing
androidTestImplementation libs.androidx.test.junit
}
66 changes: 0 additions & 66 deletions app/src/androidTest/java/com/keylesspalace/tusky/MigrationsTest.kt

This file was deleted.

49 changes: 0 additions & 49 deletions app/src/test/java/android/text/SpannableString.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/test/java/com/keylesspalace/tusky/FilterV1Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import org.robolectric.annotation.Config
import retrofit2.HttpException
import retrofit2.Response

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class FilterV1Test {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class StatusComparisonTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import retrofit2.Response
* Created by charlag on 3/7/18.
*/

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class ComposeActivityTest {
private lateinit var activity: ComposeActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package com.keylesspalace.tusky.components.compose

import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
Expand Down Expand Up @@ -89,7 +89,7 @@ class ComposeTokenizerTest(

@Test
fun tokenIndices_matchExpectations() {
Assert.assertEquals(expectedStartIndex, tokenizer.findTokenStart(text, text.length))
Assert.assertEquals(expectedEndIndex, tokenizer.findTokenEnd(text, text.length))
assertEquals(expectedStartIndex, tokenizer.findTokenStart(text, text.length))
assertEquals(expectedEndIndex, tokenizer.findTokenEnd(text, text.length))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.robolectric.annotation.Config

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class ComposeViewModelTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(sdk = [33])
@Config(sdk = [34])
class StatusLengthTest(
private val text: String,
private val expectedLength: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.robolectric.annotation.Config
import retrofit2.HttpException
import retrofit2.Response

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class NotificationsRemoteMediatorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.robolectric.annotation.Config
import retrofit2.HttpException
import retrofit2.Response

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class CachedTimelineRemoteMediatorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import androidx.paging.PagingSource
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.keylesspalace.tusky.components.timeline.viewmodel.NetworkTimelinePagingSource
import com.keylesspalace.tusky.components.timeline.viewmodel.NetworkTimelineViewModel
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.robolectric.annotation.Config

@Config(sdk = [28])
@Config(sdk = [34])
@RunWith(AndroidJUnit4::class)
class NetworkTimelinePagingSourceTest {

Expand All @@ -23,42 +23,36 @@ class NetworkTimelinePagingSourceTest {
}

@Test
fun `should return empty list when params are Append`() {
fun `should return empty list when params are Append`() = runTest {
val pagingSource = NetworkTimelinePagingSource(timelineViewModel)

val params = PagingSource.LoadParams.Append("132", 20, false)

val expectedResult = PagingSource.LoadResult.Page(emptyList(), null, null)

runBlocking {
assertEquals(expectedResult, pagingSource.load(params))
}
assertEquals(expectedResult, pagingSource.load(params))
}

@Test
fun `should return empty list when params are Prepend`() {
fun `should return empty list when params are Prepend`() = runTest {
val pagingSource = NetworkTimelinePagingSource(timelineViewModel)

val params = PagingSource.LoadParams.Prepend("132", 20, false)

val expectedResult = PagingSource.LoadResult.Page(emptyList(), null, null)

runBlocking {
assertEquals(expectedResult, pagingSource.load(params))
}
assertEquals(expectedResult, pagingSource.load(params))
}

@Test
fun `should return full list when params are Refresh`() {
fun `should return full list when params are Refresh`() = runTest {
val pagingSource = NetworkTimelinePagingSource(timelineViewModel)

val params = PagingSource.LoadParams.Refresh<String>(null, 20, false)

val expectedResult = PagingSource.LoadResult.Page(listOf(status), null, null)

runBlocking {
val result = pagingSource.load(params)
assertEquals(expectedResult, result)
}
val result = pagingSource.load(params)
assertEquals(expectedResult, result)
}
}
Loading

0 comments on commit 29914f8

Please sign in to comment.