Do I have to do something special to run tests in ...
# compose
n
Do I have to do something special to run tests in compose? I wrote this simple test on androidTest folder:
Copy code
@RunWith(JUnit4::class)
class ExampleInstrumentedTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun testProductQuantity() {
        var quantity = 0

        composeTestRule.setContent {
            MaterialTheme {
                MyComponent(onUpdate = { quantity += it })
            }
        }
        findByTag("Product_Inc")
            .doClick()
            .doClick()

        runOnIdleCompose {
            assertEquals(quantity, 2)
        }
    }
}
When I press the run button, I can see the loading on the Run panel on Android Studio, but after a few seconds I get this message:
Copy code
java.lang.AssertionError: Activity never becomes requested state "[CREATED, STARTED, RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")
h
Could you share you
MyComponent
implementation?
I tried here and is everything fine.
Copy code
@Composable
fun MyComponent(onUpdate: (Int) -> Unit) {
    val (value, onChanged) = state { 0 }
    Button(
        modifier = Modifier.testTag("component_tag"),
        text = { Text("Clicke me") },
        onClick = {
            onChanged(value + 1)
            onUpdate(value)
        })
}
n
thanks @henrikhorbovyi. I don’t think it matters… I tried your code and get the same error… 😩
h
Do you think is it a lib error and not about the implementation?
n
not sure… which compose version you’re using? I’m on
dev14
h
Same here
😞 1
If you comment the assert the test still breaks?
Copy code
runOnIdleCompose {
    assertEquals(2, quantity)
}
So we can check if the problem is there
a
This might be a silly question, but is your test device or emulator screen off/screen locked?
n
@Adam Powell I’m using emulator API 29. And no, the screen is not locked
a
had to ask just in case 🙂
n
For future reference: I needed to add this on AndroidManifest.xml
Copy code
<activity android:name="androidx.activity.ComponentActivity" />
🤘 1