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

Commit 5ad8e9b0 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Thread check does not work in MessengerServiceRule

Bug: 338916345
Flag: TEST_ONLY
Test: manual
Change-Id: I38c01d8e96ea194e8a05fe8e41d8f5d78820b919
parent 15950b39
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -42,6 +42,16 @@ open class MessengerServiceRule<C : MessengerServiceClient>(

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

    init {
        if (isRobolectric) {
            // `starting` is invoked by `startingQuietly`, which swallows exception. Hence we must
            // check thread in constructor instead.
            check(Thread.currentThread() != Looper.getMainLooper().thread) {
                "To avoid deadlock, run test with @LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)"
            }
        }
    }

    override fun starting(description: Description) {
        if (isRobolectric) {
            runBlocking { setupRobolectricService() }
@@ -58,11 +68,6 @@ open class MessengerServiceRule<C : MessengerServiceClient>(
    }

    private suspend fun setupRobolectricService() {
        if (Thread.currentThread() == Looper.getMainLooper().thread) {
            throw IllegalStateException(
                "To avoid deadlock, run test with @LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)"
            )
        }
        withContext(Dispatchers.Main) {
            serviceController = Robolectric.buildService(serviceClass)
            val service = serviceController!!.create().get()