Giter VIP home page Giter VIP logo

Comments (3)

eymar avatar eymar commented on September 26, 2024

Reproduced the on Desktop as well:

  • delay is not respected
  • while loop never stops

It makes sense because runComposeUiTest is implemented by SkikoComposeUiTest on all platforms except Android.

My undersanding of the behaviour:
SkikoComposeUiTest uses UnconfinedTestDispatcher() and it's applied to composition too. UnconfinedTestDispatcher doesn't follow the real time, it uses virtual time instead to allow "faster" tests for time-based behaviour. By defualt it skips delays.

As I remember, it was possible to disable autoAdvance of time and to manage it manually by adding:
mainClock.autoAdvance = false at the top of your test.

I tried it now, and indeed it makes the test complete. But it seems I can't manage the time manually: mainClock.advanceTimeBy(20000) - doesn't make the coroutine in LaunchedEffect resume its execution.

Perhaps @m-sasha, Could you please take a look here?
I think even if the workaround with disabling autoAdvance was helpful, it would still mean the code for android vs non-android tests is not identical?
I remember when porting some tests we indeed had to change the code a bit, but I'm not sure about current state of the tests API and its implementation.

from compose-multiplatform.

m-sasha avatar m-sasha commented on September 26, 2024

Everything seems to be working as expected here (tested with compose-plugin = "1.6.10").

delay is intentionally skipped in UI tests, on both Android and in Compose Multiplatform. On Android, the existence of suspended coroutines in a LaunchedEffect does not prevent waitForIdle from returning. Because of this, the test finishes on Android. I've reported this here, and it appears this has been reported by others as well.

Using mainClock.autoAdvance = false and mainClock.advanceTimeBy appears to also be working correctly. The code below prints once when run without mainClock.advanceTimeBy(11_000) and twice with.

internal class DelayTest {

    @OptIn(ExperimentalTestApi::class)
    @Test
    fun test_delay() = runComposeUiTest {
        mainClock.autoAdvance = false
        setContent {
            println("setContent ${getCurrentTime()}")
            var refreshKey by mutableStateOf(0)
            LaunchedEffect(Unit) {
                while (true) {
                    println("LaunchedEffect - key $refreshKey - ${getCurrentTime()}")
                    delay(10_000)
                    refreshKey++
                }
            }
            Text("Hello, World!")
            assertTrue(true)
        }

//        mainClock.advanceTimeBy(11_000)
    }

    private fun getCurrentTime(): Long {
        return Clock.System.now().toEpochMilliseconds()
    }
}

from compose-multiplatform.

okushnikov avatar okushnikov commented on September 26, 2024

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

from compose-multiplatform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.