Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 475c5149 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[Catalyst] Allow null client for MessengerServiceRule" into main

parents 760ebedb 9b54f10c
Loading
Loading
Loading
Loading
+5 −2
Original line number Original line Diff line number Diff line
@@ -35,13 +35,16 @@ import org.robolectric.android.controller.ServiceController
/** Rule for messenger service testing. */
/** Rule for messenger service testing. */
open class MessengerServiceRule<C : MessengerServiceClient>(
open class MessengerServiceRule<C : MessengerServiceClient>(
    private val serviceClass: Class<out MessengerService>,
    private val serviceClass: Class<out MessengerService>,
    val client: C,
    private val nullableClient: C? = null,
) : TestWatcher() {
) : TestWatcher() {
    val application: Application = ApplicationProvider.getApplicationContext()
    val application: Application = ApplicationProvider.getApplicationContext()
    val isRobolectric = Build.FINGERPRINT.contains("robolectric")
    val isRobolectric = Build.FINGERPRINT.contains("robolectric")


    private var serviceController: ServiceController<out Service>? = null
    private var serviceController: ServiceController<out Service>? = null


    val client: C
        get() = nullableClient!!

    init {
    init {
        if (isRobolectric) {
        if (isRobolectric) {
            // `starting` is invoked by `startingQuietly`, which swallows exception. Hence we must
            // `starting` is invoked by `startingQuietly`, which swallows exception. Hence we must
@@ -59,7 +62,7 @@ open class MessengerServiceRule<C : MessengerServiceClient>(
    }
    }


    override fun finished(description: Description) {
    override fun finished(description: Description) {
        client.close()
        nullableClient?.close()
        if (isRobolectric) {
        if (isRobolectric) {
            runBlocking {
            runBlocking {
                withContext(Dispatchers.Main) { serviceController?.run { unbind().destroy() } }
                withContext(Dispatchers.Main) { serviceController?.run { unbind().destroy() } }